Repository navigation
Fix unit conversion in Backoff.decorrelatedJittered that stops backoff from growing - #4929
Open
amalsaidov wants to merge 1 commit into
Open
amalsaidov wants to merge 1 commit into
amalsaidov wants to merge 1 commit into
Conversation
The decorrelated jitter strategy derives each step's range from the previously selected backoff (up to 3x it, capped at maxMs). The conversion of the previous value swapped the TimeUnit arguments: TimeUnit.MILLISECONDS.convert(lastMs, TimeUnit.NANOSECONDS) treats lastMs (already in milliseconds) as nanoseconds, so lastNanos collapses to lastMs / 1_000_000, which is 0 for any sub-second backoff. As a result randRange stays fixed at startNanos and the backoff never grows past 2 * startMs, so it never approaches maxMs and loses the decorrelation the strategy is meant to provide. Convert milliseconds to nanoseconds instead, matching how startMs and maxMs are converted a few lines above. Added a test that pins the regression: with a large max the sequence must climb past 2 * startMs.
codelipenghui
approved these changes
Nov 14, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
Backoff.decorrelatedJitteredis meant to produce a decorrelated jitter stream where each backoff is drawn from a range up to 3x the previously selected value, capped atmaxMs(see the method javadoc and http://www.awsarchitectureblog.com/2015/03/backoff.html).The conversion of the previous backoff into nanoseconds has its
TimeUnitarguments swapped:TimeUnit.MILLISECONDS.convert(d, NANOSECONDS)interpretsdas nanoseconds and returns milliseconds, so it computeslastMs / 1_000_000. SincelastMsis already in milliseconds,lastNanoscollapses to 0 for any sub-second backoff.With
lastNanosstuck at 0,randRange = abs(lastNanos * 3 - startNanos)is alwaysstartNanos, so every emitted backoff stays in[startMs, 2 * startMs)no matter how many steps elapse. The stream never grows towardmaxMsand the decorrelation the strategy is supposed to provide is lost. The other jitter strategies in the same class (exponentialJittered,equalJittered) convert ms to ns correctly withTimeUnit.NANOSECONDS.convert(..., MILLISECONDS).Changes
Backoff.decorrelatedJittered: convert the previous backoff from milliseconds to nanoseconds usingTimeUnit.NANOSECONDS.convert(lastMs, TimeUnit.MILLISECONDS), matching howstartMs/maxMsare converted a few lines above.TestBackoff.testDecorrelatedJitteredGrowsBeyondStart, which fails before the fix (every backoff capped below2 * startMs, observed max 19 forstartMs=10, maxMs=10000) and passes after.Verified by building and testing only the affected module:
AI-assisted; directed, reviewed and tested by the author.