#pragma once #include "distributed_host/replicated_kv.hpp" #include #include #include #include #include #include #include #include #include namespace DistributedHost { // ── Backup / restore ──────────────────────────────────────────────────── /// Stable cluster identity that a backup carries. A restore rehearsal must /// retain it: restoring into a differently identified cluster would merge /// unrelated histories. struct ClusterIdentity { std::string cluster; std::vector voters; [[nodiscard]] bool operator==(const ClusterIdentity& other) const { return cluster == other.cluster && std::ranges::is_permutation(voters, other.voters); } }; struct ClusterBackup { ClusterIdentity identity; /// Commit index observed when the backup was captured. std::uint64_t commit_index{0}; /// Quorum-confirmed applied state at that point. std::map> state; }; /// Capture a consistent backup from a quorum-confirmed leader. [[nodiscard]] inline Void::Result::Result capture_backup( ReplicatedKvCluster& cluster, std::string_view leader, ClusterIdentity identity) { auto snapshot = cluster.quorum_snapshot(leader); if (!snapshot) { return std::unexpected{std::move(snapshot).error()}; } ClusterBackup backup; backup.identity = std::move(identity); backup.commit_index = cluster.committed_index(); backup.state = std::move(*snapshot); return backup; } /// Restore a backup into an empty, separately identified cluster. /// /// The target identity must match the backup exactly. Every entry is /// written through the leader and must be majority-committed; a failed /// write stops the restore with the partial state left visible. [[nodiscard]] inline Void::Result::Result restore_backup( ReplicatedKvCluster& cluster, std::string_view leader, const ClusterBackup& backup, const ClusterIdentity& target) { if (!(backup.identity == target)) { return std::unexpected{Void::Result::Error{ Void::Result::ErrorKind::invalid_argument, "Restore: cluster identity does not match the backup"}}; } std::size_t restored = 0; for (const auto& [key, value] : backup.state) { const KvWriteReply reply = cluster.propose(leader, key, value); if (reply.outcome != KvWriteOutcome::Applied) { return std::unexpected{Void::Result::Error{ Void::Result::ErrorKind::exhausted, "Restore: entry was not majority-committed"}}; } ++restored; } return restored; } // ── Trust rotation ────────────────────────────────────────────────────── struct TrustGeneration { std::uint64_t id{0}; std::vector fingerprints; }; /// Overlapping trust generations for credential rotation. /// /// Rotation validates the new generation while the old one stays trusted, /// so live connections drain without ever disabling verification. An empty /// trust set trusts nothing (fail closed). class TrustStore { public: explicit TrustStore(std::vector initial_fingerprints = {}) { if (!initial_fingerprints.empty()) { m_generations.push_back( TrustGeneration{m_next_id++, std::move(initial_fingerprints)}); } } [[nodiscard]] bool trusted(std::string_view fingerprint) const noexcept { if (fingerprint.empty()) { return false; } for (const auto& generation : m_generations) { if (std::ranges::find(generation.fingerprints, fingerprint) != generation.fingerprints.end()) { return true; } } return false; } /// Add a new trusted generation while keeping the old ones. /// Returns the new generation id. [[nodiscard]] Void::Result::Result begin_rotation(std::vector new_fingerprints) { if (new_fingerprints.empty()) { return std::unexpected{Void::Result::Error{ Void::Result::ErrorKind::invalid_argument, "TrustStore: a rotation needs at least one fingerprint"}}; } m_generations.push_back( TrustGeneration{m_next_id++, std::move(new_fingerprints)}); return m_generations.back().id; } /// Revoke every generation older than `generation` after the /// overlap drained. The chosen generation must be active. [[nodiscard]] Void::Result::Result complete_rotation(std::uint64_t generation) { const auto found = std::ranges::find_if( m_generations, [generation](const TrustGeneration& candidate) { return candidate.id == generation; }); if (found == m_generations.end()) { return std::unexpected{Void::Result::Error{ Void::Result::ErrorKind::not_found, "TrustStore: unknown trust generation"}}; } std::erase_if(m_generations, [generation](const TrustGeneration& candidate) { return candidate.id < generation; }); return {}; } [[nodiscard]] std::size_t active_generations() const noexcept { return m_generations.size(); } private: std::vector m_generations; std::uint64_t m_next_id{1}; }; // ── Mixed-version rollout ────────────────────────────────────────────── struct VersionPolicy { /// Format version this binary writes. std::uint32_t format_version{2}; /// Oldest node version that can still read existing durable data. std::uint32_t min_reader{1}; }; /// Per-voter version advertisement gate. /// /// Readers may roll out first (every node must still read the durable /// format), and a new command is admitted only when every voter /// advertises support. Unknown voters are rejected rather than assumed /// current. class RolloutGate { public: RolloutGate(std::vector voters, VersionPolicy policy) : m_policy(policy) { for (auto& voter : voters) { m_advertised.emplace(std::move(voter), 0); } } [[nodiscard]] Void::Result::Result report_version(std::string_view node, std::uint32_t version) { const auto found = m_advertised.find(node); if (found == m_advertised.end()) { return std::unexpected{Void::Result::Error{ Void::Result::ErrorKind::not_found, "RolloutGate: unknown voter"}}; } found->second = version; return {}; } [[nodiscard]] Void::Result::Result admit_read(std::string_view node) const { const auto found = m_advertised.find(node); if (found == m_advertised.end()) { return std::unexpected{Void::Result::Error{ Void::Result::ErrorKind::not_found, "RolloutGate: unknown voter"}}; } if (found->second < m_policy.min_reader) { return std::unexpected{Void::Result::Error{ Void::Result::ErrorKind::unsupported, "RolloutGate: node cannot read the durable format"}}; } return {}; } /// A new command requires every voter to advertise it. [[nodiscard]] Void::Result::Result admit_new_command(std::string_view node) const { if (m_advertised.find(node) == m_advertised.end()) { return std::unexpected{Void::Result::Error{ Void::Result::ErrorKind::not_found, "RolloutGate: unknown voter"}}; } for (const auto& [id, version] : m_advertised) { (void)id; if (version < m_policy.format_version) { return std::unexpected{Void::Result::Error{ Void::Result::ErrorKind::unsupported, "RolloutGate: not every voter advertises the new " "command"}}; } } return {}; } [[nodiscard]] bool all_advertised() const noexcept { return std::ranges::all_of( m_advertised, [this](const auto& entry) { return entry.second >= m_policy.format_version; }); } private: VersionPolicy m_policy; std::map> m_advertised; }; } // namespace DistributedHost