You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "martinzink (via GitHub)" <gi...@apache.org> on 2026/02/06 12:23:49 UTC

[PR] MINIFICPP-2710 Refactor ControllerServiceNodeMap [nifi-minifi-cpp]

martinzink opened a new pull request, #2098:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2098

   Thank you for submitting a contribution to Apache NiFi - MiNiFi C++.
   
   In order to streamline the review of the contribution we ask you
   to ensure the following steps have been taken:
   
   ### For all changes:
   - [ ] Is there a JIRA ticket associated with this PR? Is it referenced
        in the commit message?
   
   - [ ] Does your PR title start with MINIFICPP-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
   
   - [ ] Has your PR been rebased against the latest commit within the target branch (typically main)?
   
   - [ ] Is your initial contribution a single, squashed commit?
   
   ### For code changes:
   - [ ] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)?
   - [ ] If applicable, have you updated the LICENSE file?
   - [ ] If applicable, have you updated the NOTICE file?
   
   ### For documentation related changes:
   - [ ] Have you ensured that format looks appropriate for the output in which it is rendered?
   
   ### Note:
   Please ensure that once the PR is submitted, you check GitHub Actions CI results for build issues and submit an update to your PR as soon as possible.
   


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


Re: [PR] MINIFICPP-2710 Refactor ControllerServiceNodeMap [nifi-minifi-cpp]

Posted by "fgerlits (via GitHub)" <gi...@apache.org>.
fgerlits commented on code in PR #2098:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2098#discussion_r2960984472


##########
libminifi/src/core/controller/ControllerServiceNodeMap.cpp:
##########
@@ -17,81 +17,93 @@
  */
 
 #include "core/controller/ControllerServiceNodeMap.h"
