NetworkedApplications: durable-job reference composition over Messaging with Scheduler-driven maintenance (P-NETWORKED-07)
This commit is contained in:
1 parent
21b1c8422d
commit
aad91c7d01
4 files changed
+553
-2
No files matched your search
@@ -0,0 +1,191 @@
|
||||
#include "networked_kv/job_service.hpp"
|
||||
|
||||
#include <Void/Messaging/Queue.hpp>
|
||||
#include <Void/Scheduler/Scheduler.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace NetworkedKv;
|
||||
|
||||
JobServiceConfig config()
|
||||
{
|
||||
JobServiceConfig cfg;
|
||||
cfg.max_attempts = 2;
|
||||
cfg.retry_delay = std::chrono::milliseconds{0};
|
||||
cfg.visibility_timeout = std::chrono::milliseconds{500};
|
||||
return cfg;
|
||||
}
|
||||
|
||||
void submit_requires_initialization()
|
||||
{
|
||||
Void::Messaging::InMemoryQueue queue;
|
||||
JobService service{queue};
|
||||
assert(!service.submit("payload").has_value());
|
||||
}
|
||||
|
||||
void successful_job_is_acked_and_tracked()
|
||||
{
|
||||
Void::Messaging::InMemoryQueue queue;
|
||||
JobService service{queue, config()};
|
||||
assert(service.initialize().has_value());
|
||||
|
||||
auto id = service.submit("payload");
|
||||
assert(id.has_value());
|
||||
assert(service.status(*id)->state == JobState::Submitted);
|
||||
|
||||
int calls = 0;
|
||||
auto processed = service.process_one(
|
||||
[&](std::string_view payload, std::uint32_t attempt) {
|
||||
++calls;
|
||||
assert(payload == "payload");
|
||||
assert(attempt == 1);
|
||||
return Void::Result::Result<void>{};
|
||||
});
|
||||
assert(processed.has_value() && processed->has_value());
|
||||
assert((*processed)->state == JobState::Succeeded);
|
||||
assert(calls == 1);
|
||||
|
||||
const auto status = service.status(*id);
|
||||
assert(status.has_value());
|
||||
assert(status->state == JobState::Succeeded);
|
||||
assert(status->attempts == 1);
|
||||
|
||||
// Acked: no redelivery and an empty receive.
|
||||
auto none = service.process_one([](std::string_view,
|
||||
std::uint32_t) {
|
||||
return Void::Result::Result<void>{};
|
||||
});
|
||||
assert(none.has_value() && !none->has_value());
|
||||
|
||||
// Unknown ids are an explicit not-found error.
|
||||
auto missing = service.status("never-submitted");
|
||||
assert(!missing.has_value());
|
||||
assert(missing.error().kind() == Void::Result::ErrorKind::not_found);
|
||||
}
|
||||
|
||||
void failing_job_retries_then_dead_letters()
|
||||
{
|
||||
Void::Messaging::InMemoryQueue queue;
|
||||
JobServiceConfig cfg = config();
|
||||
cfg.max_attempts = 2;
|
||||
JobService service{queue, cfg};
|
||||
assert(service.initialize().has_value());
|
||||
auto id = service.submit("poison");
|
||||
assert(id.has_value());
|
||||
|
||||
const auto failing = [](std::string_view, std::uint32_t) {
|
||||
return Void::Result::Result<void>{std::unexpected{
|
||||
Void::Result::Error{Void::Result::ErrorKind::internal,
|
||||
"handler exploded"}}};
|
||||
};
|
||||
|
||||
auto first = service.process_one(failing);
|
||||
assert(first.has_value() && first->has_value());
|
||||
assert((*first)->attempts == 1);
|
||||
assert((*first)->state == JobState::RetryScheduled);
|
||||
assert((*first)->last_error == "handler exploded");
|
||||
|
||||
// Retry is immediately available (zero delay) and exhausts attempts.
|
||||
auto second = service.process_one(failing);
|
||||
assert(second.has_value() && second->has_value());
|
||||
assert((*second)->attempts == 2);
|
||||
assert((*second)->state == JobState::DeadLettered);
|
||||
|
||||
auto dead = service.dead_letters();
|
||||
assert(dead.has_value());
|
||||
assert(dead->size() == 1);
|
||||
assert((*dead)[0].message.id == *id);
|
||||
assert((*dead)[0].attempts == 2);
|
||||
}
|
||||
|
||||
void throwing_handler_is_a_recorded_failure()
|
||||
{
|
||||
Void::Messaging::InMemoryQueue queue;
|
||||
JobService service{queue, config()};
|
||||
assert(service.initialize().has_value());
|
||||
assert(service.submit("boom").has_value());
|
||||
|
||||
auto processed = service.process_one(
|
||||
[](std::string_view, std::uint32_t) -> Void::Result::Result<void> {
|
||||
throw std::runtime_error{"unexpected"};
|
||||
});
|
||||
assert(processed.has_value() && processed->has_value());
|
||||
assert((*processed)->state == JobState::RetryScheduled);
|
||||
assert((*processed)->last_error.find("unexpected") !=
|
||||
std::string::npos);
|
||||
}
|
||||
|
||||
void scheduler_driven_maintenance_reclaims_expired_deliveries()
|
||||
{
|
||||
Void::Messaging::InMemoryQueue queue;
|
||||
JobServiceConfig cfg = config();
|
||||
cfg.visibility_timeout = std::chrono::milliseconds{50};
|
||||
JobService service{queue, cfg};
|
||||
assert(service.initialize().has_value());
|
||||
assert(service.submit("payload").has_value());
|
||||
|
||||
// Take the delivery in flight and let its visibility expire.
|
||||
auto in_flight =
|
||||
queue.try_receive(cfg.queue_name, cfg.group_name);
|
||||
assert(in_flight.has_value() && in_flight->has_value());
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{80});
|
||||
|
||||
auto scheduler = Void::Scheduler::Scheduler::create();
|
||||
assert(scheduler.has_value());
|
||||
auto job = service.schedule_maintenance(*scheduler,
|
||||
std::chrono::seconds{1});
|
||||
assert(job.has_value());
|
||||
|
||||
// The scheduler is pure: the caller drives it. A tick materializes due
|
||||
// occurrences and dispatch_one executes at most one ready occurrence;
|
||||
// its handler reaps the expired delivery.
|
||||
assert(scheduler->tick().has_value());
|
||||
(void)scheduler->dispatch_one();
|
||||
|
||||
auto processed = service.process_one([](std::string_view,
|
||||
std::uint32_t attempt) {
|
||||
assert(attempt == 2);
|
||||
return Void::Result::Result<void>{};
|
||||
});
|
||||
assert(processed.has_value() && processed->has_value());
|
||||
assert((*processed)->state == JobState::Succeeded);
|
||||
assert((*processed)->attempts == 2);
|
||||
|
||||
assert(scheduler->close_and_join().has_value());
|
||||
}
|
||||
|
||||
void tracked_status_is_bounded()
|
||||
{
|
||||
Void::Messaging::InMemoryQueue queue;
|
||||
JobServiceConfig cfg = config();
|
||||
cfg.max_tracked_jobs = 2;
|
||||
JobService service{queue, cfg};
|
||||
assert(service.initialize().has_value());
|
||||
const auto first = service.submit("a");
|
||||
const auto second = service.submit("b");
|
||||
const auto third = service.submit("c");
|
||||
assert(first.has_value() && second.has_value() && third.has_value());
|
||||
// Oldest record evicted; recent ones retained.
|
||||
assert(!service.status(*first).has_value());
|
||||
assert(service.status(*second).has_value());
|
||||
assert(service.status(*third).has_value());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
submit_requires_initialization();
|
||||
successful_job_is_acked_and_tracked();
|
||||
failing_job_retries_then_dead_letters();
|
||||
throwing_handler_is_a_recorded_failure();
|
||||
scheduler_driven_maintenance_reclaims_expired_deliveries();
|
||||
tracked_status_is_bounded();
|
||||
std::puts("networked-kv job service ok");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user