feat(scheduler): implement P3 S-SCHEDULER-01..04 Scheduler engine, calendar/DST, and SchedulerStorage durable store
This commit is contained in:
1 parent
ec8c0bd40a
commit
aaeab50758
15 files changed
+4694
No files matched your search
@@ -0,0 +1,172 @@
|
||||
#include <Void/Scheduler/Scheduler.hpp>
|
||||
#include <Void/SchedulerStorage/StorageJobStore.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>
|
||||
#include <vector>
|
||||
|
||||
Void::Testing::Test scheduler_storage_durable_recovery_ownership_and_retention()
|
||||
{
|
||||
using namespace Void::Scheduler;
|
||||
using namespace Void::SchedulerStorage;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
Void::FileSystem::TempDir tmp{"void_sched_store_"};
|
||||
|
||||
Void::Time::ManualClock mono{};
|
||||
Void::Time::ManualCivilClock civil{
|
||||
std::chrono::system_clock::time_point{std::chrono::seconds{1700000000}}};
|
||||
|
||||
OccurrenceId first_id{};
|
||||
{
|
||||
auto eng = Void::Storage::Engine::open(
|
||||
Void::Storage::OpenOptions{.directory = tmp.path()});
|
||||
Void::Testing::require(eng.has_value());
|
||||
|
||||
auto store1 = std::make_shared<StorageJobStore>(*eng);
|
||||
auto store2 = std::make_shared<StorageJobStore>(*eng);
|
||||
|
||||
// Exclusive ownership lock rejects second owner!
|
||||
Void::Testing::require(store1->acquire_ownership("owner-A").has_value());
|
||||
auto second = store2->acquire_ownership("owner-B");
|
||||
Void::Testing::expect(!second.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
second.error().code().value,
|
||||
static_cast<std::int32_t>(SchedulerErrorCode::OwnershipLocked));
|
||||
Void::Testing::require(store1->release_ownership().has_value());
|
||||
|
||||
// Manually create a job & claim one occurrence (Pending -> Running),
|
||||
// then close engine WITHOUT completing to simulate a crash!
|
||||
auto sched = Scheduler::create(SchedulerOptions{
|
||||
.owner_id = "owner-A",
|
||||
.store = store1,
|
||||
.monotonic_clock = &mono,
|
||||
.civil_clock = &civil,
|
||||
});
|
||||
Void::Testing::require(sched.has_value());
|
||||
|
||||
JobSpec j{};
|
||||
j.id = "durable-job";
|
||||
j.handler_id = "h1";
|
||||
j.schedule = Schedule::one_shot_utc(to_utc_ms(civil.now()));
|
||||
Void::Testing::require(sched->add_job(j).has_value());
|
||||
|
||||
// Materialize without handler registered (stays Pending), then claim
|
||||
// directly through store to leave it durably Running across crash.
|
||||
auto ran = sched->tick();
|
||||
Void::Testing::require(ran.has_value());
|
||||
Void::Testing::expect_eq(*ran, 0ULL);
|
||||
|
||||
auto occs = store1->load_occurrences();
|
||||
Void::Testing::require(occs.has_value());
|
||||
Void::Testing::require(occs->size() == 1U);
|
||||
first_id = occs->front().id;
|
||||
|
||||
auto claimed = store1->claim_occurrence(first_id, 0);
|
||||
Void::Testing::require(claimed.has_value());
|
||||
Void::Testing::expect(claimed->status == OccurrenceStatus::Running);
|
||||
Void::Testing::expect_eq(claimed->attempt, 1U);
|
||||
|
||||
// Simulate crash: do NOT call complete_occurrence or
|
||||
// release_ownership; close engine cleanly for reopen.
|
||||
Void::Testing::require(eng->close().has_value());
|
||||
}
|
||||
|
||||
// Reopen after crash: recovery must return Running -> Pending preserving
|
||||
// the exact same OccurrenceId and execute at-least-once!
|
||||
{
|
||||
auto eng = Void::Storage::Engine::open(
|
||||
Void::Storage::OpenOptions{.directory = tmp.path()});
|
||||
Void::Testing::require(eng.has_value());
|
||||
|
||||
auto store = std::make_shared<StorageJobStore>(
|
||||
*eng, StorageJobStoreOptions{.recover_stale_owner = true});
|
||||
auto sched = Scheduler::create(SchedulerOptions{
|
||||
.owner_id = "owner-A-restarted",
|
||||
.store = store,
|
||||
.monotonic_clock = &mono,
|
||||
.civil_clock = &civil,
|
||||
});
|
||||
Void::Testing::require(sched.has_value());
|
||||
|
||||
OccurrenceId executed_id{};
|
||||
std::uint32_t executed_attempt = 0;
|
||||
Void::Testing::require(
|
||||
sched
|
||||
->register_handler(
|
||||
"h1",
|
||||
[&](const OccurrenceContext& ctx,
|
||||
std::span<const std::byte>,
|
||||
Void::Async::OperationOptions) -> Result<void> {
|
||||
executed_id = ctx.id;
|
||||
executed_attempt = ctx.attempt;
|
||||
return {};
|
||||
})
|
||||
.has_value());
|
||||
|
||||
auto ran = sched->tick();
|
||||
Void::Testing::require(ran.has_value());
|
||||
Void::Testing::expect_eq(*ran, 1ULL);
|
||||
Void::Testing::expect(executed_id == first_id);
|
||||
// Attempt incremented from 1 (pre-crash) -> 2 (post-crash redelivery)!
|
||||
Void::Testing::expect_eq(executed_attempt, 2U);
|
||||
|
||||
Void::Testing::require(sched->close_and_join().has_value());
|
||||
Void::Testing::require(eng->close().has_value());
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test scheduler_storage_retention_cap_pauses_without_cursor_loss()
|
||||
{
|
||||
using namespace Void::Scheduler;
|
||||
using namespace Void::SchedulerStorage;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
Void::FileSystem::TempDir tmp{"void_sched_ret_"};
|
||||
|
||||
auto eng = Void::Storage::Engine::open(
|
||||
Void::Storage::OpenOptions{.directory = tmp.path()});
|
||||
Void::Testing::require(eng.has_value());
|
||||
|
||||
SchedulerLimits limits{};
|
||||
limits.max_retained_occurrences = 2;
|
||||
|
||||
Void::Time::ManualClock mono{};
|
||||
Void::Time::ManualCivilClock civil{
|
||||
std::chrono::system_clock::time_point{std::chrono::seconds{1700000000}}};
|
||||
|
||||
auto store = std::make_shared<StorageJobStore>(
|
||||
*eng, StorageJobStoreOptions{.limits = limits});
|
||||
auto sched = Scheduler::create(SchedulerOptions{
|
||||
.owner_id = "ret-owner",
|
||||
.store = store,
|
||||
.monotonic_clock = &mono,
|
||||
.civil_clock = &civil,
|
||||
.limits = limits,
|
||||
});
|
||||
Void::Testing::require(sched.has_value());
|
||||
|
||||
JobSpec j{};
|
||||
j.id = "rate-job";
|
||||
j.handler_id = "h";
|
||||
j.schedule = Schedule::fixed_rate(1s, mono.now());
|
||||
j.misfire = MisfirePolicy::CatchUpBounded;
|
||||
Void::Testing::require(sched->add_job(j).has_value());
|
||||
|
||||
// Advance 3s -> 4 due slots exist, but retention cap is 2, so
|
||||
// materialization of all 4 is rejected WITHOUT advancing cursor!
|
||||
Void::Testing::require(mono.advance(3s).has_value());
|
||||
auto r = sched->tick();
|
||||
Void::Testing::require(r.has_value());
|
||||
auto jobs = sched->list_jobs();
|
||||
Void::Testing::require(jobs.has_value());
|
||||
Void::Testing::require(jobs->size() == 1U);
|
||||
Void::Testing::expect(!jobs->front().cursor_initialized);
|
||||
|
||||
Void::Testing::require(sched->close_and_join().has_value());
|
||||
Void::Testing::require(eng->close().has_value());
|
||||
}
|
||||
Reference in new issue
Block a user