-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy paths3_client.c
2542 lines (2071 loc) · 101 KB
/
s3_client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include "aws/s3/private/s3_auto_ranged_get.h"
#include "aws/s3/private/s3_auto_ranged_put.h"
#include "aws/s3/private/s3_buffer_pool.h"
#include "aws/s3/private/s3_client_impl.h"
#include "aws/s3/private/s3_copy_object.h"
#include "aws/s3/private/s3_default_meta_request.h"
#include "aws/s3/private/s3_meta_request_impl.h"
#include "aws/s3/private/s3_parallel_input_stream.h"
#include "aws/s3/private/s3_request_messages.h"
#include "aws/s3/private/s3_util.h"
#include "aws/s3/private/s3express_credentials_provider_impl.h"
#include "aws/s3/s3express_credentials_provider.h"
#include <aws/auth/credentials.h>
#include <aws/common/assert.h>
#include <aws/common/atomics.h>
#include <aws/common/clock.h>
#include <aws/common/device_random.h>
#include <aws/common/json.h>
#include <aws/common/string.h>
#include <aws/common/system_info.h>
#include <aws/http/connection.h>
#include <aws/http/connection_manager.h>
#include <aws/http/proxy.h>
#include <aws/http/request_response.h>
#include <aws/io/channel_bootstrap.h>
#include <aws/io/event_loop.h>
#include <aws/io/host_resolver.h>
#include <aws/io/retry_strategy.h>
#include <aws/io/socket.h>
#include <aws/io/stream.h>
#include <aws/io/tls_channel_handler.h>
#include <aws/io/uri.h>
#include <inttypes.h>
#include <math.h>
#ifdef _MSC_VER
# pragma warning(disable : 4232) /* function pointer to dll symbol */
#endif /* _MSC_VER */
struct aws_s3_meta_request_work {
struct aws_linked_list_node node;
struct aws_s3_meta_request *meta_request;
};
static const enum aws_log_level s_log_level_client_stats = AWS_LL_INFO;
static const uint32_t s_max_requests_multiplier = 4;
/* TODO Provide analysis on origins of this value. */
static const double s_throughput_per_vip_gbps = 4.0;
/* Preferred amount of active connections per meta request type. */
const uint32_t g_num_conns_per_vip_meta_request_look_up[AWS_S3_META_REQUEST_TYPE_MAX] = {
10, /* AWS_S3_META_REQUEST_TYPE_DEFAULT */
10, /* AWS_S3_META_REQUEST_TYPE_GET_OBJECT */
10, /* AWS_S3_META_REQUEST_TYPE_PUT_OBJECT */
10 /* AWS_S3_META_REQUEST_TYPE_COPY_OBJECT */
};
/* Should be max of s_num_conns_per_vip_meta_request_look_up */
const uint32_t g_max_num_connections_per_vip = 10;
/**
* Default part size is 8 MiB to reach the best performance from the experiments we had.
* Default max part size is 5GiB as the server limit. Object size limit is 5TiB for now.
* max number of upload parts is 10000.
* TODO Provide more information on other values.
*/
static const size_t s_default_part_size = 8 * 1024 * 1024;
static const uint64_t s_default_max_part_size = 5368709120ULL;
static const double s_default_throughput_target_gbps = 10.0;
static const uint32_t s_default_max_retries = 5;
static size_t s_dns_host_address_ttl_seconds = 5 * 60;
/* Default time until a connection is declared dead, while handling a request but seeing no activity.
* 30 seconds mirrors the value currently used by the Java SDK. */
static const uint32_t s_default_throughput_failure_interval_seconds = 30;
/* Amount of time spent idling before trimming buffer. */
static const size_t s_buffer_pool_trim_time_offset_in_s = 5;
/* Called when ref count is 0. */
static void s_s3_client_start_destroy(void *user_data);
/* Called by s_s3_client_process_work_default when all shutdown criteria has been met. */
static void s_s3_client_finish_destroy_default(struct aws_s3_client *client);
/* Called when the body streaming elg shutdown has completed. */
static void s_s3_client_body_streaming_elg_shutdown(void *user_data);
static void s_s3_client_create_connection_for_request(struct aws_s3_client *client, struct aws_s3_request *request);
/* Callback which handles the HTTP connection retrieved by acquire_http_connection. */
static void s_s3_client_on_acquire_http_connection(
struct aws_http_connection *http_connection,
int error_code,
void *user_data);
static void s_s3_client_push_meta_request_synced(
struct aws_s3_client *client,
struct aws_s3_meta_request *meta_request);
/* Schedule task for processing work. (Calls the corresponding vtable function.) */
static void s_s3_client_schedule_process_work_synced(struct aws_s3_client *client);
/* Default implementation for scheduling processing of work. */
static void s_s3_client_schedule_process_work_synced_default(struct aws_s3_client *client);
/* Actual task function that processes work. */
static void s_s3_client_process_work_task(struct aws_task *task, void *arg, enum aws_task_status task_status);
static void s_s3_client_process_work_default(struct aws_s3_client *client);
static void s_s3_client_endpoint_shutdown_callback(struct aws_s3_client *client);
/* Default factory function for creating a meta request. */
static struct aws_s3_meta_request *s_s3_client_meta_request_factory_default(
struct aws_s3_client *client,
const struct aws_s3_meta_request_options *options);
static struct aws_s3_client_vtable s_s3_client_default_vtable = {
.meta_request_factory = s_s3_client_meta_request_factory_default,
.acquire_http_connection = aws_http_connection_manager_acquire_connection,
.get_host_address_count = aws_host_resolver_get_host_address_count,
.schedule_process_work_synced = s_s3_client_schedule_process_work_synced_default,
.process_work = s_s3_client_process_work_default,
.endpoint_shutdown_callback = s_s3_client_endpoint_shutdown_callback,
.finish_destroy = s_s3_client_finish_destroy_default,
.parallel_input_stream_new_from_file = aws_parallel_input_stream_new_from_file,
};
void aws_s3_set_dns_ttl(size_t ttl) {
s_dns_host_address_ttl_seconds = ttl;
}
/* Returns the max number of connections allowed.
*
* When meta request is NULL, this will return the overall allowed number of connections.
*
* If meta_request is not NULL, this will give the max number of connections allowed for that meta request type on
* that endpoint.
*/
uint32_t aws_s3_client_get_max_active_connections(
struct aws_s3_client *client,
struct aws_s3_meta_request *meta_request) {
AWS_PRECONDITION(client);
uint32_t num_connections_per_vip = g_max_num_connections_per_vip;
uint32_t num_vips = client->ideal_vip_count;
if (meta_request != NULL) {
num_connections_per_vip = g_num_conns_per_vip_meta_request_look_up[meta_request->type];
struct aws_s3_endpoint *endpoint = meta_request->endpoint;
AWS_ASSERT(endpoint != NULL);
AWS_ASSERT(client->vtable->get_host_address_count);
size_t num_known_vips = client->vtable->get_host_address_count(
client->client_bootstrap->host_resolver, endpoint->host_name, AWS_GET_HOST_ADDRESS_COUNT_RECORD_TYPE_A);
/* If the number of known vips is less than our ideal VIP count, clamp it. */
if (num_known_vips < (size_t)num_vips) {
num_vips = (uint32_t)num_known_vips;
}
}
/* We always want to allow for at least one VIP worth of connections. */
if (num_vips == 0) {
num_vips = 1;
}
uint32_t max_active_connections = num_vips * num_connections_per_vip;
if (client->max_active_connections_override > 0 &&
client->max_active_connections_override < max_active_connections) {
max_active_connections = client->max_active_connections_override;
}
return max_active_connections;
}
/* Returns the max number of requests allowed to be in memory */
uint32_t aws_s3_client_get_max_requests_in_flight(struct aws_s3_client *client) {
AWS_PRECONDITION(client);
return aws_s3_client_get_max_active_connections(client, NULL) * s_max_requests_multiplier;
}
/* Returns the max number of requests that should be in preparation stage (ie: reading from a stream, being signed,
* etc.) */
uint32_t aws_s3_client_get_max_requests_prepare(struct aws_s3_client *client) {
return aws_s3_client_get_max_active_connections(client, NULL);
}
static uint32_t s_s3_client_get_num_requests_network_io(
struct aws_s3_client *client,
enum aws_s3_meta_request_type meta_request_type) {
AWS_PRECONDITION(client);
uint32_t num_requests_network_io = 0;
if (meta_request_type == AWS_S3_META_REQUEST_TYPE_MAX) {
for (uint32_t i = 0; i < AWS_S3_META_REQUEST_TYPE_MAX; ++i) {
num_requests_network_io += (uint32_t)aws_atomic_load_int(&client->stats.num_requests_network_io[i]);
}
} else {
num_requests_network_io =
(uint32_t)aws_atomic_load_int(&client->stats.num_requests_network_io[meta_request_type]);
}
return num_requests_network_io;
}
void aws_s3_client_lock_synced_data(struct aws_s3_client *client) {
aws_mutex_lock(&client->synced_data.lock);
}
void aws_s3_client_unlock_synced_data(struct aws_s3_client *client) {
aws_mutex_unlock(&client->synced_data.lock);
}
static void s_s3express_provider_finish_destroy(void *user_data) {
struct aws_s3_client *client = user_data;
AWS_PRECONDITION(client);
/* BEGIN CRITICAL SECTION */
{
aws_s3_client_lock_synced_data(client);
client->synced_data.s3express_provider_active = false;
/* Schedule the work task to call s_s3_client_finish_destroy function if
* everything cleaning up asynchronously has finished. */
s_s3_client_schedule_process_work_synced(client);
aws_s3_client_unlock_synced_data(client);
}
/* END CRITICAL SECTION */
}
struct aws_s3express_credentials_provider *s_s3express_provider_default_factory(
struct aws_allocator *allocator,
struct aws_s3_client *client,
aws_simple_completion_callback on_provider_shutdown_callback,
void *shutdown_user_data,
void *factory_user_data) {
(void)factory_user_data;
struct aws_s3express_credentials_provider_default_options options = {
.client = client,
.shutdown_complete_callback = on_provider_shutdown_callback,
.shutdown_user_data = shutdown_user_data,
};
struct aws_s3express_credentials_provider *s3express_provider =
aws_s3express_credentials_provider_new_default(allocator, &options);
return s3express_provider;
}
struct aws_s3_client *aws_s3_client_new(
struct aws_allocator *allocator,
const struct aws_s3_client_config *client_config) {
AWS_PRECONDITION(allocator);
AWS_PRECONDITION(client_config);
if (client_config->client_bootstrap == NULL) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"Cannot create client from client_config; client_bootstrap provided in options is invalid.");
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
/* Cannot be less than zero. If zero, use default. */
if (client_config->throughput_target_gbps < 0.0) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"Cannot create client from client_config; throughput_target_gbps cannot less than or equal to 0.");
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
if (client_config->signing_config == NULL) {
AWS_LOGF_ERROR(AWS_LS_S3_CLIENT, "Cannot create client from client_config; signing_config is required.");
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
if (client_config->signing_config->credentials == NULL &&
client_config->signing_config->credentials_provider == NULL) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"Cannot create client from client_config; Invalid signing_config provided, either credentials or "
"credentials provider has to be set.");
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
if (!client_config->enable_s3express &&
client_config->signing_config->algorithm == AWS_SIGNING_ALGORITHM_V4_S3EXPRESS) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"Cannot create client from client_config; Client config is set use S3 Express signing, but S3 Express "
"support is "
"not configured.");
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
#ifdef BYO_CRYPTO
if (client_config->tls_mode == AWS_MR_TLS_ENABLED && client_config->tls_connection_options == NULL) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"Cannot create client from client_config; when using BYO_CRYPTO, tls_connection_options can not be "
"NULL when TLS is enabled.");
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
#endif
struct aws_s3_client *client = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_client));
client->allocator = allocator;
size_t mem_limit = 0;
if (client_config->memory_limit_in_bytes == 0) {
#if SIZE_BITS == 32
if (client_config->throughput_target_gbps > 25.0) {
mem_limit = GB_TO_BYTES(2);
} else {
mem_limit = GB_TO_BYTES(1);
}
#else
if (client_config->throughput_target_gbps > 75.0) {
mem_limit = GB_TO_BYTES(8);
} else if (client_config->throughput_target_gbps > 25.0) {
mem_limit = GB_TO_BYTES(4);
} else {
mem_limit = GB_TO_BYTES(2);
}
#endif
} else {
// cap memory limit to SIZE_MAX
if (client_config->memory_limit_in_bytes > SIZE_MAX) {
mem_limit = SIZE_MAX;
} else {
mem_limit = (size_t)client_config->memory_limit_in_bytes;
}
}
size_t part_size = s_default_part_size;
if (client_config->part_size != 0) {
if (client_config->part_size > SIZE_MAX) {
part_size = SIZE_MAX;
} else {
part_size = (size_t)client_config->part_size;
}
}
client->buffer_pool = aws_s3_buffer_pool_new(allocator, part_size, mem_limit);
if (client->buffer_pool == NULL) {
goto on_early_fail;
}
struct aws_s3_buffer_pool_usage_stats pool_usage = aws_s3_buffer_pool_get_usage(client->buffer_pool);
if (client_config->max_part_size > pool_usage.mem_limit) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"Cannot create client from client_config; configured max part size should not exceed memory limit."
"size.");
aws_raise_error(AWS_ERROR_S3_INVALID_MEMORY_LIMIT_CONFIG);
goto on_early_fail;
}
client->vtable = &s_s3_client_default_vtable;
aws_ref_count_init(&client->ref_count, client, (aws_simple_completion_callback *)s_s3_client_start_destroy);
if (aws_mutex_init(&client->synced_data.lock) != AWS_OP_SUCCESS) {
goto on_early_fail;
}
aws_linked_list_init(&client->synced_data.pending_meta_request_work);
aws_linked_list_init(&client->synced_data.prepared_requests);
aws_linked_list_init(&client->threaded_data.meta_requests);
aws_linked_list_init(&client->threaded_data.request_queue);
aws_atomic_init_int(&client->stats.num_requests_in_flight, 0);
for (uint32_t i = 0; i < (uint32_t)AWS_S3_META_REQUEST_TYPE_MAX; ++i) {
aws_atomic_init_int(&client->stats.num_requests_network_io[i], 0);
}
aws_atomic_init_int(&client->stats.num_requests_stream_queued_waiting, 0);
aws_atomic_init_int(&client->stats.num_requests_streaming_response, 0);
*((uint32_t *)&client->max_active_connections_override) = client_config->max_active_connections_override;
/* Store our client bootstrap. */
client->client_bootstrap = aws_client_bootstrap_acquire(client_config->client_bootstrap);
struct aws_event_loop_group *event_loop_group = client_config->client_bootstrap->event_loop_group;
aws_event_loop_group_acquire(event_loop_group);
client->process_work_event_loop = aws_event_loop_group_get_next_loop(event_loop_group);
/* Make a copy of the region string. */
client->region = aws_string_new_from_array(allocator, client_config->region.ptr, client_config->region.len);
*((size_t *)&client->part_size) = part_size;
if (client_config->max_part_size != 0) {
*((uint64_t *)&client->max_part_size) = client_config->max_part_size;
} else {
*((uint64_t *)&client->max_part_size) = s_default_max_part_size;
}
if (client_config->max_part_size > pool_usage.mem_limit) {
*((uint64_t *)&client->max_part_size) = pool_usage.mem_limit;
}
if (client->max_part_size > SIZE_MAX) {
/* For the 32bit max part size to be SIZE_MAX */
*((uint64_t *)&client->max_part_size) = SIZE_MAX;
}
if (client_config->multipart_upload_threshold != 0) {
*((uint64_t *)&client->multipart_upload_threshold) = client_config->multipart_upload_threshold;
} else {
*((uint64_t *)&client->multipart_upload_threshold) =
part_size > g_s3_min_upload_part_size ? part_size : g_s3_min_upload_part_size;
}
if (client_config->max_part_size < client_config->part_size) {
*((uint64_t *)&client_config->max_part_size) = client_config->part_size;
}
client->connect_timeout_ms = client_config->connect_timeout_ms;
if (client_config->proxy_ev_settings) {
client->proxy_ev_settings = aws_mem_calloc(allocator, 1, sizeof(struct proxy_env_var_settings));
*client->proxy_ev_settings = *client_config->proxy_ev_settings;
if (client_config->proxy_ev_settings->tls_options) {
client->proxy_ev_tls_options = aws_mem_calloc(allocator, 1, sizeof(struct aws_tls_connection_options));
if (aws_tls_connection_options_copy(client->proxy_ev_tls_options, client->proxy_ev_settings->tls_options)) {
goto on_error;
}
client->proxy_ev_settings->tls_options = client->proxy_ev_tls_options;
}
}
if (client_config->tcp_keep_alive_options) {
client->tcp_keep_alive_options = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_tcp_keep_alive_options));
*client->tcp_keep_alive_options = *client_config->tcp_keep_alive_options;
}
if (client_config->monitoring_options) {
client->monitoring_options = *client_config->monitoring_options;
} else {
client->monitoring_options.minimum_throughput_bytes_per_second = 1;
client->monitoring_options.allowable_throughput_failure_interval_seconds =
s_default_throughput_failure_interval_seconds;
}
if (client_config->tls_mode == AWS_MR_TLS_ENABLED) {
client->tls_connection_options =
aws_mem_calloc(client->allocator, 1, sizeof(struct aws_tls_connection_options));
if (client_config->tls_connection_options != NULL) {
aws_tls_connection_options_copy(client->tls_connection_options, client_config->tls_connection_options);
} else {
#ifdef BYO_CRYPTO
AWS_FATAL_ASSERT(false);
goto on_error;
#else
struct aws_tls_ctx_options default_tls_ctx_options;
AWS_ZERO_STRUCT(default_tls_ctx_options);
aws_tls_ctx_options_init_default_client(&default_tls_ctx_options, allocator);
struct aws_tls_ctx *default_tls_ctx = aws_tls_client_ctx_new(allocator, &default_tls_ctx_options);
if (default_tls_ctx == NULL) {
goto on_error;
}
aws_tls_connection_options_init_from_ctx(client->tls_connection_options, default_tls_ctx);
aws_tls_ctx_release(default_tls_ctx);
aws_tls_ctx_options_clean_up(&default_tls_ctx_options);
#endif
}
}
if (client_config->proxy_options) {
client->proxy_config = aws_http_proxy_config_new_from_proxy_options_with_tls_info(
allocator, client_config->proxy_options, client_config->tls_mode == AWS_MR_TLS_ENABLED);
if (client->proxy_config == NULL) {
goto on_error;
}
}
/* Set up body streaming ELG */
{
uint16_t num_event_loops =
(uint16_t)aws_array_list_length(&client->client_bootstrap->event_loop_group->event_loops);
uint16_t num_streaming_threads = num_event_loops;
if (num_streaming_threads < 1) {
num_streaming_threads = 1;
}
struct aws_shutdown_callback_options body_streaming_elg_shutdown_options = {
.shutdown_callback_fn = s_s3_client_body_streaming_elg_shutdown,
.shutdown_callback_user_data = client,
};
client->body_streaming_elg = aws_event_loop_group_new_default(
client->allocator, num_streaming_threads, &body_streaming_elg_shutdown_options);
if (!client->body_streaming_elg) {
/* Fail to create elg, we should fail the call */
goto on_error;
}
client->synced_data.body_streaming_elg_allocated = true;
}
/* Setup cannot fail after this point. */
if (client_config->throughput_target_gbps != 0.0) {
*((double *)&client->throughput_target_gbps) = client_config->throughput_target_gbps;
} else {
*((double *)&client->throughput_target_gbps) = s_default_throughput_target_gbps;
}
*((enum aws_s3_meta_request_compute_content_md5 *)&client->compute_content_md5) =
client_config->compute_content_md5;
/* Determine how many vips are ideal by dividing target-throughput by throughput-per-vip. */
{
double ideal_vip_count_double = client->throughput_target_gbps / s_throughput_per_vip_gbps;
*((uint32_t *)&client->ideal_vip_count) = (uint32_t)ceil(ideal_vip_count_double);
}
client->cached_signing_config = aws_cached_signing_config_new(client, client_config->signing_config);
if (client_config->enable_s3express) {
if (client_config->s3express_provider_override_factory) {
client->s3express_provider_factory = client_config->s3express_provider_override_factory;
client->factory_user_data = client_config->factory_user_data;
} else {
client->s3express_provider_factory = s_s3express_provider_default_factory;
}
}
client->synced_data.active = true;
if (client_config->retry_strategy != NULL) {
aws_retry_strategy_acquire(client_config->retry_strategy);
client->retry_strategy = client_config->retry_strategy;
} else {
struct aws_exponential_backoff_retry_options backoff_retry_options = {
.el_group = client_config->client_bootstrap->event_loop_group,
.max_retries = s_default_max_retries,
};
struct aws_standard_retry_options retry_options = {
.backoff_retry_options = backoff_retry_options,
};
client->retry_strategy = aws_retry_strategy_new_standard(allocator, &retry_options);
}
aws_hash_table_init(
&client->synced_data.endpoints,
client->allocator,
10,
aws_hash_string,
aws_hash_callback_string_eq,
aws_hash_callback_string_destroy,
NULL);
/* Initialize shutdown options and tracking. */
client->shutdown_callback = client_config->shutdown_callback;
client->shutdown_callback_user_data = client_config->shutdown_callback_user_data;
*((bool *)&client->enable_read_backpressure) = client_config->enable_read_backpressure;
*((size_t *)&client->initial_read_window) = client_config->initial_read_window;
return client;
on_error:
aws_string_destroy(client->region);
if (client->tls_connection_options) {
aws_tls_connection_options_clean_up(client->tls_connection_options);
aws_mem_release(client->allocator, client->tls_connection_options);
client->tls_connection_options = NULL;
}
if (client->proxy_config) {
aws_http_proxy_config_destroy(client->proxy_config);
}
if (client->proxy_ev_tls_options) {
aws_tls_connection_options_clean_up(client->proxy_ev_tls_options);
aws_mem_release(client->allocator, client->proxy_ev_tls_options);
client->proxy_ev_settings->tls_options = NULL;
}
aws_mem_release(client->allocator, client->proxy_ev_settings);
aws_mem_release(client->allocator, client->tcp_keep_alive_options);
aws_event_loop_group_release(client->client_bootstrap->event_loop_group);
aws_client_bootstrap_release(client->client_bootstrap);
aws_mutex_clean_up(&client->synced_data.lock);
on_early_fail:
aws_mem_release(client->allocator, client);
return NULL;
}
struct aws_s3_client *aws_s3_client_acquire(struct aws_s3_client *client) {
AWS_PRECONDITION(client);
aws_ref_count_acquire(&client->ref_count);
return client;
}
struct aws_s3_client *aws_s3_client_release(struct aws_s3_client *client) {
if (client != NULL) {
aws_ref_count_release(&client->ref_count);
}
return NULL;
}
static void s_s3_client_start_destroy(void *user_data) {
struct aws_s3_client *client = user_data;
AWS_PRECONDITION(client);
AWS_LOGF_DEBUG(AWS_LS_S3_CLIENT, "id=%p Client starting destruction.", (void *)client);
struct aws_linked_list local_vip_list;
aws_linked_list_init(&local_vip_list);
/* BEGIN CRITICAL SECTION */
{
aws_s3_client_lock_synced_data(client);
client->synced_data.active = false;
/* Prevent the client from cleaning up in between the mutex unlock/re-lock below.*/
client->synced_data.start_destroy_executing = true;
aws_s3_client_unlock_synced_data(client);
}
/* END CRITICAL SECTION */
aws_event_loop_group_release(client->body_streaming_elg);
client->body_streaming_elg = NULL;
aws_s3express_credentials_provider_release(client->s3express_provider);
/* BEGIN CRITICAL SECTION */
{
aws_s3_client_lock_synced_data(client);
client->synced_data.start_destroy_executing = false;
/* Schedule the work task to clean up outstanding connections and to call s_s3_client_finish_destroy function if
* everything cleaning up asynchronously has finished. */
s_s3_client_schedule_process_work_synced(client);
aws_s3_client_unlock_synced_data(client);
}
/* END CRITICAL SECTION */
}
static void s_s3_client_finish_destroy_default(struct aws_s3_client *client) {
AWS_PRECONDITION(client);
AWS_LOGF_DEBUG(AWS_LS_S3_CLIENT, "id=%p Client finishing destruction.", (void *)client);
if (client->threaded_data.trim_buffer_pool_task_scheduled) {
aws_event_loop_cancel_task(client->process_work_event_loop, &client->synced_data.trim_buffer_pool_task);
}
aws_string_destroy(client->region);
client->region = NULL;
if (client->tls_connection_options) {
aws_tls_connection_options_clean_up(client->tls_connection_options);
aws_mem_release(client->allocator, client->tls_connection_options);
client->tls_connection_options = NULL;
}
if (client->proxy_config) {
aws_http_proxy_config_destroy(client->proxy_config);
}
if (client->proxy_ev_tls_options) {
aws_tls_connection_options_clean_up(client->proxy_ev_tls_options);
aws_mem_release(client->allocator, client->proxy_ev_tls_options);
client->proxy_ev_settings->tls_options = NULL;
}
aws_mem_release(client->allocator, client->proxy_ev_settings);
aws_mem_release(client->allocator, client->tcp_keep_alive_options);
aws_mutex_clean_up(&client->synced_data.lock);
AWS_ASSERT(aws_linked_list_empty(&client->synced_data.pending_meta_request_work));
AWS_ASSERT(aws_linked_list_empty(&client->threaded_data.meta_requests));
aws_hash_table_clean_up(&client->synced_data.endpoints);
aws_retry_strategy_release(client->retry_strategy);
aws_event_loop_group_release(client->client_bootstrap->event_loop_group);
aws_client_bootstrap_release(client->client_bootstrap);
aws_cached_signing_config_destroy(client->cached_signing_config);
aws_s3_client_shutdown_complete_callback_fn *shutdown_callback = client->shutdown_callback;
void *shutdown_user_data = client->shutdown_callback_user_data;
aws_s3_buffer_pool_destroy(client->buffer_pool);
aws_mem_release(client->allocator, client);
client = NULL;
if (shutdown_callback != NULL) {
shutdown_callback(shutdown_user_data);
}
}
static void s_s3_client_body_streaming_elg_shutdown(void *user_data) {
struct aws_s3_client *client = user_data;
AWS_PRECONDITION(client);
AWS_LOGF_DEBUG(AWS_LS_S3_CLIENT, "id=%p Client body streaming ELG shutdown.", (void *)client);
/* BEGIN CRITICAL SECTION */
{
aws_s3_client_lock_synced_data(client);
client->synced_data.body_streaming_elg_allocated = false;
s_s3_client_schedule_process_work_synced(client);
aws_s3_client_unlock_synced_data(client);
}
/* END CRITICAL SECTION */
}
uint32_t aws_s3_client_queue_requests_threaded(
struct aws_s3_client *client,
struct aws_linked_list *request_list,
bool queue_front) {
AWS_PRECONDITION(client);
AWS_PRECONDITION(request_list);
if (aws_linked_list_empty(request_list)) {
return 0;
}
uint32_t request_list_size = 0;
for (struct aws_linked_list_node *node = aws_linked_list_begin(request_list);
node != aws_linked_list_end(request_list);
node = aws_linked_list_next(node)) {
++request_list_size;
}
if (queue_front) {
aws_linked_list_move_all_front(&client->threaded_data.request_queue, request_list);
} else {
aws_linked_list_move_all_back(&client->threaded_data.request_queue, request_list);
}
client->threaded_data.request_queue_size += request_list_size;
return request_list_size;
}
struct aws_s3_request *aws_s3_client_dequeue_request_threaded(struct aws_s3_client *client) {
AWS_PRECONDITION(client);
if (aws_linked_list_empty(&client->threaded_data.request_queue)) {
return NULL;
}
struct aws_linked_list_node *request_node = aws_linked_list_pop_front(&client->threaded_data.request_queue);
struct aws_s3_request *request = AWS_CONTAINER_OF(request_node, struct aws_s3_request, node);
--client->threaded_data.request_queue_size;
return request;
}
/*
* There is currently some overlap between user provided Host header and endpoint
* override. This function handles the corner cases for when either or both are provided.
*/
int s_apply_endpoint_override(
const struct aws_s3_client *client,
struct aws_http_headers *message_headers,
const struct aws_uri *endpoint) {
AWS_PRECONDITION(message_headers);
const struct aws_byte_cursor *endpoint_authority = endpoint == NULL ? NULL : aws_uri_authority(endpoint);
if (!aws_http_headers_has(message_headers, g_host_header_name)) {
if (endpoint_authority == NULL) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"id=%p Cannot create meta s3 request; message provided in options does not have either 'Host' header "
"set or endpoint override.",
(void *)client);
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
if (aws_http_headers_set(message_headers, g_host_header_name, *endpoint_authority)) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"id=%p Cannot create meta s3 request; failed to set 'Host' header based on endpoint override.",
(void *)client);
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
}
struct aws_byte_cursor host_value;
AWS_FATAL_ASSERT(aws_http_headers_get(message_headers, g_host_header_name, &host_value) == AWS_OP_SUCCESS);
if (endpoint_authority != NULL && !aws_byte_cursor_eq(&host_value, endpoint_authority)) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"id=%p Cannot create meta s3 request; host header value " PRInSTR
" does not match endpoint override " PRInSTR,
(void *)client,
AWS_BYTE_CURSOR_PRI(host_value),
AWS_BYTE_CURSOR_PRI(*endpoint_authority));
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
return AWS_OP_SUCCESS;
}
/* Public facing make-meta-request function. */
struct aws_s3_meta_request *aws_s3_client_make_meta_request(
struct aws_s3_client *client,
const struct aws_s3_meta_request_options *options) {
AWS_LOGF_INFO(AWS_LS_S3_CLIENT, "id=%p Initiating making of meta request", (void *)client);
AWS_PRECONDITION(client);
AWS_PRECONDITION(client->vtable);
AWS_PRECONDITION(client->vtable->meta_request_factory);
AWS_PRECONDITION(options);
bool use_s3express_signing = false;
if (options->signing_config != NULL) {
use_s3express_signing = options->signing_config->algorithm == AWS_SIGNING_ALGORITHM_V4_S3EXPRESS;
} else if (client->cached_signing_config) {
use_s3express_signing = client->cached_signing_config->config.algorithm == AWS_SIGNING_ALGORITHM_V4_S3EXPRESS;
}
if (options->type >= AWS_S3_META_REQUEST_TYPE_MAX) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"id=%p Cannot create meta s3 request; invalid meta request type specified.",
(void *)client);
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
if (options->message == NULL) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"id=%p Cannot create meta s3 request; message provided in options is invalid.",
(void *)client);
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
if (use_s3express_signing && client->s3express_provider_factory == NULL) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"id=%p Cannot create meta s3 request; client doesn't support S3 Express signing.",
(void *)client);
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
struct aws_http_headers *message_headers = aws_http_message_get_headers(options->message);
if (message_headers == NULL) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"id=%p Cannot create meta s3 request; message provided in options does not contain headers.",
(void *)client);
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
if (options->checksum_config) {
if (options->checksum_config->location == AWS_SCL_TRAILER) {
struct aws_http_headers *headers = aws_http_message_get_headers(options->message);
struct aws_byte_cursor existing_encoding;
AWS_ZERO_STRUCT(existing_encoding);
if (aws_http_headers_get(headers, g_content_encoding_header_name, &existing_encoding) == AWS_OP_SUCCESS) {
if (aws_byte_cursor_find_exact(&existing_encoding, &g_content_encoding_header_aws_chunked, NULL) ==
AWS_OP_SUCCESS) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"id=%p Cannot create meta s3 request; for trailer checksum, the original request cannot be "
"aws-chunked encoding. The client will encode the request instead.",
(void *)client);
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
}
}
if (options->checksum_config->location == AWS_SCL_HEADER) {
/* TODO: support calculate checksum to add to header */
aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
return NULL;
}
if (options->checksum_config->location != AWS_SCL_NONE &&
options->checksum_config->checksum_algorithm == AWS_SCA_NONE) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"id=%p Cannot create meta s3 request; checksum location is set, but no checksum algorithm selected.",
(void *)client);
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
if (options->checksum_config->checksum_algorithm != AWS_SCA_NONE &&
options->checksum_config->location == AWS_SCL_NONE) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"id=%p Cannot create meta s3 request; checksum algorithm is set, but no checksum location selected.",
(void *)client);
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
}
if (s_apply_endpoint_override(client, message_headers, options->endpoint)) {
return NULL;
}
struct aws_byte_cursor host_header_value;
/* The Host header must be set from s_apply_endpoint_override, if not errored out */
AWS_FATAL_ASSERT(aws_http_headers_get(message_headers, g_host_header_name, &host_header_value) == AWS_OP_SUCCESS);
bool is_https = true;
uint32_t port = 0;
if (options->endpoint != NULL) {
struct aws_byte_cursor https_scheme = aws_byte_cursor_from_c_str("https");
struct aws_byte_cursor http_scheme = aws_byte_cursor_from_c_str("http");
const struct aws_byte_cursor *scheme = aws_uri_scheme(options->endpoint);
is_https = aws_byte_cursor_eq_ignore_case(scheme, &https_scheme);
if (!is_https && !aws_byte_cursor_eq_ignore_case(scheme, &http_scheme)) {
AWS_LOGF_ERROR(
AWS_LS_S3_CLIENT,
"id=%p Cannot create meta s3 request; unexpected scheme '" PRInSTR "' in endpoint override.",
(void *)client,
AWS_BYTE_CURSOR_PRI(*scheme));
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
port = aws_uri_port(options->endpoint);
}
struct aws_s3_meta_request *meta_request = client->vtable->meta_request_factory(client, options);
if (meta_request == NULL) {
AWS_LOGF_ERROR(AWS_LS_S3_CLIENT, "id=%p: Could not create new meta request.", (void *)client);
return NULL;
}
bool error_occurred = false;
/* BEGIN CRITICAL SECTION */
{
aws_s3_client_lock_synced_data(client);
if (use_s3express_signing && !client->synced_data.s3express_provider_active) {
AWS_LOGF_TRACE(AWS_LS_S3_CLIENT, "id=%p Create S3 Express provider for the client.", (void *)client);
/**
* Invoke the factory within the lock. We WARNED people uses their own factory to not use ANY client related
* api during the factory.
*
* We cannot just release the lock and invoke the factory, because it can lead to the other request assume
* the provider is active, and not waiting for the provider to be created. And lead to unexpected behavior.
*/
client->s3express_provider = client->s3express_provider_factory(
client->allocator, client, s_s3express_provider_finish_destroy, client, client->factory_user_data);
/* Provider is related to client, we don't need to clean it up if meta request failed. But, if provider
* failed to be created, let's bail out earlier. */
if (!client->s3express_provider) {
AWS_LOGF_ERROR(