From 18b53b780d9e0a58a9fad0e8e509cf7b82b2d76e Mon Sep 17 00:00:00 2001 From: Jonathan Turner Eagles Date: Thu, 21 Nov 2013 16:13:16 +0000 Subject: [PATCH] HADOOP-10111. Allow DU to be initialized with an initial value (Kihwal Lee via jeagles) git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1544239 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 3 ++ .../main/java/org/apache/hadoop/fs/DU.java | 43 ++++++++++++++++--- .../java/org/apache/hadoop/fs/TestDU.java | 15 +++++++ 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 824e6474e79..f6196af25e2 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -385,6 +385,9 @@ Release 2.3.0 - UNRELEASED HADOOP-10103. update commons-lang to 2.6 (Akira AJISAKA via stevel) + HADOOP-10111. Allow DU to be initialized with an initial value (Kihwal Lee + via jeagles) + OPTIMIZATIONS HADOOP-9748. Reduce blocking on UGI.ensureInitialized (daryn) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/DU.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/DU.java index 3fa55100368..5a4f52648b6 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/DU.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/DU.java @@ -47,17 +47,32 @@ public class DU extends Shell { * @throws IOException if we fail to refresh the disk usage */ public DU(File path, long interval) throws IOException { + this(path, interval, -1L); + } + + /** + * Keeps track of disk usage. + * @param path the path to check disk usage in + * @param interval refresh the disk usage at this interval + * @param initialUsed use this value until next refresh + * @throws IOException if we fail to refresh the disk usage + */ + public DU(File path, long interval, long initialUsed) throws IOException { super(0); - + //we set the Shell interval to 0 so it will always run our command //and use this one to set the thread sleep interval this.refreshInterval = interval; this.dirPath = path.getCanonicalPath(); - - //populate the used variable - run(); + + //populate the used variable if the initial value is not specified. + if (initialUsed < 0) { + run(); + } else { + this.used.set(initialUsed); + } } - + /** * Keeps track of disk usage. * @param path the path to check disk usage in @@ -65,10 +80,24 @@ public class DU extends Shell { * @throws IOException if we fail to refresh the disk usage */ public DU(File path, Configuration conf) throws IOException { - this(path, conf.getLong(CommonConfigurationKeys.FS_DU_INTERVAL_KEY, - CommonConfigurationKeys.FS_DU_INTERVAL_DEFAULT)); + this(path, conf, -1L); } + /** + * Keeps track of disk usage. + * @param path the path to check disk usage in + * @param conf configuration object + * @param initialUsed use it until the next refresh. + * @throws IOException if we fail to refresh the disk usage + */ + public DU(File path, Configuration conf, long initialUsed) + throws IOException { + this(path, conf.getLong(CommonConfigurationKeys.FS_DU_INTERVAL_KEY, + CommonConfigurationKeys.FS_DU_INTERVAL_DEFAULT), initialUsed); + } + + + /** * This thread refreshes the "used" variable. * diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestDU.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestDU.java index ef59b85bd34..3add2a70b16 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestDU.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestDU.java @@ -116,4 +116,19 @@ public class TestDU extends TestCase { long duSize = du.getUsed(); assertTrue(String.valueOf(duSize), duSize >= 0L); } + + public void testDUSetInitialValue() throws IOException { + File file = new File(DU_DIR, "dataX"); + createFile(file, 8192); + DU du = new DU(file, 3000, 1024); + du.start(); + assertTrue("Initial usage setting not honored", du.getUsed() == 1024); + + // wait until the first du runs. + try { + Thread.sleep(5000); + } catch (InterruptedException ie) {} + + assertTrue("Usage didn't get updated", du.getUsed() == 8192); + } }