lordgamez commented on code in PR #1875: URL: https://github.com/apache/nifi-minifi-cpp/pull/1875#discussion_r1793634312
########## extensions/couchbase/controllerservices/CouchbaseClusterService.cpp: ########## @@ -0,0 +1,122 @@ +/** + * + * 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 "CouchbaseClusterService.h" + +#include "core/Resource.h" + +namespace org::apache::nifi::minifi::couchbase { + +nonstd::expected<CouchbaseUpsertResult, std::error_code> RemoteCouchbaseCollection::upsert(const std::string& document_id, const std::vector<std::byte>& buffer, + const ::couchbase::upsert_options& options) { + auto [err, resp] = collection_.upsert<::couchbase::codec::raw_binary_transcoder>(document_id, buffer, options).get(); + if (err.ec()) { + client_.setConnectionError(); + return nonstd::make_unexpected(err.ec()); + } else { + const uint64_t partition_uuid = (resp.mutation_token().has_value() ? resp.mutation_token()->partition_uuid() : 0); + const uint64_t sequence_number = (resp.mutation_token().has_value() ? resp.mutation_token()->sequence_number() : 0); + const uint16_t partition_id = (resp.mutation_token().has_value() ? resp.mutation_token()->partition_id() : 0); + return CouchbaseUpsertResult { + collection_.bucket_name(), + resp.cas().value(), + partition_uuid, + sequence_number, + partition_id + }; + } +} + +std::unique_ptr<CouchbaseCollection> CouchBaseClient::getCollection(std::string_view bucket_name, std::string_view scope_name, std::string_view collection_name) { + if (!establishConnection()) { + return nullptr; + } + return std::make_unique<RemoteCouchbaseCollection>(cluster_.bucket(bucket_name).scope(scope_name).collection(collection_name), *this); +} + +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; + } +} + +bool CouchBaseClient::establishConnection() { + std::lock_guard<std::mutex> lock(state_mutex_); + if (state_ == State::CONNECTED) { + return true; + } + + if (state_ == State::UNKNOWN) { + auto [err, resp] = cluster_.ping().get(); + if (err.ec()) { + close(); + } else { + state_ = State::CONNECTED; + return true; + } + } + + auto options = ::couchbase::cluster_options(username_, password_); + auto [connect_err, cluster] = ::couchbase::cluster::connect(connection_string_, options).get(); + if (connect_err.ec()) { + logger_->log_error("Failed to connect to Couchbase cluster: {}", connect_err.message()); + return false; + } + cluster_ = std::move(cluster); + state_ = State::CONNECTED; + return true; +} + +namespace controllers { + +void CouchbaseClusterService::initialize() { + setSupportedProperties(Properties); +} + +void CouchbaseClusterService::onEnable() { + std::string connection_string; + getProperty(ConnectionString, connection_string); + std::string username; + getProperty(UserName, username); + std::string password; + getProperty(UserPassword, password); + client_ = std::make_unique<CouchBaseClient>(connection_string, username, password, logger_); Review Comment: Good point, I added an explicit check for empty values in a9c0650247d727c21742beeeb63fbdc780b890af I also added an initial connection check if there are any fatal configuration errors (like the connection is okay, but we have wrong authentication info configured) we fail at startup. -- 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]
