Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
1d02154
added initial implementation of mutate_rows
daniel-sanche Apr 24, 2023
ab63cba
implemented mutation models
daniel-sanche Apr 24, 2023
cf9daa5
added retries to mutate_row
daniel-sanche Apr 24, 2023
1247da4
return exception group if possible
daniel-sanche Apr 24, 2023
3b3ed8c
check for idempotence
daniel-sanche Apr 24, 2023
5d20037
initial implementation for bulk_mutations
daniel-sanche Apr 24, 2023
3d322a1
include successes in bulk mutation error message
daniel-sanche Apr 24, 2023
a31232b
fixed style checks
daniel-sanche Apr 24, 2023
8da2d65
added basic system tests
daniel-sanche Apr 24, 2023
2b89d9c
added unit tests for mutate_row
daniel-sanche Apr 25, 2023
47c5985
ran blacken
daniel-sanche Apr 25, 2023
38fdcd7
improved exceptions
daniel-sanche Apr 25, 2023
504d2d8
added bulk_mutate_rows unit tests
daniel-sanche Apr 25, 2023
b16067f
ran blacken
daniel-sanche Apr 25, 2023
3ab1405
support __new___ for exceptions for python3.11+
daniel-sanche Apr 25, 2023
0a6c0c6
added exception unit tests
daniel-sanche Apr 25, 2023
ec043cf
makde exceptions tuple
daniel-sanche Apr 26, 2023
518530e
got exceptions to print consistently across versions
daniel-sanche Apr 26, 2023
9624729
added test for 311 rich traceback
daniel-sanche Apr 27, 2023
3087081
moved retryable row mutations to new file
daniel-sanche Apr 27, 2023
9df588f
use index map
daniel-sanche Apr 27, 2023
7ed8be3
added docstring
daniel-sanche Apr 27, 2023
2536cc4
added predicate check to failed mutations
daniel-sanche Apr 27, 2023
1f6875c
added _mutate_rows tests
daniel-sanche Apr 27, 2023
1ea24e6
improved client tests
daniel-sanche Apr 27, 2023
25ca2d2
refactored to loop by raising exception
daniel-sanche Apr 28, 2023
c0787db
refactored retry deadline logic into shared wrapper
daniel-sanche Apr 28, 2023
3ed5c3d
ran black
daniel-sanche Apr 28, 2023
a91fbcb
pulled in table default timeouts
daniel-sanche Apr 28, 2023
df8a058
added tests for shared deadline parsing function
daniel-sanche Apr 28, 2023
b866b57
added tests for mutation models
daniel-sanche Apr 28, 2023
54a4d43
fixed linter errors
daniel-sanche Apr 28, 2023
bd51dc4
added tests for BulkMutationsEntry
daniel-sanche Apr 28, 2023
921b05a
improved mutations documentation
daniel-sanche Apr 28, 2023
82ea61f
refactored mutate_rows logic into helper function
daniel-sanche May 2, 2023
fa42b86
implemented callbacks for mutate_rows
daniel-sanche May 2, 2023
01a16f3
made exceptions into a tuple
daniel-sanche May 5, 2023
6140acb
remove aborted from retryable errors
daniel-sanche May 22, 2023
36ba2b6
improved SetCell mutation
daniel-sanche May 22, 2023
b3c9017
fixed mutations tests
daniel-sanche May 22, 2023
cac9e2d
SetCell timestamps use millisecond precision
daniel-sanche May 22, 2023
34b051f
renamed BulkMutationsEntry to RowMutationEntry
daniel-sanche May 22, 2023
63ac35c
Merge branch 'v3' into mutate_rows
daniel-sanche May 24, 2023
a51201c
added metadata to mutate rows and bulk mutate rows
daniel-sanche May 25, 2023
a21bebf
moved _convert_retry_deadline wrapper from exceptions into _helpers
daniel-sanche May 25, 2023
4ca89d9
fixed system tests
daniel-sanche May 25, 2023
b240ee1
only handle precision adjustment when creating timestamp
daniel-sanche May 26, 2023
cb0e951
added _from_dict for mutation models
daniel-sanche May 26, 2023
a9cf385
rpc timeouts adjust when approaching operation_timeout
daniel-sanche May 26, 2023
eddc1c9
pass table instead of request dict
daniel-sanche May 26, 2023
f8b26aa
refactoring mutate rows
daniel-sanche May 27, 2023
5b80dc5
made on_terminal_state into coroutine
daniel-sanche May 27, 2023
9e5b80a
fixed style issues
daniel-sanche May 27, 2023
f7539f6
moved callback rewriting into retryable attempt
daniel-sanche May 27, 2023
e77a4fa
fixed tests
daniel-sanche May 27, 2023
4e19ed0
pop successful mutations from error dict
daniel-sanche May 30, 2023
920e4b7
removed unneeded check
daniel-sanche May 30, 2023
725f5ff
refactoring
daniel-sanche May 30, 2023
1054bc4
pass list of exceptions in callback
daniel-sanche May 30, 2023
f39a891
raise error in unexpected state
daniel-sanche May 30, 2023
1d97135
removed callback
daniel-sanche May 31, 2023
88e2bf5
refactoring mutation attempt into class
daniel-sanche May 31, 2023
a3c0166
use partial function
daniel-sanche May 31, 2023
70c35ef
renamed class
daniel-sanche May 31, 2023
e00f592
added comments
daniel-sanche Jun 1, 2023
18af78a
added tests
daniel-sanche Jun 1, 2023
23e84f5
improved helpers
daniel-sanche Jun 1, 2023
56fdf7c
refactored operation into class only
daniel-sanche Jun 5, 2023
aca31f0
restructured how remaining indices are tracked
daniel-sanche Jun 5, 2023
5a5d541
fixed tests
daniel-sanche Jun 5, 2023
afed731
added docstrings
daniel-sanche Jun 5, 2023
2396ec8
moved index deletion to end of block
daniel-sanche Jun 6, 2023
3d441a2
added comment to exception types
daniel-sanche Jun 6, 2023
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
Prev Previous commit
Next Next commit
added _mutate_rows tests
  • Loading branch information
