#pragma once #include "distributed_host/replicated_kv.hpp" #include #include #include #include #include #include #include namespace DistributedHost { /// Half-open key range `[start, end)`. struct KeyRange { std::string start; std::string end; [[nodiscard]] bool contains(std::string_view key) const noexcept { return key >= start && key < end; } [[nodiscard]] bool valid() const noexcept { return start < end; } [[nodiscard]] bool operator==(const KeyRange&) const noexcept = default; }; struct RangeOwnership { KeyRange range; std::string owner; [[nodiscard]] bool operator==(const RangeOwnership&) const noexcept = default; }; enum class MovePhase : std::uint8_t { None, /// Writes for the range are rejected; reads stay with the source. Frozen, /// The target reported a complete verified copy; completing is safe. Verified, Completed, Aborted }; [[nodiscard]] std::string_view move_phase_name(MovePhase phase) noexcept; struct MoveRecord { std::uint64_t id{0}; KeyRange range; std::string source; std::string target; MovePhase phase{MovePhase::None}; std::size_t transferred{0}; std::string note; }; /// Range ownership, freeze-based movement and replicated directory /// persistence. /// /// Invariants: /// * ranges never overlap and every range has exactly one owner; /// * a range being moved rejects writes (reads remain with the source); /// * ownership switches only after the target's copy is verified; /// * crash recovery is deterministic: a verified move rolls forward, /// anything earlier rolls back, so ownership is never ambiguous. /// /// Not thread-safe: the host serializes directory operations. class RangeDirectory { public: explicit RangeDirectory(std::vector voters); [[nodiscard]] const std::vector& voters() const noexcept { return m_voters; } [[nodiscard]] Void::Result::Result assign(KeyRange range, std::string owner); [[nodiscard]] Void::Result::Result owner_of(std::string_view key) const; [[nodiscard]] std::vector ranges() const; // ── movement ──────────────────────────────────────────────────── [[nodiscard]] Void::Result::Result begin_move(KeyRange range, std::string target); /// Record entries copied to the target. Idempotent while frozen. [[nodiscard]] Void::Result::Result transfer(std::uint64_t move_id, std::size_t entries); /// The target certifies a complete copy of `entries` entries. [[nodiscard]] Void::Result::Result verify(std::uint64_t move_id, std::size_t entries); [[nodiscard]] Void::Result::Result complete(std::uint64_t move_id); void abort(std::uint64_t move_id, std::string reason) noexcept; /// Write admission: rejected while the covering range is moving. [[nodiscard]] Void::Result::Result admit_write(std::string_view key) const; /// Reads stay with the source owner until completion. [[nodiscard]] Void::Result::Result admit_read(std::string_view key) const; // ── crash recovery ───────────────────────────────────────────── enum class Recovery : std::uint8_t { None, RolledForward, RolledBack }; /// Resolve an interrupted move after a crash: verified copies are /// completed, anything earlier is aborted. [[nodiscard]] Void::Result::Result recover(std::uint64_t move_id); [[nodiscard]] Void::Result::Result move(std::uint64_t move_id) const; // ── replicated directory persistence ─────────────────────────── /// Largest number of directory bytes accepted on load. static constexpr std::size_t max_directory_bytes = 64ULL * 1024ULL; [[nodiscard]] Void::Result::Result persist(ReplicatedKvCluster& cluster, std::string_view leader, std::string_view key = default_key); /// Replace the local directory from the replicated state. The /// loaded map is fully validated before it is installed. [[nodiscard]] Void::Result::Result load(ReplicatedKvCluster& cluster, std::string_view leader, std::string_view key = default_key); inline static constexpr std::string_view default_key = "__range_directory"; private: struct ActiveMove { MoveRecord record; }; [[nodiscard]] bool known_voter(std::string_view owner) const; [[nodiscard]] const ActiveMove* active_for(const KeyRange& range) const; [[nodiscard]] ActiveMove* mutable_move(std::uint64_t move_id); [[nodiscard]] std::string encode() const; std::vector m_voters; std::map> m_ranges; std::map m_moves; std::uint64_t m_next_move{1}; }; } // namespace DistributedHost