From db40ab4fd48040441ca458965ffd55d219c8dc54 Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Thu, 17 Aug 2017 12:25:34 -0700 Subject: [PATCH 1/2] Drop tenacity dependency; use google.api.core.retry in google.api.core.future --- core/google/api/core/future/polling.py | 46 +++++++++++--------------- core/setup.py | 1 - 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/core/google/api/core/future/polling.py b/core/google/api/core/future/polling.py index 40380d6ad938..06b277bf012a 100644 --- a/core/google/api/core/future/polling.py +++ b/core/google/api/core/future/polling.py @@ -16,16 +16,18 @@ import abc import concurrent.futures -import functools -import operator - -import six -import tenacity +from google.api.core import exceptions +from google.api.core import retry from google.api.core.future import _helpers from google.api.core.future import base +class _OperationNotComplete(Exception): + """Private exception used for polling via retry.""" + pass + + class PollingFuture(base.Future): """A Future that needs to poll some service to check its status. @@ -69,29 +71,21 @@ def _blocking_poll(self, timeout=None): if self._result_set: return - retry_on = tenacity.retry_if_result( - functools.partial(operator.is_not, True)) - # Use exponential backoff with jitter. - wait_on = ( - tenacity.wait_exponential(multiplier=1, max=10) + - tenacity.wait_random(0, 1)) - - if timeout is None: - retry = tenacity.retry(retry=retry_on, wait=wait_on) - else: - retry = tenacity.retry( - retry=retry_on, - wait=wait_on, - stop=tenacity.stop_after_delay(timeout)) + def done_or_raise(): + """Checks if the future is done and raises if it's not.""" + if not self.done(): + raise _OperationNotComplete() + + retry_ = retry.Retry( + predicate=retry.if_exception_type(_OperationNotComplete), + deadline=timeout) try: - retry(self.done)() - except tenacity.RetryError as exc: - six.raise_from( - concurrent.futures.TimeoutError( - 'Operation did not complete within the designated ' - 'timeout.'), - exc) + retry_(done_or_raise)() + except exceptions.RetryError: + raise concurrent.futures.TimeoutError( + 'Operation did not complete within the designated ' + 'timeout.') def result(self, timeout=None): """Get the result of the operation, blocking if necessary. diff --git a/core/setup.py b/core/setup.py index c45f7dd24ac2..6adacb0e6c1b 100644 --- a/core/setup.py +++ b/core/setup.py @@ -57,7 +57,6 @@ 'requests >= 2.18.0, < 3.0.0dev', 'setuptools >= 34.0.0', 'six', - 'tenacity >= 4.0.0, <5.0.0dev' ] EXTRAS_REQUIREMENTS = { From 9550e3eec378fa1fbfb82fb11f945053166a8416 Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Thu, 17 Aug 2017 15:45:05 -0700 Subject: [PATCH 2/2] Address review comments --- core/google/api/core/future/polling.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/google/api/core/future/polling.py b/core/google/api/core/future/polling.py index 06b277bf012a..9e3d07e7128f 100644 --- a/core/google/api/core/future/polling.py +++ b/core/google/api/core/future/polling.py @@ -57,6 +57,11 @@ def done(self): # pylint: disable=redundant-returns-doc, missing-raises-doc raise NotImplementedError() + def _done_or_raise(self): + """Check if the future is done and raise if it's not.""" + if not self.done(): + raise _OperationNotComplete() + def running(self): """True if the operation is currently running.""" return not self.done() @@ -71,17 +76,12 @@ def _blocking_poll(self, timeout=None): if self._result_set: return - def done_or_raise(): - """Checks if the future is done and raises if it's not.""" - if not self.done(): - raise _OperationNotComplete() - retry_ = retry.Retry( predicate=retry.if_exception_type(_OperationNotComplete), deadline=timeout) try: - retry_(done_or_raise)() + retry_(self._done_or_raise)() except exceptions.RetryError: raise concurrent.futures.TimeoutError( 'Operation did not complete within the designated '