DistributedApplications: backup/restore with identity retention, overlapping trust rotation and mixed-version rollout gates (P-DISTRIBUTED-06)

This commit is contained in:
faumaray committed 2026-12-04 10:38:10 +03:00
1 parent b386140899
commit cdc55a594e
3 files changed
+407

No files matched your search

@@ -0,0 +1,142 @@
#include "distributed_host/operations.hpp"
#include <cassert>
#include <cstdio>
#include <string>
namespace
{
using namespace DistributedHost;
ClusterIdentity identity()
{
return ClusterIdentity{"prod-cluster", {"a", "b", "c"}};
}
void backup_and_restore_retains_identity_and_state()
{
ReplicatedKvCluster source{{"a", "b", "c"}};
assert(source.elect("a").has_value());
assert(source.propose("a", "x", std::string{"1"}).outcome ==
KvWriteOutcome::Applied);
assert(source.propose("a", "y", std::string{"2"}).outcome ==
KvWriteOutcome::Applied);
assert(source.propose("a", "z", std::string{"3"}).outcome ==
KvWriteOutcome::Applied);
assert(source.propose("a", "y", std::nullopt).outcome ==
KvWriteOutcome::Applied);
auto backup = capture_backup(source, "a", identity());
assert(backup.has_value());
assert(backup->identity == identity());
assert(backup->commit_index == source.committed_index());
assert(backup->state.size() == 2);
assert(!backup->state.contains("y"));
// Restore rehearsal into a separately started cluster.
ReplicatedKvCluster target{{"a", "b", "c"}};
assert(target.elect("a").has_value());
auto restored = restore_backup(target, "a", *backup, identity());
assert(restored.has_value());
assert(*restored == 2);
auto x = target.quorum_read("a", "x");
auto z = target.quorum_read("a", "z");
auto y = target.quorum_read("a", "y");
assert(x.has_value() && x->has_value() && **x == "1");
assert(z.has_value() && z->has_value() && **z == "3");
assert(y.has_value() && !y->has_value());
// A different cluster identity is refused, so histories cannot merge.
ClusterIdentity staging{"staging-cluster", {"a", "b", "c"}};
assert(!restore_backup(target, "a", *backup, staging).has_value());
ClusterIdentity fewer_voters{"prod-cluster", {"a", "b"}};
assert(!restore_backup(target, "a", *backup, fewer_voters).has_value());
}
void backup_without_quorum_fails()
{
ReplicatedKvCluster source{{"a", "b", "c"}};
assert(source.elect("a").has_value());
source.set_partition("b", 1);
source.set_partition("c", 1);
auto backup = capture_backup(source, "a", identity());
assert(!backup.has_value());
}
void trust_rotation_overlaps_then_revokes()
{
TrustStore trust{{"old-fp"}};
assert(trust.trusted("old-fp"));
assert(!trust.trusted("new-fp"));
assert(!trust.trusted(""));
// Empty rotation is rejected: verification is never disabled.
assert(!trust.begin_rotation({}).has_value());
auto generation = trust.begin_rotation({"new-fp"});
assert(generation.has_value());
// Overlap: both generations remain trusted while connections drain.
assert(trust.trusted("old-fp"));
assert(trust.trusted("new-fp"));
assert(trust.active_generations() == 2);
assert(trust.complete_rotation(*generation).has_value());
assert(!trust.trusted("old-fp"));
assert(trust.trusted("new-fp"));
assert(trust.active_generations() == 1);
assert(!trust.complete_rotation(99).has_value());
// An empty store trusts nothing.
TrustStore empty;
assert(!empty.trusted("anything"));
}
void mixed_version_rollout_readers_first()
{
RolloutGate gate{{"a", "b", "c"},
VersionPolicy{.format_version = 2, .min_reader = 1}};
assert(!gate.all_advertised());
assert(gate.report_version("a", 2).has_value());
assert(gate.report_version("b", 1).has_value());
assert(gate.report_version("c", 1).has_value());
// Old readers can still read the durable format.
assert(gate.admit_read("b").has_value());
// But new commands wait for every voter.
assert(!gate.admit_new_command("a").has_value());
assert(!gate.all_advertised());
assert(gate.report_version("b", 2).has_value());
assert(gate.report_version("c", 2).has_value());
assert(gate.admit_new_command("a").has_value());
assert(gate.all_advertised());
// Unknown voters are never assumed current.
assert(!gate.report_version("d", 2).has_value());
assert(!gate.admit_read("d").has_value());
}
void reader_floor_is_enforced()
{
RolloutGate gate{{"a"}, VersionPolicy{.format_version = 2,
.min_reader = 2}};
assert(gate.report_version("a", 1).has_value());
assert(!gate.admit_read("a").has_value());
assert(gate.report_version("a", 2).has_value());
assert(gate.admit_read("a").has_value());
}
} // namespace
int main()
{
backup_and_restore_retains_identity_and_state();
backup_without_quorum_fails();
trust_rotation_overlaps_then_revokes();
mixed_version_rollout_readers_first();
reader_floor_is_enforced();
std::puts("distributed operations ok");
return 0;
}