Browse Source

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 years ago
parent
commit
31fa66f978
2 changed files with 7 additions and 1 deletions
  1. 3 0
      CHANGES.txt
  2. 4 1
      src/java/org/apache/hadoop/metrics/ContextFactory.java

+ 3 - 0
CHANGES.txt

@@ -32,6 +32,9 @@ Trunk (unreleased changes)
     An exception is still thrown, but corrupt blocks are now removed
     An exception is still thrown, but corrupt blocks are now removed
     when they have replicas.  (Wendy Chien via cutting)
     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
 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<String,Object> attributeMap = new HashMap<String,Object>();
     private Map attributeMap = new HashMap();
     private Map attributeMap = new HashMap();
+    private Map<String,MetricsContext> contextMap = new HashMap<String,MetricsContext>();
     
     
     /** Creates a new instance of ContextFactory */
     /** Creates a new instance of ContextFactory */
     protected ContextFactory() {
     protected ContextFactory() {
@@ -112,9 +113,10 @@ public class ContextFactory {
      * @param contextName the name of the context
      * @param contextName the name of the context
      * @return the named MetricsContext
      * @return the named MetricsContext
      */
      */
-    public MetricsContext getContext(String contextName) 
+    public synchronized MetricsContext getContext(String contextName) 
         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
     {
     {
+        if (contextMap.containsKey(contextName)) return contextMap.get(contextName);
         String classNameAttribute = contextName + CONTEXT_CLASS_SUFFIX;
         String classNameAttribute = contextName + CONTEXT_CLASS_SUFFIX;
         String className = (String) getAttribute(classNameAttribute);
         String className = (String) getAttribute(classNameAttribute);
         if (className == null) {
         if (className == null) {
@@ -124,6 +126,7 @@ public class ContextFactory {
         AbstractMetricsContext metricsContext = 
         AbstractMetricsContext metricsContext = 
                 (AbstractMetricsContext) contextClass.newInstance();
                 (AbstractMetricsContext) contextClass.newInstance();
         metricsContext.init(contextName, this);
         metricsContext.init(contextName, this);
+        contextMap.put(contextName, metricsContext);
         return metricsContext;
         return metricsContext;
     }
     }