From 70dc013cf59ff2f3f2c106dd0bcd349bca8bd005 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 17 Jan 2025 13:22:03 -0800 Subject: [PATCH] fix(Bigtable): ensure appProfileId is sent in requests when set in options (#8020) * fix(Bigtable): ensure appProfileId is sent in requests when set in options * add tests for appProfileId --- Bigtable/src/Table.php | 28 +++++++++----------- Bigtable/tests/Unit/TableTest.php | 44 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 16 deletions(-) diff --git a/Bigtable/src/Table.php b/Bigtable/src/Table.php index f0c606956b7a..cbf0097c3d7f 100644 --- a/Bigtable/src/Table.php +++ b/Bigtable/src/Table.php @@ -168,7 +168,7 @@ public function mutateRows(array $rowMutations, array $options = []) */ public function mutateRow($rowKey, Mutations $mutations, array $options = []) { - list($data, $optionalArgs) = $this->splitOptionalArgs($options); + list($data, $optionalArgs) = $this->splitOptionalArgs($options + $this->options); $data['table_name'] = $this->tableName; $data['row_key'] = $rowKey; $data['mutations'] = $mutations->toProto(); @@ -176,7 +176,6 @@ public function mutateRow($rowKey, Mutations $mutations, array $options = []) new MutateRowRequest(), $data ); - $optionalArgs += $this->options; $this->gapicClient->mutateRow($request, $optionalArgs); } @@ -287,7 +286,7 @@ public function readRows(array $options = []) $rowKeys = $this->pluck('rowKeys', $options, false) ?: []; $ranges = $this->pluck('rowRanges', $options, false) ?: []; $filter = $this->pluck('filter', $options, false) ?: null; - list($data, $optionalArgs) = $this->splitOptionalArgs($options, ['retrySettings']); + list($data, $optionalArgs) = $this->splitOptionalArgs($options + $this->options); array_walk($ranges, function (&$range) { $range = $this->serializer->decodeMessage( @@ -325,7 +324,7 @@ public function readRows(array $options = []) return new ChunkFormatter( $this->gapicClient, $request, - $optionalArgs + $this->options + $optionalArgs ); } @@ -396,7 +395,7 @@ public function readRow($rowKey, array $options = []) */ public function readModifyWriteRow($rowKey, ReadModifyWriteRowRules $rules, array $options = []) { - list($data, $optionalArgs) = $this->splitOptionalArgs($options); + list($data, $optionalArgs) = $this->splitOptionalArgs($options + $this->options); $data['table_name'] = $this->tableName; $data['row_key'] = $rowKey; $data['rules'] = $rules->toProto(); @@ -404,7 +403,7 @@ public function readModifyWriteRow($rowKey, ReadModifyWriteRowRules $rules, arra $request = $this->serializer->decodeMessage(new ReadModifyWriteRowRequest(), $data); $readModifyWriteRowResponse = $this->gapicClient->readModifyWriteRow( $request, - $optionalArgs + $this->options + $optionalArgs ); return $this->convertToArray($readModifyWriteRowResponse->getRow()); @@ -430,14 +429,11 @@ public function readModifyWriteRow($rowKey, ReadModifyWriteRowRules $rules, arra */ public function sampleRowKeys(array $options = []) { - list($data, $optionalArgs) = $this->splitOptionalArgs($options); + list($data, $optionalArgs) = $this->splitOptionalArgs($options + $this->options); $data['table_name'] = $this->tableName; $request = $this->serializer->decodeMessage(new SampleRowKeysRequest(), $data); - $stream = $this->gapicClient->sampleRowKeys( - $request, - $optionalArgs + $this->options - ); + $stream = $this->gapicClient->sampleRowKeys($request, $optionalArgs); foreach ($stream->readAll() as $response) { yield [ @@ -515,21 +511,21 @@ public function checkAndMutateRow($rowKey, array $options = []) throw new \InvalidArgumentException('checkAndMutateRow must have either trueMutations or falseMutations.'); } - list($data, $optionalArgs) = $this->splitOptionalArgs($options); + list($data, $optionalArgs) = $this->splitOptionalArgs($options + $this->options); $data['table_name'] = $this->tableName; $data['row_key'] = $rowKey; $request = $this->serializer->decodeMessage(new CheckAndMutateRowRequest(), $data); return $this->gapicClient->checkAndMutateRow( $request, - $optionalArgs + $this->options + $optionalArgs )->getPredicateMatched(); } private function mutateRowsWithEntries(array $entries, array $options = []) { $rowMutationsFailedResponse = []; - $options = $options + $this->options; + // This function is responsible to modify the $entries before every retry. $argumentFunction = function ($request, $options) use (&$entries, &$rowMutationsFailedResponse) { if (count($rowMutationsFailedResponse) > 0) { @@ -552,7 +548,7 @@ private function mutateRowsWithEntries(array $entries, array $options = []) return false; }; - list($data, $optionalArgs) = $this->splitOptionalArgs($options, ['retrySettings']); + list($data, $optionalArgs) = $this->splitOptionalArgs($options + $this->options); $request = $this->serializer->decodeMessage(new MutateRowsRequest(), $data); $request->setTableName($this->tableName); @@ -631,7 +627,7 @@ private function appendPendingEntryToFailedMutations( $rowMutationsFailedResponse[] = [ 'rowKey' => $entries[$index]->getRowKey(), 'statusCode' => $statusCode, - 'message' => $message + 'message' => $message, ]; } } diff --git a/Bigtable/tests/Unit/TableTest.php b/Bigtable/tests/Unit/TableTest.php index a34170365850..aefc6bcd1bdd 100644 --- a/Bigtable/tests/Unit/TableTest.php +++ b/Bigtable/tests/Unit/TableTest.php @@ -158,6 +158,50 @@ public function testMutateRowsOptionalConfiguration() $this->table->mutateRows($this->rowMutations, $options); } + public function testAppProfileIdInTableConstructor() + { + $this->serverStream->readAll() + ->shouldBeCalled() + ->willReturn( + $this->arrayAsGenerator([]) + ); + $this->bigtableClient->mutateRows( + Argument::that(function (MutateRowsRequest $request) { + return $request->getAppProfileId() === self::APP_PROFILE; + }), + Argument::type('array') + ) + ->shouldBeCalledOnce() + ->willReturn( + $this->serverStream->reveal() + ); + + $this->table->mutateRows($this->rowMutations); + } + + public function testAppProfileIdInMethodOptions() + { + $this->serverStream->readAll() + ->shouldBeCalled() + ->willReturn( + $this->arrayAsGenerator([]) + ); + + $this->bigtableClient->mutateRows( + Argument::that(function (MutateRowsRequest $request) { + return $request->getAppProfileId() === 'app-profile-id-2'; + }), + Argument::type('array') + ) + ->shouldBeCalledOnce() + ->willReturn( + $this->serverStream->reveal() + ); + $this->table->mutateRows($this->rowMutations, [ + 'appProfileId' => 'app-profile-id-2' + ]); + } + public function testMutateRowsFailure() { $statuses = [];