Debian Codegraph @

crt/aws-c-s3/tests/s3_asyncwrite_tests.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 "s3_tester.h"
6
7 #include <aws/checksums/crc.h>
8 #include <aws/common/atomics.h>
9 #include <aws/common/byte_order.h>
10 #include <aws/common/clock.h>
11 #include <aws/common/device_random.h>
12 #include <aws/common/encoding.h>
13 #include <aws/io/event_loop.h>
14 #include <aws/s3/private/s3_default_buffer_pool.h>
15 #include <aws/s3/private/s3_util.h>
16 #include <aws/testing/aws_test_harness.h>
17
18 #define TIMEOUT_NANOS ((uint64_t)AWS_TIMESTAMP_NANOS * 10) /* 10secs */
19 #define PART_SIZE MB_TO_BYTES(5)
20
21 struct asyncwrite_tester {
22 struct aws_allocator *allocator;
23 struct aws_s3_tester s3_tester;
24 struct aws_s3_client *client;
25 struct aws_s3_meta_request *meta_request;
26 struct aws_s3_meta_request_test_results test_results;
27 struct aws_byte_buf source_buf;
28 };
29
30 /* Optional knobs for s_asyncwrite_tester_init_ex(). Zero-initialized means "defaults". */
31 struct asyncwrite_tester_options {
32 /* If set, the client uses this custom buffer pool factory (e.g. to exercise deferred
33 * ticket reservations in the async-write path). NULL uses the default pool. */
34 aws_s3_buffer_pool_factory_fn *buffer_pool_factory_fn;
35 void *buffer_pool_user_data;
36 };
37
38 static int s_asyncwrite_tester_init_ex(
39 struct asyncwrite_tester *tester,
40 struct aws_allocator *allocator,
41 size_t object_size,
42 const struct asyncwrite_tester_options *options) {
43
44 struct asyncwrite_tester_options local_options;
45 AWS_ZERO_STRUCT(local_options);
46 if (options != NULL) {
47 local_options = *options;
48 }
49
50 AWS_ZERO_STRUCT(*tester);
51 tester->allocator = allocator;
52
53 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester->s3_tester));
54
55 /* Create S3 client */
56 struct aws_s3_client_config client_config = {
57 .part_size = PART_SIZE,
58 .buffer_pool_factory_fn = local_options.buffer_pool_factory_fn,
59 .buffer_pool_user_data = local_options.buffer_pool_user_data,
60 };
61 ASSERT_SUCCESS(aws_s3_tester_bind_client(
62 &tester->s3_tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
63 tester->client = aws_s3_client_new(allocator, &client_config);
64 ASSERT_NOT_NULL(tester->client);
65
66 /* Create buffer of data to upload */
67 aws_byte_buf_init(&tester->source_buf, allocator, object_size);
68 ASSERT_SUCCESS(aws_device_random_buffer(&tester->source_buf));
69
70 /* Create meta request */
71 aws_s3_meta_request_test_results_init(&tester->test_results, allocator);
72
73 struct aws_string *host_name =
74 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
75 struct aws_byte_cursor host_name_cursor = aws_byte_cursor_from_string(host_name);
76 struct aws_byte_buf object_path;
77 ASSERT_SUCCESS(
78 aws_s3_tester_upload_file_path_init(allocator, &object_path, aws_byte_cursor_from_c_str("/asyncwrite.bin")));
79
80 struct aws_http_message *message = aws_s3_test_put_object_request_new_without_body(
81 allocator,
82 &host_name_cursor,
83 g_test_body_content_type,
84 aws_byte_cursor_from_buf(&object_path),
85 object_size,
86 0 /*flags*/);
87
88 /* erase content-length header, because async-write doesn't currently support it */
89 aws_http_headers_erase(aws_http_message_get_headers(message), g_content_length_header_name);
90
91 struct aws_s3_checksum_config checksum_config = {
92 .checksum_algorithm = AWS_SCA_CRC32,
93 .location = AWS_SCL_TRAILER,
94 };
95
96 struct aws_s3_meta_request_options meta_request_options = {
97 .type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
98 .message = message,
99 .send_using_async_writes = true,
100 .checksum_config = &checksum_config,
101 };
102 ASSERT_SUCCESS(aws_s3_tester_bind_meta_request(&tester->s3_tester, &meta_request_options, &tester->test_results));
103
104 tester->meta_request = aws_s3_client_make_meta_request(tester->client, &meta_request_options);
105 ASSERT_NOT_NULL(tester->meta_request);
106
107 /* Clean up tmp variables */
108 aws_string_destroy(host_name);
109 aws_byte_buf_clean_up(&object_path);
110 aws_http_message_release(message);
111 return 0;
112 }
113
114 static int s_asyncwrite_tester_init(
115 struct asyncwrite_tester *tester,
116 struct aws_allocator *allocator,
117 size_t object_size) {
118 return s_asyncwrite_tester_init_ex(tester, allocator, object_size, NULL /*options*/);
119 }
120
121 static int s_asyncwrite_tester_validate(struct asyncwrite_tester *tester) {
122 ASSERT_SUCCESS(aws_s3_tester_validate_put_object_results(&tester->test_results, 0 /*flags*/));
123
124 /* Validate the checksums, to be we uploaded what we meant to upload */
125 ASSERT_TRUE(tester->test_results.upload_review.part_count > 0, "Update this code to handle whole-object checksum");
126 struct aws_byte_cursor source_cursor = aws_byte_cursor_from_buf(&tester->source_buf);
127 for (size_t part_i = 0; part_i < tester->test_results.upload_review.part_count; ++part_i) {
128 /* calculate checksum of this part, from source_buffer */
129 uint64_t part_size = tester->test_results.upload_review.part_sizes_array[part_i];
130 ASSERT_TRUE(part_size <= source_cursor.len);
131 ASSERT_TRUE(part_size < INT_MAX);
132 uint32_t crc32_val = aws_checksums_crc32(source_cursor.ptr, (int)part_size, 0x0 /*previousCrc32*/);
133 aws_byte_cursor_advance(&source_cursor, (size_t)part_size);
134
135 /* base64-encode the big-endian representation of the CRC32 */
136 uint32_t crc32_be_val = aws_hton32(crc32_val);
137 struct aws_byte_cursor crc32_be_cursor = {.ptr = (uint8_t *)&crc32_be_val, .len = sizeof(crc32_be_val)};
138 struct aws_byte_buf crc32_base64_buf;
139 aws_byte_buf_init(&crc32_base64_buf, tester->allocator, 16);
140 ASSERT_SUCCESS(aws_base64_encode(&crc32_be_cursor, &crc32_base64_buf));
141
142 /* compare to what got sent */
143 struct aws_string *sent_checksum = tester->test_results.upload_review.part_checksums_array[part_i];
144 ASSERT_BIN_ARRAYS_EQUALS(
145 crc32_base64_buf.buffer, crc32_base64_buf.len, sent_checksum->bytes, sent_checksum->len);
146
147 aws_byte_buf_clean_up(&crc32_base64_buf);
148 }
149 return 0;
150 }
151
152 static int s_asyncwrite_tester_clean_up(struct asyncwrite_tester *tester) {
153 tester->meta_request = aws_s3_meta_request_release(tester->meta_request);
154 aws_s3_tester_wait_for_meta_request_shutdown(&tester->s3_tester);
155 aws_s3_meta_request_test_results_clean_up(&tester->test_results);
156 aws_byte_buf_clean_up(&tester->source_buf);
157 tester->client = aws_s3_client_release(tester->client);
158 aws_s3_tester_clean_up(&tester->s3_tester);
159 return 0;
160 }
161
162 static int s_write(struct asyncwrite_tester *tester, struct aws_byte_cursor data, bool eof) {
163 /* use freshly allocated buffer for each write, so that we're likely to get memory violations
164 * if this data is used wrong internally. */
165 struct aws_byte_buf write_buf;
166 aws_byte_buf_init_cache_and_update_cursors(&write_buf, tester->allocator, &data, NULL);
167
168 struct aws_future_void *write_future = aws_s3_meta_request_write(tester->meta_request, data, eof);
169 ASSERT_NOT_NULL(write_future);
170 ASSERT_TRUE(aws_future_void_wait(write_future, TIMEOUT_NANOS));
171 aws_byte_buf_clean_up(&write_buf);
172 ASSERT_INT_EQUALS(0, aws_future_void_get_error(write_future));
173 aws_future_void_release(write_future);
174 return 0;
175 }
176
177 struct basic_asyncwrite_options {
178 /* Total size of object to upload */
179 size_t object_size;
180 /* Max bytes per write(). If zero, defaults to object_size */
181 size_t max_bytes_per_write;
182 /* If true, EOF is passed in a separate final empty write() */
183 bool eof_requires_extra_write;
184 };
185
186 /* Common function for tests that do successful uploads, without too much weird stuff */
187 static int s_basic_asyncwrite(
188 struct aws_allocator *allocator,
189 void *ctx,
190 const struct basic_asyncwrite_options *options) {
191
192 (void)ctx;
193 struct asyncwrite_tester tester;
194 ASSERT_SUCCESS(s_asyncwrite_tester_init(&tester, allocator, options->object_size));
195
196 size_t max_bytes_per_write = options->max_bytes_per_write > 0 ? options->max_bytes_per_write : options->object_size;
197 bool eof = false;
198 struct aws_byte_cursor source_cursor = aws_byte_cursor_from_buf(&tester.source_buf);
199 while (source_cursor.len > 0) {
200 size_t bytes_to_write = aws_min_size(max_bytes_per_write, source_cursor.len);
201 struct aws_byte_cursor write_cursor = aws_byte_cursor_advance(&source_cursor, bytes_to_write);
202 if (source_cursor.len == 0 && !options->eof_requires_extra_write) {
203 eof = true;
204 }
205
206 ASSERT_SUCCESS(s_write(&tester, write_cursor, eof));
207 }
208
209 /* Ensure EOF is sent (eof_requires_extra_write, or object_size==0) */
210 if (!eof) {
211 ASSERT_SUCCESS(s_write(&tester, (struct aws_byte_cursor){0}, true /*eof*/));
212 }
213
214 /* Done */
215 aws_s3_tester_wait_for_meta_request_finish(&tester.s3_tester);
216 ASSERT_SUCCESS(s_asyncwrite_tester_validate(&tester));
217 ASSERT_SUCCESS(s_asyncwrite_tester_clean_up(&tester));
218 return 0;
219 };
220
221 AWS_TEST_CASE(test_s3_asyncwrite_empty_file, s_test_s3_asyncwrite_empty_file)
222 static int s_test_s3_asyncwrite_empty_file(struct aws_allocator *allocator, void *ctx) {
223 struct basic_asyncwrite_options options = {
224 .object_size = 0,
225 };
226 return s_basic_asyncwrite(allocator, ctx, &options);
227 }
228
229 AWS_TEST_CASE(test_s3_asyncwrite_small_file_1_write, s_test_s3_asyncwrite_small_file_1_write)
230 static int s_test_s3_asyncwrite_small_file_1_write(struct aws_allocator *allocator, void *ctx) {
231 struct basic_asyncwrite_options options = {
232 .object_size = 100,
233 };
234 return s_basic_asyncwrite(allocator, ctx, &options);
235 }
236
237 /* In this test, the 1st write must be buffered, since it's less than part-size */
238 AWS_TEST_CASE(test_s3_asyncwrite_small_file_1_write_then_eof, s_test_s3_asyncwrite_small_file_1_write_then_eof)
239 static int s_test_s3_asyncwrite_small_file_1_write_then_eof(struct aws_allocator *allocator, void *ctx) {
240 struct basic_asyncwrite_options options = {
241 .object_size = 100,
242 .eof_requires_extra_write = true,
243 };
244 return s_basic_asyncwrite(allocator, ctx, &options);
245 }
246
247 /* In this test, we must buffer multiple writes, since their cumulative size is under part-size */
248 AWS_TEST_CASE(test_s3_asyncwrite_small_file_many_writes, s_test_s3_asyncwrite_small_file_many_writes)
249 static int s_test_s3_asyncwrite_small_file_many_writes(struct aws_allocator *allocator, void *ctx) {
250 struct basic_asyncwrite_options options = {
251 .object_size = 100,
252 .max_bytes_per_write = 1,
253 };
254 return s_basic_asyncwrite(allocator, ctx, &options);
255 }
256
257 /* 1 part-sized write */
258 AWS_TEST_CASE(test_s3_asyncwrite_1_part, s_test_s3_asyncwrite_1_part)
259 static int s_test_s3_asyncwrite_1_part(struct aws_allocator *allocator, void *ctx) {
260 struct basic_asyncwrite_options options = {
261 .object_size = PART_SIZE,
262 };
263 return s_basic_asyncwrite(allocator, ctx, &options);
264 }
265
266 /* Send 1 full part, but spread across many writes.
267 * This is just stressing data buffering. */
268 AWS_TEST_CASE(test_s3_asyncwrite_1_part_many_writes, s_test_s3_asyncwrite_1_part_many_writes)
269 static int s_test_s3_asyncwrite_1_part_many_writes(struct aws_allocator *allocator, void *ctx) {
270 struct basic_asyncwrite_options options = {
271 .object_size = PART_SIZE,
272 .max_bytes_per_write = PART_SIZE / 16,
273 };
274 return s_basic_asyncwrite(allocator, ctx, &options);
275 }
276
277 /* Send 1 full part, then a separate empty EOF write.
278 * This probably results in a second (empty) part being uploaded. */
279 AWS_TEST_CASE(test_s3_asyncwrite_1_part_then_eof, s_test_s3_asyncwrite_1_part_then_eof)
280 static int s_test_s3_asyncwrite_1_part_then_eof(struct aws_allocator *allocator, void *ctx) {
281 struct basic_asyncwrite_options options = {
282 .object_size = PART_SIZE,
283 .eof_requires_extra_write = true,
284 };
285 return s_basic_asyncwrite(allocator, ctx, &options);
286 }
287
288 /* Send 2 part-sized writes.
289 * This stresses sending multiple parts. */
290 AWS_TEST_CASE(test_s3_asyncwrite_2_parts_2_partsize_writes, s_test_s3_asyncwrite_2_parts_2_partsize_writes)
291 static int s_test_s3_asyncwrite_2_parts_2_partsize_writes(struct aws_allocator *allocator, void *ctx) {
292 struct basic_asyncwrite_options options = {
293 .object_size = PART_SIZE * 2,
294 .max_bytes_per_write = PART_SIZE,
295 };
296 return s_basic_asyncwrite(allocator, ctx, &options);
297 }
298
299 /* Send 1 write, with enough data for 2 full parts.
300 * This stresses the case where a single write-future must persist while multiple
301 * calls to poll_write() are made under the hood */
302 AWS_TEST_CASE(test_s3_asyncwrite_2_parts_1_write, s_test_s3_asyncwrite_2_parts_1_write)
303 static int s_test_s3_asyncwrite_2_parts_1_write(struct aws_allocator *allocator, void *ctx) {
304 struct basic_asyncwrite_options options = {
305 .object_size = PART_SIZE * 2,
306 };
307 return s_basic_asyncwrite(allocator, ctx, &options);
308 }
309
310 /* Send 2 full parts, but the first write is larger than part-size.
311 * This tests the case where poll_write() can't handle all the data at once,
312 * and poll() needs to send the remainder in further calls to poll_write(). */
313 AWS_TEST_CASE(
314 test_s3_asyncwrite_2_parts_first_write_over_partsize,
315 s_test_s3_asyncwrite_2_parts_first_write_over_partsize)
316 static int s_test_s3_asyncwrite_2_parts_first_write_over_partsize(struct aws_allocator *allocator, void *ctx) {
317 struct basic_asyncwrite_options options = {
318 .object_size = PART_SIZE * 2,
319 .max_bytes_per_write = PART_SIZE + 100,
320 };
321 return s_basic_asyncwrite(allocator, ctx, &options);
322 }
323
324 /* Send 2 full parts, but the first write is less than part-size.
325 * This tests the case where both parts contain data from multiple poll_write() calls */
326 AWS_TEST_CASE(
327 test_s3_asyncwrite_2_parts_first_write_under_partsize,
328 s_test_s3_asyncwrite_2_parts_first_write_under_partsize)
329 static int s_test_s3_asyncwrite_2_parts_first_write_under_partsize(struct aws_allocator *allocator, void *ctx) {
330 struct basic_asyncwrite_options options = {
331 .object_size = PART_SIZE * 2,
332 .max_bytes_per_write = PART_SIZE - 100,
333 };
334 return s_basic_asyncwrite(allocator, ctx, &options);
335 }
336
337 /* We don't explicitly bar empty writes, since it's reasonable to do an empty write with the EOF at the end.
338 * Let's make sure we can tolerate empty writes at other arbitrary points. */
339 AWS_TEST_CASE(test_s3_asyncwrite_tolerate_empty_writes, s_test_s3_asyncwrite_tolerate_empty_writes)
340 static int s_test_s3_asyncwrite_tolerate_empty_writes(struct aws_allocator *allocator, void *ctx) {
341 (void)ctx;
342 struct asyncwrite_tester tester;
343 ASSERT_SUCCESS(s_asyncwrite_tester_init(&tester, allocator, PART_SIZE /*object_size*/));
344
345 struct aws_byte_cursor source_cursor = aws_byte_cursor_from_buf(&tester.source_buf);
346
347 /* empty write at start */
348 struct aws_byte_cursor empty_data = {0};
349 ASSERT_SUCCESS(s_write(&tester, empty_data, false /*eof*/));
350
351 /* write half the data */
352 struct aws_byte_cursor next_chunk = aws_byte_cursor_advance(&source_cursor, PART_SIZE / 2);
353 ASSERT_SUCCESS(s_write(&tester, next_chunk, false /*eof*/));
354
355 /* empty write in the middle */
356 ASSERT_SUCCESS(s_write(&tester, empty_data, false /*eof*/));
357
358 /* write up till we're 1 byte short of a full part */
359 next_chunk = aws_byte_cursor_advance(&source_cursor, (PART_SIZE / 2) - 1);
360 ASSERT_SUCCESS(s_write(&tester, next_chunk, false /*eof*/));
361
362 /* empty write when we're just 1 byte away from having a full part to send */
363 ASSERT_SUCCESS(s_write(&tester, empty_data, false /*eof*/));
364
365 /* write final byte, but don't send EOF yet */
366 next_chunk = aws_byte_cursor_advance(&source_cursor, 1);
367 ASSERT_SUCCESS(s_write(&tester, next_chunk, false /*eof*/));
368
369 /* empty write at the end, but don't send EOF yet */
370 ASSERT_SUCCESS(s_write(&tester, empty_data, false /*eof*/));
371
372 /* OK, finally send EOF */
373 ASSERT_SUCCESS(s_write(&tester, empty_data, true /*eof*/));
374
375 /* Done */
376 aws_s3_tester_wait_for_meta_request_finish(&tester.s3_tester);
377 ASSERT_SUCCESS(s_asyncwrite_tester_validate(&tester));
378 ASSERT_SUCCESS(s_asyncwrite_tester_clean_up(&tester));
379 return 0;
380 }
381
382 struct asyncwrite_on_another_thread_ctx {
383 struct asyncwrite_tester *tester;
384 size_t max_bytes_per_write;
385 struct aws_byte_cursor source_cursor;
386 struct aws_future_void *write_future;
387 };
388
389 static void s_write_from_future_callback(void *user_data) {
390 struct asyncwrite_on_another_thread_ctx *thread_ctx = user_data;
391
392 /* If there was a previous write, assert it succeeded */
393 if (thread_ctx->write_future != NULL) {
394 AWS_FATAL_ASSERT(aws_future_void_get_error(thread_ctx->write_future) == 0);
395 thread_ctx->write_future = aws_future_void_release(thread_ctx->write_future);
396 }
397
398 /* If that was the final write, we're done */
399 if (thread_ctx->source_cursor.len == 0) {
400 return;
401 }
402
403 /* Write next chunk */
404 size_t bytes_to_write = aws_min_size(thread_ctx->source_cursor.len, thread_ctx->max_bytes_per_write);
405 struct aws_byte_cursor next_chunk = aws_byte_cursor_advance(&thread_ctx->source_cursor, bytes_to_write);
406 bool eof = thread_ctx->source_cursor.len == 0;
407 thread_ctx->write_future = aws_s3_meta_request_write(thread_ctx->tester->meta_request, next_chunk, eof);
408
409 /* Register this function to run again when write completes */
410 aws_future_void_register_callback(thread_ctx->write_future, s_write_from_future_callback, thread_ctx);
411 }
412
413 /* This test tries to submit new writes from the write-future's completion callback,
414 * which often fires on another thread. */
415 AWS_TEST_CASE(test_s3_asyncwrite_write_from_future_callback, s_test_s3_asyncwrite_write_from_future_callback)
416 static int s_test_s3_asyncwrite_write_from_future_callback(struct aws_allocator *allocator, void *ctx) {
417 (void)ctx;
418 struct asyncwrite_tester tester;
419
420 /* Have a few parts, so we get more chances to write from a callback on another thread */
421 ASSERT_SUCCESS(s_asyncwrite_tester_init(&tester, allocator, PART_SIZE * 4 /*object_size*/));
422
423 struct asyncwrite_on_another_thread_ctx on_another_thread_ctx = {
424 .tester = &tester,
425 .source_cursor = aws_byte_cursor_from_buf(&tester.source_buf),
426 /* Use writes that don't divide nicely into part-size.
427 * This way, we're passing some buffered data, and some unbuffered data, to each part.
428 * And getting back some buffered leftovers when the part completes.
429 * This pushes a lot of edge cases, and makes it likely we'll catch any threading bugs. */
430 .max_bytes_per_write = (PART_SIZE / 2) + 1,
431 };
432
433 /* Kick off the recursive write-future completion callback loop */
434 s_write_from_future_callback(&on_another_thread_ctx);
435
436 /* Done */
437 aws_s3_tester_wait_for_meta_request_finish(&tester.s3_tester);
438 ASSERT_SUCCESS(s_asyncwrite_tester_validate(&tester));
439 ASSERT_SUCCESS(s_asyncwrite_tester_clean_up(&tester));
440 return 0;
441 }
442
443 /* This tests checks that, if the meta request fails before write() is called,
444 * the the write-future fails with AWS_ERROR_S3_REQUEST_HAS_COMPLETED */
445 AWS_TEST_CASE(test_s3_asyncwrite_fails_if_request_has_completed, s_test_s3_asyncwrite_fails_if_request_has_completed)
446 static int s_test_s3_asyncwrite_fails_if_request_has_completed(struct aws_allocator *allocator, void *ctx) {
447 (void)ctx;
448 struct asyncwrite_tester tester;
449
450 ASSERT_SUCCESS(s_asyncwrite_tester_init(&tester, allocator, PART_SIZE /*object_size*/));
451
452 /* Cancel meta request before write() call */
453 aws_s3_meta_request_cancel(tester.meta_request);
454
455 struct aws_future_void *write_future =
456 aws_s3_meta_request_write(tester.meta_request, aws_byte_cursor_from_buf(&tester.source_buf), true /*eof*/);
457
458 ASSERT_TRUE(aws_future_void_wait(write_future, TIMEOUT_NANOS));
459
460 ASSERT_INT_EQUALS(AWS_ERROR_S3_REQUEST_HAS_COMPLETED, aws_future_void_get_error(write_future));
461 write_future = aws_future_void_release(write_future);
462
463 /* Done */
464 aws_s3_tester_wait_for_meta_request_finish(&tester.s3_tester);
465
466 /* The meta request's error-code should still be CANCELED, the failed write() shouldn't affect that */
467 ASSERT_INT_EQUALS(AWS_ERROR_S3_CANCELED, tester.test_results.finished_error_code);
468
469 ASSERT_SUCCESS(s_asyncwrite_tester_clean_up(&tester));
470 return 0;
471 }
472
473 AWS_TEST_CASE(test_s3_asyncwrite_fails_if_write_after_eof, s_test_s3_asyncwrite_fails_if_write_after_eof)
474 static int s_test_s3_asyncwrite_fails_if_write_after_eof(struct aws_allocator *allocator, void *ctx) {
475 (void)ctx;
476 struct asyncwrite_tester tester;
477
478 ASSERT_SUCCESS(s_asyncwrite_tester_init(&tester, allocator, PART_SIZE /*object_size*/));
479
480 /* Write the whole object, with EOF */
481 ASSERT_SUCCESS(s_write(&tester, aws_byte_cursor_from_buf(&tester.source_buf), true /*eof*/));
482
483 /* Any more writes should fail with INVALID_STATE error */
484 struct aws_byte_cursor empty_cursor = {0};
485 struct aws_future_void *write_future = aws_s3_meta_request_write(tester.meta_request, empty_cursor, true /*eof*/);
486 ASSERT_TRUE(aws_future_void_wait(write_future, TIMEOUT_NANOS));
487 ASSERT_INT_EQUALS(AWS_ERROR_INVALID_STATE, aws_future_void_get_error(write_future));
488 write_future = aws_future_void_release(write_future);
489
490 /* Done. Don't really care if the request completes successfully or not */
491 aws_s3_tester_wait_for_meta_request_finish(&tester.s3_tester);
492 ASSERT_SUCCESS(s_asyncwrite_tester_clean_up(&tester));
493 return 0;
494 }
495
496 AWS_TEST_CASE(test_s3_asyncwrite_fails_if_writes_overlap, s_test_s3_asyncwrite_fails_if_writes_overlap)
497 static int s_test_s3_asyncwrite_fails_if_writes_overlap(struct aws_allocator *allocator, void *ctx) {
498 (void)ctx;
499 struct asyncwrite_tester tester;
500
501 /* Make it VERY likely that some writes will overlap by issuing a lot of them as fast as possible */
502 enum { num_writes = 100 };
503 ASSERT_SUCCESS(s_asyncwrite_tester_init(&tester, allocator, num_writes * PART_SIZE /*object_size*/));
504
505 bool had_overlapping_write = false;
506 struct aws_byte_cursor source_cursor = aws_byte_cursor_from_buf(&tester.source_buf);
507 bool eof = false;
508 while (!eof && !had_overlapping_write) {
509 struct aws_byte_cursor write_cursor = aws_byte_cursor_advance(&source_cursor, PART_SIZE);
510 eof = (source_cursor.len == 0);
511 struct aws_future_void *write_future = aws_s3_meta_request_write(tester.meta_request, write_cursor, eof);
512 int write_error_code = aws_future_void_is_done(write_future) ? aws_future_void_get_error(write_future) : 0;
513 aws_future_void_release(write_future);
514
515 if (write_error_code != 0) {
516 /* INVALID_STATE is the error code for overlapping writes */
517 ASSERT_INT_EQUALS(AWS_ERROR_INVALID_STATE, write_error_code);
518 had_overlapping_write = true;
519 }
520 }
521
522 ASSERT_TRUE(had_overlapping_write);
523
524 /* Any error from the write() call should result in the meta request terminating with INVALID_STATE error */
525 aws_s3_tester_wait_for_meta_request_finish(&tester.s3_tester);
526 ASSERT_INT_EQUALS(AWS_ERROR_INVALID_STATE, tester.test_results.finished_error_code);
527
528 ASSERT_SUCCESS(s_asyncwrite_tester_clean_up(&tester));
529 return 0;
530 }
531
532 static int s_wait_for_sub_request_to_send(
533 struct asyncwrite_tester *tester,
534 enum aws_s3_request_type request_type,
535 uint64_t timeout) {
536
537 uint64_t now;
538 ASSERT_SUCCESS(aws_high_res_clock_get_ticks(&now));
539 const uint64_t timeout_timestamp = now + timeout;
540 const uint64_t sleep_between_checks = aws_timestamp_convert(100, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
541
542 bool request_sent = false;
543 while (!request_sent) {
544 aws_s3_tester_lock_synced_data(&tester->s3_tester);
545 for (size_t i = 0; i < aws_array_list_length(&tester->test_results.synced_data.metrics); ++i) {
546 struct aws_s3_request_metrics *metrics = NULL;
547 ASSERT_SUCCESS(aws_array_list_get_at(&tester->test_results.synced_data.metrics, (void **)&metrics, i));
548 enum aws_s3_request_type request_type_i;
549 aws_s3_request_metrics_get_request_type(metrics, &request_type_i);
550 if (request_type_i == request_type) {
551 if (aws_s3_request_metrics_get_error_code(metrics) == 0) {
552 request_sent = true;
553 }
554 }
555 }
556 aws_s3_tester_unlock_synced_data(&tester->s3_tester);
557
558 if (!request_sent) {
559 /* Check for timeout, then sleep a bit before checking again */
560 ASSERT_SUCCESS(aws_high_res_clock_get_ticks(&now));
561 ASSERT_TRUE(
562 now < timeout_timestamp,
563 "Timed out waiting for %s to be sent",
564 aws_s3_request_type_operation_name(request_type));
565 aws_thread_current_sleep(sleep_between_checks);
566 }
567 }
568 return 0;
569 }
570
571 /* Test that aws_s3_meta_request_cancel() will result in AbortMultipartUpload being sent.
572 * This is a regression test: once upon a time cancel() forgot to trigger the client's update(),
573 * and so the meta-request would hang until something else kicked the update loop. */
574 AWS_TEST_CASE(test_s3_asyncwrite_cancel_sends_abort, s_test_s3_asyncwrite_cancel_sends_abort)
575 static int s_test_s3_asyncwrite_cancel_sends_abort(struct aws_allocator *allocator, void *ctx) {
576 (void)ctx;
577
578 struct asyncwrite_tester tester;
579 ASSERT_SUCCESS(s_asyncwrite_tester_init(&tester, allocator, PART_SIZE * 3 /*object_size*/));
580
581 const uint64_t one_sec_in_nanos = aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL);
582
583 /* Wait for StartMultipartUpload to be sent */
584 ASSERT_SUCCESS(s_wait_for_sub_request_to_send(
585 &tester, AWS_S3_REQUEST_TYPE_CREATE_MULTIPART_UPLOAD, 10 * one_sec_in_nanos /*timeout*/));
586
587 /* Sleep a bit to ensure the client isn't doing anything, then cancel() */
588 aws_thread_current_sleep(one_sec_in_nanos);
589
590 aws_s3_meta_request_cancel(tester.meta_request);
591
592 /* Wait for AbortMultipartUpload to be sent.
593 * Ugh if timeout is too long we risk some unrelated system updating the client and hiding this bug.
594 * But if timeout is too short CI will randomly fail on super slow system. */
595 ASSERT_SUCCESS(s_wait_for_sub_request_to_send(
596 &tester, AWS_S3_REQUEST_TYPE_ABORT_MULTIPART_UPLOAD, 5 * one_sec_in_nanos /*timeout*/));
597
598 /* Wait for meta request to complete */
599 aws_s3_tester_wait_for_meta_request_finish(&tester.s3_tester);
600
601 ASSERT_INT_EQUALS(AWS_ERROR_S3_CANCELED, tester.test_results.finished_error_code);
602
603 ASSERT_SUCCESS(s_asyncwrite_tester_clean_up(&tester));
604 return 0;
605 }
606
607 /*
608 * Custom buffer pool that DEFERS async-write ticket reservations.
609 *
610 * The async-write path reserves a part buffer with can_block=true. The default pool satisfies
611 * such reservations synchronously (with a "forced" buffer). This wrapper instead defers them:
612 * it forwards the reservation to the wrapped default pool (which hands back a done future), then
613 * returns a fresh, not-yet-done future to the caller and completes it a bit later on its own
614 * event loop. This exercises aws_s3_meta_request_poll_write()'s deferral path, where it must
615 * return result.is_pending and resume once the reservation is fulfilled.
616 *
617 * Non-blocking reservations (the regular GET/PUT path) are forwarded unchanged.
618 */
619
620 /* Settings the test hands to the pool factory via buffer_pool_user_data. */
621 struct deferring_pool_settings {
622 /* How long to hold each async-write reservation pending before fulfilling it. */
623 uint64_t defer_delay_ms;
624 /* Filled in by the factory: points at the pool's deferred_count so the test can assert
625 * the deferral path was exercised. */
626 struct aws_atomic_var *deferred_count_out;
627 };
628
629 struct deferring_pool {
630 struct aws_s3_buffer_pool base; /* must be first */
631 struct aws_allocator *allocator;
632 struct aws_s3_buffer_pool *wrapped; /* the real default pool */
633 struct aws_event_loop_group *elg; /* owns the event loop we defer onto */
634 uint64_t defer_delay_ms;
635 /* Number of can_block reservations we actually deferred. Lets the test confirm the
636 * deferral path was exercised (and not silently bypassed). */
637 struct aws_atomic_var deferred_count;
638 };
639
640 /* One deferred reservation in flight. */
641 struct deferring_pool_pending {
642 struct aws_allocator *allocator;
643 struct aws_task task;
644 struct aws_event_loop *event_loop;
645 /* Reference on the wrapped pool, held until this pending task releases its ticket.
646 * Default-pool tickets don't ref-count their pool, so we must keep the pool alive until
647 * the wrapped_future (which owns the ticket) is released, even during shutdown. */
648 struct aws_s3_buffer_pool *wrapped;
649 /* The done future from the wrapped pool, holding the real ticket. */
650 struct aws_future_s3_buffer_ticket *wrapped_future;
651 /* The not-yet-done future we handed back to the caller. */
652 struct aws_future_s3_buffer_ticket *deferred_future;
653 };
654
655 static void s_deferring_pool_complete_task(struct aws_task *task, void *arg, enum aws_task_status status) {
656 (void)task;
657 struct deferring_pool_pending *pending = arg;
658
659 if (status == AWS_TASK_STATUS_RUN_READY) {
660 /* Move the real ticket out of the wrapped future and into the deferred future. */
661 struct aws_s3_buffer_ticket *ticket = aws_future_s3_buffer_ticket_get_result_by_move(pending->wrapped_future);
662 aws_future_s3_buffer_ticket_set_result_by_move(pending->deferred_future, &ticket);
663 } else {
664 /* Shutting down: just error the deferred future so it doesn't hang. */
665 aws_future_s3_buffer_ticket_set_error(pending->deferred_future, AWS_ERROR_S3_CANCELED);
666 }
667
668 /* Release the ticket (via wrapped_future) BEFORE releasing our pool reference, so the ticket's
669 * destructor still sees a live pool. */
670 aws_future_s3_buffer_ticket_release(pending->wrapped_future);
671 aws_future_s3_buffer_ticket_release(pending->deferred_future);
672 aws_s3_buffer_pool_release(pending->wrapped);
673 aws_mem_release(pending->allocator, pending);
674 }
675
676 static struct aws_future_s3_buffer_ticket *s_deferring_pool_reserve(
677 struct aws_s3_buffer_pool *pool_wrapper,
678 struct aws_s3_buffer_pool_reserve_meta meta) {
679
680 struct deferring_pool *pool = pool_wrapper->impl;
681
682 /* Forward to the real pool. */
683 struct aws_future_s3_buffer_ticket *wrapped_future = aws_s3_buffer_pool_reserve(pool->wrapped, meta);
684
685 /* Only defer the async-write (can_block) reservations, and only when the wrapped pool
686 * actually granted one synchronously (it always does for can_block). Everything else is
687 * forwarded unchanged. */
688 if (!meta.can_block || !aws_future_s3_buffer_ticket_is_done(wrapped_future) ||
689 aws_future_s3_buffer_ticket_get_error(wrapped_future) != AWS_ERROR_SUCCESS) {
690 return wrapped_future;
691 }
692
693 /* Hand back a fresh, not-yet-done future and complete it later on our event loop. */
694 struct deferring_pool_pending *pending = aws_mem_calloc(pool->allocator, 1, sizeof(struct deferring_pool_pending));
695 pending->allocator = pool->allocator;
696 pending->wrapped = aws_s3_buffer_pool_acquire(pool->wrapped); /* keep pool alive until ticket released */
697 pending->wrapped_future = wrapped_future; /* take ownership of the reference */
698 pending->deferred_future = aws_future_s3_buffer_ticket_new(pool->allocator);
699 pending->event_loop = aws_event_loop_group_get_next_loop(pool->elg);
700
701 struct aws_future_s3_buffer_ticket *deferred_future = aws_future_s3_buffer_ticket_acquire(pending->deferred_future);
702
703 aws_atomic_fetch_add(&pool->deferred_count, 1);
704
705 aws_task_init(&pending->task, s_deferring_pool_complete_task, pending, "deferring_pool_complete");
706 uint64_t now = 0;
707 aws_event_loop_current_clock_time(pending->event_loop, &now);
708 /* Delay so the reservation is genuinely pending when poll_write() first sees it. */
709 uint64_t run_at =
710 now + aws_timestamp_convert(pool->defer_delay_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
711 aws_event_loop_schedule_task_future(pending->event_loop, &pending->task, run_at);
712
713 return deferred_future;
714 }
715
716 static void s_deferring_pool_trim(struct aws_s3_buffer_pool *pool_wrapper) {
717 struct deferring_pool *pool = pool_wrapper->impl;
718 aws_s3_buffer_pool_trim(pool->wrapped);
719 }
720
721 static void s_deferring_pool_destroy(void *arg) {
722 struct aws_s3_buffer_pool *pool_wrapper = arg;
723 struct deferring_pool *pool = pool_wrapper->impl;
724 aws_s3_buffer_pool_release(pool->wrapped);
725 aws_event_loop_group_release(pool->elg);
726 struct aws_allocator *allocator = pool->allocator;
727 aws_mem_release(allocator, pool);
728 }
729
730 static struct aws_s3_buffer_pool_vtable s_deferring_pool_vtable = {
731 .reserve = s_deferring_pool_reserve,
732 .trim = s_deferring_pool_trim,
733 };
734
735 static struct aws_s3_buffer_pool *s_deferring_pool_factory(
736 struct aws_allocator *allocator,
737 struct aws_s3_buffer_pool_config config,
738 void *user_data) {
739
740 struct deferring_pool_settings *settings = user_data;
741
742 struct deferring_pool *pool = aws_mem_calloc(allocator, 1, sizeof(struct deferring_pool));
743 pool->allocator = allocator;
744 pool->wrapped = aws_s3_default_buffer_pool_new(allocator, config);
745 AWS_FATAL_ASSERT(pool->wrapped != NULL);
746 pool->elg = aws_event_loop_group_new_default(allocator, 1, NULL);
747 AWS_FATAL_ASSERT(pool->elg != NULL);
748 pool->defer_delay_ms = (settings != NULL && settings->defer_delay_ms > 0) ? settings->defer_delay_ms : 20;
749 aws_atomic_init_int(&pool->deferred_count, 0);
750
751 /* Let the test observe how many reservations we deferred. */
752 if (settings != NULL) {
753 settings->deferred_count_out = &pool->deferred_count;
754 }
755
756 pool->base.impl = pool;
757 pool->base.vtable = &s_deferring_pool_vtable;
758 aws_ref_count_init(&pool->base.ref_count, &pool->base, s_deferring_pool_destroy);
759
760 return &pool->base;
761 }
762
763 /* Verifies the buffer pool can DEFER async-write ticket reservations: the write path must wait on
764 * the reservation future instead of failing when it isn't fulfilled immediately.
765 * Uses a custom pool that defers every async-write reservation; the upload must still succeed. */
766 AWS_TEST_CASE(test_s3_asyncwrite_deferred_buffer_reservation, s_test_s3_asyncwrite_deferred_buffer_reservation)
767 static int s_test_s3_asyncwrite_deferred_buffer_reservation(struct aws_allocator *allocator, void *ctx) {
768 (void)ctx;
769 struct asyncwrite_tester tester;
770 struct deferring_pool_settings pool_settings = {.defer_delay_ms = 20};
771 struct asyncwrite_tester_options options = {
772 .buffer_pool_factory_fn = s_deferring_pool_factory,
773 .buffer_pool_user_data = &pool_settings,
774 };
775
776 const size_t part_count = 3;
777 ASSERT_SUCCESS(s_asyncwrite_tester_init_ex(&tester, allocator, PART_SIZE * part_count /*object_size*/, &options));
778
779 bool eof = false;
780 struct aws_byte_cursor source_cursor = aws_byte_cursor_from_buf(&tester.source_buf);
781 while (source_cursor.len > 0) {
782 size_t bytes_to_write = aws_min_size(PART_SIZE, source_cursor.len);
783 struct aws_byte_cursor write_cursor = aws_byte_cursor_advance(&source_cursor, bytes_to_write);
784 eof = (source_cursor.len == 0);
785 ASSERT_SUCCESS(s_write(&tester, write_cursor, eof));
786 }
787
788 aws_s3_tester_wait_for_meta_request_finish(&tester.s3_tester);
789 ASSERT_SUCCESS(s_asyncwrite_tester_validate(&tester));
790 ASSERT_NOT_NULL(pool_settings.deferred_count_out);
791 ASSERT_UINT_EQUALS(part_count, aws_atomic_load_int(pool_settings.deferred_count_out));
792
793 ASSERT_SUCCESS(s_asyncwrite_tester_clean_up(&tester));
794 return 0;
795 }
796
797 /* Verifies the lifetime/cleanup of a DEFERRED async-write reservation when the meta request is
798 * canceled while the reservation is still pending: the pending future must be errored-out (so its
799 * completion callback fires, cleans up, and releases the ref it holds on the meta request) without
800 * leaking or use-after-free. */
801 AWS_TEST_CASE(
802 test_s3_asyncwrite_cancel_while_reservation_deferred,
803 s_test_s3_asyncwrite_cancel_while_reservation_deferred)
804 static int s_test_s3_asyncwrite_cancel_while_reservation_deferred(struct aws_allocator *allocator, void *ctx) {
805 (void)ctx;
806 struct asyncwrite_tester tester;
807
808 /* Hold each async-write reservation pending for a good while, so we can reliably cancel
809 * the meta request while the very first part's buffer reservation is still deferred. */
810 struct deferring_pool_settings pool_settings = {.defer_delay_ms = 2000};
811 struct asyncwrite_tester_options options = {
812 .buffer_pool_factory_fn = s_deferring_pool_factory,
813 .buffer_pool_user_data = &pool_settings,
814 };
815
816 ASSERT_SUCCESS(s_asyncwrite_tester_init_ex(&tester, allocator, PART_SIZE * 2 /*object_size*/, &options));
817
818 /* Kick off a write of a full part. This reserves a buffer, which the pool defers, so the
819 * write future will be pending (not done) when we cancel below. We intentionally do NOT
820 * wait for this future to complete. */
821 struct aws_byte_cursor source_cursor = aws_byte_cursor_from_buf(&tester.source_buf);
822 struct aws_byte_cursor first_part = aws_byte_cursor_advance(&source_cursor, PART_SIZE);
823 struct aws_future_void *write_future = aws_s3_meta_request_write(tester.meta_request, first_part, false /*eof*/);
824 ASSERT_NOT_NULL(write_future);
825
826 /* Give poll_write() a moment to issue the (deferred) reservation and register its waker. */
827 aws_thread_current_sleep(aws_timestamp_convert(200, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL));
828
829 /* Cancel while the reservation is still pending. This drives finish_default(), which must
830 * error the pending reservation future. */
831 aws_s3_meta_request_cancel(tester.meta_request);
832
833 ASSERT_TRUE(aws_future_void_wait(write_future, TIMEOUT_NANOS));
834 ASSERT_INT_EQUALS(AWS_ERROR_S3_REQUEST_HAS_COMPLETED, aws_future_void_get_error(write_future));
835 write_future = aws_future_void_release(write_future);
836
837 aws_s3_tester_wait_for_meta_request_finish(&tester.s3_tester);
838 ASSERT_INT_EQUALS(AWS_ERROR_S3_CANCELED, tester.test_results.finished_error_code);
839 ASSERT_NOT_NULL(pool_settings.deferred_count_out);
840 ASSERT_UINT_EQUALS(1, aws_atomic_load_int(pool_settings.deferred_count_out));
841
842 ASSERT_SUCCESS(s_asyncwrite_tester_clean_up(&tester));
843 return 0;
844 }