You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by lo...@apache.org on 2024/10/30 09:51:59 UTC

(nifi-minifi-cpp) 05/15: Remove connection state management

This is an automated email from the ASF dual-hosted git repository.

lordgamez pushed a commit to branch MINIFICPP-2469
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git

commit 6e74934c5a064d5b3c6fdf07bbe5851314c3090e
Author: Gabor Gyimesi <ga...@gmail.com>
AuthorDate: Thu Oct 10 10:00:01 2024 +0200

    Remove connection state management
---
 .../controllerservices/CouchbaseClusterService.cpp | 29 +++-------------------
 .../controllerservices/CouchbaseClusterService.h   | 11 +-------
 2 files changed, 5 insertions(+), 35 deletions(-)

diff --git a/extensions/couchbase/controllerservices/CouchbaseClusterService.cpp b/extensions/couchbase/controllerservices/CouchbaseClusterService.cpp
index fbd8bfeea..f529a7fc9 100644
--- a/extensions/couchbase/controllerservices/CouchbaseClusterService.cpp
+++ b/extensions/couchbase/controllerservices/CouchbaseClusterService.cpp
@@ -35,7 +35,7 @@ nonstd::expected<::couchbase::collection, CouchbaseErrorType> CouchbaseClient::g
   if (auto error_type = establishConnection()) {
     return nonstd::make_unexpected(*error_type);
   }
-  return cluster_.bucket(collection.bucket_name).scope(collection.scope_name).collection(collection.collection_name);
+  return cluster_->bucket(collection.bucket_name).scope(collection.scope_name).collection(collection.collection_name);
 }
 
 nonstd::expected<CouchbaseUpsertResult, CouchbaseErrorType> CouchbaseClient::upsert(const CouchbaseCollection& collection,
@@ -47,7 +47,6 @@ nonstd::expected<CouchbaseUpsertResult, CouchbaseErrorType> CouchbaseClient::ups
 
   auto [upsert_err, upsert_resp] = collection_result->upsert<::couchbase::codec::raw_binary_transcoder>(document_id, buffer, options).get();
   if (upsert_err.ec()) {
-    setConnectionError();
     // ambiguous_timeout should not be retried as we do not know if the insert was successful or not
     if (getErrorType(upsert_err.ec()) == CouchbaseErrorType::TEMPORARY && upsert_err.ec().value() != static_cast<int>(::couchbase::errc::common::ambiguous_timeout)) {
       logger_->log_error("Failed to upsert document '{}' to collection '{}.{}.{}' due to temporary issue, error code: '{}', message: '{}'",
@@ -71,36 +70,17 @@ nonstd::expected<CouchbaseUpsertResult, CouchbaseErrorType> CouchbaseClient::ups
   }
 }
 
-void CouchbaseClient::setConnectionError() {
-  std::lock_guard<std::mutex> lock(state_mutex_);
-  state_ = State::UNKNOWN;
-}
-
 void CouchbaseClient::close() {
-  std::lock_guard<std::mutex> lock(state_mutex_);
-  if (state_ == State::CONNECTED || state_ == State::UNKNOWN) {
-    cluster_.close().wait();
-    state_ = State::DISCONNECTED;
+  if (cluster_) {
+    cluster_->close().wait();
   }
 }
 
 std::optional<CouchbaseErrorType> CouchbaseClient::establishConnection() {
-  std::lock_guard<std::mutex> lock(state_mutex_);
-  if (state_ == State::CONNECTED) {
+  if (cluster_) {
     return std::nullopt;
   }
 
-  if (state_ == State::UNKNOWN) {
-    auto [err, upsert_resp] = cluster_.ping().get();
-    if (err.ec()) {
-      close();
-      state_ = State::DISCONNECTED;
-    } else {
-      state_ = State::CONNECTED;
-      return std::nullopt;
-    }
-  }
-
   auto options = ::couchbase::cluster_options(username_, password_);
   auto [connect_err, cluster] = ::couchbase::cluster::connect(connection_string_, options).get();
   if (connect_err.ec()) {
@@ -108,7 +88,6 @@ std::optional<CouchbaseErrorType> CouchbaseClient::establishConnection() {
     return getErrorType(connect_err.ec());
   }
   cluster_ = std::move(cluster);
-  state_ = State::CONNECTED;
   return std::nullopt;
 }
 
diff --git a/extensions/couchbase/controllerservices/CouchbaseClusterService.h b/extensions/couchbase/controllerservices/CouchbaseClusterService.h
index 0890e0dcd..6b85c37b8 100644
--- a/extensions/couchbase/controllerservices/CouchbaseClusterService.h
+++ b/extensions/couchbase/controllerservices/CouchbaseClusterService.h
@@ -55,12 +55,6 @@ enum class CouchbaseErrorType {
 
 class CouchbaseClient {
  public:
-  enum class State {
-    DISCONNECTED,
-    CONNECTED,
-    UNKNOWN,
-  };
-
   CouchbaseClient(std::string connection_string, std::string username, std::string password, const std::shared_ptr<core::logging::Logger>& logger)
     : connection_string_(std::move(connection_string)), username_(std::move(username)), password_(std::move(password)), logger_(logger) {
   }
@@ -84,14 +78,11 @@ class CouchbaseClient {
 
   static CouchbaseErrorType getErrorType(const std::error_code& error_code);
   nonstd::expected<::couchbase::collection, CouchbaseErrorType> getCollection(const CouchbaseCollection& collection);
-  void setConnectionError();
 
-  std::mutex state_mutex_;
-  State state_ = State::DISCONNECTED;
   std::string connection_string_;
   std::string username_;
   std::string password_;
-  ::couchbase::cluster cluster_;
+  std::optional<::couchbase::cluster> cluster_;
   std::shared_ptr<core::logging::Logger> logger_;
 };