/*
 *     Copyright 2021-Present 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 "active_transaction_record.hxx"
#include "atr_ids.hxx"
#include "attempt_context_impl.hxx"
#include "cleanup_testing_hooks.hxx"
#include "uid_generator.hxx"

#include "internal/client_record.hxx"
#include "internal/logging.hxx"
#include "internal/transaction_fields.hxx"
#include "internal/transactions_cleanup.hxx"
#include "internal/utils.hxx"

#include "core/operations.hxx"
#include "core/utils/process_id.hxx"

#include <couchbase/fmt/transaction_keyspace.hxx>

#include <asio/post.hpp>

#include <deque>
#include <functional>
#include <future>
#include <list>
#include <memory>
#include <thread>
#include <utility>

namespace couchbase::core::transactions
{

transactions_cleanup_attempt::transactions_cleanup_attempt(const atr_cleanup_entry& entry)
  : atr_id_(entry.atr_id_)
  , attempt_id_(entry.attempt_id_)
  , success_(false)
  , state_(attempt_state::NOT_STARTED)
{
}

transactions_cleanup::transactions_cleanup(
  core::cluster cluster,
  couchbase::transactions::transactions_config::built config)
  : cluster_(std::move(cluster))
  , config_(std::move(config))
  , client_uuid_(uid_generator::next())
  , client_record_owner_pid_(utils::current_process_id())
{
  start();
}

namespace
{
// TODO(CXXCBC-549)
const std::string CLIENT_RECORD_DOC_ID = "_txn:client-record"; // NOLINT(cert-err58-cpp)
} // namespace

constexpr auto SAFETY_MARGIN_EXPIRY_MS = 2000;

template<class R, class P>
auto
transactions_cleanup::interruptable_wait(std::chrono::duration<R, P> time) -> bool
{
  // wait for specified time, _or_ until the condition variable changes
  std::unique_lock<std::mutex> lock(mutex_);
  if (!running_) {
    return false;
  }
  cv_.wait_for(lock, time, [&]() {
    return !running_;
  });
  return running_;
}

void
transactions_cleanup::clean_collection(
  const couchbase::transactions::transaction_keyspace& keyspace)
{
  // first make sure the collection is in the list
  while (is_running()) {
    {
      const std::scoped_lock<std::mutex> lock(mutex_);
      if (collections_.end() == std::find(collections_.begin(), collections_.end(), keyspace)) {
        CB_LOST_ATTEMPT_CLEANUP_LOG_DEBUG(
          "cleanup for {} ending, no longer in collection cleanup list", keyspace);
        return;
      }
    }
    CB_LOST_ATTEMPT_CLEANUP_LOG_INFO("cleanup for {} starting", keyspace);
    // we are running, and collection is in the list, so lets clean it.
    try {
      auto details = get_active_clients(keyspace, client_uuid_);

      auto all_atrs = atr_ids::all();

      const auto cleanup_window = std::chrono::duration_cast<std::chrono::microseconds>(
        config_.cleanup_config.cleanup_window);
      auto start = std::chrono::steady_clock::now();
      CB_LOST_ATTEMPT_CLEANUP_LOG_INFO(
        "{} active clients (including this one), {} ATRs to check in {}ms",
        details.num_active_clients,
        all_atrs.size(),
        config_.cleanup_config.cleanup_window.count());

      // Each ATR is checked with a blocking get_atr. If those were run strictly one-at-a-time the
      // scan rate would be capped by the per-ATR KV latency rather than the cleanup window: on a
      // slow or loaded KV node a single window could fail to scan all ATRs, leaving lost
      // transactions (and the documents they block) uncleaned for far longer than configured. So
      // run the checks with a bounded number in flight: launches are still paced across the window
      // (to avoid flooding the cluster), but a slow lookup no longer holds up the ones behind it.
      std::deque<std::future<void>> in_flight;
      // The pool is created in start() whenever lost-attempts cleanup is enabled, which is the only
      // configuration that spawns collection workers, so it is always present here. Bind a checked
      // reference now rather than dereferencing the optional at each launch.
      if (!atr_cleanup_pool_.has_value()) {
        // Should be unreachable (see above). If the invariant is ever broken, surface it: a silent
        // return here disables lost-attempts cleanup for this collection with no other signal.
        CB_LOST_ATTEMPT_CLEANUP_LOG_ERROR(
          "atr cleanup pool is not initialized; skipping lost-attempts cleanup for {}", keyspace);
        return;
      }
      auto& atr_cleanup_pool = *atr_cleanup_pool_;
      const auto reclaim_one = [&in_flight]() {
        const auto reclaim = [](std::future<void>& f) {
          // get() only rethrows if the task itself threw, which it is written not to, but guard
          // anyway so one bad ATR cannot abort the whole scan.
          try {
            f.get();
            // NOLINTNEXTLINE(bugprone-empty-catch) -- task already logged any error
          } catch (...) {
            // already handled in the task
          }
        };
        // Reclaim any already-finished check first, so a slow lookup at the front does not hold up
        // the ready ones queued behind it.
        for (auto it = in_flight.begin(); it != in_flight.end(); ++it) {
          if (it->wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
            reclaim(*it);
            in_flight.erase(it);
            return;
          }
        }
        // None ready yet: block on the oldest, since a slot must be freed before launching more.
        reclaim(in_flight.front());
        in_flight.pop_front();
      };

      for (auto it = all_atrs.begin() + details.index_of_this_client; it < all_atrs.end();
           it += details.num_active_clients) {
        if (!is_running()) {
          break;
        }
        // This client visits every num_active_clients-th ATR from `it`, so the number it still has
        // to launch is ceil(remaining / stride). Floor division would undercount at the tail,
        // inflating budget_for_this_atr below and over-pacing the scan past the cleanup window.
        const auto stride = std::max<decltype(it)::difference_type>(1, details.num_active_clients);
        const auto atrs_left_for_this_client =
          (std::distance(it, all_atrs.end()) + stride - 1) / stride;
        const auto now = std::chrono::steady_clock::now();
        const auto elapsed_in_cleanup_window =
          std::chrono::duration_cast<std::chrono::microseconds>(now - start);
        const auto remaining_in_cleanup_window = cleanup_window - elapsed_in_cleanup_window;
        // Divide the time left in the window evenly among the ATRs still to launch, so the scan is
        // spread across the window rather than issued as a burst.
        const auto budget_for_this_atr = std::chrono::microseconds(
          remaining_in_cleanup_window.count() /
          std::max<decltype(it)::difference_type>(1, atrs_left_for_this_client));
        // Bound concurrency: reclaim a finished slot before launching the next check.
        while (in_flight.size() >= max_in_flight_atr_checks_) {
          reclaim_one();
        }

        const std::string atr_id = *it;
        // Offload the blocking check to the shared bounded pool (never the IO thread); keep its
        // future so the loop can bound how many run concurrently and drain them at the end.
        // packaged_task is wrapped in a shared_ptr because asio::post needs a copyable handler.
        auto task = std::make_shared<std::packaged_task<void()>>([this, keyspace, atr_id]() {
          try {
            handle_atr_cleanup({ keyspace.bucket, keyspace.scope, keyspace.collection, atr_id });
          } catch (const std::exception& e) {
            CB_LOST_ATTEMPT_CLEANUP_LOG_ERROR(
              "cleanup of atr {} failed with {}, moving on", atr_id, e.what());
          }
        });
        auto future = task->get_future();
        // Post before recording the future. If asio::post() throws (e.g. allocation failure) the
        // task never runs; recording its future first would leave a future in `in_flight` that
        // never becomes ready, deadlocking reclaim_one()/the drain. On throw, `future` is discarded
        // and the exception unwinds to the per-collection retry below.
        asio::post(atr_cleanup_pool, [task]() {
          (*task)();
        });
        in_flight.emplace_back(std::move(future));

        // Pace the launch cadence so the scan spreads across the window rather than bursting.
        // Subtract the time already spent this iteration -- reclaiming a slot can block on a slow
        // lookup, and posting takes time -- from the budget, so a slow iteration does not push the
        // scan past the cleanup window. If we are already behind, the remainder is <= 0 and we
        // proceed immediately.
        const auto work_time = std::chrono::duration_cast<std::chrono::microseconds>(
          std::chrono::steady_clock::now() - now);
        const auto sleep_for_this_atr = budget_for_this_atr - work_time;
        if (sleep_for_this_atr.count() > 0 &&
            sleep_for_this_atr.count() < 1000000000) { // safety check protects against bugs
          if (!interruptable_wait(sleep_for_this_atr)) {
            break;
          }
        }
      }

      // Wait for any still-running checks before completing the run. On stop() the launch loop
      // above has already stopped starting new checks (it breaks on !is_running()), so this only
      // drains the
      // <= max_in_flight_atr_checks_ already in flight. They are reclaimed rather than abandoned
      // because each task references this cleanup state; teardown therefore blocks on them, bounded
      // by the KV read timeout (an in-flight get_atr is not cancelled).
      while (!in_flight.empty()) {
        reclaim_one();
      }
      if (!is_running()) {
        CB_LOST_ATTEMPT_CLEANUP_LOG_DEBUG("cleanup of {} complete", keyspace);
        return;
      }
    } catch (const std::exception& ex) {
      CB_LOST_ATTEMPT_CLEANUP_LOG_ERROR("cleanup failed with {}, trying again in 3 sec...",
                                        ex.what());
      // we must have gotten an exception trying to get the client records.   Let's wait 3 sec and
      // try again
      std::this_thread::sleep_for(std::chrono::seconds(3));
    }
  }
}

auto
transactions_cleanup::handle_atr_cleanup(const core::document_id& atr_id,
                                         std::vector<transactions_cleanup_attempt>* results)
  -> atr_cleanup_stats
{
  atr_cleanup_stats stats;
  auto atr = active_transaction_record::get_atr(cluster_, atr_id);
  if (atr) {
    // ok, loop through the attempts and clean them all.  The entry will
    // check if expired, nothing much to do here except call clean.
    stats.exists = true;
    stats.num_entries = atr->entries().size();
    for (const auto& entry : atr->entries()) {
      // If we were passed results, then we are testing, and want to set the
      // check_if_expired to false.
      atr_cleanup_entry cleanup_entry(entry, atr_id, *this, results == nullptr);
      try {
        if (results != nullptr) {
          results->emplace_back(cleanup_entry);
        }
        cleanup_entry.clean(results != nullptr ? &results->back() : nullptr);
        if (results != nullptr) {
          results->back().success(true);
        }
      } catch (const std::exception& e) {
        CB_LOST_ATTEMPT_CLEANUP_LOG_ERROR(
          "cleanup of {} failed: {}, moving on", cleanup_entry, e.what());
        if (results != nullptr) {
          results->back().success(false);
        }
      }
    }
  }
  return stats;
}

void
transactions_cleanup::create_client_record(
  const couchbase::transactions::transaction_keyspace& keyspace)

{
  try {
    auto id =
      document_id(keyspace.bucket, keyspace.scope, keyspace.collection, CLIENT_RECORD_DOC_ID);
    core::operations::mutate_in_request req{ id };
    req.store_semantics = couchbase::store_semantics::insert;
    req.specs =
      couchbase::mutate_in_specs{
        couchbase::mutate_in_specs::insert("records.clients", tao::json::empty_object)
          .xattr()
          .create_path(),
        // subdoc::opcode::set_doc used in replace w/ empty path
        // ExtBinaryMetadata
        couchbase::mutate_in_specs::replace_raw({}, std::vector<std::byte>{ std::byte{ 0x00 } }),
      }
        .specs();
    wrap_durable_request(req, config_);
    auto barrier = std::make_shared<std::promise<result>>();
    auto f = barrier->get_future();
    auto ec = wait_for_hook([this, bucket = keyspace.bucket](auto handler) mutable {
      return config_.cleanup_hooks->client_record_before_create(bucket, std::move(handler));
    });
    if (ec) {
      throw client_error(*ec, "client_record_before_create hook raised error");
    }
    cluster_.execute(req, [barrier](auto&& resp) {
      barrier->set_value(result::create_from_subdoc_response(resp));
    });
    wrap_operation_future(f);

  } catch (const client_error& e) {
    CB_LOST_ATTEMPT_CLEANUP_LOG_TRACE("create_client_record got error {}", e.what());
    auto ec = e.ec();
    switch (ec) {
      case FAIL_DOC_ALREADY_EXISTS:
        CB_LOST_ATTEMPT_CLEANUP_LOG_TRACE("client record already exists, moving on");
        return;
      default:
        throw;
    }
  }
}

auto
transactions_cleanup::get_active_clients(
  const couchbase::transactions::transaction_keyspace& keyspace,
  const std::string& uuid) -> client_record_details
{
  client_record_details details;
  // Write our client record, return details.
  try {
    auto id =
      document_id{ keyspace.bucket, keyspace.scope, keyspace.collection, CLIENT_RECORD_DOC_ID };
    core::operations::lookup_in_request req{ id };
    req.specs =
      lookup_in_specs{
        lookup_in_specs::get("records").xattr(),
        lookup_in_specs::get(subdoc::lookup_in_macro::vbucket).xattr(),
      }
        .specs();
    auto barrier = std::make_shared<std::promise<result>>();
    auto f = barrier->get_future();
    auto ec = wait_for_hook([this, bucket = keyspace.bucket](auto handler) mutable {
      return config_.cleanup_hooks->client_record_before_get(bucket, std::move(handler));
    });
    if (ec) {
      throw client_error(*ec, "client_record_before_get hook raised error");
    }
    cluster_.execute(req, [barrier](auto&& resp) {
      barrier->set_value(result::create_from_subdoc_response(resp));
    });
    auto res = wrap_operation_future(f);

    std::vector<std::string> active_client_uids;
    auto hlc = res.values[1].content_as();
    auto now_ms = now_ns_from_vbucket(hlc) / 1000000;
    details.override_enabled = false;
    details.override_expires = 0;
    if (res.values[0].status == subdoc_result::status_type::success) {
      auto records = res.values[0].content_as();
      CB_LOST_ATTEMPT_CLEANUP_LOG_TRACE("client records: {}", core::utils::json::generate(records));
      for (const auto& [key, value] : records.get_object()) {
        if (key == "override") {
          for (const auto& [over_ride, param] : value.get_object()) {
            if (over_ride == "enabled") {
              details.override_enabled = param.get_boolean();
            } else if (over_ride == "expires") {
              details.override_expires = param.as<std::uint64_t>();
            }
          }
        } else if (key == "clients") {
          for (const auto& [other_client_uuid, cl] : value.get_object()) {
            const std::uint64_t heartbeat_ms =
              parse_mutation_cas(cl.at("heartbeat_ms").get_string());
            auto expires_ms = cl.at("expires_ms").as<std::uint64_t>();
            auto expired_period =
              static_cast<std::int64_t>(now_ms) - static_cast<std::int64_t>(heartbeat_ms);
            const bool has_expired =
              expired_period >= static_cast<std::int64_t>(expires_ms) && now_ms > heartbeat_ms;
            if (has_expired && other_client_uuid != uuid) {
              details.expired_client_ids.push_back(other_client_uuid);
            } else {
              active_client_uids.push_back(other_client_uuid);
            }
          }
        }
      }
    }
    if (std::find(active_client_uids.begin(), active_client_uids.end(), uuid) ==
        active_client_uids.end()) {
      active_client_uids.push_back(uuid);
    }
    std::sort(active_client_uids.begin(), active_client_uids.end());
    auto this_idx =
      std::distance(active_client_uids.begin(),
                    std::find(active_client_uids.begin(), active_client_uids.end(), uuid));
    details.num_active_clients = static_cast<std::uint32_t>(active_client_uids.size());
    details.index_of_this_client = static_cast<std::uint32_t>(this_idx);
    details.num_active_clients = static_cast<std::uint32_t>(active_client_uids.size());
    details.num_expired_clients = static_cast<std::uint32_t>(details.expired_client_ids.size());
    details.num_existing_clients = details.num_expired_clients + details.num_active_clients;
    details.client_uuid = uuid;
    details.cas_now_nanos = now_ms * 1000000;
    details.override_active =
      (details.override_enabled && details.override_expires > details.cas_now_nanos);
    CB_LOST_ATTEMPT_CLEANUP_LOG_TRACE("client details {}", details);
    if (details.override_active) {
      CB_LOST_ATTEMPT_CLEANUP_LOG_TRACE("override enabled, will not update record");
      return details;
    }

    // update client record, maybe cleanup some as well...
    core::operations::mutate_in_request mutate_req{ id };
    auto mut_specs = couchbase::mutate_in_specs{
      couchbase::mutate_in_specs::upsert(fmt::format("records.clients.{}.heartbeat_ms", uuid),
                                         subdoc::mutate_in_macro::cas)
        .xattr()
        .create_path(),
      couchbase::mutate_in_specs::upsert(fmt::format("records.clients.{}.expires_ms", uuid),
                                         (config_.cleanup_config.cleanup_window.count() / 2) +
                                           SAFETY_MARGIN_EXPIRY_MS)
        .xattr()
        .create_path(),
      couchbase::mutate_in_specs::upsert(fmt::format("records.clients.{}.num_atrs", uuid),
                                         atr_ids::all().size())
        .xattr()
        .create_path(),
    };
    for (std::size_t idx = 0;
         idx < std::min(details.expired_client_ids.size(), static_cast<std::size_t>(12));
         idx++) {
      CB_LOST_ATTEMPT_CLEANUP_LOG_TRACE(
        "adding {} to list of clients to be removed when updating this client",
        details.expired_client_ids[idx]);
      mut_specs.push_back(couchbase::mutate_in_specs::remove(
                            fmt::format("records.clients.{}", details.expired_client_ids[idx]))
                            .xattr());
    }
    mutate_req.specs = mut_specs.specs();
    ec = wait_for_hook([this, bucket = keyspace.bucket](auto handler) mutable {
      return config_.cleanup_hooks->client_record_before_update(bucket, std::move(handler));
    });
    if (ec) {
      throw client_error(*ec, "client_record_before_update hook raised error");
    }
    wrap_durable_request(mutate_req, config_);
    auto mutate_barrier = std::make_shared<std::promise<result>>();
    auto mutate_f = mutate_barrier->get_future();
    CB_LOST_ATTEMPT_CLEANUP_LOG_TRACE("updating record");
    cluster_.execute(mutate_req, [mutate_barrier](auto&& resp) {
      mutate_barrier->set_value(result::create_from_subdoc_response(resp));
    });
    res = wrap_operation_future(mutate_f);

    // just update the cas, and return the details
    details.cas_now_nanos = res.cas;
    CB_LOST_ATTEMPT_CLEANUP_LOG_DEBUG("get_active_clients found {}", details);
    return details;
  } catch (const client_error& e) {
    auto ec = e.ec();
    switch (ec) {
      case FAIL_DOC_NOT_FOUND:
        CB_LOST_ATTEMPT_CLEANUP_LOG_DEBUG("client record not found, creating new one");
        create_client_record(keyspace);
        return get_active_clients(keyspace, uuid);
      default:
        throw;
    }
  }
}

void
transactions_cleanup::remove_client_record_from_all_buckets(const std::string& uuid)
{
  for (const auto& keyspace : collections_) {
    try {
      retry_op_exponential_backoff_timeout<void>(
        std::chrono::milliseconds(10),
        std::chrono::milliseconds(250),
        std::chrono::milliseconds(500),
        [this, keyspace, uuid]() {
          try {
            // proceed to remove the client uuid if it exists
            auto ec = wait_for_hook([this, bucket = keyspace.bucket](auto handler) mutable {
              return config_.cleanup_hooks->client_record_before_remove_client(bucket,
                                                                               std::move(handler));
            });
            if (ec) {
              throw client_error(*ec, "client_record_before_remove_client hook raised error");
            }
            const document_id id{
              keyspace.bucket, keyspace.scope, keyspace.collection, CLIENT_RECORD_DOC_ID
            };
            core::operations::mutate_in_request req{ id };
            req.specs =
              couchbase::mutate_in_specs{
                couchbase::mutate_in_specs::remove(fmt::format("records.clients.{}", uuid)).xattr(),
              }
                .specs();
            wrap_durable_request(req, config_);
            auto barrier = std::make_shared<std::promise<result>>();
            auto f = barrier->get_future();
            cluster_.execute(req, [barrier](auto&& resp) {
              barrier->set_value(result::create_from_subdoc_response(resp));
            });
            wrap_operation_future(f);
            CB_LOST_ATTEMPT_CLEANUP_LOG_DEBUG("removed {} from {}", uuid, keyspace);
          } catch (const client_error& e) {
            CB_LOST_ATTEMPT_CLEANUP_LOG_DEBUG("error removing client records {}", e.what());
            auto ec = e.ec();
            switch (ec) {
              case FAIL_DOC_NOT_FOUND:
                CB_LOST_ATTEMPT_CLEANUP_LOG_DEBUG("no client record in {}, ignoring", keyspace);
                return;
              case FAIL_PATH_NOT_FOUND:
                CB_LOST_ATTEMPT_CLEANUP_LOG_DEBUG(
                  "client {} not in client record for {}, ignoring", uuid, keyspace);
                return;
              default:
                throw retry_operation("retry remove until timeout");
            }
          }
        });
    } catch (const std::exception& e) {
      CB_LOST_ATTEMPT_CLEANUP_LOG_ERROR(
        "Error removing client record {} from {}: {}", uuid, keyspace, e.what());
    }
  }
}

auto
transactions_cleanup::force_cleanup_atr(const core::document_id& atr_id,
                                        std::vector<transactions_cleanup_attempt>& results)
  -> atr_cleanup_stats
{
  CB_LOST_ATTEMPT_CLEANUP_LOG_TRACE("starting force_cleanup_atr: atr_id {}", atr_id);
  return handle_atr_cleanup(atr_id, &results);
}

void
transactions_cleanup::force_cleanup_entry(atr_cleanup_entry& entry,
                                          transactions_cleanup_attempt& attempt)
{
  try {
    entry.clean(&attempt);
    attempt.success(true);

  } catch (const std::runtime_error& e) {
    CB_ATTEMPT_CLEANUP_LOG_ERROR("error attempting to clean {}: {}", entry, e.what());
    attempt.success(false);
  }
}

void
transactions_cleanup::force_cleanup_attempts(std::vector<transactions_cleanup_attempt>& results)
{
  CB_ATTEMPT_CLEANUP_LOG_TRACE("starting force_cleanup_attempts");
  while (atr_queue_.size() > 0) {
    auto entry = atr_queue_.pop(false);
    if (!entry) {
      CB_ATTEMPT_CLEANUP_LOG_ERROR("pop failed to return entry, but queue size {}",
                                   atr_queue_.size());
      return;
    }
    results.emplace_back(*entry);
    try {
      entry->clean(&results.back());
      results.back().success(true);
    } catch (std::runtime_error&) {
      results.back().success(false);
    }
  }
}

void
transactions_cleanup::attempts_loop()
{
  try {
    CB_ATTEMPT_CLEANUP_LOG_DEBUG("cleanup attempts loop starting...");
    while (interruptable_wait(cleanup_loop_delay_)) {
      while (auto entry = atr_queue_.pop()) {
        if (!is_running()) {
          CB_ATTEMPT_CLEANUP_LOG_DEBUG("loop stopping - {} entries on queue", atr_queue_.size());
          return;
        }
        // FIXME(SA): for some reason cppcheck thinks that entry here will
        // be always true, while pop() operation still might return empty optional
        if (entry) { // cppcheck-suppress knownConditionTrueFalse
          CB_ATTEMPT_CLEANUP_LOG_TRACE("beginning cleanup on {}", *entry);
          try {
            entry->clean();
          } catch (...) {
            // catch everything as we don't want to raise out of this thread
            CB_ATTEMPT_CLEANUP_LOG_DEBUG("got error cleaning {}, leaving for lost txn cleanup",
                                         entry.value());
          }
        }
      }
    }
    CB_ATTEMPT_CLEANUP_LOG_DEBUG("stopping - {} entries on queue", atr_queue_.size());
  } catch (const std::runtime_error& e) {
    CB_ATTEMPT_CLEANUP_LOG_ERROR("got error \"{}\" in attempts_loop", e.what());
  }
}

void
transactions_cleanup::add_attempt(const std::shared_ptr<attempt_context>& ctx)
{
  auto ctx_impl = std::dynamic_pointer_cast<attempt_context_impl>(ctx);
  Expects(ctx_impl != nullptr);
  switch (ctx_impl->state()) {
    case attempt_state::NOT_STARTED:
    case attempt_state::COMPLETED:
    case attempt_state::ROLLED_BACK:
      CB_ATTEMPT_CLEANUP_LOG_TRACE("attempt in state {}, not adding to cleanup",
                                   attempt_state_name(ctx_impl->state()));
      return;
    default:
      if (config_.cleanup_config.cleanup_client_attempts) {
        CB_ATTEMPT_CLEANUP_LOG_DEBUG("adding attempt {} to cleanup queue", ctx_impl->id());
        atr_queue_.push(ctx);
      } else {
        CB_ATTEMPT_CLEANUP_LOG_TRACE("not cleaning client attempts, ignoring {}", ctx_impl->id());
      }
  }
}

void
transactions_cleanup::add_collection(const couchbase::transactions::transaction_keyspace& keyspace)
{

  if (keyspace.valid() && config_.cleanup_config.cleanup_lost_attempts) {
    std::unique_lock<std::mutex> lock(mutex_);

    auto it = std::find(collections_.begin(), collections_.end(), keyspace);
    if (it == collections_.end()) {
      collections_.emplace_back(keyspace);
      // Spawn only while running. stop() takes the worker handles out and joins them, so a
      // handle appended after that point is one nothing has agreed to join -- and a
      // joinable std::thread that reaches its destructor terminates. Registering the
      // keyspace is still right: start() spawns a worker for everything in collections_,
      // so cleanup resumes for it on the next start.
      if (running_) {
        // start cleaning right away
        lost_attempt_cleanup_workers_.emplace_back([this, keyspace = collections_.back()]() {
          this->clean_collection(keyspace);
        });
      }
    }
    lock.unlock();
    CB_ATTEMPT_CLEANUP_LOG_DEBUG("added {} to lost transaction cleanup", keyspace);
  }
}

void
transactions_cleanup::start()
{
  // The pool has to exist before any worker does: clean_collection() treats a missing pool
  // as a broken invariant and gives up on the keyspace.
  if (config_.cleanup_config.cleanup_lost_attempts) {
    // Blocking per-ATR checks are offloaded to this bounded pool, never the single IO thread.
    atr_cleanup_pool_.emplace(max_in_flight_atr_checks_);
  }

  {
    // running_ is read under mutex_ everywhere else -- is_running(), interruptable_wait(),
    // add_collection() and stop() -- so it is written under it here too, rather than being
    // the one unsynchronized access to it.
    //
    // Publishing it in the same critical section as the respawn below also settles who
    // spawns what. A concurrent add_collection() either runs before this section and only
    // registers its keyspace, which the loop then spawns for, or after it and spawns for
    // itself, by which point the loop has already gone past. Exactly one worker per
    // keyspace either way.
    const std::unique_lock<std::mutex> lock(mutex_);
    running_ = config_.cleanup_config.cleanup_client_attempts ||
               config_.cleanup_config.cleanup_lost_attempts;

    if (config_.cleanup_config.cleanup_lost_attempts) {
      // Re-spawn a worker for every keyspace still registered from before a previous
      // stop(). add_collection() below cannot do it: it dedupes on collections_, so
      // for an already-registered keyspace it skips the worker and lost-attempt
      // cleanup stays dead for the rest of the process. Any fork leaves exactly that
      // state behind -- notify_fork(prepare) stops cleanup, notify_fork(parent)
      // starts it again.
      for (const auto& keyspace : collections_) {
        lost_attempt_cleanup_workers_.emplace_back([this, keyspace]() {
          this->clean_collection(keyspace);
        });
      }
    }
  }
  if (config_.cleanup_config.cleanup_client_attempts) {
    cleanup_thr_ = std::thread([this] {
      attempts_loop();
    });
  }
  if (config_.metadata_collection) {
    add_collection({ config_.metadata_collection->bucket,
                     config_.metadata_collection->scope,
                     config_.metadata_collection->collection });
  }
  for (const auto& k : config_.cleanup_config.collections) {
    add_collection(k);
  }
}

void
transactions_cleanup::stop()
{
  {
    const std::unique_lock<std::mutex> lock(mutex_);
    running_ = false;
    cv_.notify_all();
  }
  if (cleanup_thr_.joinable()) {
    cleanup_thr_.join();
    CB_ATTEMPT_CLEANUP_LOG_DEBUG("cleanup attempt thread closed");
  }
  // Take the handles out under mutex_, then join them with the lock released.
  //
  // Under the lock, because add_collection() and start() append to this list while
  // holding mutex_. Walking it while another thread appends is a data race on the
  // list itself, and an append that lands after it was emptied would leave a
  // joinable std::thread to be destroyed later, which terminates.
  //
  // With the lock released, because the workers themselves take mutex_ to see
  // running_ (interruptable_wait, and the collections_ lookup in clean_collection),
  // so joining while holding it deadlocks.
  //
  // Moving the handles out also drops them, which a later start() needs in order to
  // spawn fresh workers. collections_ is deliberately kept: it also holds keyspaces
  // registered at runtime, and discarding those would silently stop cleaning them.
  std::list<std::thread> workers;
  {
    const std::unique_lock<std::mutex> lock(mutex_);
    workers.swap(lost_attempt_cleanup_workers_);
  }
  for (auto& t : workers) {
    CB_LOST_ATTEMPT_CLEANUP_LOG_DEBUG("shutting down all lost attempt threads...");
    if (t.joinable()) {
      t.join();
    }
  }
  // Workers drain their in-flight checks before returning, so the pool is now idle. stop() before
  // join(): the pool's threads wait for work, so join() alone would block indefinitely on an idle
  // pool; stop() requests shutdown so join() returns (there is no pending work left to abandon).
  // Reset afterwards so stop() is idempotent: close() calls stop() from the destructor and the user
  // may also call it explicitly, and a joined pool must not be joined again.
  if (atr_cleanup_pool_) {
    atr_cleanup_pool_->stop();
    atr_cleanup_pool_->join();
    atr_cleanup_pool_.reset();
  }
}

void
transactions_cleanup::close()
{
  stop();
  CB_LOST_ATTEMPT_CLEANUP_LOG_DEBUG("all lost attempt cleanup threads closed");
  // In a forked child the record is the parent's, and the parent is still registered under it.
  if (client_record_owner_pid_ == utils::current_process_id()) {
    remove_client_record_from_all_buckets(client_uuid_);
  }
}

transactions_cleanup::~transactions_cleanup()
{
  close();
}
} // namespace couchbase::core::transactions
