GeezerVison is showing github.com — fix: updates timeout/retry code to respect hanging server (#2408) · googleapis/python-bigquery@24d45d0
This repository was archived by the owner on Mar 6, 2026. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/_pandas_helpers.py +40-9Lines changed: 40 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 33 | 33 |
| 34 | 34 | from google.cloud.bigquery import _pyarrow_helpers | 35 | 35 | from google.cloud.bigquery import _versions_helpers | | 36 | +from google.cloud.bigquery import retry as bq_retry | 36 | 37 | from google.cloud.bigquery import schema | 37 | 38 |
| 38 | 39 |
| @@ -740,7 +741,7 @@ def _row_iterator_page_to_arrow(page, column_names, arrow_types): | 740 | 741 | return pyarrow.RecordBatch.from_arrays(arrays, names=column_names) | 741 | 742 |
| 742 | 743 |
| 743 | | -def download_arrow_row_iterator(pages, bq_schema): | | 744 | +def download_arrow_row_iterator(pages, bq_schema, timeout=None): | 744 | 745 | """Use HTTP JSON RowIterator to construct an iterable of RecordBatches. | 745 | 746 |
| 746 | 747 | Args: | @@ -751,6 +752,10 @@ def download_arrow_row_iterator(pages, bq_schema): | 751 | 752 | Mapping[str, Any] \ | 752 | 753 | ]]): | 753 | 754 | A decription of the fields in result pages. | | 755 | + timeout (Optional[float]): | | 756 | + The number of seconds to wait for the underlying download to complete. | | 757 | + If ``None``, wait indefinitely. | | 758 | +
| 754 | 759 | Yields: | 755 | 760 | :class:`pyarrow.RecordBatch` | 756 | 761 | The next page of records as a ``pyarrow`` record batch. | @@ -759,8 +764,16 @@ def download_arrow_row_iterator(pages, bq_schema): | 759 | 764 | column_names = bq_to_arrow_schema(bq_schema) or [field.name for field in bq_schema] | 760 | 765 | arrow_types = [bq_to_arrow_data_type(field) for field in bq_schema] | 761 | 766 |
| 762 | | - for page in pages: | 763 | | - yield _row_iterator_page_to_arrow(page, column_names, arrow_types) | | 767 | + if timeout is None: | | 768 | + for page in pages: | | 769 | + yield _row_iterator_page_to_arrow(page, column_names, arrow_types) | | 770 | + else: | | 771 | + start_time = time.monotonic() | | 772 | + for page in pages: | | 773 | + if time.monotonic() - start_time > timeout: | | 774 | + raise concurrent.futures.TimeoutError() | | 775 | + | | 776 | + yield _row_iterator_page_to_arrow(page, column_names, arrow_types) | 764 | 777 |
| 765 | 778 |
| 766 | 779 | def _row_iterator_page_to_dataframe(page, column_names, dtypes): | @@ -778,7 +791,7 @@ def _row_iterator_page_to_dataframe(page, column_names, dtypes): | 778 | 791 | return pandas.DataFrame(columns, columns=column_names) | 779 | 792 |
| 780 | 793 |
| 781 | | -def download_dataframe_row_iterator(pages, bq_schema, dtypes): | | 794 | +def download_dataframe_row_iterator(pages, bq_schema, dtypes, timeout=None): | 782 | 795 | """Use HTTP JSON RowIterator to construct a DataFrame. | 783 | 796 |
| 784 | 797 | Args: | @@ -792,14 +805,27 @@ def download_dataframe_row_iterator(pages, bq_schema, dtypes): | 792 | 805 | dtypes(Mapping[str, numpy.dtype]): | 793 | 806 | The types of columns in result data to hint construction of the | 794 | 807 | resulting DataFrame. Not all column types have to be specified. | | 808 | + timeout (Optional[float]): | | 809 | + The number of seconds to wait for the underlying download to complete. | | 810 | + If ``None``, wait indefinitely. | | 811 | +
| 795 | 812 | Yields: | 796 | 813 | :class:`pandas.DataFrame` | 797 | 814 | The next page of records as a ``pandas.DataFrame`` record batch. | 798 | 815 | """ | 799 | 816 | bq_schema = schema._to_schema_fields(bq_schema) | 800 | 817 | column_names = [field.name for field in bq_schema] | 801 | | - for page in pages: | 802 | | - yield _row_iterator_page_to_dataframe(page, column_names, dtypes) | | 818 | + | | 819 | + if timeout is None: | | 820 | + for page in pages: | | 821 | + yield _row_iterator_page_to_dataframe(page, column_names, dtypes) | | 822 | + else: | | 823 | + start_time = time.monotonic() | | 824 | + for page in pages: | | 825 | + if time.monotonic() - start_time > timeout: | | 826 | + raise concurrent.futures.TimeoutError() | | 827 | + | | 828 | + yield _row_iterator_page_to_dataframe(page, column_names, dtypes) | 803 | 829 |
| 804 | 830 |
| 805 | 831 | def _bqstorage_page_to_arrow(page): | @@ -928,6 +954,7 @@ def _download_table_bqstorage( | 928 | 954 | if "@" in table.table_id: | 929 | 955 | raise ValueError("Reading from a specific snapshot is not currently supported.") | 930 | 956 |
| | 957 | + start_time = time.monotonic() | 931 | 958 | requested_streams = determine_requested_streams(preserve_order, max_stream_count) | 932 | 959 |
| 933 | 960 | requested_session = bigquery_storage.types.stream.ReadSession( | @@ -944,10 +971,16 @@ def _download_table_bqstorage( | 944 | 971 | ArrowSerializationOptions.CompressionCodec(1) | 945 | 972 | ) | 946 | 973 |
| | 974 | + retry_policy = ( | | 975 | + bq_retry.DEFAULT_RETRY.with_deadline(timeout) if timeout is not None else None | | 976 | + ) | | 977 | + | 947 | 978 | session = bqstorage_client.create_read_session( | 948 | 979 | parent="projects/{}".format(project_id), | 949 | 980 | read_session=requested_session, | 950 | 981 | max_stream_count=requested_streams, | | 982 | + retry=retry_policy, | | 983 | + timeout=timeout, | 951 | 984 | ) | 952 | 985 |
| 953 | 986 | _LOGGER.debug( | @@ -983,8 +1016,6 @@ def _download_table_bqstorage( | 983 | 1016 | # Manually manage the pool to control shutdown behavior on timeout. | 984 | 1017 | pool = concurrent.futures.ThreadPoolExecutor(max_workers=max(1, total_streams)) | 985 | 1018 | wait_on_shutdown = True | 986 | | - start_time = time.time() | 987 | | - | 988 | 1019 | try: | 989 | 1020 | # Manually submit jobs and wait for download to complete rather | 990 | 1021 | # than using pool.map because pool.map continues running in the | @@ -1006,7 +1037,7 @@ def _download_table_bqstorage( | 1006 | 1037 | while not_done: | 1007 | 1038 | # Check for timeout | 1008 | 1039 | if timeout is not None: | 1009 | | - elapsed = time.time() - start_time | | 1040 | + elapsed = time.monotonic() - start_time | 1010 | 1041 | if elapsed > timeout: | 1011 | 1042 | wait_on_shutdown = False | 1012 | 1043 | raise concurrent.futures.TimeoutError( | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/dbapi/cursor.py +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -323,6 +323,8 @@ def _bqstorage_fetch(self, bqstorage_client): | 323 | 323 | read_session=requested_session, | 324 | 324 | # a single stream only, as DB API is not well-suited for multithreading | 325 | 325 | max_stream_count=1, | | 326 | + retry=None, | | 327 | + timeout=None, | 326 | 328 | ) | 327 | 329 |
| 328 | 330 | if not read_session.streams: | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/retry.py +11-5Lines changed: 11 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 | # See the License for the specific language governing permissions and | 13 | 13 | # limitations under the License. | 14 | 14 |
| | 15 | +import logging | | 16 | + | 15 | 17 | from google.api_core import exceptions | 16 | 18 | from google.api_core import retry | 17 | 19 | import google.api_core.future.polling | 18 | 20 | from google.auth import exceptions as auth_exceptions # type: ignore | 19 | 21 | import requests.exceptions | 20 | 22 |
| | 23 | +_LOGGER = logging.getLogger(__name__) | 21 | 24 |
| 22 | 25 | _RETRYABLE_REASONS = frozenset( | 23 | 26 | ["rateLimitExceeded", "backendError", "internalError", "badGateway"] | | 61 | 64 | def _should_retry(exc): | 62 | 65 | """Predicate for determining when to retry. | 63 | 66 |
| 64 | | - We retry if and only if the 'reason' is 'backendError' | 65 | | - or 'rateLimitExceeded'. | | 67 | + We retry if and only if the 'reason' is in _RETRYABLE_REASONS or is | | 68 | + in _UNSTRUCTURED_RETRYABLE_TYPES. | 66 | 69 | """ | 67 | | - if not hasattr(exc, "errors") or len(exc.errors) == 0: | 68 | | - # Check for unstructured error returns, e.g. from GFE | | 70 | + try: | | 71 | + reason = exc.errors[0]["reason"] | | 72 | + except (AttributeError, IndexError, TypeError, KeyError): | | 73 | + # Fallback for when errors attribute is missing, empty, or not a dict | | 74 | + # or doesn't contain "reason" (e.g. gRPC exceptions). | | 75 | + _LOGGER.debug("Inspecting unstructured error for retry: %r", exc) | 69 | 76 | return isinstance(exc, _UNSTRUCTURED_RETRYABLE_TYPES) | 70 | 77 |
| 71 | | - reason = exc.errors[0]["reason"] | 72 | 78 | return reason in _RETRYABLE_REASONS | 73 | 79 |
| 74 | 80 |
| |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/table.py +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2152,7 +2152,10 @@ def to_arrow_iterable( | 2152 | 2152 | timeout=timeout, | 2153 | 2153 | ) | 2154 | 2154 | tabledata_list_download = functools.partial( | 2155 | | - _pandas_helpers.download_arrow_row_iterator, iter(self.pages), self.schema | | 2155 | + _pandas_helpers.download_arrow_row_iterator, | | 2156 | + iter(self.pages), | | 2157 | + self.schema, | | 2158 | + timeout=timeout, | 2156 | 2159 | ) | 2157 | 2160 | return self._to_page_iterable( | 2158 | 2161 | bqstorage_download, | @@ -2366,6 +2369,7 @@ def to_dataframe_iterable( | 2366 | 2369 | iter(self.pages), | 2367 | 2370 | self.schema, | 2368 | 2371 | dtypes, | | 2372 | + timeout=timeout, | 2369 | 2373 | ) | 2370 | 2374 | return self._to_page_iterable( | 2371 | 2375 | bqstorage_download, | |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/job/test_query_pandas.py +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -179,6 +179,8 @@ def test_to_dataframe_bqstorage_preserve_order(query, table_read_options_kwarg): | 179 | 179 | parent="projects/test-project", | 180 | 180 | read_session=expected_session, | 181 | 181 | max_stream_count=1, # Use a single stream to preserve row order. | | 182 | + retry=None, | | 183 | + timeout=None, | 182 | 184 | ) | 183 | 185 |
| 184 | 186 |
| @@ -593,6 +595,8 @@ def test_to_dataframe_bqstorage(table_read_options_kwarg): | 593 | 595 | parent="projects/bqstorage-billing-project", | 594 | 596 | read_session=expected_session, | 595 | 597 | max_stream_count=0, # Use default number of streams for best performance. | | 598 | + retry=None, | | 599 | + timeout=None, | 596 | 600 | ) | 597 | 601 | bqstorage_client.read_rows.assert_called_once_with(stream_id) | 598 | 602 |
| @@ -644,6 +648,8 @@ def test_to_dataframe_bqstorage_no_pyarrow_compression(): | 644 | 648 | parent="projects/bqstorage-billing-project", | 645 | 649 | read_session=expected_session, | 646 | 650 | max_stream_count=0, | | 651 | + retry=None, | | 652 | + timeout=None, | 647 | 653 | ) | 648 | 654 |
| 649 | 655 |
| |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/test__pandas_helpers.py +131Lines changed: 131 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2252,3 +2252,134 @@ def fast_download_stream( | 2252 | 2252 | results = list(result_gen) | 2253 | 2253 |
| 2254 | 2254 | assert results == ["result_page"] | | 2255 | + | | 2256 | + | | 2257 | +@pytest.mark.skipif(pandas is None, reason="Requires `pandas`") | | 2258 | +@pytest.mark.skipif(isinstance(pyarrow, mock.Mock), reason="Requires `pyarrow`") | | 2259 | +@pytest.mark.parametrize( | | 2260 | + "sleep_time, timeout, should_timeout", | | 2261 | + [ | | 2262 | + (0.1, 0.05, True), # Timeout case | | 2263 | + (0, 10.0, False), # Success case | | 2264 | + ], | | 2265 | +) | | 2266 | +def test_download_arrow_row_iterator_with_timeout( | | 2267 | + module_under_test, sleep_time, timeout, should_timeout | | 2268 | +): | | 2269 | + bq_schema = [schema.SchemaField("name", "STRING")] | | 2270 | + | | 2271 | + # Mock page with to_arrow method | | 2272 | + mock_page = mock.Mock() | | 2273 | + mock_page.to_arrow.return_value = pyarrow.RecordBatch.from_arrays( | | 2274 | + [pyarrow.array(["foo"])], | | 2275 | + names=["name"], | | 2276 | + ) | | 2277 | + mock_page.__iter__ = lambda self: iter(["row1"]) | | 2278 | + mock_page._columns = [["foo"]] | | 2279 | + | | 2280 | + def pages_gen(): | | 2281 | + # First page yields quickly | | 2282 | + yield mock_page | | 2283 | + if sleep_time > 0: | | 2284 | + time.sleep(sleep_time) | | 2285 | + yield mock_page | | 2286 | + | | 2287 | + iterator = module_under_test.download_arrow_row_iterator( | | 2288 | + pages_gen(), bq_schema, timeout=timeout | | 2289 | + ) | | 2290 | + | | 2291 | + # First item should always succeed | | 2292 | + next(iterator) | | 2293 | + | | 2294 | + if should_timeout: | | 2295 | + with pytest.raises(concurrent.futures.TimeoutError): | | 2296 | + next(iterator) | | 2297 | + else: | | 2298 | + # Should succeed and complete | | 2299 | + results = list(iterator) | | 2300 | + assert len(results) == 1 # 1 remaining item | | 2301 | + | | 2302 | + | | 2303 | +@pytest.mark.skipif(pandas is None, reason="Requires `pandas`") | | 2304 | +@pytest.mark.skipif(isinstance(pyarrow, mock.Mock), reason="Requires `pyarrow`") | | 2305 | +@pytest.mark.parametrize( | | 2306 | + "sleep_time, timeout, should_timeout", | | 2307 | + [ | | 2308 | + (0.1, 0.05, True), # Timeout case | | 2309 | + (0, 10.0, False), # Success case | | 2310 | + ], | | 2311 | +) | | 2312 | +def test_download_dataframe_row_iterator_with_timeout( | | 2313 | + module_under_test, sleep_time, timeout, should_timeout | | 2314 | +): | | 2315 | + bq_schema = [schema.SchemaField("name", "STRING")] | | 2316 | + dtypes = {} | | 2317 | + | | 2318 | + # Mock page | | 2319 | + mock_page = mock.Mock() | | 2320 | + # Mock iterator for _row_iterator_page_to_dataframe checking next(iter(page)) | | 2321 | + mock_page.__iter__ = lambda self: iter(["row1"]) | | 2322 | + mock_page._columns = [["foo"]] | | 2323 | + | | 2324 | + def pages_gen(): | | 2325 | + yield mock_page | | 2326 | + if sleep_time > 0: | | 2327 | + time.sleep(sleep_time) | | 2328 | + yield mock_page | | 2329 | + | | 2330 | + iterator = module_under_test.download_dataframe_row_iterator( | | 2331 | + pages_gen(), bq_schema, dtypes, timeout=timeout | | 2332 | + ) | | 2333 | + | | 2334 | + next(iterator) | | 2335 | + | | 2336 | + if should_timeout: | | 2337 | + with pytest.raises(concurrent.futures.TimeoutError): | | 2338 | + next(iterator) | | 2339 | + else: | | 2340 | + results = list(iterator) | | 2341 | + assert len(results) == 1 | | 2342 | + | | 2343 | + | | 2344 | +@pytest.mark.skipif( | | 2345 | + bigquery_storage is None, reason="Requires `google-cloud-bigquery-storage`" | | 2346 | +) | | 2347 | +def test_download_arrow_bqstorage_passes_timeout_to_create_read_session( | | 2348 | + module_under_test, | | 2349 | +): | | 2350 | + # Mock dependencies | | 2351 | + project_id = "test-project" | | 2352 | + table = mock.Mock() | | 2353 | + table.table_id = "test_table" | | 2354 | + table.to_bqstorage.return_value = "projects/test/datasets/test/tables/test" | | 2355 | + | | 2356 | + bqstorage_client = mock.create_autospec( | | 2357 | + bigquery_storage.BigQueryReadClient, instance=True | | 2358 | + ) | | 2359 | + # Mock create_read_session to return a session with no streams so the function returns early | | 2360 | + # (Checking start of loop logic vs empty streams return) | | 2361 | + session = mock.Mock() | | 2362 | + # If streams is empty, _download_table_bqstorage returns early, which is fine for this test | | 2363 | + session.streams = [] | | 2364 | + bqstorage_client.create_read_session.return_value = session | | 2365 | + | | 2366 | + # Call the function | | 2367 | + timeout = 123.456 | | 2368 | + # download_arrow_bqstorage yields frames, so we need to iterate to trigger execution | | 2369 | + list( | | 2370 | + module_under_test.download_arrow_bqstorage( | | 2371 | + project_id, table, bqstorage_client, timeout=timeout | | 2372 | + ) | | 2373 | + ) | | 2374 | + | | 2375 | + # Verify timeout and retry were passed | | 2376 | + bqstorage_client.create_read_session.assert_called_once() | | 2377 | + _, kwargs = bqstorage_client.create_read_session.call_args | | 2378 | + assert "timeout" in kwargs | | 2379 | + assert kwargs["timeout"] == timeout | | 2380 | + | | 2381 | + assert "retry" in kwargs | | 2382 | + retry_policy = kwargs["retry"] | | 2383 | + assert retry_policy is not None | | 2384 | + # Check if deadline is set correctly in the retry policy | | 2385 | + assert retry_policy._deadline == timeout |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/test_client_retry.py +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 23 | 23 |
| 24 | 24 | PROJECT = "test-project" | 25 | 25 |
| | 26 | +# A deadline > 1.0s is required because the default retry (google.api_core.retry.Retry) | | 27 | +# has an initial delay of 1.0s. If the deadline is <= 1.0s, the first retry attempt | | 28 | +# (scheduled for now + 1.0s) will be rejected immediately as exceeding the deadline. | | 29 | +_RETRY_DEADLINE = 10.0 | | 30 | + | 26 | 31 |
| 27 | 32 | def _make_credentials(): | 28 | 33 | import google.auth.credentials | @@ -83,7 +88,7 @@ def test_call_api_applying_custom_retry_on_timeout(global_time_lock): | 83 | 88 | "api_request", | 84 | 89 | side_effect=[TimeoutError, "result"], | 85 | 90 | ) | 86 | | - retry = DEFAULT_RETRY.with_deadline(1).with_predicate( | | 91 | + retry = DEFAULT_RETRY.with_deadline(_RETRY_DEADLINE).with_predicate( | 87 | 92 | lambda exc: isinstance(exc, TimeoutError) | 88 | 93 | ) | 89 | 94 |
| |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/test_dbapi_cursor.py +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -480,7 +480,11 @@ def fake_ensure_bqstorage_client(bqstorage_client=None, **kwargs): | 480 | 480 | data_format=bigquery_storage.DataFormat.ARROW, | 481 | 481 | ) | 482 | 482 | mock_bqstorage_client.create_read_session.assert_called_once_with( | 483 | | - parent="projects/P", read_session=expected_session, max_stream_count=1 | | 483 | + parent="projects/P", | | 484 | + read_session=expected_session, | | 485 | + max_stream_count=1, | | 486 | + retry=None, | | 487 | + timeout=None, | 484 | 488 | ) | 485 | 489 |
| 486 | 490 | # Check the data returned. | |
|