164 lines
5.1 KiB
C++
164 lines
5.1 KiB
C++
#pragma once
|
|
|
|
#include "distributed_host/replicated_kv.hpp"
|
|
|
|
#include <Void/Functional/Result.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <map>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
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<std::string> voters);
|
|
|
|
[[nodiscard]] const std::vector<std::string>& voters() const noexcept
|
|
{
|
|
return m_voters;
|
|
}
|
|
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
assign(KeyRange range, std::string owner);
|
|
|
|
[[nodiscard]] Void::Result::Result<std::string>
|
|
owner_of(std::string_view key) const;
|
|
|
|
[[nodiscard]] std::vector<RangeOwnership> ranges() const;
|
|
|
|
// ── movement ────────────────────────────────────────────────────
|
|
|
|
[[nodiscard]] Void::Result::Result<std::uint64_t>
|
|
begin_move(KeyRange range, std::string target);
|
|
|
|
/// Record entries copied to the target. Idempotent while frozen.
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
transfer(std::uint64_t move_id, std::size_t entries);
|
|
|
|
/// The target certifies a complete copy of `entries` entries.
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
verify(std::uint64_t move_id, std::size_t entries);
|
|
|
|
[[nodiscard]] Void::Result::Result<void> 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<void>
|
|
admit_write(std::string_view key) const;
|
|
|
|
/// Reads stay with the source owner until completion.
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
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<Recovery>
|
|
recover(std::uint64_t move_id);
|
|
|
|
[[nodiscard]] Void::Result::Result<MoveRecord>
|
|
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<void>
|
|
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<void>
|
|
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<std::string> m_voters;
|
|
std::map<std::string, RangeOwnership, std::less<>> m_ranges;
|
|
std::map<std::uint64_t, ActiveMove> m_moves;
|
|
std::uint64_t m_next_move{1};
|
|
};
|
|
} // namespace DistributedHost
|