Debian Codegraph @

source/s3_meta_request.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 #include "s3.h"
6
7 #include "auth.h"
8 #include "http.h"
9 #include "io.h"
10 #include <errno.h>
11
12 #include <aws/common/clock.h>
13 #include <aws/common/file.h>
14 #include <aws/http/request_response.h>
15 #include <aws/io/file_utils.h>
16 #include <aws/io/stream.h>
17 #include <aws/s3/s3_client.h>
18
19 static const char *s_capsule_name_s3_meta_request = "aws_s3_meta_request";
20
21 struct s3_meta_request_binding {
22 struct aws_s3_meta_request *native;
23
24 /* Reference to python object that reference to other related python object to keep it alive */
25 PyObject *py_core;
26
27 /* Batch up the transferred size in one sec. */
28 uint64_t size_transferred;
29 /* The time stamp when the progress reported */
30 uint64_t last_sampled_time;
31 };
32
33 struct aws_s3_meta_request *aws_py_get_s3_meta_request(PyObject *meta_request) {
34 AWS_PY_RETURN_NATIVE_FROM_BINDING(
35 meta_request, s_capsule_name_s3_meta_request, "S3Request", s3_meta_request_binding);
36 }
37
38 static void s_destroy(struct s3_meta_request_binding *meta_request) {
39 Py_XDECREF(meta_request->py_core);
40 aws_mem_release(aws_py_get_allocator(), meta_request);
41 }
42
43 static PyObject *s_get_py_headers(const struct aws_http_headers *headers) {
44 size_t num_headers = aws_http_headers_count(headers);
45 PyObject *header_list = PyList_New(num_headers);
46 if (!header_list) {
47 return NULL;
48 }
49 for (size_t i = 0; i < num_headers; i++) {
50 struct aws_http_header header;
51 AWS_ZERO_STRUCT(header);
52 aws_http_headers_get_index(headers, i, &header);
53 const char *name_str = (const char *)header.name.ptr;
54 size_t name_len = header.name.len;
55 const char *value_str = (const char *)header.value.ptr;
56 size_t value_len = header.value.len;
57 PyObject *tuple = Py_BuildValue("(s#s#)", name_str, name_len, value_str, value_len);
58 if (!tuple) {
59 goto error;
60 }
61 PyList_SetItem(header_list, i, tuple); /* steals reference to tuple */
62 }
63 return header_list;
64 error:
65 Py_XDECREF(header_list);
66 return NULL;
67 }
68
69 static int s_s3_request_on_headers(
70 struct aws_s3_meta_request *meta_request,
71 const struct aws_http_headers *headers,
72 int response_status,
73 void *user_data) {
74 (void)meta_request;
75 struct s3_meta_request_binding *request_binding = user_data;
76
77 /*************** GIL ACQUIRE ***************/
78 bool error = true;
79 PyGILState_STATE state;
80 if (aws_py_gilstate_ensure(&state)) {
81 return AWS_OP_ERR; /* Python has shut down. Nothing matters anymore, but don't crash */
82 }
83
84 /* Build up a list of (name,value) tuples */
85 PyObject *header_list = s_get_py_headers(headers);
86 if (!header_list) {
87 PyErr_WriteUnraisable(request_binding->py_core);
88 goto done;
89 }
90
91 /* Deliver the built up list of (name,value) tuples */
92 PyObject *result =
93 PyObject_CallMethod(request_binding->py_core, "_on_headers", "(iO)", response_status, header_list);
94 if (!result) {
95 PyErr_WriteUnraisable(request_binding->py_core);
96 goto done;
97 }
98 /* If user's callback raises an exception, _S3RequestCore._on_headers
99 * stores it to throw later and returns False */
100 error = (result == Py_False);
101 Py_DECREF(result);
102 done:
103 Py_XDECREF(header_list);
104 PyGILState_Release(state);
105 /*************** GIL RELEASE ***************/
106 if (error) {
107 return aws_raise_error(AWS_ERROR_CRT_CALLBACK_EXCEPTION);
108 } else {
109 return AWS_OP_SUCCESS;
110 }
111 }
112
113 /* To avoid reporting progress to python too often. We cache it up and only report to python after at least 1 sec. */
114 static int s_record_progress(struct s3_meta_request_binding *request_binding, uint64_t length, bool *report_progress) {
115 if (aws_add_u64_checked(request_binding->size_transferred, length, &request_binding->size_transferred)) {
116 /* Wow */
117 return AWS_OP_ERR;
118 }
119 uint64_t now;
120 if (aws_high_res_clock_get_ticks(&now)) {
121 return AWS_OP_ERR;
122 }
123 uint64_t nanos = 0;
124 if (aws_sub_u64_checked(now, request_binding->last_sampled_time, &nanos)) {
125 return AWS_OP_ERR;
126 }
127 uint64_t sec = aws_timestamp_convert(nanos, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, NULL);
128 *report_progress = (sec >= 1);
129 if (*report_progress) {
130 request_binding->last_sampled_time = now;
131 }
132 return AWS_OP_SUCCESS;
133 }
134
135 static int s_s3_request_on_body(
136 struct aws_s3_meta_request *meta_request,
137 const struct aws_byte_cursor *body,
138 uint64_t range_start,
139 void *user_data) {
140 (void)meta_request;
141 struct s3_meta_request_binding *request_binding = user_data;
142 bool error = true;
143 /*************** GIL ACQUIRE ***************/
144 PyGILState_STATE state;
145 PyObject *result = NULL;
146 if (aws_py_gilstate_ensure(&state)) {
147 return AWS_OP_ERR; /* Python has shut down. Nothing matters anymore, but don't crash */
148 }
149
150 result = PyObject_CallMethod(
151 request_binding->py_core, "_on_body", "(y#K)", (const char *)(body->ptr), (Py_ssize_t)body->len, range_start);
152
153 if (!result) {
154 PyErr_WriteUnraisable(request_binding->py_core);
155 goto done;
156 }
157 /* If user's callback raises an exception, _S3RequestCore._on_body
158 * stores it to throw later and returns False */
159 error = (result == Py_False);
160 Py_DECREF(result);
161 done:
162 PyGILState_Release(state);
163 /*************** GIL RELEASE ***************/
164 if (error) {
165 return aws_raise_error(AWS_ERROR_CRT_CALLBACK_EXCEPTION);
166 } else {
167 return AWS_OP_SUCCESS;
168 }
169 }
170
171 static void s_s3_request_on_finish(
172 struct aws_s3_meta_request *meta_request,
173 const struct aws_s3_meta_request_result *meta_request_result,
174 void *user_data) {
175 (void)meta_request;
176 struct s3_meta_request_binding *request_binding = user_data;
177
178 int error_code = meta_request_result->error_code;
179 /*************** GIL ACQUIRE ***************/
180 PyGILState_STATE state;
181 if (aws_py_gilstate_ensure(&state)) {
182 return; /* Python has shut down. Nothing matters anymore, but don't crash */
183 }
184
185 PyObject *header_list = NULL;
186 PyObject *result = NULL;
187
188 if (request_binding->size_transferred && (error_code == 0)) {
189 /* report the remaining progress */
190 result =
191 PyObject_CallMethod(request_binding->py_core, "_on_progress", "(K)", request_binding->size_transferred);
192 if (!result) {
193 PyErr_WriteUnraisable(request_binding->py_core);
194 /* We MUST keep going and invoke the final callback */
195 } else {
196 Py_DECREF(result);
197 }
198 request_binding->size_transferred = 0;
199 }
200 struct aws_byte_buf error_body;
201 AWS_ZERO_STRUCT(error_body);
202 /* Get the header and body of the error */
203 if (meta_request_result->error_response_headers) {
204 header_list = s_get_py_headers(meta_request_result->error_response_headers);
205 if (!header_list) {
206 PyErr_WriteUnraisable(request_binding->py_core);
207 /* We MUST keep going and invoke the final callback. These headers were optional anyway. */
208 }
209 }
210 if (meta_request_result->error_response_body) {
211 error_body = *(meta_request_result->error_response_body);
212 }
213
214 const char *operation_name = NULL;
215 if (meta_request_result->error_response_operation_name != NULL) {
216 operation_name = aws_string_c_str(meta_request_result->error_response_operation_name);
217 }
218
219 result = PyObject_CallMethod(
220 request_binding->py_core,
221 "_on_finish",
222 "(iiOy#sOi)",
223 error_code,
224 meta_request_result->response_status,
225 header_list ? header_list : Py_None,
226 (const char *)(error_body.buffer),
227 (Py_ssize_t)error_body.len,
228 operation_name,
229 meta_request_result->did_validate ? Py_True : Py_False,
230 (int)meta_request_result->validation_algorithm);
231
232 if (result) {
233 Py_DECREF(result);
234 } else {
235 /* Callback might fail during application shutdown */
236 PyErr_WriteUnraisable(request_binding->py_core);
237 }
238
239 Py_XDECREF(header_list);
240 PyGILState_Release(state);
241 /*************** GIL RELEASE ***************/
242 }
243
244 /* Invoked when S3Request._binding gets cleaned up.
245 * DO NOT destroy the C binding struct or anything inside it yet.
246 * The user might have let S3Request get GC'd,
247 * but the s3_meta_request_binding* must outlive the native aws_s3_meta_request* */
248 static void s_s3_meta_request_capsule_destructor(PyObject *capsule) {
249 struct s3_meta_request_binding *meta_request = PyCapsule_GetPointer(capsule, s_capsule_name_s3_meta_request);
250
251 if (meta_request->native) {
252 aws_s3_meta_request_release(meta_request->native);
253 } else {
254 /* we hit this branch if things failed part way through setting up the binding,
255 * before the native aws_s3_meta_request could be created. */
256 s_destroy(meta_request);
257 }
258 }
259
260 /* Callback from C land, invoked when the underlying shutdown process finished */
261 static void s_s3_request_on_shutdown(void *user_data) {
262 struct s3_meta_request_binding *request_binding = user_data;
263
264 /*************** GIL ACQUIRE ***************/
265 PyGILState_STATE state;
266 if (aws_py_gilstate_ensure(&state)) {
267 return; /* Python has shut down. Nothing matters anymore, but don't crash */
268 }
269 /* Clean thing up before invoking the callback, as the callback means everything has been shutdown, which is not
270 * true if we clean the resource after the callback */
271
272 PyObject *py_core = request_binding->py_core;
273 Py_INCREF(py_core);
274 s_destroy(request_binding);
275
276 /* Deliver the built up list of (name,value) tuples */
277 PyObject *result = PyObject_CallMethod(py_core, "_on_shutdown", NULL);
278 if (!result) {
279 PyErr_WriteUnraisable(py_core);
280 }
281
282 Py_XDECREF(py_core);
283 PyGILState_Release(state);
284 /*************** GIL RELEASE ***************/
285 }
286
287 static void s_s3_request_on_progress(
288 struct aws_s3_meta_request *meta_request,
289 const struct aws_s3_meta_request_progress *progress,
290 void *user_data) {
291
292 (void)meta_request;
293 struct s3_meta_request_binding *request_binding = user_data;
294
295 bool report_progress = false;
296 s_record_progress(request_binding, progress->bytes_transferred, &report_progress);
297
298 if (report_progress) {
299 /*************** GIL ACQUIRE ***************/
300 PyGILState_STATE state;
301 if (aws_py_gilstate_ensure(&state)) {
302 return; /* Python has shut down. Nothing matters anymore, but don't crash */
303 }
304 PyObject *result =
305 PyObject_CallMethod(request_binding->py_core, "_on_progress", "(K)", request_binding->size_transferred);
306 if (result) {
307 Py_DECREF(result);
308 }
309 request_binding->size_transferred = 0;
310 PyGILState_Release(state);
311 /*************** GIL RELEASE ***************/
312 }
313 }
314
315 PyObject *aws_py_s3_client_make_meta_request(PyObject *self, PyObject *args) {
316 (void)self;
317
318 struct aws_allocator *allocator = aws_py_get_allocator();
319
320 PyObject *py_s3_request; /* O */
321 PyObject *s3_client_py; /* O */
322 PyObject *http_request_py; /* O */
323 int type; /* i */
324 const char *operation_name; /* z */
325 PyObject *signing_config_py; /* O */
326 PyObject *credential_provider_py; /* O */
327 const char *recv_filepath; /* z */
328 const char *send_filepath; /* z */
329 struct aws_byte_cursor region; /* s# */
330 enum aws_s3_checksum_algorithm checksum_algorithm; /* i */
331 enum aws_s3_checksum_location checksum_location; /* i */
332 int validate_response_checksum; /* p - boolean predicate */
333 uint64_t part_size; /* K */
334 uint64_t multipart_upload_threshold; /* K */
335 int fio_options_set; /* p - boolean predicate */
336 int should_stream; /* p - boolean predicate */
337 double disk_throughput_gbps; /* d */
338 int direct_io; /* p - boolean predicate */
339 uint64_t max_active_connections_override; /* K */
340 PyObject *py_core; /* O */
341 if (!PyArg_ParseTuple(
342 args,
343 "OOOizOOzzs#iipKKppdpKO",
344 &py_s3_request,
345 &s3_client_py,
346 &http_request_py,
347 &type,
348 &operation_name,
349 &signing_config_py,
350 &credential_provider_py,
351 &recv_filepath,
352 &send_filepath,
353 &region.ptr,
354 &region.len,
355 &checksum_algorithm,
356 &checksum_location,
357 &validate_response_checksum,
358 &part_size,
359 &multipart_upload_threshold,
360 &fio_options_set,
361 &should_stream,
362 &disk_throughput_gbps,
363 &direct_io,
364 &max_active_connections_override,
365 &py_core)) {
366 return NULL;
367 }
368 struct aws_s3_client *s3_client = aws_py_get_s3_client(s3_client_py);
369 if (!s3_client) {
370 return NULL;
371 }
372
373 struct aws_http_message *http_request = aws_py_get_http_message(http_request_py);
374 if (!http_request) {
375 return NULL;
376 }
377
378 struct aws_signing_config_aws *signing_config = NULL;
379 if (signing_config_py != Py_None) {
380 signing_config = aws_py_get_signing_config(signing_config_py);
381 if (!signing_config) {
382 return NULL;
383 }
384 }
385
386 struct aws_credentials_provider *credential_provider = NULL;
387 if (credential_provider_py != Py_None) {
388 credential_provider = aws_py_get_credentials_provider(credential_provider_py);
389 if (!credential_provider) {
390 return NULL;
391 }
392 }
393
394 struct aws_signing_config_aws signing_config_from_credentials_provider;
395 AWS_ZERO_STRUCT(signing_config_from_credentials_provider);
396 if (credential_provider) {
397 aws_s3_init_default_signing_config(&signing_config_from_credentials_provider, region, credential_provider);
398 signing_config = &signing_config_from_credentials_provider;
399 }
400
401 struct aws_s3_checksum_config checksum_config = {
402 .checksum_algorithm = checksum_algorithm,
403 .location = checksum_location,
404 .validate_response_checksum = validate_response_checksum != 0,
405 };
406
407 struct s3_meta_request_binding *meta_request = aws_mem_calloc(allocator, 1, sizeof(struct s3_meta_request_binding));
408 if (!meta_request) {
409 return PyErr_AwsLastError();
410 }
411
412 /* From hereon, we need to clean up if errors occur */
413
414 PyObject *capsule =
415 PyCapsule_New(meta_request, s_capsule_name_s3_meta_request, s_s3_meta_request_capsule_destructor);
416 if (!capsule) {
417 aws_mem_release(allocator, meta_request);
418 return NULL;
419 }
420 struct aws_s3_file_io_options fio_opts = {
421 .should_stream = should_stream,
422 .disk_throughput_gbps = disk_throughput_gbps,
423 .direct_io = direct_io,
424 };
425
426 meta_request->py_core = py_core;
427 Py_INCREF(meta_request->py_core);
428
429 struct aws_s3_meta_request_options s3_meta_request_opt = {
430 .type = type,
431 .operation_name = aws_byte_cursor_from_c_str(operation_name),
432 .message = http_request,
433 .signing_config = signing_config,
434 .checksum_config = &checksum_config,
435 .send_filepath = aws_byte_cursor_from_c_str(send_filepath),
436 .recv_filepath = aws_byte_cursor_from_c_str(recv_filepath),
437 .headers_callback = s_s3_request_on_headers,
438 .body_callback = s_s3_request_on_body,
439 .finish_callback = s_s3_request_on_finish,
440 .shutdown_callback = s_s3_request_on_shutdown,
441 .progress_callback = s_s3_request_on_progress,
442 .part_size = part_size,
443 .multipart_upload_threshold = multipart_upload_threshold,
444 /* If fio options not set, let native code to decide the default instead */
445 .fio_opts = fio_options_set ? &fio_opts : NULL,
446 .max_active_connections_override = max_active_connections_override,
447 .user_data = meta_request,
448 };
449
450 if (aws_high_res_clock_get_ticks(&meta_request->last_sampled_time)) {
451 goto error;
452 }
453 meta_request->native = aws_s3_client_make_meta_request(s3_client, &s3_meta_request_opt);
454 if (meta_request->native == NULL) {
455 PyErr_SetAwsLastError();
456 goto error;
457 }
458
459 return capsule;
460
461 error:
462 Py_DECREF(capsule);
463 return NULL;
464 }
465
466 PyObject *aws_py_s3_meta_request_cancel(PyObject *self, PyObject *args) {
467 (void)self;
468
469 PyObject *py_meta_request = NULL;
470 if (!PyArg_ParseTuple(args, "O", &py_meta_request)) {
471 return NULL;
472 }
473
474 struct aws_s3_meta_request *meta_request = NULL;
475 meta_request = aws_py_get_s3_meta_request(py_meta_request);
476 if (!meta_request) {
477 return NULL;
478 }
479
480 aws_s3_meta_request_cancel(meta_request);
481
482 Py_RETURN_NONE;
483 }