diff --git a/packages/google-cloud-spanner/google/cloud/spanner_dbapi/cursor.py b/packages/google-cloud-spanner/google/cloud/spanner_dbapi/cursor.py index dc12d1b84609..bf1a95fdf864 100644 --- a/packages/google-cloud-spanner/google/cloud/spanner_dbapi/cursor.py +++ b/packages/google-cloud-spanner/google/cloud/spanner_dbapi/cursor.py @@ -572,13 +572,18 @@ def _fetch(self, cursor_statement_type, size=None): return break except Aborted: - if not self.connection.read_only: - if self._in_retry_mode: - raise - else: - self.transaction_helper.retry_transaction() + if self.connection.read_only or self._in_retry_mode: + raise + self.transaction_helper.retry_transaction() + replayed_cursor = self.transaction_helper._cursor_map.get(self) + if replayed_cursor is None: + raise + # Replay has already consumed the rows returned by earlier fetches. + self._result_set = replayed_cursor._result_set + self._itr = replayed_cursor._itr except Exception as e: exception = e + raise finally: if not self._in_retry_mode: diff --git a/packages/google-cloud-spanner/tests/unit/spanner_dbapi/test_cursor.py b/packages/google-cloud-spanner/tests/unit/spanner_dbapi/test_cursor.py index 8d7b33fe4a98..bbc0542847ba 100644 --- a/packages/google-cloud-spanner/tests/unit/spanner_dbapi/test_cursor.py +++ b/packages/google-cloud-spanner/tests/unit/spanner_dbapi/test_cursor.py @@ -18,7 +18,7 @@ import unittest from unittest import mock -from google.api_core.exceptions import Aborted +from google.api_core.exceptions import Aborted, DeadlineExceeded from google.auth.credentials import AnonymousCredentials from google.rpc.code_pb2 import ABORTED @@ -1210,97 +1210,50 @@ def test_peek_iterator_aborted(self, mock_client): retry_mock.assert_called_with() @mock.patch("google.cloud.spanner_v1.Client") - def test_fetchone_aborted_with_cursor_not_in_retry_mode(self, mock_client): - connection = connect("test-instance", "test-database") - cursor = connection.cursor() - transaction_helper_mock = cursor.transaction_helper = mock.Mock() - - with mock.patch( - "google.cloud.spanner_dbapi.cursor.Cursor.__next__", - side_effect=(Aborted("Aborted"), iter([])), - ): - cursor.fetchone() - - transaction_helper_mock.add_fetch_statement_for_retry.assert_called_once() - transaction_helper_mock.retry_transaction.assert_called_once() - - @mock.patch("google.cloud.spanner_v1.Client") - def test_fetchone_aborted_with_cursor_in_retry_mode(self, mock_client): - connection = connect("test-instance", "test-database") - cursor = connection.cursor() - cursor._in_retry_mode = True - transaction_helper_mock = cursor.transaction_helper = mock.Mock() - - with mock.patch( - "google.cloud.spanner_dbapi.cursor.Cursor.__next__", - side_effect=(Aborted("Aborted"), iter([])), - ): - cursor.fetchone() - - transaction_helper_mock.add_fetch_statement_for_retry.assert_not_called() - transaction_helper_mock.retry_transaction.assert_not_called() - - @mock.patch("google.cloud.spanner_v1.Client") - def test_fetchall_aborted_with_cursor_not_in_retry_mode(self, mock_client): - connection = connect("test-instance", "test-database") - cursor = connection.cursor() - transaction_helper_mock = cursor.transaction_helper = mock.Mock() - - with mock.patch( - "google.cloud.spanner_dbapi.cursor.Cursor.__iter__", - side_effect=(Aborted("Aborted"), iter([])), - ): - cursor.fetchall() - - transaction_helper_mock.add_fetch_statement_for_retry.assert_called_once() - transaction_helper_mock.retry_transaction.assert_called_once() - - @mock.patch("google.cloud.spanner_v1.Client") - def test_fetchall_aborted_with_cursor_in_retry_mode(self, mock_client): + def test_fetchall_reads_remaining_rows_from_replayed_cursor(self, mock_client): connection = connect("test-instance", "test-database") + connection._transaction_begin_marked = True cursor = connection.cursor() - cursor._in_retry_mode = True - transaction_helper_mock = cursor.transaction_helper = mock.Mock() + replayed_cursor = connection.cursor() - with mock.patch( - "google.cloud.spanner_dbapi.cursor.Cursor.__iter__", - side_effect=(Aborted("Aborted"), iter([])), - ): - cursor.fetchall() + def rows_then_abort(): + yield ("already returned",) + yield ("incomplete batch",) + raise Aborted("retry transaction") - transaction_helper_mock.add_fetch_statement_for_retry.assert_not_called() - transaction_helper_mock.retry_transaction.assert_not_called() + cursor._itr = rows_then_abort() + cursor.transaction_helper.add_execute_statement_for_retry( + cursor, "SELECT * FROM table_name", None, None, False + ) + self.assertEqual(cursor.fetchone(), ("already returned",)) - @mock.patch("google.cloud.spanner_v1.Client") - def test_fetchmany_aborted_with_cursor_not_in_retry_mode(self, mock_client): - connection = connect("test-instance", "test-database") - cursor = connection.cursor() - transaction_helper_mock = cursor.transaction_helper = mock.Mock() + def replay_query(*args): + replayed_cursor._itr = iter( + [("already returned",), ("incomplete batch",), ("remaining",)] + ) - with mock.patch( - "google.cloud.spanner_dbapi.cursor.Cursor.__next__", - side_effect=(Aborted("Aborted"), iter([])), + with ( + mock.patch.object(connection, "cursor", return_value=replayed_cursor), + mock.patch.object(replayed_cursor, "execute", side_effect=replay_query), ): - cursor.fetchmany() - - transaction_helper_mock.add_fetch_statement_for_retry.assert_called_once() - transaction_helper_mock.retry_transaction.assert_called_once() + self.assertEqual(cursor.fetchall(), [("incomplete batch",), ("remaining",)]) @mock.patch("google.cloud.spanner_v1.Client") - def test_fetchmany_aborted_with_cursor_in_retry_mode(self, mock_client): - connection = connect("test-instance", "test-database") - cursor = connection.cursor() - cursor._in_retry_mode = True - transaction_helper_mock = cursor.transaction_helper = mock.Mock() - - with mock.patch( - "google.cloud.spanner_dbapi.cursor.Cursor.__next__", - side_effect=(Aborted("Aborted"), iter([])), - ): - cursor.fetchmany() - - transaction_helper_mock.add_fetch_statement_for_retry.assert_not_called() - transaction_helper_mock.retry_transaction.assert_not_called() + def test_fetchall_raises_aborted_if_stream_cannot_be_replayed(self, mock_client): + def rows_then_abort(): + yield ("row-1",) + raise Aborted("stream aborted") + + for read_only, in_retry_mode in ((True, False), (False, True), (False, False)): + with self.subTest(read_only=read_only, in_retry_mode=in_retry_mode): + connection = connect("test-instance", "test-database") + connection.read_only = read_only + cursor = connection.cursor() + cursor._in_retry_mode = in_retry_mode + cursor._itr = rows_then_abort() + + with self.assertRaisesRegex(Aborted, "stream aborted"): + cursor.fetchall() @mock.patch("google.cloud.spanner_v1.Client") def test_fetch_exception_with_cursor_not_in_retry_mode(self, mock_client): @@ -1308,14 +1261,35 @@ def test_fetch_exception_with_cursor_not_in_retry_mode(self, mock_client): cursor = connection.cursor() transaction_helper_mock = cursor.transaction_helper = mock.Mock() - with mock.patch( - "google.cloud.spanner_dbapi.cursor.Cursor.__iter__", - side_effect=Exception("Exception"), - ): - cursor.fetchall() + row = ("row-1",) + error = DeadlineExceeded("result stream timed out") - transaction_helper_mock.add_fetch_statement_for_retry.assert_called_once() - transaction_helper_mock.retry_transaction.assert_not_called() + def rows_then_timeout(): + yield row + raise error + + for read_only in (True, False): + connection.read_only = read_only + for method, args in ( + ("fetchall", ()), + ("fetchmany", (2,)), + ("fetchone", ()), + ): + with self.subTest(read_only=read_only, method=method): + cursor._itr = rows_then_timeout() + if method == "fetchone": + self.assertEqual(cursor.fetchone(), row) + transaction_helper_mock.reset_mock() + + with self.assertRaises(DeadlineExceeded) as raised: + getattr(cursor, method)(*args) + + self.assertIs(raised.exception, error) + recorded_rows = [] if method == "fetchone" else [row] + transaction_helper_mock.add_fetch_statement_for_retry.assert_called_once_with( + cursor, recorded_rows, error, method == "fetchall" + ) + transaction_helper_mock.retry_transaction.assert_not_called() @mock.patch("google.cloud.spanner_v1.Client") def test_fetch_exception_with_cursor_in_retry_mode(self, mock_client): @@ -1328,7 +1302,8 @@ def test_fetch_exception_with_cursor_in_retry_mode(self, mock_client): "google.cloud.spanner_dbapi.cursor.Cursor.__next__", side_effect=Exception("Exception"), ): - cursor.fetchmany() + with self.assertRaisesRegex(Exception, "Exception"): + cursor.fetchmany() transaction_helper_mock.add_fetch_statement_for_retry.assert_not_called() transaction_helper_mock.retry_transaction.assert_not_called()