Skip to content

use blob.exists to check the GCS file #34818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 10, 2025
Merged

Conversation

liferoad
Copy link
Contributor

@liferoad liferoad commented May 2, 2025

Addresses #34792

Note: This wont fix the slowness when checking 50K GCD files exist. But this optimizes the speed slightly.


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

Copy link
Contributor

github-actions bot commented May 2, 2025

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

Copy link
Contributor

github-actions bot commented May 2, 2025

Assigning reviewers. If you would like to opt out of this review, comment assign to next reviewer:

R: @shunping for label python.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Copy link
Collaborator

@shunping shunping left a comment

Choose a reason for hiding this comment

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

Thanks for attempting to fix the issue.

I see you are trying to replace bucket.get_blob, called in _gcs_object, with blob.exists.

I took a look at the two function calls.

It seems that under the hood, both apis are calling the same client._get_resource, though the query parameters may be different. I am not sure if the change could make any performance improvement for the use case here.

Adding @cojenco and @andrewsg from GCS client library to chime in regarding any performance difference between bucket.get_blob and blob.exists.

@shunping
Copy link
Collaborator

shunping commented May 2, 2025

I ran some tests on both apis, it does have some difference, though not big

from google.cloud import storage
import timeit

storage_client = storage.Client()

bucket = storage_client.bucket("shunping-test")

time_taken = timeit.timeit(
  lambda : bucket.get_blob("wordcount-output-00000-of-00001"),
  number=100)
print(time_taken)

blob = bucket.blob("wordcount-output-00000-of-00001")
time_taken = timeit.timeit(blob.exists, number=100)

print(time_taken)
6.079686500015669
5.889628666976932

@cojenco
Copy link

cojenco commented May 2, 2025

Thanks for attempting to fix the issue.

I see you are trying to replace bucket.get_blob, called in _gcs_object, with blob.exists.

I took a look at the two function calls.

It seems that under the hood, both apis are calling the same client._get_resource, though the query parameters may be different. I am not sure if the change could make any performance improvement for the use case here.

Adding @cojenco and @andrewsg from GCS client library to chime in regarding any performance difference between bucket.get_blob and blob.exists.

Thanks for tagging! That's right, both bucket.get_blob and blob.exists ultimately call client._get_resource, so the performance difference is likely negligible.

Copy link
Contributor

Reminder, please take a look at this pr: @shunping

@shunping
Copy link
Collaborator

On a second thought, using blob.exists could reduce the returned payload as mentioned in the comment of the function, so it is more optimized for checking the existence of a blob. Given there is no regression on tests, I am approving the change.

Copy link
Collaborator

@shunping shunping left a comment

Choose a reason for hiding this comment

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

LGTM!

@liferoad liferoad merged commit 1f31eb0 into apache:master May 10, 2025
90 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants