Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
try to improve code coverage
  • Loading branch information
tswast committed Feb 19, 2025
commit cbb2f039d5956c36adce2f3b89747d43ca0cf8a6
19 changes: 10 additions & 9 deletions google/cloud/bigquery/_job_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,26 +169,27 @@ def do_query():
else:
return query_job

if job_retry is None:
future = do_query()
else:
# Per https://github.com/googleapis/python-bigquery/issues/2134, sometimes
# we get a 404 error. In this case, if we get this far, assume that the job
# doesn't actually exist and try again. We can't add 404 to the default
# job_retry because that happens for errors like "this table does not
# exist", which probably won't resolve with a retry.
if job_retry is not None:

def do_query_predicate(exc) -> bool:
if isinstance(exc, core_exceptions.RetryError):
exc = exc.cause

# Per https://github.com/googleapis/python-bigquery/issues/2134, sometimes
# we get a 404 error. In this case, if we get this far, assume that the job
# doesn't actually exist and try again. We can't add 404 to the default
# job_retry because that happens for errors like "this table does not
# exist", which probably won't resolve with a retry.
if isinstance(exc, core_exceptions.NotFound):
return True

# Reference the original job_retry to avoid recursion.
return job_retry._predicate(exc)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parthea likewise here. I ended up using a slightly different pattern here due to the double nesting of retry objects.


do_query_retry = job_retry.with_predicate(do_query_predicate)
future = do_query_retry(do_query)()
do_query = do_query_retry(do_query)

future = do_query()

# The future might be in a failed state now, but if it's
# unrecoverable, we'll find out when we ask for it's result, at which
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5311,6 +5311,36 @@ def test_query_job_rpc_fail_w_conflict_random_id_job_fetch_fails(self):
with pytest.raises(DataLoss, match="we lost your job, sorry"):
client.query("SELECT 1;", job_id=None)

def test_query_job_rpc_fail_w_conflict_random_id_job_fetch_fails_no_retries(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you help me understand what this test is for? It feels like we are just verifying that QueryJob._begin() and client.get_job() are called.

from google.api_core.exceptions import Conflict
from google.api_core.exceptions import DataLoss
from google.cloud.bigquery.job import QueryJob

creds = _make_credentials()
http = object()
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http)

job_create_error = Conflict("Job already exists.")
job_begin_patcher = mock.patch.object(
QueryJob, "_begin", side_effect=job_create_error
)
get_job_patcher = mock.patch.object(
client, "get_job", side_effect=DataLoss("we lost your job, sorry")
)

with job_begin_patcher, get_job_patcher:
# If get job request fails but supposedly there does exist a job
# with this ID already, raise the exception explaining why we
# couldn't recover the job.
with pytest.raises(DataLoss, match="we lost your job, sorry"):
client.query(
"SELECT 1;",
job_id=None,
# Explicitly test with no retries to make sure those branches are covered.
retry=None,
job_retry=None,
)

def test_query_job_rpc_fail_w_conflict_random_id_job_fetch_retries_404(self):
"""Regression test for https://github.com/googleapis/python-bigquery/issues/2134

Expand Down