szaszm commented on code in PR #1586:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1586#discussion_r1258127052
##########
extensions/aws/processors/PutS3Object.h:
##########
@@ -97,54 +111,61 @@ class PutS3Object : public S3Processor {
class ReadCallback {
public:
- static constexpr uint64_t MAX_SIZE = 5_GiB;
- static constexpr uint64_t BUFFER_SIZE = 4_KiB;
-
- ReadCallback(uint64_t flow_size, const
minifi::aws::s3::PutObjectRequestParameters& options, aws::s3::S3Wrapper&
s3_wrapper)
- : flow_size_(flow_size)
- , options_(options)
- , s3_wrapper_(s3_wrapper) {
+ ReadCallback(uint64_t flow_size, const
minifi::aws::s3::PutObjectRequestParameters& options, aws::s3::S3Wrapper&
s3_wrapper,
+ uint64_t multipart_threshold, uint64_t multipart_size,
core::logging::Logger& logger)
+ : flow_size_(flow_size),
+ options_(options),
+ s3_wrapper_(s3_wrapper),
+ multipart_threshold_(multipart_threshold),
+ multipart_size_(multipart_size),
+ logger_(logger) {
}
int64_t operator()(const std::shared_ptr<io::InputStream>& stream) {
- if (flow_size_ > MAX_SIZE) {
- return -1;
- }
- std::vector<std::byte> buffer;
- buffer.resize(BUFFER_SIZE);
- auto data_stream = std::make_shared<std::stringstream>();
- read_size_ = 0;
- while (read_size_ < flow_size_) {
- const auto next_read_size = (std::min)(flow_size_ - read_size_,
BUFFER_SIZE);
- const auto read_ret = stream->read(gsl::make_span(buffer).subspan(0,
next_read_size));
- if (io::isError(read_ret)) {
- return -1;
- }
- if (read_ret > 0) {
- data_stream->write(reinterpret_cast<char*>(buffer.data()),
gsl::narrow<std::streamsize>(next_read_size));
- read_size_ += read_ret;
+ try {
+ if (flow_size_ <= multipart_threshold_) {
+ logger_.log_info("Uploading S3 Object '%s' in a single upload",
options_.object_key);
Review Comment:
I think it would make sense to move the implementation to the .cpp file.
##########
extensions/aws/s3/S3Wrapper.cpp:
##########
@@ -75,42 +70,160 @@ std::string
S3Wrapper::getEncryptionString(Aws::S3::Model::ServerSideEncryption
return "";
}
-std::optional<PutObjectResult> S3Wrapper::putObject(const
PutObjectRequestParameters& put_object_params, const
std::shared_ptr<Aws::IOStream>& data_stream) {
- Aws::S3::Model::PutObjectRequest request;
- request.SetBucket(put_object_params.bucket);
- request.SetKey(put_object_params.object_key);
-
request.SetStorageClass(STORAGE_CLASS_MAP.at(put_object_params.storage_class));
-
request.SetServerSideEncryption(SERVER_SIDE_ENCRYPTION_MAP.at(put_object_params.server_side_encryption));
- request.SetContentType(put_object_params.content_type);
- request.SetMetadata(put_object_params.user_metadata_map);
+std::shared_ptr<Aws::StringStream> S3Wrapper::readFlowFileStream(const
std::shared_ptr<io::InputStream>& stream, uint64_t read_limit, uint64_t&
read_size_out) {
+ std::array<std::byte, BUFFER_SIZE> buffer{};
+ auto data_stream = std::make_shared<Aws::StringStream>();
+ uint64_t read_size = 0;
+ while (read_size < read_limit) {
+ const auto next_read_size = (std::min)(read_limit - read_size,
BUFFER_SIZE);
+ const auto read_ret = stream->read(gsl::make_span(buffer).subspan(0,
next_read_size));
+ if (io::isError(read_ret)) {
+ throw StreamReadException("Reading flow file inputstream failed!");
+ }
+ if (read_ret > 0) {
+ data_stream->write(reinterpret_cast<char*>(buffer.data()),
gsl::narrow<std::streamsize>(read_ret));
+ read_size += read_ret;
+ } else {
+ break;
+ }
+ }
+ read_size_out = read_size;
+ return data_stream;
+}
+
+std::optional<PutObjectResult> S3Wrapper::putObject(const
PutObjectRequestParameters& put_object_params, const
std::shared_ptr<io::InputStream>& stream, uint64_t flow_size) {
+ uint64_t read_size{};
+ auto data_stream = readFlowFileStream(stream, flow_size, read_size);
+ auto request =
createPutObjectRequest<Aws::S3::Model::PutObjectRequest>(put_object_params);
request.SetBody(data_stream);
- request.SetGrantFullControl(put_object_params.fullcontrol_user_list);
- request.SetGrantRead(put_object_params.read_permission_user_list);
- request.SetGrantReadACP(put_object_params.read_acl_user_list);
- request.SetGrantWriteACP(put_object_params.write_acl_user_list);
- setCannedAcl(request, put_object_params.canned_acl);
auto aws_result = request_sender_->sendPutObjectRequest(request,
put_object_params.credentials, put_object_params.client_config,
put_object_params.use_virtual_addressing);
if (!aws_result) {
return std::nullopt;
}
- PutObjectResult result;
- // Etags are returned by AWS in quoted form that should be removed
- result.etag =
minifi::utils::StringUtils::removeFramingCharacters(aws_result->GetETag(), '"');
- result.version = aws_result->GetVersionId();
+ return createPutObjectResult(*aws_result);
+}
- // GetExpiration returns a string pair with a date and a ruleid in
'expiry-date=\"<DATE>\", rule-id=\"<RULEID>\"' format
- // s3.expiration only needs the date member of this pair
- result.expiration =
getExpiration(aws_result->GetExpiration()).expiration_time;
- result.ssealgorithm =
getEncryptionString(aws_result->GetServerSideEncryption());
+std::optional<S3Wrapper::UploadPartsResult> S3Wrapper::uploadParts(const
PutObjectRequestParameters& put_object_params, const
std::shared_ptr<io::InputStream>& stream,
+ MultipartUploadState upload_state) {
+ stream->seek(upload_state.uploaded_size);
+ S3Wrapper::UploadPartsResult result;
+ result.upload_id = upload_state.upload_id;
+ result.part_etags = upload_state.uploaded_etags;
+ const auto flow_size = upload_state.full_size - upload_state.uploaded_size;
+ const auto div_ceil = [](size_t n, size_t d) {
Review Comment:
This utility already exists in GeneralUtils.h, called `intdiv_ceil`. It's
more general, works with both signed and unsigned integer types, but should
work the same as this in the `size_t` case.
You may want to double-check that the denominator isn't zero, and handle
that case appropriately.
##########
extensions/aws/s3/MultipartUploadStateStorage.cpp:
##########
@@ -0,0 +1,128 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "MultipartUploadStateStorage.h"
+
+#include "utils/StringUtils.h"
+
+namespace org::apache::nifi::minifi::aws::s3 {
+
+void MultipartUploadStateStorage::storeState(const std::string& bucket, const
std::string& key, const MultipartUploadState& state) {
+ std::unordered_map<std::string, std::string> stored_state;
+ state_manager_->get(stored_state);
+ std::string state_key = bucket + "/" + key;
+ stored_state[state_key + ".upload_id"] = state.upload_id;
+ stored_state[state_key + ".upload_time"] =
std::to_string(state.upload_time.Millis());
+ stored_state[state_key + ".uploaded_parts"] =
std::to_string(state.uploaded_parts);
+ stored_state[state_key + ".uploaded_size"] =
std::to_string(state.uploaded_size);
+ stored_state[state_key + ".part_size"] = std::to_string(state.part_size);
+ stored_state[state_key + ".full_size"] = std::to_string(state.full_size);
+ stored_state[state_key + ".uploaded_etags"] =
minifi::utils::StringUtils::join(";", state.uploaded_etags);
+ state_manager_->set(stored_state);
+ state_manager_->commit();
+ state_manager_->persist();
Review Comment:
What happens if a partial state is committed and persisted, but the flow
file is rolled back due to an exception down the line? Do we try to reuse
completed part transfers when the flow file is processed a second time after
rollback?
##########
libminifi/include/utils/TimeUtil.h:
##########
@@ -189,6 +189,20 @@ inline bool unit_matches<std::chrono::days>(const
std::string& unit) {
return unit == "d" || unit == "day" || unit == "days";
}
+template<>
+inline bool unit_matches<std::chrono::weeks>(const std::string& unit) {
+ return unit == "w" || unit == "week" || unit == "weeks";
+}
+
+template<>
+inline bool unit_matches<std::chrono::months>(const std::string& unit) {
+ return unit == "mon" || unit == "month" || unit == "months";
+}
+
+template<>
+inline bool unit_matches<std::chrono::years>(const std::string& unit) {
+ return unit == "y" || unit == "year" || unit == "years";
+}
Review Comment:
I would restrict the accepted set of spellings
- month: "month", "months"
- year: "year", "years"
For the same reason I detailed in the first comment. We can extend the
accepted formats if NiFi ever decides to add these units with alternate
spellings.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]