-
Notifications
You must be signed in to change notification settings - Fork 438
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
feat: add exponential backoff to ResumableStream #7664
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
01629bc
feat: add delay to ResumableStream
bshaffer 8acff85
cleanup test
bshaffer 0931f26
fix max retry delay
bshaffer 406021c
fix tests for PHP 8.0
bshaffer 3787737
reset attempts on successful row read, fix conformance tests
bshaffer f8d6db5
fix cs
bshaffer bfe6301
add jitter to retry delay
bshaffer 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 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 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 @@ | ||
<?php | ||
/** | ||
* Copyright 2024, Google LLC All rights reserved. | ||
* | ||
* 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. | ||
*/ | ||
|
||
namespace Google\Cloud\Bigtable\Tests\Unit; | ||
|
||
use Google\Cloud\Bigtable\ResumableStream; | ||
use Google\Cloud\Bigtable\V2\Client\BigtableClient; | ||
use Google\Cloud\Bigtable\V2\ReadRowsRequest; | ||
use Google\Cloud\Bigtable\V2\ReadRowsResponse; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Prophecy\Argument; | ||
use Google\ApiCore\ServerStream; | ||
|
||
/** | ||
* @group bigtable | ||
* @group bigtabledata | ||
*/ | ||
class ResumableStreamTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
const RETRYABLE_CODE = 1234; | ||
|
||
public function testRetryDelayIsResetWhenRowReceived() | ||
{ | ||
$stream = $this->prophesize(ServerStream::class); | ||
$generator1 = function () { | ||
yield new ReadRowsResponse(); | ||
throw new \Exception('This too is retryable!', self::RETRYABLE_CODE); | ||
}; | ||
$generator2 = fn () => yield new ReadRowsResponse(); | ||
$count = 0; | ||
$stream->readAll() | ||
->will(function () use (&$count, $generator1, $generator2) { | ||
// Simlate a call to readRows where the server throws 2 exceptions, reads a row | ||
// successfuly, throws another exception, and reads one more row successfully. | ||
return match ($count++) { | ||
0 => throw new \Exception('This is retryable!', self::RETRYABLE_CODE), | ||
1 => throw new \Exception('This is also retryable!', self::RETRYABLE_CODE), | ||
2 => $generator1(), | ||
3 => $generator2(), | ||
}; | ||
}); | ||
$bigtable = $this->prophesize(BigtableClient::class); | ||
$bigtable->readRows(Argument::type(ReadRowsRequest::class), Argument::type('array')) | ||
->shouldBeCalledTimes(4) | ||
->willReturn($stream->reveal()); | ||
$resumableStream = new ResumableStream( | ||
$bigtable->reveal(), | ||
'readRows', | ||
$this->prophesize(ReadRowsRequest::class)->reveal(), | ||
fn ($request, $callOptions) => [$request, $callOptions], | ||
fn ($exception) => $exception && $exception->getCode() === self::RETRYABLE_CODE | ||
); | ||
|
||
$retries = 0; | ||
$delayFunction = function ($delayFactor) use (&$retries) { | ||
$this->assertEquals(match (++$retries) { | ||
1 => 1, // initial delay | ||
2 => 2, // increment by 1 | ||
3 => 1, // the delay is reset by the successful call | ||
}, $delayFactor); | ||
}; | ||
$refl = new \ReflectionObject($resumableStream); | ||
$refl->getProperty('delayFunction')->setValue($resumableStream, $delayFunction); | ||
|
||
$rows = iterator_to_array($resumableStream->readAll()); | ||
$this->assertEquals(2, count($rows)); | ||
$this->assertEquals(3, $retries); | ||
} | ||
} |
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.
why does this need to be closure?
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.
It's a closure so that we can test its accuracy in the test (see this line).
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.
I think it would be better to use the mock framework to create a spy for this method:
prophesize(
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.
AFAIK, It's not possible to do what you're asking with Prophecy.
You're asking to mock a method in
ResumableStream
, but we are not mocking theResumableStream
class, and partial mocks are not supported in Prophecy.