GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: avoid unnecessary API call in QueryJob.result() when job is already finished by tswast · Pull Request #1900 · googleapis/python-bigquery

This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Conversation

@tswast

@tswast tswast commented Apr 12, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

BEGIN_COMMIT_OVERRIDE
perf: avoid unnecessary API call in QueryJob.result() when job is already finished (#1900)

fix: retry query jobs that fail even with ambiguous jobs.getQueryResults REST errors (#1903, #1900)
END_COMMIT_OVERRIDE

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)
  • Maybe fixes internal issue 332850329
  • Towards restore progress bar for long-running queries google-cloud-python#14476 and migrate to query_and_wait for lower-latency small queries google-cloud-python#15513 since this loop for waiting for the query to finish will be easier to add a progress bar (if we decide that's needed)
  • 🦕

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@tswast
fix: avoid unnecessary API call in QueryJob.result() when job is alre…
0c9a8a2
…ady finished
@product-auto-label product-auto-label Bot added size: m Pull request size is medium. api: bigquery Issues related to the googleapis/python-bigquery API. labels Apr 12, 2024
@tswast tswast changed the title fix: avoid unnecessary API call in QueryJob.result() when job is alre… fix: avoid unnecessary API call in QueryJob.result() when job is already finished Apr 12, 2024
@tswast

tswast commented Apr 12, 2024

Copy link
Copy Markdown
Contributor Author

CC @chalmerlowe

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tswast
fix most unit tests
07839b5
tswast
tswast commented Apr 13, 2024
View reviewed changes
Comment thread tests/unit/job/test_query.py
self.assertEqual(result.location, "asia-northeast1")
self.assertEqual(result.query_id, "xyz-abc")

def test_result_invokes_begins(self):

@tswast tswast Apr 13, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

As of #967 released in google-cloud-bigquery 3.0.0, the _begin method is no longer used for query jobs.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
tswast
tswast commented Apr 13, 2024
View reviewed changes
Comment thread tests/unit/job/test_query.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

tswast added 2 commits April 13, 2024 09:36
@tswast
fix remaining unit tests
5112315
@tswast
Merge remote-tracking branch 'origin/main' into tswast-fix-query-retry
6297efd
@tswast
tswast marked this pull request as ready for review April 13, 2024 14:44
@tswast
tswast requested review from a team and obada-ab April 13, 2024 14:44
@blunderbuss-gcf blunderbuss-gcf Bot assigned shollyman Apr 13, 2024
@tswast
tswast requested a review from chalmerlowe April 13, 2024 14:44
tswast added 2 commits April 13, 2024 10:02
@tswast
add timeout test, remove unneeded _done_or_raise override
e5990eb
@tswast
treat even finished jobs as PENDING in query_and_wait since we want t…
fddb557
…o trigger a job reload
@product-auto-label product-auto-label Bot added size: l Pull request size is large. and removed size: m Pull request size is medium. labels Apr 13, 2024
tswast
tswast commented Apr 13, 2024
View reviewed changes
Comment thread google/cloud/bigquery/job/query.py
timeout=transport_timeout,
)

def _done_or_raise(self, retry=DEFAULT_RETRY, timeout=None):

@tswast tswast Apr 13, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This was overridden because we wanted result() from the superclass to call jobs.getQueryResults, not just jobs.get (i.e. job.reload() in Python). Now that we aren't using the superclass for result(), this method is no longer necessary.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
tswast
tswast commented Apr 13, 2024
View reviewed changes
Comment thread google/cloud/bigquery/job/query.py
try:
self.reload(retry=retry, timeout=transport_timeout)
except exceptions.GoogleAPIError as exc:
self.set_exception(exc)

@tswast tswast Apr 13, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thought: We probably should have been calling set_exception based on the job status. Need to look into this further.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tswast tswast Apr 13, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

OK. We are. 😅

python-bigquery/google/cloud/bigquery/job/base.py

Line 909 in bf8861c

self.set_exception(exception)

Which we call from _set_properties

python-bigquery/google/cloud/bigquery/job/base.py

Line 677 in bf8861c

self._set_future_result()

Which we call from reload

python-bigquery/google/cloud/bigquery/job/base.py

Line 838 in bf8861c

