Copilot commented on code in PR #2152:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2152#discussion_r3022747423


##########
extension-framework/cpp-extension-lib/src/core/ControllerServiceImpl.cpp:
##########
@@ -0,0 +1,72 @@
+/**
+ * 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 "api/core/ControllerServiceImpl.h"
+
+#include <cctype>
+
+#include <memory>
+#include <set>
+#include <string>
+#include <vector>
+
+#include "fmt/format.h"
+
+using namespace std::literals::chrono_literals;
+
+namespace org::apache::nifi::minifi::api::core {
+
+ControllerServiceImpl::ControllerServiceImpl(minifi::core::ControllerServiceMetadata
 metadata)
+    : metadata_(std::move(metadata)),
+      logger_(metadata_.logger) {
+  logger_->log_debug("Processor {} created with uuid {}", getName(), 
getUUIDStr());
+}
+
+ControllerServiceImpl::~ControllerServiceImpl() {
+  logger_->log_debug("Destroying processor {} with uuid {}", getName(), 
getUUIDStr());

Review Comment:
   These log messages refer to a \"Processor\" even though this class is 
`ControllerServiceImpl`. This is misleading during debugging/ops; please update 
the messages to reference \"ControllerService\" (or \"controller service\") 
consistently.
   ```suggestion
     logger_->log_debug("ControllerService {} created with uuid {}", getName(), 
getUUIDStr());
   }
   
   ControllerServiceImpl::~ControllerServiceImpl() {
     logger_->log_debug("Destroying ControllerService {} with uuid {}", 
getName(), getUUIDStr());
   ```



##########
extension-framework/cpp-extension-lib/src/core/ControllerServiceImpl.cpp:
##########
@@ -0,0 +1,72 @@
+/**
+ * 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 "api/core/ControllerServiceImpl.h"
+
+#include <cctype>
+
+#include <memory>
+#include <set>
+#include <string>
+#include <vector>
+
+#include "fmt/format.h"
+
+using namespace std::literals::chrono_literals;
+

Review Comment:
   In this new file, these headers and the chrono literals namespace are unused 
(the file only uses `memory`, `string`, and logging). Removing unused includes 
and `using namespace std::literals::chrono_literals;` will reduce compile time 
and avoid misleading dependencies.
   ```suggestion
   #include <memory>
   #include <string>
   
   #include "fmt/format.h"
   ```



##########
extension-framework/cpp-extension-lib/include/api/core/ControllerServiceImpl.h:
##########
@@ -0,0 +1,66 @@
+/**
+ * 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.
+ */
+#pragma once
+
+#include <condition_variable>
+#include <memory>
+#include <mutex>
+#include <string>
+
+#include "ControllerServiceContext.h"
+#include "minifi-cpp/core/ControllerServiceMetadata.h"
+#include "minifi-cpp/utils/Id.h"
+#include "utils/SmallString.h"
+
+namespace org::apache::nifi::minifi::api {
+
+class Connection;

Review Comment:
   `<condition_variable>`, the `Connection` forward declaration, and the 
`mutex_` member are unused in the current implementation (and there are no 
synchronized accesses shown). If thread-safety is not part of this class’ 
responsibility, remove these to keep the API minimal; otherwise, add the 
corresponding synchronized logic so the extra members/includes are justified.



##########
libminifi/include/utils/CControllerService.h:
##########
@@ -104,7 +108,6 @@ class CControllerService final : public 
core::controller::ControllerServiceApi,
 };
 
 void useCControllerServiceClassDescription(const 
MinifiControllerServiceClassDefinition& class_description,
-    const BundleIdentifier& bundle_id,
     const std::function<void(ClassDescription, 
CControllerServiceClassDescription)>& fn);

Review Comment:
   The callback takes `ClassDescription` by value while `ClassDescription` is 
