lordgamez commented on code in PR #1875: URL: https://github.com/apache/nifi-minifi-cpp/pull/1875#discussion_r1792987782
########## 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: The unknown state means here that one of the calls resulted in a connection error and we want to check the connection state with the `ping`. If it succeeds we switch back to connected state, otherwise we reinitialize the connection. After testing a bit more yesterday I'm going to refactor the service to have an explicit `upsert` function and merge it with the `getCollection` function. I realized that the connection creation errors also need to be distinguished by temporary and fatal errors (like connection error vs authentication error) so it is easier to handle the connection and upsert error codes at a single point. -- 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]
