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 2019/08/07 19:38:43 UTC

[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #621: MINIFICPP-959: Review librdkafka thread safety

arpadboda commented on a change in pull request #621: MINIFICPP-959: Review librdkafka thread safety
URL: https://github.com/apache/nifi-minifi-cpp/pull/621#discussion_r311719388
 
 

 ##########
 File path: extensions/librdkafka/KafkaConnection.h
 ##########
 @@ -0,0 +1,197 @@
+/**
+ * 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.
+ */
+
+#ifndef NIFI_MINIFI_CPP_KAFKACONNECTION_H
+#define NIFI_MINIFI_CPP_KAFKACONNECTION_H
+
+#include <atomic>
+#include <mutex>
+#include <string>
+#include "core/logging/LoggerConfiguration.h"
+#include "core/logging/Logger.h"
+#include "rdkafka.h"
+#include "KafkaTopic.h"
+
+class KafkaConnectionKey {
+public:
+    std::string client_id_;
+    std::string brokers_;
+};
+
+class KafkaConnection {
+ public:
+
+  explicit KafkaConnection(const KafkaConnectionKey &key)
+      : logger_(logging::LoggerFactory<KafkaConnection>::getLogger()),
+        conf_(nullptr),
+        kafka_connection_(nullptr) {
+    // Obtain lease before unlocking KafkaPool.
+    lease_ = true;
+    initialized_ = false;
+    key_ = key;
+  }
+
+  ~KafkaConnection() {
+    remove();
+  }
+
+  void remove() {
+    topics_.clear();
+    removeConnection();
+  }
+
+  void removeConnection() {
+    if (kafka_connection_) {
+      rd_kafka_flush(kafka_connection_, 10 * 1000); /* wait for max 10 seconds */
+      rd_kafka_destroy(kafka_connection_);
+      modifyLoggers([&](std::unordered_map<const rd_kafka_t*, std::weak_ptr<logging::Logger>>& loggers) {
+        loggers.erase(kafka_connection_);
+      });
+      kafka_connection_ = nullptr;
+    }
+    if (conf_) {
+      rd_kafka_conf_destroy(conf_);
+      conf_ = nullptr;
+    }
+    initialized_ = false;
+  }
+
+  bool initialized() {
+    return initialized_;
+  }
+
+  void setConnection(rd_kafka_t *producer, rd_kafka_conf_t *conf) {
+    std::lock_guard<std::mutex> lock(mutex_);
+    removeConnection();
+    kafka_connection_ = producer;
+    conf_ = conf;
+    initialized_ = true;
+    modifyLoggers([&](std::unordered_map<const rd_kafka_t*, std::weak_ptr<logging::Logger>>& loggers) {
+      loggers[producer] = logger_;
+    });
+  }
+
+  rd_kafka_conf_t *getConf() {
+    return conf_;
+  }
+
+  rd_kafka_t *getConnection() {
+    return kafka_connection_;
+  }
+
+  bool hasTopic(const std::string &topic) {
+    return topics_.find(topic) != std::end(topics_);
 
 Review comment:
   Map has count, which returns 0 or 1 (ya, someone in c++ committee definitely had two more beers than he should had). That's just nitpicking, but this func should be const, too. 

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services