only forward-declared in this header. Passing an incomplete type by value is a 
brittle API (and forces the full definition to be available at call sites). 
Prefer taking `const ClassDescription&` (or a pointer) to avoid copies and 
reduce header coupling.
   ```suggestion
       const std::function<void(const ClassDescription&, 
CControllerServiceClassDescription)>& fn);
   ```



##########
extension-framework/cpp-extension-lib/include/api/core/ControllerServiceImpl.h:
##########
@@ -0,0 +1,66 @@
+/**
+ * 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.
+ */
+#pragma once
+
+#include <condition_variable>

Review Comment:
   `<condition_variable>`, the `Connection` forward declaration, and the 
`mutex_` member are unused in the current implementation (and there are no 
synchronized accesses shown). If thread-safety is not part of this class’ 
responsibility, remove these to keep the API minimal; otherwise, add the 
corresponding synchronized logic so the extra members/includes are justified.



##########
extension-framework/cpp-extension-lib/src/core/ControllerServiceImpl.cpp:
##########
@@ -0,0 +1,72 @@
+/**
+ * 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 "api/core/ControllerServiceImpl.h"
+
+#include <cctype>
+
+#include <memory>
+#include <set>
+#include <string>
+#include <vector>
+
+#include "fmt/format.h"
+
+using namespace std::literals::chrono_literals;
+

Review Comment:
   In this new file, these headers and the chrono literals namespace are unused 
(the file only uses `memory`, `string`, and logging). Removing unused includes 
and `using namespace std::literals::chrono_literals;` will reduce compile time 
and avoid misleading dependencies.
   ```suggestion
   #include <memory>
   #include <string>
   ```



##########
extension-framework/cpp-extension-lib/src/core/ControllerServiceImpl.cpp:
##########
@@ -0,0 +1,72 @@
+/**
+ * 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 "api/core/ControllerServiceImpl.h"
+
+#include <cctype>
+
+#include <memory>
+#include <set>
+#include <string>
+#include <vector>
+
+#include "fmt/format.h"
+
+using namespace std::literals::chrono_literals;
+
+namespace org::apache::nifi::minifi::api::core {
+
+ControllerServiceImpl::ControllerServiceImpl(minifi::core::ControllerServiceMetadata
 metadata)
+    : metadata_(std::move(metadata)),
+      logger_(metadata_.logger) {
+  logger_->log_debug("Processor {} created with uuid {}", getName(), 
getUUIDStr());
+}
+
+ControllerServiceImpl::~ControllerServiceImpl() {
+  logger_->log_debug("Destroying processor {} with uuid {}", getName(), 
getUUIDStr());

Review Comment:
   These log messages refer to a \"Processor\" even though this class is 
`ControllerServiceImpl`. This is misleading during debugging/ops; please update 
the messages to reference \"ControllerService\" (or \"controller service\") 
consistently.
   ```suggestion
     logger_->log_debug("ControllerService {} created with uuid {}", getName(), 
getUUIDStr());
   }
   
   ControllerServiceImpl::~ControllerServiceImpl() {
     logger_->log_debug("Destroying controller service {} with uuid {}", 
getName(), getUUIDStr());
   ```



##########
extension-framework/cpp-extension-lib/include/api/utils/ProcessorConfigUtils.h:
##########
@@ -167,4 +167,28 @@ std::optional<T> parseOptionalEnumProperty(const 
core::ProcessContext& context,
   return result.value();
 }
 
+template<typename ControllerServiceType>
+ControllerServiceType* parseOptionalControllerService(const 
core::ProcessContext& context, const minifi::core::PropertyReference& prop) {
+  const auto controller_service_name = context.getProperty(prop.name);
+  if (!controller_service_name || controller_service_name->empty()) {
+    return nullptr;
+  }
+
+  nonstd::expected<MinifiControllerService*, std::error_code> service = 
context.getControllerService(*controller_service_name, 
minifi::core::className<ControllerServiceType>());
+  if (!service) {
+    return nullptr;
+  }
+
+  return reinterpret_cast<ControllerServiceType*>(*service);
+}

Review Comment:
   `MinifiControllerService*` is an opaque C handle type. Reinterpreting it as 
`ControllerServiceType*` is not type-safe and is very likely incorrect at 
runtime/ABI level (handle vs. implementation object). A safer design is to 
return the opaque handle (or a small C++ wrapper around it) and expose typed 
operations through explicit API functions, rather than casting the handle to 
the implementation type.



##########
extension-framework/cpp-extension-lib/src/core/ControllerServiceContext.cpp:
##########
@@ -0,0 +1,36 @@
+/**
+* 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 "api/core/ControllerServiceContext.h"
+#include "api/utils/minifi-c-utils.h"
+
+namespace org::apache::nifi::minifi::api::core {
+
+nonstd::expected<std::string, std::error_code> 
ControllerServiceContext::getProperty(const std::string_view name) const {
+  std::optional<std::string> value;
+  const MinifiStatus status = MinifiControllerServiceContextGetProperty(impl_, 
utils::toStringView(name),
+    [] (void* data, const MinifiStringView result) {
+      (*static_cast<std::optional<std::string>*>(data)) = 
std::string(result.data, result.length);
+    }, &value);
+
+  if (!value) {
+    return nonstd::make_unexpected(utils::make_error_code(status));
+  }
+  return value.value();
+}

Review Comment:
   This returns success whenever the callback sets `value`, even if `status` 
indicates an error. The C API already returns a `MinifiStatus`, so the wrapper 
should primarily check `status != MINIFI_STATUS_SUCCESS` (and only use `value` 
as the payload). Otherwise, any implementation that invokes the callback before 
returning a failure status could be incorrectly reported as success.



##########
extension-framework/cpp-extension-lib/include/api/core/ControllerServiceImpl.h:
##########
@@ -0,0 +1,66 @@
+/**
+ * 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.
+ */
+#pragma once
+
+#include <condition_variable>
+#include <memory>
+#include <mutex>
+#include <string>
+
+#include "ControllerServiceContext.h"
+#include "minifi-cpp/core/ControllerServiceMetadata.h"
+#include "minifi-cpp/utils/Id.h"
+#include "utils/SmallString.h"
+
+namespace org::apache::nifi::minifi::api {
+
+class Connection;
+
+namespace core {
+
+class ControllerServiceImpl {
+ public:
+  explicit ControllerServiceImpl(minifi::core::ControllerServiceMetadata 
metadata);
+
+  ControllerServiceImpl(const ControllerServiceImpl&) = delete;
+  ControllerServiceImpl(ControllerServiceImpl&&) = delete;
+  ControllerServiceImpl& operator=(const ControllerServiceImpl&) = delete;
+  ControllerServiceImpl& operator=(ControllerServiceImpl&&) = delete;
+
+  virtual ~ControllerServiceImpl();
+
+  MinifiStatus enable(ControllerServiceContext&);
+  void notifyStop();
+
+  std::string getName() const;
+  minifi::utils::Identifier getUUID() const;
+  minifi::utils::SmallString<36> getUUIDStr() const;
+
+ protected:
+  virtual MinifiStatus enableImpl(api::core::ControllerServiceContext&) = 0;
+  virtual void notifyStopImpl() {}
+
+  minifi::core::ControllerServiceMetadata metadata_;
+
+  std::shared_ptr<minifi::core::logging::Logger> logger_;
+
+ private:
+  mutable std::mutex mutex_;

Review Comment:
   `<condition_variable>`, the `Connection` forward declaration, and the 
`mutex_` member are unused in the current implementation (and there are no 
synchronized accesses shown). If thread-safety is not part of this class’ 
responsibility, remove these to keep the API minimal; otherwise, add the 
corresponding synchronized logic so the extra members/includes are justified.



##########
libminifi/test/libtest/unit/TestBase.cpp:
##########
@@ -115,7 +115,7 @@ bool LogTestController::contains(const 
std::function<std::string()>& log_string_
   bool timed_out = false;
   do {
     std::string str = log_string_getter();
-    found = (str.find(ending) != std::string::npos);
+    found = (str.contains(ending));

Review Comment:
   `std::string::contains` is a C++23 feature. If this project is built with 
C++20 (the rest of this hunk uses `std::ranges`, which is C++20), this line 
will not compile. Consider using `str.find(ending) != std::string::npos` or 
gating `contains` behind a feature-test / C++ standard check.
   ```suggestion
       found = (str.find(ending) != std::string::npos);
   ```



##########
extension-framework/cpp-extension-lib/src/core/ControllerServiceContext.cpp:
##########
@@ -0,0 +1,36 @@
+/**
+* Licensed to the Apache Software Foundation (ASF) under one or more

Review Comment:
   The license header formatting is inconsistent with other files in this PR: 
this line is missing the leading space after `*` (compare with other ASF 
headers). Please align the comment formatting for consistency.
   ```suggestion
    * Licensed to the Apache Software Foundation (ASF) under one or more
   ```



##########
extension-framework/cpp-extension-lib/include/api/core/Resource.h:
##########
@@ -145,4 +147,48 @@ void useProcessorClassDescription(Fn&& fn) {
   fn(description);
 }
 
+template<typename Class, typename Fn>
+void useControllerServiceClassDescription(Fn&& fn) {
+  std::vector<std::vector<MinifiStringView>> string_vector_cache;
+
+  const auto full_name = minifi::core::className<Class>();
+
+  std::vector<MinifiPropertyDefinition> class_properties = 
utils::toProperties(Class::Properties, string_vector_cache);
+
+  MinifiControllerServiceClassDefinition description{
+    .full_name = utils::toStringView(full_name),
+    .description = utils::toStringView(Class::Description),
+    .class_properties_count = gsl::narrow<uint32_t>(class_properties.size()),
+    .class_properties_ptr = class_properties.data(),
+
+    .callbacks = MinifiControllerServiceCallbacks{
+      .create = [] (MinifiControllerServiceMetadata metadata) -> MINIFI_OWNED 
void* {
+        return new Class{minifi::core::ControllerServiceMetadata{
+          .uuid = 
minifi::utils::Identifier::parse(std::string{metadata.uuid.data, 
metadata.uuid.length}).value(),
+          .name = std::string{metadata.name.data, metadata.name.length},
+          .logger = std::make_shared<logging::Logger>(metadata.logger)
+        }};
+      },
+      .destroy = [] (MINIFI_OWNED void* self) -> void {
+        delete static_cast<Class*>(self);
+      },
+      .enable = [] (void* self, MinifiControllerServiceContext* context) -> 
MinifiStatus {
+        ControllerServiceContext context_wrapper(context);
+        try {
+          return static_cast<Class*>(self)->enable(context_wrapper);
+        } catch (...) {
+          return MINIFI_STATUS_UNKNOWN_ERROR;
+        }
+      },
+      .notifyStop = [] (void* self) -> void {
+        static_cast<Class*>(self)->notifyStop();
+      },

Review Comment:
   Exceptions must not cross the C callback boundary. Two concrete issues 
here:\n1) `.create` can throw (e.g., `parse(...).value()` on invalid UUID, `new 
Class{...}` if the constructor throws), which would unwind through a C ABI 
callback.\n2) `.notifyStop` lacks a try/catch, so any exception thrown by the 
extension implementation will unwind through the C ABI.\nWrap both callbacks in 
`try/catch (...)` and return a safe failure signal (`nullptr` from `.create`; 
swallow/log in `.notifyStop` or report via an error channel if available). Also 
avoid `.value()` here; prefer an explicit failure path.



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