daniel-sanche committed Apr 27, 2023
commit 1f6875cd2aca49d23efc4a086151bbe85b03034c
148 changes: 148 additions & 0 deletions tests/unit/test__mutate_rows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
import pytest
import sys

import google.cloud.bigtable.exceptions as bigtable_exceptions
from google.cloud.bigtable_v2.types import MutateRowsResponse
from google.rpc import status_pb2

# try/except added for compatibility with python < 3.8
try:
from unittest import mock
from unittest.mock import AsyncMock # type: ignore
except ImportError: # pragma: NO COVER
import mock # type: ignore
from mock import AsyncMock # type: ignore

class Test_MutateRowsRetryableAttempt():

async def _mock_stream(self, mutation_dict, error_dict):
for idx, entry in mutation_dict.items():
code = error_dict.get(idx, 0)
yield MutateRowsResponse(
entries=[
MutateRowsResponse.Entry(
index=idx,
status=status_pb2.Status(code=code)
)
]
)

def _make_mock_client(self, mutation_dict, error_dict=None):
client = mock.Mock()
client.mutate_rows = AsyncMock()
if error_dict is None:
error_dict = {}
client.mutate_rows.side_effect = lambda *args, **kwargs: self._mock_stream(mutation_dict, error_dict)
return client

@pytest.mark.asyncio
async def test_single_entry_success(self):
"""Test mutating a single entry"""
from google.cloud.bigtable._mutate_rows import _mutate_rows_retryable_attempt
mutation = mock.Mock()
mutations = {0: mutation}
client = self._make_mock_client(mutations)
errors = {0: []}
expected_request = {"test": "data"}
expected_timeout = 9
await _mutate_rows_retryable_attempt(client, expected_request, expected_timeout, mutations, errors, lambda x: False)
assert mutations[0] is None
assert errors[0] == []
assert client.mutate_rows.call_count == 1
args, kwargs = client.mutate_rows.call_args
assert kwargs['timeout'] == expected_timeout
assert args[0]["test"] == "data"
assert args[0]["entries"] == [mutation._to_dict()]

