Skip to content

Commit b7ec06b

Browse files
address gemini comments
1 parent 2729a5c commit b7ec06b

6 files changed

Lines changed: 63 additions & 14 deletions

File tree

packages/google-cloud-bigtable/google/cloud/bigtable/data/_async/mutations_batcher.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import atexit
1818
import concurrent.futures
19+
import logging
1920
import time
2021
import warnings
2122
from collections import deque
@@ -57,6 +58,7 @@
5758

5859
# used to make more readable default values
5960
_MB_SIZE = 1024 * 1024
61+
_LOGGER = logging.getLogger(__name__)
6062

6163

6264
@CrossSync.convert_class(sync_name="_FlowControl", add_mapping_for_name="_FlowControl")
@@ -416,7 +418,7 @@ async def _execute_mutate_rows(
416418
list of FailedMutationEntryError objects for mutations that failed.
417419
FailedMutationEntryError objects will not contain index information
418420
"""
419-
statuses = [status_pb2.Status(code=code_pb2.Code.UNKNOWN)] * len(batch)
421+
statuses = [status_pb2.Status(code=code_pb2.UNKNOWN) for _ in range(len(batch))]
420422
try:
421423
operation = CrossSync._MutateRowsOperation(
422424
self._target.client._gapic_client,
@@ -436,14 +438,19 @@ async def _execute_mutate_rows(
436438
subexc.index = None
437439
return list(e.exceptions)
438440
else:
439-
statuses = [status_pb2.Status(code=code_pb2.Code.OK)] * len(batch)
441+
statuses = [status_pb2.Status(code=code_pb2.OK) for _ in range(len(batch))]
440442
finally:
441443
# mark batch as complete in flow control
442444
await self._flow_control.remove_from_flow(batch)
443445

444446
# Call batch done callback with list of statuses.
445447
if self._user_batch_completed_callback:
446-
self._user_batch_completed_callback(statuses)
448+
try:
449+
self._user_batch_completed_callback(statuses)
450+
except Exception as exc:
451+
_LOGGER.warning(
452+
f"Exception raised in user batch completion callback: {exc}"
453+
)
447454
return []
448455

449456
def _add_exceptions(self, excs: list[Exception]):

packages/google-cloud-bigtable/google/cloud/bigtable/data/_helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def _get_statuses_from_mutations_exception_group(
266266
#
267267
# 3. In the case of a RetryExceptionGroup, we use terminal exception in the exception
268268
# group and process that.
269-
statuses = [status_pb2.Status(code=code_pb2.OK)] * batch_size
269+
statuses = [status_pb2.Status(code=code_pb2.OK) for _ in range(batch_size)]
270270
for error in exc_group.exceptions:
271271
if isinstance(error.index, int) and 0 <= error.index < len(statuses):
272272
cause = error.__cause__
@@ -297,7 +297,7 @@ def _get_status(exc: Optional[Exception]) -> status_pb2.Status:
297297
)
298298

299299
return status_pb2.Status(
300-
code=code_pb2.Code.UNKNOWN,
300+
code=code_pb2.UNKNOWN,
301301
message=str(exc) if exc else "An unknown error has occurred",
302302
)
303303

packages/google-cloud-bigtable/google/cloud/bigtable/data/_sync_autogen/mutations_batcher.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import atexit
2121
import concurrent.futures
22+
import logging
2223
import time
2324
import warnings
2425
from collections import deque
@@ -50,6 +51,7 @@
5051
)
5152
from google.cloud.bigtable.data.mutations import RowMutationEntry
5253
_MB_SIZE = 1024 * 1024
54+
_LOGGER = logging.getLogger(__name__)
5355

5456

5557
@CrossSync._Sync_Impl.add_mapping_decorator("_FlowControl")
@@ -361,7 +363,7 @@ def _execute_mutate_rows(
361363
list[FailedMutationEntryError]:
362364
list of FailedMutationEntryError objects for mutations that failed.
363365
FailedMutationEntryError objects will not contain index information"""
364-
statuses = [status_pb2.Status(code=code_pb2.Code.UNKNOWN)] * len(batch)
366+
statuses = [status_pb2.Status(code=code_pb2.UNKNOWN) for _ in range(len(batch))]
365367
try:
366368
operation = CrossSync._Sync_Impl._MutateRowsOperation(
367369
self._target.client._gapic_client,
@@ -379,11 +381,16 @@ def _execute_mutate_rows(
379381
subexc.index = None
380382
return list(e.exceptions)
381383
else:
382-
statuses = [status_pb2.Status(code=code_pb2.Code.OK)] * len(batch)
384+
statuses = [status_pb2.Status(code=code_pb2.OK) for _ in range(len(batch))]
383385
finally:
384386
self._flow_control.remove_from_flow(batch)
385387
if self._user_batch_completed_callback:
386-
self._user_batch_completed_callback(statuses)
388+
try:
389+
self._user_batch_completed_callback(statuses)
390+
except Exception as exc:
391+
_LOGGER.warning(
392+
f"Exception raised in user batch completion callback: {exc}"
393+
)
387394
return []
388395

389396
def _add_exceptions(self, excs: list[Exception]):

packages/google-cloud-bigtable/google/cloud/bigtable/table.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,10 @@ def mutate_rows(self, rows, retry=DEFAULT_RETRY, timeout=DEFAULT):
782782
mutation_entries = [
783783
RowMutationEntry(row.row_key, row._get_mutations()) for row in rows
784784
]
785-
return_statuses = [status_pb2.Status(code=code_pb2.Code.UNKNOWN)] * len(
786-
mutation_entries
787-
)
785+
return_statuses = [
786+
status_pb2.Status(code=code_pb2.UNKNOWN)
787+
for _ in range(len(mutation_entries))
788+
]
788789

789790
try:
790791
self._table_impl.bulk_mutate_rows(
@@ -798,9 +799,10 @@ def mutate_rows(self, rows, retry=DEFAULT_RETRY, timeout=DEFAULT):
798799
mut_exc_group, len(mutation_entries)
799800
)
800801
else:
801-
return_statuses = [status_pb2.Status(code=code_pb2.Code.OK)] * len(
802-
mutation_entries
803-
)
802+
return_statuses = [
803+
status_pb2.Status(code=code_pb2.OK)
804+
for _ in range(len(mutation_entries))
805+
]
804806

805807
return return_statuses
806808

packages/google-cloud-bigtable/tests/unit/data/_async/test_mutations_batcher.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,22 @@ async def test__execute_mutate_rows_batch_completed_callback_errors(self):
10591059
assert result[0].index is None
10601060
assert result[1].index is None
10611061

1062+
@CrossSync.pytest
1063+
async def test__execute_mutate_rows_batch_completed_callback_exception(self):
1064+
with mock.patch.object(CrossSync, "_MutateRowsOperation") as mutate_rows:
1065+
mutate_rows.return_value = CrossSync.Mock()
1066+
table = mock.Mock()
1067+
table.default_mutate_rows_operation_timeout = 17
1068+
table.default_mutate_rows_attempt_timeout = 13
1069+
table.default_mutate_rows_retryable_errors = ()
1070+
callback = mock.Mock(side_effect=RuntimeError("callback failed"))
1071+
async with self._make_one(table) as instance:
1072+
instance._user_batch_completed_callback = callback
1073+
batch = [self._make_mutation()]
1074+
result = await instance._execute_mutate_rows(batch, mock.Mock())
1075+
callback.assert_called_once()
1076+
assert result == []
1077+
10621078
@CrossSync.pytest
10631079
async def test__raise_exceptions(self):
10641080
"""Raise exceptions and reset error state"""

packages/google-cloud-bigtable/tests/unit/data/_sync_autogen/test_mutations_batcher.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,23 @@ def test__execute_mutate_rows_batch_completed_callback_errors(self):
940940
assert result[0].index is None
941941
assert result[1].index is None
942942

943+
def test__execute_mutate_rows_batch_completed_callback_exception(self):
944+
with mock.patch.object(
945+
CrossSync._Sync_Impl, "_MutateRowsOperation"
946+
) as mutate_rows:
947+
mutate_rows.return_value = CrossSync._Sync_Impl.Mock()
948+
table = mock.Mock()
949+
table.default_mutate_rows_operation_timeout = 17
950+
table.default_mutate_rows_attempt_timeout = 13
951+
table.default_mutate_rows_retryable_errors = ()
952+
callback = mock.Mock(side_effect=RuntimeError("callback failed"))
953+
with self._make_one(table) as instance:
954+
instance._user_batch_completed_callback = callback
955+
batch = [self._make_mutation()]
956+
result = instance._execute_mutate_rows(batch, mock.Mock())
957+
callback.assert_called_once()
958+
assert result == []
959+
943960
def test__raise_exceptions(self):
944961
"""Raise exceptions and reset error state"""
945962
from google.cloud.bigtable.data import exceptions

0 commit comments

Comments
 (0)