#include "distributed_host/cluster_fixture.hpp" #include #include #include #include #include namespace { using namespace DistributedHost; Void::Net::Header auth() { return Void::Net::Header{"authorization", "Bearer peer-secret"}; } Void::Net::Response send(Void::Net::HttpClient& client, Void::Net::Method method, const std::string& url, std::string body = {}) { Void::Net::Request request; request.method = method; request.url = url; request.body = std::move(body); request.headers.push_back(auth()); auto response = client.send(request); assert(response.has_value()); return *response; } void three_real_listeners_serve_the_replicated_kv() { auto fixture = ClusterFixture::create(); assert(fixture.has_value()); // Construction opens nothing. assert((*fixture)->endpoint("a").empty()); assert((*fixture)->start().has_value()); const std::string a = (*fixture)->endpoint("a"); const std::string b = (*fixture)->endpoint("b"); const std::string c = (*fixture)->endpoint("c"); assert(!a.empty() && !b.empty() && !c.empty()); assert(a != b && b != c && a != c); Void::Net::HttpClient client; // Every peer route is authenticated. auto anonymous = client.get(a + "/peer/health"); assert(anonymous.has_value() && anonymous->status == 401); assert(send(client, Void::Net::Method::Post, a + "/peer/elect").body == "elected"); auto proposed = send(client, Void::Net::Method::Put, a + "/peer/propose?key=k", "value"); assert(proposed.status == 200); assert(proposed.body.starts_with("applied")); auto read = client.get(a + "/peer/read?key=k", {auth()}); assert(read.has_value()); assert(read->status == 200); assert(read->body == "value"); // The follower applied the committed entry but cannot serve a // consistent read. auto follower_read = client.get(b + "/peer/read?key=k", {auth()}); assert(follower_read.has_value()); assert(follower_read->status == 409); auto follower_write = send(client, Void::Net::Method::Put, b + "/peer/propose?key=k", "x"); assert(follower_write.status == 409); (*fixture)->stop(); assert((*fixture)->endpoint("a").empty()); } void minority_leader_cannot_commit_or_read_over_http() { auto fixture = ClusterFixture::create(); assert(fixture.has_value()); assert((*fixture)->start().has_value()); const std::string a = (*fixture)->endpoint("a"); const std::string b = (*fixture)->endpoint("b"); const std::string c = (*fixture)->endpoint("c"); Void::Net::HttpClient client; assert(send(client, Void::Net::Method::Post, a + "/peer/elect").status == 200); assert(send(client, Void::Net::Method::Post, a + "/admin/partition?node=b&partition=1") .status == 200); assert(send(client, Void::Net::Method::Post, a + "/admin/partition?node=c&partition=1") .status == 200); auto proposed = send(client, Void::Net::Method::Put, a + "/peer/propose?key=k", "lost"); assert(proposed.status == 503); assert(proposed.body.starts_with("no-quorum")); auto read = client.get(a + "/peer/read?key=k", {auth()}); assert(read.has_value()); assert(read->status == 503); // Heal and let the majority take over. assert(send(client, Void::Net::Method::Post, b + "/admin/partition?node=b&partition=0") .status == 200); assert(send(client, Void::Net::Method::Post, b + "/admin/partition?node=c&partition=0") .status == 200); assert(send(client, Void::Net::Method::Post, b + "/peer/elect").status == 200); assert(send(client, Void::Net::Method::Put, b + "/peer/propose?key=k", "kept") .status == 200); auto read_after = client.get(b + "/peer/read?key=k", {auth()}); assert(read_after.has_value()); assert(read_after->status == 200); assert(read_after->body == "kept"); (*fixture)->stop(); } void crash_history_keeps_committed_replies() { auto fixture = ClusterFixture::create(); assert(fixture.has_value()); assert((*fixture)->start().has_value()); const std::string a = (*fixture)->endpoint("a"); const std::string b = (*fixture)->endpoint("b"); const std::string c = (*fixture)->endpoint("c"); Void::Net::HttpClient client; assert(send(client, Void::Net::Method::Post, a + "/peer/elect").status == 200); assert(send(client, Void::Net::Method::Put, a + "/peer/propose?key=committed", "1") .status == 200); // One voter crashes: the majority still commits. assert(send(client, Void::Net::Method::Post, a + "/admin/crash?node=c") .status == 200); assert(send(client, Void::Net::Method::Put, a + "/peer/propose?key=second", "2") .status == 200); // A second crash loses the majority: no durable reply. assert(send(client, Void::Net::Method::Post, a + "/admin/crash?node=b") .status == 200); assert(send(client, Void::Net::Method::Put, a + "/peer/propose?key=third", "3") .status == 503); // Restart and re-elect: committed history is intact, the failed // proposal was never applied. assert(send(client, Void::Net::Method::Post, b + "/admin/restart?node=b") .status == 200); assert(send(client, Void::Net::Method::Post, c + "/admin/restart?node=c") .status == 200); assert(send(client, Void::Net::Method::Post, b + "/peer/elect").status == 200); auto first = client.get(b + "/peer/read?key=committed", {auth()}); auto second = client.get(b + "/peer/read?key=second", {auth()}); auto third = client.get(b + "/peer/read?key=third", {auth()}); assert(first.has_value() && first->status == 200 && first->body == "1"); assert(second.has_value() && second->status == 200 && second->body == "2"); assert(third.has_value() && third->status == 404); (*fixture)->stop(); } } // namespace int main() { three_real_listeners_serve_the_replicated_kv(); minority_leader_cannot_commit_or_read_over_http(); crash_history_keeps_committed_replies(); std::puts("distributed cluster fixture ok"); return 0; }