DistributedApplications: deterministic replicated KV core with quorum commits, quorum reads and crash/partition histories (P-DISTRIBUTED-04 foundation)
This commit is contained in:
1 parent
9ba4738415
commit
cfa860f944
4 files changed
+736
No files matched your search
@@ -0,0 +1,192 @@
|
||||
#include "distributed_host/replicated_kv.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace DistributedHost;
|
||||
|
||||
ReplicatedKvCluster cluster()
|
||||
{
|
||||
return ReplicatedKvCluster{{"a", "b", "c"}};
|
||||
}
|
||||
|
||||
void leader_commits_with_a_majority()
|
||||
{
|
||||
auto kv = cluster();
|
||||
assert(kv.elect("a").has_value());
|
||||
const auto reply = kv.propose("a", "key", std::string{"value"});
|
||||
assert(reply.outcome == KvWriteOutcome::Applied);
|
||||
assert(reply.index == 1);
|
||||
assert(kv.committed_index() == 1);
|
||||
|
||||
auto read = kv.quorum_read("a", "key");
|
||||
assert(read.has_value());
|
||||
assert(read->has_value());
|
||||
assert(**read == "value");
|
||||
|
||||
// Followers applied the committed entry too.
|
||||
assert(kv.snapshot("b")->kv.at("key") == "value");
|
||||
assert(kv.snapshot("c")->kv.at("key") == "value");
|
||||
assert(kv.snapshot("b")->applied_index ==
|
||||
kv.snapshot("b")->commit_index);
|
||||
}
|
||||
|
||||
void minority_leader_cannot_commit_or_read()
|
||||
{
|
||||
auto kv = cluster();
|
||||
assert(kv.elect("a").has_value());
|
||||
kv.set_partition("b", 1);
|
||||
kv.set_partition("c", 1);
|
||||
|
||||
const auto reply = kv.propose("a", "key", std::string{"lost"});
|
||||
assert(reply.outcome == KvWriteOutcome::NoQuorum);
|
||||
// Never applied without a majority.
|
||||
assert(kv.snapshot("a")->kv.empty());
|
||||
|
||||
auto read = kv.quorum_read("a", "key");
|
||||
assert(!read.has_value());
|
||||
assert(read.error().kind() == Void::Result::ErrorKind::overloaded);
|
||||
}
|
||||
|
||||
void new_majority_elects_and_preserves_committed_history()
|
||||
{
|
||||
auto kv = cluster();
|
||||
assert(kv.elect("a").has_value());
|
||||
assert(kv.propose("a", "key", std::string{"one"}).outcome ==
|
||||
KvWriteOutcome::Applied);
|
||||
|
||||
// Isolate the old leader; the other two form a majority.
|
||||
kv.set_partition("a", 1);
|
||||
assert(kv.elect("b").has_value());
|
||||
assert(kv.propose("b", "key", std::string{"two"}).outcome ==
|
||||
KvWriteOutcome::Applied);
|
||||
|
||||
// Heal: the next heartbeat from the new leader delivers the committed
|
||||
// entry and term to the stale node, which steps down.
|
||||
kv.set_partition("a", 0);
|
||||
assert(kv.heartbeat("b").has_value());
|
||||
assert(kv.snapshot("a")->role == Role::Follower);
|
||||
assert(kv.snapshot("a")->kv.at("key") == "two");
|
||||
|
||||
const auto stale = kv.propose("a", "key", std::string{"stale"});
|
||||
assert(stale.outcome == KvWriteOutcome::NotLeader);
|
||||
|
||||
auto read = kv.quorum_read("b", "key");
|
||||
assert(read.has_value() && read->has_value());
|
||||
assert(**read == "two");
|
||||
// Committed history was never lost on the old leader either.
|
||||
assert(kv.snapshot("a")->kv.at("key") == "two");
|
||||
}
|
||||
|
||||
void uncommitted_entry_is_overwritten_and_never_applied()
|
||||
{
|
||||
auto kv = cluster();
|
||||
assert(kv.elect("a").has_value());
|
||||
kv.set_partition("b", 1);
|
||||
kv.set_partition("c", 1);
|
||||
|
||||
const auto ghost = kv.propose("a", "key", std::string{"ghost"});
|
||||
assert(ghost.outcome == KvWriteOutcome::NoQuorum);
|
||||
assert(!kv.snapshot("a")->kv.contains("key"));
|
||||
const std::size_t ghost_log = kv.snapshot("a")->log_size;
|
||||
|
||||
kv.set_partition("b", 0);
|
||||
kv.set_partition("c", 0);
|
||||
assert(kv.elect("b").has_value());
|
||||
assert(kv.propose("b", "key", std::string{"real"}).outcome ==
|
||||
KvWriteOutcome::Applied);
|
||||
|
||||
// The stale suffix was truncated and replaced, never applied.
|
||||
const auto old = kv.snapshot("a");
|
||||
assert(old->log_size <= ghost_log);
|
||||
assert(old->kv.at("key") == "real");
|
||||
auto read = kv.quorum_read("b", "key");
|
||||
assert(read.has_value() && read->has_value() && **read == "real");
|
||||
}
|
||||
|
||||
void crash_and_restart_preserve_committed_state()
|
||||
{
|
||||
auto kv = cluster();
|
||||
assert(kv.elect("a").has_value());
|
||||
assert(kv.propose("a", "x", std::string{"1"}).outcome ==
|
||||
KvWriteOutcome::Applied);
|
||||
assert(kv.propose("a", "y", std::string{"2"}).outcome ==
|
||||
KvWriteOutcome::Applied);
|
||||
|
||||
for (const auto& id : kv.voters()) {
|
||||
kv.crash(id);
|
||||
}
|
||||
for (const auto& id : kv.voters()) {
|
||||
kv.restart(id);
|
||||
}
|
||||
assert(kv.elect("c").has_value());
|
||||
auto x = kv.quorum_read("c", "x");
|
||||
auto y = kv.quorum_read("c", "y");
|
||||
assert(x.has_value() && x->has_value() && **x == "1");
|
||||
assert(y.has_value() && y->has_value() && **y == "2");
|
||||
}
|
||||
|
||||
void follower_cannot_serve_consistent_read()
|
||||
{
|
||||
auto kv = cluster();
|
||||
assert(kv.elect("a").has_value());
|
||||
auto read = kv.quorum_read("b", "key");
|
||||
assert(!read.has_value());
|
||||
assert(read.error().kind() == Void::Result::ErrorKind::conflict);
|
||||
}
|
||||
|
||||
void election_without_majority_or_voter_fails()
|
||||
{
|
||||
auto kv = cluster();
|
||||
kv.set_partition("b", 1);
|
||||
kv.set_partition("c", 1);
|
||||
assert(!kv.elect("a").has_value());
|
||||
|
||||
auto unknown = kv.elect("nope");
|
||||
assert(!unknown.has_value());
|
||||
assert(unknown.error().kind() == Void::Result::ErrorKind::not_found);
|
||||
}
|
||||
|
||||
void write_requires_a_leader() {
|
||||
auto kv = cluster();
|
||||
assert(kv.elect("a").has_value());
|
||||
assert(kv.propose("b", "key", std::string{"v"}).outcome ==
|
||||
KvWriteOutcome::NotLeader);
|
||||
assert(kv.propose("missing", "key", std::string{"v"}).outcome ==
|
||||
KvWriteOutcome::NodeDown);
|
||||
}
|
||||
|
||||
void deletes_replicate_as_tombstones()
|
||||
{
|
||||
auto kv = cluster();
|
||||
assert(kv.elect("a").has_value());
|
||||
assert(kv.propose("a", "key", std::string{"value"}).outcome ==
|
||||
KvWriteOutcome::Applied);
|
||||
assert(kv.propose("a", "key", std::nullopt).outcome ==
|
||||
KvWriteOutcome::Applied);
|
||||
|
||||
auto read = kv.quorum_read("a", "key");
|
||||
assert(read.has_value());
|
||||
assert(!read->has_value());
|
||||
assert(!kv.snapshot("b")->kv.contains("key"));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
leader_commits_with_a_majority();
|
||||
minority_leader_cannot_commit_or_read();
|
||||
new_majority_elects_and_preserves_committed_history();
|
||||
uncommitted_entry_is_overwritten_and_never_applied();
|
||||
crash_and_restart_preserve_committed_state();
|
||||
follower_cannot_serve_consistent_read();
|
||||
election_without_majority_or_voter_fails();
|
||||
write_requires_a_leader();
|
||||
deletes_replicate_as_tombstones();
|
||||
std::puts("distributed replicated kv ok");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user