#pragma once #include #include #include #include #include #include #include #include namespace ReloadableHost { /// Durable phase of one host reload. /// /// `Activated` is reversible and uncommitted: a crash here recovers to the /// previous healthy release. `HealthyCommitted` is the single commit /// marker: a crash after it keeps the candidate even when cleanup is /// incomplete. `RolledBack` records an explicit pre-commit rollback. enum class ActivationPhase : std::uint8_t { None, Activated, HealthyCommitted, RolledBack }; struct ActivationRecord { std::uint64_t transaction_id{0}; std::string previous_release; std::string candidate_release; std::uint64_t checkpoint_generation{0}; ActivationPhase phase{ActivationPhase::None}; }; struct ActivationLimits { std::size_t max_release_bytes{256}; std::size_t max_record_bytes{4096}; }; /// Durable journal storage boundary. `write` must not report success /// before the bytes are durable; a file-backed implementation owns /// fsync and directory sync. class ActivationStorage { public: ActivationStorage() = default; ActivationStorage(const ActivationStorage&) = delete; ActivationStorage& operator=(const ActivationStorage&) = delete; virtual ~ActivationStorage() = default; [[nodiscard]] virtual Void::Result::Result< std::optional>> read() = 0; [[nodiscard]] virtual Void::Result::Result write(std::span bytes) = 0; [[nodiscard]] virtual Void::Result::Result clear() = 0; }; /// Bounded in-memory journal used by tests and by hosts without durable /// recovery (it never survives a process, so it is not recovery evidence). class MemoryActivationStorage final : public ActivationStorage { public: [[nodiscard]] Void::Result::Result< std::optional>> read() override { if (!m_bytes) { return std::optional>{}; } return std::optional>{*m_bytes}; } [[nodiscard]] Void::Result::Result write(std::span bytes) override { if (m_fail_write) { m_fail_write = false; return std::unexpected{Void::Result::Error{ Void::Result::ErrorKind::io, "MemoryActivationStorage: injected write failure"}}; } m_bytes = std::vector{bytes.begin(), bytes.end()}; if (m_lose_ack) { m_lose_ack = false; return std::unexpected{Void::Result::Error{ Void::Result::ErrorKind::io, "MemoryActivationStorage: lost acknowledgement"}}; } return {}; } [[nodiscard]] Void::Result::Result clear() override { m_bytes.reset(); return {}; } /// Test hook: simulate a torn/garbage durable record. void corrupt(std::vector bytes) { m_bytes = std::move(bytes); } /// Test hook: fail the next write without touching the record. void fail_next_write(bool fail) noexcept { m_fail_write = fail; } /// Test hook: make the next write return an error even though the /// bytes were durably stored (lost acknowledgement). void lose_next_acknowledgement(bool lose) noexcept { m_lose_ack = lose; } private: std::optional> m_bytes{}; bool m_fail_write{false}; bool m_lose_ack{false}; }; [[nodiscard]] Void::Result::Result> encode_activation(const ActivationRecord& record, ActivationLimits limits = {}); [[nodiscard]] Void::Result::Result decode_activation(std::span bytes, ActivationLimits limits = {}); enum class RecoveryAction : std::uint8_t { /// No journal: first bootstrap. None, /// Uncommitted activation or explicit rollback: restore the previous /// healthy release. UsePrevious, /// The healthy marker is durable: keep the candidate. UseCandidate, /// Corrupt or unknown journal: start not-ready and never invent a /// release. Diagnostic }; struct RecoveryDecision { RecoveryAction action{RecoveryAction::None}; ActivationRecord record{}; /// True when the healthy marker is durable but post-commit /// cleanup did not finish. bool cleanup_pending{false}; std::string diagnostic; }; enum class CommitOutcome : std::uint8_t { Committed, /// The durable acknowledgement was lost. The caller must reconcile; /// it must never assume a rollback. Unknown, /// The transaction is not the active uncommitted activation. Rejected }; /// One authoritative activation journal. /// /// Exactly one record is live. The phase transitions are: /// None -> Activated -> HealthyCommitted (commit marker, irreversible) /// -> RolledBack (explicit pre-commit rollback) /// Committed/rolled-back records are cleared only by `cleanup()`. class ActivationJournal { public: explicit ActivationJournal(ActivationStorage& storage, ActivationLimits limits = {}); /// Durably record a new reversible activation. Requires an empty /// journal (or a cleared one) so two transactions cannot disagree. [[nodiscard]] Void::Result::Result activate(ActivationRecord record); /// Durably record the healthy commit marker. A failed write is /// resolved by re-reading: if the marker is durable the outcome is /// Committed, otherwise Unknown. [[nodiscard]] CommitOutcome commit_healthy(std::uint64_t transaction_id) noexcept; /// Durably record an explicit rollback of the uncommitted /// activation. Rejected after the healthy marker. [[nodiscard]] Void::Result::Result rollback(std::uint64_t transaction_id); /// Remove a committed or rolled-back record after reclaim. Never /// removes an uncommitted activation. [[nodiscard]] Void::Result::Result cleanup(); /// Read and classify the durable journal on startup. [[nodiscard]] RecoveryDecision reconcile(); [[nodiscard]] Void::Result::Result> current(); private: [[nodiscard]] Void::Result::Result> load(); ActivationStorage& m_storage; ActivationLimits m_limits; }; /// Reversible read-only health window after publication. /// /// During the window snapshot-safe reads are admitted and mutations get a /// retryable not-ready result; writes reopen only after the healthy marker /// is durable. A failed observation rolls back immediately. class HealthWindow { public: enum class Decision : std::uint8_t { Pending, Commit, Rollback }; explicit HealthWindow( std::chrono::milliseconds duration = std::chrono::milliseconds{10000}) noexcept; [[nodiscard]] bool admit_read() const noexcept { return m_open; } [[nodiscard]] Void::Result::Result admit_write() const; /// Record one readiness observation. Any unhealthy observation /// closes the window for mutation. void observe(bool healthy) noexcept; /// Commit once the window elapsed with no failure, roll back on a /// failed observation, otherwise keep waiting. [[nodiscard]] Decision evaluate( std::chrono::steady_clock::time_point now) const noexcept; /// Close the window after the decision was applied. void finish() noexcept { m_open = false; } [[nodiscard]] bool open() const noexcept { return m_open; } [[nodiscard]] bool failed() const noexcept { return m_failed; } private: std::chrono::steady_clock::time_point m_deadline; bool m_open{true}; bool m_failed{false}; }; } // namespace ReloadableHost