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
@@ -40,6 +40,9 @@ target_link_libraries(distributed_operations_test PRIVATE distributed_host_graph
|
||||
add_executable(distributed_range_placement_test tests/range_placement_test.cpp)
|
||||
target_link_libraries(distributed_range_placement_test PRIVATE distributed_host_graph)
|
||||
|
||||
add_executable(distributed_cluster_queue_test tests/cluster_queue_test.cpp)
|
||||
target_link_libraries(distributed_cluster_queue_test PRIVATE distributed_host_graph)
|
||||
|
||||
enable_testing()
|
||||
add_test(NAME distributed_host_graph COMMAND distributed_host_graph_test)
|
||||
add_test(NAME distributed_single_node COMMAND distributed_single_node_test)
|
||||
@@ -48,3 +51,4 @@ add_test(NAME distributed_cluster_fixture COMMAND distributed_cluster_fixture_te
|
||||
add_test(NAME distributed_replicated_queue COMMAND distributed_replicated_queue_test)
|
||||
add_test(NAME distributed_operations COMMAND distributed_operations_test)
|
||||
add_test(NAME distributed_range_placement COMMAND distributed_range_placement_test)
|
||||
add_test(NAME distributed_cluster_queue COMMAND distributed_cluster_queue_test)
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "distributed_host/replicated_kv.hpp"
|
||||
#include "distributed_host/replicated_queue.hpp"
|
||||
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/Net/HttpServer.hpp>
|
||||
@@ -22,6 +23,10 @@ namespace DistributedHost
|
||||
std::size_t max_connections{8};
|
||||
std::chrono::milliseconds shutdown_grace{
|
||||
std::chrono::seconds{2}};
|
||||
/// Queue policy shared by every node handler.
|
||||
QueueConfig queue{};
|
||||
/// Bounded recorded outbox deliveries.
|
||||
std::size_t max_delivered{1024};
|
||||
};
|
||||
|
||||
/// Three-node replicated KV reference over real loopback HTTP listeners.
|
||||
@@ -41,6 +46,17 @@ namespace DistributedHost
|
||||
/// POST /admin/partition?node=X&partition=N
|
||||
/// POST /admin/crash?node=X
|
||||
/// POST /admin/restart?node=X
|
||||
///
|
||||
/// Replicated worker queue over the same group:
|
||||
/// POST /queue/publish?key=K body = payload
|
||||
/// POST /queue/claim?consumer=C
|
||||
/// POST /queue/ack?consumer=C&id=J
|
||||
/// POST /queue/nack?consumer=C&id=J body = reason
|
||||
/// POST /queue/recover?consumer=C
|
||||
/// POST /queue/dispatch deliver pending outbox effects
|
||||
/// GET /queue/status?id=J
|
||||
/// GET /queue/dlq
|
||||
/// GET /queue/delivered
|
||||
class ClusterFixture
|
||||
{
|
||||
public:
|
||||
@@ -83,5 +99,6 @@ namespace DistributedHost
|
||||
ReplicatedKvCluster m_cluster;
|
||||
mutable std::mutex m_mutex;
|
||||
std::vector<std::unique_ptr<PeerServer>> m_peers;
|
||||
std::vector<std::string> m_delivered;
|
||||
};
|
||||
} // namespace DistributedHost
|
||||
@@ -343,7 +343,7 @@ namespace DistributedHost
|
||||
return registered;
|
||||
}
|
||||
|
||||
return add(
|
||||
registered = add(
|
||||
Void::Net::Method::Post, "/admin/restart",
|
||||
[](const std::string&,
|
||||
const Void::Net::ServerRequest& request,
|
||||
@@ -355,6 +355,215 @@ namespace DistributedHost
|
||||
fixture.m_cluster.restart(*node);
|
||||
return text_response(200, "ok");
|
||||
});
|
||||
if (!registered) {
|
||||
return registered;
|
||||
}
|
||||
|
||||
registered = add(
|
||||
Void::Net::Method::Post, "/queue/publish",
|
||||
[](const std::string& self,
|
||||
const Void::Net::ServerRequest& request,
|
||||
ClusterFixture& fixture) {
|
||||
const auto key = query_value(request.query, "key");
|
||||
if (!key || key->empty()) {
|
||||
return text_response(400, "key required");
|
||||
}
|
||||
ReplicatedQueue queue{fixture.m_cluster, self,
|
||||
fixture.m_config.queue};
|
||||
auto id = queue.publish(request.body, std::string{*key});
|
||||
if (!id) {
|
||||
return text_response(status_for(id.error()),
|
||||
id.error().message());
|
||||
}
|
||||
return text_response(200, "job=" + *id);
|
||||
});
|
||||
if (!registered) {
|
||||
return registered;
|
||||
}
|
||||
|
||||
registered = add(
|
||||
Void::Net::Method::Post, "/queue/claim",
|
||||
[](const std::string& self,
|
||||
const Void::Net::ServerRequest& request,
|
||||
ClusterFixture& fixture) {
|
||||
const auto consumer = query_value(request.query, "consumer");
|
||||
if (!consumer) {
|
||||
return text_response(400, "consumer required");
|
||||
}
|
||||
ReplicatedQueue queue{fixture.m_cluster, self,
|
||||
fixture.m_config.queue};
|
||||
auto claimed = queue.claim(*consumer);
|
||||
if (!claimed) {
|
||||
return text_response(status_for(claimed.error()),
|
||||
claimed.error().message());
|
||||
}
|
||||
if (!claimed->has_value()) {
|
||||
return text_response(204, "");
|
||||
}
|
||||
return text_response(
|
||||
200,
|
||||
std::format("job={} payload={} attempts={}",
|
||||
(**claimed).id, (**claimed).payload,
|
||||
(**claimed).attempts));
|
||||
});
|
||||
if (!registered) {
|
||||
return registered;
|
||||
}
|
||||
|
||||
registered = add(
|
||||
Void::Net::Method::Post, "/queue/ack",
|
||||
[](const std::string& self,
|
||||
const Void::Net::ServerRequest& request,
|
||||
ClusterFixture& fixture) {
|
||||
const auto consumer = query_value(request.query, "consumer");
|
||||
const auto id = query_value(request.query, "id");
|
||||
if (!consumer || !id) {
|
||||
return text_response(400, "consumer and id required");
|
||||
}
|
||||
ReplicatedQueue queue{fixture.m_cluster, self,
|
||||
fixture.m_config.queue};
|
||||
auto acknowledged = queue.ack(*consumer, *id);
|
||||
if (!acknowledged) {
|
||||
return text_response(status_for(acknowledged.error()),
|
||||
acknowledged.error().message());
|
||||
}
|
||||
return text_response(200, "done");
|
||||
});
|
||||
if (!registered) {
|
||||
return registered;
|
||||
}
|
||||
|
||||
registered = add(
|
||||
Void::Net::Method::Post, "/queue/nack",
|
||||
[](const std::string& self,
|
||||
const Void::Net::ServerRequest& request,
|
||||
ClusterFixture& fixture) {
|
||||
const auto consumer = query_value(request.query, "consumer");
|
||||
const auto id = query_value(request.query, "id");
|
||||
if (!consumer || !id) {
|
||||
return text_response(400, "consumer and id required");
|
||||
}
|
||||
ReplicatedQueue queue{fixture.m_cluster, self,
|
||||
fixture.m_config.queue};
|
||||
auto rejected = queue.nack(*consumer, *id, request.body);
|
||||
if (!rejected) {
|
||||
return text_response(status_for(rejected.error()),
|
||||
rejected.error().message());
|
||||
}
|
||||
return text_response(
|
||||
200, std::format("state={} attempts={}",
|
||||
job_state_name(rejected->state),
|
||||
rejected->attempts));
|
||||
});
|
||||
if (!registered) {
|
||||
return registered;
|
||||
}
|
||||
|
||||
registered = add(
|
||||
Void::Net::Method::Post, "/queue/recover",
|
||||
[](const std::string& self,
|
||||
const Void::Net::ServerRequest& request,
|
||||
ClusterFixture& fixture) {
|
||||
const auto consumer = query_value(request.query, "consumer");
|
||||
if (!consumer) {
|
||||
return text_response(400, "consumer required");
|
||||
}
|
||||
ReplicatedQueue queue{fixture.m_cluster, self,
|
||||
fixture.m_config.queue};
|
||||
auto recovered = queue.recover_consumer(*consumer);
|
||||
if (!recovered) {
|
||||
return text_response(status_for(recovered.error()),
|
||||
recovered.error().message());
|
||||
}
|
||||
return text_response(200,
|
||||
"recovered=" + std::to_string(*recovered));
|
||||
});
|
||||
if (!registered) {
|
||||
return registered;
|
||||
}
|
||||
|
||||
registered = add(
|
||||
Void::Net::Method::Post, "/queue/dispatch",
|
||||
[](const std::string& self, const Void::Net::ServerRequest&,
|
||||
ClusterFixture& fixture) {
|
||||
ReplicatedQueue queue{fixture.m_cluster, self,
|
||||
fixture.m_config.queue};
|
||||
auto dispatched = queue.dispatch_outbox(
|
||||
[&fixture](const QueueJob& job) {
|
||||
if (fixture.m_delivered.size() <
|
||||
fixture.m_config.max_delivered) {
|
||||
fixture.m_delivered.push_back(job.id);
|
||||
}
|
||||
});
|
||||
if (!dispatched) {
|
||||
return text_response(status_for(dispatched.error()),
|
||||
dispatched.error().message());
|
||||
}
|
||||
return text_response(200,
|
||||
"delivered=" + std::to_string(*dispatched));
|
||||
});
|
||||
if (!registered) {
|
||||
return registered;
|
||||
}
|
||||
|
||||
registered = add(
|
||||
Void::Net::Method::Get, "/queue/status",
|
||||
[](const std::string& self,
|
||||
const Void::Net::ServerRequest& request,
|
||||
ClusterFixture& fixture) {
|
||||
const auto id = query_value(request.query, "id");
|
||||
if (!id) {
|
||||
return text_response(400, "id required");
|
||||
}
|
||||
ReplicatedQueue queue{fixture.m_cluster, self,
|
||||
fixture.m_config.queue};
|
||||
auto job = queue.status(*id);
|
||||
if (!job) {
|
||||
return text_response(status_for(job.error()),
|
||||
job.error().message());
|
||||
}
|
||||
return text_response(
|
||||
200, std::format("state={} attempts={} outbox={} reason={}",
|
||||
job_state_name(job->state), job->attempts,
|
||||
job->outbox_pending ? 1 : 0, job->reason));
|
||||
});
|
||||
if (!registered) {
|
||||
return registered;
|
||||
}
|
||||
|
||||
registered = add(
|
||||
Void::Net::Method::Get, "/queue/dlq",
|
||||
[](const std::string& self, const Void::Net::ServerRequest&,
|
||||
ClusterFixture& fixture) {
|
||||
ReplicatedQueue queue{fixture.m_cluster, self,
|
||||
fixture.m_config.queue};
|
||||
auto dead = queue.dead_letters();
|
||||
if (!dead) {
|
||||
return text_response(status_for(dead.error()),
|
||||
dead.error().message());
|
||||
}
|
||||
std::string body;
|
||||
for (const auto& job : *dead) {
|
||||
body += std::format("id={} attempts={} reason={}\n",
|
||||
job.id, job.attempts, job.reason);
|
||||
}
|
||||
return text_response(200, std::move(body));
|
||||
});
|
||||
if (!registered) {
|
||||
return registered;
|
||||
}
|
||||
|
||||
return add(
|
||||
Void::Net::Method::Get, "/queue/delivered",
|
||||
[](const std::string&, const Void::Net::ServerRequest&,
|
||||
ClusterFixture& fixture) {
|
||||
std::string body;
|
||||
for (const auto& id : fixture.m_delivered) {
|
||||
body += id;
|
||||
body += '\n';
|
||||
}
|
||||
return text_response(200, std::move(body));
|
||||
});
|
||||
}
|
||||
|
||||
Void::Result::Result<void> ClusterFixture::start()
|
||||
|
||||
@@ -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