Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

DataCollector.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2005 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk.
00022  * Contributor(s): Emmanuel Cecchet.
00023  */
00024 
00025 package org.objectweb.cjdbc.controller.monitoring.datacollector;
00026 
00027 import java.util.ArrayList;
00028 
00029 import javax.management.NotCompliantMBeanException;
00030 import javax.management.ObjectName;
00031 
00032 import org.objectweb.cjdbc.common.exceptions.DataCollectorException;
00033 import org.objectweb.cjdbc.common.exceptions.ExceptionTypes;
00034 import org.objectweb.cjdbc.common.i18n.Translate;
00035 import org.objectweb.cjdbc.common.jmx.JmxConstants;
00036 import org.objectweb.cjdbc.common.jmx.JmxException;
00037 import org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean;
00038 import org.objectweb.cjdbc.common.log.Trace;
00039 import org.objectweb.cjdbc.common.monitor.AbstractDataCollector;
00040 import org.objectweb.cjdbc.common.monitor.DataCollection;
00041 import org.objectweb.cjdbc.common.monitor.backend.ActiveConnectionsCollector;
00042 import org.objectweb.cjdbc.common.monitor.backend.ActiveTransactionCollector;
00043 import org.objectweb.cjdbc.common.monitor.backend.PendingRequestsCollector;
00044 import org.objectweb.cjdbc.common.monitor.backend.ReadRequestsCollector;
00045 import org.objectweb.cjdbc.common.monitor.backend.RequestsCollector;
00046 import org.objectweb.cjdbc.common.monitor.backend.TransactionsCollector;
00047 import org.objectweb.cjdbc.common.monitor.backend.WriteRequestsCollector;
00048 import org.objectweb.cjdbc.common.monitor.cache.CacheEntriesCollector;
00049 import org.objectweb.cjdbc.common.monitor.cache.CountHitsCollector;
00050 import org.objectweb.cjdbc.common.monitor.cache.CountInsertCollector;
00051 import org.objectweb.cjdbc.common.monitor.cache.CountSelectCollector;
00052 import org.objectweb.cjdbc.common.monitor.cache.HitsRatioCollector;
00053 import org.objectweb.cjdbc.common.monitor.client.ClientActiveTimeCollector;
00054 import org.objectweb.cjdbc.common.monitor.client.ClientBytesReadCollector;
00055 import org.objectweb.cjdbc.common.monitor.client.ClientBytesWrittenCollector;
00056 import org.objectweb.cjdbc.common.monitor.client.ClientReadingSpeedCollector;
00057 import org.objectweb.cjdbc.common.monitor.client.ClientWritingSpeedCollector;
00058 import org.objectweb.cjdbc.common.monitor.controller.ControllerIdleThreadsCollector;
00059 import org.objectweb.cjdbc.common.monitor.controller.ControllerWorkerPendingQueueCollector;
00060 import org.objectweb.cjdbc.common.monitor.controller.ThreadsCountCollector;
00061 import org.objectweb.cjdbc.common.monitor.controller.TotalMemoryCollector;
00062 import org.objectweb.cjdbc.common.monitor.controller.UsedMemoryCollector;
00063 import org.objectweb.cjdbc.common.monitor.scheduler.NumberReadCollector;
00064 import org.objectweb.cjdbc.common.monitor.scheduler.NumberRequestsCollector;
00065 import org.objectweb.cjdbc.common.monitor.scheduler.NumberWriteCollector;
00066 import org.objectweb.cjdbc.common.monitor.scheduler.PendingTransactionsCollector;
00067 import org.objectweb.cjdbc.common.monitor.scheduler.PendingWritesCollector;
00068 import org.objectweb.cjdbc.common.monitor.virtualdatabase.ActiveDatabaseThreadCollector;
00069 import org.objectweb.cjdbc.common.monitor.virtualdatabase.DatabaseThreadsCollector;
00070 import org.objectweb.cjdbc.common.monitor.virtualdatabase.PendingDatabaseConnectionCollector;
00071 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
00072 import org.objectweb.cjdbc.controller.cache.result.AbstractResultCache;
00073 import org.objectweb.cjdbc.controller.core.Controller;
00074 import org.objectweb.cjdbc.controller.jmx.AbstractStandardMBean;
00075 import org.objectweb.cjdbc.controller.jmx.MBeanServerManager;
00076 import org.objectweb.cjdbc.controller.recoverylog.AbstractRecoveryLog;
00077 import org.objectweb.cjdbc.controller.scheduler.AbstractScheduler;
00078 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase;
00079 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabaseWorkerThread;
00080 
00081 /**
00082  * This class implements retrieval of data to all possible objects in the
00083  * controller or other c-jdbc components. It gets its interface from the
00084  * corresponding MBean.
00085  * 
00086  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00087  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
00088  */
00089 public class DataCollector extends AbstractStandardMBean
00090     implements
00091       DataCollectorMBean,
00092       DataCollection
00093 {
00094   /** Logger instance */
00095   static Trace logger = Trace.getLogger("org.objectweb.cjdbc.controller.jmx");
00096   Controller   controller;
00097 
00098   /**
00099    * Create a new DataCollector associated to this controller.
00100    * 
00101    * @param controller to collect data from
00102    * @throws NotCompliantMBeanException - if the mbeanInterface does not follow
00103    *           JMX design patterns for Management Interfaces, or if this does
00104    *           not implement the specified interface.
00105    * @exception JmxException the bean could not be registered
00106    */
00107   public DataCollector(Controller controller)
00108       throws NotCompliantMBeanException, JmxException
00109   {
00110     super(DataCollectorMBean.class);
00111     this.controller = controller;
00112     ObjectName objectName = JmxConstants.getDataCollectorObjectName();
00113     MBeanServerManager.registerMBean(this, objectName);
00114 
00115   }
00116 
00117   /**
00118    * @see AbstractStandardMBean#getAssociatedString
00119    */
00120   public String getAssociatedString()
00121   {
00122     return "datacollector";
00123   }
00124 
00125   /**
00126    * Try to get a virtual database from its name
00127    * 
00128    * @param name of the virtual database
00129    * @return virtual database object
00130    * @throws DataCollectorException if does not exist
00131    */
00132   private VirtualDatabase getVirtualDatabase(String name)
00133       throws DataCollectorException
00134   {
00135     VirtualDatabase vd = controller.getVirtualDatabase(name);
00136     if (vd == null)
00137       throw new DataCollectorException("Unknown Database");
00138     return vd;
00139   }
00140 
00141   /**
00142    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveBackendsData()
00143    */
00144   public String[][] retrieveBackendsData() throws DataCollectorException
00145   {
00146     throw new DataCollectorException("Not Implemented");
00147   }
00148 
00149   /**
00150    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveBackendsData(java.lang.String)
00151    */
00152   public String[][] retrieveBackendsData(String virtualDatabasename)
00153       throws DataCollectorException
00154   {
00155     VirtualDatabase vdb = getVirtualDatabase(virtualDatabasename);
00156     try
00157     {
00158       vdb.acquireReadLockBackendLists();
00159     }
00160     catch (InterruptedException e)
00161     {
00162       String msg = Translate.get("virtualdatabase.fail.read.lock", e);
00163       throw new DataCollectorException(msg);
00164     }
00165     ArrayList backends = vdb.getBackends();
00166     int backendListSize = backends.size();
00167     String[][] data = new String[backendListSize][];
00168     for (int i = 0; i < backendListSize; i++)
00169     {
00170       data[i] = ((DatabaseBackend) backends.get(i)).getBackendData();
00171     }
00172     vdb.releaseReadLockBackendLists();
00173     return data;
00174   }
00175 
00176   /**
00177    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveCacheData()
00178    */
00179   public String[][] retrieveCacheData() throws DataCollectorException
00180   {
00181     throw new DataCollectorException(ExceptionTypes.NOT_IMPLEMENTED);
00182 
00183   }
00184 
00185   /**
00186    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveRecoveryLogData(java.lang.String)
00187    */
00188   public String[][] retrieveRecoveryLogData(String databaseName)
00189       throws DataCollectorException
00190   {
00191     VirtualDatabase vdb = getVirtualDatabase(databaseName);
00192     AbstractRecoveryLog log = vdb.getRequestManager().getRecoveryLog();
00193     if (log == null)
00194       throw new DataCollectorException(ExceptionTypes.NO_RECOVERY_LOG);
00195     return log.getData();
00196   }
00197 
00198   /**
00199    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveCacheData(java.lang.String)
00200    */
00201   public String[][] retrieveCacheData(String virtualDatabasename)
00202       throws DataCollectorException
00203   {
00204     VirtualDatabase vdb = getVirtualDatabase(virtualDatabasename);
00205     AbstractResultCache cache = vdb.getRequestManager().getResultCache();
00206     if (cache == null)
00207       throw new DataCollectorException(ExceptionTypes.NO_CACHE_ENABLED);
00208     else
00209     {
00210       try
00211       {
00212         return cache.getCacheData();
00213       }
00214       catch (Exception e)
00215       {
00216         throw new DataCollectorException(e.getMessage());
00217       }
00218     }
00219   }
00220 
00221   /**
00222    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveCacheStatsData()
00223    */
00224   public String[][] retrieveCacheStatsData() throws DataCollectorException
00225   {
00226     throw new DataCollectorException(ExceptionTypes.NOT_IMPLEMENTED);
00227   }
00228 
00229   /**
00230    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveCacheStatsData(java.lang.String)
00231    */
00232   public String[][] retrieveCacheStatsData(String virtualDatabasename)
00233       throws DataCollectorException
00234   {
00235     VirtualDatabase vdb = getVirtualDatabase(virtualDatabasename);
00236     AbstractResultCache cache = vdb.getRequestManager().getResultCache();
00237     if (cache == null)
00238       throw new DataCollectorException("No Cache enabled.");
00239     else
00240     {
00241       try
00242       {
00243         return cache.getCacheStatsData();
00244       }
00245       catch (Exception e)
00246       {
00247         throw new DataCollectorException(e.getMessage());
00248       }
00249     }
00250   }
00251 
00252   /**
00253    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveClientsData()
00254    */
00255   public String[][] retrieveClientsData() throws DataCollectorException
00256   {
00257     throw new DataCollectorException("Not Implemented");
00258   }
00259 
00260   /**
00261    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveClientsData(java.lang.String)
00262    */
00263   public String[][] retrieveClientsData(String virtualDatabasename)
00264       throws DataCollectorException
00265   {
00266     VirtualDatabase vdb = getVirtualDatabase(virtualDatabasename);
00267     ArrayList activeThreads = vdb.getActiveThreads();
00268     int size = activeThreads.size();
00269     String[][] data = new String[size][];
00270     for (int i = 0; i < size; i++)
00271     {
00272       data[i] = ((VirtualDatabaseWorkerThread) activeThreads.get(i))
00273           .retrieveClientData();
00274     }
00275     return data;
00276   }
00277 
00278   /**
00279    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveControllerLoadData()
00280    */
00281   public String[][] retrieveControllerLoadData()
00282   {
00283     long total = Runtime.getRuntime().totalMemory();
00284     long free = Runtime.getRuntime().freeMemory();
00285     String[][] data = new String[1][6];
00286     data[0][0] = controller.getIPAddress();
00287     data[0][1] = String.valueOf(total / 1024 / 1024);
00288     data[0][2] = String.valueOf((total - free) / 1024 / 1024);
00289     data[0][3] = String.valueOf(Thread.activeCount());
00290     data[0][4] = String.valueOf(controller.getConnectionThread()
00291         .getControllerServerThreadPendingQueueSize());
00292     data[0][5] = String.valueOf(controller.getConnectionThread()
00293         .getIdleWorkerThreads());
00294     return data;
00295   }
00296 
00297   /**
00298    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveSQLStats()
00299    */
00300   public String[][] retrieveSQLStats() throws DataCollectorException
00301   {
00302     throw new DataCollectorException("Not Implemented");
00303   }
00304 
00305   /**
00306    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveSQLStats()
00307    */
00308   public String[][] retrieveSQLStats(String virtualDatabasename)
00309       throws DataCollectorException
00310   {
00311     VirtualDatabase vdb = getVirtualDatabase(virtualDatabasename);
00312     if (vdb.getSQLMonitor() == null)
00313       throw new DataCollectorException("No SQL monitoring enabled.");
00314     else
00315       return vdb.getSQLMonitor().getAllStatsInformation();
00316   }
00317 
00318   /**
00319    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveVirtualDatabasesData()
00320    */
00321   public String[][] retrieveVirtualDatabasesData()
00322   {
00323     ArrayList dbs = controller.getVirtualDatabases();
00324     int size = dbs.size();
00325     String[][] data = new String[size][4];
00326     VirtualDatabase db;
00327     for (int i = 0; i < size; i++)
00328     {
00329       db = (VirtualDatabase) dbs.get(i);
00330       data[i][0] = db.getVirtualDatabaseName();
00331       data[i][1] = String.valueOf(db.getActiveThreads().size());
00332       data[i][2] = String.valueOf(db.getPendingConnections().size());
00333       data[i][3] = String.valueOf(db.getCurrentNbOfThreads());
00334     }
00335     return data;
00336   }
00337 
00338   /**
00339    * @return Returns the controller.
00340    */
00341   public Controller getController()
00342   {
00343     return controller;
00344   }
00345 
00346   /**
00347    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveSchedulerData(java.lang.String)
00348    */
00349   public String[][] retrieveSchedulerData(String virtualDatabasename)
00350       throws DataCollectorException
00351   {
00352     VirtualDatabase vdb = getVirtualDatabase(virtualDatabasename);
00353     AbstractScheduler scheduler = vdb.getRequestManager().getScheduler();
00354     String[][] data = new String[1][];
00355     data[0] = scheduler.getSchedulerData();
00356     return data;
00357   }
00358 
00359   /**
00360    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveDataCollectorInstance
00361    */
00362   public AbstractDataCollector retrieveDataCollectorInstance(int dataType,
00363       String targetName, String virtualDbName) throws DataCollectorException
00364   {
00365     switch (dataType)
00366     {
00367       /*
00368        * Controller Collectors
00369        */
00370       case CONTROLLER_TOTAL_MEMORY :
00371         return new TotalMemoryCollector(controller);
00372       case CONTROLLER_USED_MEMORY :
00373         return new UsedMemoryCollector(controller);
00374       case CONTROLLER_WORKER_PENDING_QUEUE :
00375         return new ControllerWorkerPendingQueueCollector(controller);
00376       case CONTROLLER_THREADS_NUMBER :
00377         return new ThreadsCountCollector(controller);
00378       case CONTROLLER_IDLE_WORKER_THREADS :
00379         return new ControllerIdleThreadsCollector(controller);
00380       /*
00381        * Backend collectors
00382        */
00383       case BACKEND_ACTIVE_TRANSACTION :
00384         return new ActiveTransactionCollector(targetName, virtualDbName);
00385       case BACKEND_PENDING_REQUESTS :
00386         return new PendingRequestsCollector(targetName, virtualDbName);
00387       case BACKEND_TOTAL_ACTIVE_CONNECTIONS :
00388         return new ActiveConnectionsCollector(targetName, virtualDbName);
00389       case BACKEND_TOTAL_REQUEST :
00390         return new RequestsCollector(targetName, virtualDbName);
00391       case BACKEND_TOTAL_READ_REQUEST :
00392         return new ReadRequestsCollector(targetName, virtualDbName);
00393       case BACKEND_TOTAL_WRITE_REQUEST :
00394         return new WriteRequestsCollector(targetName, virtualDbName);
00395       case BACKEND_TOTAL_TRANSACTIONS :
00396         return new TransactionsCollector(targetName, virtualDbName);
00397       /*
00398        * VirtualDatabase collectors
00399        */
00400       case DATABASES_ACTIVE_THREADS :
00401         return new ActiveDatabaseThreadCollector(virtualDbName);
00402       case DATABASES_PENDING_CONNECTIONS :
00403         return new PendingDatabaseConnectionCollector(virtualDbName);
00404       case DATABASES_NUMBER_OF_THREADS :
00405         return new DatabaseThreadsCollector(virtualDbName);
00406       /*
00407        * Cache stats collectors
00408        */
00409       case CACHE_STATS_COUNT_HITS :
00410         return new CountHitsCollector(virtualDbName);
00411       case CACHE_STATS_COUNT_INSERT :
00412         return new CountInsertCollector(virtualDbName);
00413       case CACHE_STATS_COUNT_SELECT :
00414         return new CountSelectCollector(virtualDbName);
00415       case CACHE_STATS_HITS_PERCENTAGE :
00416         return new HitsRatioCollector(virtualDbName);
00417       case CACHE_STATS_NUMBER_ENTRIES :
00418         return new CacheEntriesCollector(virtualDbName);
00419       /*
00420        * Scheduler collectors
00421        */
00422       case SCHEDULER_NUMBER_READ :
00423         return new NumberReadCollector(virtualDbName);
00424       case SCHEDULER_NUMBER_REQUESTS :
00425         return new NumberRequestsCollector(virtualDbName);
00426       case SCHEDULER_NUMBER_WRITES :
00427         return new NumberWriteCollector(virtualDbName);
00428       case SCHEDULER_PENDING_TRANSACTIONS :
00429         return new PendingTransactionsCollector(virtualDbName);
00430       case SCHEDULER_PENDING_WRITES :
00431         return new PendingWritesCollector(virtualDbName);
00432       /*
00433        * Client collectors
00434        */
00435       case CLIENT_BYTES_READ :
00436         return new ClientBytesReadCollector(virtualDbName, targetName);
00437       case CLIENT_BYTES_WRITTEN :
00438         return new ClientBytesWrittenCollector(virtualDbName, targetName);
00439       case CLIENT_TIME_ACTIVE :
00440         return new ClientActiveTimeCollector(virtualDbName, targetName);
00441       case CLIENT_READING_SPEED :
00442         return new ClientReadingSpeedCollector(virtualDbName, targetName);
00443       case CLIENT_WRITING_SPEED :
00444         return new ClientWritingSpeedCollector(virtualDbName, targetName);
00445       /*
00446        * Unknown collector
00447        */
00448       default :
00449         throw new DataCollectorException(ExceptionTypes.INVALID_COLLECTOR_TYPE);
00450     }
00451   }
00452 
00453   /**
00454    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#retrieveData
00455    */
00456   public long retrieveData(AbstractDataCollector collector)
00457       throws DataCollectorException
00458   {
00459     // Get the new value and return it
00460     // recall reference to controller (is transient in collector)
00461     collector.setController(controller);
00462     return collector.collectValue();
00463   }
00464 
00465   /**
00466    * @see org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean#hasVirtualDatabase(java.lang.String)
00467    */
00468   public boolean hasVirtualDatabase(String name)
00469   {
00470     return controller.hasVirtualDatabase(name);
00471   }
00472 }

Generated on Mon Apr 11 22:01:31 2005 for C-JDBC by  doxygen 1.3.9.1