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

SQLMonitoring.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 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): Emmanuel Cecchet.
00022  * Contributor(s): ______________________________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.controller.monitoring;
00026 
00027 import java.util.ArrayList;
00028 import java.util.Collection;
00029 import java.util.Hashtable;
00030 import java.util.Iterator;
00031 
00032 import org.objectweb.cjdbc.common.log.Trace;
00033 import org.objectweb.cjdbc.common.sql.AbstractRequest;
00034 import org.objectweb.cjdbc.common.util.Stats;
00035 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
00036 
00037 /**
00038  * This class implements a SQL monitoring module.
00039  * 
00040  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
00041  * @version 1.0
00042  */
00043 public class SQLMonitoring extends Monitoring
00044 {
00045   private Hashtable    statList;     // SQL query -> Stat
00046   private ArrayList    ruleList;
00047   private boolean      defaultRule;
00048 
00049   private static Trace logger = null;
00050 
00051   /**
00052    * Create a SQLMonitoring object.
00053    * 
00054    * @param vdbName name of the virtual database to be used by the logger
00055    */
00056   public SQLMonitoring(String vdbName)
00057   {
00058     statList = new Hashtable();
00059     ruleList = new ArrayList();
00060     logger = Trace.getLogger("org.objectweb.cjdbc.controller.monitoring."
00061         + vdbName);
00062   }
00063 
00064   /**
00065    * @see org.objectweb.cjdbc.controller.monitoring.Monitoring#cleanStats()
00066    */
00067   public void cleanStats()
00068   {
00069     statList.clear();
00070   }
00071   
00072   /**
00073    * Log the time elapsed to execute the given request.
00074    * 
00075    * @param request the request executed
00076    * @param time time elapsed to execute this request
00077    */
00078   public final void logRequestTime(AbstractRequest request, long time)
00079   {
00080     Stats stat = getStatForRequest(request);
00081     if (stat == null)
00082       return;
00083     stat.incrementCount();
00084     stat.updateTime(time);
00085     if (logger.isDebugEnabled())
00086       logger.debug(time + " " + stat.getName());
00087   }
00088 
00089   /**
00090    * Log an error for the given request.
00091    * 
00092    * @param request the request that failed to execute
00093    */
00094   public final void logError(AbstractRequest request)
00095   {
00096     Stats stat = getStatForRequest(request);
00097     if (stat == null)
00098       return;
00099     stat.incrementError();
00100     if (logger.isDebugEnabled())
00101       logger.debug("ERROR " + stat.getName());
00102   }
00103 
00104   /**
00105    * Log a cache hit for the given request.
00106    * 
00107    * @param request the request that failed to execute
00108    */
00109   public final void logCacheHit(AbstractRequest request)
00110   {
00111     Stats stat = getStatForRequest(request);
00112     if (stat == null)
00113       return;
00114     stat.incrementCacheHit();
00115     if (logger.isDebugEnabled())
00116       logger.debug("Cache hit " + stat.getName());
00117   }
00118 
00119   /**
00120    * Reset the stats associated to a request.
00121    * 
00122    * @param request the request to reset
00123    */
00124   public final void resetRequestStat(AbstractRequest request)
00125   {
00126     Stats stat = getStatForRequest(request);
00127     if (stat == null)
00128       return;
00129     stat.reset();
00130   }
00131 
00132   /**
00133    * Retrieve the stat corresponding to a request and create it if it does not
00134    * exist.
00135    * 
00136    * @param request the request to look for
00137    * @return corresponding stat or null if a rule does not authorize this
00138    *         request to be monitored
00139    */
00140   public final Stats getStatForRequest(AbstractRequest request)
00141   {
00142     String sql = monitorRequestRule(request);
00143     if (sql == null)
00144       return null;
00145 
00146     // Note that the Hashtable is synchronized
00147     Stats stat = (Stats) statList.get(sql);
00148     if (stat == null)
00149     { // No entry for this query, create a new Stats entry
00150       stat = new Stats(sql);
00151       statList.put(sql, stat);
00152     }
00153     return stat;
00154   }
00155 
00156   /**
00157    * Return all stats information in the form of a String
00158    * 
00159    * @return stats information
00160    */
00161   public String[][] getAllStatsInformation()
00162   {
00163     Collection values = statList.values();
00164     String[][] result = new String[values.size()][];
00165     int i = 0;
00166     for (Iterator iter = values.iterator(); iter.hasNext(); i++)
00167     {
00168       Stats stat = (Stats) iter.next();
00169       result[i] = stat.toStringTable();
00170     }
00171     return result;
00172   }
00173 
00174   /**
00175    * Dump all stats using the current logger (INFO level).
00176    */
00177   public void dumpAllStatsInformation()
00178   {
00179     if (logger.isInfoEnabled())
00180     {
00181       for (Iterator iter = statList.values().iterator(); iter.hasNext();)
00182       {
00183         Stats stat = (Stats) iter.next();
00184         logger.info(stat.singleLineDisplay());
00185       }
00186     }
00187   }
00188 
00189   /*
00190    * Rules Management
00191    */
00192 
00193   /**
00194    * Get the default monitoring rule
00195    * 
00196    * @return true if default is monitoring enabled
00197    */
00198   public boolean getDefaultRule()
00199   {
00200     return defaultRule;
00201   }
00202 
00203   /**
00204    * Defines the default rule
00205    * 
00206    * @param monitoring true if on, false is off
00207    */
00208   public void setDefaultRule(boolean monitoring)
00209   {
00210     this.defaultRule = monitoring;
00211   }
00212 
00213   /**
00214    * Add a rule to the list.
00215    * 
00216    * @param rule the rule to add
00217    */
00218   public void addRule(SQLMonitoringRule rule)
00219   {
00220     this.ruleList.add(rule);
00221   }
00222 
00223   /**
00224    * Check the rule list to check of this request should be monitored or not.
00225    * 
00226    * @param request the query to look for
00227    * @return the SQL query to monitor or null if monitoring is off for this
00228    *         request
00229    */
00230   private String monitorRequestRule(AbstractRequest request)
00231   {
00232     for (int i = 0; i < ruleList.size(); i++)
00233     {
00234       SQLMonitoringRule rule = (SQLMonitoringRule) ruleList.get(i);
00235       String sql = rule.matches(request);
00236       if (sql != null)
00237       { // This rule matches
00238         if (rule.isMonitoring())
00239           return sql;
00240         else
00241           return null;
00242       }
00243     }
00244 
00245     // No rule matched, use the default rule
00246     if (defaultRule)
00247     {
00248       if (request.getSqlSkeleton() == null)
00249         return request.getSQL();
00250       else
00251         return request.getSqlSkeleton();
00252     }
00253     else
00254       return null;
00255   }
00256 
00257   /**
00258    * @return Returns the ruleList.
00259    */
00260   public ArrayList getRuleList()
00261   {
00262     return ruleList;
00263   }
00264 
00265   /**
00266    * @see org.objectweb.cjdbc.common.xml.XmlComponent#getXml()
00267    */
00268   public String getXmlImpl()
00269   {
00270     String info = "<" + DatabasesXmlTags.ELT_SQLMonitoring + " "
00271         + DatabasesXmlTags.ATT_defaultMonitoring + "=\"";
00272     info += getDefaultRule();
00273     info += "\">";
00274     for (int i = 0; i < ruleList.size(); i++)
00275     {
00276       SQLMonitoringRule rule = (SQLMonitoringRule) ruleList.get(i);
00277       info += rule.getXml();
00278     }
00279     info += "</" + DatabasesXmlTags.ELT_SQLMonitoring + ">";
00280     return info;
00281   }
00282 
00283 }

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