You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by as...@apache.org on 2017/04/17 15:15:30 UTC
[05/50] hadoop git commit: HADOOP-14276. Add a nanosecond API to
Time/Timer/FakeTimer. Contributed by Erik Krogen.
HADOOP-14276. Add a nanosecond API to Time/Timer/FakeTimer. Contributed by Erik Krogen.
Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/95b7f1d2
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/95b7f1d2
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/95b7f1d2
Branch: refs/heads/trunk
Commit: 95b7f1d29a5e2dadd70a56fca5faa006c5bd74fc
Parents: 0116c3c
Author: Zhe Zhang <zh...@apache.org>
Authored: Thu Apr 6 16:52:22 2017 -0700
Committer: Zhe Zhang <zh...@apache.org>
Committed: Thu Apr 6 16:59:21 2017 -0700
----------------------------------------------------------------------
.../apache/hadoop/util/LightWeightCache.java | 20 +++++-----------
.../main/java/org/apache/hadoop/util/Time.java | 10 ++++++++
.../main/java/org/apache/hadoop/util/Timer.java | 10 ++++++++
.../java/org/apache/hadoop/util/FakeTimer.java | 24 ++++++++++++++++----
.../hadoop/util/TestLightWeightCache.java | 19 ++++++----------
5 files changed, 52 insertions(+), 31 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/hadoop/blob/95b7f1d2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightCache.java
----------------------------------------------------------------------
diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightCache.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightCache.java
index a0a553a..d79aade 100644
--- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightCache.java
+++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightCache.java
@@ -76,14 +76,6 @@ public class LightWeightCache<K, E extends K> extends LightWeightGSet<K, E> {
return l > r? 1: l < r? -1: 0;
}
};
-
- /** A clock for measuring time so that it can be mocked in unit tests. */
- static class Clock {
- /** @return the current time. */
- long currentTime() {
- return System.nanoTime();
- }
- }
private static int updateRecommendedLength(int recommendedLength,
int sizeLimit) {
@@ -102,7 +94,7 @@ public class LightWeightCache<K, E extends K> extends LightWeightGSet<K, E> {
private final long creationExpirationPeriod;
private final long accessExpirationPeriod;
private final int sizeLimit;
- private final Clock clock;
+ private final Timer timer;
/**
* @param recommendedLength Recommended size of the internal array.
@@ -120,7 +112,7 @@ public class LightWeightCache<K, E extends K> extends LightWeightGSet<K, E> {
final long creationExpirationPeriod,
final long accessExpirationPeriod) {
this(recommendedLength, sizeLimit,
- creationExpirationPeriod, accessExpirationPeriod, new Clock());
+ creationExpirationPeriod, accessExpirationPeriod, new Timer());
}
@VisibleForTesting
@@ -128,7 +120,7 @@ public class LightWeightCache<K, E extends K> extends LightWeightGSet<K, E> {
final int sizeLimit,
final long creationExpirationPeriod,
final long accessExpirationPeriod,
- final Clock clock) {
+ final Timer timer) {
super(updateRecommendedLength(recommendedLength, sizeLimit));
this.sizeLimit = sizeLimit;
@@ -147,11 +139,11 @@ public class LightWeightCache<K, E extends K> extends LightWeightGSet<K, E> {
this.queue = new PriorityQueue<Entry>(
sizeLimit > 0? sizeLimit + 1: 1 << 10, expirationTimeComparator);
- this.clock = clock;
+ this.timer = timer;
}
void setExpirationTime(final Entry e, final long expirationPeriod) {
- e.setExpirationTime(clock.currentTime() + expirationPeriod);
+ e.setExpirationTime(timer.monotonicNowNanos() + expirationPeriod);
}
boolean isExpired(final Entry e, final long now) {
@@ -168,7 +160,7 @@ public class LightWeightCache<K, E extends K> extends LightWeightGSet<K, E> {
/** Evict expired entries. */
private void evictExpiredEntries() {
- final long now = clock.currentTime();
+ final long now = timer.monotonicNowNanos();
for(int i = 0; i < EVICTION_LIMIT; i++) {
final Entry peeked = queue.peek();
if (peeked == null || !isExpired(peeked, now)) {
http://git-wip-us.apache.org/repos/asf/hadoop/blob/95b7f1d2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Time.java
----------------------------------------------------------------------
diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Time.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Time.java
index e96fa77..db5a567 100644
--- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Time.java
+++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Time.java
@@ -66,6 +66,16 @@ public final class Time {
}
/**
+ * Same as {@link #monotonicNow()} but returns its result in nanoseconds.
+ * Note that this is subject to the same resolution constraints as
+ * {@link System#nanoTime()}.
+ * @return a monotonic clock that counts in nanoseconds.
+ */
+ public static long monotonicNowNanos() {
+ return System.nanoTime();
+ }
+
+ /**
* Convert time in millisecond to human readable format.
* @return a human readable string for the input time
*/
http://git-wip-us.apache.org/repos/asf/hadoop/blob/95b7f1d2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Timer.java
----------------------------------------------------------------------
diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Timer.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Timer.java
index e1e21a7..3165a9b 100644
--- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Timer.java
+++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Timer.java
@@ -48,4 +48,14 @@ public class Timer {
* @return a monotonic clock that counts in milliseconds.
*/
public long monotonicNow() { return Time.monotonicNow(); }
+
+ /**
+ * Same as {@link #monotonicNow()} but returns its result in nanoseconds.
+ * Note that this is subject to the same resolution constraints as
+ * {@link System#nanoTime()}.
+ * @return a monotonic clock that counts in nanoseconds.
+ */
+ public long monotonicNowNanos() {
+ return Time.monotonicNowNanos();
+ }
}
http://git-wip-us.apache.org/repos/asf/hadoop/blob/95b7f1d2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/FakeTimer.java
----------------------------------------------------------------------
diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/FakeTimer.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/FakeTimer.java
index 66386fd..2b5f850 100644
--- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/FakeTimer.java
+++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/FakeTimer.java
@@ -18,6 +18,7 @@
package org.apache.hadoop.util;
+import java.util.concurrent.TimeUnit;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
@@ -28,25 +29,38 @@ import org.apache.hadoop.classification.InterfaceStability;
@InterfaceAudience.Private
@InterfaceStability.Unstable
public class FakeTimer extends Timer {
- private long nowMillis;
+ private long nowNanos;
/** Constructs a FakeTimer with a non-zero value */
public FakeTimer() {
- nowMillis = 1000; // Initialize with a non-trivial value.
+ nowNanos = 1000; // Initialize with a non-trivial value.
}
@Override
public long now() {
- return nowMillis;
+ return TimeUnit.NANOSECONDS.toMillis(nowNanos);
}
@Override
public long monotonicNow() {
- return nowMillis;
+ return TimeUnit.NANOSECONDS.toMillis(nowNanos);
+ }
+
+ @Override
+ public long monotonicNowNanos() {
+ return nowNanos;
}
/** Increases the time by milliseconds */
public void advance(long advMillis) {
- nowMillis += advMillis;
+ nowNanos += TimeUnit.MILLISECONDS.toNanos(advMillis);
+ }
+
+ /**
+ * Increases the time by nanoseconds.
+ * @param advNanos Nanoseconds to advance by.
+ */
+ public void advanceNanos(long advNanos) {
+ nowNanos += advNanos;
}
}
http://git-wip-us.apache.org/repos/asf/hadoop/blob/95b7f1d2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestLightWeightCache.java
----------------------------------------------------------------------
diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestLightWeightCache.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestLightWeightCache.java
index dff6937..de61818 100644
--- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestLightWeightCache.java
+++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestLightWeightCache.java
@@ -213,7 +213,7 @@ public class TestLightWeightCache {
int iterate_count = 0;
int contain_count = 0;
- private long currentTestTime = ran.nextInt();
+ private FakeTimer fakeTimer = new FakeTimer();
LightWeightCacheTestCase(int tablelength, int sizeLimit,
long creationExpirationPeriod, long accessExpirationPeriod,
@@ -230,12 +230,7 @@ public class TestLightWeightCache {
data = new IntData(datasize, modulus);
cache = new LightWeightCache<IntEntry, IntEntry>(tablelength, sizeLimit,
- creationExpirationPeriod, 0, new LightWeightCache.Clock() {
- @Override
- long currentTime() {
- return currentTestTime;
- }
- });
+ creationExpirationPeriod, 0, fakeTimer);
Assert.assertEquals(0, cache.size());
}
@@ -247,7 +242,7 @@ public class TestLightWeightCache {
} else {
final IntEntry h = hashMap.remove(key);
if (h != null) {
- Assert.assertTrue(cache.isExpired(h, currentTestTime));
+ Assert.assertTrue(cache.isExpired(h, fakeTimer.monotonicNowNanos()));
}
}
return c;
@@ -266,7 +261,7 @@ public class TestLightWeightCache {
} else {
final IntEntry h = hashMap.remove(key);
if (h != null) {
- Assert.assertTrue(cache.isExpired(h, currentTestTime));
+ Assert.assertTrue(cache.isExpired(h, fakeTimer.monotonicNowNanos()));
}
}
return c;
@@ -286,7 +281,7 @@ public class TestLightWeightCache {
final IntEntry h = hashMap.put(entry);
if (h != null && h != entry) {
// if h == entry, its expiration time is already updated
- Assert.assertTrue(cache.isExpired(h, currentTestTime));
+ Assert.assertTrue(cache.isExpired(h, fakeTimer.monotonicNowNanos()));
}
}
return c;
@@ -305,7 +300,7 @@ public class TestLightWeightCache {
} else {
final IntEntry h = hashMap.remove(key);
if (h != null) {
- Assert.assertTrue(cache.isExpired(h, currentTestTime));
+ Assert.assertTrue(cache.isExpired(h, fakeTimer.monotonicNowNanos()));
}
}
return c;
@@ -339,7 +334,7 @@ public class TestLightWeightCache {
}
void check() {
- currentTestTime += ran.nextInt() & 0x3;
+ fakeTimer.advanceNanos(ran.nextInt() & 0x3);
//test size
sizeTest();
---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org