Skip to content

Commit

Permalink
fix: ensure attempts increment without exception (#7688)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Sep 20, 2024
1 parent 89f982a commit 95dff6d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
10 changes: 6 additions & 4 deletions src/ResumableStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ public function readAll()
$headers['bigtable-attempt'] = [(string) $totalAttempt];
($this->delayFunction)($currentAttempt);
}

$stream = call_user_func_array(
[$this->gapicClient, $this->method],
[$this->request, ['headers' => $headers] + $this->callOptions]
Expand All @@ -175,11 +174,14 @@ public function readAll()
$currentAttempt = 0; // reset delay and attempt on successful read.
}
} catch (\Exception $ex) {
$totalAttempt++;
$currentAttempt++;
}
// It's possible for the retry function to retry even when `$ex` is null
// (see Table::mutateRowsWithEntries). For this reason, we increment the attemts
// outside the try/catch block.
$totalAttempt++;
$currentAttempt++;
}
} while ((!$this->retryFunction || ($this->retryFunction)($ex)) && $currentAttempt <= $this->retries);
} while (($this->retryFunction)($ex) && $currentAttempt <= $this->retries);
if ($ex !== null) {
throw $ex;
}
Expand Down
20 changes: 11 additions & 9 deletions tests/Unit/SmartRetriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,20 +676,22 @@ public function testMutateRowsOnlyRetriesFailedEntries()
return iterator_to_array($request->getEntries()) == $entries;
}),
Argument::type('array')
)->shouldBeCalledTimes(1)
->willReturn(
$this->serverStream->reveal()
);
)
->shouldBeCalledTimes(1)
->willReturn($this->serverStream->reveal());

$entries = $this->generateEntries(2, 3);

// ensure the bigtable-attempt header is sent in for the next retry.
$this->bigtableClient->mutateRows(
Argument::that(function ($request) use ($entries) {
return iterator_to_array($request->getEntries()) == $entries;
}),
Argument::type('array')
)->shouldBeCalled()
->willReturn(
$this->serverStream->reveal()
);
Argument::withEntry('headers', ['bigtable-attempt' => ['1']] + $this->options['headers'])
)
->shouldBeCalledTimes(1)
->willReturn($this->serverStream->reveal());

$mutations = $this->generateMutations(0, 5);
$this->table->mutateRows($mutations);
}
Expand Down

0 comments on commit 95dff6d

Please sign in to comment.