| 1 |
|
| 2 |
|
| 3 |
|
| 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) |
| 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 |
|
| 31 |
struct asyncwrite_tester_options { |
| 32 |
|
| 33 |
|
| 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 |
|
| 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 |
|
| 67 |
aws_byte_buf_init(&tester->source_buf, allocator, object_size); |
| 68 |
ASSERT_SUCCESS(aws_device_random_buffer(&tester->source_buf)); |
| 69 |
|
| 70 |
|
| 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 ); |
| 87 |
|
| 88 |
|
| 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 |
|
| 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 ); |
| 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 )); |
| 123 |
|
| 124 |
|
| 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 |
|
| 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 ); |
| 133 |
aws_byte_cursor_advance(&source_cursor, (size_t)part_size); |
| 134 |
|
| 135 |
|
| 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 |
|
| 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 |
|
| 164 |
|
| 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 |
|
| 179 |
size_t object_size; |
| 180 |
|
| 181 |
size_t max_bytes_per_write; |
| 182 |
|
| 183 |
bool eof_requires_extra_write; |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 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 |
|
| 210 |
if (!eof) { |
| 211 |
ASSERT_SUCCESS(s_write(&tester, (struct aws_byte_cursor){0}, true )); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 267 |
|
| 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 |
|
| 278 |
|
| 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 |
|
| 289 |
|
| 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 |
|
| 300 |
|
| 301 |
|
| 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 |
|
| 311 |
|
| 312 |
|
| 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 |
|
| 325 |
|
| 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 |
|
| 338 |
|
| 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 )); |
| 344 |
|
| 345 |
struct aws_byte_cursor source_cursor = aws_byte_cursor_from_buf(&tester.source_buf); |
| 346 |
|
| 347 |
|
| 348 |
struct aws_byte_cursor empty_data = {0}; |
| 349 |
ASSERT_SUCCESS(s_write(&tester, empty_data, false )); |
| 350 |
|
| 351 |
|
| 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 )); |
| 354 |
|
| 355 |
|
| 356 |
ASSERT_SUCCESS(s_write(&tester, empty_data, false )); |
| 357 |
|
| 358 |
|
| 359 |
next_chunk = aws_byte_cursor_advance(&source_cursor, (PART_SIZE / 2) - 1); |
| 360 |
ASSERT_SUCCESS(s_write(&tester, next_chunk, false )); |
| 361 |
|
| 362 |
|
| 363 |
ASSERT_SUCCESS(s_write(&tester, empty_data, false )); |
| 364 |
|
| 365 |
|
| 366 |
next_chunk = aws_byte_cursor_advance(&source_cursor, 1); |
| 367 |
ASSERT_SUCCESS(s_write(&tester, next_chunk, false )); |
| 368 |
|
| 369 |
|
| 370 |
ASSERT_SUCCESS(s_write(&tester, empty_data, false )); |
| 371 |
|
| 372 |
|
| 373 |
ASSERT_SUCCESS(s_write(&tester, empty_data, true )); |
| 374 |
|
| 375 |
|
| 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 |
|
| 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 |
|
| 399 |
if (thread_ctx->source_cursor.len == 0) { |
| 400 |
return; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 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 |
|
| 410 |
aws_future_void_register_callback(thread_ctx->write_future, s_write_from_future_callback, thread_ctx); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 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 |
|
| 421 |
ASSERT_SUCCESS(s_asyncwrite_tester_init(&tester, allocator, PART_SIZE * 4 )); |
| 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 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
.max_bytes_per_write = (PART_SIZE / 2) + 1, |
| 431 |
}; |
| 432 |
|
| 433 |
|
| 434 |
s_write_from_future_callback(&on_another_thread_ctx); |
| 435 |
|
| 436 |
|
| 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 |
|
| 444 |
|
| 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 )); |
| 451 |
|
| 452 |
|
| 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 ); |
| 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 |
|
| 464 |
aws_s3_tester_wait_for_meta_request_finish(&tester.s3_tester); |
| 465 |
|
| 466 |
|
| 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 )); |
| 479 |
|
| 480 |
|
| 481 |
ASSERT_SUCCESS(s_write(&tester, aws_byte_cursor_from_buf(&tester.source_buf), true )); |
| 482 |
|
| 483 |
|
| 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 ); |
| 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 |
|
| 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 |
|
| 502 |
enum { num_writes = 100 }; |
| 503 |
ASSERT_SUCCESS(s_asyncwrite_tester_init(&tester, allocator, num_writes * PART_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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 572 |
|
| 573 |
|
| 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 )); |
| 580 |
|
| 581 |
const uint64_t one_sec_in_nanos = aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL); |
| 582 |
|
| 583 |
|
| 584 |
ASSERT_SUCCESS(s_wait_for_sub_request_to_send( |
| 585 |
&tester, AWS_S3_REQUEST_TYPE_CREATE_MULTIPART_UPLOAD, 10 * one_sec_in_nanos )); |
| 586 |
|
| 587 |
|
| 588 |
aws_thread_current_sleep(one_sec_in_nanos); |
| 589 |
|
| 590 |
aws_s3_meta_request_cancel(tester.meta_request); |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
ASSERT_SUCCESS(s_wait_for_sub_request_to_send( |
| 596 |
&tester, AWS_S3_REQUEST_TYPE_ABORT_MULTIPART_UPLOAD, 5 * one_sec_in_nanos )); |
| 597 |
|
| 598 |
|
| 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 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
struct deferring_pool_settings { |
| 622 |
|
| 623 |
uint64_t defer_delay_ms; |
| 624 |
|
| 625 |
|
| 626 |
struct aws_atomic_var *deferred_count_out; |
| 627 |
}; |
| 628 |
|
| 629 |
struct deferring_pool { |
| 630 |
struct aws_s3_buffer_pool base; |
| 631 |
struct aws_allocator *allocator; |
| 632 |
struct aws_s3_buffer_pool *wrapped; |
| 633 |
struct aws_event_loop_group *elg; |
| 634 |
uint64_t defer_delay_ms; |
| 635 |
|
| 636 |
|
| 637 |
struct aws_atomic_var deferred_count; |
| 638 |
}; |
| 639 |
|
| 640 |
|
| 641 |
struct deferring_pool_pending { |
| 642 |
struct aws_allocator *allocator; |
| 643 |
struct aws_task task; |
| 644 |
struct aws_event_loop *event_loop; |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
struct aws_s3_buffer_pool *wrapped; |
| 649 |
|
| 650 |
struct aws_future_s3_buffer_ticket *wrapped_future; |
| 651 |
|
| 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 |
|
| 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 |
|
| 665 |
aws_future_s3_buffer_ticket_set_error(pending->deferred_future, AWS_ERROR_S3_CANCELED); |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 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 |
|
| 683 |
struct aws_future_s3_buffer_ticket *wrapped_future = aws_s3_buffer_pool_reserve(pool->wrapped, meta); |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 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 |
|
| 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); |
| 697 |
pending->wrapped_future = wrapped_future; |
| 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 |
|
| 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 |
|
| 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 |
|
| 764 |
|
| 765 |
|
| 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 , &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 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 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 |
|
| 809 |
|
| 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 , &options)); |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 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 ); |
| 824 |
ASSERT_NOT_NULL(write_future); |
| 825 |
|
| 826 |
|
| 827 |
aws_thread_current_sleep(aws_timestamp_convert(200, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL)); |
| 828 |
|
| 829 |
|
| 830 |
|
| 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 |
} |