Skip to content

Commit 01847b8

Browse files
committed
comment resolution
1 parent 5a23443 commit 01847b8

5 files changed

Lines changed: 44 additions & 28 deletions

File tree

core/src/main/java/com/linecorp/armeria/common/util/AndSampler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public boolean isSampled(T t) {
3232

3333
@Override
3434
public String toString() {
35-
return left.toString() + " and " + right.toString();
35+
return left + " and " + right;
3636
}
3737
}

core/src/main/java/com/linecorp/armeria/common/util/OrSampler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public boolean isSampled(T t) {
3232

3333
@Override
3434
public String toString() {
35-
return left.toString() + " or " + right.toString();
35+
return left + " or " + right;
3636
}
3737
}

core/src/main/java/com/linecorp/armeria/common/util/Sampler.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
*/
3030
package com.linecorp.armeria.common.util;
3131

32+
import static java.util.Objects.requireNonNull;
33+
3234
/**
3335
* Sampler is responsible for deciding if a particular trace should be "sampled", i.e. whether the
3436
* overhead of tracing will occur and/or if a trace will be reported to the collection tier.
@@ -47,48 +49,60 @@ public interface Sampler<T> {
4749
* Returns a sampler that applies logical or operator to both samplers decisions.
4850
*/
4951
default Sampler<T> or(Sampler<T> other) {
50-
return new OrSampler<>(this, other);
52+
return new OrSampler<>(this, requireNonNull(other, "other"));
5153
}
5254

5355
/**
5456
* Returns a sampler that applies logical and operator to both samplers decisions.
5557
*/
5658
default Sampler<T> and(Sampler<T> other) {
57-
return new AndSampler<>(this, other);
59+
return new AndSampler<>(this, requireNonNull(other, "other"));
60+
}
61+
62+
/**
63+
* Returns a sampler that applies logical not operator to the sampler decision.
64+
*/
65+
default Sampler<T> not() {
66+
return object -> !isSampled(object);
5867
}
5968

6069
/**
6170
* Returns a sampler that returns {@code true} if the value is greater than the given value.
6271
*/
6372
static <T extends Comparable<T>> Sampler<T> greaterThan(T val) {
73+
requireNonNull(val, "val");
6474
return object -> object.compareTo(val) > 0;
6575
}
6676

6777
/**
6878
* Returns a sampler that returns {@code true} if the value is less than or equal to the given value.
6979
*/
7080
static <T extends Comparable<T>> Sampler<T> greaterThanOrEqual(T val) {
81+
requireNonNull(val, "val");
7182
return object -> object.compareTo(val) >= 0;
7283
}
7384

7485
/**
7586
* Returns a sampler that returns {@code true} if the value is less than the given value.
7687
*/
7788
static <T extends Comparable<T>> Sampler<T> lessThan(T val) {
89+
requireNonNull(val, "val");
7890
return object -> object.compareTo(val) < 0;
7991
}
8092

8193
/**
8294
* Returns a sampler that returns {@code true} if the value is less than or equal to the given value.
8395
*/
8496
static <T extends Comparable<T>> Sampler<T> lessThanOrEqual(T val) {
97+
requireNonNull(val, "val");
8598
return object -> object.compareTo(val) <= 0;
8699
}
87100

88101
/**
89102
* Returns a sampler that returns {@code true} if the value is equal to the given value.
90103
*/
91104
static <T extends Comparable<T>> Sampler<T> equal(T val) {
105+
requireNonNull(val, "val");
92106
return object -> object.compareTo(val) == 0;
93107
}
94108

core/src/main/java/com/linecorp/armeria/common/util/TimeWindowPercentileSampler.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
package com.linecorp.armeria.common.util;
1111

1212
import java.time.Duration;
13+
import java.util.concurrent.atomic.AtomicReference;
14+
15+
import com.google.common.annotations.VisibleForTesting;
1316

