DistributedApplications: queue workflow over the three-listener fixture with crash/partition histories (P-DISTRIBUTED-05 integration)
This commit is contained in:
1 parent
57ebf3130b
commit
91e7a90213
4 files changed
+492
-1
No files matched your search
@@ -0,0 +1,261 @@
|
||||
#include "distributed_host/cluster_fixture.hpp"
|
||||
|
||||
#include <Void/Net/Http.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
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::Net::Response get(Void::Net::HttpClient& client,
|
||||
const std::string& url)
|
||||
{
|
||||
auto response = client.get(url, {auth()});
|
||||
assert(response.has_value());
|
||||
return *response;
|
||||
}
|
||||
|
||||
void publish_claim_ack_dispatch_over_http()
|
||||
{
|
||||
auto fixture = ClusterFixture::create();
|
||||
assert(fixture.has_value());
|
||||
assert((*fixture)->start().has_value());
|
||||
const std::string a = (*fixture)->endpoint("a");
|
||||
assert(!a.empty());
|
||||
|
||||
Void::Net::HttpClient client;
|
||||
assert(send(client, Void::Net::Method::Post, a + "/peer/elect").status ==
|
||||
200);
|
||||
|
||||
auto published = send(client, Void::Net::Method::Post,
|
||||
a + "/queue/publish?key=q1", "hello");
|
||||
assert(published.status == 200);
|
||||
assert(published.body == "job=q1");
|
||||
// Idempotent by key.
|
||||
auto duplicate = send(client, Void::Net::Method::Post,
|
||||
a + "/queue/publish?key=q1", "ignored");
|
||||
assert(duplicate.status == 200);
|
||||
assert(duplicate.body == "job=q1");
|
||||
|
||||
auto claimed = send(client, Void::Net::Method::Post,
|
||||
a + "/queue/claim?consumer=w1");
|
||||
assert(claimed.status == 200);
|
||||
assert(claimed.body.find("job=q1") != std::string::npos);
|
||||
assert(claimed.body.find("payload=hello") != std::string::npos);
|
||||
assert(claimed.body.find("attempts=1") != std::string::npos);
|
||||
|
||||
auto empty = send(client, Void::Net::Method::Post,
|
||||
a + "/queue/claim?consumer=w1");
|
||||
assert(empty.status == 204);
|
||||
|
||||
assert(send(client, Void::Net::Method::Post,
|
||||
a + "/queue/ack?consumer=w1&id=q1")
|
||||
.status == 200);
|
||||
auto status = get(client, a + "/queue/status?id=q1");
|
||||
assert(status.status == 200);
|
||||
assert(status.body.find("state=done") != std::string::npos);
|
||||
assert(status.body.find("outbox=1") != std::string::npos);
|
||||
|
||||
// Duplicate ack does not create a second outbox entry.
|
||||
assert(send(client, Void::Net::Method::Post,
|
||||
a + "/queue/ack?consumer=w1&id=q1")
|
||||
.status == 200);
|
||||
|
||||
auto dispatched = send(client, Void::Net::Method::Post,
|
||||
a + "/queue/dispatch");
|
||||
assert(dispatched.status == 200);
|
||||
assert(dispatched.body == "delivered=1");
|
||||
auto again =
|
||||
send(client, Void::Net::Method::Post, a + "/queue/dispatch");
|
||||
assert(again.body == "delivered=0");
|
||||
assert(get(client, a + "/queue/delivered").body == "q1\n");
|
||||
|
||||
// Every queue route is authenticated.
|
||||
auto anonymous = client.get(a + "/queue/dlq");
|
||||
assert(anonymous.has_value());
|
||||
assert(anonymous->status == 401);
|
||||
|
||||
(*fixture)->stop();
|
||||
}
|
||||
|
||||
void retry_then_dead_letter_over_http()
|
||||
{
|
||||
ClusterFixtureConfig config;
|
||||
config.queue.max_attempts = 2;
|
||||
auto fixture = ClusterFixture::create(config);
|
||||
assert(fixture.has_value());
|
||||
assert((*fixture)->start().has_value());
|
||||
const std::string a = (*fixture)->endpoint("a");
|
||||
|
||||
Void::Net::HttpClient client;
|
||||
assert(send(client, Void::Net::Method::Post, a + "/peer/elect").status ==
|
||||
200);
|
||||
assert(send(client, Void::Net::Method::Post,
|
||||
a + "/queue/publish?key=r1", "poison")
|
||||
.status == 200);
|
||||
|
||||
assert(send(client, Void::Net::Method::Post,
|
||||
a + "/queue/claim?consumer=w")
|
||||
.status == 200);
|
||||
auto retried = send(client, Void::Net::Method::Post,
|
||||
a + "/queue/nack?consumer=w&id=r1", "boom");
|
||||
assert(retried.status == 200);
|
||||
assert(retried.body == "state=pending attempts=1");
|
||||
|
||||
assert(send(client, Void::Net::Method::Post,
|
||||
a + "/queue/claim?consumer=w")
|
||||
.status == 200);
|
||||
auto dead = send(client, Void::Net::Method::Post,
|
||||
a + "/queue/nack?consumer=w&id=r1", "boom again");
|
||||
assert(dead.status == 200);
|
||||
assert(dead.body == "state=dead attempts=2");
|
||||
|
||||
auto dlq = get(client, a + "/queue/dlq");
|
||||
assert(dlq.status == 200);
|
||||
assert(dlq.body.find("id=r1") != std::string::npos);
|
||||
assert(dlq.body.find("reason=boom again") != std::string::npos);
|
||||
|
||||
assert(send(client, Void::Net::Method::Post,
|
||||
a + "/queue/claim?consumer=w")
|
||||
.status == 204);
|
||||
(*fixture)->stop();
|
||||
}
|
||||
|
||||
void partition_history_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);
|
||||
|
||||
// Minority leader: no durable reply for the queue.
|
||||
assert(send(client, Void::Net::Method::Post,
|
||||
a + "/queue/publish?key=p1", "lost")
|
||||
.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::Post,
|
||||
b + "/queue/publish?key=p1", "kept")
|
||||
.status == 200);
|
||||
auto claimed = send(client, Void::Net::Method::Post,
|
||||
b + "/queue/claim?consumer=w");
|
||||
assert(claimed.status == 200);
|
||||
assert(claimed.body.find("payload=kept") != std::string::npos);
|
||||
|
||||
(*fixture)->stop();
|
||||
}
|
||||
|
||||
void crash_history_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 + "/queue/publish?key=j1", "one")
|
||||
.status == 200);
|
||||
assert(send(client, Void::Net::Method::Post,
|
||||
a + "/queue/claim?consumer=w")
|
||||
.status == 200);
|
||||
assert(send(client, Void::Net::Method::Post,
|
||||
a + "/queue/ack?consumer=w&id=j1")
|
||||
.status == 200);
|
||||
|
||||
// One crash keeps the majority: j2 is durably published.
|
||||
assert(send(client, Void::Net::Method::Post, a + "/admin/crash?node=c")
|
||||
.status == 200);
|
||||
assert(send(client, Void::Net::Method::Post,
|
||||
a + "/queue/publish?key=j2", "two")
|
||||
.status == 200);
|
||||
|
||||
// A second crash loses the majority: j3 has no durable reply.
|
||||
assert(send(client, Void::Net::Method::Post, a + "/admin/crash?node=b")
|
||||
.status == 200);
|
||||
assert(send(client, Void::Net::Method::Post,
|
||||
a + "/queue/publish?key=j3", "three")
|
||||
.status == 503);
|
||||
|
||||
// Restart and re-elect: committed history is intact, the failed
|
||||
// publish left no record.
|
||||
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);
|
||||
|
||||
assert(get(client, b + "/queue/status?id=j1").body.find("state=done") !=
|
||||
std::string::npos);
|
||||
assert(get(client, b + "/queue/status?id=j2")
|
||||
.body.find("state=pending") != std::string::npos);
|
||||
assert(get(client, b + "/queue/status?id=j3").status == 404);
|
||||
|
||||
auto reclaimed = send(client, Void::Net::Method::Post,
|
||||
b + "/queue/claim?consumer=w");
|
||||
assert(reclaimed.status == 200);
|
||||
assert(reclaimed.body.find("job=j2") != std::string::npos);
|
||||
|
||||
(*fixture)->stop();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
publish_claim_ack_dispatch_over_http();
|
||||
retry_then_dead_letter_over_http();
|
||||
partition_history_over_http();
|
||||
crash_history_over_http();
|
||||
std::puts("distributed cluster queue ok");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user