Skip to content

Commit 7989055

Browse files
authored
Merge pull request #208 from kilink/sample-listener-primitive
Add methods to SampleListener to handle primitives
2 parents 450aa7a + f2528de commit 7989055

File tree

7 files changed

+34
-13
lines changed

7 files changed

+34
-13
lines changed

concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/MetricRegistry.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public interface MetricRegistry {
2727
*/
2828
interface SampleListener {
2929
void addSample(Number value);
30+
31+
default void addLongSample(long value) {
32+
addSample(value);
33+
}
34+
35+
default void addDoubleSample(double value) {
36+
addSample(value);
37+
}
3038
}
3139

3240
interface Counter {

concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limit/Gradient2Limit.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,9 @@ public int _update(final long startTime, final long rtt, final int inflight, fin
268268
final double shortRtt = (double)rtt;
269269
final double longRtt = this.longRtt.add(rtt).doubleValue();
270270

271-
shortRttSampleListener.addSample(shortRtt);
272-
longRttSampleListener.addSample(longRtt);
273-
queueSizeSampleListener.addSample(queueSize);
271+
shortRttSampleListener.addDoubleSample(shortRtt);
272+
longRttSampleListener.addDoubleSample(longRtt);
273+
queueSizeSampleListener.addDoubleSample(queueSize);
274274

275275
// If the long RTT is substantially larger than the short RTT then reduce the long RTT measurement.
276276
// This can happen when latency returns to normal after a prolonged prior of excessive load. Reducing the

concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limit/GradientLimit.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,10 @@ private int nextProbeCountdown() {
264264
@Override
265265
public int _update(final long startTime, final long rtt, final int inflight, final boolean didDrop) {
266266
lastRtt = rtt;
267-
minWindowRttSampleListener.addSample(rtt);
267+
minWindowRttSampleListener.addLongSample(rtt);
268268

269269
final double queueSize = this.queueSize.apply((int)this.estimatedLimit);
270-
queueSizeSampleListener.addSample(queueSize);
270+
queueSizeSampleListener.addDoubleSample(queueSize);
271271

272272
// Reset or probe for a new noload RTT and a new estimatedLimit. It's necessary to cut the limit
273273
// in half to avoid having the limit drift upwards when the RTT is probed during heavy load.
@@ -283,7 +283,7 @@ public int _update(final long startTime, final long rtt, final int inflight, fin
283283
}
284284

285285
final long rttNoLoad = rttNoLoadMeasurement.add(rtt).longValue();
286-
minRttSampleListener.addSample(rttNoLoad);
286+
minRttSampleListener.addLongSample(rttNoLoad);
287287

288288
// Rtt could be higher than rtt_noload because of smoothing rtt noload updates
289289
// so set to 1.0 to indicate no queuing. Otherwise calculate the slope and don't

concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limit/VegasLimit.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ protected int _update(long startTime, long rtt, int inflight, boolean didDrop) {
218218
return (int)estimatedLimit;
219219
}
220220

221-
rttSampleListener.addSample(rtt_noload);
221+
rttSampleListener.addLongSample(rtt_noload);
222222

223223
return updateEstimatedLimit(rtt, inflight, didDrop);
224224
}

concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limiter/AbstractPartitionedLimiter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ boolean isLimitExceeded() {
138138

139139
void acquire() {
140140
int nowBusy = busy.incrementAndGet();
141-
inflightDistribution.addSample(nowBusy);
141+
inflightDistribution.addLongSample(nowBusy);
142142
}
143143

144144
/**
@@ -149,7 +149,7 @@ boolean tryAcquire() {
149149
int current = busy.get();
150150
while (current < limit) {
151151
if (busy.compareAndSet(current, current + 1)) {
152-
inflightDistribution.addSample(current + 1);
152+
inflightDistribution.addLongSample(current + 1);
153153
return true;
154154
}
155155
current = busy.get();

concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limiter/SimpleLimiter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ else if (!semaphore.tryAcquire()) {
5959
else {
6060
listener = Optional.of(new Listener(createListener()));
6161
}
62-
inflightDistribution.addSample(getInflight());
62+
inflightDistribution.addLongSample(getInflight());
6363
return listener;
6464
}
6565

concurrency-limits-spectator/src/main/java/com/netflix/concurrency/limits/spectator/SpectatorMetricRegistry.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,24 @@ public SpectatorMetricRegistry(Registry registry, Id baseId) {
3131
this.registry = registry;
3232
this.baseId = baseId;
3333
}
34-
34+
3535
@Override
3636
public SampleListener distribution(String id, String... tagNameValuePairs) {
3737
DistributionSummary summary = registry.distributionSummary(suffixBaseId(id).withTags(tagNameValuePairs));
38-
return value -> summary.record(value.longValue());
38+
return new SampleListener() {
39+
@Override
40+
public void addSample(Number value) {
41+
summary.record(value.longValue());
42+
}
43+
@Override
44+
public void addLongSample(long value) {
45+
summary.record(value);
46+
}
47+
@Override
48+
public void addDoubleSample(double value) {
49+
summary.record((long) value);
50+
}
51+
};
3952
}
4053

4154
@Override
@@ -52,7 +65,7 @@ public void gauge(String id, Supplier<Number> supplier, String... tagNameValuePa
5265
public Counter counter(String id, String... tagNameValuePairs) {
5366
Id metricId = suffixBaseId(id).withTags(tagNameValuePairs);
5467
com.netflix.spectator.api.Counter spectatorCounter = registry.counter(metricId);
55-
return () -> spectatorCounter.increment();
68+
return spectatorCounter::increment;
5669
}
5770

5871
private Id suffixBaseId(String suffix) {

0 commit comments

Comments
 (0)