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

Feat: Threaded MutationsBatcher #722

Merged
merged 24 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test for the batcher time interval
  • Loading branch information
Mariatta committed Apr 5, 2023
commit e6573214092d0db2718bf8c31e19bc9c54761c24
3 changes: 2 additions & 1 deletion google/cloud/bigtable/batcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ def __init__(
self._executor = concurrent.futures.ThreadPoolExecutor()
self._is_open = True
atexit.register(self.close)
threading.Timer(flush_interval, self.flush).start()
self._timer = threading.Timer(flush_interval, self.flush)
self._timer.start()

@property
def flush_count(self):
Expand Down
21 changes: 20 additions & 1 deletion tests/unit/test_batcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@


import mock
import time

import pytest

from google.cloud.bigtable.row import DirectRow
Expand Down Expand Up @@ -180,12 +182,29 @@ def test_mutations_batcher_mutate_after_batcher_closed_raise_error():

assert table.mutation_calls == 0
with pytest.raises(BatcherIsClosedError):
mutation_batcher.close()
row = DirectRow(row_key=b"row_key")
row.set_cell("cf1", b"c1", 1)
mutation_batcher.mutate(row)


@mock.patch("google.cloud.bigtable.batcher.MutationsBatcher.flush")
def test_mutations_batcher_flush_interval(mocked_flush):
table = _Table(TABLE_NAME)
flush_interval = 0.5
mutation_batcher = MutationsBatcher(table=table, flush_interval=flush_interval)

assert mutation_batcher._timer.interval == flush_interval
mocked_flush.assert_not_called()

time.sleep(0.4)
mocked_flush.assert_not_called()

time.sleep(0.1)
mocked_flush.assert_called_once_with()

mutation_batcher.close()


class _Instance(object):
def __init__(self, client=None):
self._client = client
Expand Down