-
Notifications
You must be signed in to change notification settings - Fork 262
PHPLIB-1719 Exponential backoff and jitter in retry loops #1880
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f67b455
Extract fixed jitter generation function to `Util`
paulinevos f58035a
Implement exponential backoff and jitter in retry loops
paulinevos 3c2c68e
Fix CS
GromNaN a7d06e1
Use RequiresPhpExtension
GromNaN 95b0310
Use hrtime for duration mesurement
GromNaN 511b62e
Fix MAX_RETRIES to 2 as per the client backpressure spec
GromNaN ff8258f
Fix assertion in Prose1 to match the spec
GromNaN 224d2d9
Skip tests requiring errorLabels in configureFailPoint on older servers
GromNaN f9f4b20
Add prose test 4 and drop RequiresPhpExtension from backpressure tests
GromNaN b547d8d
Adapt Prose1 to not depend on C-level jitter
GromNaN 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
67 changes: 67 additions & 0 deletions
67
tests/SpecTests/ClientBackpressure/Prose1_OpRetryExponentialBackoffTest.php
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,67 @@ | ||
| <?php | ||
|
|
||
| namespace MongoDB\Tests\SpecTests\ClientBackpressure; | ||
|
|
||
| use MongoDB\Driver\Exception\ServerException; | ||
| use MongoDB\Driver\Session; | ||
| use MongoDB\Operation\WithTransaction; | ||
| use MongoDB\Tests\SpecTests\FunctionalTestCase; | ||
|
|
||
| use function hrtime; | ||
|
|
||
| /** | ||
| * Prose test 1: Retry operation uses exponential backoff | ||
| * | ||
| * @see https://github.com/mongodb/specifications/blob/master/source/client-backpressure/tests/README.md#test-1-operation-retry-uses-exponential-backoff | ||
| */ | ||
| class Prose1_OpRetryExponentialBackoffTest extends FunctionalTestCase | ||
| { | ||
| public function testOperationRetryUsesExponentialBackoff(): void | ||
| { | ||
| $this->skipIfTransactionsAreNotSupported(); | ||
| $this->skipIfServerVersion('<', '4.3.1', 'Test requires configureFailPoint to support errorLabels'); | ||
|
|
||
| $client = self::createTestClient(); | ||
| $collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName()); | ||
|
|
||
| $callback = static function (Session $session) use ($collection): void { | ||
| $collection->insertOne(['a' => 1], ['session' => $session]); | ||
| }; | ||
|
|
||
| $operation = new WithTransaction($callback); | ||
| $session = $client->startSession(); | ||
|
|
||
| $this->configureFailPoint([ | ||
| 'configureFailPoint' => 'failCommand', | ||
| 'mode' => 'alwaysOn', | ||
| 'data' => [ | ||
| 'failCommands' => ['insert'], | ||
| 'errorCode' => 2, | ||
| 'errorLabels' => ['SystemOverloadedError', 'RetryableError'], | ||
| ], | ||
| ]); | ||
|
GromNaN marked this conversation as resolved.
GromNaN marked this conversation as resolved.
|
||
|
|
||
| $start = hrtime(true); | ||
|
|
||
| try { | ||
| $operation->execute($session); | ||
| $this->fail('Expected exception was not thrown'); | ||
| } catch (ServerException) { | ||
| // Expected exception due to failCommand | ||
| } | ||
|
|
||
| $elapsed = (hrtime(true) - $start) / 1e9; | ||
|
|
||
| /* The spec requires comparing two runs with jitter fixed at 0 and 1 to verify | ||
| * that backoff delay scales with the jitter value (expected difference: ~0.3s). | ||
| * | ||
| * This is not achievable from PHPLIB because the overload retry and its backoff | ||
| * are implemented inside ext-mongodb (C level). WithTransaction only retries on | ||
| * TransientTransactionError, not on SystemOverloadedError, so setFixedJitter() | ||
| * has no effect on the timing of this test. | ||
| * | ||
| * As partial verification, we assert that the operation completed within the | ||
| * maximum possible backoff window: MAX_RETRIES (2) × MAX_BACKOFF (10s) = 20s. */ | ||
| self::assertLessThan(20.0, $elapsed); | ||
|
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. The assertion has been updated to reflect the fact that we cannot force a specific jitter value. |
||
| } | ||
|
GromNaN marked this conversation as resolved.
|
||
| } | ||
71 changes: 71 additions & 0 deletions
71
tests/SpecTests/ClientBackpressure/Prose3_OverloadErrorMaxRetryTest.php
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,71 @@ | ||
| <?php | ||
|
|
||
| namespace MongoDB\Tests\SpecTests\ClientBackpressure; | ||
|
|
||
| use MongoDB\Driver\Exception\RuntimeException; | ||
| use MongoDB\Driver\Monitoring\CommandFailedEvent; | ||
| use MongoDB\Driver\Monitoring\CommandStartedEvent; | ||
| use MongoDB\Driver\Monitoring\CommandSubscriber; | ||
| use MongoDB\Driver\Monitoring\CommandSucceededEvent; | ||
| use MongoDB\Tests\SpecTests\FunctionalTestCase; | ||
|
|
||
| /** | ||
| * Prose test 3: Overload Errors are Retried a Maximum of MAX_RETRIES times | ||
| * | ||
| * @see https://github.com/mongodb/specifications/blob/master/source/client-backpressure/tests/README.md#test-3-overload-errors-are-retried-a-maximum-of-max_retries-times | ||
| */ | ||
| class Prose3_OverloadErrorMaxRetryTest extends FunctionalTestCase | ||
| { | ||
| private const MAX_RETRIES = 2; | ||
|
|
||
| public function testOverloadErrorsAreRetriedMaxRetryTimes(): void | ||
| { | ||
| $this->skipIfServerVersion('<', '4.3.1', 'Test requires configureFailPoint to support errorLabels'); | ||
|
|
||
| $client = self::createTestClient(); | ||
|
GromNaN marked this conversation as resolved.
|
||
| $collection = $client->getCollection($this->getDatabaseName(), $this->getCollectionName()); | ||
|
|
||
| $subscriber = new class implements CommandSubscriber { | ||
| public int $findCommandsStarted = 0; | ||
|
|
||
| public function commandStarted(CommandStartedEvent $event): void | ||
| { | ||
| if ($event->getCommandName() === 'find') { | ||
| $this->findCommandsStarted++; | ||
| } | ||
| } | ||
|
|
||
| public function commandSucceeded(CommandSucceededEvent $event): void | ||
| { | ||
| } | ||
|
|
||
| public function commandFailed(CommandFailedEvent $event): void | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| $client->addSubscriber($subscriber); | ||
|
|
||
| $this->configureFailPoint([ | ||
| 'configureFailPoint' => 'failCommand', | ||
| 'mode' => 'alwaysOn', | ||
| 'data' => [ | ||
| 'failCommands' => ['find'], | ||
| 'errorCode' => 462, // IngressRequestRateLimitExceeded | ||
| 'errorLabels' => ['SystemOverloadedError', 'RetryableError'], | ||
| ], | ||
| ]); | ||
|
GromNaN marked this conversation as resolved.
|
||
|
|
||
| try { | ||
| $collection->find([]); | ||
| $this->fail('Expected RuntimeException was not thrown'); | ||
| } catch (RuntimeException $e) { | ||
| $this->assertTrue($e->hasErrorLabel('RetryableError')); | ||
| $this->assertTrue($e->hasErrorLabel('SystemOverloadedError')); | ||
| } | ||
|
|
||
| $client->removeSubscriber($subscriber); | ||
|
|
||
| $this->assertSame(self::MAX_RETRIES + 1, $subscriber->findCommandsStarted); | ||
| } | ||
| } | ||
71 changes: 71 additions & 0 deletions
71
tests/SpecTests/ClientBackpressure/Prose4_OverloadErrorMaxAdaptiveRetriesTest.php
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,71 @@ | ||
| <?php | ||
|
|
||
| namespace MongoDB\Tests\SpecTests\ClientBackpressure; | ||
|
|
||
| use MongoDB\Driver\Exception\RuntimeException; | ||
| use MongoDB\Driver\Monitoring\CommandFailedEvent; | ||
| use MongoDB\Driver\Monitoring\CommandStartedEvent; | ||
| use MongoDB\Driver\Monitoring\CommandSubscriber; | ||
| use MongoDB\Driver\Monitoring\CommandSucceededEvent; | ||
| use MongoDB\Tests\SpecTests\FunctionalTestCase; | ||
|
|
||
| /** | ||
| * Prose test 4: Overload Errors are Retried a Maximum of maxAdaptiveRetries times when configured | ||
| * | ||
| * @see https://github.com/mongodb/specifications/blob/master/source/client-backpressure/tests/README.md#test-4-overload-errors-are-retried-a-maximum-of-maxadaptiveretries-times-when-configured | ||
| */ | ||
| class Prose4_OverloadErrorMaxAdaptiveRetriesTest extends FunctionalTestCase | ||
| { | ||
| private const MAX_ADAPTIVE_RETRIES = 1; | ||
|
|
||
| public function testOverloadErrorsAreRetriedMaxAdaptiveRetryTimes(): void | ||
| { | ||
| $this->skipIfServerVersion('<', '4.3.1', 'Test requires configureFailPoint to support errorLabels'); | ||
|
|
||
| $client = self::createTestClient(options: ['maxAdaptiveRetries' => self::MAX_ADAPTIVE_RETRIES]); | ||
| $collection = $client->getCollection($this->getDatabaseName(), $this->getCollectionName()); | ||
|
|
||
| $subscriber = new class implements CommandSubscriber { | ||
| public int $findCommandsStarted = 0; | ||
|
|
||
| public function commandStarted(CommandStartedEvent $event): void | ||
| { | ||
| if ($event->getCommandName() === 'find') { | ||
| $this->findCommandsStarted++; | ||
| } | ||
| } | ||
|
|
||
| public function commandSucceeded(CommandSucceededEvent $event): void | ||
| { | ||
| } | ||
|
|
||
| public function commandFailed(CommandFailedEvent $event): void | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| $client->addSubscriber($subscriber); | ||
|
|
||
| $this->configureFailPoint([ | ||
| 'configureFailPoint' => 'failCommand', | ||
| 'mode' => 'alwaysOn', | ||
| 'data' => [ | ||
| 'failCommands' => ['find'], | ||
| 'errorCode' => 462, // IngressRequestRateLimitExceeded | ||
| 'errorLabels' => ['SystemOverloadedError', 'RetryableError'], | ||
| ], | ||
| ]); | ||
|
|
||
| try { | ||
| $collection->find([]); | ||
| $this->fail('Expected RuntimeException was not thrown'); | ||
| } catch (RuntimeException $e) { | ||
| $this->assertTrue($e->hasErrorLabel('RetryableError')); | ||
| $this->assertTrue($e->hasErrorLabel('SystemOverloadedError')); | ||
| } | ||
|
|
||
| $client->removeSubscriber($subscriber); | ||
|
|
||
| $this->assertSame(self::MAX_ADAPTIVE_RETRIES + 1, $subscriber->findCommandsStarted); | ||
| } | ||
| } |
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.
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.
The failpoint only adds
SystemOverloadedErrorandRetryableErrorlabels, butWithTransaction::checkForRetryableError()only retries (and callsbackoff()) for exceptions with theTransientTransactionErrorlabel. As written,execute()will rethrow immediately and jitter/backoff timing assertions won’t be exercising the intended retry path. Consider adjusting the failpoint/error labels (or the operation under test) so it triggers the retry/backoff logic you want to measure.