You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by yh...@apache.org on 2025/05/02 18:39:39 UTC

(beam) branch master updated: Fix test retry on throttling (#34786)

This is an automated email from the ASF dual-hosted git repository.

yhu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new 9fd7d092490 Fix test retry on throttling (#34786)
9fd7d092490 is described below

commit 9fd7d092490e2850822520356ab43d0e24db5281
Author: akashorabek <70...@users.noreply.github.com>
AuthorDate: Fri May 2 23:39:21 2025 +0500

    Fix test retry on throttling (#34786)
    
    * Change google-api-core version
    
    * Update ThrottlingHandler logic
    
    * Fix formatting
    
    * Add dynamic arg name choice to ThrottlingHandler
    
    * Fix formatting
    
    * Fix formatting
---
 sdks/python/apache_beam/io/gcp/gcsio_retry.py      | 33 ++++++++++++++++++++--
 sdks/python/apache_beam/io/gcp/gcsio_retry_test.py |  1 -
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/sdks/python/apache_beam/io/gcp/gcsio_retry.py b/sdks/python/apache_beam/io/gcp/gcsio_retry.py
index 29fd71c5195..5e75c0e5632 100644
--- a/sdks/python/apache_beam/io/gcp/gcsio_retry.py
+++ b/sdks/python/apache_beam/io/gcp/gcsio_retry.py
@@ -22,11 +22,14 @@ Throttling Handler for GCSIO
 import inspect
 import logging
 import math
+from itertools import tee
 
+from google.api_core import __version__ as _api_core_version
 from google.api_core import exceptions as api_exceptions
 from google.api_core import retry
 from google.cloud.storage.retry import DEFAULT_RETRY
 from google.cloud.storage.retry import _should_retry  # pylint: disable=protected-access
+from packaging import version
 
 from apache_beam.metrics.metric import Metrics
 from apache_beam.options.pipeline_options import GoogleCloudOptions
@@ -34,11 +37,25 @@ from apache_beam.options.pipeline_options import GoogleCloudOptions
 _LOGGER = logging.getLogger(__name__)
 
 __all__ = ['DEFAULT_RETRY_WITH_THROTTLING_COUNTER']
+_MIN_SLEEP_ARG_SWITCH_VERSION = version.parse("2.25.0rc0")
+_LEGACY_SLEEP_ARG_NAME = "next_sleep"
+_CURRENT_SLEEP_ARG_NAME = "sleep_iterator"
 
 
 class ThrottlingHandler(object):
   _THROTTLED_SECS = Metrics.counter('gcsio', "cumulativeThrottlingSeconds")
 
+  def __init__(self):
+    # decide which arg name google-api-core uses
+    try:
+      core_ver = version.parse(_api_core_version)
+    except Exception:
+      core_ver = version.parse("0")
+    if core_ver < _MIN_SLEEP_ARG_SWITCH_VERSION:
+      self._sleep_arg = _LEGACY_SLEEP_ARG_NAME
+    else:
+      self._sleep_arg = _CURRENT_SLEEP_ARG_NAME
+
   def __call__(self, exc):
     if isinstance(exc, api_exceptions.TooManyRequests):
       _LOGGER.debug('Caught GCS quota error (%s), retrying.', exc.reason)
@@ -54,9 +71,19 @@ class ThrottlingHandler(object):
         _LOGGER.warning('cannot inspect the caller stack frame')
         return
 
-      # next_sleep is one of the arguments in the caller
-      # i.e. _retry_error_helper() in google/api_core/retry/retry_base.py
-      sleep_seconds = prev_frame.f_locals.get("next_sleep", 0)
+      # Determine which retry helper argument to inspect in
+      # google/api_core/retry/retry_base.py’s _retry_error_helper():
+      #  - versions < 2.25.0rc0 use “next_sleep”
+      #  - versions ≥ 2.25.0rc0 use “sleep_iterator”
+      if self._sleep_arg == _LEGACY_SLEEP_ARG_NAME:
+        sleep_seconds = prev_frame.f_locals.get(self._sleep_arg, 0)
+      else:
+        sleep_iterator = prev_frame.f_locals.get(self._sleep_arg, iter([]))
+        sleep_iterator, sleep_iterator_copy = tee(sleep_iterator)
+        try:
+          sleep_seconds = next(sleep_iterator_copy)
+        except StopIteration:
+          sleep_seconds = 0
       ThrottlingHandler._THROTTLED_SECS.inc(math.ceil(sleep_seconds))
 
 
diff --git a/sdks/python/apache_beam/io/gcp/gcsio_retry_test.py b/sdks/python/apache_beam/io/gcp/gcsio_retry_test.py
index a27860fcf8c..750879ae028 100644
--- a/sdks/python/apache_beam/io/gcp/gcsio_retry_test.py
+++ b/sdks/python/apache_beam/io/gcp/gcsio_retry_test.py
@@ -34,7 +34,6 @@ except ImportError:
   api_exceptions = None
 
 
-@unittest.skip("https://github.com/apache/beam/issues/34736")
 @unittest.skipIf((gcsio_retry is None or api_exceptions is None),
                  'GCP dependencies are not installed')
 class TestGCSIORetry(unittest.TestCase):