1417
import io.micrometer.core.instrument.Clock;
1518
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
@@ -23,13 +26,19 @@ final class TimeWindowPercentileSampler implements Sampler<Long> {
2326

2427
private final float percentile;
2528
private final long windowLengthMillis;
26-
2729
private final TimeWindowPercentileHistogram histogram;
2830
private static final long SNAPSHOT_UPDATE_MILLIS = 1000L;
29-
private long lastSnapshotMillis = 0L;
31+
private long lastSnapshotMillis;
3032
private HistogramSnapshot histogramSnapshot;
3133

34+
private final AtomicReference<Boolean> isTakingSnapshot = new AtomicReference<>(false);
35+
3236
TimeWindowPercentileSampler(float percentile, long windowLengthMillis) {
37+
this(percentile, windowLengthMillis, Clock.SYSTEM);
38+
}
39+
40+
@VisibleForTesting
41+
TimeWindowPercentileSampler(float percentile, long windowLengthMillis, Clock clock) {
3342
this.percentile = percentile;
3443
this.windowLengthMillis = windowLengthMillis;
3544

@@ -42,7 +51,7 @@ final class TimeWindowPercentileSampler implements Sampler<Long> {
4251
.expiry(Duration.ofMillis(windowLengthMillis))
4352
.bufferLength(3)
4453
.build();
45-
this.histogram = new TimeWindowPercentileHistogram(Clock.SYSTEM, distributionStatisticConfig, true);
54+
this.histogram = new TimeWindowPercentileHistogram(clock, distributionStatisticConfig, true);
4655
this.histogramSnapshot = histogram.takeSnapshot(0, 0, 0);
4756
this.lastSnapshotMillis = System.currentTimeMillis();
4857
}
@@ -56,17 +65,20 @@ public boolean isSampled(Long t) {
5665
histogram.recordLong(t);
5766

5867
if (lastSnapshotMillis + SNAPSHOT_UPDATE_MILLIS < System.currentTimeMillis()) {
59-
histogramSnapshot = histogram.takeSnapshot(0, 0, 0);
60-
lastSnapshotMillis = System.currentTimeMillis();
68+
if (isTakingSnapshot.compareAndSet(false, true)) {
69+
histogramSnapshot = histogram.takeSnapshot(0, 0, 0);
70+
lastSnapshotMillis = System.currentTimeMillis();
71+
isTakingSnapshot.set(false);
72+
}
6173
}
6274

6375
final Double percentileValue = histogramSnapshot.percentileValues()[0].value();
64-
return percentileValue.longValue() <= t;
76+
return t >= percentileValue.longValue();
6577
}
6678

6779
@Override
6880
public String toString() {
69-
return "SlidingWindowPercentileSampler with " + windowLengthMillis + " ms window and " + percentile +
81+
return "TimeWindowPercentileSampler with " + windowLengthMillis + " ms window and " + percentile +
7082
" percentile";
7183
}
7284
}

core/src/test/java/com/linecorp/armeria/common/util/SamplerTest.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,15 @@ void andOr() {
111111
assertThat(neverAndAlwaysSampler.isSampled(i)).isFalse();
112112
}
113113

114-
final Sampler<Object> halfAndHalfSampler = Sampler.random(0.5f).and(Sampler.random(0.5f));
115-
int halfAndHalfSamplerCount = 0;
116-
for (int i = 0; i < 10000; i++) {
117-
if (halfAndHalfSampler.isSampled(i)) {
118-
halfAndHalfSamplerCount += 1;
119-
}
114+
final Sampler<Object> notNeverSampler = Sampler.never().not();
115+
for (int i = 0; i < 10; i++) {
116+
assertThat(notNeverSampler.isSampled(i)).isTrue();
120117
}
121-
// 0.5*0.5 = 0.25
122-
assertThat(halfAndHalfSamplerCount).isBetween(2000, 3000); // Should be roughly 2500 //
123-
124-
final Sampler<Object> halfOrHalfSampler = Sampler.random(0.5f).or(Sampler.random(0.5f));
125-
int halfOrHalfSamplerCount = 0;
126-
for (int i = 0; i < 10000; i++) {
127-
if (halfOrHalfSampler.isSampled(i)) {
128-
halfOrHalfSamplerCount += 1;
129-
}
118+
119+
final Sampler<Object> notAlwaysSampler = Sampler.always().not();
120+
for (int i = 0; i < 10; i++) {
121+
assertThat(notAlwaysSampler.isSampled(i)).isFalse();
130122
}
131-
// 1 - (0.5*0.5) = 0.75
132-
assertThat(halfOrHalfSamplerCount).isBetween(7000, 8000); // Should be roughly 7500
133123
}
134124

135125
private static class SampleOnce implements Sampler<Object> {

0 commit comments

Comments
 (0)