DistributedApplications: range movement with freeze/verify/crash recovery and replicated directory persistence (P-DISTRIBUTED-07)
This commit is contained in:
1 parent
cdc55a594e
commit
504f259d02
4 files changed
+734
No files matched your search
@@ -12,6 +12,7 @@ find_package(Void CONFIG REQUIRED COMPONENTS Functional Net)
|
|||||||
add_library(distributed_host_graph STATIC
|
add_library(distributed_host_graph STATIC
|
||||||
src/cluster_fixture.cpp
|
src/cluster_fixture.cpp
|
||||||
src/host_graph.cpp
|
src/host_graph.cpp
|
||||||
|
src/range_placement.cpp
|
||||||
src/replicated_kv.cpp
|
src/replicated_kv.cpp
|
||||||
src/replicated_queue.cpp
|
src/replicated_queue.cpp
|
||||||
src/single_node.cpp)
|
src/single_node.cpp)
|
||||||
@@ -36,6 +37,9 @@ target_link_libraries(distributed_replicated_queue_test PRIVATE distributed_host
|
|||||||
add_executable(distributed_operations_test tests/operations_test.cpp)
|
add_executable(distributed_operations_test tests/operations_test.cpp)
|
||||||
target_link_libraries(distributed_operations_test PRIVATE distributed_host_graph)
|
target_link_libraries(distributed_operations_test PRIVATE distributed_host_graph)
|
||||||
|
|
||||||
|
add_executable(distributed_range_placement_test tests/range_placement_test.cpp)
|
||||||
|
target_link_libraries(distributed_range_placement_test PRIVATE distributed_host_graph)
|
||||||
|
|
||||||
enable_testing()
|
enable_testing()
|
||||||
add_test(NAME distributed_host_graph COMMAND distributed_host_graph_test)
|
add_test(NAME distributed_host_graph COMMAND distributed_host_graph_test)
|
||||||
add_test(NAME distributed_single_node COMMAND distributed_single_node_test)
|
add_test(NAME distributed_single_node COMMAND distributed_single_node_test)
|
||||||
@@ -43,3 +47,4 @@ add_test(NAME distributed_replicated_kv COMMAND distributed_replicated_kv_test)
|
|||||||
add_test(NAME distributed_cluster_fixture COMMAND distributed_cluster_fixture_test)
|
add_test(NAME distributed_cluster_fixture COMMAND distributed_cluster_fixture_test)
|
||||||
add_test(NAME distributed_replicated_queue COMMAND distributed_replicated_queue_test)
|
add_test(NAME distributed_replicated_queue COMMAND distributed_replicated_queue_test)
|
||||||
add_test(NAME distributed_operations COMMAND distributed_operations_test)
|
add_test(NAME distributed_operations COMMAND distributed_operations_test)
|
||||||
|
add_test(NAME distributed_range_placement COMMAND distributed_range_placement_test)
|
||||||
@@ -0,0 +1,163 @@
|
|||||||
|
#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
|
||||||
@@ -0,0 +1,395 @@
|
|||||||
|
#include "distributed_host/range_placement.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <format>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace DistributedHost
|
||||||
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
[[nodiscard]] Void::Result::Error invalid(std::string message)
|
||||||
|
{
|
||||||
|
return Void::Result::Error{
|
||||||
|
Void::Result::ErrorKind::invalid_argument, std::move(message)};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Error conflict(std::string message)
|
||||||
|
{
|
||||||
|
return Void::Result::Error{Void::Result::ErrorKind::conflict,
|
||||||
|
std::move(message)};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Error not_found(std::string message)
|
||||||
|
{
|
||||||
|
return Void::Result::Error{Void::Result::ErrorKind::not_found,
|
||||||
|
std::move(message)};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Error corrupt(std::string message)
|
||||||
|
{
|
||||||
|
return Void::Result::Error{Void::Result::ErrorKind::corrupt,
|
||||||
|
std::move(message)};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] bool overlapping(const KeyRange& a,
|
||||||
|
const KeyRange& b) noexcept
|
||||||
|
{
|
||||||
|
return a.start < b.end && b.start < a.end;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] bool has_separator(std::string_view text) noexcept
|
||||||
|
{
|
||||||
|
return text.find_first_of("\t\n") != std::string_view::npos;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
std::string_view move_phase_name(MovePhase phase) noexcept
|
||||||
|
{
|
||||||
|
switch (phase) {
|
||||||
|
case MovePhase::None: return "none";
|
||||||
|
case MovePhase::Frozen: return "frozen";
|
||||||
|
case MovePhase::Verified: return "verified";
|
||||||
|
case MovePhase::Completed: return "completed";
|
||||||
|
case MovePhase::Aborted: return "aborted";
|
||||||
|
}
|
||||||
|
return "none";
|
||||||
|
}
|
||||||
|
|
||||||
|
RangeDirectory::RangeDirectory(std::vector<std::string> voters)
|
||||||
|
: m_voters(std::move(voters))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RangeDirectory::known_voter(std::string_view owner) const
|
||||||
|
{
|
||||||
|
return std::ranges::find(m_voters, owner) != m_voters.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<void>
|
||||||
|
RangeDirectory::assign(KeyRange range, std::string owner)
|
||||||
|
{
|
||||||
|
if (!range.valid() || has_separator(range.start) ||
|
||||||
|
has_separator(range.end)) {
|
||||||
|
return std::unexpected{
|
||||||
|
invalid("RangeDirectory: invalid range bounds")};
|
||||||
|
}
|
||||||
|
if (!known_voter(owner)) {
|
||||||
|
return std::unexpected{
|
||||||
|
invalid("RangeDirectory: owner is not a configured voter")};
|
||||||
|
}
|
||||||
|
for (const auto& [start, ownership] : m_ranges) {
|
||||||
|
if (overlapping(range, ownership.range)) {
|
||||||
|
return std::unexpected{conflict(std::format(
|
||||||
|
"RangeDirectory: range overlaps the existing [{}, {})",
|
||||||
|
ownership.range.start, ownership.range.end))};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_ranges.emplace(range.start, RangeOwnership{range, std::move(owner)});
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<std::string>
|
||||||
|
RangeDirectory::owner_of(std::string_view key) const
|
||||||
|
{
|
||||||
|
// Highest range start <= key.
|
||||||
|
auto candidate = m_ranges.upper_bound(std::string{key});
|
||||||
|
if (candidate == m_ranges.begin()) {
|
||||||
|
return std::unexpected{
|
||||||
|
not_found("RangeDirectory: no range owns the key")};
|
||||||
|
}
|
||||||
|
--candidate;
|
||||||
|
if (!candidate->second.range.contains(key)) {
|
||||||
|
return std::unexpected{
|
||||||
|
not_found("RangeDirectory: no range owns the key")};
|
||||||
|
}
|
||||||
|
return candidate->second.owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<RangeOwnership> RangeDirectory::ranges() const
|
||||||
|
{
|
||||||
|
std::vector<RangeOwnership> out;
|
||||||
|
out.reserve(m_ranges.size());
|
||||||
|
for (const auto& [start, ownership] : m_ranges) {
|
||||||
|
(void)start;
|
||||||
|
out.push_back(ownership);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
const RangeDirectory::ActiveMove*
|
||||||
|
RangeDirectory::active_for(const KeyRange& range) const
|
||||||
|
{
|
||||||
|
for (const auto& [id, move] : m_moves) {
|
||||||
|
(void)id;
|
||||||
|
if (move.record.range == range &&
|
||||||
|
(move.record.phase == MovePhase::Frozen ||
|
||||||
|
move.record.phase == MovePhase::Verified)) {
|
||||||
|
return &move;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
RangeDirectory::ActiveMove*
|
||||||
|
RangeDirectory::mutable_move(std::uint64_t move_id)
|
||||||
|
{
|
||||||
|
const auto found = m_moves.find(move_id);
|
||||||
|
return found == m_moves.end() ? nullptr : &found->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<std::uint64_t>
|
||||||
|
RangeDirectory::begin_move(KeyRange range, std::string target)
|
||||||
|
{
|
||||||
|
if (!known_voter(target)) {
|
||||||
|
return std::unexpected{
|
||||||
|
invalid("RangeDirectory: target is not a configured voter")};
|
||||||
|
}
|
||||||
|
if (active_for(range) != nullptr) {
|
||||||
|
return std::unexpected{conflict(
|
||||||
|
"RangeDirectory: the range already has an active move")};
|
||||||
|
}
|
||||||
|
auto owner = owner_of(range.start);
|
||||||
|
if (!owner) {
|
||||||
|
return std::unexpected{std::move(owner).error()};
|
||||||
|
}
|
||||||
|
const auto found = m_ranges.find(range.start);
|
||||||
|
if (found == m_ranges.end() || !(found->second.range == range)) {
|
||||||
|
return std::unexpected{not_found(
|
||||||
|
"RangeDirectory: the range must match one assigned range")};
|
||||||
|
}
|
||||||
|
if (*owner == target) {
|
||||||
|
return std::unexpected{
|
||||||
|
conflict("RangeDirectory: target already owns the range")};
|
||||||
|
}
|
||||||
|
|
||||||
|
MoveRecord record;
|
||||||
|
record.id = m_next_move++;
|
||||||
|
record.range = std::move(range);
|
||||||
|
record.source = *owner;
|
||||||
|
record.target = std::move(target);
|
||||||
|
record.phase = MovePhase::Frozen;
|
||||||
|
m_moves.emplace(record.id, ActiveMove{record});
|
||||||
|
return record.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<void>
|
||||||
|
RangeDirectory::transfer(std::uint64_t move_id, std::size_t entries)
|
||||||
|
{
|
||||||
|
ActiveMove* move = mutable_move(move_id);
|
||||||
|
if (move == nullptr || move->record.phase != MovePhase::Frozen) {
|
||||||
|
return std::unexpected{
|
||||||
|
conflict("RangeDirectory: move is not frozen")};
|
||||||
|
}
|
||||||
|
move->record.transferred = entries;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<void>
|
||||||
|
RangeDirectory::verify(std::uint64_t move_id, std::size_t entries)
|
||||||
|
{
|
||||||
|
ActiveMove* move = mutable_move(move_id);
|
||||||
|
if (move == nullptr || move->record.phase != MovePhase::Frozen) {
|
||||||
|
return std::unexpected{
|
||||||
|
conflict("RangeDirectory: move is not frozen")};
|
||||||
|
}
|
||||||
|
if (entries != move->record.transferred) {
|
||||||
|
return std::unexpected{conflict(
|
||||||
|
"RangeDirectory: verified entry count does not match the "
|
||||||
|
"transfer")};
|
||||||
|
}
|
||||||
|
move->record.phase = MovePhase::Verified;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<void> RangeDirectory::complete(std::uint64_t move_id)
|
||||||
|
{
|
||||||
|
ActiveMove* move = mutable_move(move_id);
|
||||||
|
if (move == nullptr || move->record.phase != MovePhase::Verified) {
|
||||||
|
return std::unexpected{
|
||||||
|
conflict("RangeDirectory: move is not verified")};
|
||||||
|
}
|
||||||
|
const auto found = m_ranges.find(move->record.range.start);
|
||||||
|
if (found == m_ranges.end() ||
|
||||||
|
found->second.owner != move->record.source) {
|
||||||
|
return std::unexpected{
|
||||||
|
conflict("RangeDirectory: source no longer owns the range")};
|
||||||
|
}
|
||||||
|
found->second.owner = move->record.target;
|
||||||
|
move->record.phase = MovePhase::Completed;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
void RangeDirectory::abort(std::uint64_t move_id, std::string reason) noexcept
|
||||||
|
{
|
||||||
|
if (ActiveMove* move = mutable_move(move_id); move != nullptr) {
|
||||||
|
if (move->record.phase == MovePhase::Frozen ||
|
||||||
|
move->record.phase == MovePhase::Verified) {
|
||||||
|
move->record.phase = MovePhase::Aborted;
|
||||||
|
move->record.note = std::move(reason);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<void>
|
||||||
|
RangeDirectory::admit_write(std::string_view key) const
|
||||||
|
{
|
||||||
|
if (auto owned = owner_of(key); !owned) {
|
||||||
|
return std::unexpected{std::move(owned).error()};
|
||||||
|
}
|
||||||
|
for (const auto& [id, move] : m_moves) {
|
||||||
|
(void)id;
|
||||||
|
if (move.record.range.contains(key) &&
|
||||||
|
(move.record.phase == MovePhase::Frozen ||
|
||||||
|
move.record.phase == MovePhase::Verified)) {
|
||||||
|
return std::unexpected{conflict(
|
||||||
|
"RangeDirectory: range is frozen for movement")};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<void>
|
||||||
|
RangeDirectory::admit_read(std::string_view key) const
|
||||||
|
{
|
||||||
|
if (auto owned = owner_of(key); !owned) {
|
||||||
|
return std::unexpected{std::move(owned).error()};
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<RangeDirectory::Recovery>
|
||||||
|
RangeDirectory::recover(std::uint64_t move_id)
|
||||||
|
{
|
||||||
|
ActiveMove* move = mutable_move(move_id);
|
||||||
|
if (move == nullptr) {
|
||||||
|
return std::unexpected{
|
||||||
|
not_found("RangeDirectory: unknown move")};
|
||||||
|
}
|
||||||
|
if (move->record.phase == MovePhase::Verified) {
|
||||||
|
// The copy was verified before the crash: roll forward.
|
||||||
|
if (auto completed = complete(move_id); !completed) {
|
||||||
|
return std::unexpected{std::move(completed).error()};
|
||||||
|
}
|
||||||
|
return Recovery::RolledForward;
|
||||||
|
}
|
||||||
|
if (move->record.phase == MovePhase::Frozen) {
|
||||||
|
// No complete copy is proven: roll back to the source.
|
||||||
|
abort(move_id, "recovered after crash before verification");
|
||||||
|
return Recovery::RolledBack;
|
||||||
|
}
|
||||||
|
return Recovery::None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<MoveRecord>
|
||||||
|
RangeDirectory::move(std::uint64_t move_id) const
|
||||||
|
{
|
||||||
|
const auto found = m_moves.find(move_id);
|
||||||
|
if (found == m_moves.end()) {
|
||||||
|
return std::unexpected{not_found("RangeDirectory: unknown move")};
|
||||||
|
}
|
||||||
|
return found->second.record;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string RangeDirectory::encode() const
|
||||||
|
{
|
||||||
|
std::string out;
|
||||||
|
for (const auto& [start, ownership] : m_ranges) {
|
||||||
|
(void)start;
|
||||||
|
out += ownership.range.start;
|
||||||
|
out += '\t';
|
||||||
|
out += ownership.range.end;
|
||||||
|
out += '\t';
|
||||||
|
out += ownership.owner;
|
||||||
|
out += '\n';
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<void>
|
||||||
|
RangeDirectory::persist(ReplicatedKvCluster& cluster,
|
||||||
|
std::string_view leader, std::string_view key)
|
||||||
|
{
|
||||||
|
const std::string encoded = encode();
|
||||||
|
if (encoded.size() > max_directory_bytes) {
|
||||||
|
return std::unexpected{invalid(
|
||||||
|
"RangeDirectory: directory exceeds max_directory_bytes")};
|
||||||
|
}
|
||||||
|
const KvWriteReply reply =
|
||||||
|
cluster.propose(leader, std::string{key}, encoded);
|
||||||
|
if (reply.outcome != KvWriteOutcome::Applied) {
|
||||||
|
return std::unexpected{Void::Result::Error{
|
||||||
|
Void::Result::ErrorKind::exhausted,
|
||||||
|
"RangeDirectory: directory write was not majority-committed"}};
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<void>
|
||||||
|
RangeDirectory::load(ReplicatedKvCluster& cluster,
|
||||||
|
std::string_view leader, std::string_view key)
|
||||||
|
{
|
||||||
|
auto stored = cluster.quorum_read(leader, key);
|
||||||
|
if (!stored) {
|
||||||
|
return std::unexpected{std::move(stored).error()};
|
||||||
|
}
|
||||||
|
if (!stored->has_value()) {
|
||||||
|
return std::unexpected{
|
||||||
|
not_found("RangeDirectory: no persisted directory")};
|
||||||
|
}
|
||||||
|
const std::string& encoded = **stored;
|
||||||
|
if (encoded.size() > max_directory_bytes) {
|
||||||
|
return std::unexpected{
|
||||||
|
corrupt("RangeDirectory: persisted directory is too large")};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate everything into a candidate map before installing it.
|
||||||
|
std::map<std::string, RangeOwnership, std::less<>> candidate;
|
||||||
|
std::size_t line_begin = 0;
|
||||||
|
while (line_begin < encoded.size()) {
|
||||||
|
const std::size_t line_end = encoded.find('\n', line_begin);
|
||||||
|
if (line_end == std::string::npos) {
|
||||||
|
return std::unexpected{
|
||||||
|
corrupt("RangeDirectory: unterminated record")};
|
||||||
|
}
|
||||||
|
const std::string_view line = std::string_view{encoded}.substr(
|
||||||
|
line_begin, line_end - line_begin);
|
||||||
|
const std::size_t first = line.find('\t');
|
||||||
|
const std::size_t second =
|
||||||
|
first == std::string_view::npos ? std::string_view::npos
|
||||||
|
: line.find('\t', first + 1);
|
||||||
|
if (first == std::string_view::npos ||
|
||||||
|
second == std::string_view::npos ||
|
||||||
|
line.find('\t', second + 1) != std::string_view::npos) {
|
||||||
|
return std::unexpected{
|
||||||
|
corrupt("RangeDirectory: malformed record")};
|
||||||
|
}
|
||||||
|
RangeOwnership ownership;
|
||||||
|
ownership.range.start = std::string{line.substr(0, first)};
|
||||||
|
ownership.range.end =
|
||||||
|
std::string{line.substr(first + 1, second - first - 1)};
|
||||||
|
ownership.owner = std::string{line.substr(second + 1)};
|
||||||
|
if (!ownership.range.valid() ||
|
||||||
|
!known_voter(ownership.owner) ||
|
||||||
|
candidate.contains(ownership.range.start)) {
|
||||||
|
return std::unexpected{
|
||||||
|
corrupt("RangeDirectory: invalid record")};
|
||||||
|
}
|
||||||
|
for (const auto& [start, existing] : candidate) {
|
||||||
|
(void)start;
|
||||||
|
if (overlapping(ownership.range, existing.range)) {
|
||||||
|
return std::unexpected{
|
||||||
|
corrupt("RangeDirectory: overlapping ranges")};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
candidate.emplace(ownership.range.start, std::move(ownership));
|
||||||
|
line_begin = line_end + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_ranges = std::move(candidate);
|
||||||
|
// Interrupted moves do not survive a directory load.
|
||||||
|
m_moves.clear();
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
} // namespace DistributedHost
|
||||||
@@ -0,0 +1,171 @@
|
|||||||
|
#include "distributed_host/range_placement.hpp"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
using namespace DistributedHost;
|
||||||
|
|
||||||
|
RangeDirectory directory()
|
||||||
|
{
|
||||||
|
RangeDirectory dir{{"a", "b", "c"}};
|
||||||
|
assert(dir.assign(KeyRange{"a", "m"}, "a").has_value());
|
||||||
|
assert(dir.assign(KeyRange{"m", "z"}, "b").has_value());
|
||||||
|
return dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ranges_do_not_overlap_and_must_have_known_owners()
|
||||||
|
{
|
||||||
|
auto dir = directory();
|
||||||
|
assert(*dir.owner_of("apple") == "a");
|
||||||
|
assert(*dir.owner_of("monkey") == "b");
|
||||||
|
assert(!dir.owner_of("zebra").has_value()); // outside [a, z)
|
||||||
|
|
||||||
|
assert(!dir.assign(KeyRange{"l", "n"}, "c").has_value());
|
||||||
|
assert(!dir.assign(KeyRange{"n", "p"}, "unknown").has_value());
|
||||||
|
assert(!dir.assign(KeyRange{"p", "p"}, "a").has_value());
|
||||||
|
assert(dir.ranges().size() == 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void move_freezes_writes_until_completion()
|
||||||
|
{
|
||||||
|
auto dir = directory();
|
||||||
|
auto move = dir.begin_move(KeyRange{"a", "m"}, "c");
|
||||||
|
assert(move.has_value());
|
||||||
|
|
||||||
|
// Writes in the moving range are rejected; reads stay with the source.
|
||||||
|
assert(!dir.admit_write("apple").has_value());
|
||||||
|
assert(dir.admit_read("apple").has_value());
|
||||||
|
assert(*dir.owner_of("apple") == "a");
|
||||||
|
// Other ranges are unaffected.
|
||||||
|
assert(dir.admit_write("monkey").has_value());
|
||||||
|
|
||||||
|
// Completion requires a verified copy.
|
||||||
|
assert(!dir.complete(*move).has_value());
|
||||||
|
assert(!dir.verify(*move, 5).has_value()); // nothing transferred
|
||||||
|
assert(dir.transfer(*move, 3).has_value());
|
||||||
|
assert(!dir.verify(*move, 2).has_value()); // count mismatch
|
||||||
|
assert(dir.verify(*move, 3).has_value());
|
||||||
|
assert(dir.complete(*move).has_value());
|
||||||
|
|
||||||
|
assert(*dir.owner_of("apple") == "c");
|
||||||
|
assert(dir.admit_write("apple").has_value());
|
||||||
|
const auto record = dir.move(*move);
|
||||||
|
assert(record.has_value());
|
||||||
|
assert(record->phase == MovePhase::Completed);
|
||||||
|
assert(record->transferred == 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void crash_before_verification_rolls_back()
|
||||||
|
{
|
||||||
|
auto dir = directory();
|
||||||
|
auto move = dir.begin_move(KeyRange{"a", "m"}, "c");
|
||||||
|
assert(move.has_value());
|
||||||
|
assert(dir.transfer(*move, 2).has_value());
|
||||||
|
|
||||||
|
// Simulated restart of the move owner: no verified copy exists.
|
||||||
|
auto recovered = dir.recover(*move);
|
||||||
|
assert(recovered.has_value());
|
||||||
|
assert(*recovered == RangeDirectory::Recovery::RolledBack);
|
||||||
|
assert(*dir.owner_of("apple") == "a");
|
||||||
|
assert(dir.admit_write("apple").has_value());
|
||||||
|
assert(dir.move(*move)->phase == MovePhase::Aborted);
|
||||||
|
}
|
||||||
|
|
||||||
|
void crash_after_verification_rolls_forward()
|
||||||
|
{
|
||||||
|
auto dir = directory();
|
||||||
|
auto move = dir.begin_move(KeyRange{"m", "z"}, "a");
|
||||||
|
assert(move.has_value());
|
||||||
|
assert(dir.transfer(*move, 4).has_value());
|
||||||
|
assert(dir.verify(*move, 4).has_value());
|
||||||
|
|
||||||
|
auto recovered = dir.recover(*move);
|
||||||
|
assert(recovered.has_value());
|
||||||
|
assert(*recovered == RangeDirectory::Recovery::RolledForward);
|
||||||
|
assert(*dir.owner_of("monkey") == "a");
|
||||||
|
assert(dir.admit_write("monkey").has_value());
|
||||||
|
assert(dir.move(*move)->phase == MovePhase::Completed);
|
||||||
|
}
|
||||||
|
|
||||||
|
void explicit_abort_restores_the_source()
|
||||||
|
{
|
||||||
|
auto dir = directory();
|
||||||
|
auto move = dir.begin_move(KeyRange{"a", "m"}, "c");
|
||||||
|
assert(move.has_value());
|
||||||
|
dir.abort(*move, "operator cancelled");
|
||||||
|
assert(*dir.owner_of("apple") == "a");
|
||||||
|
assert(dir.admit_write("apple").has_value());
|
||||||
|
assert(dir.move(*move)->note == "operator cancelled");
|
||||||
|
// A completed move cannot be aborted.
|
||||||
|
auto second = dir.begin_move(KeyRange{"a", "m"}, "c");
|
||||||
|
assert(second.has_value());
|
||||||
|
assert(dir.transfer(*second, 1).has_value());
|
||||||
|
assert(dir.verify(*second, 1).has_value());
|
||||||
|
assert(dir.complete(*second).has_value());
|
||||||
|
dir.abort(*second, "too late");
|
||||||
|
assert(*dir.owner_of("apple") == "c");
|
||||||
|
}
|
||||||
|
|
||||||
|
void one_active_move_per_range()
|
||||||
|
{
|
||||||
|
auto dir = directory();
|
||||||
|
assert(dir.begin_move(KeyRange{"a", "m"}, "c").has_value());
|
||||||
|
auto second = dir.begin_move(KeyRange{"a", "m"}, "b");
|
||||||
|
assert(!second.has_value());
|
||||||
|
assert(second.error().kind() == Void::Result::ErrorKind::conflict);
|
||||||
|
// Moving to the current owner is refused.
|
||||||
|
auto same = dir.begin_move(KeyRange{"m", "z"}, "b");
|
||||||
|
assert(!same.has_value());
|
||||||
|
}
|
||||||
|
|
||||||
|
void directory_survives_a_replicated_restart()
|
||||||
|
{
|
||||||
|
ReplicatedKvCluster cluster{{"a", "b", "c"}};
|
||||||
|
assert(cluster.elect("a").has_value());
|
||||||
|
|
||||||
|
auto original = directory();
|
||||||
|
assert(original.persist(cluster, "a").has_value());
|
||||||
|
|
||||||
|
// A new directory instance loads the replicated ownership map.
|
||||||
|
RangeDirectory restored{{"a", "b", "c"}};
|
||||||
|
assert(restored.load(cluster, "a").has_value());
|
||||||
|
assert(restored.ranges().size() == 2);
|
||||||
|
assert(*restored.owner_of("apple") == "a");
|
||||||
|
assert(*restored.owner_of("monkey") == "b");
|
||||||
|
|
||||||
|
// A directory whose voters do not include the stored owners is
|
||||||
|
// rejected rather than partially installed.
|
||||||
|
RangeDirectory narrower{{"a"}};
|
||||||
|
assert(!narrower.load(cluster, "a").has_value());
|
||||||
|
assert(narrower.ranges().empty());
|
||||||
|
|
||||||
|
// Corrupt persisted content is rejected.
|
||||||
|
assert(cluster.propose("a", std::string{RangeDirectory::default_key},
|
||||||
|
std::string{"broken-record\n"})
|
||||||
|
.outcome == KvWriteOutcome::Applied);
|
||||||
|
RangeDirectory corrupt_dir{{"a", "b", "c"}};
|
||||||
|
assert(!corrupt_dir.load(cluster, "a").has_value());
|
||||||
|
assert(corrupt_dir.ranges().empty());
|
||||||
|
|
||||||
|
// Persisting without quorum is not durable.
|
||||||
|
cluster.set_partition("b", 1);
|
||||||
|
cluster.set_partition("c", 1);
|
||||||
|
assert(!original.persist(cluster, "a").has_value());
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ranges_do_not_overlap_and_must_have_known_owners();
|
||||||
|
move_freezes_writes_until_completion();
|
||||||
|
crash_before_verification_rolls_back();
|
||||||
|
crash_after_verification_rolls_forward();
|
||||||
|
explicit_abort_restores_the_source();
|
||||||
|
one_active_move_per_range();
|
||||||
|
directory_survives_a_replicated_restart();
|
||||||
|
std::puts("distributed range placement ok");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user