Skip to content

Commit

Permalink
feat(spanner): support max_commit_delay in Spanner transactions (#7026)
Browse files Browse the repository at this point in the history
  • Loading branch information
nginsberg-google committed Feb 2, 2024
1 parent e817671 commit 7e7440d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Spanner/src/Operation.php
Expand Up @@ -124,6 +124,9 @@ public function deleteMutation($table, KeySet $keySet)
* @type string $transactionId The ID of the transaction.
* @type bool $returnCommitStats If true, return the full response.
* **Defaults to** `false`.
* @type Duration $maxCommitDelay The amount of latency this request
* is willing to incur in order to improve throughput.
* **Defaults to** null.
* @type array $requestOptions Request options.
* For more information on available options, please see
* [the upstream documentation](https://cloud.google.com/spanner/docs/reference/rest/v1/RequestOptions).
Expand Down Expand Up @@ -151,6 +154,9 @@ public function commit(Session $session, array $mutations, array $options = [])
* @type string $transactionId The ID of the transaction.
* @type bool $returnCommitStats If true, return the full response.
* **Defaults to** `false`.
* @type Duration $maxCommitDelay The amount of latency this request
* is willing to incur in order to improve throughput.
* **Defaults to** null.
* @type array $requestOptions Request options.
* For more information on available options, please see
* [the upstream documentation](https://cloud.google.com/spanner/docs/reference/rest/v1/RequestOptions).
Expand Down
3 changes: 3 additions & 0 deletions Spanner/src/Transaction.php
Expand Up @@ -593,6 +593,9 @@ public function rollback(array $options = [])
* @type bool $returnCommitStats If true, commit statistics will be
* returned and accessible via {@see \Google\Cloud\Spanner\Transaction::getCommitStats()}.
* **Defaults to** `false`.
* @type Duration $maxCommitDelay The amount of latency this request
* is willing to incur in order to improve throughput.
* **Defaults to** null.
* @type array $requestOptions Request options.
* For more information on available options, please see
* [the upstream documentation](https://cloud.google.com/spanner/docs/reference/rest/v1/RequestOptions).
Expand Down
31 changes: 31 additions & 0 deletions Spanner/tests/Unit/OperationTest.php
Expand Up @@ -23,6 +23,7 @@
use Google\Cloud\Spanner\Batch\QueryPartition;
use Google\Cloud\Spanner\Batch\ReadPartition;
use Google\Cloud\Spanner\Database;
use Google\Cloud\Spanner\Duration;
use Google\Cloud\Spanner\KeyRange;
use Google\Cloud\Spanner\KeySet;
use Google\Cloud\Spanner\Operation;
Expand Down Expand Up @@ -164,6 +165,36 @@ public function testCommitWithReturnCommitStats()
], $res[1]);
}

public function testCommitWithMaxCommitDelay()
{
$duration = new Duration(0, 100000000);
$mutations = [
$this->operation->mutation(Operation::OP_INSERT, 'Posts', [
'foo' => 'bar'
])
];

$this->connection->commit(Argument::allOf(
Argument::withEntry('mutations', $mutations),
Argument::withEntry('transactionId', 'foo'),
Argument::withEntry('maxCommitDelay', $duration)
))->shouldBeCalled()->willReturn([
'commitTimestamp' => self::TIMESTAMP,
]);

$this->operation->___setProperty('connection', $this->connection->reveal());

$res = $this->operation->commitWithResponse($this->session, $mutations, [
'transactionId' => 'foo',
'maxCommitDelay' => $duration,
]);

$this->assertInstanceOf(Timestamp::class, $res[0]);
$this->assertEquals([
'commitTimestamp' => self::TIMESTAMP,
], $res[1]);
}

public function testCommitWithExistingTransaction()
{
$mutations = [
Expand Down
32 changes: 32 additions & 0 deletions Spanner/tests/Unit/TransactionTest.php
Expand Up @@ -22,6 +22,7 @@
use Google\Cloud\Core\TimeTrait;
use Google\Cloud\Spanner\BatchDmlResult;
use Google\Cloud\Spanner\Database;
use Google\Cloud\Spanner\Duration;
use Google\Cloud\Spanner\KeySet;
use Google\Cloud\Spanner\Operation;
use Google\Cloud\Spanner\Result;
Expand Down Expand Up @@ -527,6 +528,37 @@ public function testCommitWithReturnCommitStats()
$this->assertEquals(['mutationCount' => 1], $this->transaction->getCommitStats());
}

public function testCommitWithMaxCommitDelay()
{
$duration = new Duration(0, 100000000);
$this->transaction->insert('Posts', ['foo' => 'bar']);

$mutations = $this->transaction->___getProperty('mutations');

$operation = $this->prophesize(Operation::class);
$operation->commitWithResponse(
$this->session,
$mutations,
[
'transactionId' => self::TRANSACTION,
'returnCommitStats' => true,
'maxCommitDelay' => $duration,
'requestOptions' => [
'transactionTag' => self::TRANSACTION_TAG
]
]
)->shouldBeCalled()->willReturn($this->commitResponseWithCommitStats());

$this->transaction->___setProperty('operation', $operation->reveal());

$this->transaction->commit([
'returnCommitStats' => true,
'maxCommitDelay' => $duration
]);

$this->assertEquals(['mutationCount' => 1], $this->transaction->getCommitStats());
}

public function testCommitInvalidState()
{
$this->expectException(\BadMethodCallException::class);
Expand Down

0 comments on commit 7e7440d

Please sign in to comment.