GeezerVison is showing github.com — feat(bigquery): add timeout parameter to QueryJob.done() method (#9875) · okothchristopher/python-bigquery@c7ce674
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/client.py +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1081,7 +1081,7 @@ def delete_table(self, table, retry=DEFAULT_RETRY, not_found_ok=False): | 1081 | 1081 | raise | 1082 | 1082 |
| 1083 | 1083 | def _get_query_results( | 1084 | | - self, job_id, retry, project=None, timeout_ms=None, location=None | | 1084 | + self, job_id, retry, project=None, timeout_ms=None, location=None, timeout=None, | 1085 | 1085 | ): | 1086 | 1086 | """Get the query results object for a query job. | 1087 | 1087 |
| @@ -1096,6 +1096,9 @@ def _get_query_results( | 1096 | 1096 | (Optional) number of milliseconds the the API call should | 1097 | 1097 | wait for the query to complete before the request times out. | 1098 | 1098 | location (str): Location of the query job. | | 1099 | + timeout (Optional[float]): | | 1100 | + The number of seconds to wait for the underlying HTTP transport | | 1101 | + before retrying the HTTP request. | 1099 | 1102 |
| 1100 | 1103 | Returns: | 1101 | 1104 | google.cloud.bigquery.query._QueryResults: | @@ -1122,7 +1125,7 @@ def _get_query_results( | 1122 | 1125 | # job is complete (from QueryJob.done(), called ultimately from | 1123 | 1126 | # QueryJob.result()). So we don't need to poll here. | 1124 | 1127 | resource = self._call_api( | 1125 | | - retry, method="GET", path=path, query_params=extra_params | | 1128 | + retry, method="GET", path=path, query_params=extra_params, timeout=timeout | 1126 | 1129 | ) | 1127 | 1130 | return _QueryResults.from_api_repr(resource) | 1128 | 1131 |
| |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/job.py +44-8Lines changed: 44 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 14 | 14 |
| 15 | 15 | """Define API Jobs.""" | 16 | 16 |
| | 17 | +from __future__ import division | | 18 | + | | 19 | +import concurrent.futures | 17 | 20 | import copy | 18 | 21 | import re | 19 | 22 | import threading | 20 | 23 |
| | 24 | +import requests | 21 | 25 | import six | 22 | 26 | from six.moves import http_client | 23 | 27 |
| | 50 | 54 | _DONE_STATE = "DONE" | 51 | 55 | _STOPPED_REASON = "stopped" | 52 | 56 | _TIMEOUT_BUFFER_SECS = 0.1 | | 57 | +_SERVER_TIMEOUT_MARGIN_SECS = 1.0 | 53 | 58 | _CONTAINS_ORDER_BY = re.compile(r"ORDER\s+BY", re.IGNORECASE) | 54 | 59 |
| 55 | 60 | _ERROR_REASON_TO_EXCEPTION = { | @@ -663,7 +668,7 @@ def exists(self, client=None, retry=DEFAULT_RETRY): | 663 | 668 | else: | 664 | 669 | return True | 665 | 670 |
| 666 | | - def reload(self, client=None, retry=DEFAULT_RETRY): | | 671 | + def reload(self, client=None, retry=DEFAULT_RETRY, timeout=None): | 667 | 672 | """API call: refresh job properties via a GET request. | 668 | 673 |
| 669 | 674 | See | @@ -675,6 +680,9 @@ def reload(self, client=None, retry=DEFAULT_RETRY): | 675 | 680 | ``client`` stored on the current dataset. | 676 | 681 |
| 677 | 682 | retry (google.api_core.retry.Retry): (Optional) How to retry the RPC. | | 683 | + timeout (Optional[float]): | | 684 | + The number of seconds to wait for the underlying HTTP transport | | 685 | + before retrying the HTTP request. | 678 | 686 | """ | 679 | 687 | client = self._require_client(client) | 680 | 688 |
| @@ -683,7 +691,11 @@ def reload(self, client=None, retry=DEFAULT_RETRY): | 683 | 691 | extra_params["location"] = self.location | 684 | 692 |
| 685 | 693 | api_response = client._call_api( | 686 | | - retry, method="GET", path=self.path, query_params=extra_params | | 694 | + retry, | | 695 | + method="GET", | | 696 | + path=self.path, | | 697 | + query_params=extra_params, | | 698 | + timeout=timeout, | 687 | 699 | ) | 688 | 700 | self._set_properties(api_response) | 689 | 701 |
| @@ -2994,9 +3006,16 @@ def estimated_bytes_processed(self): | 2994 | 3006 | result = int(result) | 2995 | 3007 | return result | 2996 | 3008 |
| 2997 | | - def done(self, retry=DEFAULT_RETRY): | | 3009 | + def done(self, retry=DEFAULT_RETRY, timeout=None): | 2998 | 3010 | """Refresh the job and checks if it is complete. | 2999 | 3011 |
| | 3012 | + Args: | | 3013 | + retry (Optional[google.api_core.retry.Retry]): | | 3014 | + How to retry the call that retrieves query results. | | 3015 | + timeout (Optional[float]): | | 3016 | + The number of seconds to wait for the underlying HTTP transport | | 3017 | + before retrying the HTTP request. | | 3018 | +
| 3000 | 3019 | Returns: | 3001 | 3020 | bool: True if the job is complete, False otherwise. | 3002 | 3021 | """ | @@ -3007,11 +3026,25 @@ def done(self, retry=DEFAULT_RETRY): | 3007 | 3026 | timeout_ms = None | 3008 | 3027 | if self._done_timeout is not None: | 3009 | 3028 | # Subtract a buffer for context switching, network latency, etc. | 3010 | | - timeout = self._done_timeout - _TIMEOUT_BUFFER_SECS | 3011 | | - timeout = max(min(timeout, 10), 0) | 3012 | | - self._done_timeout -= timeout | | 3029 | + api_timeout = self._done_timeout - _TIMEOUT_BUFFER_SECS | | 3030 | + api_timeout = max(min(api_timeout, 10), 0) | | 3031 | + self._done_timeout -= api_timeout | 3013 | 3032 | self._done_timeout = max(0, self._done_timeout) | 3014 | | - timeout_ms = int(timeout * 1000) | | 3033 | + timeout_ms = int(api_timeout * 1000) | | 3034 | + | | 3035 | + # If the server-side processing timeout (timeout_ms) is specified and | | 3036 | + # would be picked as the total request timeout, we want to add a small | | 3037 | + # margin to it - we don't want to timeout the connection just as the | | 3038 | + # server-side processing might have completed, but instead slightly | | 3039 | + # after the server-side deadline. | | 3040 | + # However, if `timeout` is specified, and is shorter than the adjusted | | 3041 | + # server timeout, the former prevails. | | 3042 | + if timeout_ms is not None and timeout_ms > 0: | | 3043 | + server_timeout_with_margin = timeout_ms / 1000 + _SERVER_TIMEOUT_MARGIN_SECS | | 3044 | + if timeout is not None: | | 3045 | + timeout = min(server_timeout_with_margin, timeout) | | 3046 | + else: | | 3047 | + timeout = server_timeout_with_margin | 3015 | 3048 |
| 3016 | 3049 | # Do not refresh is the state is already done, as the job will not | 3017 | 3050 | # change once complete. | @@ -3022,13 +3055,14 @@ def done(self, retry=DEFAULT_RETRY): | 3022 | 3055 | project=self.project, | 3023 | 3056 | timeout_ms=timeout_ms, | 3024 | 3057 | location=self.location, | | 3058 | + timeout=timeout, | 3025 | 3059 | ) | 3026 | 3060 |
| 3027 | 3061 | # Only reload the job once we know the query is complete. | 3028 | 3062 | # This will ensure that fields such as the destination table are | 3029 | 3063 | # correctly populated. | 3030 | 3064 | if self._query_results.complete: | 3031 | | - self.reload(retry=retry) | | 3065 | + self.reload(retry=retry, timeout=timeout) | 3032 | 3066 |
| 3033 | 3067 | return self.state == _DONE_STATE | 3034 | 3068 |
| @@ -3132,6 +3166,8 @@ def result( | 3132 | 3166 | exc.message += self._format_for_exception(self.query, self.job_id) | 3133 | 3167 | exc.query_job = self | 3134 | 3168 | raise | | 3169 | + except requests.exceptions.Timeout as exc: | | 3170 | + six.raise_from(concurrent.futures.TimeoutError, exc) | 3135 | 3171 |
| 3136 | 3172 | # If the query job is complete but there are no query results, this was | 3137 | 3173 | # special job, such as a DDL query. Return an empty result set to | |
Collapse file Copy file name to clipboardExpand all lines: setup.py +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 30 | 30 | release_status = "Development Status :: 5 - Production/Stable" | 31 | 31 | dependencies = [ | 32 | 32 | 'enum34; python_version < "3.4"', | | 33 | + "google-auth >= 1.9.0, < 2.0dev", | | 34 | + "google-api-core >= 1.15.0, < 2.0dev", | 33 | 35 | "google-cloud-core >= 1.0.3, < 2.0dev", | 34 | 36 | "google-resumable-media >= 0.3.1, != 0.4.0, < 0.6.0dev", | 35 | 37 | "protobuf >= 3.6.0", | |
Collapse file Copy file name to clipboardExpand all lines: tests/system.py +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 26 | 26 | import uuid | 27 | 27 | import re | 28 | 28 |
| | 29 | +import requests | 29 | 30 | import six | 30 | 31 | import psutil | 31 | 32 | import pytest | @@ -1893,6 +1894,29 @@ def test_query_iter(self): | 1893 | 1894 | row_tuples = [r.values() for r in query_job] | 1894 | 1895 | self.assertEqual(row_tuples, [(1,)]) | 1895 | 1896 |
| | 1897 | + def test_querying_data_w_timeout(self): | | 1898 | + job_config = bigquery.QueryJobConfig() | | 1899 | + job_config.use_query_cache = False | | 1900 | + | | 1901 | + query_job = Config.CLIENT.query( | | 1902 | + """ | | 1903 | + SELECT name, SUM(number) AS total_people | | 1904 | + FROM `bigquery-public-data.usa_names.usa_1910_current` | | 1905 | + GROUP BY name | | 1906 | + """, | | 1907 | + location="US", | | 1908 | + job_config=job_config, | | 1909 | + ) | | 1910 | + | | 1911 | + # Specify a very tight deadline to demonstrate that the timeout | | 1912 | + # actually has effect. | | 1913 | + with self.assertRaises(requests.exceptions.Timeout): | | 1914 | + query_job.done(timeout=0.1) | | 1915 | + | | 1916 | + # Now wait for the result using a more realistic deadline. | | 1917 | + query_job.result(timeout=30) | | 1918 | + self.assertTrue(query_job.done(timeout=30)) | | 1919 | + | 1896 | 1920 | @unittest.skipIf(pandas is None, "Requires `pandas`") | 1897 | 1921 | def test_query_results_to_dataframe(self): | 1898 | 1922 | QUERY = """ | |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/test_client.py +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -226,12 +226,14 @@ def test__get_query_results_miss_w_explicit_project_and_timeout(self): | 226 | 226 | project="other-project", | 227 | 227 | location=self.LOCATION, | 228 | 228 | timeout_ms=500, | | 229 | + timeout=42, | 229 | 230 | ) | 230 | 231 |
| 231 | 232 | conn.api_request.assert_called_once_with( | 232 | 233 | method="GET", | 233 | 234 | path="/projects/other-project/queries/nothere", | 234 | 235 | query_params={"maxResults": 0, "timeoutMs": 500, "location": self.LOCATION}, | | 236 | + timeout=42, | 235 | 237 | ) | 236 | 238 |
| 237 | 239 | def test__get_query_results_miss_w_client_location(self): | @@ -248,6 +250,7 @@ def test__get_query_results_miss_w_client_location(self): | 248 | 250 | method="GET", | 249 | 251 | path="/projects/PROJECT/queries/nothere", | 250 | 252 | query_params={"maxResults": 0, "location": self.LOCATION}, | | 253 | + timeout=None, | 251 | 254 | ) | 252 | 255 |
| 253 | 256 | def test__get_query_results_hit(self): | |
|