KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > monitoring > datacollector > DataCollector


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): Emmanuel Cecchet.
23  */

24
25 package org.objectweb.cjdbc.controller.monitoring.datacollector;
26
27 import java.util.ArrayList JavaDoc;
28
29 import javax.management.NotCompliantMBeanException JavaDoc;
30 import javax.management.ObjectName JavaDoc;
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 /**
77  * This class implements retrieval of data to all possible objects in the
78  * controller or other c-jdbc components. It gets its interface from the
79  * corresponding MBean.
80  *
81  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
82  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
83  */

84 public class DataCollector extends AbstractStandardMBean
85     implements
86       DataCollectorMBean
87 {
88   /** Logger instance */
89   static Trace logger = Trace
90                                                             .getLogger("org.objectweb.cjdbc.controller.jmx");
91   Controller controller;
92
93   /**
94    * No cache enabled exception
95    */

96   public static final String JavaDoc NO_CACHE_ENABLED = "No cache enabled";
97   /**
98    * Not Implemented exception
99    */

100   public static final String JavaDoc NOT_IMPLEMENTED = "Not Implemented";
101   /**
102    * Cannot get access to backend exception
103    */

104   public static final String JavaDoc BACKEND_NOT_ACCESSIBLE = "Cannot reach backend";
105
106   /**
107    * Cannot disable backend exception
108    */

109   public static final String JavaDoc BACKEND_CANNOT_BE_DISABLED = "Backend cannot be disabled";
110
111   /**
112    * Client not found exception
113    */

114   public static final String JavaDoc CLIENT_NOT_FOUND = "Client not found";
115   /**
116    * Invalid Data type for collector
117    */

118   public static final String JavaDoc INVALID_COLLECTOR_TYPE = "Invalid Collector Type";
119   /**
120    * Authentication failed
121    */

122   public static final String JavaDoc AUTHENTICATION_FAILED = "Authentication failed";
123   /**
124    * Database not found
125    */

126   public static final String JavaDoc DATABASE_NOT_FOUND = "Database does not exists";
127
128   /**
129    * Create a new DataCollector associated to this controller.
130    *
131    * @param controller to collect data from
132    * @throws NotCompliantMBeanException - if the mbeanInterface does not follow
133    * JMX design patterns for Management Interfaces, or if this does
134    * not implement the specified interface.
135    * @exception JmxException the bean could not be registered
136    */

137   public DataCollector(Controller controller)
138       throws NotCompliantMBeanException JavaDoc, JmxException
139   {
140     super(DataCollectorMBean.class);
141     this.controller = controller;
142     ObjectName JavaDoc objectName = JmxConstants.getDataCollectorObjectName();
143     MBeanServerManager.registerMBean(this, objectName);
144
145   }
146
147   /**
148    * @see AbstractStandardMBean#getAssociatedString
149    */

150   public String JavaDoc getAssociatedString()
151   {
152     return "datacollector";
153   }
154
155   /**
156    * Try to get a virtual database from its name
157    *
158    * @param name of the virtual database
159    * @return virtual database object
160    * @throws DataCollectorException if does not exist
161    */

162   private VirtualDatabase getVirtualDatabase(String JavaDoc 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   /**
172    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveBackendsData()
173    */

174   public String JavaDoc[][] retrieveBackendsData() throws DataCollectorException
175   {
176     throw new DataCollectorException("Not Implemented");
177   }
178
179   /**
180    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveBackendsData(java.lang.String)
181    */

182   public String JavaDoc[][] retrieveBackendsData(String JavaDoc virtualDatabasename)
183       throws DataCollectorException
184   {
185     VirtualDatabase vdb = getVirtualDatabase(virtualDatabasename);
186     try
187     {
188       vdb.acquireReadLockBackendLists();
189     }
190     catch (InterruptedException JavaDoc e)
191     {
192       String JavaDoc msg = Translate.get("virtualdatabase.fail.read.lock", e);
193       throw new DataCollectorException(msg);
194     }
195     ArrayList JavaDoc backends = vdb.getBackends();
196     int backendListSize = backends.size();
197     String JavaDoc[][] data = new String JavaDoc[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   /**
207    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveCacheData()
208    */

209   public String JavaDoc[][] retrieveCacheData() throws DataCollectorException
210   {
211     throw new DataCollectorException(NOT_IMPLEMENTED);
212   }
213
214   /**
215    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveRecoveryLogData(java.lang.String)
216    */

217   public String JavaDoc[][] retrieveRecoveryLogData(String JavaDoc 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   /**
228    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveCacheData(java.lang.String)
229    */

230   public String JavaDoc[][] retrieveCacheData(String JavaDoc 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 JavaDoc e)
244       {
245         throw new DataCollectorException(e.getMessage());
246       }
247     }
248   }
249
250   /**
251    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveCacheStatsData()
252    */

253   public String JavaDoc[][] retrieveCacheStatsData() throws DataCollectorException
254   {
255     throw new DataCollectorException(NOT_IMPLEMENTED);
256   }
257
258   /**
259    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveCacheStatsData(java.lang.String)
260    */

261   public String JavaDoc[][] retrieveCacheStatsData(String JavaDoc 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 JavaDoc e)
275       {
276         throw new DataCollectorException(e.getMessage());
277       }
278     }
279   }
280
281   /**
282    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveClientsData()
283    */

284   public String JavaDoc[][] retrieveClientsData() throws DataCollectorException
285   {
286     throw new DataCollectorException("Not Implemented");
287   }
288
289   /**
290    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveClientsData(java.lang.String)
291    */

292   public String JavaDoc[][] retrieveClientsData(String JavaDoc virtualDatabasename)
293       throws DataCollectorException
294   {
295     VirtualDatabase vdb = getVirtualDatabase(virtualDatabasename);
296     ArrayList JavaDoc activeThreads = vdb.getActiveThreads();
297     int size = activeThreads.size();
298     String JavaDoc[][] data = new String JavaDoc[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   /**
308    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveControllerLoadData()
309    */

310   public String JavaDoc[][] retrieveControllerLoadData()
311   {
312     long total = Runtime.getRuntime().totalMemory();
313     long free = Runtime.getRuntime().freeMemory();
314     String JavaDoc[][] data = new String JavaDoc[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   /**
327    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveSQLStats()
328    */

329   public String JavaDoc[][] retrieveSQLStats() throws DataCollectorException
330   {
331     throw new DataCollectorException("Not Implemented");
332   }
333
334   /**
335    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveSQLStats()
336    */

337   public String JavaDoc[][] retrieveSQLStats(String JavaDoc 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   /**
348    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveVirtualDatabasesData()
349    */

350   public String JavaDoc[][] retrieveVirtualDatabasesData()
351   {
352     ArrayList JavaDoc dbs = controller.getVirtualDatabases();
353     int size = dbs.size();
354     String JavaDoc[][] data = new String JavaDoc[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   /**
368    * @return Returns the controller.
369    */

370   public Controller getController()
371   {
372     return controller;
373   }
374
375   /**
376    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveSchedulerData(java.lang.String)
377    */

378   public String JavaDoc[][] retrieveSchedulerData(String JavaDoc virtualDatabasename)
379       throws DataCollectorException
380   {
381     VirtualDatabase vdb = getVirtualDatabase(virtualDatabasename);
382     AbstractScheduler scheduler = vdb.getRequestManager().getScheduler();
383     String JavaDoc[][] data = new String JavaDoc[1][];
384     data[0] = scheduler.getSchedulerData();
385     return data;
386   }
387
388   /**
389    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveDataCollectorInstance
390    */

391   public AbstractDataCollector retrieveDataCollectorInstance(int dataType,
392       String JavaDoc targetName, String JavaDoc virtualDbName) throws DataCollectorException
393   {
394     switch (dataType)
395     {
396       /*
397        * Controller Collectors
398        */

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       /*
410        * Backend collectors
411        */

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       /*
427        * VirtualDatabase collectors
428        */

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       /*
436        * Cache stats collectors
437        */

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       /*
449        * Scheduler collectors
450        */

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       /*
462        * Client collectors
463        */

464       case DataCollection.CLIENT_TIME_ACTIVE :
465         return new ClientActiveTimeCollector(virtualDbName, targetName);
466
467       /*
468        * Unknown collector
469        */

470       default :
471         throw new DataCollectorException(INVALID_COLLECTOR_TYPE);
472     }
473   }
474
475   /**
476    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveData
477    */

478   public long retrieveData(AbstractDataCollector collector)
479       throws DataCollectorException
480   {
481     // Get the new value and return it
482
// recall reference to controller (is transient in collector)
483
collector.setController(controller);
484     return collector.collectValue();
485   }
486
487   /**
488    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#hasVirtualDatabase(java.lang.String)
489    */

490   public boolean hasVirtualDatabase(String JavaDoc name)
491   {
492     return controller.hasVirtualDatabase(name);
493   }
494 }
Popular Tags