/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *   Copyright 2020-2021 Couchbase, Inc.
 *
 *   Licensed 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 "bucket.hxx"

#include "bucket_unit_test_api.hxx"

#include "collection_id_cache_entry.hxx"
#include "core/app_telemetry_meter.hxx"
#include "core/cluster_options.hxx"
#include "core/config_listener.hxx"
#include "core/document_id.hxx"
#include "core/error_context/key_value_error_map_info.hxx"
#include "core/error_context/key_value_status_code.hxx"
#include "core/io/mcbp_message.hxx"
#include "core/logger/logger.hxx"
#include "core/logger/redaction.hxx"
#include "core/mcbp/codec.hxx"
#include "core/metrics/meter_wrapper.hxx"
#include "core/protocol/client_opcode.hxx"
#include "core/protocol/client_request.hxx"
#include "core/protocol/hello_feature.hxx"
#include "core/response_handler.hxx"
#include "core/service_type.hxx"
#include "core/tracing/constants.hxx"
#include "core/tracing/tracer_wrapper.hxx"
#include "core/utils/movable_function.hxx"
#include "dispatcher.hxx"
#include "impl/bootstrap_state_listener.hxx"
#include "mcbp/operation_queue.hxx"
#include "mcbp/queue_request.hxx"
#include "mcbp/queue_response.hxx"
#include "origin.hxx"
#include "ping_collector.hxx"
#include "protocol/cmd_get_cluster_config.hxx"
#include "retry_orchestrator.hxx"

#include <couchbase/build_config.hxx>
#include <couchbase/error_codes.hxx>
#include <couchbase/retry_reason.hxx>
#include <couchbase/retry_strategy.hxx>

#include <asio/bind_executor.hpp>
#include <asio/error.hpp>
#include <asio/io_context.hpp>
#include <asio/post.hpp>
#include <asio/ssl/context.hpp>
#include <gsl/span>
#include <spdlog/fmt/bundled/core.h>

#include <algorithm>
#include <chrono>
#include <cstddef>
#include <cstdint>

#include <iterator>
#include <map>
#include <memory>
#include <mutex>
#include <optional>
#include <queue>
#include <string>
#include <system_error>
#include <utility>
#include <vector>

