Debian Codegraph @

crt/aws-c-io/source/exponential_backoff_retry_strategy.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/io/retry_strategy.h>
6
7 #include <aws/io/event_loop.h>
8 #include <aws/io/logging.h>
9
10 #include <aws/common/clock.h>
11 #include <aws/common/device_random.h>
12 #include <aws/common/mutex.h>
13 #include <aws/common/shutdown_types.h>
14 #include <aws/common/task_scheduler.h>
15
16 #include <inttypes.h>
17
18 struct exponential_backoff_strategy {
19 struct aws_retry_strategy base;
20 struct aws_exponential_backoff_retry_options config;
21 struct aws_shutdown_callback_options shutdown_options;
22 };
23
24 struct exponential_backoff_retry_token {
25 struct aws_retry_token base;
26 struct aws_atomic_var current_retry_count;
27 struct aws_atomic_var last_backoff;
28 size_t max_retries;
29 uint64_t backoff_scale_factor_ns;
30 uint64_t maximum_backoff_ns;
31 enum aws_exponential_backoff_jitter_mode jitter_mode;
32 /* Let's not make this worse by constantly moving across threads if we can help it */
33 struct aws_event_loop *bound_loop;
34 uint64_t (*generate_random)(void);
35 aws_generate_random_fn *generate_random_impl;
36 void *generate_random_user_data;
37 struct aws_task retry_task;
38
39 struct {
40 struct aws_mutex mutex;
41 aws_retry_strategy_on_retry_token_acquired_fn *acquired_fn;
42 aws_retry_strategy_on_retry_ready_fn *retry_ready_fn;
43 void *user_data;
44 } thread_data;
45 };
46
47 static void s_exponential_retry_destroy(struct aws_retry_strategy *retry_strategy) {
48 if (retry_strategy) {
49 struct exponential_backoff_strategy *exponential_strategy = retry_strategy->impl;
50 struct aws_event_loop_group *el_group = exponential_strategy->config.el_group;
51 aws_simple_completion_callback *completion_callback =
52 exponential_strategy->shutdown_options.shutdown_callback_fn;
53 void *completion_user_data = exponential_strategy->shutdown_options.shutdown_callback_user_data;
54
55 aws_mem_release(retry_strategy->allocator, exponential_strategy);
56 if (completion_callback != NULL) {
57 completion_callback(completion_user_data);
58 }
59 aws_event_loop_group_release(el_group);
60 }
61 }
62
63 static void s_exponential_retry_task(struct aws_task *task, void *arg, enum aws_task_status status) {
64 (void)task;
65
66 int error_code = AWS_ERROR_IO_OPERATION_CANCELLED;
67 if (status == AWS_TASK_STATUS_RUN_READY) {
68 error_code = AWS_OP_SUCCESS;
69 }
70
71 struct exponential_backoff_retry_token *backoff_retry_token = arg;
72 aws_retry_strategy_on_retry_token_acquired_fn *acquired_fn = NULL;
73 aws_retry_strategy_on_retry_ready_fn *retry_ready_fn = NULL;
74 void *user_data = NULL;
75
76 { /***** BEGIN CRITICAL SECTION *********/
77 AWS_FATAL_ASSERT(
78 !aws_mutex_lock(&backoff_retry_token->thread_data.mutex) && "Retry token mutex acquisition failed");
79 acquired_fn = backoff_retry_token->thread_data.acquired_fn;
80 retry_ready_fn = backoff_retry_token->thread_data.retry_ready_fn;
81 user_data = backoff_retry_token->thread_data.user_data;
82 backoff_retry_token->thread_data.user_data = NULL;
83 backoff_retry_token->thread_data.retry_ready_fn = NULL;
84 backoff_retry_token->thread_data.acquired_fn = NULL;
85 AWS_FATAL_ASSERT(
86 !aws_mutex_unlock(&backoff_retry_token->thread_data.mutex) && "Retry token mutex release failed");
87 } /**** END CRITICAL SECTION ***********/
88
89 aws_retry_token_acquire(&backoff_retry_token->base);
90 if (acquired_fn) {
91 AWS_LOGF_DEBUG(
92 AWS_LS_IO_EXPONENTIAL_BACKOFF_RETRY_STRATEGY,
93 "id=%p: Vending retry_token %p",
94 (void *)backoff_retry_token->base.retry_strategy,
95 (void *)&backoff_retry_token->base);
96 acquired_fn(backoff_retry_token->base.retry_strategy, error_code, &backoff_retry_token->base, user_data);
97 } else if (retry_ready_fn) {
98 AWS_LOGF_DEBUG(
99 AWS_LS_IO_EXPONENTIAL_BACKOFF_RETRY_STRATEGY,
100 "id=%p: Invoking retry_ready for token %p",
101 (void *)backoff_retry_token->base.retry_strategy,
102 (void *)&backoff_retry_token->base);
103 retry_ready_fn(&backoff_retry_token->base, error_code, user_data);
104 /* it's acquired before being scheduled for retry */
105 aws_retry_token_release(&backoff_retry_token->base);
106 }
107 aws_retry_token_release(&backoff_retry_token->base);
108 }
109
110 static int s_exponential_retry_acquire_token(
111 struct aws_retry_strategy *retry_strategy,
112 const struct aws_byte_cursor *partition_id,
113 aws_retry_strategy_on_retry_token_acquired_fn *on_acquired,
114 void *user_data,
115 uint64_t timeout_ms) {
116 (void)partition_id;
117 /* no resource contention here so no timeouts. */
118 (void)timeout_ms;
119
120 struct exponential_backoff_retry_token *backoff_retry_token =
121 aws_mem_calloc(retry_strategy->allocator, 1, sizeof(struct exponential_backoff_retry_token));
122
123 if (!backoff_retry_token) {
124 return AWS_OP_ERR;
125 }
126
127 AWS_LOGF_DEBUG(
128 AWS_LS_IO_EXPONENTIAL_BACKOFF_RETRY_STRATEGY,
129 "id=%p: Initializing retry token %p",
130 (void *)retry_strategy,
131 (void *)&backoff_retry_token->base);
132
133 backoff_retry_token->base.allocator = retry_strategy->allocator;
134 backoff_retry_token->base.retry_strategy = retry_strategy;
135 aws_atomic_init_int(&backoff_retry_token->base.ref_count, 1u);
136 aws_retry_strategy_acquire(retry_strategy);
137 backoff_retry_token->base.impl = backoff_retry_token;
138
139 struct exponential_backoff_strategy *exponential_backoff_strategy = retry_strategy->impl;
140 backoff_retry_token->bound_loop = aws_event_loop_group_get_next_loop(exponential_backoff_strategy->config.el_group);
141 backoff_retry_token->max_retries = exponential_backoff_strategy->config.max_retries;
142 backoff_retry_token->backoff_scale_factor_ns = aws_timestamp_convert(
143 exponential_backoff_strategy->config.backoff_scale_factor_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
144 backoff_retry_token->maximum_backoff_ns = aws_timestamp_convert(
145 exponential_backoff_strategy->config.max_backoff_secs, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL);
146 backoff_retry_token->jitter_mode = exponential_backoff_strategy->config.jitter_mode;
147 backoff_retry_token->generate_random = exponential_backoff_strategy->config.generate_random;
148 backoff_retry_token->generate_random_impl = exponential_backoff_strategy->config.generate_random_impl;
149 backoff_retry_token->generate_random_user_data = exponential_backoff_strategy->config.generate_random_user_data;
150
151 aws_atomic_init_int(&backoff_retry_token->current_retry_count, 0);
152 aws_atomic_init_int(&backoff_retry_token->last_backoff, 0);
153
154 backoff_retry_token->thread_data.acquired_fn = on_acquired;
155 backoff_retry_token->thread_data.user_data = user_data;
156 AWS_FATAL_ASSERT(
157 !aws_mutex_init(&backoff_retry_token->thread_data.mutex) && "Retry strategy mutex initialization failed");
158
159 aws_task_init(
160 &backoff_retry_token->retry_task,
161 s_exponential_retry_task,
162 backoff_retry_token,
163 "aws_exponential_backoff_retry_task");
164 aws_event_loop_schedule_task_now(backoff_retry_token->bound_loop, &backoff_retry_token->retry_task);
165
166 return AWS_OP_SUCCESS;
167 }
168
169 static inline uint64_t s_random_in_range(uint64_t from, uint64_t to, struct exponential_backoff_retry_token *token) {
170 uint64_t max = aws_max_u64(from, to);
171 uint64_t min = aws_min_u64(from, to);
172
173 uint64_t diff = max - min;
174
175 if (!diff) {
176 return 0;
177 }
178 uint64_t random;
179 if (token->generate_random_impl) {
180 random = token->generate_random_impl(token->generate_random_user_data);
181 } else {
182 random = token->generate_random();
183 }
184 return min + random % (diff);
185 }
186
187 typedef uint64_t(compute_backoff_fn)(struct exponential_backoff_retry_token *token);
188
189 static uint64_t s_compute_no_jitter(struct exponential_backoff_retry_token *token) {
190 uint64_t retry_count = aws_min_u64(aws_atomic_load_int(&token->current_retry_count), 63);
191 uint64_t backoff_ns = aws_mul_u64_saturating((uint64_t)1 << retry_count, token->backoff_scale_factor_ns);
192 return aws_min_u64(backoff_ns, token->maximum_backoff_ns);
193 }
194
195 static uint64_t s_compute_full_jitter(struct exponential_backoff_retry_token *token) {
196 uint64_t non_jittered = s_compute_no_jitter(token);
197 return s_random_in_range(0, non_jittered, token);
198 }
199
200 static uint64_t s_compute_deccorelated_jitter(struct exponential_backoff_retry_token *token) {
201 size_t last_backoff_val = aws_atomic_load_int(&token->last_backoff);
202
203 if (!last_backoff_val) {
204 return s_compute_full_jitter(token);
205 }
206 uint64_t backoff_ns = aws_min_u64(token->maximum_backoff_ns, aws_mul_u64_saturating(last_backoff_val, 3));
207 return s_random_in_range(token->backoff_scale_factor_ns, backoff_ns, token);
208 }
209
210 static compute_backoff_fn *s_backoff_compute_table[] = {
211 [AWS_EXPONENTIAL_BACKOFF_JITTER_DEFAULT] = s_compute_full_jitter,
212 [AWS_EXPONENTIAL_BACKOFF_JITTER_NONE] = s_compute_no_jitter,
213 [AWS_EXPONENTIAL_BACKOFF_JITTER_FULL] = s_compute_full_jitter,
214 [AWS_EXPONENTIAL_BACKOFF_JITTER_DECORRELATED] = s_compute_deccorelated_jitter,
215 };
216
217 static int s_exponential_retry_schedule_retry(
218 struct aws_retry_token *token,
219 enum aws_retry_error_type error_type,
220 aws_retry_strategy_on_retry_ready_fn *retry_ready,
221 void *user_data) {
222 struct exponential_backoff_retry_token *backoff_retry_token = token->impl;
223
224 AWS_LOGF_DEBUG(
225 AWS_LS_IO_EXPONENTIAL_BACKOFF_RETRY_STRATEGY,
226 "id=%p: Attempting retry on token %p with error type %d",
227 (void *)backoff_retry_token->base.retry_strategy,
228 (void *)token,
229 error_type);
230 uint64_t schedule_at = 0;
231
232 /* AWS_RETRY_ERROR_TYPE_CLIENT_ERROR does not count against your retry budget since you were responding to an
233 * improperly crafted request. */
234 if (error_type != AWS_RETRY_ERROR_TYPE_CLIENT_ERROR) {
235 size_t retry_count = aws_atomic_load_int(&backoff_retry_token->current_retry_count);
236
237 if (retry_count >= backoff_retry_token->max_retries) {
238 AWS_LOGF_WARN(
239 AWS_LS_IO_EXPONENTIAL_BACKOFF_RETRY_STRATEGY,
240 "id=%p: token %p has exhausted allowed retries. Retry count %zu max retries %zu",
241 (void *)backoff_retry_token->base.retry_strategy,
242 (void *)token,
243 backoff_retry_token->max_retries,
244 retry_count);
245 return aws_raise_error(AWS_IO_MAX_RETRIES_EXCEEDED);
246 }
247
248 uint64_t backoff = s_backoff_compute_table[backoff_retry_token->jitter_mode](backoff_retry_token);
249 uint64_t current_time = 0;
250
251 aws_event_loop_current_clock_time(backoff_retry_token->bound_loop, &current_time);
252 schedule_at = backoff + current_time;
253 aws_atomic_init_int(&backoff_retry_token->last_backoff, (size_t)backoff);
254 aws_atomic_fetch_add(&backoff_retry_token->current_retry_count, 1u);
255 AWS_LOGF_DEBUG(
256 AWS_LS_IO_EXPONENTIAL_BACKOFF_RETRY_STRATEGY,
257 "id=%p: Computed backoff value of %" PRIu64 "ns on token %p",
258 (void *)backoff_retry_token->base.retry_strategy,
259 backoff,
260 (void *)token);
261 }
262
263 bool already_scheduled = false;
264
265 { /***** BEGIN CRITICAL SECTION *********/
266 AWS_FATAL_ASSERT(
267 !aws_mutex_lock(&backoff_retry_token->thread_data.mutex) && "Retry token mutex acquisition failed");
268
269 if (backoff_retry_token->thread_data.user_data) {
270 already_scheduled = true;
271 } else {
272 backoff_retry_token->thread_data.retry_ready_fn = retry_ready;
273 backoff_retry_token->thread_data.user_data = user_data;
274 /* acquire to hold until the task runs. */
275 aws_retry_token_acquire(token);
276 aws_task_init(
277 &backoff_retry_token->retry_task,
278 s_exponential_retry_task,
279 backoff_retry_token,
280 "aws_exponential_backoff_retry_task");
281 }
282 AWS_FATAL_ASSERT(
283 !aws_mutex_unlock(&backoff_retry_token->thread_data.mutex) && "Retry token mutex release failed");
284 } /**** END CRITICAL SECTION ***********/
285
286 if (already_scheduled) {
287 AWS_LOGF_ERROR(
288 AWS_LS_IO_EXPONENTIAL_BACKOFF_RETRY_STRATEGY,
289 "id=%p: retry token %p is already scheduled.",
290 (void *)backoff_retry_token->base.retry_strategy,
291 (void *)token);
292 return aws_raise_error(AWS_ERROR_INVALID_STATE);
293 }
294
295 aws_event_loop_schedule_task_future(backoff_retry_token->bound_loop, &backoff_retry_token->retry_task, schedule_at);
296 return AWS_OP_SUCCESS;
297 }
298
299 static int s_exponential_backoff_record_success(struct aws_retry_token *token) {
300 /* we don't do book keeping in this mode. */
301 (void)token;
302 return AWS_OP_SUCCESS;
303 }
304
305 static void s_exponential_backoff_release_token(struct aws_retry_token *token) {
306 if (token) {
307 aws_retry_strategy_release(token->retry_strategy);
308 struct exponential_backoff_retry_token *backoff_retry_token = token->impl;
309 aws_mutex_clean_up(&backoff_retry_token->thread_data.mutex);
310 aws_mem_release(token->allocator, backoff_retry_token);
311 }
312 }
313
314 static struct aws_retry_strategy_vtable s_exponential_retry_vtable = {
315 .destroy = s_exponential_retry_destroy,
316 .acquire_token = s_exponential_retry_acquire_token,
317 .schedule_retry = s_exponential_retry_schedule_retry,
318 .record_success = s_exponential_backoff_record_success,
319 .release_token = s_exponential_backoff_release_token,
320 };
321
322 static uint64_t s_default_gen_rand(void *user_data) {
323 (void)user_data;
324 uint64_t res = 0;
325 aws_device_random_u64(&res);
326 return res;
327 }
328
329 struct aws_retry_strategy *aws_retry_strategy_new_exponential_backoff(
330 struct aws_allocator *allocator,
331 const struct aws_exponential_backoff_retry_options *config) {
332 AWS_PRECONDITION(config);
333 AWS_PRECONDITION(config->el_group);
334 AWS_PRECONDITION(config->jitter_mode <= AWS_EXPONENTIAL_BACKOFF_JITTER_DECORRELATED);
335 AWS_PRECONDITION(config->max_retries);
336
337 if (config->max_retries > 63 || !config->el_group ||
338 config->jitter_mode > AWS_EXPONENTIAL_BACKOFF_JITTER_DECORRELATED) {
339 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
340 return NULL;
341 }
342
343 struct exponential_backoff_strategy *exponential_backoff_strategy =
344 aws_mem_calloc(allocator, 1, sizeof(struct exponential_backoff_strategy));
345
346 if (!exponential_backoff_strategy) {
347 return NULL;
348 }
349
350 AWS_LOGF_INFO(
351 AWS_LS_IO_EXPONENTIAL_BACKOFF_RETRY_STRATEGY,
352 "id=%p: Initializing exponential backoff retry strategy with scale factor: %" PRIu32
353 " jitter mode: %d and max retries %zu",
354 (void *)&exponential_backoff_strategy->base,
355 config->backoff_scale_factor_ms,
356 config->jitter_mode,
357 config->max_retries);
358
359 exponential_backoff_strategy->base.allocator = allocator;
360 exponential_backoff_strategy->base.impl = exponential_backoff_strategy;
361 exponential_backoff_strategy->base.vtable = &s_exponential_retry_vtable;
362 aws_atomic_init_int(&exponential_backoff_strategy->base.ref_count, 1);
363 exponential_backoff_strategy->config = *config;
364 exponential_backoff_strategy->config.el_group =
365 aws_event_loop_group_acquire(exponential_backoff_strategy->config.el_group);
366
367 if (!exponential_backoff_strategy->config.generate_random &&
368 !exponential_backoff_strategy->config.generate_random_impl) {
369 exponential_backoff_strategy->config.generate_random_impl = s_default_gen_rand;
370 }
371
372 if (!exponential_backoff_strategy->config.max_retries) {
373 exponential_backoff_strategy->config.max_retries = 5;
374 }
375
376 if (!exponential_backoff_strategy->config.backoff_scale_factor_ms) {
377 exponential_backoff_strategy->config.backoff_scale_factor_ms = 500;
378 }
379
380 if (!exponential_backoff_strategy->config.max_backoff_secs) {
381 exponential_backoff_strategy->config.max_backoff_secs = 20;
382 }
383
384 if (config->shutdown_options) {
385 exponential_backoff_strategy->shutdown_options = *config->shutdown_options;
386 }
387 return &exponential_backoff_strategy->base;
388 }