lordgamez commented on code in PR #1875: URL: https://github.com/apache/nifi-minifi-cpp/pull/1875#discussion_r1794922869
########## 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(); Review Comment: After testing connection error handling further it seems that the connection state management is unnecessary on our part. When the connection is interrupted or couchbase goes offline the `::couchbase::cluster` object seems to handle these interruptions automatically and restores the connection when couchbase is available again without a need for manual reconnection. I removed the state management in bbfa6f6a697db57a4452b49763817bd350cc3835 -- 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]