namespace couchbase::core
{
class bucket_impl
  : public std::enable_shared_from_this<bucket_impl>
  , public config_listener
  , public response_handler
{
public:
  bucket_impl(std::string client_id,
              std::string name,
              couchbase::core::origin origin,
              std::shared_ptr<tracing::tracer_wrapper> tracer,
              std::shared_ptr<metrics::meter_wrapper> meter,
              std::shared_ptr<orphan_reporter> orphan_reporter,
              std::shared_ptr<core::app_telemetry_meter> app_telemetry,
              std::vector<protocol::hello_feature> known_features,
              std::shared_ptr<impl::bootstrap_state_listener> state_listener,
              asio::io_context& ctx,
              tls_context_provider& tls)
    : client_id_{ std::move(client_id) }
    , name_{ std::move(name) }
    , log_prefix_{ fmt::format("[{}/{}]", client_id_, logger::metadata(name_)) }
    , tracer_{ std::move(tracer) }
    , meter_{ std::move(meter) }
    , orphan_reporter_{ std::move(orphan_reporter) }
    , app_telemetry_meter_{ std::move(app_telemetry) }
    , known_features_{ std::move(known_features) }
    , state_listener_{ std::move(state_listener) }
    , origin_{ std::move(origin) }
    , codec_{ { known_features_.begin(), known_features_.end() } }
    , ctx_{ ctx }
    , tls_{ tls }
    , heartbeat_timer_(ctx_)
    , heartbeat_interval_{ origin_.options().config_poll_floor >
                               origin_.options().config_poll_interval
                             ? origin_.options().config_poll_floor
                             : origin_.options().config_poll_interval }
  {
  }

  auto resolve_response(const std::shared_ptr<mcbp::queue_request>& req,
                        const std::shared_ptr<mcbp::queue_response>& resp,
                        std::error_code ec,
                        retry_reason reason,
                        std::optional<key_value_error_map_info> error_info)
  {
    // TODO(SA): copy from mcbp_command, subject to refactor later
#ifdef COUCHBASE_CXX_CLIENT_CREATE_OPERATION_SPAN_IN_CORE
    metrics::metric_attributes attrs{
      tracing::service::key_value,
      fmt::format("{}", req->command_),
      metrics::standardized_error_type(ec),
      name_,
      req->scope_name_,
      req->collection_name_,
    };
    meter_->record_value(std::move(attrs), req->dispatched_time_);
#endif

    if (ec == asio::error::operation_aborted) {
      // TODO(SA): fix tracing
      //  self->span_->add_tag(tracing::attributes::orphan, "aborted");
      return req->try_callback(resp,
                               req->idempotent() ? errc::common::unambiguous_timeout
                                                 : errc::common::ambiguous_timeout);
    }
    if (ec == errc::common::request_canceled) {
      if (!req->idempotent() && !allows_non_idempotent_retry(reason)) {
        // TODO(SA): fix tracing
        // self->span_->add_tag(tracing::attributes::orphan, "canceled");
        return req->try_callback(resp, ec);
      }
      if (!backoff_and_retry(req,
                             reason == retry_reason::do_not_retry ? retry_reason::node_not_available
                                                                  : reason)) {
        // The retry was refused, so nothing else will carry this request: complete it here, as
        // every other caller of backoff_and_retry() does.
        req->try_callback(resp, ec);
      }
      return;
    }
    key_value_status_code status{ key_value_status_code::unknown };
    if (resp) {
      status = resp->status_code_;
    }
    if (status == key_value_status_code::not_my_vbucket) {
      reason = retry_reason::key_value_not_my_vbucket;
    }
    if (status == key_value_status_code::unknown && error_info &&
        error_info.value().has_retry_attribute()) {
      reason = retry_reason::key_value_error_map_retry_indicated;
    } else {
      switch (status) {
        case key_value_status_code::unknown_collection:
          /*
           * SDK-RFC 0049 (SDK3 Retry Handling), section "Response Code Retry List":
           * > Explicitly excluded here is the GetCollectionID KV command, since it returns 0x88 if
           * > the collection is not found.
           *
           * The request completes with collection_not_found.
           */
          if (req->command_ == protocol::client_opcode::get_collection_id) {
            reason = retry_reason::do_not_retry;
            break;
          }
          /**
           * The server does not recognise the collection id we sent. This covers two cases:
           *
           *   1. The collection was dropped and recreated, so its id changed. The cached id we sent
           *      is now stale and re-sending it would loop forever. We must invalidate the cache
           *      and re-resolve the id via GetCollectionID before retrying.
           *   2. The node's collection manifest simply lags ns_server's view: the id is still valid
           *      but has not propagated to this particular KV node yet. Here re-sending the same id
           *      succeeds once the manifest catches up, bounded by the operation timeout.
           *
           * Prefer re-resolution: if the request was dispatched through the collection-id cache the
           * hook invalidates the cached id and schedules a re-resolution retry (covering case 1,
           * and case 2 too since GetCollectionID re-fetches). This matches the legacy mcbp_command
           * path, which handled both via handle_unknown_collection(); when KV ops were migrated to
           * this path that handling was dropped and the status surfaced directly as
           * collection_not_found, which consumers such as the range scan orchestrator treat as
           * fatal.
           *
           * If there is no hook (request bypassed the cid cache) or re-resolution gave up, fall
           * back to a bounded collection-outdated retry with the same id, which still recovers
           * case 2.
           */
          if (req->on_unknown_collection_ && req->on_unknown_collection_()) {
            return;
          }
          reason = retry_reason::key_value_collection_outdated;
          break;
        case key_value_status_code::config_only:
          /**
           * The node has a configuration but is not serving data operations yet (e.g. during
           * warmup or rebalance). Mirror the legacy path: fetch a fresh configuration and retry so
           * the request is routed to a node that can serve it.
           */
          CB_LOG_DEBUG(
            "{} server returned config_only status, meaning that the node does not serve "
            "data operations, requesting new configuration and retrying",
            log_prefix_);
          fetch_config();
          reason = retry_reason::service_response_code_indicated;
          break;
        case key_value_status_code::locked:
          if (req->command_ != protocol::client_opcode::unlock) {
            /**
             * special case for unlock command, when it should not be retried, because it does not
             * make sense (someone else unlocked the document)
             */
            reason = retry_reason::key_value_locked;
          }
          break;
        case key_value_status_code::temporary_failure:
          reason = retry_reason::key_value_temporary_failure;
          break;
        case key_value_status_code::sync_write_in_progress:
          reason = retry_reason::key_value_sync_write_in_progress;
          break;
        case key_value_status_code::sync_write_re_commit_in_progress:
          reason = retry_reason::key_value_sync_write_re_commit_in_progress;
          break;
        default:
          break;
      }
    }
    if (reason == retry_reason::do_not_retry || !backoff_and_retry(req, reason)) {
      return req->try_callback(resp, ec);
    }
  }

  void handle_response(std::shared_ptr<mcbp::queue_request> req,
                       std::optional<io::mcbp_session> /* session */,
                       std::error_code error,
                       retry_reason reason,
                       io::mcbp_message msg,
                       std::optional<key_value_error_map_info> error_info) override
  {
    std::shared_ptr<mcbp::queue_response> resp{};
    auto header = msg.header_data();
    auto [packet, size, err] =
      codec_.decode_packet(gsl::span(header.data(), header.size()), msg.body);
    if (err) {
      error = errc::network::protocol_error;
    } else {
      resp = std::make_shared<mcbp::queue_response>(std::move(packet));
    }
    resolve_response(req, resp, error, reason, std::move(error_info));
  }

  auto direct_dispatch(std::shared_ptr<mcbp::queue_request> req) -> std::error_code
  {
    if (closed_) {
      req->cancel(errc::network::bucket_closed);
      return errc::network::bucket_closed;
    }
    if (!configured_) {
      return defer_command([self = shared_from_this(), req](std::error_code ec) {
        if (ec == errc::common::request_canceled) {
          return req->cancel(ec);
        }
        // This runs after direct_dispatch() returned, so its error code has nobody left to report
        // to. Complete the request here rather than leave it to its deadline.
        if (auto rc = self->direct_dispatch(req); rc) {
          req->try_callback({}, rc);
        }
      });
    }

    req->dispatched_time_ = std::chrono::steady_clock::now();

    auto routed = route_request(req);
    if (!routed.session || !routed.session->has_config()) {
      if (!routed.connect_pending) {
        // No bootstrap was started for this node, and only a bootstrap
        // completion or a bucket close drains the deferred queue. Deferring
        // here would leave the request waiting for its deadline, so retry
        // instead: that re-routes against the current configuration.
        if (backoff_and_retry(req, retry_reason::node_not_available)) {
          return {};
        }
        return errc::common::service_not_available;
      }
      return defer_command([self = shared_from_this(), req](std::error_code ec) {
        if (ec == errc::common::request_canceled) {
          return req->cancel(ec);
        }
        if (auto rc = self->direct_dispatch(req); rc) {
          req->try_callback({}, rc);
        }
      });
    }
    auto session = routed.session;
    if (session->is_stopped()) {
      if (backoff_and_retry(req, retry_reason::node_not_available)) {
        return {};
      }
      return errc::common::service_not_available;
    }
    req->opaque_ = session->next_opaque();
    session->write_and_subscribe(req, shared_from_this());
    return {};
  }

  auto direct_re_queue(const std::shared_ptr<mcbp::queue_request>& req, bool is_retry)
    -> std::error_code
  {
    auto handle_error = [is_retry, req](std::error_code ec) {
      // We only want to log an error on retries if the error isn't cancelled.
      if (!is_retry || ec != errc::common::request_canceled) {
        CB_LOG_ERROR("reschedule failed, failing request ({})", ec.message());
      }

      req->try_callback({}, ec);
    };

    CB_LOG_DEBUG("request being re-queued. opaque={}, opcode={}", req->opaque_, req->command_);

    auto routed = route_request(req);
    if (!routed.session || !routed.session->has_config()) {
      if (!routed.connect_pending) {
        // As in direct_dispatch(): no bootstrap is pending for this node, so a
        // deferred request would wait for its deadline instead of being sent.
        if (backoff_and_retry(req, retry_reason::node_not_available)) {
          return {};
        }
        // A re-queue is driven by a backoff timer that ignores what this
        // returns, so the request has to be completed here.
        handle_error(errc::common::service_not_available);
        return errc::common::service_not_available;
      }
      return defer_command([self = shared_from_this(), req, handle_error](std::error_code ec) {
        if (ec == errc::common::request_canceled) {
          return req->cancel(ec);
        }
        if (auto rc = self->direct_dispatch(req); rc) {
          handle_error(rc);
        }
      });
    }
    auto session = routed.session;
    if (session->is_stopped()) {
      if (backoff_and_retry(req, retry_reason::node_not_available)) {
        return {};
      }
      handle_error(errc::common::service_not_available);
      return errc::common::service_not_available;
    }
    req->opaque_ = session->next_opaque();
    auto data = codec_.encode_packet(*req);
    if (!data) {
      CB_LOG_DEBUG("unable to encode packet. ec={}", data.error().message());
      handle_error(data.error());
      return data.error();
    }
    session->write_and_subscribe(
      req->opaque_,
      std::move(data.value()),
      [self = shared_from_this(), req, session](
        std::error_code error,
        retry_reason reason,
        io::mcbp_message msg,
        std::optional<key_value_error_map_info> error_info) {
        std::shared_ptr<mcbp::queue_response> resp{};
        auto header = msg.header_data();
        auto [packet, size, err] =
          self->codec_.decode_packet(gsl::span(header.data(), header.size()), msg.body);
        if (err) {
          error = errc::network::protocol_error;
        } else {
          resp = std::make_shared<mcbp::queue_response>(std::move(packet));
        }
        return self->resolve_response(req, resp, error, reason, std::move(error_info));
      });
    return {};
  }

  auto backoff_and_retry(const std::shared_ptr<mcbp::queue_request>& request, retry_reason reason)
    -> bool
  {
    auto action = retry_orchestrator::should_retry(request, reason);
    if (!action.need_to_retry()) {
      return false;
    }

    // Park the request where close() can reach it. The timer below is the only
    // thing that would re-dispatch it, and asio abandons a pending wait when the
    // io_context goes away rather than completing it, so a request reachable only
    // from that handler is neither completed nor released. A queue also lets
    // cancel() take it back out, since a request de-queues from whichever queue
    // holds it.
    if (auto ec = retry_backoff_queue_.push(request, 0); ec) {
      // Nothing will retry it: the bucket has closed, or it is already cancelled.
      request->try_callback({}, errc::common::request_canceled);
      return true;
    }

    auto timer = std::make_shared<asio::steady_timer>(ctx_);
    timer->expires_after(action.duration());
    timer->async_wait([self = shared_from_this(), request](auto error) {
      if (!self->retry_backoff_queue_.remove(request)) {
        // close() completed it, or it was cancelled: either way it is done.
        return;
      }
      if (error == asio::error::operation_aborted) {
        request->try_callback({}, errc::common::request_canceled);
        return;
      }
      self->direct_re_queue(request, true);
    });
    request->set_retry_backoff(timer);
    return true;
  }

  struct routed_session {
    std::optional<io::mcbp_session> session{};

    /**
     * False when no session exists for the node and no bootstrap was started
     * for it, so nothing will drain the deferred queue. The caller must retry
     * rather than defer; see connect_session().
     *
     * The default is false. route_request() returns a default-constructed
     * value when the configuration names no server for the vbucket, and a
     * request deferred on that has nothing to wait for.
     */
    bool connect_pending{ false };
  };

  auto route_request(const std::shared_ptr<mcbp::queue_request>& req) -> routed_session
  {
    if (req->key_.empty()) {
      if (auto server = server_by_vbucket(req->vbucket_, req->replica_index_); server) {
        return find_or_connect_session_by_index(server.value());
      }
    } else if (auto [partition, server] = map_id(req->key_, req->replica_index_); server) {
      req->vbucket_ = partition;
      return find_or_connect_session_by_index(server.value());
    }
    return {};
  }

  [[nodiscard]] auto server_by_vbucket(std::uint16_t vbucket, std::size_t node_index)
    -> std::optional<std::size_t>
  {
    const std::scoped_lock lock(config_mutex_);
    if (config_) {
      return config_->server_by_vbucket(vbucket, node_index);
    }
    return std::nullopt;
  }

  [[nodiscard]] auto map_id(const document_id& id)
    -> std::pair<std::uint16_t, std::optional<std::size_t>>
  {
    const std::scoped_lock lock(config_mutex_);
    if (config_) {
      return config_->map_key(id.key(), id.node_index());
    }
    return { 0, std::nullopt };
  }

  [[nodiscard]] auto config_snapshot() const -> std::shared_ptr<topology::configuration>
  {
    const std::scoped_lock lock(config_mutex_);
    return config_;
  }

  auto config_rev() const -> std::string
  {
    const std::scoped_lock lock(config_mutex_);
    if (config_) {
      return config_->rev_str();
    }
    return "<no-config>";
  }

  [[nodiscard]] auto map_id(const std::vector<std::byte>& key, std::size_t node_index)
    -> std::pair<std::uint16_t, std::optional<std::size_t>>
  {
    const std::scoped_lock lock(config_mutex_);
    if (config_) {
      return config_->map_key(key, node_index);
    }
    return { 0, std::nullopt };
  }

  /**
   * A command deferred for this index is only ever dispatched or completed by
   * one of two events: a bootstrap finishing, or the bucket closing.
   *
   * @return true when one of them is coming. Either a bootstrap is now pending
   * for this index, or the bucket is closed and defer_command() completes the
   * command with a cancellation. False when neither is, in which case the
   * caller must retry: a command deferred now would wait for its deadline.
   */
  [[nodiscard]] auto connect_session(std::size_t index) -> bool
  {
    const std::scoped_lock lock(config_mutex_, sessions_mutex_);
    if (!config_) {
      return false;
    }
    if (closed_) {
      // Do not connect a session into a bucket that is being torn down: close() has already stopped
      // sessions_ and will not run again, so a session added here would never be stopped and would
      // strand the whole bucket/session graph as a pure cycle (see the closed_ guard in bootstrap()
      // and update_config()).
      return true;
    }

    if (index >= config_->nodes.size()) {
      // The index was resolved against an earlier configuration and the node it
      // named is no longer in the topology, so there is nothing to connect to.
      return false;
    }

    const auto& node = config_->nodes[index];

    const auto& hostname = node.hostname_for(origin_.options().network);
    auto port = node.port_or(
      origin_.options().network, service_type::key_value, origin_.options().enable_tls, 0);
    if (port == 0) {
      // The node is in the topology but advertises no key-value port, so there
      // is no session to open for it either.
      return false;
    }

    const couchbase::core::origin origin(origin_.credentials(), hostname, port, origin_.options());
    io::mcbp_session session =
      origin_.options().enable_tls
        ? io::mcbp_session(
            client_id_, node.node_uuid, ctx_, tls_, origin, state_listener_, name_, known_features_)
        : io::mcbp_session(
            client_id_, node.node_uuid, ctx_, origin, state_listener_, name_, known_features_);
    CB_LOG_DEBUG(R"({} rev={}, connect idx={}, session="{}", address="{}")",
                 log_prefix_,
                 config_->rev_str(),
                 node.index,
                 session.id(),
                 logger::system_data(fmt::format("{}:{}", hostname, port)));
    session.bootstrap(
      [self = shared_from_this(), session](std::error_code err,
                                           topology::configuration cfg) mutable -> void {
        if (err) {
          return self->remove_session(session.id());
        }
        self->update_config(std::move(cfg));
        session.on_configuration_update(self);
        session.on_stop([id = session.id(), self]() -> void {
          self->remove_session(id);
        });
        self->drain_deferred_queue({});
      },
      true);
    sessions_.insert_or_assign(index, std::move(session));
    return true;
  }

  void restart_sessions()
  {
    const std::scoped_lock lock(config_mutex_, sessions_mutex_);
    if (!config_) {
      return;
    }
    if (closed_) {
      // remove_session() posts restart_sessions() whenever a session drops. If that post is
      // serviced after close() has torn the bucket down, recreating sessions here would strand them
      // (nothing stops a session added after close()), leaking the whole bucket/session graph as a
      // pure cycle. close() sets closed_ before it swaps and stops sessions_, so bail out.
      return;
    }

    std::size_t kv_node_index{ 0 };
    for (std::size_t index = 0; index < config_->nodes.size(); ++index) {
      const auto& node = config_->nodes[index];

      const auto& hostname = node.hostname_for(origin_.options().network);
      auto port = node.port_or(
        origin_.options().network, service_type::key_value, origin_.options().enable_tls, 0);
      if (port == 0) {
        continue;
      }

      auto ptr =
        std::find_if(sessions_.begin(), sessions_.end(), [&hostname, &port](const auto& session) {
          return session.second.bootstrap_hostname() == hostname &&
                 session.second.bootstrap_port_number() == port;
        });
      if (ptr != sessions_.end()) {

        if (auto found_kv_node_index = ptr->first; found_kv_node_index != kv_node_index) {
          if (auto current = sessions_.find(kv_node_index); current == sessions_.end()) {
            CB_LOG_WARNING(
              R"({} KV node index mismatch: config rev={} states that address="{}" should be at idx={}, )"
              R"(but it is at idx={} ("{}"). Moving session to idx={}.)",
              log_prefix_,
              config_->rev_str(),
              logger::system_data(fmt::format("{}:{}", hostname, port)),
              kv_node_index,
              found_kv_node_index,
              ptr->second.id(),
              kv_node_index);
            sessions_.insert_or_assign(kv_node_index, std::move(ptr->second));
            sessions_.erase(ptr);
          } else {
            CB_LOG_WARNING(
              R"({} KV node index mismatch: config rev={} states that address="{}" should be at idx={}, )"
              R"(but it is at idx={} ("{}"). Slot with idx={} is holds session with address="{}" ("{}"), swapping them.)",
              log_prefix_,
              config_->rev_str(),
              logger::system_data(fmt::format("{}:{}", hostname, port)),
              kv_node_index,
              found_kv_node_index,
              ptr->second.id(),
              kv_node_index,
              logger::system_data(current->second.bootstrap_address()),
              current->second.id());
            std::swap(current->second, ptr->second);
          }
        }
        ++kv_node_index;
        continue;
      }
      const couchbase::core::origin origin(
        origin_.credentials(), hostname, port, origin_.options());
      io::mcbp_session session =
        origin_.options().enable_tls
          ? io::mcbp_session(client_id_,
                             node.node_uuid,
                             ctx_,
                             tls_,
                             origin,
                             state_listener_,
                             name_,
                             known_features_)
          : io::mcbp_session(
              client_id_, node.node_uuid, ctx_, origin, state_listener_, name_, known_features_);
      CB_LOG_DEBUG(R"({} rev={}, restart idx={}, session="{}", address="{}")",
                   log_prefix_,
                   config_->rev_str(),
                   node.index,
                   session.id(),
                   logger::system_data(fmt::format("{}:{}", hostname, port)));
      session.bootstrap(
        [self = shared_from_this(), session](std::error_code err,
                                             topology::configuration cfg) mutable {
          if (err) {
            return self->remove_session(session.id());
          }
          self->update_config(std::move(cfg));
          session.on_configuration_update(self);
          session.on_stop([id = session.id(), self]() {
            self->remove_session(id);
          });
          self->drain_deferred_queue({});
        },
        true);
      sessions_.insert_or_assign(index, std::move(session));
      ++kv_node_index;
    }
  }

  void remove_session(const std::string& id)
  {
    bool found{ false };
    const std::scoped_lock lock(sessions_mutex_);
    for (auto ptr = sessions_.cbegin(); ptr != sessions_.cend();) {
      if (ptr->second.id() == id) {
        CB_LOG_DEBUG(R"({} removed session id="{}", address="{}", bootstrap_address="{}")",
                     log_prefix_,
                     ptr->second.id(),
                     logger::system_data(ptr->second.remote_address()),
                     logger::system_data(ptr->second.bootstrap_address()));
        ptr = sessions_.erase(ptr);
        found = true;
      } else {
        ptr = std::next(ptr);
      }
    }

    if (found) {
      asio::post(asio::bind_executor(ctx_, [self = shared_from_this()]() {
        return self->restart_sessions();
      }));
    }
  }

  void bootstrap(utils::movable_function<void(std::error_code, topology::configuration)>&& handler)
  {
    if (closed_) {
      // The bucket was closed before this bootstrap even started -- e.g. the cluster was torn down
      // in the window between cluster_impl::open_bucket() inserting the bucket into buckets_ and
      // calling bootstrap() (the buckets_ lock is released in between). close() is one-shot and
      // will not run again, so registering a config listener or starting a session here would never
      // be undone: the session, its self-capturing bootstrap continuation, and this bucket would be
      // stranded as a pure cycle that LeakSanitizer/Valgrind report as leaked, and the continuation
      // (which a scan/ping/etc. may be parked behind) would never fire. Bail before touching any
      // shared state.
      return handler(errc::common::request_canceled, {});
    }
    if (state_listener_) {
      state_listener_->register_config_listener(shared_from_this());
    }
    io::mcbp_session new_session =
      origin_.options().enable_tls
        ? io::mcbp_session(
            client_id_, {}, ctx_, tls_, origin_, state_listener_, name_, known_features_)
        : io::mcbp_session(client_id_, {}, ctx_, origin_, state_listener_, name_, known_features_);
    {
      // Publish the in-flight session so close() can stop it if the cluster is torn down before
      // this bootstrap completes; otherwise the continuation below (which captures new_session)
      // would keep it alive forever. See bootstrapping_session_.
      const std::scoped_lock lock(sessions_mutex_);
      if (closed_) {
        // close() ran concurrently after the entry check above (it sets closed_ before it swaps and
        // stops sessions_ under sessions_mutex_). Do not publish a session close() has already
        // swept past -- nothing would ever stop it. The bootstrap continuation is not registered
        // yet, so returning here strands nothing.
        return handler(errc::common::request_canceled, {});
      }
      bootstrapping_session_ = new_session;
    }
    new_session.bootstrap([self = shared_from_this(), new_session, h = std::move(handler)](
                            std::error_code ec, topology::configuration cfg) mutable {
      if (ec) {
        CB_LOG_WARNING(R"({} failed to bootstrap session ec={}, bucket="{}")",
                       new_session.log_prefix(),
                       ec.message(),
                       logger::metadata(self->name_));
        self->remove_session(new_session.id());
        {
          const std::scoped_lock lock(self->sessions_mutex_);
          self->bootstrapping_session_.reset();
        }
        // Deliver the failure inline rather than through the asio::post below: on cluster teardown
        // the io_context is stopped and any queued post is discarded, so a posted completion would
        // be dropped -- a std::future caller then observes broken_promise and a callback caller
        // hangs. On the error path the handler only reports the failure to the operation (it does
        // not re-enter the bucket under a held lock here), so completing it synchronously is safe.
        return h(ec, cfg);
      }
      {
        const std::size_t this_index = new_session.index();
        bool established{ false };
        {
          // Establish the session and stop tracking it as bootstrapping in one critical section, so
          // a concurrent close() finds it in exactly one of the two containers (never neither).
          const std::scoped_lock lock(self->sessions_mutex_);
          self->bootstrapping_session_.reset();
          if (self->closed_) {
            // The bucket was closed while this session was still bootstrapping (close() sets
            // closed_ before it swaps the session maps). Do not register callbacks or add it to
            // sessions_: a session established after close() would be stranded, and its on_stop
            // handler could post restart_sessions() during shutdown. Stop it here instead -- no
            // on_stop is registered, so stopping under the lock cannot re-enter remove_session().
            new_session.stop(retry_reason::do_not_retry);
          } else {
            new_session.on_configuration_update(self);
            new_session.on_stop([id = new_session.id(), self]() {
              self->remove_session(id);
            });
            self->sessions_.insert_or_assign(this_index, std::move(new_session));
            established = true;
          }
        }
        if (established) {
          self->update_config(cfg);
          self->drain_deferred_queue({});
          self->poll_config({});
        }
      }
      asio::post(
        asio::bind_executor(self->ctx_, [h = std::move(h), ec, cfg = std::move(cfg)]() mutable {
          h(ec, cfg);
        }));
    });
  }

  void with_configuration(
    utils::movable_function<void(std::error_code, std::shared_ptr<topology::configuration>)>&&
      handler)
  {
    if (closed_) {
      return handler(errc::network::configuration_not_available, nullptr);
    }
    if (configured_) {
      std::shared_ptr<topology::configuration> config{};
      {
        const std::scoped_lock config_lock(config_mutex_);
        config = config_;
      }
      if (config) {
        return handler({}, config);
      }
      return handler(errc::network::configuration_not_available, nullptr);
    }
    // Park the continuation until the bucket is configured. defer_command re-checks closed_ under
    // deferred_commands_mutex_, so a continuation that races close() is either drained or completed
    // with request_canceled instead of being stranded. Stranding would leak the whole cluster
    // graph: the continuation strongly captures the cluster_impl (core), which owns this bucket,
    // which owns the deferred queue that would hold the orphaned continuation (CXXCBC-846).
    defer_command(
      [self = shared_from_this(), handler = std::move(handler)](std::error_code ec) mutable {
        if (ec == errc::common::request_canceled || self->closed_ || !self->configured_) {
          return handler(errc::network::configuration_not_available, nullptr);
        }

        std::shared_ptr<topology::configuration> config{};
        {
          const std::scoped_lock config_lock(self->config_mutex_);
          config = self->config_;
        }
        if (config) {
          return handler({}, config);
        }
        return handler(errc::network::configuration_not_available, nullptr);
      });
  }

  void drain_deferred_queue(std::error_code ec)
  {
    std::queue<utils::movable_function<void(std::error_code)>> commands{};
    {
      const std::scoped_lock lock(deferred_commands_mutex_);
      std::swap(deferred_commands_, commands);
    }
    if (!commands.empty()) {
      CB_LOG_TRACE(
        R"({} draining deferred operation queue, size={})", log_prefix_, commands.size());
    }
    while (!commands.empty()) {
      commands.front()(ec);
      commands.pop();
    }
  }

  void fetch_config()
  {
    if (closed_) {
      return;
    }
    std::optional<io::mcbp_session> session{};
    {
      const std::scoped_lock lock(sessions_mutex_);
      if (sessions_.empty()) {
        CB_LOG_DEBUG(R"({} unable to find connected session (sessions_ is empty), retry in {})",
                     log_prefix_,
                     heartbeat_interval_);
        return;
      }

      const std::size_t start = heartbeat_next_index_.fetch_add(1);
      std::size_t i = start;
      do {
        auto ptr = sessions_.find(i % sessions_.size());
        if (ptr != sessions_.end() && ptr->second.is_bootstrapped() &&
            ptr->second.supports_gcccp()) {
          session = ptr->second;
        }
        i = heartbeat_next_index_.fetch_add(1);
      } while (start % sessions_.size() != i % sessions_.size());
    }
    if (session) {
      protocol::client_request<protocol::get_cluster_config_request_body> req;
      req.opaque(session->next_opaque());
      session->write_and_flush(req.data());
    } else {
      CB_LOG_DEBUG(R"({} unable to find connected session with GCCCP support, retry in {})",
                   log_prefix_,
                   heartbeat_interval_);
    }
  }

  void poll_config(std::error_code ec)
  {
    if (ec == asio::error::operation_aborted || closed_) {
      return;
    }
    if (heartbeat_timer_.expiry() > std::chrono::steady_clock::now()) {
      return;
    }

    fetch_config();

    heartbeat_timer_.expires_after(heartbeat_interval_);
    return heartbeat_timer_.async_wait([self = shared_from_this()](std::error_code e) {
      if (e == asio::error::operation_aborted) {
        return;
      }
      self->poll_config(e);
    });
  }

  void close()
  {
    if (bool expected_state{ false }; !closed_.compare_exchange_strong(expected_state, true)) {
      return;
    }

    heartbeat_timer_.cancel();

    drain_deferred_queue(errc::common::request_canceled);

    // A request waiting for a retry backoff is only ever re-dispatched by its
    // timer, which a closed bucket never fires. Complete them here, for the same
    // reason the deferred queue is drained above.
    retry_backoff_queue_.close();
    retry_backoff_queue_.drain([](const auto& request) {
      request->try_callback({}, errc::common::request_canceled);
    });

    if (state_listener_ != nullptr) {
      state_listener_->unregister_config_listener(shared_from_this());
    }

    {
      const std::scoped_lock lock(config_listeners_mutex_);
      config_listeners_.clear();
    }

    std::map<size_t, io::mcbp_session> old_sessions;
    std::optional<io::mcbp_session> bootstrapping_session;
    {
      const std::scoped_lock lock(sessions_mutex_);
      std::swap(old_sessions, sessions_);
      std::swap(bootstrapping_session, bootstrapping_session_);
    }
    for (auto& [index, session] : old_sessions) {
      session.stop(retry_reason::do_not_retry);
    }
    // Stop the session that is still bootstrapping (if any): this completes its bootstrap with an
    // error, releasing the continuation that would otherwise strand the session and this bucket.
    if (bootstrapping_session) {
      bootstrapping_session->stop(retry_reason::do_not_retry);
    }
  }

  /**
   * copies nodes from rhs that are not in lhs to output vector
   */
  void diff_nodes(const std::vector<topology::configuration::node>& lhs,
                  const std::vector<topology::configuration::node>& rhs,
                  std::vector<topology::configuration::node>& output)
  {
    for (const auto& re : rhs) {
      bool known = false;
      const auto& rhost = re.hostname_for(origin_.options().network);
      const auto rport = re.port_or(
        origin_.options().network, service_type::key_value, origin_.options().enable_tls, 0);
      for (const auto& le : lhs) {
        const auto& lhost = le.hostname_for(origin_.options().network);
        const auto lport = le.port_or(
          origin_.options().network, service_type::key_value, origin_.options().enable_tls, 0);
        if (rhost == lhost && rport == lport) {
          known = true;
          break;
        }
      }
      if (!known) {
        output.push_back(re);
      }
    }
  }

  void update_credentials(cluster_credentials credentials)
  {
    origin_.update_credentials(std::move(credentials));
  }

  void update_config(topology::configuration config) override
  {
    std::vector<topology::configuration::node> added{};
    std::vector<topology::configuration::node> removed{};
    bool sequence_changed = false;
    {
      const std::scoped_lock lock(config_mutex_);
      // MB-60405 fixes this for 7.6.2, but for earlier versions we need to protect against using a
      // config that has an empty vbucket map.  Ideally we only run into this condition on initial
      // bootstrap and that is handled in the session's update_config(), but just in case, only
      // accept a config w/ a non-empty vbucket map.
      if (config.vbmap && config.vbmap->empty()) {
        if (!config_) {
          CB_LOG_WARNING(
            "{} will not initialize configuration rev={} because config has an empty partition map",
            log_prefix_,
            config.rev_str());
        } else {
          CB_LOG_WARNING("{} will not update the configuration old={} -> new={}, because new "
                         "config has an empty partition map",
                         log_prefix_,
                         config_->rev_str(),
                         config.rev_str());
        }
        // this is to make sure we can get a correct config soon
        poll_config(errc::network::configuration_not_available);
        return;
      }
      if (!config_) {
        CB_LOG_DEBUG("{} initialize configuration rev={}", log_prefix_, config.rev_str());
      } else if (config.force) {
        CB_LOG_DEBUG("{} forced to accept configuration rev={}", log_prefix_, config.rev_str());
      } else if (!config.vbmap) {
        CB_LOG_DEBUG("{} will not update the configuration old={} -> new={}, because new config "
                     "does not have partition map",
                     log_prefix_,
                     config_->rev_str(),
                     config.rev_str());
        return;
      } else if (*config_ < config) {
        CB_LOG_DEBUG("{} will update the configuration old={} -> new={}",
                     log_prefix_,
                     config_->rev_str(),
                     config.rev_str());
      } else {
        return;
      }

      if (config_) {
        diff_nodes(config_->nodes, config.nodes, added);
        diff_nodes(config.nodes, config_->nodes, removed);
        if (added.empty() && removed.empty() && config.nodes.size() == config_->nodes.size()) {
          for (std::size_t i = 0; i < config.nodes.size(); ++i) {
            if (config.nodes[i] != config_->nodes[i]) {
              sequence_changed = true;
              break;
            }
          }
        } else {
          sequence_changed = true;
        }
      } else {
        sequence_changed = true;
        added = config.nodes;
      }
      config_.reset();
      config_ = std::make_shared<topology::configuration>(config);
      configured_ = true;

      {
        const std::scoped_lock listeners_lock(config_listeners_mutex_);
        for (const auto& listener : config_listeners_) {
          listener->update_config(*config_);
        }
      }
    }
    std::vector<io::mcbp_session> dropped_sessions{};
    if (!added.empty() || !removed.empty() || sequence_changed) {
      const std::scoped_lock lock(sessions_mutex_);
      if (closed_) {
        // The bucket was closed concurrently (close() sets closed_ before it swaps and stops
        // sessions_ under sessions_mutex_). Do not rebuild the session list: any session created
        // here would be established after close() has already torn everything down. Because each
        // new session's bootstrap continuation strongly captures the session and this bucket (via
        // on_configuration_update()/on_stop()), and nothing stops a session added after close(),
        // it would strand the whole bucket/session graph as a pure cycle that LeakSanitizer reports
        // as leaked. bootstrap() guards the initial session the same way (see closed_ there).
        return;
      }
      std::map<size_t, io::mcbp_session> new_sessions{};

      std::size_t next_index{ 0 };
      for (const auto& node : config.nodes) {
        const auto& hostname = node.hostname_for(origin_.options().network);
        auto port = node.port_or(
          origin_.options().network, service_type::key_value, origin_.options().enable_tls, 0);
        if (port == 0) {
          continue;
        }

        bool reused_session{ false };
        for (auto it = sessions_.begin(); it != sessions_.end(); ++it) {
          if (it->second.bootstrap_hostname() == hostname &&
              it->second.bootstrap_port_number() == port) {
            CB_LOG_DEBUG(R"({} rev={}, preserve session="{}", address="{}", index={}->{})",
                         log_prefix_,
                         config.rev_str(),
                         it->second.id(),
                         logger::system_data(it->second.bootstrap_address()),
                         it->first,
                         next_index);
            new_sessions.insert_or_assign(next_index, std::move(it->second));
            reused_session = true;
            ++next_index;
            sessions_.erase(it);
            break;
          }
        }
        if (reused_session) {
          continue;
        }

        if (origin_.options().enable_lazy_connections) {
          ++next_index;
          continue;
        }
        const couchbase::core::origin origin(
          origin_.credentials(), hostname, port, origin_.options());
        io::mcbp_session session =
          origin_.options().enable_tls
            ? io::mcbp_session(client_id_,
                               node.node_uuid,
                               ctx_,
                               tls_,
                               origin,
                               state_listener_,
                               name_,
                               known_features_)
            : io::mcbp_session(
                client_id_, node.node_uuid, ctx_, origin, state_listener_, name_, known_features_);
        CB_LOG_DEBUG(R"({} rev={}, add session="{}", address="{}", index={})",
                     log_prefix_,
                     config.rev_str(),
                     session.id(),
                     logger::system_data(fmt::format("{}:{}", hostname, port)),
                     node.index);
        session.bootstrap(
          [self = shared_from_this(), session, idx = next_index](
            std::error_code err, topology::configuration cfg) mutable {
            if (err) {
              CB_LOG_WARNING(
                R"({} failed to bootstrap session="{}", address="{}", index={}, ec={})",
                session.log_prefix(),
                session.id(),
                logger::system_data(session.bootstrap_address()),
                idx,
                err.message());
              return self->remove_session(session.id());
            }
            self->update_config(std::move(cfg));
            session.on_configuration_update(self);
            session.on_stop([id = session.id(), self]() {
              self->remove_session(id);
            });
            self->drain_deferred_queue({});
          },
          true);
        new_sessions.insert_or_assign(next_index, std::move(session));
        ++next_index;
      }
      std::swap(sessions_, new_sessions);

      for (auto it = new_sessions.begin(); it != new_sessions.end(); ++it) {
        CB_LOG_DEBUG(R"({} rev={}, drop session="{}", address="{}", index={})",
                     log_prefix_,
                     config.rev_str(),
                     it->second.id(),
                     logger::system_data(it->second.bootstrap_address()),
                     it->first);
        dropped_sessions.push_back(std::move(it->second));
      }
    }
    // Stop dropped sessions synchronously, after releasing sessions_mutex_ (stop() fires on_stop ->
    // remove_session(), which re-acquires the mutex). Stopping synchronously -- rather than via
    // asio::post -- clears each session's bucket-capturing handlers (config_listeners_,
    // on_stop_handler_) right now. A posted stop can be abandoned by io_context shutdown before it
    // runs, leaving those handlers set and stranding the whole bucket/session graph as a pure cycle
    // (LeakSanitizer reports it as all-indirect leaks).
    for (auto& session : dropped_sessions) {
      session.stop(retry_reason::do_not_retry);
    }
  }

  [[nodiscard]] auto find_session_by_index(std::size_t index) const
    -> std::optional<io::mcbp_session>
  {
    const std::scoped_lock lock(sessions_mutex_);
    if (auto ptr = sessions_.find(index); ptr != sessions_.end()) {
      return ptr->second;
    }
    return {};
  }

  [[nodiscard]] auto find_or_connect_session_by_index(std::size_t index) -> routed_session
  {
    if (auto session = find_session_by_index(index); session) {
      // A session exists, so it is safe to defer behind it. That holds even
      // if it is still bootstrapping: the completion that finishes it is what
      // drains the deferred queue.
      return { session, true };
    }
    return { {}, connect_session(index) };
  }

  [[nodiscard]] auto next_session_index() -> std::size_t
  {
    const std::scoped_lock lock(sessions_mutex_);

    if (auto index = round_robin_next_.fetch_add(1); index < sessions_.size()) {
      return index;
    }
    round_robin_next_ = 0;
    return 0;
  }

  [[nodiscard]] auto default_timeout() const -> std::chrono::milliseconds
  {
    return origin_.options().default_timeout_for(service_type::key_value);
  }

  [[nodiscard]] auto name() const -> const std::string&
  {
    return name_;
  }

  [[nodiscard]] auto log_prefix() const -> const std::string&
  {
    return client_id_;
  }

  [[nodiscard]] auto is_closed() const -> bool
  {
    return closed_;
  }

  [[nodiscard]] auto is_configured() const -> bool
  {
    return configured_;
  }

  [[nodiscard]] auto tracer() const -> std::shared_ptr<tracing::tracer_wrapper>
  {
    return tracer_;
  }

  [[nodiscard]] auto meter() const -> std::shared_ptr<metrics::meter_wrapper>
  {
    return meter_;
  }

  [[nodiscard]] auto orphan_reporter() const -> std::shared_ptr<core::orphan_reporter>
  {
    return orphan_reporter_;
  }

  [[nodiscard]] auto app_telemetry_meter() const -> std::shared_ptr<core::app_telemetry_meter>
  {
    return app_telemetry_meter_;
  }

  void export_diag_info(diag::diagnostics_result& res) const
  {
    std::map<size_t, io::mcbp_session> sessions;
    {
      const std::scoped_lock lock(sessions_mutex_);
      sessions = sessions_;
    }
    for (const auto& [index, session] : sessions) {
      res.services[service_type::key_value].emplace_back(session.diag_info());
    }
  }

  void ping(const std::shared_ptr<diag::ping_collector>& collector,
            std::optional<std::chrono::milliseconds> timeout)
  {
    std::map<size_t, io::mcbp_session> sessions;
    {
      const std::scoped_lock lock(sessions_mutex_);
      sessions = sessions_;
    }
    for (const auto& [index, session] : sessions) {
      session.ping(collector->build_reporter(), timeout);
    }
  }

  auto default_retry_strategy() const -> std::shared_ptr<couchbase::retry_strategy>
  {
    return origin_.options().default_retry_strategy_;
  }

  void on_configuration_update(std::shared_ptr<config_listener> handler)
  {
    const std::scoped_lock lock(config_listeners_mutex_);
    config_listeners_.emplace_back(std::move(handler));
  }

  auto defer_command(utils::movable_function<void(std::error_code)> command) -> std::error_code
  {
    {
      const std::scoped_lock lock_for_deferred_commands(deferred_commands_mutex_);
      if (!closed_) {
        deferred_commands_.emplace(std::move(command));
        return {};
      }
    }
    // The bucket was closed (and its deferred queue already drained) before this command could be
    // parked. Enqueuing now would strand the command forever, leaking whatever its continuation
    // captures. Complete it with a cancellation instead so its state is released (CXXCBC-846).
    command(errc::common::request_canceled);
    return errc::common::request_canceled;
  }

  void for_each_session(utils::movable_function<void(io::mcbp_session&)> handler)
  {
    std::map<size_t, io::mcbp_session> sessions;
    {
      const std::scoped_lock lock(sessions_mutex_);
      sessions = sessions_;
    }
    for (auto& [index, session] : sessions) {
      handler(session);
    }
  }

private:
  const std::string client_id_;
  const std::string name_;
  const std::string log_prefix_;
  const std::shared_ptr<tracing::tracer_wrapper> tracer_;
  const std::shared_ptr<metrics::meter_wrapper> meter_;
  const std::shared_ptr<core::orphan_reporter> orphan_reporter_;
  const std::shared_ptr<core::app_telemetry_meter> app_telemetry_meter_;
  const std::vector<protocol::hello_feature> known_features_;
  const std::shared_ptr<impl::bootstrap_state_listener> state_listener_;
  origin origin_;
  mcbp::codec codec_;

  asio::io_context& ctx_;
  tls_context_provider& tls_;

  asio::steady_timer heartbeat_timer_;
  std::chrono::milliseconds heartbeat_interval_;
  std::atomic_size_t heartbeat_next_index_{ 0 };

  std::atomic_bool closed_{ false };
  std::atomic_bool configured_{ false };

  std::shared_ptr<topology::configuration> config_{};
  mutable std::mutex config_mutex_{};

  std::vector<std::shared_ptr<config_listener>> config_listeners_{};
  std::mutex config_listeners_mutex_{};

  std::queue<utils::movable_function<void(std::error_code)>> deferred_commands_{};
  std::mutex deferred_commands_mutex_{};

  // Requests waiting for a retry backoff, so that close() can complete them.
  mcbp::operation_queue retry_backoff_queue_{};

  std::map<size_t, io::mcbp_session> sessions_{};
  // The very first session is only moved into sessions_ once it finishes bootstrapping; until then
  // it lives solely inside its own bootstrap continuation (which captures a copy of the session and
  // this bucket). If the cluster is closed while that bootstrap is in flight (e.g. a bucket that
  // never opens), close() would otherwise never see the session, so its self-capture would strand
  // the session, the bucket, and the whole cluster graph. Track it here so close() can stop it and
  // let the continuation fire. Guarded by sessions_mutex_.
  std::optional<io::mcbp_session> bootstrapping_session_{};
  mutable std::mutex sessions_mutex_{};
  std::atomic_size_t round_robin_next_{ 0 };
};

bucket::bucket(std::string client_id,
               asio::io_context& ctx,
               tls_context_provider& tls,
               std::shared_ptr<tracing::tracer_wrapper> tracer,
               std::shared_ptr<metrics::meter_wrapper> meter,
               std::shared_ptr<core::orphan_reporter> orphan_reporter,
               std::shared_ptr<core::app_telemetry_meter> app_telemetry_meter,
               std::string name,
               couchbase::core::origin origin,
               std::vector<protocol::hello_feature> known_features,
               std::shared_ptr<impl::bootstrap_state_listener> state_listener)