@pytest.mark.asyncio
async def test_empty_request(self):
"""Calling with no mutations should result in no calls"""
from google.cloud.bigtable._mutate_rows import _mutate_rows_retryable_attempt
client = self._make_mock_client({})
await _mutate_rows_retryable_attempt(client, {}, None, {}, {}, lambda x: False)
assert client.mutate_rows.call_count == 0

@pytest.mark.asyncio
async def test_eventual_success(self):
"""Fail at first, but eventually succeed"""
from google.cloud.bigtable._mutate_rows import _mutate_rows_retryable_attempt
mutation = mock.Mock()
mutations = {0: mutation}
client = self._make_mock_client(mutations)
# fail 10 times, then succeed
client.mutate_rows.side_effect = [self._mock_stream(mutations, {0: 400})] * 10 + [self._mock_stream(mutations, {})]
errors = {0: []}
expected_request = {}
expected_timeout = 9
await _mutate_rows_retryable_attempt(client, expected_request, expected_timeout, mutations, errors, lambda x: True)
assert mutations[0] is None
assert errors[0] == []
assert client.mutate_rows.call_count == 11
args, kwargs = client.mutate_rows.call_args
assert kwargs['timeout'] == expected_timeout
assert args[0]["entries"] == [mutation._to_dict()]

@pytest.mark.asyncio
async def test_partial_success(self):
"""Some entries succeed, but one fails. Should report the proper index"""
from google.cloud.bigtable._mutate_rows import _mutate_rows_retryable_attempt
success_mutation = mock.Mock()
success_mutation_2 = mock.Mock()
failure_mutation = mock.Mock()
mutations = {0: success_mutation, 1: failure_mutation, 2: success_mutation_2}
errors = {0: [], 1: [], 2: []}
client = self._make_mock_client(mutations, error_dict={1: 400})
# raise retryable error 3 times, then raise non-retryable error
client.mutate_rows.side_effect = [self._mock_stream(mutations, {1: 300}), self._mock_stream({0: failure_mutation}, {0: 400}), self._mock_stream({0: failure_mutation}, {0: 400}), self._mock_stream({0: failure_mutation}, {0: 500})] * 2
expected_request = {}
expected_timeout = 9
await _mutate_rows_retryable_attempt(client, expected_request, expected_timeout, mutations, errors, lambda x: x.grpc_status_code <= 400)
assert mutations == {0: None, 1: None, 2: None}
assert errors[0] == []
assert errors[2] == []
# assert len(errors[1]) == 4
assert errors[1][0].grpc_status_code == 300
assert errors[1][1].grpc_status_code == 400
assert errors[1][2].grpc_status_code == 400
assert errors[1][3].grpc_status_code == 500


@pytest.mark.asyncio
async def test_retryable_entry_error(self):
"""Should continue retrying if the error is retryable"""
from google.cloud.bigtable._mutate_rows import _mutate_rows_retryable_attempt
import asyncio
mutation = mock.Mock()
mutations = {0: mutation}
errors = {0: []}
client = self._make_mock_client(mutations, error_dict={0: 400})
# raise retryable error 3 times, then raise non-retryable error
expected_request = {}
expected_timeout = 9
with mock.patch.object(asyncio, 'sleep') as mock_sleep:
fail_after = 100
mock_sleep.side_effect = [None] * fail_after + [asyncio.CancelledError("Cancelled")]
try:
await _mutate_rows_retryable_attempt(client, expected_request, expected_timeout, mutations, errors, lambda x: True)
except asyncio.CancelledError:
pass
assert mock_sleep.call_count == fail_after + 1
assert client.mutate_rows.call_count == fail_after
assert len(errors[0]) == fail_after