Debian Codegraph @

crt/aws-c-s3/include/aws/s3/private/s3_meta_request_impl.h

raw symbols SCIP
1 #ifndef AWS_S3_META_REQUEST_IMPL_H
2 #define AWS_S3_META_REQUEST_IMPL_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/auth/signing.h>
10 #include <aws/common/atomics.h>
11 #include <aws/common/linked_list.h>
12 #include <aws/common/mutex.h>
13 #include <aws/common/ref_count.h>
14 #include <aws/common/task_scheduler.h>
15 #include <aws/http/request_response.h>
16
17 #include "aws/s3/private/s3_checksums.h"
18 #include "aws/s3/private/s3_client_impl.h"
19 #include "aws/s3/private/s3_request.h"
20
21 struct aws_s3_client;
22 struct aws_s3_connection;
23 struct aws_s3_meta_request;
24 struct aws_s3_request;
25 struct aws_http_headers;
26 struct aws_http_make_request_options;
27 struct aws_retry_strategy;
28 struct aws_s3_upload_request_checksum_context;
29 enum aws_s3_meta_request_state {
30 AWS_S3_META_REQUEST_STATE_ACTIVE,
31 AWS_S3_META_REQUEST_STATE_FINISHED,
32 };
33
34 enum aws_s3_meta_request_update_flags {
35 /* The client potentially has multiple meta requests that it can spread across connections, and the given meta
36 request can selectively not return a request if there is a performance reason to do so.*/
37 AWS_S3_META_REQUEST_UPDATE_FLAG_CONSERVATIVE = 0x00000002,
38 };
39
40 typedef void(aws_s3_meta_request_prepare_request_callback_fn)(
41 struct aws_s3_meta_request *meta_request,
42 struct aws_s3_request *request,
43 int error_code,
44 void *user_data);
45
46 struct aws_s3_prepare_request_payload {
47 struct aws_allocator *allocator;
48 struct aws_s3_request *request;
49 struct aws_task task;
50 /* async step: wait for vtable->prepare_request() call to complete */
51 struct aws_future_void *asyncstep_prepare_request;
52 /* callback to invoke when all request preparation work is complete */
53 aws_s3_meta_request_prepare_request_callback_fn *callback;
54 void *user_data;
55 };
56
57 /* An event to be delivered on the meta-request's io_event_loop thread. */
58 struct aws_s3_meta_request_event {
59 enum aws_s3_meta_request_event_type {
60 AWS_S3_META_REQUEST_EVENT_RESPONSE_BODY, /* body_callback */
61 AWS_S3_META_REQUEST_EVENT_PROGRESS, /* progress_callback */
62 AWS_S3_META_REQUEST_EVENT_TELEMETRY, /* telemetry_callback */
63 } type;
64
65 union {
66 /* data for AWS_S3_META_REQUEST_EVENT_RESPONSE_BODY */
67 struct {
68 struct aws_s3_request *completed_request;
69 size_t bytes_delivered;
70 } response_body;
71
72 /* data for AWS_S3_META_REQUEST_EVENT_PROGRESS */
73 struct {
74 struct aws_s3_meta_request_progress info;
75 } progress;
76
77 /* data for AWS_S3_META_REQUEST_EVENT_TELEMETRY */
78 struct {
79 struct aws_s3_request_metrics *metrics;
80 } telemetry;
81 } u;
82 };
83
84 struct aws_s3_meta_request_vtable {
85 /* Update the meta request. out_request is required to be non-null. Returns true if there is any work in
86 * progress, false if there is not. */
87 bool (*update)(struct aws_s3_meta_request *meta_request, uint32_t flags, struct aws_s3_request **out_request);
88
89 /* Run vtable->prepare_request() on the meta-request's event loop.
90 * We do this because body streaming is slow, and we don't want it on our networking threads.
91 * The callback may fire on any thread (an async sub-step may run on another thread). */
92 void (*schedule_prepare_request)(
93 struct aws_s3_meta_request *meta_request,
94 struct aws_s3_request *request,
95 aws_s3_meta_request_prepare_request_callback_fn *callback,
96 void *user_data);
97
98 /* Given a request, asynchronously prepare it for sending
99 * (creating the correct HTTP message, reading from a stream (if necessary), computing hashes, etc.).
100 * Returns a future, which may complete on any thread (and may complete synchronously). */
101 struct aws_future_void *(*prepare_request)(struct aws_s3_request *request);
102
103 void (*init_signing_date_time)(struct aws_s3_meta_request *meta_request, struct aws_date_time *date_time);
104
105 /* Sign the given request. */
106 void (*sign_request)(
107 struct aws_s3_meta_request *meta_request,
108 struct aws_s3_request *request,
109 aws_signing_complete_fn *on_signing_complete,
110 void *user_data);
111
112 /* Called when any sending of the request is finished, including for each retry. */
113 void (*send_request_finish)(struct aws_s3_connection *connection, struct aws_http_stream *stream, int error_code);
114
115 /* Called when the request is done being sent, and will not be retried/sent again. */
116 void (*finished_request)(struct aws_s3_meta_request *meta_request, struct aws_s3_request *request, int error_code);
117
118 /* Called by the derived meta request when the meta request is completely finished. */
119 void (*finish)(struct aws_s3_meta_request *meta_request);
120
121 /* Handle de-allocation of the meta request. */
122 void (*destroy)(struct aws_s3_meta_request *);
123
124 /* Pause the given request */
125 int (*pause)(struct aws_s3_meta_request *meta_request, struct aws_s3_meta_request_resume_token **resume_token);
126
127 #ifdef AWS_C_S3_ENABLE_TEST_STUBS
128 /********************* TEST ONLY STUB **************************/
129 /* A stub to the update implementation from meta request with the lock held. Only for tests. */
130 bool (*synced_update_stub)(struct aws_s3_meta_request *meta_request);
131 #endif
132 };
133
134 /**
135 * This represents one meta request, ie, one accelerated file transfer. One S3 meta request can represent multiple S3
136 * requests.
137 */
138 struct aws_s3_meta_request {
139 struct aws_allocator *allocator;
140
141 struct aws_ref_count ref_count;
142
143 void *impl;
144
145 struct aws_s3_meta_request_vtable *vtable;
146
147 /* Initial HTTP Message that this meta request is based on. */
148 struct aws_http_message *initial_request_message;
149
150 /* The meta request's outgoing body comes from one of these:
151 * 1) request_body_async_stream: if set, then async stream 1 part at a time
152 * 2) request_body_parallel_stream: if set, then stream multiple parts in parallel
153 * 3) request_body_using_async_writes: if set, then synchronously copy async_write data from 1 part at a time
154 * 4) initial_request_message's body_stream: else synchronously stream parts */
155 struct aws_async_input_stream *request_body_async_stream;
156 struct aws_parallel_input_stream *request_body_parallel_stream;
157 bool request_body_using_async_writes;
158
159 /* Part size to use for uploads and downloads. Passed down by the creating client. */
160 const size_t part_size;
161 /* Hard limit on max connections set through the meta request option. */
162 const uint32_t max_active_connections_override;
163
164 struct aws_cached_signing_config_aws *cached_signing_config;
165
166 /* Client that created this meta request which also processes this request. After the meta request is finished, this
167 * reference is removed.*/
168 struct aws_s3_client *client;
169
170 struct aws_s3_endpoint *endpoint;
171
172 /* Number of requests being sent/received over network for the meta request. */
173 struct aws_atomic_var num_requests_network;
174
175 /* Event loop to schedule IO work related on, ie, reading from streams, streaming parts back to the caller, etc...
176 * After the meta request is finished, this will be reset along with the client reference.*/
177 struct aws_event_loop *io_event_loop;
178
179 /* User data to be passed to each customer specified callback.*/
180 void *user_data;
181
182 /* Customer specified callbacks. */
183 aws_s3_meta_request_headers_callback_fn *headers_callback;
184 aws_s3_meta_request_receive_body_callback_fn *body_callback;
185 aws_s3_meta_request_receive_body_callback_ex_fn *body_callback_ex;
186 aws_s3_meta_request_finish_fn *finish_callback;
187 aws_s3_meta_request_shutdown_fn *shutdown_callback;
188 aws_s3_meta_request_progress_fn *progress_callback;
189 aws_s3_meta_request_telemetry_fn *telemetry_callback;
190 aws_s3_meta_request_upload_review_fn *upload_review_callback;
191
192 enum aws_s3_meta_request_type type;
193 struct aws_string *s3express_session_host;
194 /* Is the meta request made to s3express bucket or not. */
195 bool is_express;
196 /* If the buffer pool optimized for the specific size or not. */
197 bool buffer_pool_optimized;
198
199 /* Track the number of requests being prepared for this meta request. */
200 struct aws_atomic_var num_request_being_prepared;
201
202 struct {
203 struct aws_mutex lock;
204
205 /* Priority queue for pending streaming requests. We use a priority queue to keep parts in order so that we
206 * can stream them to the caller in order. */
207 struct aws_priority_queue pending_body_streaming_requests;
208
209 /* Current state of the meta request. */
210 enum aws_s3_meta_request_state state;
211
212 /* The sum of initial_read_window, plus all window_increment() calls. This number never goes down. */
213 uint64_t read_window_running_total;
214
215 /* The next expected streaming part number needed to continue streaming part bodies. (For example, this will
216 * initially be 1 for part 1, and after that part is received, it will be 2, then 3, etc.. )*/
217 uint32_t next_streaming_part;
218
219 /* Number of parts scheduled for delivery. */
220 uint32_t num_parts_delivery_sent;
221
222 /* Total number of parts that have been attempted to be delivered. (Will equal the sum of succeeded and
223 * failed.)*/
224 uint32_t num_parts_delivery_completed;
225
226 /* Task for delivering events on the meta-request's io_event_loop thread.
227 * We do this to ensure a meta-request's callbacks are fired sequentially and non-overlapping.
228 * If `event_delivery_task_scheduled` is true, then this task is scheduled.
229 * If `event_delivery_active` is true, then this task is actively running.
230 * Delivery is not 100% complete until `event_delivery_array` is empty AND `event_delivery_active` is false
231 * (use aws_s3_meta_request_are_events_out_for_delivery_synced() to check) */
232 struct aws_task event_delivery_task;
233
234 /* Whether or not event delivery is currently scheduled. */
235 uint32_t event_delivery_task_scheduled : 1;
236
237 /* Array of `struct aws_s3_meta_request_event` to deliver when the `event_delivery_task` runs. */
238 struct aws_array_list event_delivery_array;
239
240 /* When true, events are actively being delivered to the user. */
241 bool event_delivery_active;
242
243 /* The end finish result of the meta request. */
244 struct aws_s3_meta_request_result finish_result;
245
246 /* True if the finish result has been set. */
247 uint32_t finish_result_set : 1;
248
249 /* To track aws_s3_requests with cancellable HTTP streams */
250 struct aws_linked_list cancellable_http_streams_list;
251
252 /* To track aws_future_s3_buffers that might need to be cleaned up on cancel */
253 struct aws_linked_list pending_buffer_futures;
254
255 /* Data for async-writes. */
256 struct aws_s3_async_write_data {
257 /* Whether a part request can be sent (we have 1 part's worth of data, or EOF) */
258 bool ready_to_send;
259
260 /* True once user passes `eof` to their final write() call */
261 bool eof;
262
263 /* Holds buffered data we can't immediately send.
264 * The length will always be less than part-size */
265 struct aws_byte_buf buffered_data;
266 struct aws_s3_buffer_ticket *buffered_data_ticket;
267
268 /* Set while we're waiting on the buffer pool to fulfill a (deferred) ticket reservation
269 * for buffered_data_ticket. While set, poll_write() returns result.is_pending. */
270 struct aws_future_s3_buffer_ticket *buffered_data_ticket_future;
271
272 /* Waker callback.
273 * Stored if a poll_write() call returns result.is_pending
274 * (because we already had 1 part's worth of data, or because we're
275 * waiting on the buffer pool to fulfill a ticket reservation).
276 * Invoked when we're ready to accept another poll_write() call. */
277 aws_simple_completion_callback *waker;
278 void *waker_user_data;
279 } async_write;
280
281 } synced_data;
282
283 /* Anything in this structure should only ever be accessed by the client on its process work event loop task. */
284 struct {
285
286 /* Linked list node for the meta requests linked list in the client. */
287 /* Note: this needs to be first for using AWS_CONTAINER_OF with the nested structure. */
288 struct aws_linked_list_node node;
289
290 /* True if this meta request is currently in the client's list. */
291 bool scheduled;
292
293 } client_process_work_threaded_data;
294
295 /* Anything in this structure should only ever be accessed by the meta-request from its io_event_loop thread. */
296 struct {
297 /* When delivering events, we swap contents with `synced_data.event_delivery_array`.
298 * This is an optimization, we could have just copied the array when the task runs,
299 * but swapping two array-lists back and forth avoids an allocation. */
300 struct aws_array_list event_delivery_array;
301
302 /* The range start for the next response body delivery */
303 uint64_t next_deliver_range_start;
304
305 /* Total number of bytes that have been attempted to be delivered. (Will equal the sum of succeeded and
306 * failed.)*/
307 uint64_t num_bytes_delivery_completed;
308
309 /* Priority queue of requests that have acquired their buffer ticket but must wait
310 * for earlier parts to be dispatched for prepare first. This ensures sequential
311 * input stream reads happen in part-number order even when buffer pool reservations
312 * complete out of order.
313 * Note: each element is the pending request, callback to the next state and some user data
314 * (aws_s3_pending_prepare_entry). */
315 struct aws_priority_queue pending_put_prepare_queue;
316
317 /* Next part number that should be dispatched from pending_put_prepare_queue. */
318 uint32_t next_put_part_to_prepare;
319
320 /* Expected offset for the next sequential stream read. Used to detect out-of-order reads. */
321 uint64_t next_read_offset;
322 } io_threaded_data;
323
324 const bool should_compute_content_md5;
325
326 /* deep copy of the checksum config. */
327 struct aws_s3_meta_request_checksum_config_storage checksum_config;
328
329 /* checksum found in either a default get request, or in the initial head request of a multipart get */
330 struct aws_byte_buf meta_request_level_response_header_checksum;
331
332 /* running checksum of all the parts of a default get, or ranged get meta request*/
333 struct aws_s3_checksum *meta_request_level_running_response_sum;
334
335 /* The receiving file handler */
336 FILE *recv_file;
337 struct aws_string *recv_filepath;
338 bool recv_file_delete_on_failure;
339 /* When true, use O_DIRECT for writing received data to file */
340 bool recv_file_direct_io;
341 /* Base file offset for O_DIRECT writes. 0 for CREATE_*, recv_file_position for WRITE_TO_POSITION,
342 * existing file size for CREATE_OR_APPEND. The actual write offset for each part is
343 * base_position + delivery_range_start. Only meaningful when recv_file_direct_io is true. */
344 uint64_t recv_file_direct_io_base_position;
345 /* Counter for how many times we fell back from O_DIRECT to buffered I/O for a single part.
346 * Init-time fallbacks (non-Linux, unaligned part_size, unaligned WRITE_TO_POSITION/APPEND offset)
347 * also increment this counter. The warning is only logged when this transitions from 0,
348 * to avoid log spam. */
349 size_t recv_file_direct_io_fallback_count;
350
351 /* File I/O options. */
352 struct aws_s3_file_io_options fio_opts;
353 };
354
355 /* Entry in the pending_put_prepare_queue. Holds a request that has acquired its buffer
356 * ticket but must wait for earlier parts to dispatch for prepare first. */
357 struct aws_s3_pending_prepare_entry {
358 struct aws_s3_request *request;
359 aws_s3_meta_request_prepare_request_callback_fn *callback;
360 void *user_data;
361 };
362
363 /* Info for each part, that we need to remember until we send CompleteMultipartUpload */
364 struct aws_s3_mpu_part_info {
365 uint64_t size;
366 struct aws_string *etag;
367 struct aws_s3_upload_request_checksum_context *checksum_context;
368 bool was_previously_uploaded;
369 };
370
371 AWS_EXTERN_C_BEGIN
372
373 /* Initialize the base meta request structure. */
374 AWS_S3_API
375 int aws_s3_meta_request_init_base(
376 struct aws_allocator *allocator,
377 struct aws_s3_client *client,
378 size_t part_size,
379 bool should_compute_content_md5,
380 bool should_default_streaming,
381 const struct aws_s3_meta_request_options *options,
382 void *impl,
383 struct aws_s3_meta_request_vtable *vtable,
384 struct aws_s3_meta_request *base_type);
385
386 /* Returns true if the meta request is still in the "active" state. */
387 AWS_S3_API
388 bool aws_s3_meta_request_is_active(struct aws_s3_meta_request *meta_request);
389
390 /* Returns true if the meta request is in the "finished" state. */
391 AWS_S3_API
392 bool aws_s3_meta_request_is_finished(struct aws_s3_meta_request *meta_request);
393
394 /* Returns true if the meta request has a finish result, which indicates that the meta request has trying to finish or
395 * has already finished. */
396 AWS_S3_API
397 bool aws_s3_meta_request_has_finish_result(struct aws_s3_meta_request *meta_request);
398
399 AWS_S3_API
400 void aws_s3_meta_request_lock_synced_data(struct aws_s3_meta_request *meta_request);
401
402 AWS_S3_API
403 void aws_s3_meta_request_unlock_synced_data(struct aws_s3_meta_request *meta_request);
404
405 /* Called by the client to retrieve the next request and update the meta request's internal state. out_request is
406 * optional, and can be NULL if just desiring to update internal state. */
407 AWS_S3_API
408 bool aws_s3_meta_request_update(
409 struct aws_s3_meta_request *meta_request,
410 uint32_t flags,
411 struct aws_s3_request **out_request);
412
413 AWS_S3_API
414 void aws_s3_meta_request_prepare_request(
415 struct aws_s3_meta_request *meta_request,
416 struct aws_s3_request *request,
417 aws_s3_meta_request_prepare_request_callback_fn *callback,
418 void *user_data);
419
420 AWS_S3_API
421 void aws_s3_meta_request_send_request(struct aws_s3_meta_request *meta_request, struct aws_s3_connection *connection);
422
423 AWS_S3_API
424 void aws_s3_meta_request_init_signing_date_time_default(
425 struct aws_s3_meta_request *meta_request,
426 struct aws_date_time *date_time);
427
428 AWS_S3_API
429 void aws_s3_meta_request_sign_request_default_impl(
430 struct aws_s3_meta_request *meta_request,
431 struct aws_s3_request *request,
432 aws_signing_complete_fn *on_signing_complete,
433 void *user_data,
434 bool disable_s3_express_signing);
435
436 AWS_S3_API
437 void aws_s3_meta_request_sign_request_default(
438 struct aws_s3_meta_request *meta_request,
439 struct aws_s3_request *request,
440 aws_signing_complete_fn *on_signing_complete,
441 void *user_data);
442
443 /* Default implementation for when a request finishes a particular send. */
444 AWS_S3_API
445 void aws_s3_meta_request_send_request_finish_default(
446 struct aws_s3_connection *connection,
447 struct aws_http_stream *stream,
448 int error_code);
449
450 /* Called by the client when a request is completely finished and not doing any further retries. */
451 AWS_S3_API
452 void aws_s3_meta_request_finished_request(
453 struct aws_s3_meta_request *meta_request,
454 struct aws_s3_request *request,
455 int error_code);
456
457 /* Called to place the request in the meta request's priority queue for streaming back to the caller. Once all requests
458 * with a part number less than the given request has been received, the given request and the previous requests will
459 * be scheduled for streaming. */
460 AWS_S3_API
461 void aws_s3_meta_request_stream_response_body_synced(
462 struct aws_s3_meta_request *meta_request,
463 struct aws_s3_request *request);
464
465 /* Add an event for delivery on the meta-request's io_event_loop thread.
466 * These events usually correspond to callbacks that must fire sequentially and non-overlapping,
467 * such as delivery of a part's response body. */
468 void aws_s3_meta_request_add_event_for_delivery_synced(
469 struct aws_s3_meta_request *meta_request,
470 const struct aws_s3_meta_request_event *event);
471
472 /* Returns whether any events are out for delivery.
473 * The meta-request's finish callback must not be invoked until this returns false. */
474 bool aws_s3_meta_request_are_events_out_for_delivery_synced(struct aws_s3_meta_request *meta_request);
475
476 /* Cancel the requests with cancellable HTTP stream for the meta request */
477 AWS_S3_API
478 void aws_s3_meta_request_cancel_cancellable_requests_synced(struct aws_s3_meta_request *meta_request, int error_code);
479
480 /* Cancel the pending buffer futures for the meta request */
481 AWS_S3_API
482 void aws_s3_meta_request_cancel_pending_buffer_futures_synced(struct aws_s3_meta_request *meta_request, int error_code);
483
484 /* Asynchronously read from the meta request's input stream. Should always be done outside of any mutex,
485 * as reading from the stream could cause user code to call back into aws-c-s3.
486 * This will fill the buffer to capacity, unless end of stream is reached.
487 * It may read from the underlying stream multiple times, if that's what it takes to fill the buffer.
488 * Returns a future whose result bool indicates whether end of stream was reached.
489 * This future may complete on any thread, and may complete synchronously.
490 *
491 * Read from offset to fill the buffer
492 */
493 AWS_S3_API
494 struct aws_future_bool *aws_s3_meta_request_read_body(
495 struct aws_s3_meta_request *meta_request,
496 uint64_t offset,
497 struct aws_byte_buf *buffer);
498
499 /* Set the meta request finish result as failed. This is meant to be called sometime before aws_s3_meta_request_finish.
500 * Subsequent calls to this function or to aws_s3_meta_request_set_success_synced will not overwrite the end result of
501 * the meta request. */
502 AWS_S3_API
503 void aws_s3_meta_request_set_fail_synced(
504 struct aws_s3_meta_request *meta_request,
505 struct aws_s3_request *failed_request,
506 int error_code);
507
508 /* Set the meta request finish result as successful. This is meant to be called sometime before
509 * aws_s3_meta_request_finish. Subsequent calls this function or to aws_s3_meta_request_set_fail_synced will not
510 * overwrite the end result of the meta request. */
511 AWS_S3_API
512 void aws_s3_meta_request_set_success_synced(struct aws_s3_meta_request *meta_request, int response_status);
513
514 /* Returns true if the finish result has been set (ie: either aws_s3_meta_request_set_fail_synced or
515 * aws_s3_meta_request_set_success_synced have been called.) */
516 AWS_S3_API
517 bool aws_s3_meta_request_has_finish_result_synced(struct aws_s3_meta_request *meta_request);
518
519 /* Virtual function called by the meta request derived type when it's completely finished and there is no other work to
520 * be done. */
521 AWS_S3_API
522 void aws_s3_meta_request_finish(struct aws_s3_meta_request *meta_request);
523
524 /* Default implementation of the meta request finish function. */
525 AWS_S3_API
526 void aws_s3_meta_request_finish_default(struct aws_s3_meta_request *meta_request);
527
528 /* Sets up a meta request result structure. */
529 AWS_S3_API
530 void aws_s3_meta_request_result_setup(
531 struct aws_s3_meta_request *meta_request,
532 struct aws_s3_meta_request_result *result,
533 struct aws_s3_request *failed_request,
534 int response_status,
535 int error_code);
536
537 /* Cleans up a meta request result structure. */
538 AWS_S3_API
539 void aws_s3_meta_request_result_clean_up(
540 struct aws_s3_meta_request *meta_request,
541 struct aws_s3_meta_request_result *result);
542
543 AWS_S3_API
544 bool aws_s3_meta_request_checksum_config_has_algorithm(
545 struct aws_s3_meta_request *meta_request,
546 enum aws_s3_checksum_algorithm algorithm);
547
548 void aws_s3_meta_request_schedule_prepare_request_default_impl(
549 struct aws_s3_meta_request *meta_request,
550 struct aws_s3_request *request,
551 bool parallel,
552 aws_s3_meta_request_prepare_request_callback_fn *callback,
553 void *user_data);
554
555 AWS_EXTERN_C_END
556
557 #endif /* AWS_S3_META_REQUEST_IMPL_H */