Debian Codegraph @

crt/aws-c-io/source/standard_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
6 #include <aws/io/logging.h>
7 #include <aws/io/retry_strategy.h>
8
9 #include <aws/common/byte_buf.h>
10 #include <aws/common/hash_table.h>
11 #include <aws/common/mutex.h>
12 #include <aws/common/string.h>
13
14 #include <inttypes.h>
15
16 AWS_STRING_FROM_LITERAL(s_empty_string, "");
17 static struct aws_byte_cursor s_empty_string_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("");
18 static const size_t s_initial_retry_bucket_capacity = 500u;
19 static const size_t s_standard_retry_cost = 5u;
20 static const size_t s_standard_transient_cost = 10u;
21 static const size_t s_standard_no_retry_cost = 1u;
22
23 struct retry_bucket {
24 struct aws_allocator *allocator;
25 struct aws_retry_strategy *owner;
26 struct aws_string *partition_id;
27 struct aws_byte_cursor partition_id_cur;
28 struct {
29 size_t current_capacity;
30 struct aws_mutex partition_lock;
31 } synced_data;
32 };
33
34 struct retry_bucket_token {
35 struct aws_retry_token retry_token;
36 struct retry_bucket *strategy_bucket;
37 struct aws_retry_token *exp_backoff_token;
38 aws_retry_strategy_on_retry_token_acquired_fn *original_on_acquired;
39 aws_retry_strategy_on_retry_ready_fn *original_on_ready;
40 size_t last_retry_cost;
41 void *original_user_data;
42 };
43
44 static bool s_partition_id_equals_byte_cur(const void *seated_cur, const void *cur_ptr) {
45 return aws_byte_cursor_eq_ignore_case(seated_cur, cur_ptr);
46 }
47
48 static uint64_t s_hash_partition_id(const void *seated_partition_ptr) {
49 return aws_hash_byte_cursor_ptr_ignore_case(seated_partition_ptr);
50 }
51
52 static void s_destroy_standard_retry_bucket(void *retry_bucket) {
53 struct retry_bucket *standard_retry_bucket = retry_bucket;
54 AWS_LOGF_TRACE(
55 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
56 "id=%p: destroying bucket partition " PRInSTR,
57 (void *)standard_retry_bucket->owner,
58 AWS_BYTE_CURSOR_PRI(standard_retry_bucket->partition_id_cur));
59 aws_string_destroy(standard_retry_bucket->partition_id);
60 aws_mutex_clean_up(&standard_retry_bucket->synced_data.partition_lock);
61 aws_mem_release(standard_retry_bucket->allocator, standard_retry_bucket);
62 }
63
64 struct standard_strategy {
65 struct aws_retry_strategy base;
66 struct aws_retry_strategy *exponential_backoff_retry_strategy;
67 size_t max_capacity;
68 struct {
69 struct aws_hash_table token_buckets;
70 struct aws_mutex lock;
71 } synced_data;
72 };
73
74 static void s_standard_retry_destroy(struct aws_retry_strategy *retry_strategy) {
75 AWS_LOGF_TRACE(AWS_LS_IO_STANDARD_RETRY_STRATEGY, "id=%p: destroying self", (void *)retry_strategy);
76 struct standard_strategy *standard_strategy = retry_strategy->impl;
77 aws_retry_strategy_release(standard_strategy->exponential_backoff_retry_strategy);
78 aws_hash_table_clean_up(&standard_strategy->synced_data.token_buckets);
79 aws_mutex_clean_up(&standard_strategy->synced_data.lock);
80 aws_mem_release(retry_strategy->allocator, standard_strategy);
81 }
82
83 static void s_on_standard_retry_token_acquired(
84 struct aws_retry_strategy *retry_strategy,
85 int error_code,
86 struct aws_retry_token *token,
87 void *user_data) {
88 (void)retry_strategy;
89 (void)token;
90
91 struct retry_bucket_token *retry_token = user_data;
92 AWS_LOGF_DEBUG(
93 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
94 "id=%p: token acquired callback invoked with error %s with token %p and nested token %p",
95 (void *)retry_token->retry_token.retry_strategy,
96 aws_error_str(error_code),
97 (void *)&retry_token->retry_token,
98 (void *)token);
99
100 AWS_LOGF_TRACE(
101 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
102 "id=%p: invoking on_retry_token_acquired callback",
103 (void *)retry_token->retry_token.retry_strategy);
104
105 aws_retry_token_acquire(&retry_token->retry_token);
106 if (!error_code) {
107 retry_token->exp_backoff_token = token;
108
109 retry_token->original_on_acquired(
110 retry_token->strategy_bucket->owner,
111 error_code,
112 &retry_token->retry_token,
113 retry_token->original_user_data);
114 AWS_LOGF_TRACE(
115 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
116 "id=%p: on_retry_token_acquired callback completed",
117 (void *)retry_token->retry_token.retry_strategy);
118
119 } else {
120 retry_token->original_on_acquired(
121 retry_token->strategy_bucket->owner, error_code, NULL, retry_token->original_user_data);
122 AWS_LOGF_TRACE(
123 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
124 "id=%p: on_retry_token_acquired callback completed",
125 (void *)retry_token->retry_token.retry_strategy);
126 }
127 aws_retry_token_release(&retry_token->retry_token);
128 }
129
130 static int s_standard_retry_acquire_token(
131 struct aws_retry_strategy *retry_strategy,
132 const struct aws_byte_cursor *partition_id,
133 aws_retry_strategy_on_retry_token_acquired_fn *on_acquired,
134 void *user_data,
135 uint64_t timeout_ms) {
136 struct standard_strategy *standard_strategy = retry_strategy->impl;
137 bool bucket_needs_cleanup = false;
138
139 const struct aws_byte_cursor *partition_id_ptr =
140 !partition_id || partition_id->len == 0 ? &s_empty_string_cur : partition_id;
141
142 AWS_LOGF_DEBUG(
143 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
144 "id=%p: attempting to acquire retry token for partition_id " PRInSTR,
145 (void *)retry_strategy,
146 AWS_BYTE_CURSOR_PRI(*partition_id_ptr));
147
148 struct retry_bucket_token *token = aws_mem_calloc(retry_strategy->allocator, 1, sizeof(struct retry_bucket_token));
149 if (!token) {
150 return AWS_OP_ERR;
151 }
152
153 token->original_user_data = user_data;
154 token->original_on_acquired = on_acquired;
155
156 struct aws_hash_element *element_ptr;
157 struct retry_bucket *bucket_ptr;
158 AWS_FATAL_ASSERT(!aws_mutex_lock(&standard_strategy->synced_data.lock) && "Lock acquisition failed.");
159 aws_hash_table_find(&standard_strategy->synced_data.token_buckets, partition_id_ptr, &element_ptr);
160 if (!element_ptr) {
161 AWS_LOGF_DEBUG(
162 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
163 "id=%p: bucket for partition_id " PRInSTR " does not exist, attempting to create one",
164 (void *)retry_strategy,
165 AWS_BYTE_CURSOR_PRI(*partition_id_ptr));
166 bucket_ptr = aws_mem_calloc(standard_strategy->base.allocator, 1, sizeof(struct retry_bucket));
167
168 if (!bucket_ptr) {
169 AWS_LOGF_ERROR(
170 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
171 "id=%p: error when allocating bucket %s",
172 (void *)retry_strategy,
173 aws_error_debug_str(aws_last_error()));
174 goto table_locked;
175 }
176
177 bucket_needs_cleanup = true;
178 bucket_ptr->allocator = standard_strategy->base.allocator;
179 bucket_ptr->partition_id = partition_id_ptr->len > 0
180 ? aws_string_new_from_cursor(standard_strategy->base.allocator, partition_id)
181 : (struct aws_string *)s_empty_string;
182
183 if (!bucket_ptr->partition_id) {
184 AWS_LOGF_ERROR(
185 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
186 "id=%p: error when allocating partition_id %s",
187 (void *)retry_strategy,
188 aws_error_debug_str(aws_last_error()));
189 goto table_locked;
190 }
191
192 bucket_ptr->partition_id_cur = aws_byte_cursor_from_string(bucket_ptr->partition_id);
193 AWS_FATAL_ASSERT(!aws_mutex_init(&bucket_ptr->synced_data.partition_lock) && "mutex init failed!");
194 bucket_ptr->owner = retry_strategy;
195 bucket_ptr->synced_data.current_capacity = standard_strategy->max_capacity;
196 AWS_LOGF_DEBUG(
197 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
198 "id=%p: bucket %p for partition_id " PRInSTR " created",
199 (void *)retry_strategy,
200 (void *)bucket_ptr,
201 AWS_BYTE_CURSOR_PRI(*partition_id_ptr));
202
203 if (aws_hash_table_put(
204 &standard_strategy->synced_data.token_buckets, &bucket_ptr->partition_id_cur, bucket_ptr, NULL)) {
205 AWS_LOGF_ERROR(
206 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
207 "id=%p: error when putting bucket to token_bucket table %s",
208 (void *)retry_strategy,
209 aws_error_debug_str(aws_last_error()));
210 goto table_locked;
211 }
212 bucket_needs_cleanup = false;
213 } else {
214 bucket_ptr = element_ptr->value;
215 AWS_LOGF_DEBUG(
216 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
217 "id=%p: bucket %p for partition_id " PRInSTR " found",
218 (void *)retry_strategy,
219 (void *)bucket_ptr,
220 AWS_BYTE_CURSOR_PRI(*partition_id_ptr));
221 }
222 AWS_FATAL_ASSERT(!aws_mutex_unlock(&standard_strategy->synced_data.lock) && "Mutex unlock failed");
223
224 token->strategy_bucket = bucket_ptr;
225 token->retry_token.retry_strategy = retry_strategy;
226 aws_atomic_init_int(&token->retry_token.ref_count, 1u);
227 aws_retry_strategy_acquire(retry_strategy);
228 token->retry_token.allocator = retry_strategy->allocator;
229 token->retry_token.impl = token;
230
231 /* don't decrement the capacity counter, but add the retry payback, so making calls that succeed allows for a
232 * gradual recovery of the bucket capacity. Otherwise, we'd never recover from an outage. */
233 token->last_retry_cost = s_standard_no_retry_cost;
234
235 AWS_LOGF_TRACE(
236 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
237 "id=%p: allocated token %p for partition_id " PRInSTR,
238 (void *)retry_strategy,
239 (void *)&token->retry_token,
240 AWS_BYTE_CURSOR_PRI(*partition_id_ptr));
241
242 if (aws_retry_strategy_acquire_retry_token(
243 standard_strategy->exponential_backoff_retry_strategy,
244 partition_id_ptr,
245 s_on_standard_retry_token_acquired,
246 token,
247 timeout_ms)) {
248 AWS_LOGF_ERROR(
249 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
250 "id=%p: error when acquiring retry token from backing retry strategy %p: %s",
251 (void *)retry_strategy,
252 (void *)standard_strategy->exponential_backoff_retry_strategy,
253 aws_error_debug_str(aws_last_error()));
254 goto table_updated;
255 }
256
257 return AWS_OP_SUCCESS;
258
259 table_updated:
260 AWS_FATAL_ASSERT(!aws_mutex_lock(&standard_strategy->synced_data.lock) && "Mutex lock failed");
261 aws_hash_table_remove(&standard_strategy->synced_data.token_buckets, &bucket_ptr->partition_id_cur, NULL, NULL);
262 bucket_needs_cleanup = false;
263
264 table_locked:
265 AWS_FATAL_ASSERT(!aws_mutex_unlock(&standard_strategy->synced_data.lock) && "Mutex unlock failed");
266
267 if (bucket_needs_cleanup) {
268 s_destroy_standard_retry_bucket(bucket_ptr);
269 }
270
271 aws_retry_token_release(&token->retry_token);
272
273 return AWS_OP_ERR;
274 }
275
276 void s_standard_retry_strategy_on_retry_ready(struct aws_retry_token *token, int error_code, void *user_data) {
277 (void)token;
278
279 struct aws_retry_token *standard_retry_token = user_data;
280 struct retry_bucket_token *impl = standard_retry_token->impl;
281 AWS_LOGF_TRACE(
282 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
283 "id=%p: invoking on_retry_ready callback with error %s, token %p, and nested token %p",
284 (void *)token->retry_strategy,
285 aws_error_str(error_code),
286 (void *)standard_retry_token,
287 (void *)token);
288 struct aws_retry_strategy *retry_strategy = token->retry_strategy;
289 /* we already hold a reference count here due to the previous acquire before scheduling, so don't worry
290 * about incrementing standard_retry_token here */
291 impl->original_on_ready(standard_retry_token, error_code, impl->original_user_data);
292 AWS_LOGF_TRACE(
293 AWS_LS_IO_STANDARD_RETRY_STRATEGY, "id=%p: on_retry_ready callback completed", (void *)retry_strategy);
294 /* this is to release the acquire we did before scheduling the retry. Release it now. */
295 aws_retry_token_release(standard_retry_token);
296 }
297
298 static int s_standard_retry_strategy_schedule_retry(
299 struct aws_retry_token *token,
300 enum aws_retry_error_type error_type,
301 aws_retry_strategy_on_retry_ready_fn *retry_ready,
302 void *user_data) {
303
304 if (error_type == AWS_RETRY_ERROR_TYPE_CLIENT_ERROR) {
305 return aws_raise_error(AWS_IO_RETRY_PERMISSION_DENIED);
306 }
307
308 struct retry_bucket_token *impl = token->impl;
309
310 size_t capacity_consumed = 0;
311
312 AWS_FATAL_ASSERT(!aws_mutex_lock(&impl->strategy_bucket->synced_data.partition_lock) && "mutex lock failed");
313 size_t current_capacity = impl->strategy_bucket->synced_data.current_capacity;
314 if (current_capacity == 0) {
315 AWS_FATAL_ASSERT(
316 !aws_mutex_unlock(&impl->strategy_bucket->synced_data.partition_lock) && "mutex unlock failed");
317 AWS_LOGF_INFO(
318 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
319 "token_id=%p: requested to schedule retry but the bucket capacity is empty. Rejecting retry request.",
320 (void *)token);
321 return aws_raise_error(AWS_IO_RETRY_PERMISSION_DENIED);
322 }
323
324 if (error_type == AWS_RETRY_ERROR_TYPE_TRANSIENT) {
325 capacity_consumed = aws_min_size(current_capacity, s_standard_transient_cost);
326 } else {
327 /* you may be looking for throttling, but if that happened, the service told us to slow down,
328 * but is otherwise healthy. Pay a smaller penalty for those. */
329 capacity_consumed = aws_min_size(current_capacity, s_standard_retry_cost);
330 }
331
332 AWS_LOGF_DEBUG(
333 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
334 "token_id=%p: reducing retry capacity by %zu from %zu and scheduling retry.",
335 (void *)token,
336 capacity_consumed,
337 current_capacity);
338 impl->original_user_data = user_data;
339 impl->original_on_ready = retry_ready;
340
341 size_t previous_cost = impl->last_retry_cost;
342 impl->last_retry_cost = capacity_consumed;
343 impl->strategy_bucket->synced_data.current_capacity -= capacity_consumed;
344 AWS_FATAL_ASSERT(!aws_mutex_unlock(&impl->strategy_bucket->synced_data.partition_lock) && "mutex unlock failed");
345
346 /* acquire before scheduling to prevent clean up before the callback runs. */
347 aws_retry_token_acquire(&impl->retry_token);
348 if (aws_retry_strategy_schedule_retry(
349 impl->exp_backoff_token, error_type, s_standard_retry_strategy_on_retry_ready, token)) {
350 /* release for the above acquire */
351 aws_retry_token_release(&impl->retry_token);
352 AWS_LOGF_ERROR(
353 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
354 "token_id=%p: error occurred while scheduling retry: %s.",
355 (void *)token,
356 aws_error_debug_str(aws_last_error()));
357 /* roll it back. */
358 AWS_FATAL_ASSERT(!aws_mutex_lock(&impl->strategy_bucket->synced_data.partition_lock) && "mutex lock failed");
359 impl->last_retry_cost = previous_cost;
360 size_t desired_capacity = impl->strategy_bucket->synced_data.current_capacity + capacity_consumed;
361 struct standard_strategy *strategy_impl = token->retry_strategy->impl;
362 impl->strategy_bucket->synced_data.current_capacity =
363 desired_capacity < strategy_impl->max_capacity ? desired_capacity : strategy_impl->max_capacity;
364 AWS_FATAL_ASSERT(
365 !aws_mutex_unlock(&impl->strategy_bucket->synced_data.partition_lock) && "mutex unlock failed");
366
367 return AWS_OP_ERR;
368 }
369
370 return AWS_OP_SUCCESS;
371 }
372
373 static int s_standard_retry_strategy_record_success(struct aws_retry_token *token) {
374 struct retry_bucket_token *impl = token->impl;
375
376 AWS_FATAL_ASSERT(!aws_mutex_lock(&impl->strategy_bucket->synced_data.partition_lock) && "mutex lock failed");
377 AWS_LOGF_DEBUG(
378 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
379 "token_id=%p: partition=" PRInSTR
380 ": recording successful operation and adding %zu units of capacity back to the bucket.",
381 (void *)token,
382 AWS_BYTE_CURSOR_PRI(impl->strategy_bucket->partition_id_cur),
383 impl->last_retry_cost);
384 size_t capacity_payback = impl->strategy_bucket->synced_data.current_capacity + impl->last_retry_cost;
385 struct standard_strategy *standard_strategy = token->retry_strategy->impl;
386 impl->strategy_bucket->synced_data.current_capacity =
387 capacity_payback < standard_strategy->max_capacity ? capacity_payback : standard_strategy->max_capacity;
388 impl->last_retry_cost = 0;
389 AWS_LOGF_TRACE(
390 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
391 "bucket_id=%p: partition=" PRInSTR " : new capacity is %zu.",
392 (void *)token,
393 AWS_BYTE_CURSOR_PRI(impl->strategy_bucket->partition_id_cur),
394 impl->strategy_bucket->synced_data.current_capacity);
395 AWS_FATAL_ASSERT(!aws_mutex_unlock(&impl->strategy_bucket->synced_data.partition_lock) && "mutex unlock failed");
396 return AWS_OP_SUCCESS;
397 }
398
399 static void s_standard_retry_strategy_release_token(struct aws_retry_token *token) {
400 if (token) {
401 AWS_LOGF_TRACE(AWS_LS_IO_STANDARD_RETRY_STRATEGY, "id=%p: releasing token", (void *)token);
402 struct retry_bucket_token *impl = token->impl;
403 aws_retry_token_release(impl->exp_backoff_token);
404 aws_retry_strategy_release(token->retry_strategy);
405 aws_mem_release(token->allocator, impl);
406 }
407 }
408
409 static struct aws_retry_strategy_vtable s_standard_retry_vtable = {
410 .schedule_retry = s_standard_retry_strategy_schedule_retry,
411 .acquire_token = s_standard_retry_acquire_token,
412 .release_token = s_standard_retry_strategy_release_token,
413 .destroy = s_standard_retry_destroy,
414 .record_success = s_standard_retry_strategy_record_success,
415 };
416
417 struct aws_retry_strategy *aws_retry_strategy_new_standard(
418 struct aws_allocator *allocator,
419 const struct aws_standard_retry_options *config) {
420 AWS_PRECONDITION(allocator);
421 AWS_PRECONDITION(config);
422
423 AWS_LOGF_INFO(AWS_LS_IO_STANDARD_RETRY_STRATEGY, "static: creating new standard retry strategy");
424 struct standard_strategy *standard_strategy = aws_mem_calloc(allocator, 1, sizeof(struct standard_strategy));
425
426 if (!standard_strategy) {
427 AWS_LOGF_ERROR(AWS_LS_IO_STANDARD_RETRY_STRATEGY, "static: allocation of new standard retry strategy failed");
428 return NULL;
429 }
430
431 aws_atomic_init_int(&standard_strategy->base.ref_count, 1);
432
433 struct aws_exponential_backoff_retry_options config_cpy = config->backoff_retry_options;
434
435 /* standard default is 3. */
436 if (!config->backoff_retry_options.max_retries) {
437 config_cpy.max_retries = 3;
438 }
439
440 AWS_LOGF_INFO(
441 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
442 "id=%p: creating backing exponential backoff strategy with max_retries of %zu",
443 (void *)&standard_strategy->base,
444 config_cpy.max_retries);
445
446 standard_strategy->exponential_backoff_retry_strategy =
447 aws_retry_strategy_new_exponential_backoff(allocator, &config_cpy);
448
449 if (!standard_strategy->exponential_backoff_retry_strategy) {
450 AWS_LOGF_ERROR(
451 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
452 "id=%p: allocation of new exponential backoff retry strategy failed: %s",
453 (void *)&standard_strategy->base,
454 aws_error_debug_str(aws_last_error()));
455 goto error;
456 }
457
458 if (aws_hash_table_init(
459 &standard_strategy->synced_data.token_buckets,
460 allocator,
461 16u,
462 s_hash_partition_id,
463 s_partition_id_equals_byte_cur,
464 NULL,
465 s_destroy_standard_retry_bucket)) {
466 AWS_LOGF_ERROR(
467 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
468 "id=%p: token bucket table creation failed: %s",
469 (void *)&standard_strategy->base,
470 aws_error_debug_str(aws_last_error()));
471 goto error;
472 }
473
474 standard_strategy->max_capacity =
475 config->initial_bucket_capacity ? config->initial_bucket_capacity : s_initial_retry_bucket_capacity;
476
477 AWS_LOGF_DEBUG(
478 AWS_LS_IO_STANDARD_RETRY_STRATEGY,
479 "id=%p: maximum bucket capacity set to %zu",
480 (void *)&standard_strategy->base,
481 standard_strategy->max_capacity);
482 AWS_FATAL_ASSERT(!aws_mutex_init(&standard_strategy->synced_data.lock) && "mutex init failed");
483
484 standard_strategy->base.allocator = allocator;
485 standard_strategy->base.vtable = &s_standard_retry_vtable;
486 standard_strategy->base.impl = standard_strategy;
487 return &standard_strategy->base;
488
489 error:
490 if (standard_strategy->exponential_backoff_retry_strategy) {
491 aws_retry_strategy_release(standard_strategy->exponential_backoff_retry_strategy);
492 }
493
494 aws_mem_release(allocator, standard_strategy);
495
496 return NULL;
497 }