-
Notifications
You must be signed in to change notification settings - Fork 3.5k
metric: improve accuracy of timer metric under contention #18219
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
Merged
yaauie
merged 2 commits into
elastic:main
from
yaauie:eliminate-concurrent-live-timer-contention-drift
Sep 29, 2025
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
| package org.logstash.instrument.metrics.timer; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
yaauie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import org.logstash.instrument.metrics.AbstractMetric; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.TimeUnit; | ||
yaauie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
yaauie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.concurrent.atomic.LongAdder; | ||
| import java.util.function.LongSupplier; | ||
|
|
@@ -43,10 +48,13 @@ protected ConcurrentLiveTimerMetric(final String name) { | |
| @Override | ||
| public <T, E extends Throwable> T time(ExceptionalSupplier<T, E> exceptionalSupplier) throws E { | ||
| try { | ||
| trackedMillisState.getAndUpdate(TrackedMillisState::withIncrementedConcurrency); | ||
| trackedMillisState.getAndUpdate(existing -> existing.withIncrementedConcurrency(nanoTimeSupplier.getAsLong())); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: the |
||
| return exceptionalSupplier.get(); | ||
| } finally { | ||
| trackedMillisState.getAndUpdate(TrackedMillisState::withDecrementedConcurrency); | ||
| // lock in the actual time completed, and resolve state separately | ||
| // so that contention for recording state is not included in measurement. | ||
| final long endTime = nanoTimeSupplier.getAsLong(); | ||
| trackedMillisState.getAndUpdate(existing -> existing.withDecrementedConcurrency(endTime)); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -65,13 +73,13 @@ private long getUntrackedMillis() { | |
| } | ||
|
|
||
| private long getTrackedMillis() { | ||
| return this.trackedMillisState.getAcquire().getValue(); | ||
| return this.trackedMillisState.getAcquire().getValue(nanoTimeSupplier.getAsLong()); | ||
| } | ||
|
|
||
| interface TrackedMillisState { | ||
| TrackedMillisState withIncrementedConcurrency(); | ||
| TrackedMillisState withDecrementedConcurrency(); | ||
| long getValue(); | ||
| TrackedMillisState withIncrementedConcurrency(long asOfNanoTime); | ||
| TrackedMillisState withDecrementedConcurrency(long asOfNanoTime); | ||
| long getValue(long asOfNanoTime); | ||
| } | ||
|
|
||
| private class StaticTrackedMillisState implements TrackedMillisState { | ||
|
|
@@ -89,18 +97,18 @@ public StaticTrackedMillisState() { | |
| } | ||
|
|
||
| @Override | ||
| public TrackedMillisState withIncrementedConcurrency() { | ||
| return new DynamicTrackedMillisState(nanoTimeSupplier.getAsLong(), this.cumulativeMillis, this.excessNanos, 1); | ||
| public TrackedMillisState withIncrementedConcurrency(final long asOfNanoTime) { | ||
| return new DynamicTrackedMillisState(asOfNanoTime, this.cumulativeMillis, this.excessNanos, 1); | ||
| } | ||
|
|
||
| @Override | ||
| public TrackedMillisState withDecrementedConcurrency() { | ||
| public TrackedMillisState withDecrementedConcurrency(final long asOfNanoTime) { | ||
| throw new IllegalStateException("TimerMetrics cannot track negative concurrency"); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public long getValue() { | ||
| public long getValue(final long asOfNanoTime) { | ||
| return cumulativeMillis; | ||
| } | ||
| } | ||
|
|
@@ -122,26 +130,26 @@ private class DynamicTrackedMillisState implements TrackedMillisState { | |
| } | ||
|
|
||
| @Override | ||
| public TrackedMillisState withIncrementedConcurrency() { | ||
| return withAdjustedConcurrency(Vector.INCREMENT); | ||
| public TrackedMillisState withIncrementedConcurrency(final long asOfNanoTime) { | ||
| return withAdjustedConcurrency(asOfNanoTime, Vector.INCREMENT); | ||
| } | ||
|
|
||
| @Override | ||
| public TrackedMillisState withDecrementedConcurrency() { | ||
| return withAdjustedConcurrency(Vector.DECREMENT); | ||
| public TrackedMillisState withDecrementedConcurrency(final long asOfNanoTime) { | ||
| return withAdjustedConcurrency(asOfNanoTime, Vector.DECREMENT); | ||
| } | ||
|
|
||
| @Override | ||
| public long getValue() { | ||
| final long nanoAdjustment = getNanoAdjustment(nanoTimeSupplier.getAsLong()); | ||
| public long getValue(final long asOfNanoTime) { | ||
| final long nanoAdjustment = getNanoAdjustment(asOfNanoTime); | ||
| final long milliAdjustment = wholeMillisFromNanos(nanoAdjustment); | ||
|
|
||
| return Math.addExact(this.millisAtCheckpoint, milliAdjustment); | ||
| } | ||
|
|
||
| private TrackedMillisState withAdjustedConcurrency(final Vector concurrencyAdjustmentVector) { | ||
| private TrackedMillisState withAdjustedConcurrency(final long asOfNanoTime, final Vector concurrencyAdjustmentVector) { | ||
| final int newConcurrency = Math.addExact(this.concurrencySinceCheckpoint, concurrencyAdjustmentVector.value()); | ||
| final long newCheckpointNanoTime = nanoTimeSupplier.getAsLong(); | ||
| final long newCheckpointNanoTime = asOfNanoTime; | ||
|
|
||
| final long totalNanoAdjustment = getNanoAdjustment(newCheckpointNanoTime); | ||
|
|
||
|
|
@@ -165,7 +173,7 @@ private long getNanoAdjustment(final long checkpointNanoTime) { | |
|
|
||
| /** | ||
| * This private enum is a type-safety guard for | ||
| * {@link DynamicTrackedMillisState#withAdjustedConcurrency(Vector)}. | ||
| * {@link DynamicTrackedMillisState#withAdjustedConcurrency(long, Vector)}. | ||
| */ | ||
| private enum Vector { | ||
| INCREMENT{ int value() { return +1; } }, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.