hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tomwh...@apache.org
Subject svn commit: r890576 - in /hadoop/common/trunk: CHANGES.txt src/java/org/apache/hadoop/fs/DF.java src/test/core/org/apache/hadoop/fs/TestDFVariations.java
Date Tue, 15 Dec 2009 00:10:14 GMT
Author: tomwhite
Date: Tue Dec 15 00:10:13 2009
New Revision: 890576

URL: http://svn.apache.org/viewvc?rev=890576&view=rev
Log:
HADOOP-5958. Use JDK 1.6 File APIs in DF.java wherever possible. Contributed by Aaron Kimball.

Modified:
    hadoop/common/trunk/CHANGES.txt
    hadoop/common/trunk/src/java/org/apache/hadoop/fs/DF.java
    hadoop/common/trunk/src/test/core/org/apache/hadoop/fs/TestDFVariations.java

Modified: hadoop/common/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/CHANGES.txt?rev=890576&r1=890575&r2=890576&view=diff
==============================================================================
--- hadoop/common/trunk/CHANGES.txt (original)
+++ hadoop/common/trunk/CHANGES.txt Tue Dec 15 00:10:13 2009
@@ -56,6 +56,9 @@
     HADOOP-6422. Make RPC backend plugable, protocol-by-protocol, to
     ease evolution towards Avro.  (cutting)
 
+    HADOOP-5958. Use JDK 1.6 File APIs in DF.java wherever possible.
+    (Aaron Kimball via tomwhite)
+
   OPTIMIZATIONS
 
   BUG FIXES

Modified: hadoop/common/trunk/src/java/org/apache/hadoop/fs/DF.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/java/org/apache/hadoop/fs/DF.java?rev=890576&r1=890575&r2=890576&view=diff
==============================================================================
--- hadoop/common/trunk/src/java/org/apache/hadoop/fs/DF.java (original)
+++ hadoop/common/trunk/src/java/org/apache/hadoop/fs/DF.java Tue Dec 15 00:10:13 2009
@@ -28,17 +28,17 @@
 import org.apache.hadoop.fs.CommonConfigurationKeys;
 import org.apache.hadoop.util.Shell;
 