  : ctx_(ctx)
  , impl_{ std::make_shared<bucket_impl>(std::move(client_id),
                                         std::move(name),
                                         std::move(origin),
                                         std::move(tracer),
                                         std::move(meter),
                                         std::move(orphan_reporter),
                                         std::move(app_telemetry_meter),
                                         std::move(known_features),
                                         std::move(state_listener),
                                         ctx,
                                         tls) }
{
}

bucket::~bucket()
{
  impl_->close();
}

void
bucket::export_diag_info(diag::diagnostics_result& res) const
{
  return impl_->export_diag_info(res);
}

void
bucket::ping(const std::shared_ptr<diag::ping_collector>& collector,
             std::optional<std::chrono::milliseconds> timeout)
{
  return impl_->ping(collector, timeout);
}

void
bucket::fetch_config()
{
  return impl_->fetch_config();
}

void
bucket::update_config(topology::configuration config)
{
  return impl_->update_config(std::move(config));
}

void
bucket::update_credentials(cluster_credentials credentials)
{
  return impl_->update_credentials(std::move(credentials));
}

auto
bucket::name() const -> const std::string&
{
  return impl_->name();
}

void
bucket::close()
{
  return impl_->close();
}

auto
bucket::log_prefix() const -> const std::string&
{
  return impl_->log_prefix();
}

auto
bucket::tracer() const -> std::shared_ptr<tracing::tracer_wrapper>
{
  return impl_->tracer();
}

auto
bucket::meter() const -> std::shared_ptr<metrics::meter_wrapper>
{
  return impl_->meter();
}

auto
bucket::orphan_reporter() const -> std::shared_ptr<core::orphan_reporter>
{
  return impl_->orphan_reporter();
}

auto
bucket::app_telemetry_meter() const -> std::shared_ptr<core::app_telemetry_meter>
{
  return impl_->app_telemetry_meter();
}

auto
bucket::default_retry_strategy() const -> std::shared_ptr<couchbase::retry_strategy>
{
  return impl_->default_retry_strategy();
}

void
bucket::on_configuration_update(std::shared_ptr<config_listener> handler)
{
  return impl_->on_configuration_update(std::move(handler));
}

void
bucket::bootstrap(utils::movable_function<void(std::error_code, topology::configuration)>&& handler)
{
  return impl_->bootstrap(std::move(handler));
}

void
bucket::with_configuration(
  utils::movable_function<void(std::error_code, std::shared_ptr<topology::configuration>)>&&
    handler)
{
  return impl_->with_configuration(std::move(handler));
}

auto
bucket::is_closed() const -> bool
{
  return impl_->is_closed();
}

auto
bucket::is_configured() const -> bool
{
  return impl_->is_configured();
}

void
bucket::defer_command(utils::movable_function<void(std::error_code)> command)
{
  impl_->defer_command(std::move(command));
}

void
bucket::for_each_session(utils::movable_function<void(io::mcbp_session&)> handler)
{
  impl_->for_each_session(std::move(handler));
}

auto
bucket::default_timeout() const -> std::chrono::milliseconds
{
  return impl_->default_timeout();
}

auto
bucket::find_session_by_index(std::size_t index) const -> std::optional<io::mcbp_session>
{
  return impl_->find_session_by_index(index);
}

auto
bucket::next_session_index() -> std::size_t
{
  return impl_->next_session_index();
}

auto
bucket::map_id(const document_id& id) -> std::pair<std::uint16_t, std::optional<std::size_t>>
{
  return impl_->map_id(id);
}

auto
bucket::config_rev() const -> std::string
{
  return impl_->config_rev();
}

auto
bucket::config_snapshot() const -> std::shared_ptr<topology::configuration>
{
  return impl_->config_snapshot();
}

bucket_unit_test_api::bucket_unit_test_api(std::shared_ptr<bucket_impl> impl)
  : impl_{ std::move(impl) }
{
}

void
bucket_unit_test_api::resolve_response(const std::shared_ptr<mcbp::queue_request>& request,
                                       const std::shared_ptr<mcbp::queue_response>& response,
                                       std::error_code ec,
                                       retry_reason reason,
                                       std::optional<key_value_error_map_info> error_info)
{
  impl_->resolve_response(request, response, ec, reason, std::move(error_info));
}

auto
bucket::unit_test_api() -> bucket_unit_test_api
{
  return bucket_unit_test_api(impl_);
}

auto
bucket::direct_dispatch(std::shared_ptr<mcbp::queue_request> req) -> std::error_code
{
  return impl_->direct_dispatch(std::move(req));
}

auto
bucket::direct_re_queue(const std::shared_ptr<mcbp::queue_request>& req, bool is_retry)
  -> std::error_code
{
  return impl_->direct_re_queue(req, is_retry);
}

auto
bucket::connect_session(std::size_t index) -> bool
{
  return impl_->connect_session(index);
}
} // namespace couchbase::core
