| 1 | #ifndef AWS_S3_REQUEST_H |
| 2 | #define AWS_S3_REQUEST_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/common/byte_buf.h> |
| 10 | #include <aws/common/linked_list.h> |
| 11 | #include <aws/common/ref_count.h> |
| 12 | #include <aws/common/thread.h> |
| 13 | #include <aws/s3/s3.h> |
| 14 | |
| 15 | #include <aws/s3/private/s3_checksums.h> |
| 16 | |
| 17 | struct aws_http_message; |
| 18 | struct aws_signable; |
| 19 | struct aws_s3_meta_request; |
| 20 | |
| 21 | enum aws_s3_request_flags { |
| 22 | AWS_S3_REQUEST_FLAG_RECORD_RESPONSE_HEADERS = 0x00000001, |
| 23 | AWS_S3_REQUEST_FLAG_ALWAYS_SEND = 0x00000002, |
| 24 | AWS_S3_REQUEST_FLAG_ALLOCATE_BUFFER_FROM_POOL = 0x00000004, |
| 25 | }; |
| 26 | |
| 27 | /** |
| 28 | * Information sent in the telemetry_callback after each aws_s3_request finished/retried from meta request. |
| 29 | */ |
| 30 | struct aws_s3_request_metrics { |
| 31 | struct aws_allocator *allocator; |
| 32 | |
| 33 | struct { |
| 34 | /* The time stamp when the request was first initiated by the client, including the very first attempt. |
| 35 | * This timestamp is set once at the beginning and never updated during retries. Timestamps are from |
| 36 | * `aws_high_res_clock_get_ticks`. This will always be available. */ |
| 37 | int64_t s3_request_first_attempt_start_timestamp_ns; |
| 38 | |
| 39 | /* The time stamp when the request completely finished (success or final failure), including all retry |
| 40 | * attempts. This is set when the request reaches its final state - either successful completion or |
| 41 | * exhaustion of all retry attempts. Timestamps are from `aws_high_res_clock_get_ticks`. This will |
| 42 | * be available with the last attempt. */ |
| 43 | int64_t s3_request_last_attempt_end_timestamp_ns; |
| 44 | |
| 45 | /* The total time duration for the complete request lifecycle from initial start to final completion, |
| 46 | * including all retry attempts, backoff delays, and connection establishment time |
| 47 | * (s3_request_last_attempt_end_timestamp_ns - s3_request_first_attempt_start_timestamp_ns). This represents the |
| 48 | * end-to-end user experience time. This will be available with the last attempt. */ |
| 49 | int64_t s3_request_total_duration_ns; |
| 50 | |
| 51 | /* The time stamp when the request started by S3 client, which is prepared time by the client. Timestamps |
| 52 | * are from `aws_high_res_clock_get_ticks`. This will always be available. */ |
| 53 | int64_t start_timestamp_ns; |
| 54 | /* The time stamp when the request finished by S3 client succeed or failed or to be retried. Timestamps |
| 55 | * are from `aws_high_res_clock_get_ticks`. This will always be available. */ |
| 56 | int64_t end_timestamp_ns; |
| 57 | /* The time duration for the request from start to finish. end_timestamp_ns - start_timestamp_ns. This will |
| 58 | * always be available. */ |
| 59 | int64_t total_duration_ns; |
| 60 | |
| 61 | /* The time stamp when the request started to be encoded. -1 means data not available. Timestamp |
| 62 | * are from `aws_high_res_clock_get_ticks` */ |
| 63 | int64_t send_start_timestamp_ns; |
| 64 | /* The time stamp when the request finished to be encoded. -1 means data not available. |
| 65 | * Timestamp are from `aws_high_res_clock_get_ticks` */ |
| 66 | int64_t send_end_timestamp_ns; |
| 67 | /* The time duration for the request from start encoding to finish encoding (send_end_timestamp_ns - |
| 68 | * send_start_timestamp_ns). When send_end_timestamp_ns is -1, means data not available. */ |
| 69 | int64_t sending_duration_ns; |
| 70 | |
| 71 | /* The time stamp when the response started to be received from the network channel. -1 means data not |
| 72 | * available. Timestamp are from `aws_high_res_clock_get_ticks` */ |
| 73 | int64_t receive_start_timestamp_ns; |
| 74 | /* The time stamp when the response finished to be received from the network channel. -1 means data not |
| 75 | * available. Timestamp are from `aws_high_res_clock_get_ticks` */ |
| 76 | int64_t receive_end_timestamp_ns; |
| 77 | /* The time duration for the request from start receiving to finish receiving (receive_end_timestamp_ns - |
| 78 | * receive_start_timestamp_ns). When receive_end_timestamp_ns is 0, means data not available. */ |
| 79 | int64_t receiving_duration_ns; |
| 80 | |
| 81 | /* The time stamp when the request started to be signed. -1 means data not |
| 82 | * available. Timestamp are from `aws_high_res_clock_get_ticks` */ |
| 83 | int64_t sign_start_timestamp_ns; |
| 84 | /* The time stamp when the response finished to be signed. -1 means data not |
| 85 | * available. Timestamp are from `aws_high_res_clock_get_ticks` */ |
| 86 | int64_t sign_end_timestamp_ns; |
| 87 | /* The time duration for the request from start signing to finish signing (sign_end_timestamp_ns - |
| 88 | * sign_start_timestamp_ns). When sign_end_timestamp_ns is 0, means data not available. */ |
| 89 | int64_t signing_duration_ns; |
| 90 | |
| 91 | /* The time stamp when the request started to acquire memory. -1 means data not |
| 92 | * available. Timestamp are from `aws_high_res_clock_get_ticks` */ |
| 93 | int64_t mem_acquire_start_timestamp_ns; |
| 94 | /* The time stamp when the response to acquire memory. -1 means data not |
| 95 | * available. Timestamp are from `aws_high_res_clock_get_ticks` */ |
| 96 | int64_t mem_acquire_end_timestamp_ns; |
| 97 | /* The time duration for the request from start mem acquire to finish mem acquire (mem_acquire_end_timestamp_ns |
| 98 | * - mem_acquire_start_timestamp_ns). When sign_end_timestamp_ns is 0, means data not available. */ |
| 99 | int64_t mem_acquire_duration_ns; |
| 100 | |
| 101 | /* The time stamp when the request started to be delivered (i.e. on body callback is invoked or write to file |
| 102 | * initiated). -1 means data not available. Timestamp are from `aws_high_res_clock_get_ticks` */ |
| 103 | int64_t deliver_start_timestamp_ns; |
| 104 | /* The time stamp when the response finished to be delivered. -1 means data not |
| 105 | * available. Timestamp are from `aws_high_res_clock_get_ticks` */ |
| 106 | int64_t deliver_end_timestamp_ns; |
| 107 | /* The time duration for the request from start of delivery to finish of delivery (deliver_end_timestamp_ns - |
| 108 | * deliver_start_timestamp_ns). When deliver_duration_ns is 0, means data not available. */ |
| 109 | int64_t deliver_duration_ns; |
| 110 | |
| 111 | /* The time stamp when the request started to acquire connection. -1 means data not available. Timestamp |
| 112 | * are from `aws_high_res_clock_get_ticks` */ |
| 113 | int64_t conn_acquire_start_timestamp_ns; |
| 114 | /* The time stamp when the request finished to acquire connection. -1 means data not available. |
| 115 | * Timestamp are from `aws_high_res_clock_get_ticks` */ |
| 116 | int64_t conn_acquire_end_timestamp_ns; |
| 117 | /* The time duration for the request from start acquiring connection to finish acquiring connection |
| 118 | * (conn_acquire_end_timestamp_ns - conn_acquire_start_timestamp_ns). When conn_acquire_end_timestamp_ns is -1, |
| 119 | * means data not available. */ |
| 120 | int64_t conn_acquire_duration_ns; |
| 121 | |
| 122 | /* The time stamp when the request started to delay for retry. -1 means data not available. Timestamp |
| 123 | * are from `aws_high_res_clock_get_ticks` */ |
| 124 | int64_t retry_delay_start_timestamp_ns; |
| 125 | /* The time stamp when the request finished to delay for retry. -1 means data not available. |
| 126 | * Timestamp are from `aws_high_res_clock_get_ticks` */ |
| 127 | int64_t retry_delay_end_timestamp_ns; |
| 128 | /* The time duration for the request from start retry delay to finish retry delay (retry_delay_end_timestamp_ns |
| 129 | * - retry_delay_start_timestamp_ns). When retry_delay_end_timestamp_ns is -1, means data not available. */ |
| 130 | int64_t retry_delay_duration_ns; |
| 131 | |
| 132 | /* The time duration for the service call from connection acquisition to first response byte. -1 means data not |
| 133 | * available. */ |
| 134 | int64_t service_call_duration_ns; |
| 135 | } time_metrics; |
| 136 | |
| 137 | struct { |
| 138 | /* Response status code for the request */ |
| 139 | int response_status; |
| 140 | /* HTTP Headers of the response received. */ |
| 141 | struct aws_http_headers *response_headers; |
| 142 | /* Path and query of the request. */ |
| 143 | struct aws_string *request_path_query; |
| 144 | /* The host address of the request. */ |
| 145 | struct aws_string *host_address; |
| 146 | /* The the request ID header value. */ |
| 147 | struct aws_string *request_id; |
| 148 | /* The the extended request ID header value. */ |
| 149 | struct aws_string *extended_request_id; |
| 150 | /* S3 operation name for the request */ |
| 151 | struct aws_string *operation_name; |
| 152 | /* The type of request made */ |
| 153 | enum aws_s3_request_type request_type; |
| 154 | } req_resp_info_metrics; |
| 155 | |
| 156 | struct { |
| 157 | /* The IP address of the request connected to */ |
| 158 | struct aws_string *ip_address; |
| 159 | /* The pointer to the connection that request was made from */ |
| 160 | void *connection_ptr; |
| 161 | /* The pointer to the request that the request attempt was made from */ |
| 162 | void *request_ptr; |
| 163 | /* The aws_thread_id_t to the thread that request ran on */ |
| 164 | aws_thread_id_t thread_id; |
| 165 | /* The stream-id, which is the idex when the stream was activated. */ |
| 166 | uint32_t stream_id; |
| 167 | /* CRT error code when the aws_s3_request finishes. */ |
| 168 | int error_code; |
| 169 | /* Retry attempt. */ |
| 170 | uint32_t retry_attempt; |
| 171 | /* Is the memory for the request allocated from the buffer pool or not. */ |
| 172 | bool memory_allocated_from_pool; |
| 173 | } crt_info_metrics; |
| 174 | |
| 175 | /* TODO: align the part info metrics with the others, eg: `aws_s3_mpu_part_info`. */ |
| 176 | struct { |
| 177 | /* Beginning range of this part. */ |
| 178 | uint64_t part_range_start; |
| 179 | /* Last byte of this part. */ |
| 180 | uint64_t part_range_end; |
| 181 | /* Part number that this request refers to. */ |
| 182 | uint32_t part_number; |
| 183 | } part_info_metrics; |
| 184 | |
| 185 | struct aws_ref_count ref_count; |
| 186 | }; |
| 187 | |
| 188 | /* Represents a single request made to S3. */ |
| 189 | struct aws_s3_request { |
| 190 | |
| 191 | /* Linked list node used for queuing. */ |
| 192 | struct aws_linked_list_node node; |
| 193 | |
| 194 | /* Timestamp when retry attempt started. Overwritten on each retry and copied to new attempt's setup data. |
| 195 | * -1 means data not available. Timestamp from `aws_high_res_clock_get_ticks` */ |
| 196 | int64_t retry_start_timestamp_ns; |
| 197 | /* Timestamp when retry attempt ended. Overwritten on each retry and copied to new attempt's setup data. |
| 198 | * -1 means data not available. Timestamp from `aws_high_res_clock_get_ticks` */ |
| 199 | int64_t retry_end_timestamp_ns; |
| 200 | |
| 201 | /* Linked list node used for tracking the request is active from HTTP level. */ |
| 202 | struct aws_linked_list_node cancellable_http_streams_list_node; |
| 203 | |
| 204 | /* Linked list node used for tracking buffer acquire futures. */ |
| 205 | struct aws_linked_list_node pending_buffer_future_list_node; |
| 206 | |
| 207 | /* The meta request lock must be held to access the data */ |
| 208 | struct { |
| 209 | /* The underlying http stream, only valid when the request is active from HTTP level */ |
| 210 | struct aws_http_stream *cancellable_http_stream; |
| 211 | |
| 212 | /* Buffer future. */ |
| 213 | struct aws_future_s3_buffer_ticket *buffer_future; |
| 214 | } synced_data; |
| 215 | |
| 216 | /* TODO Ref count on the request is no longer needed--only one part of code should ever be holding onto a request, |
| 217 | * and we can just transfer ownership.*/ |
| 218 | struct aws_ref_count ref_count; |
| 219 | |
| 220 | struct aws_allocator *allocator; |
| 221 | |
| 222 | /* Owning meta request. */ |
| 223 | struct aws_s3_meta_request *meta_request; |
| 224 | |
| 225 | /* The buffer size to be allocated for the request, defaults the part size. */ |
| 226 | size_t buffer_size; |
| 227 | /* Request body to use when sending the request. The contents of this body will be re-used if a request is |
| 228 | * retried.*/ |
| 229 | struct aws_byte_buf request_body; |
| 230 | |
| 231 | /* Set when the request will be streaming from file directly instead of the request_body. */ |
| 232 | bool fio_streaming; |
| 233 | /* If file I/O options configure streaming. The request body will be streaming from this. If a request is retried, |
| 234 | * this stream will be recreated. */ |
| 235 | struct aws_input_stream *request_body_stream; |
| 236 | uint64_t content_length; |
| 237 | |
| 238 | /** |
| 239 | * Ticket to acquire the buffer. |
| 240 | */ |
| 241 | struct aws_s3_buffer_ticket *ticket; |
| 242 | |
| 243 | /* Beginning range of this part. */ |
| 244 | uint64_t part_range_start; |
| 245 | |
| 246 | /* Last byte of this part. |
| 247 | * Note: this is available on both put and get, but in put case it can be optimistic, |
| 248 | * ex for unknown content length we dont know what the end will be until we finish reading data. */ |
| 249 | uint64_t part_range_end; |
| 250 | |
| 251 | /* Part number that this request refers to. If this is not a part, this can be 0. (S3 Part Numbers start at 1.) |
| 252 | * However, must currently be a valid part number (ie: greater than 0) if the response body is to be streamed to the |
| 253 | * caller. |
| 254 | */ |
| 255 | uint32_t part_number; |
| 256 | |
| 257 | /* The upload_timeout used. Zero, if the request is not a upload part */ |
| 258 | size_t upload_timeout_ms; |
| 259 | |
| 260 | /* Number of times aws_s3_meta_request_prepare has been called for a request. During the first call to the virtual |
| 261 | * prepare function, this will be 0.*/ |
| 262 | uint32_t num_times_prepared; |
| 263 | |
| 264 | /* checksum found in the header of an individual get part http request */ |
| 265 | struct aws_byte_buf request_level_response_header_checksum; |
| 266 | |
| 267 | /* running checksum of the response to an individual get part http request */ |
| 268 | struct aws_s3_checksum *request_level_running_response_sum; |
| 269 | /* The algorithm used to validate the checksum */ |
| 270 | enum aws_s3_checksum_algorithm validation_algorithm; |
| 271 | |
| 272 | /* Get request only, was there a checksum to validate */ |
| 273 | bool did_validate; |
| 274 | |
| 275 | /* Get request only, if there was an attached checksum to validate did it match the computed checksum */ |
| 276 | bool checksum_match; |
| 277 | |
| 278 | /* Tag that defines what the built request will actually consist of. This is meant to be space for an enum defined |
| 279 | * by the derived type. Request tags do not necessarily map 1:1 with actual S3 API requests. (For example, they can |
| 280 | * be more contextual, like "first part" instead of just "part".) */ |
| 281 | |
| 282 | /* TODO: Eliminate the concept of "request tag" and just use request_type. |
| 283 | * It's confusing having 2 concepts that are so similar. |
| 284 | * There's only 1 case where 2 tags used the same type, |
| 285 | * we can use some other bool/flag to differentiate this 1 case. */ |
| 286 | int request_tag; |
| 287 | |
| 288 | /* Actual S3 type for the single request (may be AWS_S3_REQUEST_TYPE_UNKNOWN) */ |
| 289 | enum aws_s3_request_type request_type; |
| 290 | |
| 291 | /* S3 operation name for the single request (e.g. "CompleteMultipartUpload") */ |
| 292 | struct aws_string *operation_name; |
| 293 | |
| 294 | /* Members of this structure will be repopulated each time the request is sent. If the request fails, and needs to |
| 295 | * be retried, then the members of this structure will be cleaned up and re-populated on the next send. |
| 296 | */ |
| 297 | /* TODO rename this anonymous structure to something more intuitive. (Maybe "attempt_data")*/ |
| 298 | struct { |
| 299 | |
| 300 | /* The HTTP message to send for this request. */ |
| 301 | struct aws_http_message *message; |
| 302 | |
| 303 | /* Signable created for the above message. */ |
| 304 | struct aws_signable *signable; |
| 305 | |
| 306 | /* Recorded response headers for the request. Set only when the request desc has record_response_headers set to |
| 307 | * true or when this response indicates an error. */ |
| 308 | struct aws_http_headers *response_headers; |
| 309 | |
| 310 | /* Recorded response body of the request. */ |
| 311 | struct aws_byte_buf response_body; |
| 312 | |
| 313 | /* Returned response status of this request. */ |
| 314 | int response_status; |
| 315 | |
| 316 | /* Returned request ID of this request. */ |
| 317 | struct aws_string *request_id; |
| 318 | /* Returned extended request id of this request. */ |
| 319 | struct aws_string *amz_id_2; |
| 320 | |
| 321 | /* The metrics for the request telemetry */ |
| 322 | struct aws_s3_request_metrics *metrics; |
| 323 | |
| 324 | /* The request is required to have the unsigned payload */ |
| 325 | uint32_t require_streaming_unsigned_payload_header : 1; |
| 326 | } send_data; |
| 327 | |
| 328 | /* When true, response headers from the request will be stored in the request's response_headers variable. */ |
| 329 | uint32_t record_response_headers : 1; |
| 330 | |
| 331 | /* Indicates whether buffer should be allocated for the request from the pool. */ |
| 332 | uint32_t should_allocate_buffer_from_pool : 1; |
| 333 | |
| 334 | /* When true, this request is being tracked by the client for limiting the amount of in-flight-requests/stats. */ |
| 335 | uint32_t tracked_by_client : 1; |
| 336 | |
| 337 | /* When true, even when the meta request has a finish result set, this request will be sent. */ |
| 338 | uint32_t always_send : 1; |
| 339 | |
| 340 | /* When true, this request is intended to find out the object size. This is currently only used by auto_range_get. |
| 341 | */ |
| 342 | uint32_t discovers_object_size : 1; |
| 343 | |
| 344 | /* When true, this request does not represent a useful http request and |
| 345 | * must not be sent, however client must still call corresponding finished |
| 346 | * callback for the request. Those requests can occur when request is |
| 347 | * optimistically created during update, but cannot be prepared. ex. when |
| 348 | * put has no content length, requests will be scheduled as regular to |
| 349 | * ensure fair distribution against other requests, but can also result in |
| 350 | * requests for uploading data after the end of the stream (those requests |
| 351 | * will use below flag to indicate that they should not be sent). */ |
| 352 | uint32_t is_noop : 1; |
| 353 | |
| 354 | /* When true, this request has already been uploaded. we still prepare the request to check the durability. */ |
| 355 | uint32_t was_previously_uploaded : 1; |
| 356 | }; |
| 357 | |
| 358 | AWS_EXTERN_C_BEGIN |
| 359 | |
| 360 | /* Create a new s3 request structure with the given options. */ |
| 361 | AWS_S3_API |
| 362 | struct aws_s3_request *aws_s3_request_new( |
| 363 | struct aws_s3_meta_request *meta_request, |
| 364 | int request_tag, |
| 365 | enum aws_s3_request_type request_type, |
| 366 | uint32_t part_number, |
| 367 | uint32_t flags); |
| 368 | |
| 369 | /* Gets the size of the part payload. |
| 370 | * Range is inclusive (i.e. 0-0 range has 1 byte size). In case of misconfigured range returns 0. */ |
| 371 | AWS_S3_API |
| 372 | uint64_t aws_s3_request_get_payload_size(struct aws_s3_request *request); |
| 373 | |
| 374 | /* Set up the request to be sent. Called each time before the request is sent. Will initially call |
| 375 | * aws_s3_request_clean_up_send_data to clear out anything previously existing in send_data. */ |
| 376 | AWS_S3_API |
| 377 | void aws_s3_request_setup_send_data(struct aws_s3_request *request, struct aws_http_message *message); |
| 378 | |
| 379 | /* Clear out send_data members so that they can be repopulated before the next send. */ |
| 380 | AWS_S3_API |
| 381 | void aws_s3_request_clean_up_send_data(struct aws_s3_request *request); |
| 382 | |
| 383 | AWS_S3_API |
| 384 | struct aws_s3_request *aws_s3_request_acquire(struct aws_s3_request *request); |
| 385 | |
| 386 | AWS_S3_API |
| 387 | struct aws_s3_request *aws_s3_request_release(struct aws_s3_request *request); |
| 388 | |
| 389 | AWS_S3_API |
| 390 | struct aws_s3_request_metrics *aws_s3_request_metrics_new(struct aws_allocator *allocator); |
| 391 | |
| 392 | AWS_EXTERN_C_END |
| 393 | |
| 394 | #endif |