Skip to content

Commit c027ccf

Browse files
authored
fix: replace default retry for upload operations (#480)
* fix: revise upload operations preconditions to if_generation_match * fix docstrings * add retry configuration to blob.create_resumable_upload_session * align var values in test * test coverage * Revert "test coverage" This reverts commit e91916f. * Revert "align var values in test" This reverts commit aec585b. * Revert "add retry configuration to blob.create_resumable_upload_session" This reverts commit 8c1ae3c. * revise tests after reverting
1 parent e3e57a9 commit c027ccf

File tree

4 files changed

+36
-40
lines changed

4 files changed

+36
-40
lines changed

google/cloud/storage/blob.py

+25-26
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
from google.cloud.storage.retry import DEFAULT_RETRY
8383
from google.cloud.storage.retry import DEFAULT_RETRY_IF_ETAG_IN_JSON
8484
from google.cloud.storage.retry import DEFAULT_RETRY_IF_GENERATION_SPECIFIED
85-
from google.cloud.storage.retry import DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED
8685
from google.cloud.storage.fileio import BlobReader
8786
from google.cloud.storage.fileio import BlobWriter
8887

@@ -1703,10 +1702,10 @@ def _do_multipart_upload(
17031702
:type num_retries: int
17041703
:param num_retries:
17051704
Number of upload retries. By default, only uploads with
1706-
if_metageneration_match set will be retried, as uploads without the
1705+
if_generation_match set will be retried, as uploads without the
17071706
argument are not guaranteed to be idempotent. Setting num_retries
17081707
will override this default behavior and guarantee retries even when
1709-
if_metageneration_match is not set. (Deprecated: This argument
1708+
if_generation_match is not set. (Deprecated: This argument
17101709
will be removed in a future release.)
17111710
17121711
:type predefined_acl: str
@@ -1750,7 +1749,7 @@ def _do_multipart_upload(
17501749
17511750
This private method does not accept ConditionalRetryPolicy values
17521751
because the information necessary to evaluate the policy is instead
1753-
evaluated in client.download_blob_to_file().
1752+
evaluated in blob._do_upload().
17541753
17551754
See the retry.py source code and docstrings in this package
17561755
(google.cloud.storage.retry) for information on retry types and how
@@ -1877,10 +1876,10 @@ def _initiate_resumable_upload(
18771876
:type num_retries: int
18781877
:param num_retries:
18791878
Number of upload retries. By default, only uploads with
1880-
if_metageneration_match set will be retried, as uploads without the
1879+
if_generation_match set will be retried, as uploads without the
18811880
argument are not guaranteed to be idempotent. Setting num_retries
18821881
will override this default behavior and guarantee retries even when
1883-
if_metageneration_match is not set. (Deprecated: This argument
1882+
if_generation_match is not set. (Deprecated: This argument
18841883
will be removed in a future release.)
18851884
18861885
:type extra_headers: dict
@@ -1936,7 +1935,7 @@ def _initiate_resumable_upload(
19361935
19371936
This private method does not accept ConditionalRetryPolicy values
19381937
because the information necessary to evaluate the policy is instead
1939-
evaluated in client.download_blob_to_file().
1938+
evaluated in blob._do_upload().
19401939
19411940
See the retry.py source code and docstrings in this package
19421941
(google.cloud.storage.retry) for information on retry types and how
@@ -2070,10 +2069,10 @@ def _do_resumable_upload(
20702069
:type num_retries: int
20712070
:param num_retries:
20722071
Number of upload retries. By default, only uploads with
2073-
if_metageneration_match set will be retried, as uploads without the
2072+
if_generation_match set will be retried, as uploads without the
20742073
argument are not guaranteed to be idempotent. Setting num_retries
20752074
will override this default behavior and guarantee retries even when
2076-
if_metageneration_match is not set. (Deprecated: This argument
2075+
if_generation_match is not set. (Deprecated: This argument
20772076
will be removed in a future release.)
20782077
20792078
:type predefined_acl: str
@@ -2119,7 +2118,7 @@ def _do_resumable_upload(
21192118
21202119
This private method does not accept ConditionalRetryPolicy values
21212120
because the information necessary to evaluate the policy is instead
2122-
evaluated in client.download_blob_to_file().
2121+
evaluated in blob._do_upload().
21232122
21242123
See the retry.py source code and docstrings in this package
21252124
(google.cloud.storage.retry) for information on retry types and how
@@ -2204,10 +2203,10 @@ def _do_upload(
22042203
:type num_retries: int
22052204
:param num_retries:
22062205
Number of upload retries. By default, only uploads with
2207-
if_metageneration_match set will be retried, as uploads without the
2206+
if_generation_match set will be retried, as uploads without the
22082207
argument are not guaranteed to be idempotent. Setting num_retries
22092208
will override this default behavior and guarantee retries even when
2210-
if_metageneration_match is not set. (Deprecated: This argument
2209+
if_generation_match is not set. (Deprecated: This argument
22112210
will be removed in a future release.)
22122211
22132212
:type predefined_acl: str
@@ -2258,7 +2257,7 @@ def _do_upload(
22582257
This class exists to provide safe defaults for RPC calls that are
22592258
not technically safe to retry normally (due to potential data
22602259
duplication or other side-effects) but become safe to retry if a
2261-
condition such as if_metageneration_match is set.
2260+
condition such as if_generation_match is set.
22622261
22632262
See the retry.py source code and docstrings in this package
22642263
(google.cloud.storage.retry) for information on retry types and how
@@ -2337,7 +2336,7 @@ def upload_from_file(
23372336
if_metageneration_not_match=None,
23382337
timeout=_DEFAULT_TIMEOUT,
23392338
checksum=None,
2340-
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
2339+
retry=DEFAULT_RETRY_IF_GENERATION_SPECIFIED,
23412340
):
23422341
"""Upload the contents of this blob from a file-like object.
23432342
@@ -2397,10 +2396,10 @@ def upload_from_file(
23972396
:type num_retries: int
23982397
:param num_retries:
23992398
Number of upload retries. By default, only uploads with
2400-
if_metageneration_match set will be retried, as uploads without the
2399+
if_generation_match set will be retried, as uploads without the
24012400
argument are not guaranteed to be idempotent. Setting num_retries
24022401
will override this default behavior and guarantee retries even when
2403-
if_metageneration_match is not set. (Deprecated: This argument
2402+
if_generation_match is not set. (Deprecated: This argument
24042403
will be removed in a future release.)
24052404
24062405
:type client: :class:`~google.cloud.storage.client.Client`
@@ -2456,7 +2455,7 @@ def upload_from_file(
24562455
This class exists to provide safe defaults for RPC calls that are
24572456
not technically safe to retry normally (due to potential data
24582457
duplication or other side-effects) but become safe to retry if a
2459-
condition such as if_metageneration_match is set.
2458+
condition such as if_generation_match is set.
24602459
24612460
See the retry.py source code and docstrings in this package
24622461
(google.cloud.storage.retry) for information on retry types and how
@@ -2479,7 +2478,7 @@ def upload_from_file(
24792478
# num_retries and retry are mutually exclusive. If num_retries is
24802479
# set and retry is exactly the default, then nullify retry for
24812480
# backwards compatibility.
2482-
if retry is DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED:
2481+
if retry is DEFAULT_RETRY_IF_GENERATION_SPECIFIED:
24832482
retry = None
24842483

24852484
_maybe_rewind(file_obj, rewind=rewind)
@@ -2518,7 +2517,7 @@ def upload_from_filename(
25182517
if_metageneration_not_match=None,
25192518
timeout=_DEFAULT_TIMEOUT,
25202519
checksum=None,
2521-
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
2520+
retry=DEFAULT_RETRY_IF_GENERATION_SPECIFIED,
25222521
):
25232522
"""Upload this blob's contents from the content of a named file.
25242523
@@ -2558,10 +2557,10 @@ def upload_from_filename(
25582557
:type num_retries: int
25592558
:param num_retries:
25602559
Number of upload retries. By default, only uploads with
2561-
if_metageneration_match set will be retried, as uploads without the
2560+
if_generation_match set will be retried, as uploads without the
25622561
argument are not guaranteed to be idempotent. Setting num_retries
25632562
will override this default behavior and guarantee retries even when
2564-
if_metageneration_match is not set. (Deprecated: This argument
2563+
if_generation_match is not set. (Deprecated: This argument
25652564
will be removed in a future release.)
25662565
25672566
:type predefined_acl: str
@@ -2612,7 +2611,7 @@ def upload_from_filename(
26122611
This class exists to provide safe defaults for RPC calls that are
26132612
not technically safe to retry normally (due to potential data
26142613
duplication or other side-effects) but become safe to retry if a
2615-
condition such as if_metageneration_match is set.
2614+
condition such as if_generation_match is set.
26162615
26172616
See the retry.py source code and docstrings in this package
26182617
(google.cloud.storage.retry) for information on retry types and how
@@ -2656,7 +2655,7 @@ def upload_from_string(
26562655
if_metageneration_not_match=None,
26572656
timeout=_DEFAULT_TIMEOUT,
26582657
checksum=None,
2659-
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
2658+
retry=DEFAULT_RETRY_IF_GENERATION_SPECIFIED,
26602659
):
26612660
"""Upload contents of this blob from the provided string.
26622661
@@ -2687,10 +2686,10 @@ def upload_from_string(
26872686
:type num_retries: int
26882687
:param num_retries:
26892688
Number of upload retries. By default, only uploads with
2690-
if_metageneration_match set will be retried, as uploads without the
2689+
if_generation_match set will be retried, as uploads without the
26912690
argument are not guaranteed to be idempotent. Setting num_retries
26922691
will override this default behavior and guarantee retries even when
2693-
if_metageneration_match is not set. (Deprecated: This argument
2692+
if_generation_match is not set. (Deprecated: This argument
26942693
will be removed in a future release.)
26952694
26962695
:type client: :class:`~google.cloud.storage.client.Client`
@@ -2746,7 +2745,7 @@ def upload_from_string(
27462745
This class exists to provide safe defaults for RPC calls that are
27472746
not technically safe to retry normally (due to potential data
27482747
duplication or other side-effects) but become safe to retry if a
2749-
condition such as if_metageneration_match is set.
2748+
condition such as if_generation_match is set.
27502749
27512750
See the retry.py source code and docstrings in this package
27522751
(google.cloud.storage.retry) for information on retry types and how

google/cloud/storage/fileio.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from google.api_core.exceptions import RequestRangeNotSatisfiable
1919
from google.cloud.storage._helpers import _NUM_RETRIES_MESSAGE
2020
from google.cloud.storage.retry import DEFAULT_RETRY
21-
from google.cloud.storage.retry import DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED
21+
from google.cloud.storage.retry import DEFAULT_RETRY_IF_GENERATION_SPECIFIED
2222
from google.cloud.storage.retry import ConditionalRetryPolicy
2323

2424

@@ -278,7 +278,7 @@ def __init__(
278278
blob,
279279
chunk_size=None,
280280
text_mode=False,
281-
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
281+
retry=DEFAULT_RETRY_IF_GENERATION_SPECIFIED,
282282
**upload_kwargs
283283
):
284284
for kwarg in upload_kwargs:
@@ -346,7 +346,7 @@ def _initiate_upload(self):
346346
# num_retries and retry are mutually exclusive. If num_retries is
347347
# set and retry is exactly the default, then nullify retry for
348348
# backwards compatibility.
349-
if retry is DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED:
349+
if retry is DEFAULT_RETRY_IF_GENERATION_SPECIFIED:
350350
retry = None
351351

352352
# Handle ConditionalRetryPolicy.

tests/unit/test_blob.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from google.cloud.storage.retry import DEFAULT_RETRY
3030
from google.cloud.storage.retry import DEFAULT_RETRY_IF_ETAG_IN_JSON
3131
from google.cloud.storage.retry import DEFAULT_RETRY_IF_GENERATION_SPECIFIED
32-
from google.cloud.storage.retry import DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED
3332

3433

3534
def _make_credentials():
@@ -2853,8 +2852,8 @@ def _do_upload_helper(
28532852
**timeout_kwarg
28542853
)
28552854

2856-
if retry is DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED:
2857-
retry = DEFAULT_RETRY if if_metageneration_match else None
2855+
if retry is DEFAULT_RETRY_IF_GENERATION_SPECIFIED:
2856+
retry = DEFAULT_RETRY if if_generation_match else None
28582857

28592858
self.assertIs(created_json, mock.sentinel.json)
28602859
response.json.assert_called_once_with()
@@ -2925,11 +2924,11 @@ def test__do_upload_with_num_retries(self):
29252924

29262925
def test__do_upload_with_conditional_retry_success(self):
29272926
self._do_upload_helper(
2928-
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED, if_metageneration_match=1
2927+
retry=DEFAULT_RETRY_IF_GENERATION_SPECIFIED, if_generation_match=123456
29292928
)
29302929

29312930
def test__do_upload_with_conditional_retry_failure(self):
2932-
self._do_upload_helper(retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED)
2931+
self._do_upload_helper(retry=DEFAULT_RETRY_IF_GENERATION_SPECIFIED)
29332932

29342933
def _upload_from_file_helper(self, side_effect=None, **kwargs):
29352934
from google.cloud._helpers import UTC
@@ -2955,7 +2954,7 @@ def _upload_from_file_helper(self, side_effect=None, **kwargs):
29552954
if_metageneration_not_match = kwargs.get("if_metageneration_not_match", None)
29562955
num_retries = kwargs.get("num_retries", None)
29572956
default_retry = (
2958-
DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED if not num_retries else None
2957+
DEFAULT_RETRY_IF_GENERATION_SPECIFIED if not num_retries else None
29592958
)
29602959
retry = kwargs.get("retry", default_retry)
29612960
ret_val = blob.upload_from_file(
@@ -3062,9 +3061,7 @@ def _do_upload_mock_call_helper(
30623061

30633062
expected_timeout = self._get_default_timeout() if timeout is None else timeout
30643063
if not retry:
3065-
retry = (
3066-
DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED if not num_retries else None
3067-
)
3064+
retry = DEFAULT_RETRY_IF_GENERATION_SPECIFIED if not num_retries else None
30683065
self.assertEqual(
30693066
kwargs, {"timeout": expected_timeout, "checksum": None, "retry": retry}
30703067
)

tests/unit/test_fileio.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def test_conditional_retry_pass(self):
395395
blob,
396396
chunk_size=chunk_size,
397397
content_type=PLAIN_CONTENT_TYPE,
398-
if_metageneration_match=1,
398+
if_generation_match=123456,
399399
)
400400

401401
# The transmit_next_chunk method must actually consume bytes from the
@@ -421,7 +421,7 @@ def test_conditional_retry_pass(self):
421421
None, # num_retries
422422
chunk_size=chunk_size,
423423
retry=DEFAULT_RETRY,
424-
if_metageneration_match=1,
424+
if_generation_match=123456,
425425
)
426426
upload.transmit_next_chunk.assert_called_with(transport)
427427
self.assertEqual(upload.transmit_next_chunk.call_count, 4)

0 commit comments

Comments
 (0)