self._set_properties(api_response)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@tswast
remove unnecessary if statement
6a43cfd
@chalmerlowe chalmerlowe self-assigned this Apr 15, 2024
tswast
tswast commented Apr 15, 2024
View reviewed changes
Comment thread google/cloud/bigquery/job/query.py
# wait for the query to finish. Unlike most methods,
# jobs.getQueryResults hangs as long as it can to ensure we
# know when the query has finished as soon as possible.
self._reload_query_results(retry=retry, timeout=timeout)

@tswast tswast Apr 15, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Uh oh, if jobs.getQueryResults fails because the job failed it can throw an exception but restart_query_job will still be False.

But we don't want restart_query_job = True because sometimes this can raise an ambiguous exception such as quota exceeded, where we don't know if it's the job quota and it's a failed job or at a higher level (Google Frontend - GFE) where the job might actually still be running and/or succeeded.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tswast tswast Apr 15, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This isn't the worst way to fail, but it'd be nice to do the jobs.get call above in case of an exception to get a chance at retrying this job if the job failed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tswast tswast Apr 16, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Filed #1903 to track improvements to ambiguous errors. 12fa9fb fixes an issue where we weren't actually retrying after an ambiguous failure even though we thought we were.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@tswast tswast mentioned this pull request Apr 16, 2024
perf: don't retry getQueryResults as often with ambiguous errors in QueryJob.result() googleapis/google-cloud-python#15714
Open
@tswast
fix: retry query job after ambiguous failures
12fa9fb
chalmerlowe
chalmerlowe suggested changes Apr 16, 2024
View reviewed changes

@chalmerlowe chalmerlowe left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I am halfway through my review.
Releasing these comments for now.
Will come back to this to finish out my review as soon as possible.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread google/cloud/bigquery/job/query.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/bigquery/job/query.py Outdated
is_job_done = job_retry(is_job_done)

do_get_result()
# timeout can be `None` or an object from our superclass

@chalmerlowe chalmerlowe Apr 16, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Which superclass are we discussing here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tswast tswast Apr 17, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

google.api_core.future.polling.PollingFuture._DEFAULT_VALUE introduced in googleapis/python-api-core#462.

I've updated the comments with some more info as well as some things to consider in case we want to have a default value for timeout in future.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread google/cloud/bigquery/retry.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/bigquery/retry.py Outdated
# rateLimitExceeded errors, which can be raised either by the Google load
# balancer or the BigQuery job server.
_DEFAULT_JOB_DEADLINE = 3.0 * _DEFAULT_RETRY_DEADLINE
_DEFAULT_JOB_DEADLINE = 4.0 * _DEFAULT_RETRY_DEADLINE

@chalmerlowe chalmerlowe Apr 16, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

What is the purpose of using 4.0 here?
Can we get a comment indicating why 4.0?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tswast tswast Apr 17, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Updated to 2.0 * (2.0 * _DEFAULT_RETRY_DEADLINE) and added some explanation both here and in QueryJob.result().