+
+#include <ranges>
+
 #include "core/ProcessGroup.h"
 
 namespace org::apache::nifi::minifi::core::controller {
 
 ControllerServiceNode* ControllerServiceNodeMap::get(const std::string &id) const {
-  std::lock_guard<std::mutex> lock(mutex_);
-  auto exists = controller_service_nodes_.find(id);
-  if (exists != controller_service_nodes_.end())
-    return exists->second.get();
-  else
-    return nullptr;
+  const std::scoped_lock lock(mutex_);
+  if (const auto entry = getEntry(id, lock)) {
+    return entry->controller_service_node.get();
+  }
+  return nullptr;
 }
 
 ControllerServiceNode* ControllerServiceNodeMap::get(const std::string &id, const utils::Identifier& processor_or_controller_uuid) const {
-  std::lock_guard<std::mutex> lock(mutex_);
-  ControllerServiceNode* controller = nullptr;
-  auto exists = controller_service_nodes_.find(id);
-  if (exists != controller_service_nodes_.end()) {
-    controller = exists->second.get();
-  } else {
+  const std::scoped_lock lock(mutex_);
+  const auto entry = getEntry(id, lock);
+  if (!entry || !entry->parent_group) {
     return nullptr;
   }
 
-  auto process_group_of_controller_exists = process_groups_.find(id);
-  ProcessGroup* process_group = nullptr;
-  if (process_group_of_controller_exists != process_groups_.end()) {
-    process_group = process_group_of_controller_exists->second;
-  } else {
-    return nullptr;
-  }
 
-  if (process_group->findProcessorById(processor_or_controller_uuid, ProcessGroup::Traverse::IncludeChildren)) {
-    return controller;
+  if (entry->parent_group->findProcessorById(processor_or_controller_uuid, ProcessGroup::Traverse::IncludeChildren)) {
+    return entry->controller_service_node.get();
   }
 
-  if (process_group->findControllerService(processor_or_controller_uuid.to_string(), ProcessGroup::Traverse::IncludeChildren)) {
-    return controller;
+  if (entry->parent_group->findControllerService(processor_or_controller_uuid.to_string(), ProcessGroup::Traverse::IncludeChildren)) {
+    return entry->controller_service_node.get();
   }
 
   return nullptr;
 }
 
-bool ControllerServiceNodeMap::put(const std::string &id, const std::shared_ptr<ControllerServiceNode> &serviceNode) {
-  if (id.empty() || serviceNode == nullptr)
+bool ControllerServiceNodeMap::put(std::string id, std::shared_ptr<ControllerServiceNode> controller_service_node,
+    ProcessGroup* parent_group) {
+  std::scoped_lock lock(mutex_);
+  if (id.empty() || controller_service_node == nullptr || alternative_keys.contains(id)) {
     return false;
-  std::lock_guard<std::mutex> lock(mutex_);
-  controller_service_nodes_[id] = serviceNode;
-  return true;
+  }
+  auto [_it, success] = services_.emplace(std::move(id), ServiceEntry{.controller_service_node = std::move(controller_service_node), .parent_group = parent_group});
+  return success;
 }
 
-bool ControllerServiceNodeMap::put(const std::string &id, ProcessGroup* process_group) {
-  if (id.empty() || process_group == nullptr)
-    return false;
-  std::lock_guard<std::mutex> lock(mutex_);
-  process_groups_.emplace(id, gsl::make_not_null(process_group));
-  return true;
-}
 
 void ControllerServiceNodeMap::clear() {
-  std::lock_guard<std::mutex> lock(mutex_);
-  for (const auto& [id, node] : controller_service_nodes_) {
-    node->disable();
+  std::scoped_lock lock(mutex_);
+  for (const auto& node: services_ | std::views::values) {
+    node.controller_service_node->disable();

Review Comment:
   can we call this `service_entry`, please?
   ```suggestion
     for (const auto& service_entry : services_ | std::views::values) {
       service_entry.controller_service_node->disable();
   ```



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


Re: [PR] MINIFICPP-2710 Refactor ControllerServiceNodeMap [nifi-minifi-cpp]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2098:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2098#discussion_r2773893953


##########
libminifi/src/core/controller/ControllerServiceNodeMap.cpp:
##########
@@ -17,81 +17,92 @@
  */
 
 #include "core/controller/ControllerServiceNodeMap.h"
+
+#include <ranges>
+
 #include "core/ProcessGroup.h"
 
 namespace org::apache::nifi::minifi::core::controller {
 
 ControllerServiceNode* ControllerServiceNodeMap::get(const std::string &id) const {
-  std::lock_guard<std::mutex> lock(mutex_);
-  auto exists = controller_service_nodes_.find(id);
-  if (exists != controller_service_nodes_.end())
-    return exists->second.get();
-  else
-    return nullptr;
+  const std::scoped_lock lock(mutex_);
+  if (const auto entry = getEntry(id, lock)) {
+    return entry->controller_service_node.get();
+  }
+  return nullptr;
 }
 
 ControllerServiceNode* ControllerServiceNodeMap::get(const std::string &id, const utils::Identifier& processor_or_controller_uuid) const {
-  std::lock_guard<std::mutex> lock(mutex_);
-  ControllerServiceNode* controller = nullptr;
-  auto exists = controller_service_nodes_.find(id);
-  if (exists != controller_service_nodes_.end()) {
-    controller = exists->second.get();
-  } else {
+  const std::scoped_lock lock(mutex_);
+  const auto entry = getEntry(id, lock);
+  if (!entry || !entry->parent_group) {
     return nullptr;
   }
 
-  auto process_group_of_controller_exists = process_groups_.find(id);
-  ProcessGroup* process_group = nullptr;
-  if (process_group_of_controller_exists != process_groups_.end()) {
-    process_group = process_group_of_controller_exists->second;
-  } else {
-    return nullptr;
-  }
 
-  if (process_group->findProcessorById(processor_or_controller_uuid, ProcessGroup::Traverse::IncludeChildren)) {
-    return controller;
+  if (entry->parent_group->findProcessorById(processor_or_controller_uuid, ProcessGroup::Traverse::IncludeChildren)) {
+    return entry->controller_service_node.get();
   }
 
-  if (process_group->findControllerService(processor_or_controller_uuid.to_string(), ProcessGroup::Traverse::IncludeChildren)) {
-    return controller;
+  if (entry->parent_group->findControllerService(processor_or_controller_uuid.to_string(), ProcessGroup::Traverse::IncludeChildren)) {
+    return entry->controller_service_node.get();
   }
 
   return nullptr;
 }
 
-bool ControllerServiceNodeMap::put(const std::string &id, const std::shared_ptr<ControllerServiceNode> &serviceNode) {
-  if (id.empty() || serviceNode == nullptr)
+bool ControllerServiceNodeMap::put(std::string id, std::shared_ptr<ControllerServiceNode> controller_service_node,
+    ProcessGroup* parent_group) {
+  if (id.empty() || controller_service_node == nullptr || alternative_keys.contains(id)) {
     return false;
-  std::lock_guard<std::mutex> lock(mutex_);
-  controller_service_nodes_[id] = serviceNode;
-  return true;
+  }
+  std::scoped_lock lock(mutex_);
+  auto [_it, success] = services_.emplace(std::move(id), ServiceEntry{.controller_service_node = std::move(controller_service_node), .parent_group = parent_group});
+  return success;
 }
 
-bool ControllerServiceNodeMap::put(const std::string &id, ProcessGroup* process_group) {
-  if (id.empty() || process_group == nullptr)
-    return false;
-  std::lock_guard<std::mutex> lock(mutex_);
-  process_groups_.emplace(id, gsl::make_not_null(process_group));
-  return true;
-}
 
 void ControllerServiceNodeMap::clear() {
-  std::lock_guard<std::mutex> lock(mutex_);
-  for (const auto& [id, node] : controller_service_nodes_) {
-    node->disable();
+  std::scoped_lock lock(mutex_);
+  for (const auto& node: services_ | std::views::values) {
+    node.controller_service_node->disable();
   }
-  controller_service_nodes_.clear();
-  process_groups_.clear();
+  services_.clear();
 }
 
 std::vector<std::shared_ptr<ControllerServiceNode>> ControllerServiceNodeMap::getAllControllerServices() const {
-  std::lock_guard<std::mutex> lock(mutex_);
+  std::scoped_lock lock(mutex_);
   std::vector<std::shared_ptr<ControllerServiceNode>> services;
-  services.reserve(controller_service_nodes_.size());
-  for (const auto& [id, node] : controller_service_nodes_) {
-    services.push_back(node);
+  services.reserve(services_.size());
+  for (const auto& [controller_service_node, _parent_group]: services_ | std::views::values) {
+    services.push_back(controller_service_node);
   }
   return services;
 }
 
+const ControllerServiceNodeMap::ServiceEntry* ControllerServiceNodeMap::getEntry(const std::string_view key, const std::scoped_lock<std::mutex>&) const {
+  const auto it = services_.find(key);
+  if (it != services_.end()) {
+    return &it->second;
+  }
+  const auto primary_key_it = alternative_keys.find(key);
+  if (primary_key_it == alternative_keys.end()) {
+    return nullptr;
+  }
+  const auto it_from_primary = services_.find(primary_key_it->second);
+  gsl_Expects(it_from_primary != services_.end());
+  return &it_from_primary->second;
+}
+
+
+bool ControllerServiceNodeMap::register_alternative_key(std::string primary_key, std::string alternative_key) {
+  std::scoped_lock lock(mutex_);
+  if (!services_.contains(primary_key)) {

Review Comment:
   The `register_alternative_key` method checks if the primary key exists in `services_` but does not check if the alternative key already exists in either `services_` or `alternative_keys`. This could allow registering an alternative key that collides with an existing primary key or another alternative key, leading to inconsistent state.
   
   Add validation to ensure the alternative_key doesn't already exist: `if (!services_.contains(primary_key) || services_.contains(alternative_key) || alternative_keys.contains(alternative_key))`
   ```suggestion
     if (!services_.contains(primary_key) ||
         services_.contains(alternative_key) ||
         alternative_keys.contains(alternative_key)) {
   ```



##########
libminifi/src/core/controller/ControllerServiceNodeMap.cpp:
##########
@@ -17,81 +17,92 @@
  */
 
 #include "core/controller/ControllerServiceNodeMap.h"
+
+#include <ranges>
+
 #include "core/ProcessGroup.h"
 
 namespace org::apache::nifi::minifi::core::controller {
 
 ControllerServiceNode* ControllerServiceNodeMap::get(const std::string &id) const {
-  std::lock_guard<std::mutex> lock(mutex_);
-  auto exists = controller_service_nodes_.find(id);
-  if (exists != controller_service_nodes_.end())
-    return exists->second.get();
-  else
-    return nullptr;
+  const std::scoped_lock lock(mutex_);
+  if (const auto entry = getEntry(id, lock)) {
+    return entry->controller_service_node.get();
+  }
+  return nullptr;
 }
 
 ControllerServiceNode* ControllerServiceNodeMap::get(const std::string &id, const utils::Identifier& processor_or_controller_uuid) const {
-  std::lock_guard<std::mutex> lock(mutex_);
-  ControllerServiceNode* controller = nullptr;
-  auto exists = controller_service_nodes_.find(id);
-  if (exists != controller_service_nodes_.end()) {
-    controller = exists->second.get();
-  } else {
+  const std::scoped_lock lock(mutex_);
+  const auto entry = getEntry(id, lock);
+  if (!entry || !entry->parent_group) {
     return nullptr;
   }
 
-  auto process_group_of_controller_exists = process_groups_.find(id);
-  ProcessGroup* process_group = nullptr;
-  if (process_group_of_controller_exists != process_groups_.end()) {
-    process_group = process_group_of_controller_exists->second;
-  } else {
-    return nullptr;
-  }
 
-  if (process_group->findProcessorById(processor_or_controller_uuid, ProcessGroup::Traverse::IncludeChildren)) {
-    return controller;
+  if (entry->parent_group->findProcessorById(processor_or_controller_uuid, ProcessGroup::Traverse::IncludeChildren)) {
+    return entry->controller_service_node.get();
   }
 
-  if (process_group->findControllerService(processor_or_controller_uuid.to_string(), ProcessGroup::Traverse::IncludeChildren)) {
-    return controller;
+  if (entry->parent_group->findControllerService(processor_or_controller_uuid.to_string(), ProcessGroup::Traverse::IncludeChildren)) {
+    return entry->controller_service_node.get();
   }
 
   return nullptr;
 }
 
-bool ControllerServiceNodeMap::put(const std::string &id, const std::shared_ptr<ControllerServiceNode> &serviceNode) {
-  if (id.empty() || serviceNode == nullptr)
+bool ControllerServiceNodeMap::put(std::string id, std::shared_ptr<ControllerServiceNode> controller_service_node,
+    ProcessGroup* parent_group) {
+  if (id.empty() || controller_service_node == nullptr || alternative_keys.contains(id)) {
     return false;
-  std::lock_guard<std::mutex> lock(mutex_);
-  controller_service_nodes_[id] = serviceNode;
-  return true;
+  }
+  std::scoped_lock lock(mutex_);
+  auto [_it, success] = services_.emplace(std::move(id), ServiceEntry{.controller_service_node = std::move(controller_service_node), .parent_group = parent_group});
+  return success;
 }
 
-bool ControllerServiceNodeMap::put(const std::string &id, ProcessGroup* process_group) {
-  if (id.empty() || process_group == nullptr)
-    return false;
-  std::lock_guard<std::mutex> lock(mutex_);
-  process_groups_.emplace(id, gsl::make_not_null(process_group));
-  return true;
-}
 
 void ControllerServiceNodeMap::clear() {
-  std::lock_guard<std::mutex> lock(mutex_);
-  for (const auto& [id, node] : controller_service_nodes_) {
-    node->disable();
+  std::scoped_lock lock(mutex_);
+  for (const auto& node: services_ | std::views::values) {
+    node.controller_service_node->disable();
   }
-  controller_service_nodes_.clear();
-  process_groups_.clear();
+  services_.clear();

Review Comment:
   The `clear()` method clears the `services_` map but does not clear the `alternative_keys` map. This creates an inconsistent state where alternative keys point to services that no longer exist, which would cause the gsl_Expects assertion in `getEntry()` to fail when those alternative keys are used after a clear operation.
   
   Add `alternative_keys.clear();` after `services_.clear();` to maintain consistency.
   ```suggestion
     services_.clear();
     alternative_keys.clear();
   ```



##########
libminifi/include/core/controller/ControllerServiceNodeMap.h:
##########
@@ -43,18 +43,25 @@ class ControllerServiceNodeMap {
   ControllerServiceNode* get(const std::string &id) const;
   ControllerServiceNode* get(const std::string &id, const utils::Identifier &processor_or_controller_uuid) const;
 
-  bool put(const std::string &id, const std::shared_ptr<ControllerServiceNode> &serviceNode);
-  bool put(const std::string &id, ProcessGroup* process_group);
+  bool put(std::string id, std::shared_ptr<ControllerServiceNode> controller_service_node, ProcessGroup* parent_group);
+
+  bool register_alternative_key(std::string primary_key, std::string alternative_key);

Review Comment:
   The method name `register_alternative_key` uses snake_case, which is inconsistent with the codebase's naming convention. All other methods in this class use camelCase (e.g., `getAllControllerServices`, `getEntry`). The method should be renamed to `registerAlternativeKey` for consistency.
   ```suggestion
     bool registerAlternativeKey(std::string primary_key, std::string alternative_key);
   ```



##########
libminifi/src/core/controller/ControllerServiceNodeMap.cpp:
##########
@@ -17,81 +17,92 @@
  */
 
 #include "core/controller/ControllerServiceNodeMap.h"
+
+#include <ranges>
+
 #include "core/ProcessGroup.h"
 
 namespace org::apache::nifi::minifi::core::controller {
 
 ControllerServiceNode* ControllerServiceNodeMap::get(const std::string &id) const {
-  std::lock_guard<std::mutex> lock(mutex_);
-  auto exists = controller_service_nodes_.find(id);
-  if (exists != controller_service_nodes_.end())
-    return exists->second.get();
-  else
-    return nullptr;
+  const std::scoped_lock lock(mutex_);
+  if (const auto entry = getEntry(id, lock)) {
+    return entry->controller_service_node.get();
+  }
+  return nullptr;
 }
 
 ControllerServiceNode* ControllerServiceNodeMap::get(const std::string &id, const utils::Identifier& processor_or_controller_uuid) const {
-  std::lock_guard<std::mutex> lock(mutex_);
-  ControllerServiceNode* controller = nullptr;
-  auto exists = controller_service_nodes_.find(id);
-  if (exists != controller_service_nodes_.end()) {
-    controller = exists->second.get();
-  } else {
+  const std::scoped_lock lock(mutex_);
+  const auto entry = getEntry(id, lock);
+  if (!entry || !entry->parent_group) {
     return nullptr;
   }
 
-  auto process_group_of_controller_exists = process_groups_.find(id);
-  ProcessGroup* process_group = nullptr;
-  if (process_group_of_controller_exists != process_groups_.end()) {
-    process_group = process_group_of_controller_exists->second;
-  } else {
-    return nullptr;
-  }
 
-  if (process_group->findProcessorById(processor_or_controller_uuid, ProcessGroup::Traverse::IncludeChildren)) {
-    return controller;
+  if (entry->parent_group->findProcessorById(processor_or_controller_uuid, ProcessGroup::Traverse::IncludeChildren)) {
+    return entry->controller_service_node.get();
   }
 
-  if (process_group->findControllerService(processor_or_controller_uuid.to_string(), ProcessGroup::Traverse::IncludeChildren)) {
-    return controller;
+  if (entry->parent_group->findControllerService(processor_or_controller_uuid.to_string(), ProcessGroup::Traverse::IncludeChildren)) {
+    return entry->controller_service_node.get();
   }
 
   return nullptr;
 }
 
-bool ControllerServiceNodeMap::put(const std::string &id, const std::shared_ptr<ControllerServiceNode> &serviceNode) {
-  if (id.empty() || serviceNode == nullptr)
+bool ControllerServiceNodeMap::put(std::string id, std::shared_ptr<ControllerServiceNode> controller_service_node,
+    ProcessGroup* parent_group) {
+  if (id.empty() || controller_service_node == nullptr || alternative_keys.contains(id)) {

Review Comment:
   The check `alternative_keys.contains(id)` is performed before acquiring the mutex lock, which creates a race condition. Between the time this check is performed and the mutex is acquired, another thread could register an alternative key with the same value as `id`. This could lead to inconsistent state where both a primary key and an alternative key exist with the same value.
   
   Move this check inside the mutex-protected region after acquiring the lock to ensure thread safety.



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


Re: [PR] MINIFICPP-2710 Refactor ControllerServiceNodeMap [nifi-minifi-cpp]

Posted by "fgerlits (via GitHub)" <gi...@apache.org>.
fgerlits closed pull request #2098: MINIFICPP-2710 Refactor ControllerServiceNodeMap
URL: https://github.com/apache/nifi-minifi-cpp/pull/2098


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


Re: [PR] MINIFICPP-2710 Refactor ControllerServiceNodeMap [nifi-minifi-cpp]

Posted by "fgerlits (via GitHub)" <gi...@apache.org>.
fgerlits commented on code in PR #2098:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2098#discussion_r2960258116


##########
libminifi/src/core/FlowConfiguration.cpp:
##########
@@ -195,8 +195,8 @@ std::unique_ptr<minifi::Connection> FlowConfiguration::createConnection(const st
 }
 
 std::shared_ptr<core::controller::ControllerServiceNode> FlowConfiguration::createControllerService(const std::string &class_name, const std::string &name,
-    const utils::Identifier& uuid) {
-  std::shared_ptr<core::controller::ControllerServiceNode> controllerServicesNode = service_provider_->createControllerService(class_name, name);
+    const utils::Identifier& uuid, ProcessGroup* parent) {
+  std::shared_ptr<core::controller::ControllerServiceNode> controllerServicesNode = service_provider_->createControllerService(class_name, name, parent, uuid.to_string());

Review Comment:
   also nitpicking, but I would make the `uuid` the primary key and `name` the alternative key:
   ```suggestion
     std::shared_ptr<core::controller::ControllerServiceNode> controllerServicesNode = service_provider_->createControllerService(class_name, uuid.to_string(), parent, name);
   ```



##########
libminifi/include/core/controller/ControllerServiceNodeMap.h:
##########
@@ -43,18 +43,25 @@ class ControllerServiceNodeMap {
   ControllerServiceNode* get(const std::string &id) const;
   ControllerServiceNode* get(const std::string &id, const utils::Identifier &processor_or_controller_uuid) const;
 
-  bool put(const std::string &id, const std::shared_ptr<ControllerServiceNode> &serviceNode);
-  bool put(const std::string &id, ProcessGroup* process_group);
+  bool put(std::string id, std::shared_ptr<ControllerServiceNode> controller_service_node, ProcessGroup* parent_group);
+
+  bool registerAlternativeKey(std::string primary_key, std::string alternative_key);
 
   void clear();
   std::vector<std::shared_ptr<ControllerServiceNode>> getAllControllerServices() const;
 
  protected:
   mutable std::mutex mutex_;
-  // Map of controller service id to the controller service node
-  std::map<std::string, std::shared_ptr<ControllerServiceNode>> controller_service_nodes_;
-  // Map of controller service id to the process group that contains it
-  std::map<std::string, gsl::not_null<ProcessGroup*>> process_groups_;
+
+  struct ServiceEntry {
+    std::shared_ptr<ControllerServiceNode> controller_service_node;
+    ProcessGroup* parent_group;
+  };
+
+  const ServiceEntry* getEntry(std::string_view primary_key, const std::scoped_lock<std::mutex>& mutex) const;
+
+  std::map<std::string, ServiceEntry, std::less<>> services_;
+  std::map<std::string, std::string, std::less<>> alternative_keys;

Review Comment:
   nitpicking, but this should be `alternative_keys_`



##########
libminifi/src/core/controller/ControllerServiceNodeMap.cpp:
##########
@@ -17,81 +17,93 @@
  */
 
 #include "core/controller/ControllerServiceNodeMap.h"
+
+#include <ranges>
+
 #include "core/ProcessGroup.h"
 
 namespace org::apache::nifi::minifi::core::controller {
 
 ControllerServiceNode* ControllerServiceNodeMap::get(const std::string &id) const {
-  std::lock_guard<std::mutex> lock(mutex_);
-  auto exists = controller_service_nodes_.find(id);
-  if (exists != controller_service_nodes_.end())
-    return exists->second.get();
-  else
-    return nullptr;
+  const std::scoped_lock lock(mutex_);
+  if (const auto entry = getEntry(id, lock)) {
+    return entry->controller_service_node.get();
+  }
+  return nullptr;
 }
 
 ControllerServiceNode* ControllerServiceNodeMap::get(const std::string &id, const utils::Identifier& processor_or_controller_uuid) const {
-  std::lock_guard<std::mutex> lock(mutex_);
-  ControllerServiceNode* controller = nullptr;
-  auto exists = controller_service_nodes_.find(id);
-  if (exists != controller_service_nodes_.end()) {
-    controller = exists->second.get();
-  } else {
+  const std::scoped_lock lock(mutex_);
+  const auto entry = getEntry(id, lock);
+  if (!entry || !entry->parent_group) {
     return nullptr;
   }
 
-  auto process_group_of_controller_exists = process_groups_.find(id);
-  ProcessGroup* process_group = nullptr;
-  if (process_group_of_controller_exists != process_groups_.end()) {
-    process_group = process_group_of_controller_exists->second;
-  } else {
-    return nullptr;
-  }
 
-  if (process_group->findProcessorById(processor_or_controller_uuid, ProcessGroup::Traverse::IncludeChildren)) {
-    return controller;
+  if (entry->parent_group->findProcessorById(processor_or_controller_uuid, ProcessGroup::Traverse::IncludeChildren)) {
+    return entry->controller_service_node.get();
   }
 
-  if (process_group->findControllerService(processor_or_controller_uuid.to_string(), ProcessGroup::Traverse::IncludeChildren)) {
-    return controller;
+  if (entry->parent_group->findControllerService(processor_or_controller_uuid.to_string(), ProcessGroup::Traverse::IncludeChildren)) {
+    return entry->controller_service_node.get();
   }
 
   return nullptr;
 }
 
-bool ControllerServiceNodeMap::put(const std::string &id, const std::shared_ptr<ControllerServiceNode> &serviceNode) {
-  if (id.empty() || serviceNode == nullptr)
+bool ControllerServiceNodeMap::put(std::string id, std::shared_ptr<ControllerServiceNode> controller_service_node,
+    ProcessGroup* parent_group) {
+  std::scoped_lock lock(mutex_);
+  if (id.empty() || controller_service_node == nullptr || alternative_keys.contains(id)) {
     return false;
-  std::lock_guard<std::mutex> lock(mutex_);
-  controller_service_nodes_[id] = serviceNode;
-  return true;
+  }
+  auto [_it, success] = services_.emplace(std::move(id), ServiceEntry{.controller_service_node = std::move(controller_service_node), .parent_group = parent_group});
+  return success;
 }
 
-bool ControllerServiceNodeMap::put(const std::string &id, ProcessGroup* process_group) {
-  if (id.empty() || process_group == nullptr)
-    return false;
-  std::lock_guard<std::mutex> lock(mutex_);
-  process_groups_.emplace(id, gsl::make_not_null(process_group));
-  return true;
-}
 
 void ControllerServiceNodeMap::clear() {
-  std::lock_guard<std::mutex> lock(mutex_);
-  for (const auto& [id, node] : controller_service_nodes_) {
-    node->disable();
+  std::scoped_lock lock(mutex_);
+  for (const auto& node: services_ | std::views::values) {
+    node.controller_service_node->disable();

Review Comment:
   can we call this `service_entry`, please?
   ```suggestion
     for (const auto& service_entry: services_ | std::views::values) {
       service_entry.controller_service_node->disable();
   ```



##########
libminifi/test/libtest/unit/TestBase.cpp:
##########
@@ -368,7 +368,7 @@ std::shared_ptr<minifi::core::controller::ControllerServiceNode> TestPlan::addCo
   minifi::utils::Identifier uuid = minifi::utils::IdGenerator::getIdGenerator()->generate();
 
   std::shared_ptr<minifi::core::controller::ControllerServiceNode> controller_service_node =
-      controller_services_provider_->createControllerService(controller_name, name);
+      controller_services_provider_->createControllerService(controller_name, name, root_process_group_.get(), uuid.to_string());

Review Comment:
   switch `name` and `uuid` here, too?
   ```suggestion
         controller_services_provider_->createControllerService(controller_name, uuid.to_string(), root_process_group_.get(), name);
   ```



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


Re: [PR] MINIFICPP-2710 Refactor ControllerServiceNodeMap [nifi-minifi-cpp]

Posted by "martinzink (via GitHub)" <gi...@apache.org>.
martinzink commented on code in PR #2098:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2098#discussion_r2773878889


##########
extensions/azure/tests/ListAzureBlobStorageTests.cpp:
##########
@@ -217,12 +215,11 @@ TEST_CASE_METHOD(ListAzureBlobStorageTestsFixture, "Test credentials settings",
       credential_configuration_strategy_string = "Workload Identity";
     }
 
-    auto azure_storage_cred_service = plan_->addController("AzureStorageCredentialsService", "AzureStorageCredentialsService");

Review Comment:
   The main change in this file was I removed these duplicate ControllerServices from the stack and used the azure_storage_cred_service_ already defined in the test fixture



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