martinzink commented on code in PR #1927:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1927#discussion_r2077543079


##########
extensions/aws/processors/PutKinesisStream.cpp:
##########
@@ -0,0 +1,164 @@
+/**
+ * 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 "PutKinesisStream.h"
+
+#include <memory>
+#include <random>
+#include <unordered_map>
+
+#include "aws/kinesis/KinesisClient.h"
+#include "aws/kinesis/model/PutRecordsRequest.h"
+#include "core/ProcessContext.h"
+#include "core/ProcessSession.h"
+#include "core/Resource.h"
+#include "utils/ProcessorConfigUtils.h"
+
+namespace org::apache::nifi::minifi::aws::processors {
+
+void PutKinesisStream::initialize() {
+  setSupportedProperties(Properties);
+  setSupportedRelationships(Relationships);
+}
+
+void PutKinesisStream::onSchedule(core::ProcessContext& context, 
core::ProcessSessionFactory& session_factory) {
+  AwsProcessor::onSchedule(context, session_factory);
+
+  batch_size_ = parseU64Property(context, MessageBatchSize);
+  if (batch_size_ == 0 || batch_size_ > 500) {
+    logger_->log_warn("PutKinesisStream::MessageBatchSize is invalid. Setting 
it to the maximum 500 value.");
+    batch_size_ = 500;
+  }
+  batch_data_size_soft_cap_ = parseDataSizeProperty(context, MaxBatchDataSize);
+  if (batch_data_size_soft_cap_ > 4_MB) {
+    logger_->log_warn("PutKinesisStream::MaxMessageBufferSize is invalid. 
Setting it to the maximum 4 MB value.");
+    batch_data_size_soft_cap_ = 4_MB;
+  }
+
+  endpoint_override_url_ = context.getProperty(EndpointOverrideURL.name) | 
minifi::utils::toOptional();
+}
+
+struct StreamBatch {
+  uint64_t batch_size = 0;
+  std::vector<std::shared_ptr<core::FlowFile>> flow_files;
+};
+
+void PutKinesisStream::onTrigger(core::ProcessContext& context, 
core::ProcessSession& session) {
+  logger_->log_trace("PutKinesisStream onTrigger");
+
+  constexpr uint64_t SINGLE_RECORD_MAX_SIZE = 1_MB;
+  std::unordered_map<std::string, StreamBatch> stream_batches;
+  auto credentials = getAWSCredentials(context, nullptr);
+
+  if (!credentials) {
+    logger_->log_error("Failed to get credentials for PutKinesisStream");
+    context.yield();
+    return;
+  }
+
+  for (uint64_t i = 0; i < batch_size_; i++) {
+    std::shared_ptr<core::FlowFile> flow_file = session.get();
+    if (!flow_file) { break; }
+    const auto flow_file_size = flow_file->getSize();
+    if (flow_file_size > SINGLE_RECORD_MAX_SIZE) {
+      flow_file->setAttribute(AwsKinesisErrorMessage.name, fmt::format("record 
too big {}, max allowed {}", flow_file_size, SINGLE_RECORD_MAX_SIZE));
+      session.transfer(flow_file, Failure);
+      logger_->log_error("Failed to publish to kinesis record {} because the 
size was greater than {} bytes", flow_file->getUUID().to_string(), 
SINGLE_RECORD_MAX_SIZE);
+      continue;
+    }
+
+    auto stream_name = context.getProperty(AmazonKinesisStreamName.name, 
flow_file.get());
+    if (!stream_name) {
+      logger_->log_error("Stream name is invalid due to {}", 
stream_name.error().message());
+      session.transfer(flow_file, Failure);
+      continue;
+    }
+    auto partition_key = 
context.getProperty(AmazonKinesisStreamPartitionKey.name, flow_file.get())
+        | minifi::utils::valueOrElse([&flow_file]() -> std::string { return 
flow_file->getUUID().to_string(); });
+
+    stream_batches[*stream_name].flow_files.push_back(std::move(flow_file));
+    stream_batches[*stream_name].batch_size += flow_file_size;
+
+    if (stream_batches[*stream_name].batch_size > batch_data_size_soft_cap_) {
+      break;

Review Comment:
   Yeah, I think this seems more useful compared to NiFi's implementation, 
because KinesisStream enforces their limit per request, which we send out per 
stream. 
   `Each record in the request can be as large as 1 MB, up to a limit of 5 MB 
for the entire request, including partition keys. `



-- 
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]

Reply via email to