Skip to content

Commit 0ef1114

Browse files
authored
Merge pull request #52 from chang-chao/master
Initializes Reservoir reset setting eagerly.
2 parents 35d99a5 + e54f28f commit 0ef1114

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

  • aws-xray-recorder-sdk-core/src

aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/strategy/sampling/reservoir/Reservoir.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Reservoir {
1111
private final int tracesPerSecond;
1212
private final MaxFunction maxFunction;
1313
private final AtomicInteger usage = new AtomicInteger(0);
14-
private final AtomicLong nextReset = new AtomicLong(0);
14+
private final AtomicLong nextReset;
1515

1616
public Reservoir() {
1717
this(0);
@@ -21,6 +21,9 @@ public Reservoir(int tracesPerSecond) {
2121
this.tracesPerSecond = tracesPerSecond;
2222
this.maxFunction =
2323
tracesPerSecond < 10 ? new LessThan10(tracesPerSecond) : new AtLeast10(tracesPerSecond);
24+
25+
long now = System.nanoTime();
26+
this.nextReset = new AtomicLong(now + NANOS_PER_SECOND);
2427
}
2528

2629
public boolean take() {

aws-xray-recorder-sdk-core/src/test/java/com/amazonaws/xray/strategy/sampling/reservoir/ReservoirTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ public class ReservoirTest {
3636
assertFalse(reservoir.take());
3737
}
3838

39+
@Test public void samplesFairNegativeNanoTime() {
40+
mockStatic(System.class);
41+
when(System.nanoTime()).thenReturn(-2 * NANOS_PER_SECOND);
42+
Reservoir reservoir = new Reservoir(10);
43+
44+
when(System.nanoTime()).thenReturn(-2 * NANOS_PER_SECOND + 1);
45+
assertTrue(reservoir.take());
46+
when(System.nanoTime()).thenReturn(-2 * NANOS_PER_SECOND + 2);
47+
assertTrue(reservoir.take());
48+
when(System.nanoTime()).thenReturn(-2 * NANOS_PER_SECOND + 2);
49+
assertFalse(reservoir.take());
50+
}
51+
3952
@Test public void resetsAfterASecond() {
4053
mockStatic(System.class);
4154

0 commit comments

Comments
 (0)