Note: This still only gets us 1 query retry in the face of the problematic ambiguous error codes from jobs.getQueryResults() but that's better than the nothing that we were actually getting before in some cases. I don't feel comfortable bumping this much further, though maybe 3.0 * 2.0 * _DEFAULT_RETRY_DEADLINE would be slightly less arbitrary at 1 hour?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread google/cloud/bigquery/retry.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread tests/unit/job/test_query.py
}
conn = make_connection(
query_resource, query_resource_done, job_resource_done, query_page_resource
job_resource,

@chalmerlowe chalmerlowe Apr 16, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Not sure I am tracking the relationship between the make connection inputs versus the assert_has_calls checks.

Can you explain how these tests are supposed to work?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tswast tswast Apr 17, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

make_connection is a convention in google-cloud-bigquery unit tests that actually predates our use of the "mock" package. It mocks out the responses to REST API calls, previously with a fake implementation of our "Connection" class from the _http module and now with a true mock object. For every quest that our test makes, there should be a corresponding response. As with Mock.side_effect, any exceptions in this list will be raised, instead.

I'm guessing your question also relates to "Why this particular set of requests / responses?". I've added some comments explaining why we're expecting this sequence of API calls. I've also updated this test to more explicitly check for a possible cause of customer issue b/332850329.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/unit/job/test_query.py
connection.api_request.assert_has_calls(
[query_results_call, query_results_call, reload_call]
[
reload_call,

@chalmerlowe chalmerlowe Apr 16, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Same thing here. Can I get some clarity on what we are doing and looking for?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tswast tswast Apr 17, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Added some explanation here as well as above in the make_connection() call.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@tswast @chalmerlowe
Apply suggestions from code review
5149c25
Co-authored-by: Chalmer Lowe <[email protected]>
chalmerlowe
chalmerlowe suggested changes Apr 17, 2024
View reviewed changes

@chalmerlowe chalmerlowe left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/unit/job/test_query.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread tests/unit/job/test_query.py Outdated

job.result()
with freezegun.freeze_time("1970-01-01 00:00:00", tick=False):
job.result(timeout=1.125)

@chalmerlowe chalmerlowe Apr 17, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Is there are reason we are using such a specific number?
1.125.

Can I get a comment here to let future me know why we picked this number?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/unit/job/test_query.py
method="GET",
path=f"/projects/{self.PROJECT}/queries/{self.JOB_ID}",
query_params={
"maxResults": 0,

@chalmerlowe chalmerlowe Apr 17, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Is maxResults of 0 synonymous with asking for all results? OR is it really asking for zero results?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tswast tswast Apr 17, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We actually want 0 rows. If we omit this or ask for non-zero number of rows, the jobs.getQueryResults API can hang when the query has wide rows (many columns).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/unit/test_job_retry.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

tswast and others added 4 commits April 17, 2024 13:37
@tswast
add comments explaining expected sequences of API calls
4ee4975
@tswast
explain timeout choice
08373bc
@tswast @chalmerlowe
Apply suggestions from code review
2a587aa
Co-authored-by: Chalmer Lowe <[email protected]>
@tswast
Merge remote-tracking branch 'origin/tswast-fix-query-retry' into tsw…
f7f1e81
…ast-fix-query-retry
@tswast
tswast requested a review from chalmerlowe April 17, 2024 19:34
chalmerlowe
chalmerlowe approved these changes Apr 18, 2024
View reviewed changes

@chalmerlowe chalmerlowe left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks for all the comments, etc.
Future me thanks you as well.

LGTM, APPROVED.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tswast reacted with heart emoji
All reactions
  • ❤️ 1 reaction
Comment thread google/cloud/bigquery/job/query.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@chalmerlowe
Update google/cloud/bigquery/job/query.py
e42d8ad
@tswast
tswast merged commit 1367b58 into main Apr 18, 2024
@tswast
tswast deleted the tswast-fix-query-retry branch April 18, 2024 14:31
@release-please release-please Bot mentioned this pull request Apr 18, 2024
chore(main): release 3.21.0 #1883
Merged
@colin-k-rogers colin-k-rogers mentioned this pull request Apr 19, 2024
Preserve nicely formatted timeout exception dbt-labs/dbt-bigquery#1187
Merged
4 tasks
This was referenced Apr 23, 2024
April 22, 2024 kitta65/bq-extension-vscode#374
Closed
April 22, 2024 kitta65/prettier-plugin-bq#364
Closed
April 22, 2024 kitta65/bq2cst#349
Closed
@pankajastro pankajastro mentioned this pull request May 7, 2024
Adjust test for Airflow-2.9 astronomer/astro-sdk#2149
Merged
@tswast tswast mentioned this pull request May 22, 2024
Retry requests.exceptions.ConnectionError #1929
Closed
@tswast tswast mentioned this pull request Jun 3, 2024
QueryJob class break flow after upgrade to 3.21.0 #1940
Closed
@Linchin Linchin mentioned this pull request Jun 4, 2024
fix: create query job in job.result() if doesn't exist #1944
Merged
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@obada-ab obada-ab Awaiting requested review from obada-ab
1 more reviewer
@chalmerlowe chalmerlowe chalmerlowe approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@shollyman shollyman

@chalmerlowe chalmerlowe

Labels

api: bigquery Issues related to the googleapis/python-bigquery API. size: l Pull request size is large.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@tswast @chalmerlowe @shollyman
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.