Skip to content
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(Bigtable): add bigtable-attempt header #7414

Merged
merged 7 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(
) {
$this->metadata = $metadata;

parent::__construct($message, $code);
parent::__construct($message, $code ?? 0);
}

/**
Expand Down
16 changes: 9 additions & 7 deletions Bigtable/src/ResumableStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,22 @@ public function __construct(
*/
public function readAll()
{
$tries = 0;
$argumentFunction = $this->argumentFunction;
$retryFunction = $this->retryFunction;
$attempt = 0;
do {
$ex = null;
list($this->request, $this->callOptions) = $argumentFunction($this->request, $this->callOptions);
list($this->request, $this->callOptions) =
($this->argumentFunction)($this->request, $this->callOptions);

$completed = $this->pluck('requestCompleted', $this->callOptions, false);

if ($completed !== true) {
// Send in "bigtable-attempt" header on each request
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
$headers = $this->callOptions['headers'] ?? [];
$headers['bigtable-attempt'] = [(string) ++$attempt];
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
bshaffer marked this conversation as resolved.
Show resolved Hide resolved

$stream = call_user_func_array(
[$this->gapicClient, $this->method],
[$this->request, $this->callOptions]
[$this->request, ['headers' => $headers] + $this->callOptions]
);

try {
Expand All @@ -148,8 +151,7 @@ public function readAll()
} catch (\Exception $ex) {
}
}
$tries++;
} while ((!$this->retryFunction || $retryFunction($ex)) && $tries <= $this->retries);
} while ((!$this->retryFunction || ($this->retryFunction)($ex)) && $attempt <= $this->retries);
if ($ex !== null) {
throw $ex;
}
Expand Down
29 changes: 28 additions & 1 deletion Bigtable/tests/Unit/SmartRetriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,33 @@ public function testReadRowsShouldRetryForProvidedAttempts()
$iterator->getIterator()->current();
}

public function testReadRowsContainsAttemptHeader()
{
$attempt = 1;
$expectedArgs = $this->options;
$retryingApiException = $this->retryingApiException;
$phpunit = $this;
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
$this->serverStream->readAll()
->shouldBeCalledTimes(2)
->will(function () use (&$attempt, $retryingApiException) {
// throw a retriable exception on the first call
return 1 === $attempt++ ? throw $retryingApiException : [];
});
$this->bigtableClient->readRows(
Argument::type(ReadRowsRequest::class),
Argument::that(function ($callOptions) use (&$attempt) {
$this->assertSame((string) $attempt, $callOptions['headers']['bigtable-attempt'][0] ?? null);
return true;
})
)->shouldBeCalledTimes(2)
->willReturn(
$this->serverStream->reveal()
);

$iterator = $this->table->readRows();
$iterator->getIterator()->current();
}

public function testReadRowsPartialSuccess()
{
$expectedArgs = $this->options;
Expand Down Expand Up @@ -186,7 +213,7 @@ public function testReadRowsWithRowsLimit()
$this->generateRowsResponse(3, 4)
)
);

$allowedRowsLimit = ['5' => 1, '3' => 1];
$this->bigtableClient->readRows(
Argument::that(function ($request) use (&$allowedRowsLimit) {
Expand Down
Loading