Selaa lähdekoodia

commit 90bd40ff0cc5a45b82759e227c46599bb4df6c22
Author: Jitendra Nath Pandey <jitendra@sufferhome-lm.(none)>
Date: Tue Feb 1 16:44:50 2011 -0800

HADOOP-6432. A few files were missed in the previous commit.
Add Statistics support in FileContext.

+++ b/YAHOO-CHANGES.txt
+ HADOOP-6432. Add Statistics support in FileContext. (jitendra)
+


git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/yahoo-merge@1079166 13f79535-47bb-0310-9956-ffa450edef68

Owen O'Malley 14 vuotta sitten
vanhempi
commit
ba5f3c4330

+ 48 - 16
src/java/org/apache/hadoop/fs/AbstractFileSystem.java

@@ -24,7 +24,7 @@ import java.lang.reflect.Constructor;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.EnumSet;
-import java.util.IdentityHashMap;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.StringTokenizer;
@@ -60,9 +60,8 @@ public abstract class AbstractFileSystem {
   static final Log LOG = LogFactory.getLog(AbstractFileSystem.class);
 
   /** Recording statistics per a file system class. */
-  private static final Map<Class<? extends AbstractFileSystem>, Statistics> 
-  STATISTICS_TABLE =
-      new IdentityHashMap<Class<? extends AbstractFileSystem>, Statistics>();
+  private static final Map<URI, Statistics> 
+      STATISTICS_TABLE = new HashMap<URI, Statistics>();
   
   /** Cache of constructors for each file system class. */
   private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = 
@@ -144,35 +143,68 @@ public abstract class AbstractFileSystem {
     }
     return (AbstractFileSystem) newInstance(clazz, uri, conf);
   }
-  
-  
+
   /**
    * Get the statistics for a particular file system.
-   * @param cls the class to lookup
+   * 
+   * @param uri
+   *          used as key to lookup STATISTICS_TABLE. Only scheme and authority
+   *          part of the uri are used.
    * @return a statistics object
    */
-  public static synchronized Statistics getStatistics(String scheme,
-      Class<? extends AbstractFileSystem> cls) {
-    Statistics result = STATISTICS_TABLE.get(cls);
+  protected static synchronized Statistics getStatistics(URI uri) {
+    String scheme = uri.getScheme();
+    if (scheme == null) {
+      throw new IllegalArgumentException("Scheme not defined in the uri: "
+          + uri);
+    }
+    URI baseUri = getBaseUri(uri);
+    Statistics result = STATISTICS_TABLE.get(baseUri);
     if (result == null) {
       result = new Statistics(scheme);
-      STATISTICS_TABLE.put(cls, result);
+      STATISTICS_TABLE.put(baseUri, result);
     }
     return result;
   }
   
+  private static URI getBaseUri(URI uri) {
+    String scheme = uri.getScheme();
+    String authority = uri.getAuthority();
+    String baseUriString = scheme + "://";
+    if (authority != null) {
+      baseUriString = baseUriString + authority;
+    } else {
+      baseUriString = baseUriString + "/";
+    }
+    return URI.create(baseUriString);
+  }
+  
   public static synchronized void clearStatistics() {
     for(Statistics stat: STATISTICS_TABLE.values()) {
       stat.reset();
     }
   }
 
+  /**
+   * Prints statistics for all file systems.
+   */
   public static synchronized void printStatistics() {
-    for (Map.Entry<Class<? extends AbstractFileSystem>, Statistics> pair: 
-            STATISTICS_TABLE.entrySet()) {
-      System.out.println("  FileSystem " + pair.getKey().getName() + 
-                         ": " + pair.getValue());
+    for (Map.Entry<URI, Statistics> pair : STATISTICS_TABLE.entrySet()) {
+      System.out.println("  FileSystem " + pair.getKey().getScheme() + "://"
+          + pair.getKey().getAuthority() + ": " + pair.getValue());
+    }
+  }
+  
+  protected static synchronized Map<URI, Statistics> getAllStatistics() {
+    Map<URI, Statistics> statsMap = new HashMap<URI, Statistics>(
+        STATISTICS_TABLE.size());
+    for (Map.Entry<URI, Statistics> pair : STATISTICS_TABLE.entrySet()) {
+      URI key = pair.getKey();
+      Statistics value = pair.getValue();
+      Statistics newStatsObj = new Statistics(value);
+      statsMap.put(URI.create(key.toString()), newStatsObj);
     }
+    return statsMap;
   }
 
   /**
@@ -211,7 +243,7 @@ public abstract class AbstractFileSystem {
       final boolean authorityNeeded, final int defaultPort)
       throws URISyntaxException {
     myUri = getUri(uri, supportedScheme, authorityNeeded, defaultPort);
-    statistics = getStatistics(supportedScheme, getClass()); 
+    statistics = getStatistics(uri); 
   }
   
   /**

+ 39 - 2
src/java/org/apache/hadoop/fs/FileContext.java

@@ -40,6 +40,7 @@ import org.apache.hadoop.HadoopIllegalArgumentException;
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.classification.InterfaceStability;
 import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem.Statistics;
 import org.apache.hadoop.fs.Options.CreateOpts;
 import org.apache.hadoop.fs.Options.Rename;
 import org.apache.hadoop.fs.permission.FsPermission;
@@ -812,8 +813,8 @@ public final class FileContext {
    * 
    * @throws AccessControlException If access is denied
    * @throws FileAlreadyExistsException If <code>dst</code> already exists and
-   *           <code>options</options> has {@link Rename#OVERWRITE} option
-   *           false.
+   *           <code>options</options> has {@link Options.Rename#OVERWRITE} 
+   *           option false.
    * @throws FileNotFoundException If <code>src</code> does not exist
    * @throws ParentNotDirectoryException If parent of <code>dst</code> is not a
    *           directory
@@ -2252,4 +2253,40 @@ public final class FileContext {
       return in;
     }
   }
+  
+  /**
+   * Get the statistics for a particular file system
+   * 
+   * @param uri
+   *          the uri to lookup the statistics. Only scheme and authority part
+   *          of the uri are used as the key to store and lookup.
+   * @return a statistics object
+   */
+  public static Statistics getStatistics(URI uri) {
+    return AbstractFileSystem.getStatistics(uri);
+  }
+
+  /**
+   * Clears all the statistics stored in AbstractFileSystem, for all the file
+   * systems.
+   */
+  public static void clearStatistics() {
+    AbstractFileSystem.clearStatistics();
+  }
+
+  /**
+   * Prints the statistics to standard output. File System is identified by the
+   * scheme and authority.
+   */
+  public static void printStatistics() {
+    AbstractFileSystem.printStatistics();
+  }
+
+  /**
+   * @return Map of uri and statistics for each filesystem instantiated. The uri
+   *         consists of scheme and authority for the filesystem.
+   */
+  public static Map<URI, Statistics> getAllStatistics() {
+    return AbstractFileSystem.getAllStatistics();
+  }
 }

+ 12 - 0
src/java/org/apache/hadoop/fs/FileSystem.java

@@ -2007,6 +2007,18 @@ public abstract class FileSystem extends Configured implements Closeable {
       this.scheme = scheme;
     }
 
+    /**
+     * Copy constructor.
+     * 
+     * @param st
+     *          The input Statistics object which is cloned.
+     */
+    public Statistics(Statistics st) {
+      this.scheme = st.scheme;
+      this.bytesRead = new AtomicLong(st.bytesRead.longValue());
+      this.bytesWritten = new AtomicLong(st.bytesWritten.longValue());
+    }
+
     /**
      * Increment the bytes read in the statistics
      * @param newBytes the additional bytes read