feat(messaging): implement P3 S-MESSAGING-01..04 Messaging queue, StorageQueue/OutboxInbox, and MessagingRpc adapter
This commit is contained in:
1 parent
aaeab50758
commit
60ee28e4ca
17 files changed
+4641
No files matched your search
@@ -0,0 +1,215 @@
|
||||
#include <Void/MessagingStorage/OutboxInbox.hpp>
|
||||
#include <Void/MessagingStorage/StorageQueue.hpp>
|
||||
#include <Void/Storage/Engine.hpp>
|
||||
#include <Void/System/TempDir.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
Void::Messaging::Message make_msg(std::string id, std::string body)
|
||||
{
|
||||
Void::Messaging::Message m{};
|
||||
m.id = std::move(id);
|
||||
m.payload.reserve(body.size());
|
||||
for (char c : body) {
|
||||
m.payload.push_back(static_cast<std::byte>(c));
|
||||
}
|
||||
return m;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test messaging_storage_restart_fencing_poison_dlq_and_caps()
|
||||
{
|
||||
using namespace Void::Messaging;
|
||||
using namespace Void::MessagingStorage;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
Void::FileSystem::TempDir tmp{"void_msg_store_"};
|
||||
Void::Time::ManualClock mono{};
|
||||
Void::Time::ManualCivilClock civil{
|
||||
std::chrono::system_clock::time_point{std::chrono::seconds{1700000000}}};
|
||||
|
||||
Receipt pre_crash_receipt{};
|
||||
{
|
||||
auto eng = Void::Storage::Engine::open(
|
||||
Void::Storage::OpenOptions{.directory = tmp.path()});
|
||||
Void::Testing::require(eng.has_value());
|
||||
|
||||
auto sq = StorageQueue::open(*eng, StorageQueueOptions{
|
||||
.monotonic_clock = &mono,
|
||||
.civil_clock = &civil,
|
||||
.rng = []() { return 0ULL; },
|
||||
});
|
||||
Void::Testing::require(sq.has_value());
|
||||
|
||||
QueueConfig qcfg{};
|
||||
qcfg.name = "events";
|
||||
qcfg.retry.max_attempts = 2;
|
||||
Void::Testing::require(sq->create_queue(qcfg).has_value());
|
||||
Void::Testing::require(
|
||||
sq->create_group("events", GroupConfig{.name = "g1"}).has_value());
|
||||
|
||||
auto pub = sq->publish("events", make_msg("poison-1", "bad-data"),
|
||||
PublishDurability::Durable);
|
||||
Void::Testing::require(pub.has_value());
|
||||
Void::Testing::expect(pub->durability == PublishDurability::Durable);
|
||||
|
||||
auto d1 = sq->try_receive("events", "g1");
|
||||
Void::Testing::require(d1.has_value() && d1->has_value());
|
||||
Void::Testing::expect_eq((*d1)->attempt, 1U);
|
||||
pre_crash_receipt = (*d1)->receipt;
|
||||
|
||||
// Crash before ack!
|
||||
Void::Testing::require(eng->close().has_value());
|
||||
}
|
||||
|
||||
// Reopen: prior InFlight must be redelivered with bumped generation and
|
||||
// preserved attempt count (1 -> 2)! Old receipt must fail StaleReceipt!
|
||||
{
|
||||
auto eng = Void::Storage::Engine::open(
|
||||
Void::Storage::OpenOptions{.directory = tmp.path()});
|
||||
Void::Testing::require(eng.has_value());
|
||||
|
||||
QueueLimits lim{};
|
||||
lim.max_dlq_messages = 1;
|
||||
lim.max_idempotency_entries = 2;
|
||||
auto sq = StorageQueue::open(*eng, StorageQueueOptions{
|
||||
.limits = lim,
|
||||
.monotonic_clock = &mono,
|
||||
.civil_clock = &civil,
|
||||
.rng = []() { return 0ULL; },
|
||||
});
|
||||
Void::Testing::require(sq.has_value());
|
||||
|
||||
// Old worker receipt from before crash is fenced!
|
||||
auto stale = sq->ack(pre_crash_receipt);
|
||||
Void::Testing::expect(!stale.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
stale.error().code().value,
|
||||
static_cast<std::int32_t>(MessagingErrorCode::StaleReceipt));
|
||||
|
||||
auto d2 = sq->try_receive("events", "g1");
|
||||
Void::Testing::require(d2.has_value() && d2->has_value());
|
||||
Void::Testing::expect((*d2)->receipt.generation >
|
||||
pre_crash_receipt.generation);
|
||||
// Attempt counter survived restart!
|
||||
Void::Testing::expect_eq((*d2)->attempt, 2U);
|
||||
|
||||
// Attempt 2 nack exhausts max_attempts=2 -> atomic DLQ move!
|
||||
Void::Testing::require(sq->nack((*d2)->receipt).has_value());
|
||||
auto dlq = sq->list_dead_letters("events", "g1");
|
||||
Void::Testing::require(dlq.has_value());
|
||||
Void::Testing::require(dlq->size() == 1U);
|
||||
Void::Testing::expect_eq(dlq->front().message.id,
|
||||
std::string{"poison-1"});
|
||||
|
||||
// Second poison message hits DLQ cap (max_dlq_messages = 1) -> keeps
|
||||
// message durable and increments stuck_dlq_count without dropping data!
|
||||
Void::Testing::require(
|
||||
sq->publish("events", make_msg("poison-2", "bad2"),
|
||||
PublishDurability::Durable)
|
||||
.has_value());
|
||||
auto p2a = sq->try_receive("events", "g1");
|
||||
Void::Testing::require(p2a.has_value() && p2a->has_value());
|
||||
auto q_full = sq->quarantine_corrupt((*p2a)->receipt, "unknown schema");
|
||||
Void::Testing::expect(!q_full.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
q_full.error().code().value,
|
||||
static_cast<std::int32_t>(MessagingErrorCode::DlqFull));
|
||||
Void::Testing::expect_eq(sq->stuck_dlq_count("events", "g1"), 1ULL);
|
||||
|
||||
// Idempotency table is now at capacity (2/2 live within 24h) -> third
|
||||
// distinct publish MUST fail with IdempotencyCapacityExceeded rather
|
||||
// than silently evicting live contracts!
|
||||
auto idem_cap = sq->publish("events", make_msg("msg-3", "data"),
|
||||
PublishDurability::Durable);
|
||||
Void::Testing::expect(!idem_cap.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
idem_cap.error().code().value,
|
||||
static_cast<std::int32_t>(
|
||||
MessagingErrorCode::IdempotencyCapacityExceeded));
|
||||
|
||||
// Unhealthy civil clock pauses retention expiry even after 30 days!
|
||||
sq->set_clock_healthy(false);
|
||||
Void::Testing::require(
|
||||
civil.advance(std::chrono::hours{24 * 30}).has_value());
|
||||
auto exp_paused = sq->expire_retention();
|
||||
Void::Testing::require(exp_paused.has_value());
|
||||
Void::Testing::expect_eq(*exp_paused, 0ULL);
|
||||
|
||||
Void::Testing::require(eng->close().has_value());
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test messaging_storage_outbox_inbox_single_batch_atomicity()
|
||||
{
|
||||
using namespace Void::Messaging;
|
||||
using namespace Void::MessagingStorage;
|
||||
|
||||
Void::FileSystem::TempDir tmp{"void_msg_outbox_"};
|
||||
auto eng = Void::Storage::Engine::open(
|
||||
Void::Storage::OpenOptions{.directory = tmp.path()});
|
||||
Void::Testing::require(eng.has_value());
|
||||
|
||||
auto sq = StorageQueue::open(*eng);
|
||||
Void::Testing::require(sq.has_value());
|
||||
Void::Testing::require(
|
||||
sq->create_queue(QueueConfig{.name = "orders"}).has_value());
|
||||
Void::Testing::require(
|
||||
sq->create_group("orders", GroupConfig{.name = "ledger"}).has_value());
|
||||
|
||||
// 1. Atomic business mutation + outbox append in ONE Storage::Batch!
|
||||
{
|
||||
Void::Storage::Batch b{};
|
||||
b.put("biz:order:100", "created");
|
||||
OutboxInbox::append_outbox_in_batch(
|
||||
b, OutboxRecord{
|
||||
.outbox_id = "ob-100",
|
||||
.target_queue = "orders",
|
||||
.message = make_msg("order-100", "amount=42"),
|
||||
});
|
||||
Void::Testing::require(
|
||||
eng->write(std::move(b), Void::Storage::Durability::Sync)
|
||||
.has_value());
|
||||
}
|
||||
|
||||
// 2. Relay outbox into durable queue (idempotent publish).
|
||||
auto relayed = OutboxInbox::relay_outbox(*eng, *sq);
|
||||
Void::Testing::require(relayed.has_value());
|
||||
Void::Testing::expect_eq(*relayed, 1ULL);
|
||||
|
||||
// 3. Consumer receives message and atomically applies local effect + inbox
|
||||
// dedup key in ONE Storage::Batch!
|
||||
auto del = sq->try_receive("orders", "ledger");
|
||||
Void::Testing::require(del.has_value() && del->has_value());
|
||||
|
||||
{
|
||||
Void::Storage::Batch apply_b{};
|
||||
OutboxInbox::apply_inbox_in_batch(apply_b, "ledger-worker",
|
||||
(*del)->message.id);
|
||||
apply_b.put("biz:ledger:100", "credited");
|
||||
Void::Testing::require(
|
||||
eng->write(std::move(apply_b), Void::Storage::Durability::Sync)
|
||||
.has_value());
|
||||
}
|
||||
|
||||
// Simulate duplicate delivery (e.g. crash before ack): second apply of
|
||||
// identical inbox key in local batch fails cleanly with Conflict!
|
||||
{
|
||||
Void::Storage::Batch dup_b{};
|
||||
OutboxInbox::apply_inbox_in_batch(dup_b, "ledger-worker",
|
||||
(*del)->message.id);
|
||||
dup_b.put("biz:ledger:100", "double-credited");
|
||||
auto wr = eng->write(std::move(dup_b), Void::Storage::Durability::Sync);
|
||||
Void::Testing::expect(!wr.has_value());
|
||||
Void::Testing::expect(wr.error().cause.code ==
|
||||
Void::Storage::StorageErrorCode::conflict);
|
||||
}
|
||||
|
||||
Void::Testing::require(sq->ack((*del)->receipt).has_value());
|
||||
Void::Testing::require(eng->close().has_value());
|
||||
}
|
||||
Reference in new issue
Block a user