Debian Codegraph @

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

raw symbols SCIP
1 #ifndef AWS_S3_CLIENT_IMPL_H
2 #define AWS_S3_CLIENT_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/s3/s3_client.h"
10
11 #include <aws/common/atomics.h>
12 #include <aws/common/byte_buf.h>
13 #include <aws/common/hash_table.h>
14 #include <aws/common/linked_list.h>
15 #include <aws/common/mutex.h>
16 #include <aws/common/ref_count.h>
17 #include <aws/common/task_scheduler.h>
18 #include <aws/http/connection.h>
19 #include <aws/http/connection_manager.h>
20 #include <aws/http/proxy.h>
21
22 /* TODO automate this value in the future to prevent it from becoming out-of-sync. */
23 #define AWS_S3_CLIENT_VERSION "0.1.x"
24
25 struct aws_http_connection;
26 struct aws_http_connection_manager;
27 struct aws_host_resolver;
28 struct aws_s3_endpoint;
29 struct aws_priority_queue;
30
31 enum aws_s3_connection_finish_code {
32 AWS_S3_CONNECTION_FINISH_CODE_SUCCESS,
33 AWS_S3_CONNECTION_FINISH_CODE_FAILED,
34 AWS_S3_CONNECTION_FINISH_CODE_RETRY,
35 };
36
37 /* Callback for the owner of the endpoint when the endpoint has completely cleaned up. */
38 typedef void(aws_s3_endpoint_shutdown_fn)(void *user_data);
39
40 struct aws_s3_endpoint_options {
41 /* URL of the host that this endpoint refers to. */
42 struct aws_string *host_name;
43
44 /* Callback for when this endpoint completely shuts down. */
45 aws_s3_endpoint_shutdown_fn *shutdown_callback;
46
47 /* Bootstrap of the client to be used for spawning a connection manager. */
48 struct aws_client_bootstrap *client_bootstrap;
49
50 /* TLS connection options to be used for the connection manager. */
51 const struct aws_tls_connection_options *tls_connection_options;
52
53 /* DNS TTL to use for addresses for this endpoint. */
54 size_t dns_host_address_ttl_seconds;
55
56 /* Client that owns this endpoint */
57 struct aws_s3_client *client;
58
59 /* Maximum number of connections that can be spawned for this endpoint. */
60 uint32_t max_connections;
61
62 /* HTTP port override. If zero, determine port based on TLS context */
63 uint32_t port;
64
65 /**
66 * Optional.
67 * Proxy configuration for http connection.
68 */
69 struct aws_http_proxy_config *proxy_config;
70
71 /**
72 * Optional.
73 * Configuration for fetching proxy configuration from environment.
74 * By Default proxy_ev_settings.aws_http_proxy_env_var_type is set to AWS_HPEV_ENABLE which means read proxy
75 * configuration from environment.
76 * Only works when proxy_config is not set. If both are set, configuration from proxy_config is used.
77 */
78 struct proxy_env_var_settings *proxy_ev_settings;
79
80 /**
81 * Optional.
82 * If set to 0, default value is used.
83 */
84 uint32_t connect_timeout_ms;
85
86 /**
87 * Optional.
88 * Set keepalive to periodically transmit messages for detecting a disconnected peer.
89 */
90 struct aws_s3_tcp_keep_alive_options *tcp_keep_alive_options;
91
92 /**
93 * Optional.
94 * Configuration options for connection monitoring.
95 * If the transfer speed falls below the specified minimum_throughput_bytes_per_second, the operation is aborted.
96 */
97 struct aws_http_connection_monitoring_options *monitoring_options;
98
99 /* An array of `struct aws_byte_cursor` of network interface names. */
100 const struct aws_byte_cursor *network_interface_names_array;
101 size_t num_network_interface_names;
102 };
103
104 /* global vtable, only used when mocking for tests */
105 struct aws_s3_endpoint_system_vtable {
106 void (*acquire)(struct aws_s3_endpoint *endpoint, bool already_holding_lock);
107 void (*release)(struct aws_s3_endpoint *endpoint);
108 };
109
110 struct aws_s3_endpoint {
111 struct {
112 /* This is NOT an atomic ref-count.
113 * The endpoint lives in hashtable: `aws_s3_client.synced_data.endpoints`
114 * This ref-count can only be touched while holding client's lock */
115 size_t ref_count;
116 } client_synced_data;
117
118 /* What allocator was used to create this endpoint. */
119 struct aws_allocator *allocator;
120
121 /* URL of the host that this endpoint refers to. */
122 struct aws_string *host_name;
123
124 /* Connection manager that manages all connections to this endpoint. */
125 struct aws_http_connection_manager *http_connection_manager;
126
127 /* Client that owns this endpoint */
128 struct aws_s3_client *client;
129 };
130
131 /* Represents one connection on a particular VIP. */
132 struct aws_s3_connection {
133 /* Endpoint that this connection is connected to. */
134 struct aws_s3_endpoint *endpoint;
135
136 /* The underlying, currently in-use HTTP connection. */
137 struct aws_http_connection *http_connection;
138
139 /* Request currently being processed on this connection. */
140 struct aws_s3_request *request;
141
142 /* Current retry token for the request. If it has never been retried, this will be NULL. */
143 struct aws_retry_token *retry_token;
144 };
145
146 struct aws_s3_client_vtable {
147
148 struct aws_s3_meta_request *(
149 *meta_request_factory)(struct aws_s3_client *client, const struct aws_s3_meta_request_options *options);
150
151 void (*create_connection_for_request)(struct aws_s3_client *client, struct aws_s3_request *request);
152
153 void (*acquire_http_connection)(
154 struct aws_http_connection_manager *conn_manager,
155 aws_http_connection_manager_on_connection_setup_fn *on_connection_acquired_callback,
156 void *user_data);
157
158 size_t (*get_host_address_count)(
159 struct aws_host_resolver *host_resolver,
160 const struct aws_string *host_name,
161 uint32_t flags);
162
163 void (*schedule_process_work_synced)(struct aws_s3_client *client);
164
165 void (*process_work)(struct aws_s3_client *client);
166
167 void (*endpoint_shutdown_callback)(struct aws_s3_client *client);
168
169 void (*finish_destroy)(struct aws_s3_client *client);
170
171 struct aws_parallel_input_stream *(*parallel_input_stream_new_from_file)(
172 struct aws_allocator *allocator,
173 struct aws_byte_cursor file_name,
174 struct aws_event_loop_group *reading_elg,
175 bool direct_io_read);
176
177 struct aws_http_stream *(*http_connection_make_request)(
178 struct aws_http_connection *client_connection,
179 const struct aws_http_make_request_options *options);
180
181 #ifdef AWS_C_S3_ENABLE_TEST_STUBS
182 /********************* TEST ONLY STUB **************************/
183 void (*after_prepare_upload_part_finish_stub)(struct aws_s3_request *request, struct aws_http_message *message);
184 #endif
185 };
186
187 struct aws_s3_upload_part_timeout_stats {
188 bool stop_timeout;
189
190 /* Total number of successful upload requests */
191 uint64_t num_successful_upload_requests;
192
193 /* Stats for the request time of first 10 succeed requests */
194 struct {
195 uint64_t sum_ns;
196 uint64_t num_samples;
197 bool collecting_p90;
198 struct aws_priority_queue p90_samples;
199 } initial_request_time;
200
201 /* Track the timeout rate. */
202 struct {
203 uint64_t num_completed;
204 uint64_t num_failed;
205 } timeout_rate_tracking;
206
207 /* Stats for the response to first byte time of tracked succeed requests */
208 struct {
209 uint64_t sum_ns;
210 uint64_t num_samples;
211 } response_to_first_byte_time;
212 };
213
214 /* Represents the state of the S3 client. */
215 struct aws_s3_client {
216 struct aws_allocator *allocator;
217
218 struct aws_s3_buffer_pool *buffer_pool;
219
220 struct aws_s3_client_vtable *vtable;
221
222 struct aws_ref_count ref_count;
223
224 /* Client bootstrap for setting up connection managers. */
225 struct aws_client_bootstrap *client_bootstrap;
226
227 /* Event loop on the client bootstrap ELG for processing work/dispatching requests. */
228 struct aws_event_loop *process_work_event_loop;
229
230 /* Event loop group for streaming request bodies back to the user. */
231 struct aws_event_loop_group *body_streaming_elg;
232
233 /* Region of the S3 bucket. */
234 struct aws_string *region;
235
236 /* Size of parts for files when doing gets or puts. This exists on the client as configurable option that is passed
237 * to meta requests for use. */
238 const size_t part_size;
239
240 bool part_size_set;
241
242 /* Size of parts for files when doing gets or puts. This exists on the client as configurable option that is passed
243 * to meta requests for use. */
244 const uint64_t max_part_size;
245
246 /* Calculated optimal range size for GET operations based on client configuration (memory limits, throughput
247 * targets). This is used when part_size is not explicitly configured, replacing the default with reasonable
248 * calculation. Value is calculated during client initialization and remains constant for the client's lifetime. */
249 const uint64_t optimal_range_size;
250
251 /* File I/O options. */
252 bool fio_options_set;
253 struct aws_s3_file_io_options fio_opts;
254
255 /* The size threshold in bytes for when to use multipart uploads for a AWS_S3_META_REQUEST_TYPE_PUT_OBJECT meta
256 * request. Uploads over this size will automatically use a multipart upload strategy, while uploads smaller or
257 * equal to this threshold will use a single request to upload the whole object. If not set, `part_size` will be
258 * used as threshold. */
259 const uint64_t multipart_upload_threshold;
260
261 /* TLS Options to be used for each connection. */
262 struct aws_tls_connection_options *tls_connection_options;
263
264 /* Cached signing config. Can be NULL if no signing config was specified. */
265 struct aws_cached_signing_config_aws *cached_signing_config;
266
267 /* The auth provider for S3 Express. */
268 aws_s3express_provider_factory_fn *s3express_provider_factory;
269 void *factory_user_data;
270 struct aws_s3express_credentials_provider *s3express_provider;
271
272 /* Throughput target in Gbps that we are trying to reach. */
273 const double throughput_target_gbps;
274
275 /* The calculated ideal number of HTTP connections, based on throughput target and throughput per connection. */
276 const uint32_t ideal_connection_count;
277
278 /**
279 * For multi-part upload, content-md5 will be calculated if the AWS_MR_CONTENT_MD5_ENABLED is specified
280 * or initial request has content-md5 header.
281 * For single-part upload, if the content-md5 header is specified, it will remain unchanged. If the header is not
282 * specified, and this is set to AWS_MR_CONTENT_MD5_ENABLED, it will be calculated. */
283 const enum aws_s3_meta_request_compute_content_md5 compute_content_md5;
284
285 /* Hard limit on max connections set through the client config. */
286 const uint32_t max_active_connections_override;
287
288 struct aws_atomic_var max_allowed_connections;
289
290 /* Retry strategy used for scheduling request retries. */
291 struct aws_retry_strategy *retry_strategy;
292
293 /**
294 * Optional.
295 * Proxy configuration for http connection.
296 */
297 struct aws_http_proxy_config *proxy_config;
298
299 /**
300 * Optional.
301 * Configuration for fetching proxy configuration from environment.
302 * By Default proxy_ev_settings.aws_http_proxy_env_var_type is set to AWS_HPEV_ENABLE which means read proxy
303 * configuration from environment.
304 * Only works when proxy_config is not set. If both are set, configuration from proxy_config is used.
305 */
306 struct proxy_env_var_settings *proxy_ev_settings;
307
308 /**
309 * Optional.
310 * If set to 0, default value is used.
311 */
312 uint32_t connect_timeout_ms;
313
314 /**
315 * Optional.
316 * Set keepalive to periodically transmit messages for detecting a disconnected peer.
317 */
318 struct aws_s3_tcp_keep_alive_options *tcp_keep_alive_options;
319
320 /**
321 * Configuration options for connection monitoring.
322 * If the transfer speed falls below the specified minimum_throughput_bytes_per_second, the operation is aborted.
323 * If user passes in NULL, default values are used.
324 */
325 struct aws_http_connection_monitoring_options monitoring_options;
326
327 /* tls options from proxy environment settings. */
328 struct aws_tls_connection_options *proxy_ev_tls_options;
329
330 /* Shutdown callbacks to notify when the client is completely cleaned up. */
331 aws_s3_client_shutdown_complete_callback_fn *shutdown_callback;
332 void *shutdown_callback_user_data;
333
334 /* Whether read backpressure (aka flow-control window) is being applied. */
335 const bool enable_read_backpressure;
336
337 /* The starting size of each meta request's flow-control window, in bytes.
338 * Ignored unless `enable_read_backpressure` is true. */
339 const size_t initial_read_window;
340
341 /**
342 * Timeout in ms for upload request for request after sending to the response first byte received.
343 */
344 struct aws_atomic_var upload_timeout_ms;
345
346 /*
347 * An aws_array_list<struct aws_string *> of network interface names.
348 */
349 struct aws_array_list network_interface_names;
350 /*
351 * An array of `struct aws_byte_cursor` of network interface names. The cursors are over the strings in
352 * `network_interface_names` so that we can easily pass it to the connection manager without any processing. We need
353 * to create both `struct aws_array_list<struct aws_string *>` and the array of `struct aws_byte_cursor` since byte
354 * cursors are non-owning and we need to ensure that the underlying memory is valid as long as the client lives.
355 */
356 struct aws_byte_cursor *network_interface_names_cursor_array;
357 size_t num_network_interface_names;
358
359 struct {
360 /* Number of overall requests currently being processed by the client. */
361 struct aws_atomic_var num_requests_in_flight;
362
363 /* Number of requests being sent/received over network. */
364 struct aws_atomic_var num_requests_network_io[AWS_S3_META_REQUEST_TYPE_MAX];
365
366 /* Number of requests sitting in their meta request priority queue, waiting to be streamed. */
367 struct aws_atomic_var num_requests_stream_queued_waiting;
368
369 /* Number of requests currently scheduled to be streamed the response body or are actively being streamed. */
370 struct aws_atomic_var num_requests_streaming_response;
371
372 /* Number of overall requests currently streaming the request body instead of buffering. */
373 struct aws_atomic_var num_requests_streaming_request_body;
374 } stats;
375
376 struct {
377 struct aws_mutex lock;
378
379 /* Hash table of endpoints that are in-use by the client.
380 * Key: aws_string of endpoint hostname. Value: aws_s3_endpoint */
381 struct aws_hash_table endpoints;
382
383 /* How many requests failed to be prepared. */
384 uint32_t num_failed_prepare_requests;
385
386 /* Meta requests that need added in the work event loop.
387 * List contains aws_s3_meta_request_work */
388 struct aws_linked_list pending_meta_request_work;
389
390 /* aws_s3_request that are prepared and ready to be put in the threaded_data request queue. */
391 struct aws_linked_list prepared_requests;
392
393 /* Task for processing requests from meta requests on connections. */
394 struct aws_task process_work_task;
395
396 /* Task for trimming buffer bool. */
397 struct aws_task trim_buffer_pool_task;
398
399 /* Task to cleanup endpoints */
400 struct aws_task endpoints_cleanup_task;
401
402 /* Number of endpoints currently allocated. Used during clean up to know how many endpoints are still in
403 * memory.*/
404 uint32_t num_endpoints_allocated;
405
406 /* Whether or not the client has started cleaning up all of its resources */
407 uint32_t active : 1;
408
409 /* True if the start_destroy function is still executing, which blocks shutdown from completing. */
410 uint32_t start_destroy_executing : 1;
411
412 /* Whether or not work processing is currently scheduled. */
413 uint32_t process_work_task_scheduled : 1;
414
415 /* Whether or not work process is currently in progress. */
416 uint32_t process_work_task_in_progress : 1;
417
418 /* Whether or not the body streaming ELG is allocated. If the body streaming ELG is NULL, but this is true, the
419 * shutdown callback has not yet been called.*/
420 uint32_t body_streaming_elg_allocated : 1;
421
422 /* Whether or not a S3 Express provider is active with the client.*/
423 uint32_t s3express_provider_active : 1;
424
425 /* True if client has been flagged to finish destroying itself. Used to catch double-destroy bugs.*/
426 uint32_t finish_destroy : 1;
427
428 /* Whether or not endpoints cleanup task is currently scheduled. */
429 uint32_t endpoints_cleanup_task_scheduled : 1;
430
431 struct aws_s3_upload_part_timeout_stats upload_part_stats;
432 } synced_data;
433
434 struct {
435 /* Queue of prepared aws_s3_request that are waiting to be assigned to connections. */
436 struct aws_linked_list request_queue;
437
438 /* Client list of ongoing aws_s3_meta_requests. */
439 struct aws_linked_list meta_requests;
440
441 /* Number of requests in the request_queue linked_list. */
442 uint32_t request_queue_size;
443
444 /* Number of requests currently being prepared. */
445 uint32_t num_requests_being_prepared;
446
447 /* Whether or not work processing is currently scheduled. */
448 uint32_t trim_buffer_pool_task_scheduled : 1;
449 } threaded_data;
450 };
451
452 struct aws_s3_meta_request_resume_token {
453 struct aws_allocator *allocator;
454 struct aws_ref_count ref_count;
455
456 enum aws_s3_meta_request_type type;
457
458 /* Note: since pause currently only supports upload, this structure only has
459 upload specific fields. Extending it to support other types is left as
460 exercise for future. */
461 struct aws_string *multipart_upload_id;
462 size_t part_size;
463 size_t total_num_parts;
464
465 /* Note: this field is used only when s3 tells us that upload id no longer
466 exists, and if this indicates that all parts have already been uploaded,
467 request is completed instead of failing it.*/
468 size_t num_parts_completed;
469 };
470
471 void aws_s3_client_notify_connection_finished(
472 struct aws_s3_client *client,
473 struct aws_s3_connection *connection,
474 int error_code,
475 enum aws_s3_connection_finish_code finish_code);
476
477 AWS_EXTERN_C_BEGIN
478
479 AWS_S3_API
480 struct aws_s3_meta_request_resume_token *aws_s3_meta_request_resume_token_new(struct aws_allocator *allocator);
481
482 AWS_S3_API
483 void aws_s3_set_dns_ttl(size_t ttl);
484
485 AWS_S3_API
486 uint32_t aws_s3_client_get_max_requests_prepare(struct aws_s3_client *client);
487
488 AWS_S3_API
489 uint32_t aws_s3_client_get_max_active_connections(
490 struct aws_s3_client *client,
491 struct aws_s3_meta_request *meta_request);
492
493 AWS_S3_API
494 uint32_t aws_s3_client_get_max_requests_in_flight(struct aws_s3_client *client);
495
496 AWS_S3_API
497 uint32_t aws_s3_client_queue_requests_threaded(
498 struct aws_s3_client *client,
499 struct aws_linked_list *request_list,
500 bool queue_front);
501
502 AWS_S3_API
503 struct aws_s3_request *aws_s3_client_dequeue_request_threaded(struct aws_s3_client *client);
504
505 AWS_S3_API
506 void aws_s3_client_schedule_process_work(struct aws_s3_client *client);
507
508 AWS_S3_API
509 void aws_s3_client_update_meta_requests_threaded(struct aws_s3_client *client);
510
511 AWS_S3_API
512 void aws_s3_client_update_connections_threaded(struct aws_s3_client *client);
513
514 AWS_S3_API
515 struct aws_s3_endpoint *aws_s3_endpoint_new(
516 struct aws_allocator *allocator,
517 const struct aws_s3_endpoint_options *options);
518
519 AWS_S3_API void aws_s3_client_lock_synced_data(struct aws_s3_client *client);
520
521 AWS_S3_API
522 void aws_s3_client_unlock_synced_data(struct aws_s3_client *client);
523
524 /* Used for mocking */
525 AWS_S3_API
526 void aws_s3_endpoint_set_system_vtable(const struct aws_s3_endpoint_system_vtable *vtable);
527
528 /* Increment the endpoint's ref-count.
529 * If `already_holding_lock` is false, then this call will briefly take hold of the client's lock */
530 struct aws_s3_endpoint *aws_s3_endpoint_acquire(struct aws_s3_endpoint *endpoint, bool already_holding_lock);
531
532 /* Decrement the endpoint's ref-count.
533 * You MUST NOT call this while the client's lock is held.
534 * (this call briefly holds the client's lock and may remove the endpoint
535 * from the client's hashtable) */
536 void aws_s3_endpoint_release(struct aws_s3_endpoint *endpoint);
537
538 /*
539 * Destroys the endpoint. Before calling this function, the endpoint must be removed from the Client's hash table, and
540 * its ref count must be zero. You MUST NOT call this while the client's lock is held.
541 */
542 void aws_s3_endpoint_destroy(struct aws_s3_endpoint *endpoint);
543
544 AWS_S3_API
545 extern const uint32_t g_min_num_connections;
546
547 AWS_S3_API
548 extern const size_t g_expect_timeout_offset_ms;
549
550 AWS_S3_API
551 void aws_s3_client_update_upload_part_timeout(
552 struct aws_s3_client *client,
553 struct aws_s3_request *finished_upload_part_request,
554 int finished_error_code);
555
556 AWS_EXTERN_C_END
557
558 #endif /* AWS_S3_CLIENT_IMPL_H */