Ver código fonte

HADOOP-886. Reduce number of timer threads created by metrics API by pooling contexts. Contributed by Nigel.

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@496899 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 18 anos atrás
pai
commit
31fa66f978

+ 3 - 0
CHANGES.txt

@@ -32,6 +32,9 @@ Trunk (unreleased changes)
     An exception is still thrown, but corrupt blocks are now removed
     when they have replicas.  (Wendy Chien via cutting)
 
+ 9. HADOOP-886.  Reduce number of timer threads created by metrics API
+    by pooling contexts.  (Nigel Daley via cutting)
+
 
 Release 0.10.1 - 2007-01-10
 

+ 4 - 1
src/java/org/apache/hadoop/metrics/ContextFactory.java

@@ -45,6 +45,7 @@ public class ContextFactory {
     
     // private Map<String,Object> attributeMap = new HashMap<String,Object>();
     private Map attributeMap = new HashMap();
+    private Map<String,MetricsContext> contextMap = new HashMap<String,MetricsContext>();
     
     /** Creates a new instance of ContextFactory */
     protected ContextFactory() {
@@ -112,9 +113,10 @@ public class ContextFactory {
      * @param contextName the name of the context
      * @return the named MetricsContext
      */
-    public MetricsContext getContext(String contextName) 
+    public synchronized MetricsContext getContext(String contextName) 
         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
     {
+        if (contextMap.containsKey(contextName)) return contextMap.get(contextName);
         String classNameAttribute = contextName + CONTEXT_CLASS_SUFFIX;
         String className = (String) getAttribute(classNameAttribute);
         if (className == null) {
@@ -124,6 +126,7 @@ public class ContextFactory {
         AbstractMetricsContext metricsContext = 
                 (AbstractMetricsContext) contextClass.newInstance();
         metricsContext.init(contextName, this);
+        contextMap.put(contextName, metricsContext);
         return metricsContext;
     }