From 2fdc8fa029b7970090126bbbe22f144b1d5937bd Mon Sep 17 00:00:00 2001 From: chang-chao Date: Sun, 6 Jan 2019 10:39:04 +0900 Subject: [PATCH] Initailizes reset setting eagerly. --- .../xray/strategy/sampling/reservoir/Reservoir.java | 5 ++++- .../strategy/sampling/reservoir/ReservoirTest.java | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/strategy/sampling/reservoir/Reservoir.java b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/strategy/sampling/reservoir/Reservoir.java index a7466bb2..21f78fd7 100644 --- a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/strategy/sampling/reservoir/Reservoir.java +++ b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/strategy/sampling/reservoir/Reservoir.java @@ -11,7 +11,7 @@ public class Reservoir { private final int tracesPerSecond; private final MaxFunction maxFunction; private final AtomicInteger usage = new AtomicInteger(0); - private final AtomicLong nextReset = new AtomicLong(0); + private final AtomicLong nextReset; public Reservoir() { this(0); @@ -21,6 +21,9 @@ public Reservoir(int tracesPerSecond) { this.tracesPerSecond = tracesPerSecond; this.maxFunction = tracesPerSecond < 10 ? new LessThan10(tracesPerSecond) : new AtLeast10(tracesPerSecond); + + long now = System.nanoTime(); + this.nextReset = new AtomicLong(now + NANOS_PER_SECOND); } public boolean take() { diff --git a/aws-xray-recorder-sdk-core/src/test/java/com/amazonaws/xray/strategy/sampling/reservoir/ReservoirTest.java b/aws-xray-recorder-sdk-core/src/test/java/com/amazonaws/xray/strategy/sampling/reservoir/ReservoirTest.java index b7ae7671..e64f9ebd 100644 --- a/aws-xray-recorder-sdk-core/src/test/java/com/amazonaws/xray/strategy/sampling/reservoir/ReservoirTest.java +++ b/aws-xray-recorder-sdk-core/src/test/java/com/amazonaws/xray/strategy/sampling/reservoir/ReservoirTest.java @@ -36,6 +36,19 @@ public class ReservoirTest { assertFalse(reservoir.take()); } + @Test public void samplesFairNegativeNanoTime() { + mockStatic(System.class); + when(System.nanoTime()).thenReturn(-2 * NANOS_PER_SECOND); + Reservoir reservoir = new Reservoir(10); + + when(System.nanoTime()).thenReturn(-2 * NANOS_PER_SECOND + 1); + assertTrue(reservoir.take()); + when(System.nanoTime()).thenReturn(-2 * NANOS_PER_SECOND + 2); + assertTrue(reservoir.take()); + when(System.nanoTime()).thenReturn(-2 * NANOS_PER_SECOND + 2); + assertFalse(reservoir.take()); + } + @Test public void resetsAfterASecond() { mockStatic(System.class);