forked from grpc/psm-interop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretryers.py
More file actions
338 lines (288 loc) · 11.1 KB
/
Copy pathretryers.py
File metadata and controls
338 lines (288 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# Copyright 2020 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This contains common retrying helpers (retryers).
We use tenacity as a general-purpose retrying library.
> It [tenacity] originates from a fork of retrying which is sadly no
> longer maintained. Tenacity isn’t api compatible with retrying but >
> adds significant new functionality and fixes a number of longstanding bugs.
> - https://tenacity.readthedocs.io/en/latest/index.html
"""
import datetime
import logging
from typing import Any, Callable, List, Optional, Tuple, Type
import tenacity
from tenacity import _utils as tenacity_utils
from tenacity import stop
from tenacity import wait
from tenacity.retry import retry_base
retryers_logger = logging.getLogger(__name__)
# Type aliases
timedelta = datetime.timedelta
Retrying = tenacity.Retrying
CheckResultFn = Callable[[Any], bool]
_ExceptionClasses = Tuple[Type[Exception], ...]
def _build_retry_conditions(
*,
retry_on_exceptions: Optional[_ExceptionClasses] = None,
check_result: Optional[CheckResultFn] = None,
) -> List[retry_base]:
# Retry on all exceptions by default
if retry_on_exceptions is None:
retry_on_exceptions = (Exception,)
retry_conditions = [tenacity.retry_if_exception_type(retry_on_exceptions)]
if check_result is not None:
if retry_on_exceptions:
# When retry_on_exceptions is set, also catch them while executing
# check_result callback.
check_result = _safe_check_result(check_result, retry_on_exceptions)
retry_conditions.append(tenacity.retry_if_not_result(check_result))
return retry_conditions
def exponential_retryer_with_timeout(
*,
wait_min: timedelta,
wait_max: timedelta,
timeout: timedelta,
retry_on_exceptions: Optional[_ExceptionClasses] = None,
check_result: Optional[CheckResultFn] = None,
logger: Optional[logging.Logger] = None,
log_level: Optional[int] = logging.DEBUG,
error_note: str = "",
) -> Retrying:
if logger is None:
logger = retryers_logger
if log_level is None:
log_level = logging.DEBUG
retry_conditions = _build_retry_conditions(
retry_on_exceptions=retry_on_exceptions, check_result=check_result
)
retry_error_callback = _on_error_callback(
timeout=timeout, check_result=check_result, error_note=error_note
)
return Retrying(
retry=tenacity.retry_any(*retry_conditions),
wait=wait.wait_exponential(
min=wait_min.total_seconds(), max=wait_max.total_seconds()
),
stop=stop.stop_after_delay(timeout.total_seconds()),
before_sleep=_before_sleep_log(logger, log_level),
retry_error_callback=retry_error_callback,
)
def exponential_retryer_with_attempts(
*,
wait_min: timedelta,
wait_max: timedelta,
attempts: int,
retry_on_exceptions: Optional[_ExceptionClasses] = None,
retry_on_predicate: Optional[Callable[[BaseException], bool]] = None,
logger: Optional[logging.Logger] = None,
log_level: Optional[int] = logging.DEBUG,
) -> Retrying:
if logger is None:
logger = retryers_logger
if log_level is None:
log_level = logging.DEBUG
retry_conditions = []
if retry_on_exceptions:
retry_conditions.append(
tenacity.retry_if_exception_type(retry_on_exceptions)
)
if retry_on_predicate:
retry_conditions.append(tenacity.retry_if_exception(retry_on_predicate))
if not retry_conditions:
retry_conditions.append(tenacity.retry_if_exception_type(Exception))
retry_error_callback = _on_error_callback(attempts=attempts)
return Retrying(
retry=tenacity.retry_any(*retry_conditions),
wait=wait.wait_exponential(
min=wait_min.total_seconds(), max=wait_max.total_seconds()
),
stop=stop.stop_after_attempt(attempts),
before_sleep=_before_sleep_log(logger, log_level),
retry_error_callback=retry_error_callback,
)
def constant_retryer(
*,
wait_fixed: timedelta,
attempts: int = 0,
timeout: Optional[timedelta] = None,
retry_on_exceptions: Optional[_ExceptionClasses] = None,
check_result: Optional[CheckResultFn] = None,
logger: Optional[logging.Logger] = None,
log_level: Optional[int] = logging.DEBUG,
error_note: str = "",
) -> Retrying:
if logger is None:
logger = retryers_logger
if log_level is None:
log_level = logging.DEBUG
if attempts < 1 and timeout is None:
raise ValueError("The number of attempts or the timeout must be set")
stops = []
if attempts > 0:
stops.append(stop.stop_after_attempt(attempts))
if timeout is not None:
stops.append(stop.stop_after_delay(timeout.total_seconds()))
retry_conditions = _build_retry_conditions(
retry_on_exceptions=retry_on_exceptions, check_result=check_result
)
retry_error_callback = _on_error_callback(
timeout=timeout,
attempts=attempts,
check_result=check_result,
error_note=error_note,
)
return Retrying(
retry=tenacity.retry_any(*retry_conditions),
wait=wait.wait_fixed(wait_fixed.total_seconds()),
stop=stop.stop_any(*stops),
before_sleep=_before_sleep_log(logger, log_level),
retry_error_callback=retry_error_callback,
)
def _on_error_callback(
*,
timeout: Optional[timedelta] = None,
attempts: int = 0,
check_result: Optional[CheckResultFn] = None,
error_note: str = "",
):
"""A helper to propagate the initial state to the RetryError, so that
it can assemble a helpful message containing timeout/number of attempts.
"""
def error_handler(retry_state: tenacity.RetryCallState):
raise RetryError(
retry_state,
timeout=timeout,
attempts=attempts,
check_result=check_result,
note=error_note,
)
return error_handler
def _safe_check_result(
check_result: CheckResultFn, retry_on_exceptions: _ExceptionClasses
) -> CheckResultFn:
"""Wraps check_result callback to catch and handle retry_on_exceptions.
Normally tenacity doesn't retry when retry_if_result/retry_if_not_result
raise an error. This wraps the callback to automatically catch Exceptions
specified in the retry_on_exceptions argument.
Ideally we should make all check_result callbacks to not throw, but
in case it does, we'd rather be annoying in the logs, than break the test.
"""
def _check_result_wrapped(result):
try:
return check_result(result)
except retry_on_exceptions:
retryers_logger.warning(
(
"Result check callback %s raised an exception."
"This shouldn't happen, please handle any exceptions and "
"return return a boolean."
),
tenacity_utils.get_callback_name(check_result),
exc_info=True,
)
return False
return _check_result_wrapped
def _before_sleep_log(logger, log_level, exc_info=False):
"""Same as tenacity.before_sleep_log, but only logs primitive return values.
This is not useful when the return value is a dump of a large object.
"""
def log_it(retry_state):
if retry_state.outcome.failed:
ex = retry_state.outcome.exception()
verb, value = "raised", "%s: %s" % (type(ex).__name__, ex)
if exc_info:
local_exc_info = ex
else:
local_exc_info = False
else:
local_exc_info = False # exc_info does not apply when no exception
result = retry_state.outcome.result()
if isinstance(result, (int, bool, str)):
verb, value = "returned", result
else:
verb, value = "returned type", type(result)
logger.log(
log_level,
"Retrying %s in %s seconds as it %s %s",
tenacity_utils.get_callback_name(retry_state.fn),
getattr(retry_state.next_action, "sleep"),
verb,
value,
exc_info=local_exc_info,
)
return log_it
class RetryError(tenacity.RetryError):
# Note: framework.errors.FrameworkError could be used as a mixin,
# but this would rely too much on tenacity.RetryError to not change.
last_attempt: tenacity.Future
note: str = ""
def __init__(
self,
retry_state,
*,
timeout: Optional[timedelta] = None,
attempts: int = 0,
check_result: Optional[CheckResultFn] = None,
note: str = "",
):
last_attempt: tenacity.Future = retry_state.outcome
super().__init__(last_attempt)
self.message = f"Retry error"
if retry_state.fn is None:
# Context manager
self.message += f":"
else:
callback_name = tenacity_utils.get_callback_name(retry_state.fn)
self.message += f" calling {callback_name}:"
if timeout:
self.message += f" timeout {timeout} (h:mm:ss) exceeded"
if attempts:
self.message += " or"
if attempts:
self.message += f" {attempts} attempts exhausted"
self.message += "."
if last_attempt.failed:
err = last_attempt.exception()
self.message += f" Last exception: {type(err).__name__}: {err}"
elif check_result:
self.message += " Check result callback returned False."
if note:
self.add_note(note)
def result(self, *, default=None):
return (
self.last_attempt.result()
if not self.last_attempt.failed
else default
)
def exception(self, *, default=None):
return (
self.last_attempt.exception()
if self.last_attempt.failed
else default
)
def exception_str(self) -> str:
return f"Error: {self._exception_str(self.exception())}"
def result_str(self) -> str:
result = self.result()
return f"Result: {result}" if result is not None else "No result"
def reason_str(self):
return self.exception_str() if self.exception() else self.result_str()
@classmethod
def _exception_str(cls, err: Optional[BaseException]) -> str:
return f"{type(err).__name__}: {err}" if err else "???"
# TODO(sergiitk): Remove in py3.11, this will be built-in. See PEP 678.
def add_note(self, note: str):
self.note = note
def __str__(self):
return self.message if not self.note else f"{self.message}\n{self.note}"