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,135 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Scheduler/Types.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Scheduler
|
||||
{
|
||||
struct CivilMinute {
|
||||
std::int32_t year{1970};
|
||||
std::uint32_t month{1}; // 1..12
|
||||
std::uint32_t day{1}; // 1..31
|
||||
std::uint32_t hour{0}; // 0..23
|
||||
std::uint32_t minute{0}; // 0..59
|
||||
std::uint32_t weekday{4}; // 0=Sunday .. 6=Saturday
|
||||
};
|
||||
|
||||
/// Convert civil UTC components to minutes since 1970-01-01T00:00Z.
|
||||
[[nodiscard]] std::int64_t
|
||||
civil_to_epoch_minute(std::int32_t year, std::uint32_t month,
|
||||
std::uint32_t day, std::uint32_t hour,
|
||||
std::uint32_t minute) noexcept;
|
||||
|
||||
/// Convert minutes since 1970-01-01T00:00Z to civil components + weekday.
|
||||
[[nodiscard]] CivilMinute
|
||||
epoch_minute_to_civil(std::int64_t epoch_minute) noexcept;
|
||||
|
||||
/// Explicit DST transition rule for deterministic timezone resolution
|
||||
/// without relying on ambient OS tzdata behavior.
|
||||
struct DstTransitionRule {
|
||||
std::string name{};
|
||||
std::int32_t standard_offset_minutes{0};
|
||||
std::int32_t dst_offset_minutes{0};
|
||||
/// Spring-forward gap window in local wall minutes `[start, end)`.
|
||||
std::optional<std::int64_t> gap_start_local_min{};
|
||||
std::optional<std::int64_t> gap_end_local_min{};
|
||||
/// Fall-back fold window in local wall minutes `[start, end)`.
|
||||
/// Minutes in `[start, end)` happen twice: fold 0 at `dst_offset`,
|
||||
/// fold 1 at `standard_offset`.
|
||||
std::optional<std::int64_t> fold_start_local_min{};
|
||||
std::optional<std::int64_t> fold_end_local_min{};
|
||||
};
|
||||
|
||||
struct ResolvedSlot {
|
||||
std::int64_t utc_ms{0};
|
||||
std::uint8_t fold_index{0};
|
||||
};
|
||||
|
||||
/// Explicit timezone registry. Recognizes `"UTC"`, `"Z"`, fixed numeric
|
||||
/// offsets (`+HH:MM`, `-HH:MM`, `UTC+HH:MM`, `UTC-HH:MM`), and explicitly
|
||||
/// registered `DstTransitionRule` zones. Unknown zones fail with
|
||||
/// `UnsupportedTimezone` (never silent UTC fallback).
|
||||
class TimezoneRegistry final
|
||||
{
|
||||
public:
|
||||
|
||||
TimezoneRegistry() = default;
|
||||
|
||||
void register_dst_zone(DstTransitionRule rule);
|
||||
|
||||
[[nodiscard]] Result<void>
|
||||
validate_timezone(std::string_view tz) const;
|
||||
|
||||
[[nodiscard]] Result<std::vector<ResolvedSlot>>
|
||||
resolve_local_minute(std::string_view tz,
|
||||
std::int64_t local_epoch_minute,
|
||||
DstGapPolicy gap_policy,
|
||||
DstFoldPolicy fold_policy) const;
|
||||
|
||||
/// Approximate local epoch minute range covering `[utc_min_lo,
|
||||
/// utc_min_hi]` for `tz`.
|
||||
[[nodiscard]] Result<std::pair<std::int64_t, std::int64_t>>
|
||||
local_minute_bounds(std::string_view tz,
|
||||
std::int64_t utc_min_lo,
|
||||
std::int64_t utc_min_hi) const;
|
||||
|
||||
private:
|
||||
|
||||
std::map<std::string, DstTransitionRule, std::less<>> m_dst_zones{};
|
||||
};
|
||||
|
||||
struct MaterializedSlots {
|
||||
std::vector<ResolvedSlot> slots{};
|
||||
std::int64_t next_cursor_utc_ms{0};
|
||||
bool more_pending{false};
|
||||
};
|
||||
|
||||
/// 5-field cron-like calendar expression (`minute hour day month weekday`)
|
||||
/// supporting `*`, lists, ranges, and steps. Both day-of-month and weekday
|
||||
/// constraints apply.
|
||||
class CalendarExpression final
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]] static Result<CalendarExpression>
|
||||
parse(std::string_view expression);
|
||||
|
||||
[[nodiscard]] bool matches(const CivilMinute& cm) const noexcept;
|
||||
|
||||
/// Next matching local epoch minute strictly >= `from_local_min`,
|
||||
/// capped by `max_local_min`.
|
||||
[[nodiscard]] std::optional<std::int64_t>
|
||||
next_matching_minute(std::int64_t from_local_min,
|
||||
std::int64_t max_local_min) const noexcept;
|
||||
|
||||
/// Latest matching local epoch minute <= `from_local_min` and
|
||||
/// >= `min_local_min`. Allows O(1) coalescing across millions of
|
||||
/// missed minutes.
|
||||
[[nodiscard]] std::optional<std::int64_t>
|
||||
prev_matching_minute(std::int64_t from_local_min,
|
||||
std::int64_t min_local_min) const noexcept;
|
||||
|
||||
[[nodiscard]] Result<MaterializedSlots>
|
||||
materialize_range(const CalendarSpec& spec,
|
||||
const TimezoneRegistry& tz_registry,
|
||||
std::int64_t after_utc_ms,
|
||||
std::int64_t up_to_utc_ms,
|
||||
MisfirePolicy misfire,
|
||||
std::size_t max_catch_up) const;
|
||||
|
||||
private:
|
||||
|
||||
std::uint64_t m_minutes{0}; // bits 0..59
|
||||
std::uint32_t m_hours{0}; // bits 0..23
|
||||
std::uint32_t m_days{0}; // bits 1..31
|
||||
std::uint32_t m_months{0}; // bits 1..12
|
||||
std::uint32_t m_weekdays{0}; // bits 0..6 (0 = Sunday)
|
||||
};
|
||||
|
||||
} // namespace Void::Scheduler
|
||||
@@ -0,0 +1,100 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Scheduler/Calendar.hpp>
|
||||
#include <Void/Scheduler/Store.hpp>
|
||||
#include <Void/Scheduler/Types.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Scheduler
|
||||
{
|
||||
struct SchedulerOptions {
|
||||
std::string owner_id{"scheduler-default"};
|
||||
std::shared_ptr<JobStore> store{};
|
||||
const Void::Time::MonotonicClock* monotonic_clock{
|
||||
&Void::Time::SystemMonotonicClock::instance()};
|
||||
const Void::Time::CivilClock* civil_clock{
|
||||
&Void::Time::SystemCivilClock::instance()};
|
||||
TimezoneRegistry timezones{};
|
||||
SchedulerLimits limits{};
|
||||
RandomU64Fn rng{};
|
||||
ObservationHook observer{};
|
||||
};
|
||||
|
||||
/// Pure, thread-free timer/calendar job scheduler with pluggable durable
|
||||
/// persistence (`InMemoryJobStore` or `SchedulerStorage::StorageJobStore`).
|
||||
class Scheduler final
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]] static Result<Scheduler>
|
||||
create(SchedulerOptions options = {});
|
||||
|
||||
Scheduler(Scheduler&&) noexcept;
|
||||
Scheduler& operator=(Scheduler&&) noexcept;
|
||||
Scheduler(const Scheduler&) = delete;
|
||||
Scheduler& operator=(const Scheduler&) = delete;
|
||||
~Scheduler() noexcept;
|
||||
|
||||
[[nodiscard]] Result<void> register_handler(std::string handler_id,
|
||||
HandlerFn handler);
|
||||
|
||||
[[nodiscard]] Result<std::uint64_t>
|
||||
add_job(JobSpec spec,
|
||||
Void::Async::OperationOptions options = {});
|
||||
|
||||
/// Increment revision and atomically record an old-revision
|
||||
/// invalidation watermark. Failed updates leave prior policy intact.
|
||||
[[nodiscard]] Result<std::uint64_t>
|
||||
update_job(JobSpec spec,
|
||||
Void::Async::OperationOptions options = {});
|
||||
|
||||
/// Write a job tombstone, stop new admission, and signal stop to
|
||||
/// active handlers for the job.
|
||||
[[nodiscard]] Result<void>
|
||||
remove_job(std::string_view job_id,
|
||||
Void::Async::OperationOptions options = {});
|
||||
|
||||
/// Run one bounded scheduling pass: materialize due occurrences
|
||||
/// under misfire/catch-up/retention limits and dispatch ready
|
||||
/// occurrences outside scheduler/store locks. Returns count of
|
||||
/// occurrences executed in this pass.
|
||||
[[nodiscard]] Result<std::size_t>
|
||||
tick(Void::Async::OperationOptions options = {});
|
||||
|
||||
/// Execute at most one ready occurrence from the monotonic min-heap
|
||||
/// outside scheduler/store locks.
|
||||
[[nodiscard]] Result<bool>
|
||||
dispatch_one(Void::Async::OperationOptions options = {});
|
||||
|
||||
/// Stop materialization/dispatch, signal stop to active handlers,
|
||||
/// and wait up to `options.deadline` for live handlers to exit.
|
||||
/// Never erases running records or abandons active frames.
|
||||
[[nodiscard]] Result<ShutdownReport>
|
||||
close_and_join(Void::Async::OperationOptions options = {});
|
||||
|
||||
[[nodiscard]] Result<std::vector<JobRecord>> list_jobs() const;
|
||||
[[nodiscard]] Result<std::vector<OccurrenceRecord>>
|
||||
list_occurrences() const;
|
||||
[[nodiscard]] Result<void>
|
||||
acknowledge_dead(const OccurrenceId& id);
|
||||
[[nodiscard]] Result<std::size_t>
|
||||
prune_completed(std::size_t max_count = 256);
|
||||
[[nodiscard]] Result<std::size_t>
|
||||
reclaim_invalidated(std::size_t max_count = 256);
|
||||
[[nodiscard]] Result<StoreStats> stats() const;
|
||||
|
||||
private:
|
||||
|
||||
struct State;
|
||||
explicit Scheduler(std::shared_ptr<State> state) noexcept;
|
||||
std::shared_ptr<State> m_state;
|
||||
};
|
||||
|
||||
} // namespace Void::Scheduler
|
||||
@@ -0,0 +1,105 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Scheduler/Types.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Scheduler
|
||||
{
|
||||
/// Persistence port for Scheduler state transitions.
|
||||
/// Implementations guarantee atomic conditional commits (in-memory mutex
|
||||
/// or `Void::Storage::Engine` conditional batches).
|
||||
class JobStore
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~JobStore() = default;
|
||||
|
||||
[[nodiscard]] virtual Result<void>
|
||||
acquire_ownership(std::string_view owner_id) = 0;
|
||||
|
||||
[[nodiscard]] virtual Result<void> release_ownership() = 0;
|
||||
|
||||
[[nodiscard]] virtual Result<void> put_job(const JobRecord& job) = 0;
|
||||
|
||||
[[nodiscard]] virtual Result<void>
|
||||
update_job(const JobRecord& updated,
|
||||
std::uint64_t expected_revision) = 0;
|
||||
|
||||
[[nodiscard]] virtual Result<void>
|
||||
tombstone_job(std::string_view job_id) = 0;
|
||||
|
||||
[[nodiscard]] virtual Result<std::vector<JobRecord>>
|
||||
load_jobs() const = 0;
|
||||
|
||||
[[nodiscard]] virtual Result<std::vector<OccurrenceRecord>>
|
||||
load_occurrences() const = 0;
|
||||
|
||||
/// Atomically insert new occurrences (enforcing unique
|
||||
/// `(job, revision, intended_utc_ms, fold_index)` dedup and
|
||||
/// retention cap) AND advance the job's cursor/sequence.
|
||||
[[nodiscard]] virtual Result<std::size_t> materialize_occurrences(
|
||||
std::string_view job_id, std::uint64_t expected_revision,
|
||||
std::int64_t new_cursor_utc_ms,
|
||||
std::int64_t new_cursor_monotonic_ns, bool cursor_initialized,
|
||||
bool one_shot_materialized, std::uint64_t next_sequence,
|
||||
const std::vector<OccurrenceRecord>& records) = 0;
|
||||
|
||||
/// Atomically verify job is not tombstoned, check old-revision
|
||||
/// invalidation watermark, and transition `Pending -> Running`
|
||||
/// bumping `attempt`.
|
||||
[[nodiscard]] virtual Result<OccurrenceRecord>
|
||||
claim_occurrence(const OccurrenceId& id,
|
||||
std::uint32_t expected_attempt) = 0;
|
||||
|
||||
/// Atomically transition `Running -> Completed` and optionally
|
||||
/// advance a FixedDelay job's monotonic cursor.
|
||||
[[nodiscard]] virtual Result<void> complete_occurrence(
|
||||
const OccurrenceId& id, std::uint32_t attempt,
|
||||
std::int64_t completed_utc_ms,
|
||||
std::optional<std::int64_t> fixed_delay_next_monotonic_ns =
|
||||
std::nullopt) = 0;
|
||||
|
||||
/// Atomically transition `Running -> Pending` (retry) or
|
||||
/// `Running -> Dead` (exhausted).
|
||||
[[nodiscard]] virtual Result<void> fail_occurrence(
|
||||
const OccurrenceId& id, std::uint32_t attempt,
|
||||
std::int64_t next_due_monotonic_ns,
|
||||
std::int64_t next_due_utc_ms, bool mark_dead,
|
||||
std::string diagnostic_category) = 0;
|
||||
|
||||
/// Return a `Running` occurrence to `Pending` during host shutdown
|
||||
/// without consuming a retry attempt.
|
||||
[[nodiscard]] virtual Result<void>
|
||||
requeue_on_shutdown(const OccurrenceId& id,
|
||||
std::uint32_t attempt) = 0;
|
||||
|
||||
/// Startup recovery: return every `Running` record to `Pending`
|
||||
/// preserving its exact `OccurrenceId` and attempt counter.
|
||||
[[nodiscard]] virtual Result<std::size_t>
|
||||
recover_running_occurrences() = 0;
|
||||
|
||||
[[nodiscard]] virtual Result<void>
|
||||
acknowledge_dead(const OccurrenceId& id) = 0;
|
||||
|
||||
[[nodiscard]] virtual Result<std::size_t>
|
||||
prune_completed(std::int64_t before_utc_ms,
|
||||
std::size_t max_count) = 0;
|
||||
|
||||
[[nodiscard]] virtual Result<std::size_t>
|
||||
reclaim_invalidated(std::size_t max_count) = 0;
|
||||
|
||||
[[nodiscard]] virtual Result<StoreStats> stats() const = 0;
|
||||
};
|
||||
|
||||
/// Default in-memory `JobStore` implementation requiring zero `Storage`
|
||||
/// dependency.
|
||||
[[nodiscard]] std::shared_ptr<JobStore>
|
||||
make_in_memory_job_store(SchedulerLimits limits = {});
|
||||
|
||||
} // namespace Void::Scheduler
|
||||
@@ -0,0 +1,339 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Async/Detail/Types.hpp>
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <compare>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Scheduler
|
||||
{
|
||||
using Void::Result::Error;
|
||||
template <typename T> using Result = Void::Result::Result<T, Error>;
|
||||
|
||||
enum class SchedulerErrorCode : std::int32_t {
|
||||
None = 0,
|
||||
InvalidArgument = 1,
|
||||
InvalidRule = 2,
|
||||
UnsupportedTimezone = 3,
|
||||
NotFound = 4,
|
||||
Conflict = 5,
|
||||
Overloaded = 6,
|
||||
RetentionFull = 7,
|
||||
PersistenceError = 8,
|
||||
UnknownOutcome = 9,
|
||||
HandlerFailed = 10,
|
||||
Cancelled = 11,
|
||||
Closed = 12,
|
||||
SelfJoin = 13,
|
||||
OwnershipLocked = 14,
|
||||
CorruptRecord = 15,
|
||||
};
|
||||
|
||||
[[nodiscard]] inline Error make_scheduler_error(SchedulerErrorCode code,
|
||||
std::string message)
|
||||
{
|
||||
using Void::Result::ErrorCode;
|
||||
using Void::Result::ErrorKind;
|
||||
|
||||
ErrorKind kind = ErrorKind::internal;
|
||||
switch (code) {
|
||||
case SchedulerErrorCode::None:
|
||||
case SchedulerErrorCode::HandlerFailed:
|
||||
kind = ErrorKind::internal;
|
||||
break;
|
||||
case SchedulerErrorCode::InvalidArgument:
|
||||
case SchedulerErrorCode::InvalidRule:
|
||||
kind = ErrorKind::invalid_argument;
|
||||
break;
|
||||
case SchedulerErrorCode::UnsupportedTimezone:
|
||||
kind = ErrorKind::unsupported;
|
||||
break;
|
||||
case SchedulerErrorCode::NotFound:
|
||||
kind = ErrorKind::not_found;
|
||||
break;
|
||||
case SchedulerErrorCode::Conflict:
|
||||
case SchedulerErrorCode::UnknownOutcome:
|
||||
case SchedulerErrorCode::SelfJoin:
|
||||
case SchedulerErrorCode::OwnershipLocked:
|
||||
kind = ErrorKind::conflict;
|
||||
break;
|
||||
case SchedulerErrorCode::Overloaded:
|
||||
kind = ErrorKind::overloaded;
|
||||
break;
|
||||
case SchedulerErrorCode::RetentionFull:
|
||||
kind = ErrorKind::exhausted;
|
||||
break;
|
||||
case SchedulerErrorCode::PersistenceError:
|
||||
kind = ErrorKind::io;
|
||||
break;
|
||||
case SchedulerErrorCode::CorruptRecord:
|
||||
kind = ErrorKind::corrupt;
|
||||
break;
|
||||
case SchedulerErrorCode::Cancelled:
|
||||
case SchedulerErrorCode::Closed:
|
||||
kind = ErrorKind::cancelled;
|
||||
break;
|
||||
}
|
||||
return Error{kind, std::move(message),
|
||||
ErrorCode{Void::Result::kSchedulerErrorDomain,
|
||||
static_cast<std::int32_t>(code)}};
|
||||
}
|
||||
|
||||
struct OccurrenceId {
|
||||
std::string job_id{};
|
||||
std::uint64_t revision{0};
|
||||
std::uint64_t sequence{0};
|
||||
|
||||
[[nodiscard]] auto
|
||||
operator<=>(const OccurrenceId&) const noexcept = default;
|
||||
|
||||
[[nodiscard]] std::string to_idempotency_key() const
|
||||
{
|
||||
return job_id + "#" + std::to_string(revision) + "#" +
|
||||
std::to_string(sequence);
|
||||
}
|
||||
};
|
||||
|
||||
enum class MisfirePolicy : std::uint8_t {
|
||||
Skip = 0,
|
||||
CoalesceLatest = 1,
|
||||
CatchUpBounded = 2,
|
||||
};
|
||||
|
||||
enum class OverlapPolicy : std::uint8_t {
|
||||
Forbid = 0,
|
||||
QueueOne = 1,
|
||||
AllowBounded = 2,
|
||||
};
|
||||
|
||||
enum class DstGapPolicy : std::uint8_t {
|
||||
Skip = 0,
|
||||
NextValid = 1,
|
||||
};
|
||||
|
||||
enum class DstFoldPolicy : std::uint8_t {
|
||||
First = 0,
|
||||
Second = 1,
|
||||
Twice = 2,
|
||||
};
|
||||
|
||||
enum class ScheduleKind : std::uint8_t {
|
||||
OneShot = 0,
|
||||
FixedDelay = 1,
|
||||
FixedRate = 2,
|
||||
Calendar = 3,
|
||||
};
|
||||
|
||||
struct RetryPolicy {
|
||||
std::uint32_t max_attempts{5};
|
||||
std::chrono::milliseconds base_delay{std::chrono::seconds{1}};
|
||||
std::chrono::milliseconds max_delay{std::chrono::minutes{5}};
|
||||
std::uint32_t multiplier{2};
|
||||
};
|
||||
|
||||
struct CalendarSpec {
|
||||
std::string expression{};
|
||||
std::string timezone{"UTC"};
|
||||
std::uint32_t tz_data_version{1};
|
||||
DstGapPolicy gap_policy{DstGapPolicy::Skip};
|
||||
DstFoldPolicy fold_policy{DstFoldPolicy::First};
|
||||
};
|
||||
|
||||
struct Schedule {
|
||||
ScheduleKind kind{ScheduleKind::OneShot};
|
||||
std::int64_t start_utc_ms{0};
|
||||
std::optional<Void::Time::MonotonicClock::time_point>
|
||||
due_monotonic{};
|
||||
std::chrono::milliseconds interval{0};
|
||||
CalendarSpec calendar{};
|
||||
|
||||
[[nodiscard]] static Schedule
|
||||
one_shot_monotonic(Void::Time::MonotonicClock::time_point due)
|
||||
{
|
||||
Schedule s{};
|
||||
s.kind = ScheduleKind::OneShot;
|
||||
s.due_monotonic = due;
|
||||
return s;
|
||||
}
|
||||
|
||||
[[nodiscard]] static Schedule one_shot_utc(std::int64_t utc_ms)
|
||||
{
|
||||
Schedule s{};
|
||||
s.kind = ScheduleKind::OneShot;
|
||||
s.start_utc_ms = utc_ms;
|
||||
return s;
|
||||
}
|
||||
|
||||
[[nodiscard]] static Schedule fixed_delay(
|
||||
std::chrono::milliseconds delay,
|
||||
std::optional<Void::Time::MonotonicClock::time_point>
|
||||
first_due = std::nullopt)
|
||||
{
|
||||
Schedule s{};
|
||||
s.kind = ScheduleKind::FixedDelay;
|
||||
s.interval = delay;
|
||||
s.due_monotonic = first_due;
|
||||
return s;
|
||||
}
|
||||
|
||||
[[nodiscard]] static Schedule fixed_rate(
|
||||
std::chrono::milliseconds period,
|
||||
std::optional<Void::Time::MonotonicClock::time_point>
|
||||
first_due = std::nullopt)
|
||||
{
|
||||
Schedule s{};
|
||||
s.kind = ScheduleKind::FixedRate;
|
||||
s.interval = period;
|
||||
s.due_monotonic = first_due;
|
||||
return s;
|
||||
}
|
||||
|
||||
[[nodiscard]] static Schedule calendar_schedule(CalendarSpec spec)
|
||||
{
|
||||
Schedule s{};
|
||||
s.kind = ScheduleKind::Calendar;
|
||||
s.calendar = std::move(spec);
|
||||
return s;
|
||||
}
|
||||
};
|
||||
|
||||
struct JobSpec {
|
||||
std::string id{};
|
||||
Schedule schedule{};
|
||||
std::string handler_id{};
|
||||
std::uint32_t payload_schema{1};
|
||||
std::vector<std::byte> payload{};
|
||||
MisfirePolicy misfire{MisfirePolicy::CoalesceLatest};
|
||||
OverlapPolicy overlap{OverlapPolicy::Forbid};
|
||||
std::uint32_t max_concurrent{1};
|
||||
RetryPolicy retry{};
|
||||
};
|
||||
|
||||
enum class OccurrenceStatus : std::uint8_t {
|
||||
Pending = 0,
|
||||
Running = 1,
|
||||
Completed = 2,
|
||||
Dead = 3,
|
||||
Cancelled = 4,
|
||||
};
|
||||
|
||||
struct JobRecord {
|
||||
std::uint32_t format_version{1};
|
||||
JobSpec spec{};
|
||||
std::uint64_t revision{1};
|
||||
/// Unstarted occurrences with `revision < invalidated_before_revision`
|
||||
/// are logically cancelled and cannot be claimed.
|
||||
std::uint64_t invalidated_before_revision{1};
|
||||
bool tombstoned{false};
|
||||
std::int64_t cursor_utc_ms{0};
|
||||
std::int64_t cursor_monotonic_ns{0};
|
||||
bool cursor_initialized{false};
|
||||
bool one_shot_materialized{false};
|
||||
std::uint64_t next_sequence{1};
|
||||
};
|
||||
|
||||
struct OccurrenceRecord {
|
||||
std::uint32_t format_version{1};
|
||||
OccurrenceId id{};
|
||||
std::int64_t intended_utc_ms{0};
|
||||
std::int64_t due_monotonic_ns{0};
|
||||
std::uint8_t fold_index{0};
|
||||
OccurrenceStatus status{OccurrenceStatus::Pending};
|
||||
std::uint32_t attempt{0};
|
||||
std::string handler_id{};
|
||||
std::uint32_t payload_schema{1};
|
||||
std::vector<std::byte> payload{};
|
||||
std::string diagnostic_category{};
|
||||
std::int64_t completed_utc_ms{0};
|
||||
bool dead_acknowledged{false};
|
||||
};
|
||||
|
||||
struct SchedulerLimits {
|
||||
std::size_t max_jobs{10000};
|
||||
std::size_t max_retained_occurrences{100000};
|
||||
std::size_t max_payload_bytes{64ULL * 1024};
|
||||
std::size_t max_loaded_payload_bytes{16ULL * 1024 * 1024};
|
||||
std::size_t max_ready_dispatches{256};
|
||||
std::size_t max_active_handlers{32};
|
||||
std::size_t max_catch_up_per_job_per_pass{100};
|
||||
std::size_t max_retries_per_turn{64};
|
||||
std::size_t max_durable_batch_bytes{1024ULL * 1024};
|
||||
std::chrono::milliseconds completed_retention{
|
||||
std::chrono::hours{24 * 7}};
|
||||
};
|
||||
|
||||
struct ShutdownReport {
|
||||
bool complete{true};
|
||||
std::vector<OccurrenceId> live_occurrences{};
|
||||
};
|
||||
|
||||
struct StoreStats {
|
||||
std::size_t jobs{0};
|
||||
std::size_t pending{0};
|
||||
std::size_t running{0};
|
||||
std::size_t completed{0};
|
||||
std::size_t dead{0};
|
||||
std::size_t cancelled{0};
|
||||
std::size_t total_retained{0};
|
||||
bool retention_saturated{false};
|
||||
};
|
||||
|
||||
enum class ObservationKind : std::uint8_t {
|
||||
Materialize = 0,
|
||||
Dispatch = 1,
|
||||
Complete = 2,
|
||||
Retry = 3,
|
||||
DeadLetter = 4,
|
||||
RetentionPause = 5,
|
||||
Recover = 6,
|
||||
};
|
||||
|
||||
struct ObservationEvent {
|
||||
ObservationKind kind{ObservationKind::Dispatch};
|
||||
std::size_t count{1};
|
||||
std::int64_t lag_ms{0};
|
||||
};
|
||||
|
||||
using ObservationHook = std::function<void(const ObservationEvent&)>;
|
||||
using RandomU64Fn = std::function<std::uint64_t()>;
|
||||
|
||||
struct OccurrenceContext {
|
||||
OccurrenceId id{};
|
||||
std::uint32_t attempt{1};
|
||||
std::uint32_t payload_schema{1};
|
||||
std::int64_t intended_utc_ms{0};
|
||||
std::uint8_t fold_index{0};
|
||||
};
|
||||
|
||||
using HandlerFn = std::function<Result<void>(
|
||||
const OccurrenceContext&, std::span<const std::byte>,
|
||||
Void::Async::OperationOptions)>;
|
||||
|
||||
[[nodiscard]] inline std::int64_t
|
||||
to_ns(Void::Time::MonotonicClock::time_point tp) noexcept
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::nanoseconds>(
|
||||
tp.time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::int64_t
|
||||
to_utc_ms(Void::Time::CivilClock::time_point tp) noexcept
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
tp.time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
|
||||
} // namespace Void::Scheduler
|
||||
@@ -0,0 +1,111 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Scheduler/Store.hpp>
|
||||
#include <Void/Storage/Engine.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace Void::SchedulerStorage
|
||||
{
|
||||
struct StorageJobStoreOptions {
|
||||
Void::Scheduler::SchedulerLimits limits{};
|
||||
/// Key prefix namespace within the shared `Void::Storage::Engine`.
|
||||
std::string key_prefix{"sched:v1:"};
|
||||
/// Set true only when explicitly recovering after a process crash
|
||||
/// where the previous owner exited without calling
|
||||
/// `release_ownership()` (the underlying `Storage::Engine` OS lock
|
||||
/// guarantees single-process directory ownership).
|
||||
bool recover_stale_owner{false};
|
||||
};
|
||||
|
||||
/// Durable `Void::Scheduler::JobStore` backed by `Void::Storage::Engine`
|
||||
/// conditional `Batch` writes (`Durability::Sync`, 1 MiB batch cap,
|
||||
/// exclusive logical owner lock, unique civil-slot dedup index, and
|
||||
/// `UnknownOutcome` propagation).
|
||||
class StorageJobStore final : public Void::Scheduler::JobStore
|
||||
{
|
||||
public:
|
||||
|
||||
StorageJobStore(Void::Storage::Engine& engine,
|
||||
StorageJobStoreOptions options = {});
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<void>
|
||||
acquire_ownership(std::string_view owner_id) override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<void>
|
||||
release_ownership() override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<void>
|
||||
put_job(const Void::Scheduler::JobRecord& job) override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<void>
|
||||
update_job(const Void::Scheduler::JobRecord& updated,
|
||||
std::uint64_t expected_revision) override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<void>
|
||||
tombstone_job(std::string_view job_id) override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<
|
||||
std::vector<Void::Scheduler::JobRecord>>
|
||||
load_jobs() const override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<
|
||||
std::vector<Void::Scheduler::OccurrenceRecord>>
|
||||
load_occurrences() const override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<std::size_t>
|
||||
materialize_occurrences(
|
||||
std::string_view job_id, std::uint64_t expected_revision,
|
||||
std::int64_t new_cursor_utc_ms,
|
||||
std::int64_t new_cursor_monotonic_ns,
|
||||
bool cursor_initialized, bool one_shot_materialized,
|
||||
std::uint64_t next_sequence,
|
||||
const std::vector<Void::Scheduler::OccurrenceRecord>&
|
||||
records) override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<
|
||||
Void::Scheduler::OccurrenceRecord>
|
||||
claim_occurrence(const Void::Scheduler::OccurrenceId& id,
|
||||
std::uint32_t expected_attempt) override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<void> complete_occurrence(
|
||||
const Void::Scheduler::OccurrenceId& id, std::uint32_t attempt,
|
||||
std::int64_t completed_utc_ms,
|
||||
std::optional<std::int64_t> fixed_delay_next_monotonic_ns =
|
||||
std::nullopt) override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<void> fail_occurrence(
|
||||
const Void::Scheduler::OccurrenceId& id, std::uint32_t attempt,
|
||||
std::int64_t next_due_monotonic_ns,
|
||||
std::int64_t next_due_utc_ms, bool mark_dead,
|
||||
std::string diagnostic_category) override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<void>
|
||||
requeue_on_shutdown(const Void::Scheduler::OccurrenceId& id,
|
||||
std::uint32_t attempt) override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<std::size_t>
|
||||
recover_running_occurrences() override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<void>
|
||||
acknowledge_dead(const Void::Scheduler::OccurrenceId& id) override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<std::size_t>
|
||||
prune_completed(std::int64_t before_utc_ms,
|
||||
std::size_t max_count) override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<std::size_t>
|
||||
reclaim_invalidated(std::size_t max_count) override;
|
||||
|
||||
[[nodiscard]] Void::Scheduler::Result<Void::Scheduler::StoreStats>
|
||||
stats() const override;
|
||||
|
||||
private:
|
||||
|
||||
Void::Storage::Engine* m_engine{nullptr};
|
||||
StorageJobStoreOptions m_options{};
|
||||
std::string m_owner_id{};
|
||||
};
|
||||
|
||||
} // namespace Void::SchedulerStorage
|
||||
Reference in new issue
Block a user