-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Calculate recent write load in indexing stats #124652
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
PeteGillinElastic
merged 6 commits into
elastic:main
from
PeteGillinElastic:ES-10037-recent-load-metric
Mar 18, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
066552c
Calculate recent write load in indexing stats
PeteGillinElastic d47d3d8
--amend
PeteGillinElastic 882aff2
Respond to review comment about logging, and make it lazy as well
PeteGillinElastic f83235f
Use `Strings.format` instead of `String.format`
PeteGillinElastic 2440273
Merge remote-tracking branch 'upstream/main' into ES-10037-recent-loa…
PeteGillinElastic 3c08c55
Merge remote-tracking branch 'upstream/main' into ES-10037-recent-loa…
PeteGillinElastic 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
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
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
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 |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
|
|
||
| import static java.lang.Math.exp; | ||
| import static java.lang.Math.expm1; | ||
| import static java.lang.Math.log; | ||
|
|
||
| /** | ||
| * Implements a version of an exponentially weighted moving rate (EWMR). This is a calculation over a finite time series of increments to | ||
|
|
@@ -41,7 +42,8 @@ public class ExponentiallyWeightedMovingRate { | |
| private final double lambda; | ||
| private final long startTime; | ||
| private double rate; | ||
| long lastTime; | ||
| private long lastTime; | ||
| private boolean waitingForFirstIncrement; | ||
|
|
||
| /** | ||
| * Constructor. | ||
|
|
@@ -57,14 +59,12 @@ public ExponentiallyWeightedMovingRate(double lambda, long startTime) { | |
| if (lambda < 0.0) { | ||
| throw new IllegalArgumentException("lambda must be non-negative but was " + lambda); | ||
| } | ||
| if (startTime <= 0.0) { | ||
| throw new IllegalArgumentException("startTime must be non-negative but was " + startTime); | ||
| } | ||
| synchronized (this) { | ||
| this.lambda = lambda; | ||
| this.rate = Double.NaN; // should never be used | ||
| this.startTime = startTime; | ||
| this.lastTime = 0; // after an increment, this must be positive, so a zero value indicates we're waiting for the first | ||
| this.lastTime = 0; // should never be used | ||
| this.waitingForFirstIncrement = true; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -80,7 +80,7 @@ public ExponentiallyWeightedMovingRate(double lambda, long startTime) { | |
| */ | ||
| public double getRate(long time) { | ||
| synchronized (this) { | ||
| if (lastTime == 0) { // indicates that no increment has happened yet | ||
| if (waitingForFirstIncrement) { | ||
| return 0.0; | ||
| } else if (time <= lastTime) { | ||
| return rate; | ||
|
|
@@ -104,6 +104,9 @@ public double getRate(long time) { | |
| * instance. It is only non-static because it uses this instance's {@code lambda} and {@code startTime}. | ||
| */ | ||
| public double calculateRateSince(long currentTime, double currentRate, long oldTime, double oldRate) { | ||
| if (oldTime < startTime) { | ||
| oldTime = startTime; | ||
| } | ||
|
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. We need this guard for a few edge cases. |
||
| if (currentTime <= oldTime) { | ||
| return 0.0; | ||
| } | ||
|
|
@@ -127,12 +130,13 @@ public double calculateRateSince(long currentTime, double currentRate, long oldT | |
| */ | ||
| public void addIncrement(double increment, long time) { | ||
| synchronized (this) { | ||
| if (lastTime == 0) { // indicates that this is the first increment | ||
| if (waitingForFirstIncrement) { | ||
| if (time <= startTime) { | ||
| time = startTime + 1; | ||
| } | ||
| // This is the formula for R(t_1) given in subsection 2.6 of the document referenced above: | ||
| rate = increment / expHelper(time - startTime); | ||
| waitingForFirstIncrement = false; | ||
| } else { | ||
| if (time < lastTime) { | ||
| time = lastTime; | ||
|
|
@@ -165,4 +169,12 @@ private double expHelper(double time) { | |
| return time * (1.0 - 0.5 * lambdaTime); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the configured half-life of this instance. The units are the same as all other times in API calls, and the inverse of the | ||
| * units used for the {@code lambda} constructor parameter. If {@code lambda} is {@code 0.0}, returns {@link Double#POSITIVE_INFINITY}. | ||
| */ | ||
| public double getHalfLife() { | ||
| return log(2.0) / lambda; | ||
| } | ||
| } | ||
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are tweaking this class so that it allows times to be zero. This is technically the correct thing to do, since it is allowed for
System.nanoTime()to return zero (though it is vanishingly unlikely!). More importantly, it avoids having to tweak the times used in some tests.As a result, we can no longer use
lastTime == 0as a marker that we're waiting for the first increment, so we add a boolean to do that job explicitly.