|
| 1 | +/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | +/* |
| 3 | + * Copyright 2020-Present Couchbase, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "lookup_in_all_replicas.hxx" |
| 19 | +#include "lookup_in_replica.hxx" |
| 20 | + |
| 21 | +#include "core/cluster.hxx" |
| 22 | +#include "core/error_context/key_value.hxx" |
| 23 | +#include "core/operations/document_lookup_in.hxx" |
| 24 | +#include "core/topology/configuration.hxx" |
| 25 | + |
| 26 | +namespace couchbase::core::impl |
| 27 | +{ |
| 28 | + |
| 29 | +void |
| 30 | +initiate_lookup_in_all_replicas_operation(std::shared_ptr<cluster> core, |
| 31 | + const std::string& bucket_name, |
| 32 | + const std::string& scope_name, |
| 33 | + const std::string& collection_name, |
| 34 | + std::string document_key, |
| 35 | + const std::vector<subdoc::command>& specs, |
| 36 | + lookup_in_all_replicas_options::built options, |
| 37 | + lookup_in_all_replicas_handler&& handler) |
| 38 | +{ |
| 39 | + return initiate_lookup_in_all_replicas_operation(std::move(core), |
| 40 | + bucket_name, |
| 41 | + scope_name, |
| 42 | + collection_name, |
| 43 | + std::move(document_key), |
| 44 | + specs, |
| 45 | + options.timeout, |
| 46 | + movable_lookup_in_all_replicas_handler{ std::move(handler) }); |
| 47 | +} |
| 48 | + |
| 49 | +void |
| 50 | +initiate_lookup_in_all_replicas_operation(std::shared_ptr<cluster> core, |
| 51 | + const std::string& bucket_name, |
| 52 | + const std::string& scope_name, |
| 53 | + const std::string& collection_name, |
| 54 | + std::string document_key, |
| 55 | + const std::vector<subdoc::command>& specs, |
| 56 | + std::optional<std::chrono::milliseconds> timeout, |
| 57 | + movable_lookup_in_all_replicas_handler&& handler) |
| 58 | +{ |
| 59 | + auto request = std::make_shared<couchbase::core::impl::lookup_in_all_replicas_request>( |
| 60 | + bucket_name, scope_name, collection_name, std::move(document_key), specs, timeout); |
| 61 | + core->with_bucket_configuration( |
| 62 | + bucket_name, |
| 63 | + [core, r = std::move(request), h = std::move(handler)](std::error_code ec, const core::topology::configuration& config) mutable { |
| 64 | + if (!config.supports_subdoc_read_replica()) { |
| 65 | + ec = errc::common::feature_not_available; |
| 66 | + } |
| 67 | + |
| 68 | + if (ec) { |
| 69 | + std::optional<std::string> first_error_path{}; |
| 70 | + std::optional<std::size_t> first_error_index{}; |
| 71 | + return h( |
| 72 | + make_subdocument_error_context(make_key_value_error_context(ec, r->id()), ec, first_error_path, first_error_index, false), |
| 73 | + lookup_in_all_replicas_result{}); |
| 74 | + } |
| 75 | + struct replica_context { |
| 76 | + replica_context(movable_lookup_in_all_replicas_handler handler, std::uint32_t expected_responses) |
| 77 | + : handler_(std::move(handler)) |
| 78 | + , expected_responses_(expected_responses) |
| 79 | + { |
| 80 | + } |
| 81 | + |
| 82 | + movable_lookup_in_all_replicas_handler handler_; |
| 83 | + std::uint32_t expected_responses_; |
| 84 | + bool done_{ false }; |
| 85 | + std::mutex mutex_{}; |
| 86 | + lookup_in_all_replicas_result result_{}; |
| 87 | + }; |
| 88 | + auto ctx = std::make_shared<replica_context>(std::move(h), config.num_replicas.value_or(0U) + 1U); |
| 89 | + |
| 90 | + for (std::size_t idx = 1U; idx <= config.num_replicas.value_or(0U); ++idx) { |
| 91 | + document_id replica_id{ r->id() }; |
| 92 | + replica_id.node_index(idx); |
| 93 | + core->execute( |
| 94 | + impl::lookup_in_replica_request{ std::move(replica_id), r->specs(), r->timeout() }, |
| 95 | + [ctx](impl::lookup_in_replica_response&& resp) { |
| 96 | + movable_lookup_in_all_replicas_handler local_handler{}; |
| 97 | + { |
| 98 | + std::scoped_lock lock(ctx->mutex_); |
| 99 | + if (ctx->done_) { |
| 100 | + return; |
| 101 | + } |
| 102 | + --ctx->expected_responses_; |
| 103 | + if (resp.ctx.ec()) { |
| 104 | + if (ctx->expected_responses_ > 0) { |
| 105 | + // just ignore the response |
| 106 | + return; |
| 107 | + } |
| 108 | + } else { |
| 109 | + std::vector<lookup_in_replica_result::entry> entries{}; |
| 110 | + for (auto& field : resp.fields) { |
| 111 | + lookup_in_replica_result::entry lookup_in_entry{}; |
| 112 | + lookup_in_entry.path = field.path; |
| 113 | + lookup_in_entry.value = field.value; |
| 114 | + lookup_in_entry.exists = field.exists; |
| 115 | + lookup_in_entry.original_index = field.original_index; |
| 116 | + entries.emplace_back(lookup_in_entry); |
| 117 | + } |
| 118 | + ctx->result_.emplace_back(lookup_in_replica_result{ resp.cas, entries, resp.deleted, true /* replica */ }); |
| 119 | + } |
| 120 | + if (ctx->expected_responses_ == 0) { |
| 121 | + ctx->done_ = true; |
| 122 | + std::swap(local_handler, ctx->handler_); |
| 123 | + } |
| 124 | + } |
| 125 | + if (local_handler) { |
| 126 | + if (!ctx->result_.empty()) { |
| 127 | + resp.ctx.override_ec({}); |
| 128 | + } |
| 129 | + return local_handler(std::move(resp.ctx), std::move(ctx->result_)); |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + core::operations::lookup_in_request active{ document_id{ r->id() } }; |
| 135 | + active.specs = r->specs(); |
| 136 | + active.timeout = r->timeout(); |
| 137 | + core->execute(active, [ctx](core::operations::lookup_in_response&& resp) { |
| 138 | + movable_lookup_in_all_replicas_handler local_handler{}; |
| 139 | + { |
| 140 | + std::scoped_lock lock(ctx->mutex_); |
| 141 | + if (ctx->done_) { |
| 142 | + return; |
| 143 | + } |
| 144 | + --ctx->expected_responses_; |
| 145 | + if (resp.ctx.ec()) { |
| 146 | + if (ctx->expected_responses_ > 0) { |
| 147 | + // just ignore the response |
| 148 | + return; |
| 149 | + } |
| 150 | + } else { |
| 151 | + std::vector<lookup_in_replica_result::entry> entries{}; |
| 152 | + for (auto& field : resp.fields) { |
| 153 | + lookup_in_replica_result::entry lookup_in_entry{}; |
| 154 | + lookup_in_entry.path = field.path; |
| 155 | + lookup_in_entry.value = field.value; |
| 156 | + lookup_in_entry.exists = field.exists; |
| 157 | + lookup_in_entry.original_index = field.original_index; |
| 158 | + entries.emplace_back(lookup_in_entry); |
| 159 | + } |
| 160 | + ctx->result_.emplace_back(lookup_in_replica_result{ resp.cas, entries, resp.deleted, false /* active */ }); |
| 161 | + } |
| 162 | + if (ctx->expected_responses_ == 0) { |
| 163 | + ctx->done_ = true; |
| 164 | + std::swap(local_handler, ctx->handler_); |
| 165 | + } |
| 166 | + } |
| 167 | + if (local_handler) { |
| 168 | + if (!ctx->result_.empty()) { |
| 169 | + resp.ctx.override_ec({}); |
| 170 | + } |
| 171 | + return local_handler(std::move(resp.ctx), std::move(ctx->result_)); |
| 172 | + } |
| 173 | + }); |
| 174 | + }); |
| 175 | +} |
| 176 | +} // namespace couchbase::core::impl |
0 commit comments