Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add a callback function on flush_rows #796

Merged
merged 6 commits into from
Jun 8, 2023
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion google/cloud/bigtable/batcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ class MutationsBatcher(object):
:type flush_interval: float
:param flush_interval: (Optional) The interval (in seconds) between asynchronous flush.
Default is 1 second.

:type batch_completed_callback: Callable = None
:param batch_completed_callback: (Optional) A callable for handling responses
after the current batch is sent.
"""

def __init__(
Expand All @@ -200,6 +204,7 @@ def __init__(
flush_count=FLUSH_COUNT,
max_row_bytes=MAX_MUTATION_SIZE,
flush_interval=1,
batch_completed_callback=None,
):
self._rows = _MutationsBatchQueue(
max_mutation_bytes=max_row_bytes, flush_count=flush_count
Expand All @@ -215,6 +220,7 @@ def __init__(
)
self.futures_mapping = {}
self.exceptions = queue.Queue()
self._user_batch_completed_callback = batch_completed_callback

@property
def flush_count(self):
Expand Down Expand Up @@ -337,7 +343,8 @@ def _flush_async(self):
batch_info = _BatchInfo()

def _batch_completed_callback(self, future):
"""Callback for when the mutation has finished.
"""Callback for when the mutation has finished to clean up the current batch
and release items from the flow controller.

Raise exceptions if there's any.
Release the resources locked by the flow control and allow enqueued tasks to be run.
Expand All @@ -357,6 +364,9 @@ def _flush_rows(self, rows_to_flush):
if len(rows_to_flush) > 0:
response = self.table.mutate_rows(rows_to_flush)

if self._user_batch_completed_callback:
self._user_batch_completed_callback(response)

for result in response:
if result.code != 0:
exc = from_grpc_status(result.code, result.message)
Expand Down