Debian Codegraph @

crt/aws-c-s3/source/s3_part_streaming_input_stream.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/s3/private/s3_meta_request_impl.h>
7 #include <aws/s3/private/s3_parallel_input_stream.h>
8 #include <aws/s3/private/s3_part_streaming_input_stream.h>
9 #include <aws/s3/private/s3_util.h>
10
11 #include <aws/common/atomics.h>
12 #include <aws/common/clock.h>
13 #include <aws/common/file.h>
14 #include <aws/common/logging.h>
15 #include <aws/common/string.h>
16 #include <aws/common/system_info.h>
17 #include <aws/common/task_scheduler.h>
18 #include <aws/common/thread.h>
19
20 #include <aws/io/event_loop.h>
21 #include <aws/io/future.h>
22 #include <aws/io/stream.h>
23
24 #include <inttypes.h>
25
26 /* 60 secs. */
27 static const uint64_t s_max_timeout_ns = 60 * (uint64_t)AWS_TIMESTAMP_NANOS;
28
29 struct aws_s3_part_streaming_input_stream_impl {
30 struct aws_input_stream base;
31 struct aws_allocator *allocator;
32
33 struct aws_s3_buffer_ticket *ticket;
34 struct aws_parallel_input_stream *para_stream;
35
36 /* Settings won't be changed during read. */
37 size_t page_size;
38 uint64_t offset;
39 size_t total_length;
40 size_t chunk_load_size;
41
42 /* The reading counters */
43 /* Incase the `offset` is not aligned with page size. The offset - page_aligned_offset will be aligned the page size
44 * to load the chunk. And read from the chunk can start from this `page_aligned_offset` */
45 size_t page_aligned_offset;
46 /* The offset of the chunk in the `reading_chunk_buf` that will start reading. */
47 size_t in_chunk_offset;
48 /* So far, how many bytes has been read */
49 size_t total_length_read;
50
51 /* The pointer to the buffer that is reading from */
52 struct aws_byte_buf *reading_chunk_buf;
53 /* The pointer to the buffer that is loading in the background. And will be the next chunk to read from. */
54 struct aws_byte_buf *loading_chunk_buf;
55 /* The future when the loading_chunk_buf is read. */
56 struct aws_future_bool *loading_future;
57
58 /* Stores two buffers for reading and loading. */
59 struct aws_byte_buf chunk_buf_1;
60 struct aws_byte_buf chunk_buf_2;
61
62 /* End of stream has been load from the `para_stream` */
63 bool eos_loaded_from_para_stream;
64 /* End of stream reached for this input stream */
65 bool eos_reached;
66 };
67
68 static int s_part_streaming_input_stream_seek(
69 struct aws_input_stream *stream,
70 int64_t offset,
71 enum aws_stream_seek_basis basis) {
72 (void)stream;
73 (void)offset;
74 (void)basis;
75
76 AWS_LOGF_ERROR(AWS_LS_S3_GENERAL, "Seek operation not supported on part streaming input stream");
77
78 return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
79 }
80
81 static void s_kick_off_next_load(struct aws_s3_part_streaming_input_stream_impl *impl) {
82
83 size_t length_after_chunk_read = impl->total_length_read + (impl->reading_chunk_buf->len - impl->in_chunk_offset);
84
85 uint64_t new_offset = impl->offset + length_after_chunk_read;
86 impl->page_aligned_offset = new_offset % impl->page_size;
87 new_offset -= impl->page_aligned_offset;
88
89 size_t remaining_length = impl->total_length - length_after_chunk_read + impl->page_aligned_offset;
90
91 if (remaining_length > 0 && !impl->eos_loaded_from_para_stream) {
92 /* Align the remaining length with the page size. */
93 if (remaining_length < impl->chunk_load_size) {
94 size_t aligned_remaining_length = remaining_length % impl->page_size;
95 /* Read more tha needed to align with the page size. */
96 if (aligned_remaining_length > 0) {
97 remaining_length = remaining_length + impl->page_size - aligned_remaining_length;
98 AWS_LOGF_TRACE(
99 AWS_LS_S3_GENERAL,
100 "id=%p: Aligned remaining length to page boundary: %zu bytes",
101 (void *)impl,
102 remaining_length);
103 }
104 }
105
106 size_t load_size = aws_min_size(remaining_length, impl->chunk_load_size);
107 AWS_LOGF_TRACE(
108 AWS_LS_S3_GENERAL,
109 "id=%p: Starting background load - offset=%" PRIu64 ", size=%zu",
110 (void *)impl,
111 new_offset,
112 load_size);
113
114 /* Kick off loading the next chunk. */
115 impl->loading_future =
116 aws_parallel_input_stream_read(impl->para_stream, new_offset, load_size, impl->loading_chunk_buf);
117 }
118 }
119
120 /* The input stream read can only be invoked serially. No concurrent read supported. */
121 static int s_part_streaming_input_stream_read(struct aws_input_stream *stream, struct aws_byte_buf *dest) {
122 struct aws_s3_part_streaming_input_stream_impl *impl =
123 AWS_CONTAINER_OF(stream, struct aws_s3_part_streaming_input_stream_impl, base);
124 size_t read_length = aws_min_size(dest->capacity - dest->len, impl->total_length - impl->total_length_read);
125
126 if (impl->reading_chunk_buf->len == 0) {
127 /* The reading buf is not ready. Block until the loading buf is available. */
128 if (impl->loading_future == NULL) {
129 /* Nothing to read */
130 AWS_LOGF_TRACE(AWS_LS_S3_GENERAL, "id=%p: Nothing to read, EOS reached", (void *)impl);
131 AWS_ASSERT(impl->eos_reached);
132 return AWS_OP_SUCCESS;
133 }
134
135 AWS_LOGF_TRACE(AWS_LS_S3_GENERAL, "id=%p: Waiting for background load to complete", (void *)impl);
136 /* TODO: the HTTP interface doesn't support async streaming, we have to block the thread here. */
137 if (!aws_future_bool_wait(impl->loading_future, s_max_timeout_ns)) {
138 /* Timeout */
139 AWS_LOGF_ERROR(AWS_LS_S3_GENERAL, "id=%p: Timeout waiting for background load", (void *)impl);
140 return AWS_OP_ERR;
141 }
142
143 int read_error = aws_future_bool_get_error(impl->loading_future);
144 if (read_error != AWS_ERROR_SUCCESS) {
145 /* Read failed. */
146 AWS_LOGF_ERROR(
147 AWS_LS_S3_GENERAL,
148 "id=%p: Background load failed with error %s",
149 (void *)impl,
150 aws_error_debug_str(read_error));
151 return aws_raise_error(read_error);
152 }
153
154 impl->eos_loaded_from_para_stream = aws_future_bool_get_result(impl->loading_future);
155 impl->loading_future = aws_future_bool_release(impl->loading_future);
156
157 AWS_LOGF_TRACE(
158 AWS_LS_S3_GENERAL,
159 "id=%p: Background load completed - loaded_bytes=%zu, eos_loaded=%s",
160 (void *)impl,
161 impl->loading_chunk_buf->len,
162 impl->eos_loaded_from_para_stream ? "true" : "false");
163
164 /* Swap the reading the loading pointer. */
165 struct aws_byte_buf *tmp = impl->reading_chunk_buf;
166 impl->reading_chunk_buf = impl->loading_chunk_buf;
167 impl->loading_chunk_buf = tmp;
168
169 if (impl->page_aligned_offset > 0) {
170 impl->in_chunk_offset = impl->page_aligned_offset;
171 impl->page_aligned_offset = 0;
172 } else {
173 impl->in_chunk_offset = 0;
174 }
175 s_kick_off_next_load(impl);
176 }
177
178 read_length = aws_min_size(read_length, impl->reading_chunk_buf->len - impl->in_chunk_offset);
179
180 struct aws_byte_cursor chunk_cursor = aws_byte_cursor_from_buf(impl->reading_chunk_buf);
181 aws_byte_cursor_advance(&chunk_cursor, impl->in_chunk_offset);
182 chunk_cursor.len = read_length;
183 aws_byte_buf_append(dest, &chunk_cursor);
184 impl->in_chunk_offset += read_length;
185 impl->total_length_read += read_length;
186
187 AWS_LOGF_TRACE(
188 AWS_LS_S3_GENERAL,
189 "id=%p: Read completed - bytes_read=%zu, total_read=%zu, in_chunk_offset=%zu",
190 (void *)impl,
191 read_length,
192 impl->total_length_read,
193 impl->in_chunk_offset);
194
195 /* We finished reading the chunk or we reached the expected EOS. */
196 if (impl->in_chunk_offset == impl->reading_chunk_buf->len || impl->total_length_read == impl->total_length) {
197 /* We finished reading the reading buffer, reset it. */
198
199 if (impl->eos_loaded_from_para_stream || impl->total_length_read == impl->total_length) {
200 /* We reached the end of the stream. */
201 impl->eos_reached = true;
202 AWS_LOGF_DEBUG(AWS_LS_S3_GENERAL, "id=%p: End of stream reached", (void *)impl);
203 AWS_ASSERT(impl->total_length_read <= impl->total_length);
204 }
205 aws_byte_buf_reset(impl->reading_chunk_buf, false);
206 impl->in_chunk_offset = 0;
207 }
208
209 return AWS_OP_SUCCESS;
210 }
211
212 static int s_part_streaming_input_stream_get_status(struct aws_input_stream *stream, struct aws_stream_status *status) {
213 (void)stream;
214 (void)status;
215
216 struct aws_s3_part_streaming_input_stream_impl *impl =
217 AWS_CONTAINER_OF(stream, struct aws_s3_part_streaming_input_stream_impl, base);
218
219 status->is_end_of_stream = (impl->total_length_read == impl->total_length) || impl->eos_reached;
220 status->is_valid = true;
221
222 return AWS_OP_SUCCESS;
223 }
224
225 static int s_part_streaming_input_stream_get_length(struct aws_input_stream *stream, int64_t *out_length) {
226 AWS_ASSERT(stream != NULL);
227 struct aws_s3_part_streaming_input_stream_impl *impl =
228 AWS_CONTAINER_OF(stream, struct aws_s3_part_streaming_input_stream_impl, base);
229 *out_length = (int64_t)impl->total_length;
230 return AWS_OP_SUCCESS;
231 }
232
233 static void s_part_streaming_input_stream_destroy(void *user_data) {
234 struct aws_s3_part_streaming_input_stream_impl *impl = user_data;
235
236 if (impl->loading_future) {
237 AWS_LOGF_DEBUG(AWS_LS_S3_GENERAL, "id=%p: Waiting for pending load to complete before destroy", (void *)impl);
238 if (aws_future_bool_is_done(impl->loading_future)) {
239 aws_future_bool_release(impl->loading_future);
240 } else {
241 /* If there is a loading future, wait for it to complete.
242 * Don't block the thead, to avoid dead lock when the future needs the thread to complete.*/
243 /* TODO: probably better to cancel the future, but we don't support cancel yet */
244 aws_future_bool_register_callback(impl->loading_future, s_part_streaming_input_stream_destroy, impl);
245 return;
246 }
247 }
248 AWS_LOGF_DEBUG(
249 AWS_LS_S3_GENERAL,
250 "id=%p: Destroying part streaming input stream - total_read=%zu, total_length=%zu",
251 (void *)impl,
252 impl->total_length_read,
253 impl->total_length);
254 aws_parallel_input_stream_release(impl->para_stream);
255 aws_s3_buffer_ticket_release(impl->ticket);
256 aws_mem_release(impl->allocator, impl);
257 }
258
259 static struct aws_input_stream_vtable s_part_streaming_input_stream_vtable = {
260 .seek = s_part_streaming_input_stream_seek,
261 .read = s_part_streaming_input_stream_read,
262 .get_status = s_part_streaming_input_stream_get_status,
263 .get_length = s_part_streaming_input_stream_get_length,
264 };
265
266 struct aws_input_stream *aws_part_streaming_input_stream_new(
267 struct aws_allocator *allocator,
268 struct aws_parallel_input_stream *para_stream,
269 struct aws_s3_buffer_ticket *buffer_ticket,
270 uint64_t offset,
271 size_t request_body_size,
272 bool page_aligned) {
273 AWS_PRECONDITION(para_stream);
274 AWS_PRECONDITION(buffer_ticket);
275
276 struct aws_s3_part_streaming_input_stream_impl *impl =
277 aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_part_streaming_input_stream_impl));
278
279 aws_ref_count_init(
280 &impl->base.ref_count, impl, (aws_simple_completion_callback *)s_part_streaming_input_stream_destroy);
281 impl->allocator = allocator;
282 impl->base.vtable = &s_part_streaming_input_stream_vtable;
283
284 if (page_aligned) {
285 impl->page_size = aws_system_info_page_size();
286 } else {
287 /* Disable page alignment by using 1 as the page size */
288 impl->page_size = 1;
289 }
290 impl->offset = offset;
291 int64_t para_stream_total_length = 0;
292 if (aws_parallel_input_stream_get_length(para_stream, &para_stream_total_length)) {
293 AWS_LOGF_ERROR(
294 AWS_LS_S3_GENERAL,
295 "id=%p: Failed to create part_streaming_input_stream: get length from parallel input stream with error %s",
296 (void *)impl,
297 aws_error_debug_str(aws_last_error()));
298 goto error;
299 }
300 uint64_t total_available_length = aws_sub_u64_saturating((uint64_t)para_stream_total_length, offset);
301 impl->total_length = (size_t)aws_min_u64((uint64_t)request_body_size, total_available_length);
302
303 impl->para_stream = aws_parallel_input_stream_acquire(para_stream);
304 impl->ticket = aws_s3_buffer_ticket_acquire(buffer_ticket);
305
306 struct aws_byte_buf buffer = aws_s3_buffer_ticket_claim(impl->ticket);
307 if (buffer.capacity % 2 != 0) {
308 AWS_LOGF_ERROR(
309 AWS_LS_S3_GENERAL,
310 "id=%p: Failed to create part_streaming_input_stream: Only supports even length of buffer from the ticket.",
311 (void *)impl);
312 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
313 goto error;
314 }
315 if (buffer.buffer == NULL || buffer.capacity == 0) {
316 AWS_LOGF_ERROR(
317 AWS_LS_S3_GENERAL,
318 "id=%p: Failed to create part_streaming_input_stream: The buffer from ticket is invalid.",
319 (void *)impl);
320 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
321 goto error;
322 }
323 impl->chunk_load_size = buffer.capacity / 2;
324
325 if (impl->chunk_load_size < impl->page_size) {
326 AWS_LOGF_ERROR(
327 AWS_LS_S3_GENERAL,
328 "id=%p: Failed to create part_streaming_input_stream: The buffer from ticket is smaller than the two times "
329 "of page size. Cannot align the page.",
330 (void *)impl);
331 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
332 goto error;
333 }
334
335 /* Split the buffer to the first and second half. */
336 /* There is no need to clean up the buffer acquired from the ticket, only need to release the ticket itself. */
337 impl->chunk_buf_1 = aws_byte_buf_from_array(buffer.buffer, impl->chunk_load_size);
338 impl->chunk_buf_2 = aws_byte_buf_from_array(buffer.buffer + impl->chunk_load_size, impl->chunk_load_size);
339
340 aws_byte_buf_reset(&impl->chunk_buf_1, false);
341 aws_byte_buf_reset(&impl->chunk_buf_2, false);
342
343 impl->loading_chunk_buf = &impl->chunk_buf_1;
344 impl->reading_chunk_buf = &impl->chunk_buf_2;
345
346 AWS_LOGF_TRACE(
347 AWS_LS_S3_GENERAL,
348 "id=%p: Created part streaming input stream - offset=%" PRIu64
349 ", total_length_to_read=%zu, chunk_load_size=%zu",
350 (void *)impl,
351 offset,
352 impl->total_length,
353 impl->chunk_load_size);
354
355 /* Handle zero-length request case */
356 if (impl->total_length == 0) {
357 impl->eos_reached = true;
358 AWS_LOGF_TRACE(AWS_LS_S3_GENERAL, "id=%p: Zero-length request, immediately setting EOS", (void *)impl);
359 } else {
360 /* Start to load into the loading buffer. Cannot fail the create function after this. */
361 s_kick_off_next_load(impl);
362 }
363 return &impl->base;
364 error:
365 aws_parallel_input_stream_release(impl->para_stream);
366 aws_s3_buffer_ticket_release(impl->ticket);
367 aws_mem_release(allocator, impl);
368 return NULL;
369 }