-/** Filesystem disk space usage statistics.  Uses the unix 'df' program.
- * Tested on Linux, FreeBSD, Cygwin. */
+/** Filesystem disk space usage statistics.
+ * Uses the unix 'df' program to get mount points, and java.io.File for
+ * space utilization. Tested on Linux, FreeBSD, Cygwin. */
 public class DF extends Shell {
-  public static final long DF_INTERVAL_DEFAULT = 3 * 1000; // default DF refresh interval

-  
-  private String dirPath;
+
+  /** Default DF refresh interval. */
+  public static final long DF_INTERVAL_DEFAULT = 3 * 1000;
+
+  private final String dirPath;
+  private final File dirFile;
   private String filesystem;
-  private long capacity;
-  private long used;
-  private long available;
-  private int percentUsed;
   private String mount;
 
   enum OSType {
@@ -79,6 +79,7 @@
   public DF(File path, long dfInterval) throws IOException {
     super(dfInterval);
     this.dirPath = path.getCanonicalPath();
+    this.dirFile = new File(this.dirPath);
   }
 
   protected OSType getOSType() {
@@ -87,35 +88,40 @@
   
   /// ACCESSORS
 
+  /** @return the canonical path to the volume we're checking. */
   public String getDirPath() {
     return dirPath;
   }
-  
-  public String getFilesystem() throws IOException { 
-    run(); 
-    return filesystem; 
+
+  /** @return a string indicating which filesystem volume we're checking. */
+  public String getFilesystem() throws IOException {
+    run();
+    return filesystem;
   }
-  
-  public long getCapacity() throws IOException { 
-    run(); 
-    return capacity; 
+
+  /** @return the capacity of the measured filesystem in bytes. */
+  public long getCapacity() {
+    return dirFile.getTotalSpace();
   }
-  
-  public long getUsed() throws IOException { 
-    run(); 
-    return used;
+
+  /** @return the total used space on the filesystem in bytes. */
+  public long getUsed() {
+    return dirFile.getTotalSpace() - dirFile.getFreeSpace();
   }
-  
-  public long getAvailable() throws IOException { 
-    run(); 
-    return available;
+
+  /** @return the usable space remaining on the filesystem in bytes. */
+  public long getAvailable() {
+    return dirFile.getUsableSpace();
   }
-  
-  public int getPercentUsed() throws IOException {
-    run();
-    return percentUsed;
+
+  /** @return the amount of the volume full, as a percent. */
+  public int getPercentUsed() {
+    double cap = (double) getCapacity();
+    double used = (cap - (double) getAvailable());
+    return (int) (used * 100.0 / cap);
   }
 
+  /** @return the filesystem mount point for the indicated volume */
   public String getMount() throws IOException {
     run();
     return mount;
@@ -125,10 +131,10 @@
     return
       "df -k " + mount +"\n" +
       filesystem + "\t" +
-      capacity / 1024 + "\t" +
-      used / 1024 + "\t" +
-      available / 1024 + "\t" +
-      percentUsed + "%\t" +
+      getCapacity() / 1024 + "\t" +
+      getUsed() / 1024 + "\t" +
+      getAvailable() / 1024 + "\t" +
+      getPercentUsed() + "%\t" +
       mount;
   }
 
@@ -161,13 +167,12 @@
 
     switch(getOSType()) {
       case OS_TYPE_AIX:
-        this.capacity = Long.parseLong(tokens.nextToken()) * 1024;
-        this.available = Long.parseLong(tokens.nextToken()) * 1024;
-        this.percentUsed = Integer.parseInt(tokens.nextToken());
+        Long.parseLong(tokens.nextToken()); // capacity
+        Long.parseLong(tokens.nextToken()); // available
+        Integer.parseInt(tokens.nextToken()); // pct used
         tokens.nextToken();
         tokens.nextToken();
         this.mount = tokens.nextToken();
-        this.used = this.capacity - this.available;
         break;
 
       case OS_TYPE_WIN:
@@ -175,10 +180,10 @@
       case OS_TYPE_MAC:
       case OS_TYPE_UNIX:
       default:
-        this.capacity = Long.parseLong(tokens.nextToken()) * 1024;
-        this.used = Long.parseLong(tokens.nextToken()) * 1024;
-        this.available = Long.parseLong(tokens.nextToken()) * 1024;
-        this.percentUsed = Integer.parseInt(tokens.nextToken());
+        Long.parseLong(tokens.nextToken()); // capacity
+        Long.parseLong(tokens.nextToken()); // used
+        Long.parseLong(tokens.nextToken()); // available
+        Integer.parseInt(tokens.nextToken()); // pct used
         this.mount = tokens.nextToken();
         break;
    }

Modified: hadoop/common/trunk/src/test/core/org/apache/hadoop/fs/TestDFVariations.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/test/core/org/apache/hadoop/fs/TestDFVariations.java?rev=890576&r1=890575&r2=890576&view=diff
==============================================================================
--- hadoop/common/trunk/src/test/core/org/apache/hadoop/fs/TestDFVariations.java (original)
+++ hadoop/common/trunk/src/test/core/org/apache/hadoop/fs/TestDFVariations.java Tue Dec 15
00:10:13 2009
@@ -51,10 +51,6 @@
   public void testOSParsing() throws Exception {
     for (DF.OSType ost : EnumSet.allOf(DF.OSType.class)) {
       XXDF df = new XXDF(ost.getId());
-      assertEquals(ost.getId() + " total", 453115160 * 1024L, df.getCapacity());
-      assertEquals(ost.getId() + " used", 53037920 * 1024L, df.getUsed());
-      assertEquals(ost.getId() + " avail", 400077240 * 1024L, df.getAvailable());
-      assertEquals(ost.getId() + " pcnt used", 11, df.getPercentUsed());
       assertEquals(ost.getId() + " mount", "/foo/bar", df.getMount());
     }
   }



Mime
View raw message