Skip to content

Commit

Permalink
fix(Bigtable): ensure appProfileId is sent in requests when set in op…
Browse files Browse the repository at this point in the history
…tions (#8020)

* fix(Bigtable): ensure appProfileId is sent in requests when set in options

* add tests for appProfileId
  • Loading branch information
bshaffer authored Jan 17, 2025
1 parent a617dfa commit 70dc013
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
28 changes: 12 additions & 16 deletions Bigtable/src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,14 @@ 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();
$request = $this->serializer->decodeMessage(
new MutateRowRequest(),
$data
);
$optionalArgs += $this->options;

$this->gapicClient->mutateRow($request, $optionalArgs);
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -325,7 +324,7 @@ public function readRows(array $options = [])
return new ChunkFormatter(
$this->gapicClient,
$request,
$optionalArgs + $this->options
$optionalArgs
);
}

Expand Down Expand Up @@ -396,15 +395,15 @@ 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();

$request = $this->serializer->decodeMessage(new ReadModifyWriteRowRequest(), $data);
$readModifyWriteRowResponse = $this->gapicClient->readModifyWriteRow(
$request,
$optionalArgs + $this->options
$optionalArgs
);

return $this->convertToArray($readModifyWriteRowResponse->getRow());
Expand All @@ -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 [
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -631,7 +627,7 @@ private function appendPendingEntryToFailedMutations(
$rowMutationsFailedResponse[] = [
'rowKey' => $entries[$index]->getRowKey(),
'statusCode' => $statusCode,
'message' => $message
'message' => $message,
];
}
}
Expand Down
44 changes: 44 additions & 0 deletions Bigtable/tests/Unit/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down

0 comments on commit 70dc013

Please sign in to comment.