You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2022/01/11 15:14:52 UTC

[GitHub] [nifi-minifi-cpp] szaszm commented on a change in pull request #1221: MINIFICPP-1630 Create FetchAzureDataLakeStorage processor

szaszm commented on a change in pull request #1221:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1221#discussion_r782222859



##########
File path: extensions/azure/processors/FetchAzureDataLakeStorage.cpp
##########
@@ -0,0 +1,125 @@
+/**
+ * @file FetchAzureDataLakeStorage.cpp
+ * FetchAzureDataLakeStorage class implementation
+ *
+ * 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 "FetchAzureDataLakeStorage.h"
+
+#include "core/Resource.h"
+
+namespace org::apache::nifi::minifi::azure::processors {
+
+const core::Property FetchAzureDataLakeStorage::RangeStart(
+    core::PropertyBuilder::createProperty("Range Start")
+      ->withDescription("The byte position at which to start reading from the object. An empty value or a value of zero will start reading at the beginning of the object.")
+      ->supportsExpressionLanguage(true)
+      ->build());
+
+const core::Property FetchAzureDataLakeStorage::RangeLength(
+    core::PropertyBuilder::createProperty("Range Length")
+      ->withDescription("The number of bytes to download from the object, starting from the Range Start. "
+                        "An empty value or a value that extends beyond the end of the object will read to the end of the object.")
+      ->supportsExpressionLanguage(true)
+      ->build());
+
+const core::Property FetchAzureDataLakeStorage::NumberOfRetries(
+    core::PropertyBuilder::createProperty("Number of Retries")
+      ->withDescription("The number of automatic retries to perform if the download fails.")
+      ->withDefaultValue<uint64_t>(0)
+      ->supportsExpressionLanguage(true)
+      ->build());
+
+const core::Relationship FetchAzureDataLakeStorage::Success("success", "Files that have been successfully fetched from Azure storage are transferred to this relationship");
+const core::Relationship FetchAzureDataLakeStorage::Failure("failure", "In case of fetch failure flowfiles are transferred to this relationship");
+
+void FetchAzureDataLakeStorage::initialize() {
+  // Add new supported properties
+  setSupportedProperties({
+    AzureStorageCredentialsService,
+    FilesystemName,
+    DirectoryName,
+    FileName,
+    RangeStart,
+    RangeLength,
+    NumberOfRetries
+  });
+  // Set the supported relationships
+  setSupportedRelationships({
+    Success,
+    Failure
+  });
+}
+
+std::optional<storage::FetchAzureDataLakeStorageParameters> FetchAzureDataLakeStorage::buildFetchParameters(
+    const std::shared_ptr<core::ProcessContext>& context, const std::shared_ptr<core::FlowFile>& flow_file) {
+  storage::FetchAzureDataLakeStorageParameters params;
+  if (!setCommonParameters(params, *context, flow_file)) {
+    return std::nullopt;
+  }
+
+  std::string value;
+  if (context->getProperty(RangeStart, value, flow_file)) {
+    params.range_start = std::stoull(value);
+    logger_->log_debug("Range Start property set to %llu", *params.range_start);
+  }
+
+  if (context->getProperty(RangeLength, value, flow_file)) {
+    params.range_length = std::stoull(value);
+    logger_->log_debug("Range Length property set to %llu", *params.range_length);
+  }
+
+  if (context->getProperty(NumberOfRetries, value, flow_file)) {
+    params.number_of_retries = std::stoull(value);
+    logger_->log_debug("Number Of Retries property set to %llu", *params.number_of_retries);
+  }
+
+  return params;
+}
+
+void FetchAzureDataLakeStorage::onTrigger(const std::shared_ptr<core::ProcessContext>& context, const std::shared_ptr<core::ProcessSession>& session) {

Review comment:
       It would be nice to declare the preconditions that `context` and `session` must be valid.
   ```suggestion
   void FetchAzureDataLakeStorage::onTrigger(const std::shared_ptr<core::ProcessContext>& context, const std::shared_ptr<core::ProcessSession>& session) {
     gsl_Expects(context && session);
   ```

##########
File path: extensions/azure/storage/AzureDataLakeStorage.cpp
##########
@@ -61,4 +61,29 @@ bool AzureDataLakeStorage::deleteFile(const storage::DeleteAzureDataLakeStorageP
   }
 }
 
+std::optional<uint64_t> AzureDataLakeStorage::fetchFile(const FetchAzureDataLakeStorageParameters& params, io::BaseStream& stream) {
+  try {
+    auto fetch_res = data_lake_storage_client_->fetchFile(params);
+
+    std::array<uint8_t, 4096> buffer;
+    size_t write_size = 0;
+    if (fetch_res.FileSize < 0) return 0;
+    while (write_size < gsl::narrow<uint64_t>(fetch_res.FileSize)) {
+      const auto next_write_size = (std::min)(gsl::narrow<size_t>(fetch_res.FileSize) - write_size, buffer.size());
+      if (!fetch_res.Body->Read(buffer.data(), gsl::narrow<std::streamsize>(next_write_size))) {
+        return -1;
+      }
+      const auto ret = stream.write(buffer.data(), next_write_size);
+      if (io::isError(ret)) {
+        return -1;
+      }
+      write_size += next_write_size;
+    }

Review comment:
       Not sure if it's worth the extra effort, but if you could wrap the download result in an `InputStream`, then you could reuse `internal::pipe` (which has similar code to this function) or just return the stream directly.]
   It would also probably be easier to mock `fetchFile`.

##########
File path: extensions/azure/processors/FetchAzureDataLakeStorage.cpp
##########
@@ -0,0 +1,125 @@
+/**
+ * @file FetchAzureDataLakeStorage.cpp
+ * FetchAzureDataLakeStorage class implementation
+ *
+ * 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 "FetchAzureDataLakeStorage.h"
+
+#include "core/Resource.h"
+
+namespace org::apache::nifi::minifi::azure::processors {
+
+const core::Property FetchAzureDataLakeStorage::RangeStart(
+    core::PropertyBuilder::createProperty("Range Start")
+      ->withDescription("The byte position at which to start reading from the object. An empty value or a value of zero will start reading at the beginning of the object.")
+      ->supportsExpressionLanguage(true)
+      ->build());
+
+const core::Property FetchAzureDataLakeStorage::RangeLength(
+    core::PropertyBuilder::createProperty("Range Length")
+      ->withDescription("The number of bytes to download from the object, starting from the Range Start. "
+                        "An empty value or a value that extends beyond the end of the object will read to the end of the object.")
+      ->supportsExpressionLanguage(true)
+      ->build());
+
+const core::Property FetchAzureDataLakeStorage::NumberOfRetries(
+    core::PropertyBuilder::createProperty("Number of Retries")
+      ->withDescription("The number of automatic retries to perform if the download fails.")
+      ->withDefaultValue<uint64_t>(0)
+      ->supportsExpressionLanguage(true)
+      ->build());
+
+const core::Relationship FetchAzureDataLakeStorage::Success("success", "Files that have been successfully fetched from Azure storage are transferred to this relationship");
+const core::Relationship FetchAzureDataLakeStorage::Failure("failure", "In case of fetch failure flowfiles are transferred to this relationship");
+
+void FetchAzureDataLakeStorage::initialize() {
+  // Add new supported properties
+  setSupportedProperties({
+    AzureStorageCredentialsService,
+    FilesystemName,
+    DirectoryName,
+    FileName,
+    RangeStart,
+    RangeLength,
+    NumberOfRetries
+  });
+  // Set the supported relationships
+  setSupportedRelationships({
+    Success,
+    Failure
+  });
+}
+
+std::optional<storage::FetchAzureDataLakeStorageParameters> FetchAzureDataLakeStorage::buildFetchParameters(
+    const std::shared_ptr<core::ProcessContext>& context, const std::shared_ptr<core::FlowFile>& flow_file) {
+  storage::FetchAzureDataLakeStorageParameters params;

Review comment:
       There is no reason for this function to necessitate shared ownership. The first parameter should preferably be of type `const core::ProcessContext&`. The second one needs to be a `shared_ptr` due to `getProperty` (also unnecessarily) requiring a `shared_ptr` parameter.




-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org