Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue where histogram snapshot mutates its state #126

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions core/src/main/java/com/uber/m3/tally/HistogramImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ private double getLowerBoundValueForBucket(int bucketIndex) {
return bucketIndex == 0 ? Double.MIN_VALUE : specification.getValueUpperBounds().get(bucketIndex - 1);
}

private long getCounterValue(int index) {
return bucketCounters[index] != null ? bucketCounters[index].value() : 0;
private long snapshotCounterValue(int index) {
return bucketCounters[index] != null ? bucketCounters[index].snapshot() : 0;
}

// NOTE: Only used in testing
Expand All @@ -158,7 +158,7 @@ Map<Double, Long> snapshotValues() {
Map<Double, Long> values = new HashMap<>(bucketCounters.length, 1);

for (int i = 0; i < bucketCounters.length; ++i) {
values.put(getUpperBoundValueForBucket(i), getCounterValue(i));
values.put(getUpperBoundValueForBucket(i), snapshotCounterValue(i));
}

return values;
Expand All @@ -172,7 +172,7 @@ Map<Duration, Long> snapshotDurations() {
Map<Duration, Long> durations = new HashMap<>(bucketCounters.length, 1);

for (int i = 0; i < bucketCounters.length; ++i) {
durations.put(getUpperBoundDurationForBucket(i), getCounterValue(i));
durations.put(getUpperBoundDurationForBucket(i), snapshotCounterValue(i));
}

return durations;
Expand Down
44 changes: 44 additions & 0 deletions core/src/test/java/com/uber/m3/tally/HistogramImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,28 @@ public void snapshotValues() {
assertEquals(expectedMap, histogram.snapshotValues());
}

@Test
public void snapshotValuesIsIdempotent() {
ValueBuckets buckets = ValueBuckets.custom(0, 10);

histogram =
new HistogramImpl(
scope,
"",
null,
buckets
);

histogram.recordValue(5);

Map<Double, Long> snapshot = histogram.snapshotValues();
assertEquals(1, snapshot.get(10D).longValue());

// snapshot again to ensure the first snapshot didn't mutate internal state
snapshot = histogram.snapshotValues();
assertEquals(1, snapshot.get(10D).longValue());
}

@Test
public void snapshotDurations() {
Buckets buckets = DurationBuckets.linear(Duration.ZERO, Duration.ofMillis(10), 5);
Expand Down Expand Up @@ -247,4 +269,26 @@ public void snapshotDurations() {

assertEquals(expectedMap, histogram.snapshotDurations());
}

@Test
public void snapshotDurationsIsIdempotent() {
DurationBuckets buckets = DurationBuckets.custom(Duration.ofMillis(0), Duration.ofMillis(10));

histogram =
new HistogramImpl(
scope,
"",
null,
buckets
);

histogram.recordDuration(Duration.ofMillis(5));

Map<Duration, Long> snapshot = histogram.snapshotDurations();
assertEquals(1, snapshot.get(Duration.ofMillis(10)).longValue());

// snapshot again to ensure the first snapshot didn't mutate internal state
snapshot = histogram.snapshotDurations();
assertEquals(1, snapshot.get(Duration.ofMillis(10)).longValue());
}
}
Loading