szaszm commented on code in PR #1340:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1340#discussion_r905075513


##########
libminifi/src/c2/C2Client.cpp:
##########
@@ -206,35 +130,13 @@ void C2Client::loadC2ResponseConfiguration(const 
std::string &prefix) {
       }
       std::shared_ptr<state::response::ResponseNode> new_node = 
std::make_shared<state::response::ObjectNode>(name);
       if (configuration_->get(classOption, class_definitions)) {
-        std::vector<std::string> classes = 
utils::StringUtils::split(class_definitions, ",");
-        for (const std::string& clazz : classes) {
-          // instantiate the object
-          std::shared_ptr<core::CoreComponent> ptr = 
core::ClassLoader::getDefaultClassLoader().instantiate(clazz, clazz);
-          if (nullptr == ptr) {
-            const bool found_metric = [&] {
-              std::lock_guard<std::mutex> guard{metrics_mutex_};
-              auto metric = component_metrics_.find(clazz);
-              if (metric != component_metrics_.end()) {
-                ptr = metric->second;
-                return true;
-              }
-              return false;
-            }();
-            if (!found_metric) {
-              logger_->log_error("No metric defined for %s", clazz);
-              continue;
-            }
-          }
-          auto node = 
std::dynamic_pointer_cast<state::response::ResponseNode>(ptr);
-          
std::static_pointer_cast<state::response::ObjectNode>(new_node)->add_node(node);
-        }
-
+        loadNodeClasses(class_definitions, new_node);
       } else {
         std::string optionName = option + "." + name;
-        auto node = loadC2ResponseConfiguration(optionName, new_node);
+        loadC2ResponseConfiguration(optionName, new_node);
       }
 
-      std::lock_guard<std::mutex> guard{metrics_mutex_};
+      // We don't need to lock here we do it in the initializeResponseNodes

Review Comment:
   Consider adding a comma. I'm not good a grammar, but I think it helps 
readability by separating the parts of the sentence.
   ```suggestion
         // We don't need to lock here, we do it in the initializeResponseNodes
   ```



##########
libminifi/include/core/state/ConnectionStore.h:
##########
@@ -0,0 +1,60 @@
+/**
+ *
+ * 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 <map>
+#include <string>
+#include <vector>
+
+#include "Connection.h"
+#include "utils/gsl.h"
+
+namespace org::apache::nifi::minifi::state {
+
+class ConnectionStore {
+ public:
+  void updateConnection(minifi::Connection* connection) {
+    if (nullptr != connection) {
+      connections_[connection->getUUIDStr()] = connection;
+    }
+  }
+
+  std::vector<PublishedMetric> calculateConnectionMetrics(const std::string& 
metric_class) {
+    std::vector<PublishedMetric> metrics;
+
+    for (const auto& [_, connection] : connections_) {
+      metrics.push_back({"queue_data_size", 
static_cast<double>(connection->getQueueDataSize()),
+        {{"connection_uuid", connection->getUUIDStr()}, {"connection_name", 
connection->getName()}, {"metric_class", metric_class}}});
+      metrics.push_back({"queue_data_size_max", 
static_cast<double>(connection->getMaxQueueDataSize()),
+        {{"connection_uuid", connection->getUUIDStr()}, {"connection_name", 
connection->getName()}, {"metric_class", metric_class}}});
+      metrics.push_back({"queue_size", 
static_cast<double>(connection->getQueueSize()),
+        {{"connection_uuid", connection->getUUIDStr()}, {"connection_name", 
connection->getName()}, {"metric_class", metric_class}}});
+      metrics.push_back({"queue_size_max", 
static_cast<double>(connection->getMaxQueueSize()),
+        {{"connection_uuid", connection->getUUIDStr()}, {"connection_name", 
connection->getName()}, {"metric_class", metric_class}}});
+    }
+
+    return metrics;
+  }
+
+  virtual ~ConnectionStore() = default;
+
+ protected:
+  std::map<std::string, minifi::Connection*> connections_;

Review Comment:
   Consider using `std::unordered_map<utils::Identifier, Connection*>`.
   
   Untested pseudocode to help with `std::hash` specialization for `Identifier`:
   ```
   namespace std {
   template<>
   struct hash<Identifier> {
     size_t operator()(const Identifier& id) const noexcept {
       constexpr int slices = sizeof(Identifier) / sizeof(size_t);
       const auto combine = [](size_t& seed, size_t new_hash) {
         // from the boost hash_combine docs
         seed ^= new_hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
       };
       const auto get_slice = [](const Identifier& id, size_t idx) -> size_t {
         // Needs access to Identifier internals
         size_t result{};
         memcpy(&result, reinterpret_cast<const unsigned char*>(&id.data_) + 
idx * sizeof(size_t), sizeof(size_t));
         return result;
       };
       size_t hash = get_slice(id, 0);
       for (size_t i = 1; i < slices; ++i) {
         combine(hash, get_slice(id, i));
       };
       return hash;
     }
   };
   }  // namespace std
   
   
   



-- 
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-unsubscr...@nifi.apache.org

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

Reply via email to