7 from collections import deque
11 _HTTP_SUCCESSES = set(xrange(200, 300))
12 _HTTP_CAN_RETRY = set([408, 409, 422, 423, 500, 502, 503, 504])
14 class RetryLoop(object):
15 """Coordinate limited retries of code.
17 RetryLoop coordinates a loop that runs until it records a
18 successful result or tries too many times, whichever comes first.
19 Typical use looks like:
21 loop = RetryLoop(num_retries=2)
22 for tries_left in loop:
24 result = do_something()
25 except TemporaryError as error:
26 log("error: {} ({} tries left)".format(error, tries_left))
28 loop.save_result(result)
30 return loop.last_result()
32 def __init__(self, num_retries, success_check=lambda r: True,
33 backoff_start=0, backoff_growth=2, save_results=1):
34 """Construct a new RetryLoop.
37 * num_retries: The maximum number of times to retry the loop if it
38 doesn't succeed. This means the loop could run at most 1+N times.
39 * success_check: This is a function that will be called each
40 time the loop saves a result. The function should return
41 True if the result indicates loop success, False if it
42 represents a permanent failure state, and None if the loop
43 should continue. If no function is provided, the loop will
44 end as soon as it records any result.
45 * backoff_start: The number of seconds that must pass before the
46 loop's second iteration. Default 0, which disables all waiting.
47 * backoff_growth: The wait time multiplier after each iteration.
48 Default 2 (i.e., double the wait time each time).
49 * save_results: Specify a number to save the last N results
50 that the loop recorded. These records are available through
51 the results attribute, oldest first. Default 1.
53 self.tries_left = num_retries + 1
54 self.check_result = success_check
55 self.backoff_wait = backoff_start
56 self.backoff_growth = backoff_growth
57 self.next_start_time = 0
58 self.results = deque(maxlen=save_results)
66 return self._running and (self._success is None)
69 if self._running is None:
71 if (self.tries_left < 1) or not self.running():
75 wait_time = max(0, self.next_start_time - time.time())
77 self.backoff_wait *= self.backoff_growth
78 self.next_start_time = time.time() + self.backoff_wait
80 return self.tries_left
82 def save_result(self, result):
83 """Record a loop result.
85 Save the given result, and end the loop if it indicates
86 success or permanent failure. See __init__'s documentation
87 about success_check to learn how to make that indication.
89 if not self.running():
90 raise arvados.errors.AssertionError(
91 "recorded a loop result after the loop finished")
92 self.results.append(result)
93 self._success = self.check_result(result)
96 """Return the loop's end state.
98 Returns True if the loop obtained a successful result, False if it
99 encountered permanent failure, or else None.
103 def last_result(self):
104 """Return the most recent result the loop recorded."""
106 return self.results[-1]
108 raise arvados.errors.AssertionError(
109 "queried loop results before any were recorded")
112 def check_http_response_success(result):
113 """Convert an httplib2 request result to a loop control flag.
115 Pass this method the 2-tuple returned by httplib2.Http.request. It
116 returns True if the response indicates success, None if it indicates
117 temporary failure, and False otherwise. You can use this as the
118 success_check for a RetryLoop.
120 Implementation details:
121 * Any 2xx result returns True.
122 * A select few status codes, or any malformed responses, return None.
123 422 Unprocessable Entity is in this category. This may not meet the
124 letter of the HTTP specification, but the Arvados API server will
125 use it for various server-side problems like database connection
127 * Everything else returns False. Note that this includes 1xx and
128 3xx status codes. They don't indicate success, and you can't
129 retry those requests verbatim.
132 status = int(result[0].status)
135 if status in _HTTP_SUCCESSES:
137 elif status in _HTTP_CAN_RETRY:
139 elif 100 <= status < 600:
142 return None # Get well soon, server.
144 def retry_method(orig_func):
145 """Provide a default value for a method's num_retries argument.
147 This is a decorator for instance and class methods that accept a
148 num_retries argument, with a None default. When the method is called
149 without a value for num_retries, it will be set from the underlying
150 instance or class' num_retries attribute.
152 @functools.wraps(orig_func)
153 def num_retries_setter(self, *args, **kwargs):
154 arg_vals = inspect.getcallargs(orig_func, self, *args, **kwargs)
155 if arg_vals['num_retries'] is None:
156 kwargs['num_retries'] = self.num_retries
157 return orig_func(self, *args, **kwargs)
158 return num_retries_setter