-
Notifications
You must be signed in to change notification settings - Fork 1.5k
JAVA-5950 Update Transactions Convenient API with exponential backoff on retries #1852
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
Open
nhachicha
wants to merge
29
commits into
mongodb:backpressure
Choose a base branch
from
nhachicha:nh/withTransaction_delay
base: backpressure
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+275
−9
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
bd98229
JAVA-5950 - Update Transactions Convenient API with exponential back…
nhachicha 72cd714
Simplifying test, clean up.
nhachicha 573c86c
Fixing test
nhachicha ecf9b4d
Update driver-sync/src/main/com/mongodb/client/internal/ClientSession…
nhachicha 68dbece
retrigger checks
nhachicha 7cfc4c4
retrigger checks
nhachicha cd96a49
retrigger checks
nhachicha 82dc8b1
retrigger checks
nhachicha 558bfad
test cleanup
nhachicha fbe881e
retrigger checks
nhachicha d89c714
Test cleanup
nhachicha 2eccff0
retrigger checks
nhachicha c406ae7
Update the implementation according to the spec
nhachicha a28ce98
Added prose test
nhachicha 27ddef9
Flaky test
nhachicha 3bef60a
Remove extra Test annotation
nhachicha b051701
Throwing correct exception when CSOT is used
nhachicha 90ec4d5
Simplifying implementation by relying on CSOT to throw when timeout i…
nhachicha 19f129c
Fixing implementation according to spec changes in JAVA-6046 and http…
nhachicha a7d4ee7
Update driver-sync/src/test/functional/com/mongodb/client/WithTransac…
nhachicha 7222af9
Update driver-sync/src/test/functional/com/mongodb/client/WithTransac…
nhachicha 5d5506b
Update driver-sync/src/test/functional/com/mongodb/client/WithTransac…
nhachicha 028e144
Update driver-sync/src/test/functional/com/mongodb/client/WithTransac…
nhachicha b68100d
Update driver-sync/src/test/functional/com/mongodb/client/WithTransac…
nhachicha b70df05
Update driver-sync/src/test/functional/com/mongodb/client/WithTransac…
nhachicha c2c1d44
Update driver-sync/src/test/functional/com/mongodb/client/WithTransac…
nhachicha 4dadb70
Update driver-core/src/main/com/mongodb/internal/ExponentialBackoff.java
nhachicha b397236
PR feedback
nhachicha 5c2145c
remove annotation
nhachicha 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
86 changes: 86 additions & 0 deletions
86
driver-core/src/main/com/mongodb/internal/time/ExponentialBackoff.java
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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * Copyright 2008-present MongoDB, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.mongodb.internal.time; | ||
|
|
||
| import com.mongodb.internal.VisibleForTesting; | ||
|
|
||
| import java.util.concurrent.ThreadLocalRandom; | ||
| import java.util.function.DoubleSupplier; | ||
|
|
||
| import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE; | ||
|
|
||
| /** | ||
| * Implements exponential backoff with jitter for retry scenarios. | ||
| */ | ||
| public enum ExponentialBackoff { | ||
| TRANSACTION(5.0, 500.0, 1.5); | ||
|
|
||
| private final double baseMs, maxMs, growth; | ||
|
|
||
| // TODO remove this global state once https://jira.mongodb.org/browse/JAVA-6060 is done | ||
| private static DoubleSupplier testJitterSupplier = null; | ||
|
|
||
| ExponentialBackoff(final double baseMs, final double maxMs, final double growth) { | ||
| this.baseMs = baseMs; | ||
| this.maxMs = maxMs; | ||
| this.growth = growth; | ||
| } | ||
|
|
||
| /** | ||
| * Calculate the next delay in milliseconds based on the retry count. | ||
| * | ||
| * @param retryCount The number of retries that have occurred. | ||
| * @return The calculated delay in milliseconds. | ||
| */ | ||
| public long calculateDelayBeforeNextRetryMs(final int retryCount) { | ||
| double jitter = testJitterSupplier != null | ||
| ? testJitterSupplier.getAsDouble() | ||
| : ThreadLocalRandom.current().nextDouble(); | ||
| double backoff = Math.min(baseMs * Math.pow(growth, retryCount), maxMs); | ||
| return Math.round(jitter * backoff); | ||
| } | ||
|
|
||
| /** | ||
| * Calculate the next delay in milliseconds based on the retry count and a provided jitter. | ||
| * | ||
| * @param retryCount The number of retries that have occurred. | ||
| * @param jitter A double in the range [0, 1) to apply as jitter. | ||
| * @return The calculated delay in milliseconds. | ||
| */ | ||
| public long calculateDelayBeforeNextRetryMs(final int retryCount, final double jitter) { | ||
| double backoff = Math.min(baseMs * Math.pow(growth, retryCount), maxMs); | ||
| return Math.round(jitter * backoff); | ||
| } | ||
|
|
||
| /** | ||
| * Set a custom jitter supplier for testing purposes. | ||
| * | ||
| * @param supplier A DoubleSupplier that returns values in [0, 1) range. | ||
| */ | ||
| @VisibleForTesting(otherwise = PRIVATE) | ||
| public static void setTestJitterSupplier(final DoubleSupplier supplier) { | ||
| testJitterSupplier = supplier; | ||
| } | ||
|
|
||
| /** | ||
| * Clear the test jitter supplier, reverting to default ThreadLocalRandom behavior. | ||
| */ | ||
| @VisibleForTesting(otherwise = PRIVATE) | ||
| public static void clearTestJitterSupplier() { | ||
| testJitterSupplier = null; | ||
| } | ||
| } |
73 changes: 73 additions & 0 deletions
73
driver-core/src/test/unit/com/mongodb/internal/ExponentialBackoffTest.java
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 |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright 2008-present MongoDB, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.mongodb.internal; | ||
|
|
||
| import com.mongodb.internal.time.ExponentialBackoff; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| public class ExponentialBackoffTest { | ||
|
|
||
| @Test | ||
| void testTransactionRetryBackoff() { | ||
| // Test that the backoff sequence follows the expected pattern with growth factor 1.5 | ||
| // Expected sequence (without jitter): 5, 7.5, 11.25, ... | ||
| // With jitter, actual values will be between 0 and these maxima | ||
| double[] expectedMaxValues = {5.0, 7.5, 11.25, 16.875, 25.3125, 37.96875, 56.953125, 85.4296875, 128.14453125, 192.21679688, 288.32519531, 432.48779297, 500.0}; | ||
|
|
||
| ExponentialBackoff backoff = ExponentialBackoff.TRANSACTION; | ||
| for (int retry = 0; retry < expectedMaxValues.length; retry++) { | ||
| long delay = backoff.calculateDelayBeforeNextRetryMs(retry); | ||
| assertTrue(delay >= 0 && delay <= Math.round(expectedMaxValues[retry]), String.format("Retry %d: delay should be 0-%d ms, got: %d", retry, Math.round(expectedMaxValues[retry]), delay)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testTransactionRetryBackoffRespectsMaximum() { | ||
| ExponentialBackoff backoff = ExponentialBackoff.TRANSACTION; | ||
|
|
||
| // Even at high retry counts, delay should never exceed 500ms | ||
| for (int retry = 0; retry < 25; retry++) { | ||
| long delay = backoff.calculateDelayBeforeNextRetryMs(retry); | ||
| assertTrue(delay >= 0 && delay <= 500, String.format("Retry %d: delay should be capped at 500 ms, got: %d ms", retry, delay)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testCustomJitter() { | ||
| ExponentialBackoff backoff = ExponentialBackoff.TRANSACTION; | ||
|
|
||
| // Expected delays with jitter=1.0 and growth factor 1.5 | ||
| double[] expectedDelays = {5.0, 7.5, 11.25, 16.875, 25.3125, 37.96875, 56.953125, 85.4296875, 128.14453125, 192.21679688, 288.32519531, 432.48779297, 500.0}; | ||
| double jitter = 1.0; | ||
|
|
||
| for (int retry = 0; retry < expectedDelays.length; retry++) { | ||
| long delay = backoff.calculateDelayBeforeNextRetryMs(retry, jitter); | ||
| long expected = Math.round(expectedDelays[retry]); | ||
| assertEquals(expected, delay, String.format("Retry %d: with jitter=1.0, delay should be %d ms", retry, expected)); | ||
| } | ||
|
|
||
| // With jitter = 0, all delays should be 0 | ||
| jitter = 0; | ||
| for (int retry = 0; retry < 10; retry++) { | ||
| long delay = backoff.calculateDelayBeforeNextRetryMs(retry, jitter); | ||
| assertEquals(0, delay, "With jitter=0, delay should always be 0 ms"); | ||
| } | ||
| } | ||
| } |
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
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.
[just a comment on a code that wasn't changed in this PR]
I have just noticed this
ClientSessionClock- it uses non-monotonic clock. Horrendous.