KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > monitoring > SQLMonitoring


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 Continuent, Inc.
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Emmanuel Cecchet.
21  * Contributor(s): ______________________________________.
22  */

23
24 package org.continuent.sequoia.controller.monitoring;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import org.continuent.sequoia.common.log.Trace;
32 import org.continuent.sequoia.common.util.Stats;
33 import org.continuent.sequoia.common.xml.DatabasesXmlTags;
34 import org.continuent.sequoia.controller.requests.AbstractRequest;
35
36 /**
37  * This class implements a SQL monitoring module.
38  *
39  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
40  * @version 1.0
41  */

42 public class SQLMonitoring extends Monitoring
43 {
44   private Hashtable JavaDoc statList; // SQL query -> Stat
45
private ArrayList JavaDoc ruleList;
46   private boolean defaultRule;
47
48   private static Trace logger = null;
49
50   /**
51    * Create a SQLMonitoring object.
52    *
53    * @param vdbName name of the virtual database to be used by the logger
54    */

55   public SQLMonitoring(String JavaDoc vdbName)
56   {
57     statList = new Hashtable JavaDoc();
58     ruleList = new ArrayList JavaDoc();
59     logger = Trace.getLogger("org.continuent.sequoia.controller.monitoring."
60         + vdbName);
61     setActive(true);
62   }
63
64   /**
65    * @see org.continuent.sequoia.controller.monitoring.Monitoring#cleanStats()
66    */

67   public void cleanStats()
68   {
69     statList.clear();
70   }
71
72   /**
73    * Log the time elapsed to execute the given request. The time is computed
74    * from the request start and end time stored in the object.
75    *
76    * @param request the request executed
77    */

78   public final void logRequestTime(AbstractRequest request)
79   {
80     Stats stat = getStatForRequest(request);
81     if (stat == null)
82       return;
83     stat.incrementCount();
84     long time = request.getEndTime() - request.getStartTime();
85     stat.updateTime(time);
86     if (logger.isDebugEnabled())
87       logger.debug(time + "ms for " + stat.getName());
88   }
89
90   /**
91    * Log an error for the given request.
92    *
93    * @param request the request that failed to execute
94    */

95   public final void logError(AbstractRequest request)
96   {
97     Stats stat = getStatForRequest(request);
98     if (stat == null)
99       return;
100     stat.incrementError();
101     if (logger.isDebugEnabled())
102       logger.debug("ERROR " + stat.getName());
103   }
104
105   /**
106    * Log a cache hit for the given request.
107    *
108    * @param request the request that failed to execute
109    */

110   public final void logCacheHit(AbstractRequest request)
111   {
112     Stats stat = getStatForRequest(request);
113     if (stat == null)
114       return;
115     stat.incrementCacheHit();
116     if (logger.isDebugEnabled())
117       logger.debug("Cache hit " + stat.getName());
118   }
119
120   /**
121    * Reset the stats associated to a request.
122    *
123    * @param request the request to reset
124    */

125   public final void resetRequestStat(AbstractRequest request)
126   {
127     Stats stat = getStatForRequest(request);
128     if (stat == null)
129       return;
130     stat.reset();
131   }
132
133   /**
134    * Retrieve the stat corresponding to a request and create it if it does not
135    * exist.
136    *
137    * @param request the request to look for
138    * @return corresponding stat or null if a rule does not authorize this
139    * request to be monitored
140    */

141   public final Stats getStatForRequest(AbstractRequest request)
142   {
143     String JavaDoc sql = monitorRequestRule(request);
144     if (sql == null)
145       return null;
146
147     // Note that the Hashtable is synchronized
148
Stats stat = (Stats) statList.get(sql);
149     if (stat == null)
150     { // No entry for this query, create a new Stats entry
151
stat = new Stats(sql);
152       statList.put(sql, stat);
153     }
154     return stat;
155   }
156
157   /**
158    * Return all stats information in the form of a String
159    *
160    * @return stats information
161    */

162   public String JavaDoc[][] getAllStatsInformation()
163   {
164     Collection JavaDoc values = statList.values();
165     String JavaDoc[][] result = new String JavaDoc[values.size()][];
166     int i = 0;
167     for (Iterator JavaDoc iter = values.iterator(); iter.hasNext(); i++)
168     {
169       Stats stat = (Stats) iter.next();
170       result[i] = stat.toStringTable();
171     }
172     return result;
173   }
174
175   /**
176    * Dump all stats using the current logger (INFO level).
177    */

178   public void dumpAllStatsInformation()
179   {
180     if (logger.isInfoEnabled())
181     {
182       for (Iterator JavaDoc iter = statList.values().iterator(); iter.hasNext();)
183       {
184         Stats stat = (Stats) iter.next();
185         logger.info(stat.singleLineDisplay());
186       }
187     }
188   }
189
190   /*
191    * Rules Management
192    */

193
194   /**
195    * Get the default monitoring rule
196    *
197    * @return true if default is monitoring enabled
198    */

199   public boolean getDefaultRule()
200   {
201     return defaultRule;
202   }
203
204   /**
205    * Defines the default rule
206    *
207    * @param monitoring true if on, false is off
208    */

209   public void setDefaultRule(boolean monitoring)
210   {
211     this.defaultRule = monitoring;
212   }
213
214   /**
215    * Add a rule to the list.
216    *
217    * @param rule the rule to add
218    */

219   public void addRule(SQLMonitoringRule rule)
220   {
221     this.ruleList.add(rule);
222   }
223
224   /**
225    * Check the rule list to check if this request should be monitored or not.
226    *
227    * @param request the query to look for
228    * @return the SQL query to monitor or null if monitoring is off for this
229    * request
230    */

231   private String JavaDoc monitorRequestRule(AbstractRequest request)
232   {
233     for (int i = 0; i < ruleList.size(); i++)
234     {
235       SQLMonitoringRule rule = (SQLMonitoringRule) ruleList.get(i);
236       String JavaDoc sql = rule.matches(request);
237       if (sql != null)
238       { // This rule matches
239
if (rule.isMonitoring())
240           return sql;
241         else
242           return null;
243       }
244     }
245
246     // No rule matched, use the default rule
247
if (defaultRule)
248       return request.getSqlOrTemplate();
249     else
250       return null;
251   }
252
253   /**
254    * @return Returns the ruleList.
255    */

256   public ArrayList JavaDoc getRuleList()
257   {
258     return ruleList;
259   }
260
261   /**
262    * @see org.continuent.sequoia.common.xml.XmlComponent#getXml()
263    */

264   public String JavaDoc getXmlImpl()
265   {
266     String JavaDoc info = "<" + DatabasesXmlTags.ELT_SQLMonitoring + " "
267         + DatabasesXmlTags.ATT_defaultMonitoring + "=\"";
268     String JavaDoc defaultMonitoring = getDefaultRule() ? "on" : "off";
269     info += defaultMonitoring;
270     info += "\">";
271     for (int i = 0; i < ruleList.size(); i++)
272     {
273       SQLMonitoringRule rule = (SQLMonitoringRule) ruleList.get(i);
274       info += rule.getXml();
275     }
276     info += "</" + DatabasesXmlTags.ELT_SQLMonitoring + ">";
277     return info;
278   }
279
280 }
Popular Tags