Debian Codegraph @

crt/aws-c-s3/source/s3_parallel_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_parallel_input_stream.h"
7
8 #include <aws/common/file.h>
9 #include <aws/common/task_scheduler.h>
10
11 #include <aws/io/event_loop.h>
12 #include <aws/io/future.h>
13 #include <aws/io/stream.h>
14
15 #include <errno.h>
16 #include <inttypes.h>
17
18 AWS_STATIC_STRING_FROM_LITERAL(s_readonly_bytes_mode, "rb");
19
20 void aws_parallel_input_stream_init_base(
21 struct aws_parallel_input_stream *stream,
22 struct aws_allocator *alloc,
23 const struct aws_parallel_input_stream_vtable *vtable,
24 void *impl) {
25
26 AWS_ZERO_STRUCT(*stream);
27 stream->alloc = alloc;
28 stream->vtable = vtable;
29 stream->impl = impl;
30 stream->shutdown_future = aws_future_void_new(alloc);
31 aws_ref_count_init(&stream->ref_count, stream, (aws_simple_completion_callback *)vtable->destroy);
32 }
33
34 struct aws_parallel_input_stream *aws_parallel_input_stream_acquire(struct aws_parallel_input_stream *stream) {
35 if (stream != NULL) {
36 aws_ref_count_acquire(&stream->ref_count);
37 }
38 return stream;
39 }
40
41 struct aws_parallel_input_stream *aws_parallel_input_stream_release(struct aws_parallel_input_stream *stream) {
42 if (stream != NULL) {
43 aws_ref_count_release(&stream->ref_count);
44 }
45 return NULL;
46 }
47
48 int aws_parallel_input_stream_get_length(struct aws_parallel_input_stream *stream, int64_t *out_length) {
49 if (stream->vtable->get_length) {
50 return stream->vtable->get_length(stream, out_length);
51 } else {
52 return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
53 }
54 }
55
56 struct aws_future_bool *aws_parallel_input_stream_read(
57 struct aws_parallel_input_stream *stream,
58 uint64_t offset,
59 size_t max_length,
60 struct aws_byte_buf *dest) {
61 /* Ensure the buffer has space available */
62 if (dest->len == dest->capacity) {
63 struct aws_future_bool *future = aws_future_bool_new(stream->alloc);
64 aws_future_bool_set_error(future, AWS_ERROR_SHORT_BUFFER);
65 return future;
66 }
67
68 struct aws_future_bool *future = stream->vtable->read(stream, offset, max_length, dest);
69 AWS_POSTCONDITION(future != NULL);
70 return future;
71 }
72
73 struct aws_future_void *aws_parallel_input_stream_get_shutdown_future(struct aws_parallel_input_stream *stream) {
74 return aws_future_void_acquire(stream->shutdown_future);
75 }
76
77 struct aws_parallel_input_stream_from_file_impl {
78 struct aws_parallel_input_stream base;
79
80 struct aws_string *file_path;
81 struct aws_event_loop_group *reading_elg;
82
83 bool direct_io_read;
84 };
85
86 static void s_parallel_from_file_destroy(struct aws_parallel_input_stream *stream) {
87 struct aws_parallel_input_stream_from_file_impl *impl =
88 AWS_CONTAINER_OF(stream, struct aws_parallel_input_stream_from_file_impl, base);
89
90 aws_string_destroy(impl->file_path);
91 aws_event_loop_group_release(impl->reading_elg);
92
93 aws_future_void_set_result(stream->shutdown_future);
94 aws_future_void_release(stream->shutdown_future);
95 aws_mem_release(stream->alloc, impl);
96
97 return;
98 }
99
100 struct read_task_impl {
101 struct aws_parallel_input_stream *para_stream;
102
103 struct aws_future_bool *end_future;
104 uint64_t offset;
105 size_t length;
106 struct aws_byte_buf *dest;
107 };
108
109 static void s_s3_parallel_from_file_read_task(struct aws_task *task, void *arg, enum aws_task_status task_status) {
110 (void)task_status;
111 struct read_task_impl *read_task = arg;
112 struct aws_parallel_input_stream *stream = read_task->para_stream;
113 struct aws_parallel_input_stream_from_file_impl *impl =
114 AWS_CONTAINER_OF(stream, struct aws_parallel_input_stream_from_file_impl, base);
115 struct aws_future_bool *end_future = read_task->end_future;
116 bool eof_reached = false;
117 size_t actually_read = 0;
118 int error_code = AWS_ERROR_SUCCESS;
119 if (impl->direct_io_read) {
120 /* Try direct IO. */
121 if (aws_file_path_read_from_offset_direct_io(
122 impl->file_path, read_task->offset, read_task->length, read_task->dest, &actually_read)) {
123 /* Direct IO not supported, fallback to normal read */
124 /* Log the warning */
125 AWS_LOGF_WARN(
126 AWS_LS_S3_GENERAL,
127 "Direct IO not supported, fallback to normal read. File path: %s",
128 aws_string_c_str(impl->file_path));
129 /* Set direct IO to be false to avoid extra checks. */
130 impl->direct_io_read = false;
131 aws_reset_error();
132 } else {
133 /* Succeed. */
134 goto finish;
135 }
136 }
137
138 if (aws_file_path_read_from_offset(
139 impl->file_path, read_task->offset, read_task->length, read_task->dest, &actually_read)) {
140 error_code = aws_last_error();
141 }
142
143 finish:
144 if (error_code != AWS_ERROR_SUCCESS) {
145 aws_future_bool_set_error(end_future, error_code);
146 } else {
147 /* If the reading length is smaller than expected, and no error raised, we encountered the EOS. */
148 /* The length is guaranteed to be not larger than the available space in the buffer. */
149 eof_reached = (actually_read < read_task->length);
150 aws_future_bool_set_result(end_future, eof_reached);
151 }
152 aws_future_bool_release(end_future);
153 aws_mem_release(stream->alloc, task);
154 aws_mem_release(stream->alloc, read_task);
155 aws_parallel_input_stream_release(stream);
156 }
157
158 struct aws_future_bool *s_parallel_from_file_read(
159 struct aws_parallel_input_stream *stream,
160 uint64_t offset,
161 size_t max_length,
162 struct aws_byte_buf *dest) {
163
164 struct aws_future_bool *future = aws_future_bool_new(stream->alloc);
165 struct aws_parallel_input_stream_from_file_impl *impl =
166 AWS_CONTAINER_OF(stream, struct aws_parallel_input_stream_from_file_impl, base);
167
168 /* Calculate how much we can read based on available buffer space and max_length */
169 size_t available_space = dest->capacity - dest->len;
170 size_t length = aws_min_size(available_space, max_length);
171
172 if (length == 0) {
173 /* Nothing to read. Complete the read with success. */
174 aws_future_bool_set_result(future, false);
175 return future;
176 }
177
178 struct read_task_impl *read_task = aws_mem_calloc(impl->base.alloc, 1, sizeof(struct read_task_impl));
179
180 AWS_LOGF_TRACE(AWS_LS_S3_GENERAL, "id=%p: Read %zu bytes from offset %" PRIu64 "", (void *)stream, length, offset);
181
182 /* Initialize for one read */
183 read_task->dest = dest;
184
185 AWS_FATAL_ASSERT(dest->buffer);
186 read_task->offset = offset;
187 read_task->length = length;
188 read_task->end_future = aws_future_bool_acquire(future);
189 read_task->para_stream = aws_parallel_input_stream_acquire(&impl->base);
190
191 struct aws_event_loop *loop = aws_event_loop_group_get_next_loop(impl->reading_elg);
192 struct aws_task *task = aws_mem_calloc(impl->base.alloc, 1, sizeof(struct aws_task));
193 aws_task_init(task, s_s3_parallel_from_file_read_task, read_task, "s3_parallel_read_task");
194 aws_event_loop_schedule_task_now(loop, task);
195 return future;
196 }
197
198 int s_parallel_from_file_get_length(struct aws_parallel_input_stream *stream, int64_t *length) {
199 struct aws_parallel_input_stream_from_file_impl *impl =
200 AWS_CONTAINER_OF(stream, struct aws_parallel_input_stream_from_file_impl, base);
201 FILE *file = aws_fopen_safe(impl->file_path, s_readonly_bytes_mode);
202 if (!file) {
203 return AWS_OP_ERR;
204 }
205
206 int ret_val = aws_file_get_length(file, length);
207 fclose(file);
208 return ret_val;
209 }
210
211 static struct aws_parallel_input_stream_vtable s_parallel_input_stream_from_file_vtable = {
212 .destroy = s_parallel_from_file_destroy,
213 .read = s_parallel_from_file_read,
214 .get_length = s_parallel_from_file_get_length,
215 };
216
217 struct aws_parallel_input_stream *aws_parallel_input_stream_new_from_file(
218 struct aws_allocator *allocator,
219 struct aws_byte_cursor file_name,
220 struct aws_event_loop_group *reading_elg,
221 bool direct_io_read) {
222
223 struct aws_parallel_input_stream_from_file_impl *impl =
224 aws_mem_calloc(allocator, 1, sizeof(struct aws_parallel_input_stream_from_file_impl));
225
226 aws_parallel_input_stream_init_base(&impl->base, allocator, &s_parallel_input_stream_from_file_vtable, impl);
227 impl->file_path = aws_string_new_from_cursor(allocator, &file_name);
228 impl->reading_elg = aws_event_loop_group_acquire(reading_elg);
229 impl->direct_io_read = direct_io_read;
230
231 if (!aws_path_exists(impl->file_path)) {
232 /* If file path not exists, raise error from errno. */
233 aws_translate_and_raise_io_error(errno);
234 s_parallel_from_file_destroy(&impl->base);
235 return NULL;
236 }
237
238 return &impl->base;
239 }