1 24 25 package org.objectweb.cjdbc.controller.monitoring.datacollector; 26 27 import java.util.ArrayList ; 28 29 import javax.management.NotCompliantMBeanException ; 30 import javax.management.ObjectName ; 31 32 import org.objectweb.cjdbc.common.exceptions.DataCollectorException; 33 import org.objectweb.cjdbc.common.i18n.Translate; 34 import org.objectweb.cjdbc.common.jmx.JmxConstants; 35 import org.objectweb.cjdbc.common.jmx.JmxException; 36 import org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean; 37 import org.objectweb.cjdbc.common.log.Trace; 38 import org.objectweb.cjdbc.common.monitor.AbstractDataCollector; 39 import org.objectweb.cjdbc.common.monitor.DataCollection; 40 import org.objectweb.cjdbc.common.monitor.backend.ActiveConnectionsCollector; 41 import org.objectweb.cjdbc.common.monitor.backend.ActiveTransactionCollector; 42 import org.objectweb.cjdbc.common.monitor.backend.PendingRequestsCollector; 43 import org.objectweb.cjdbc.common.monitor.backend.ReadRequestsCollector; 44 import org.objectweb.cjdbc.common.monitor.backend.RequestsCollector; 45 import org.objectweb.cjdbc.common.monitor.backend.TransactionsCollector; 46 import org.objectweb.cjdbc.common.monitor.backend.WriteRequestsCollector; 47 import org.objectweb.cjdbc.common.monitor.cache.CacheEntriesCollector; 48 import org.objectweb.cjdbc.common.monitor.cache.CountHitsCollector; 49 import org.objectweb.cjdbc.common.monitor.cache.CountInsertCollector; 50 import org.objectweb.cjdbc.common.monitor.cache.CountSelectCollector; 51 import org.objectweb.cjdbc.common.monitor.cache.HitsRatioCollector; 52 import org.objectweb.cjdbc.common.monitor.client.ClientActiveTimeCollector; 53 import org.objectweb.cjdbc.common.monitor.controller.ControllerIdleThreadsCollector; 54 import org.objectweb.cjdbc.common.monitor.controller.ControllerWorkerPendingQueueCollector; 55 import org.objectweb.cjdbc.common.monitor.controller.ThreadsCountCollector; 56 import org.objectweb.cjdbc.common.monitor.controller.TotalMemoryCollector; 57 import org.objectweb.cjdbc.common.monitor.controller.UsedMemoryCollector; 58 import org.objectweb.cjdbc.common.monitor.scheduler.NumberReadCollector; 59 import org.objectweb.cjdbc.common.monitor.scheduler.NumberRequestsCollector; 60 import org.objectweb.cjdbc.common.monitor.scheduler.NumberWriteCollector; 61 import org.objectweb.cjdbc.common.monitor.scheduler.PendingTransactionsCollector; 62 import org.objectweb.cjdbc.common.monitor.scheduler.PendingWritesCollector; 63 import org.objectweb.cjdbc.common.monitor.virtualdatabase.ActiveDatabaseThreadCollector; 64 import org.objectweb.cjdbc.common.monitor.virtualdatabase.DatabaseThreadsCollector; 65 import org.objectweb.cjdbc.common.monitor.virtualdatabase.PendingDatabaseConnectionCollector; 66 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 67 import org.objectweb.cjdbc.controller.cache.result.AbstractResultCache; 68 import org.objectweb.cjdbc.controller.core.Controller; 69 import org.objectweb.cjdbc.controller.jmx.AbstractStandardMBean; 70 import org.objectweb.cjdbc.controller.jmx.MBeanServerManager; 71 import org.objectweb.cjdbc.controller.recoverylog.RecoveryLog; 72 import org.objectweb.cjdbc.controller.scheduler.AbstractScheduler; 73 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase; 74 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabaseWorkerThread; 75 76 84 public class DataCollector extends AbstractStandardMBean 85 implements 86 DataCollectorMBean 87 { 88 89 static Trace logger = Trace 90 .getLogger("org.objectweb.cjdbc.controller.jmx"); 91 Controller controller; 92 93 96 public static final String NO_CACHE_ENABLED = "No cache enabled"; 97 100 public static final String NOT_IMPLEMENTED = "Not Implemented"; 101 104 public static final String BACKEND_NOT_ACCESSIBLE = "Cannot reach backend"; 105 106 109 public static final String BACKEND_CANNOT_BE_DISABLED = "Backend cannot be disabled"; 110 111 114 public static final String CLIENT_NOT_FOUND = "Client not found"; 115 118 public static final String INVALID_COLLECTOR_TYPE = "Invalid Collector Type"; 119 122 public static final String AUTHENTICATION_FAILED = "Authentication failed"; 123 126 public static final String DATABASE_NOT_FOUND = "Database does not exists"; 127 128 137 public DataCollector(Controller controller) 138 throws NotCompliantMBeanException , JmxException 139 { 140 super(DataCollectorMBean.class); 141 this.controller = controller; 142 ObjectName objectName = JmxConstants.getDataCollectorObjectName(); 143 MBeanServerManager.registerMBean(this, objectName); 144 145 } 146 147 150 public String getAssociatedString() 151 { 152 return "datacollector"; 153 } 154 155 162 private VirtualDatabase getVirtualDatabase(String name) 163 throws DataCollectorException 164 { 165 VirtualDatabase vd = controller.getVirtualDatabase(name); 166 if (vd == null) 167 throw new DataCollectorException("Unknown Database"); 168 return vd; 169 } 170 171 174 public String [][] retrieveBackendsData() throws DataCollectorException 175 { 176 throw new DataCollectorException("Not Implemented"); 177 } 178 179 182 public String [][] retrieveBackendsData(String virtualDatabasename) 183 throws DataCollectorException 184 { 185 VirtualDatabase vdb = getVirtualDatabase(virtualDatabasename); 186 try 187 { 188 vdb.acquireReadLockBackendLists(); 189 } 190 catch (InterruptedException e) 191 { 192 String msg = Translate.get("virtualdatabase.fail.read.lock", e); 193 throw new DataCollectorException(msg); 194 } 195 ArrayList backends = vdb.getBackends(); 196 int backendListSize = backends.size(); 197 String [][] data = new String [backendListSize][]; 198 for (int i = 0; i < backendListSize; i++) 199 { 200 data[i] = ((DatabaseBackend) backends.get(i)).getBackendData(); 201 } 202 vdb.releaseReadLockBackendLists(); 203 return data; 204 } 205 206 209 public String [][] retrieveCacheData() throws DataCollectorException 210 { 211 throw new DataCollectorException(NOT_IMPLEMENTED); 212 } 213 214 217 public String [][] retrieveRecoveryLogData(String databaseName) 218 throws DataCollectorException 219 { 220 VirtualDatabase vdb = getVirtualDatabase(databaseName); 221 RecoveryLog log = vdb.getRequestManager().getRecoveryLog(); 222 if (log == null) 223 throw new DataCollectorException("Recovery log is not defined"); 224 return log.getData(); 225 } 226 227 230 public String [][] retrieveCacheData(String virtualDatabasename) 231 throws DataCollectorException 232 { 233 VirtualDatabase vdb = getVirtualDatabase(virtualDatabasename); 234 AbstractResultCache cache = vdb.getRequestManager().getResultCache(); 235 if (cache == null) 236 throw new DataCollectorException(NO_CACHE_ENABLED); 237 else 238 { 239 try 240 { 241 return cache.getCacheData(); 242 } 243 catch (Exception e) 244 { 245 throw new DataCollectorException(e.getMessage()); 246 } 247 } 248 } 249 250 253 public String [][] retrieveCacheStatsData() throws DataCollectorException 254 { 255 throw new DataCollectorException(NOT_IMPLEMENTED); 256 } 257 258 261 public String [][] retrieveCacheStatsData(String virtualDatabasename) 262 throws DataCollectorException 263 { 264 VirtualDatabase vdb = getVirtualDatabase(virtualDatabasename); 265 AbstractResultCache cache = vdb.getRequestManager().getResultCache(); 266 if (cache == null) 267 throw new DataCollectorException("No Cache enabled."); 268 else 269 { 270 try 271 { 272 return cache.getCacheStatsData(); 273 } 274 catch (Exception e) 275 { 276 throw new DataCollectorException(e.getMessage()); 277 } 278 } 279 } 280 281 284 public String [][] retrieveClientsData() throws DataCollectorException 285 { 286 throw new DataCollectorException("Not Implemented"); 287 } 288 289 292 public String [][] retrieveClientsData(String virtualDatabasename) 293 throws DataCollectorException 294 { 295 VirtualDatabase vdb = getVirtualDatabase(virtualDatabasename); 296 ArrayList activeThreads = vdb.getActiveThreads(); 297 int size = activeThreads.size(); 298 String [][] data = new String [size][]; 299 for (int i = 0; i < size; i++) 300 { 301 data[i] = ((VirtualDatabaseWorkerThread) activeThreads.get(i)) 302 .retrieveClientData(); 303 } 304 return data; 305 } 306 307 310 public String [][] retrieveControllerLoadData() 311 { 312 long total = Runtime.getRuntime().totalMemory(); 313 long free = Runtime.getRuntime().freeMemory(); 314 String [][] data = new String [1][6]; 315 data[0][0] = controller.getIPAddress(); 316 data[0][1] = String.valueOf(total / 1024 / 1024); 317 data[0][2] = String.valueOf((total - free) / 1024 / 1024); 318 data[0][3] = String.valueOf(Thread.activeCount()); 319 data[0][4] = String.valueOf(controller.getConnectionThread() 320 .getControllerServerThreadPendingQueueSize()); 321 data[0][5] = String.valueOf(controller.getConnectionThread() 322 .getIdleWorkerThreads()); 323 return data; 324 } 325 326 329 public String [][] retrieveSQLStats() throws DataCollectorException 330 { 331 throw new DataCollectorException("Not Implemented"); 332 } 333 334 337 public String [][] retrieveSQLStats(String virtualDatabasename) 338 throws DataCollectorException 339 { 340 VirtualDatabase vdb = getVirtualDatabase(virtualDatabasename); 341 if (vdb.getSQLMonitor() == null) 342 throw new DataCollectorException("No SQL monitoring enabled."); 343 else 344 return vdb.getSQLMonitor().getAllStatsInformation(); 345 } 346 347 350 public String [][] retrieveVirtualDatabasesData() 351 { 352 ArrayList dbs = controller.getVirtualDatabases(); 353 int size = dbs.size(); 354 String [][] data = new String [size][4]; 355 VirtualDatabase db; 356 for (int i = 0; i < size; i++) 357 { 358 db = (VirtualDatabase) dbs.get(i); 359 data[i][0] = db.getVirtualDatabaseName(); 360 data[i][1] = String.valueOf(db.getActiveThreads().size()); 361 data[i][2] = String.valueOf(db.getPendingConnections().size()); 362 data[i][3] = String.valueOf(db.getCurrentNbOfThreads()); 363 } 364 return data; 365 } 366 367 370 public Controller getController() 371 { 372 return controller; 373 } 374 375 378 public String [][] retrieveSchedulerData(String virtualDatabasename) 379 throws DataCollectorException 380 { 381 VirtualDatabase vdb = getVirtualDatabase(virtualDatabasename); 382 AbstractScheduler scheduler = vdb.getRequestManager().getScheduler(); 383 String [][] data = new String [1][]; 384 data[0] = scheduler.getSchedulerData(); 385 return data; 386 } 387 388 391 public AbstractDataCollector retrieveDataCollectorInstance(int dataType, 392 String targetName, String virtualDbName) throws DataCollectorException 393 { 394 switch (dataType) 395 { 396 399 case DataCollection.CONTROLLER_TOTAL_MEMORY : 400 return new TotalMemoryCollector(controller); 401 case DataCollection.CONTROLLER_USED_MEMORY : 402 return new UsedMemoryCollector(controller); 403 case DataCollection.CONTROLLER_WORKER_PENDING_QUEUE : 404 return new ControllerWorkerPendingQueueCollector(controller); 405 case DataCollection.CONTROLLER_THREADS_NUMBER : 406 return new ThreadsCountCollector(controller); 407 case DataCollection.CONTROLLER_IDLE_WORKER_THREADS : 408 return new ControllerIdleThreadsCollector(controller); 409 412 case DataCollection.BACKEND_ACTIVE_TRANSACTION : 413 return new ActiveTransactionCollector(targetName, virtualDbName); 414 case DataCollection.BACKEND_PENDING_REQUESTS : 415 return new PendingRequestsCollector(targetName, virtualDbName); 416 case DataCollection.BACKEND_TOTAL_ACTIVE_CONNECTIONS : 417 return new ActiveConnectionsCollector(targetName, virtualDbName); 418 case DataCollection.BACKEND_TOTAL_REQUEST : 419 return new RequestsCollector(targetName, virtualDbName); 420 case DataCollection.BACKEND_TOTAL_READ_REQUEST : 421 return new ReadRequestsCollector(targetName, virtualDbName); 422 case DataCollection.BACKEND_TOTAL_WRITE_REQUEST : 423 return new WriteRequestsCollector(targetName, virtualDbName); 424 case DataCollection.BACKEND_TOTAL_TRANSACTIONS : 425 return new TransactionsCollector(targetName, virtualDbName); 426 429 case DataCollection.DATABASES_ACTIVE_THREADS : 430 return new ActiveDatabaseThreadCollector(virtualDbName); 431 case DataCollection.DATABASES_PENDING_CONNECTIONS : 432 return new PendingDatabaseConnectionCollector(virtualDbName); 433 case DataCollection.DATABASES_NUMBER_OF_THREADS : 434 return new DatabaseThreadsCollector(virtualDbName); 435 438 case DataCollection.CACHE_STATS_COUNT_HITS : 439 return new CountHitsCollector(virtualDbName); 440 case DataCollection.CACHE_STATS_COUNT_INSERT : 441 return new CountInsertCollector(virtualDbName); 442 case DataCollection.CACHE_STATS_COUNT_SELECT : 443 return new CountSelectCollector(virtualDbName); 444 case DataCollection.CACHE_STATS_HITS_PERCENTAGE : 445 return new HitsRatioCollector(virtualDbName); 446 case DataCollection.CACHE_STATS_NUMBER_ENTRIES : 447 return new CacheEntriesCollector(virtualDbName); 448 451 case DataCollection.SCHEDULER_NUMBER_READ : 452 return new NumberReadCollector(virtualDbName); 453 case DataCollection.SCHEDULER_NUMBER_REQUESTS : 454 return new NumberRequestsCollector(virtualDbName); 455 case DataCollection.SCHEDULER_NUMBER_WRITES : 456 return new NumberWriteCollector(virtualDbName); 457 case DataCollection.SCHEDULER_PENDING_TRANSACTIONS : 458 return new PendingTransactionsCollector(virtualDbName); 459 case DataCollection.SCHEDULER_PENDING_WRITES : 460 return new PendingWritesCollector(virtualDbName); 461 464 case DataCollection.CLIENT_TIME_ACTIVE : 465 return new ClientActiveTimeCollector(virtualDbName, targetName); 466 467 470 default : 471 throw new DataCollectorException(INVALID_COLLECTOR_TYPE); 472 } 473 } 474 475 478 public long retrieveData(AbstractDataCollector collector) 479 throws DataCollectorException 480 { 481 collector.setController(controller); 484 return collector.collectValue(); 485 } 486 487 490 public boolean hasVirtualDatabase(String name) 491 { 492 return controller.hasVirtualDatabase(name); 493 } 494 } | Popular Tags |