Debian Codegraph @

crt/aws-c-s3/tests/s3_many_async_uploads_without_data_test.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/common/clock.h>
8 #include <aws/s3/private/s3_util.h>
9 #include <aws/testing/aws_test_harness.h>
10
11 /**
12 * Regression test for deadlock discovered by a user of Mountpoint (which wraps aws-c-s3
13 * with a filesystem-like API). The user opened MANY files at once.
14 * The user wrote data to some of the later files they opened,
15 * and waited for those writes to complete.
16 * But aws-c-s3 was waiting on data from the first few files.
17 * Both sides were waiting on each other. It was a deadlock.
18 *
19 * This test starts N upload meta-requests.
20 * Then, it only sends data to 1 meta-request at a time, starting with the last
21 * meta-request it created, and working backwards to the first.
22 * If the test times out, then we still suffer from the deadlock.
23 */
24
25 /* Number of simultaneous upload meta-requests to create */
26 #define MANY_ASYNC_UPLOADS_COUNT 200
27
28 /* Number of bytes each meta-request should upload (small so this this doesn't take forever) */
29 #define MANY_ASYNC_UPLOADS_OBJECT_SIZE 100
30
31 /* Bytes per write */
32 #define MANY_ASYNC_UPLOADS_BYTES_PER_WRITE 10
33
34 /* How long to spend doing nothing, before assuming we're deadlocked */
35 #define SEND_DATA_TIMEOUT_NANOS ((uint64_t)AWS_TIMESTAMP_NANOS * 10) /* 10secs */
36
37 /* See top of file for full description of what's going on in this test. */
38 AWS_TEST_CASE(test_s3_many_async_uploads_without_data, s_test_s3_many_async_uploads_without_data)
39 static int s_test_s3_many_async_uploads_without_data(struct aws_allocator *allocator, void *ctx) {
40 (void)ctx;
41
42 /* Set up */
43 struct aws_s3_tester tester;
44 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
45
46 struct aws_s3_client *client = NULL;
47 struct aws_s3_tester_client_options client_options;
48 AWS_ZERO_STRUCT(client_options);
49 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
50
51 struct aws_s3_meta_request *meta_requests[MANY_ASYNC_UPLOADS_COUNT];
52 struct aws_s3_meta_request_test_results meta_request_test_results[MANY_ASYNC_UPLOADS_COUNT];
53
54 /* Create N upload meta-requests, each with an async-input-stream that
55 * won't provide data until later in this test... */
56 for (int i = 0; i < MANY_ASYNC_UPLOADS_COUNT; ++i) {
57 aws_s3_meta_request_test_results_init(&meta_request_test_results[i], allocator);
58
59 struct aws_string *host_name =
60 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
61 struct aws_byte_cursor host_name_cursor = aws_byte_cursor_from_string(host_name);
62
63 char object_name[128] = {0};
64 snprintf(object_name, sizeof(object_name), "/many-async-uploads-%d.txt", i);
65 struct aws_byte_buf object_path;
66 ASSERT_SUCCESS(
67 aws_s3_tester_upload_file_path_init(allocator, &object_path, aws_byte_cursor_from_c_str(object_name)));
68
69 struct aws_http_message *message = aws_s3_test_put_object_request_new_without_body(
70 allocator,
71 &host_name_cursor,
72 g_test_body_content_type,
73 aws_byte_cursor_from_buf(&object_path),
74 MANY_ASYNC_UPLOADS_OBJECT_SIZE,
75 0 /*flags*/);
76
77 /* Erase content-length header, because Mountpoint always uploads with unknown content-length */
78 aws_http_headers_erase(aws_http_message_get_headers(message), g_content_length_header_name);
79
80 struct aws_s3_meta_request_options options = {
81 .type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
82 .message = message,
83 .send_using_async_writes = true,
84 };
85 ASSERT_SUCCESS(aws_s3_tester_bind_meta_request(&tester, &options, &meta_request_test_results[i]));
86
87 meta_requests[i] = aws_s3_client_make_meta_request(client, &options);
88
89 /* Release stuff created in this loop */
90 aws_string_destroy(host_name);
91 aws_byte_buf_clean_up(&object_path);
92 aws_http_message_release(message);
93 }
94
95 /* Starting at the end, and working backwards, only provide data to one meta-request at a time. */
96 for (int i = MANY_ASYNC_UPLOADS_COUNT - 1; i >= 0; --i) {
97
98 struct aws_s3_meta_request *meta_request_i = meta_requests[i];
99
100 /* Perform sequential writes to meta_request_i, until EOF */
101 size_t bytes_written = 0;
102 bool eof = false;
103 while (!eof) {
104 size_t bytes_to_write =
105 aws_min_size(MANY_ASYNC_UPLOADS_BYTES_PER_WRITE, MANY_ASYNC_UPLOADS_OBJECT_SIZE - bytes_written);
106
107 eof = (bytes_written + bytes_to_write) == MANY_ASYNC_UPLOADS_OBJECT_SIZE;
108
109 /* use freshly allocated buffer for each write, so that we're likely to get memory violations
110 * if this data is used wrong internally. */
111 struct aws_byte_buf tmp_data;
112 aws_byte_buf_init(&tmp_data, allocator, bytes_to_write);
113 aws_byte_buf_write_u8_n(&tmp_data, 'z', bytes_to_write);
114
115 struct aws_future_void *write_future =
116 aws_s3_meta_request_write(meta_request_i, aws_byte_cursor_from_buf(&tmp_data), eof);
117
118 ASSERT_TRUE(
119 aws_future_void_wait(write_future, SEND_DATA_TIMEOUT_NANOS),
120 "Timed out waiting to send data on upload %d/%d."
121 " After writing %zu bytes, timed out on write(data=%zu, eof=%d)",
122 i + 1,
123 MANY_ASYNC_UPLOADS_COUNT,
124 bytes_written,
125 bytes_to_write,
126 eof);
127
128 /* write complete! */
129 aws_byte_buf_clean_up(&tmp_data);
130
131 ASSERT_INT_EQUALS(0, aws_future_void_get_error(write_future));
132 aws_future_void_release(write_future);
133
134 bytes_written += bytes_to_write;
135 }
136 }
137
138 /* Wait for everything to finish */
139 for (int i = 0; i < MANY_ASYNC_UPLOADS_COUNT; ++i) {
140 meta_requests[i] = aws_s3_meta_request_release(meta_requests[i]);
141 }
142
143 aws_s3_tester_wait_for_meta_request_finish(&tester);
144 aws_s3_tester_wait_for_meta_request_shutdown(&tester);
145
146 for (int i = 0; i < MANY_ASYNC_UPLOADS_COUNT; ++i) {
147 aws_s3_tester_validate_put_object_results(&meta_request_test_results[i], 0 /*flags*/);
148 aws_s3_meta_request_test_results_clean_up(&meta_request_test_results[i]);
149 }
150
151 /* Cleanup */
152 aws_s3_client_release(client);
153 aws_s3_tester_clean_up(&tester);
154
155 return 0;
156 }