diff --git a/examples/reloadable-host/CMakeLists.txt b/examples/reloadable-host/CMakeLists.txt index a1d5ab4..ee89537 100644 --- a/examples/reloadable-host/CMakeLists.txt +++ b/examples/reloadable-host/CMakeLists.txt @@ -8,6 +8,7 @@ set(CMAKE_CXX_EXTENSIONS OFF) find_package(Void CONFIG REQUIRED COMPONENTS Functional) add_library(reloadable_host STATIC + src/activation.cpp src/checkpoint.cpp src/control.cpp src/release_registry.cpp @@ -27,8 +28,12 @@ target_link_libraries(reloadable_host_control_test PRIVATE reloadable_host) add_executable(reloadable_host_checkpoint_test tests/checkpoint_test.cpp) target_link_libraries(reloadable_host_checkpoint_test PRIVATE reloadable_host) +add_executable(reloadable_host_activation_test tests/activation_test.cpp) +target_link_libraries(reloadable_host_activation_test PRIVATE reloadable_host) + enable_testing() add_test(NAME reloadable_host_transaction COMMAND reloadable_host_test) add_test(NAME reloadable_host_release COMMAND reloadable_host_release_test) add_test(NAME reloadable_host_control COMMAND reloadable_host_control_test) add_test(NAME reloadable_host_checkpoint COMMAND reloadable_host_checkpoint_test) +add_test(NAME reloadable_host_activation COMMAND reloadable_host_activation_test) diff --git a/examples/reloadable-host/include/reloadable_host/activation.hpp b/examples/reloadable-host/include/reloadable_host/activation.hpp new file mode 100644 index 0000000..7131ce1 --- /dev/null +++ b/examples/reloadable-host/include/reloadable_host/activation.hpp @@ -0,0 +1,256 @@ +#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 diff --git a/examples/reloadable-host/src/activation.cpp b/examples/reloadable-host/src/activation.cpp new file mode 100644 index 0000000..1a2bc79 --- /dev/null +++ b/examples/reloadable-host/src/activation.cpp @@ -0,0 +1,407 @@ +#include "reloadable_host/activation.hpp" + +#include +#include +#include + +namespace ReloadableHost +{ + namespace + { + constexpr std::array kMagic{ + std::byte{'V'}, std::byte{'A'}, std::byte{'C'}, std::byte{'T'}}; + constexpr std::uint32_t kVersion{1}; + + [[nodiscard]] Void::Result::Error invalid(std::string message) + { + return Void::Result::Error{Void::Result::ErrorKind::invalid_argument, + std::move(message)}; + } + + [[nodiscard]] Void::Result::Error corrupt(std::string message) + { + return Void::Result::Error{Void::Result::ErrorKind::corrupt, + std::move(message)}; + } + + [[nodiscard]] Void::Result::Error conflict(std::string message) + { + return Void::Result::Error{Void::Result::ErrorKind::conflict, + std::move(message)}; + } + + void append_u32(std::vector& out, std::uint32_t value) + { + for (int shift = 24; shift >= 0; shift -= 8) { + out.push_back(static_cast((value >> shift) & 0xFFU)); + } + } + + void append_u64(std::vector& out, std::uint64_t value) + { + for (int shift = 56; shift >= 0; shift -= 8) { + out.push_back(static_cast((value >> shift) & 0xFFU)); + } + } + + [[nodiscard]] bool read_u32(std::span in, + std::size_t& pos, std::uint32_t& value) noexcept + { + if (pos + 4 > in.size()) { + return false; + } + value = 0; + for (int i = 0; i < 4; ++i) { + value = (value << 8) | + std::to_integer(in[pos + i]); + } + pos += 4; + return true; + } + + [[nodiscard]] bool read_u64(std::span in, + std::size_t& pos, std::uint64_t& value) noexcept + { + if (pos + 8 > in.size()) { + return false; + } + value = 0; + for (int i = 0; i < 8; ++i) { + value = (value << 8) | + std::to_integer(in[pos + i]); + } + pos += 8; + return true; + } + + void append_string(std::vector& out, std::string_view text) + { + append_u32(out, static_cast(text.size())); + for (const char c : text) { + out.push_back(static_cast( + static_cast(c))); + } + } + + [[nodiscard]] bool read_string(std::span in, + std::size_t& pos, std::string& out, + std::size_t max_bytes) noexcept + { + std::uint32_t length = 0; + if (!read_u32(in, pos, length) || length > max_bytes || + pos + length > in.size()) { + return false; + } + out.clear(); + out.reserve(length); + for (std::uint32_t i = 0; i < length; ++i) { + out.push_back(static_cast( + std::to_integer(in[pos + i]))); + } + pos += length; + return true; + } + + [[nodiscard]] std::uint64_t checksum(std::span bytes) + { + std::uint64_t hash = 0xCBF29CE484222325ULL; + for (const std::byte byte : bytes) { + hash ^= std::to_integer(byte); + hash *= 0x100000001B3ULL; + } + return hash; + } + + [[nodiscard]] bool is_known_phase(ActivationPhase phase) noexcept + { + return phase == ActivationPhase::Activated || + phase == ActivationPhase::HealthyCommitted || + phase == ActivationPhase::RolledBack; + } + } // namespace + + Void::Result::Result> + encode_activation(const ActivationRecord& record, ActivationLimits limits) + { + if (record.previous_release.size() > limits.max_release_bytes || + record.candidate_release.size() > limits.max_release_bytes) { + return std::unexpected{ + invalid("Activation: release id exceeds the size limit")}; + } + if (!is_known_phase(record.phase)) { + return std::unexpected{ + invalid("Activation: cannot encode an unset phase")}; + } + + std::vector out; + out.insert(out.end(), kMagic.begin(), kMagic.end()); + append_u32(out, kVersion); + append_u64(out, record.transaction_id); + append_u64(out, record.checkpoint_generation); + out.push_back(static_cast(record.phase)); + append_string(out, record.previous_release); + append_string(out, record.candidate_release); + append_u64(out, checksum(out)); + if (out.size() > limits.max_record_bytes) { + return std::unexpected{ + invalid("Activation: record exceeds max_record_bytes")}; + } + return out; + } + + Void::Result::Result + decode_activation(std::span bytes, + ActivationLimits limits) + { + if (bytes.size() > limits.max_record_bytes || + bytes.size() < kMagic.size() + 4 + 8 + 8 + 1 + 4 + 4 + 8) { + return std::unexpected{corrupt("Activation: record is truncated")}; + } + if (!std::equal(kMagic.begin(), kMagic.end(), bytes.begin())) { + return std::unexpected{corrupt("Activation: bad magic")}; + } + const std::size_t payload = bytes.size() - 8; + std::uint64_t stored_checksum = 0; + { + std::size_t pos = payload; + if (!read_u64(bytes, pos, stored_checksum)) { + return std::unexpected{ + corrupt("Activation: missing checksum")}; + } + } + if (checksum(bytes.first(payload)) != stored_checksum) { + return std::unexpected{ + corrupt("Activation: checksum mismatch")}; + } + + std::size_t pos = kMagic.size(); + std::uint32_t version = 0; + if (!read_u32(bytes, pos, version) || version != kVersion) { + return std::unexpected{ + corrupt("Activation: unsupported record version")}; + } + ActivationRecord record; + if (!read_u64(bytes, pos, record.transaction_id) || + !read_u64(bytes, pos, record.checkpoint_generation) || + pos >= payload) { + return std::unexpected{corrupt("Activation: malformed record")}; + } + record.phase = static_cast( + std::to_integer(bytes[pos++])); + if (!is_known_phase(record.phase)) { + return std::unexpected{corrupt("Activation: unknown phase")}; + } + if (!read_string(bytes, pos, record.previous_release, + limits.max_release_bytes) || + !read_string(bytes, pos, record.candidate_release, + limits.max_release_bytes) || + pos != payload) { + return std::unexpected{corrupt("Activation: malformed record")}; + } + return record; + } + + ActivationJournal::ActivationJournal(ActivationStorage& storage, + ActivationLimits limits) + : m_storage(storage), m_limits(limits) + { + } + + Void::Result::Result> + ActivationJournal::load() + { + auto bytes = m_storage.read(); + if (!bytes) { + return std::unexpected{std::move(bytes).error()}; + } + if (!bytes->has_value()) { + return std::optional{}; + } + auto decoded = decode_activation(**bytes, m_limits); + if (!decoded) { + return std::unexpected{std::move(decoded).error()}; + } + return std::optional{std::move(*decoded)}; + } + + Void::Result::Result + ActivationJournal::activate(ActivationRecord record) + { + record.phase = ActivationPhase::Activated; + auto existing = load(); + if (!existing) { + return std::unexpected{std::move(existing).error()}; + } + if (existing->has_value()) { + return std::unexpected{conflict( + "Activation: journal already holds a record; cleanup first")}; + } + auto encoded = encode_activation(record, m_limits); + if (!encoded) { + return std::unexpected{std::move(encoded).error()}; + } + if (auto written = m_storage.write(*encoded); !written) { + // The write may still have landed durably; make the caller decide + // from the journal rather than assuming nothing happened. + return std::unexpected{std::move(written).error()}; + } + return {}; + } + + CommitOutcome + ActivationJournal::commit_healthy(std::uint64_t transaction_id) noexcept + { + auto existing = load(); + if (!existing || !existing->has_value() || + (*existing)->transaction_id != transaction_id) { + return CommitOutcome::Rejected; + } + ActivationRecord record = **existing; + if (record.phase == ActivationPhase::HealthyCommitted) { + return CommitOutcome::Committed; + } + if (record.phase != ActivationPhase::Activated) { + return CommitOutcome::Rejected; + } + record.phase = ActivationPhase::HealthyCommitted; + auto encoded = encode_activation(record, m_limits); + if (!encoded) { + return CommitOutcome::Unknown; + } + if (auto written = m_storage.write(*encoded); written) { + return CommitOutcome::Committed; + } + // Lost acknowledgement: re-read the durable record. The marker being + // present means committed; its absence is Unknown, never a rollback. + auto reread = load(); + if (reread && reread->has_value() && + (*reread)->phase == ActivationPhase::HealthyCommitted && + (*reread)->transaction_id == transaction_id) { + return CommitOutcome::Committed; + } + return CommitOutcome::Unknown; + } + + Void::Result::Result + ActivationJournal::rollback(std::uint64_t transaction_id) + { + auto existing = load(); + if (!existing) { + return std::unexpected{std::move(existing).error()}; + } + if (!existing->has_value()) { + return std::unexpected{ + conflict("Activation: no activation to roll back")}; + } + ActivationRecord record = **existing; + if (record.transaction_id != transaction_id) { + return std::unexpected{ + conflict("Activation: transaction is not the active one")}; + } + if (record.phase == ActivationPhase::HealthyCommitted) { + return std::unexpected{conflict( + "Activation: healthy marker is durable; rollback is not the " + "recovery action")}; + } + if (record.phase == ActivationPhase::RolledBack) { + return {}; + } + record.phase = ActivationPhase::RolledBack; + auto encoded = encode_activation(record, m_limits); + if (!encoded) { + return std::unexpected{std::move(encoded).error()}; + } + return m_storage.write(*encoded); + } + + Void::Result::Result ActivationJournal::cleanup() + { + auto existing = load(); + if (!existing) { + return std::unexpected{std::move(existing).error()}; + } + if (existing->has_value() && + (*existing)->phase == ActivationPhase::Activated) { + return std::unexpected{conflict( + "Activation: cannot clean up an uncommitted activation")}; + } + return m_storage.clear(); + } + + RecoveryDecision ActivationJournal::reconcile() + { + RecoveryDecision decision; + auto existing = load(); + if (!existing) { + decision.action = RecoveryAction::Diagnostic; + decision.diagnostic = existing.error().message(); + return decision; + } + if (!existing->has_value()) { + decision.action = RecoveryAction::None; + return decision; + } + decision.record = **existing; + switch (decision.record.phase) { + case ActivationPhase::Activated: + // Crash before the healthy marker: the activation is + // reversible, so the prior healthy release wins. + decision.action = RecoveryAction::UsePrevious; + break; + case ActivationPhase::HealthyCommitted: + // Committed even if reclaim never ran. + decision.action = RecoveryAction::UseCandidate; + decision.cleanup_pending = true; + break; + case ActivationPhase::RolledBack: + decision.action = RecoveryAction::UsePrevious; + break; + case ActivationPhase::None: + decision.action = RecoveryAction::Diagnostic; + decision.diagnostic = "Activation: unset phase"; + break; + } + return decision; + } + + Void::Result::Result> + ActivationJournal::current() + { + return load(); + } + + HealthWindow::HealthWindow(std::chrono::milliseconds duration) noexcept + : m_deadline(std::chrono::steady_clock::now() + duration) + { + } + + Void::Result::Result HealthWindow::admit_write() const + { + if (!m_open) { + return {}; + } + return std::unexpected{Void::Result::Error{ + Void::Result::ErrorKind::overloaded, + "HealthWindow: mutations are closed during the read-only health " + "window"}}; + } + + void HealthWindow::observe(bool healthy) noexcept + { + if (!healthy) { + m_failed = true; + } + } + + HealthWindow::Decision HealthWindow::evaluate( + std::chrono::steady_clock::time_point now) const noexcept + { + if (!m_open) { + return Decision::Pending; + } + if (m_failed) { + return Decision::Rollback; + } + return now >= m_deadline ? Decision::Commit : Decision::Pending; + } +} // namespace ReloadableHost diff --git a/examples/reloadable-host/tests/activation_test.cpp b/examples/reloadable-host/tests/activation_test.cpp new file mode 100644 index 0000000..6d67123 --- /dev/null +++ b/examples/reloadable-host/tests/activation_test.cpp @@ -0,0 +1,184 @@ +#include "reloadable_host/activation.hpp" + +#include +#include +#include +#include +#include + +namespace +{ + using namespace ReloadableHost; + + ActivationRecord make_record(std::uint64_t id = 1) + { + ActivationRecord record; + record.transaction_id = id; + record.previous_release = "release-a"; + record.candidate_release = "release-b"; + record.checkpoint_generation = 7; + return record; + } + + void encoding_roundtrip() + { + auto record = make_record(); + record.phase = ActivationPhase::Activated; + auto encoded = encode_activation(record); + assert(encoded.has_value()); + auto decoded = decode_activation(*encoded); + assert(decoded.has_value()); + assert(decoded->transaction_id == record.transaction_id); + assert(decoded->previous_release == record.previous_release); + assert(decoded->candidate_release == record.candidate_release); + assert(decoded->checkpoint_generation == record.checkpoint_generation); + assert(decoded->phase == ActivationPhase::Activated); + } + + void encoding_rejects_corruption_and_limits() + { + auto record = make_record(); + record.phase = ActivationPhase::Activated; + auto encoded = encode_activation(record); + assert(encoded.has_value()); + + auto flipped = *encoded; + flipped[flipped.size() / 2] ^= std::byte{0xFF}; + assert(!decode_activation(flipped).has_value()); + + auto truncated = std::vector{ + encoded->begin(), encoded->begin() + 10}; + assert(!decode_activation(truncated).has_value()); + + ActivationLimits tiny; + tiny.max_record_bytes = 8; + assert(!decode_activation(*encoded, tiny).has_value()); + + // An unset phase is not a valid durable record. + auto unset = make_record(); + assert(!encode_activation(unset).has_value()); + } + + void activation_is_reversible_until_the_healthy_marker() + { + MemoryActivationStorage storage; + ActivationJournal journal{storage}; + + assert(journal.reconcile().action == RecoveryAction::None); + + assert(journal.activate(make_record(11)).has_value()); + // A second activation cannot overwrite the first. + assert(!journal.activate(make_record(12)).has_value()); + + const auto crashed = journal.reconcile(); + assert(crashed.action == RecoveryAction::UsePrevious); + assert(crashed.record.previous_release == "release-a"); + assert(crashed.record.candidate_release == "release-b"); + assert(!crashed.cleanup_pending); + + // Cleanup of an uncommitted activation is refused. + assert(!journal.cleanup().has_value()); + + assert(journal.rollback(11).has_value()); + assert(journal.reconcile().action == RecoveryAction::UsePrevious); + assert(journal.cleanup().has_value()); + assert(journal.reconcile().action == RecoveryAction::None); + } + + void healthy_marker_is_the_commit_point() + { + MemoryActivationStorage storage; + ActivationJournal journal{storage}; + assert(journal.activate(make_record(21)).has_value()); + assert(journal.commit_healthy(21) == CommitOutcome::Committed); + + const auto recovered = journal.reconcile(); + assert(recovered.action == RecoveryAction::UseCandidate); + // Commit durable, reclaim not done yet: reported, not rolled back. + assert(recovered.cleanup_pending); + + // Rolling back a committed activation is refused. + assert(!journal.rollback(21).has_value()); + + assert(journal.cleanup().has_value()); + assert(journal.reconcile().action == RecoveryAction::None); + } + + void commit_rejects_a_foreign_transaction() + { + MemoryActivationStorage storage; + ActivationJournal journal{storage}; + assert(journal.activate(make_record(31)).has_value()); + assert(journal.commit_healthy(99) == CommitOutcome::Rejected); + assert(journal.reconcile().action == RecoveryAction::UsePrevious); + } + + void lost_acknowledgement_is_never_a_rollback() + { + MemoryActivationStorage storage; + ActivationJournal journal{storage}; + assert(journal.activate(make_record(41)).has_value()); + + // The write lands durably but reports failure. + storage.lose_next_acknowledgement(true); + assert(journal.commit_healthy(41) == CommitOutcome::Committed); + assert(journal.reconcile().action == RecoveryAction::UseCandidate); + } + + void failed_write_without_durable_marker_is_unknown() + { + MemoryActivationStorage storage; + ActivationJournal journal{storage}; + assert(journal.activate(make_record(51)).has_value()); + + storage.fail_next_write(true); + assert(journal.commit_healthy(51) == CommitOutcome::Unknown); + // The durable record is still the uncommitted activation. + assert(journal.reconcile().action == RecoveryAction::UsePrevious); + } + + void corrupt_journal_never_invents_a_release() + { + MemoryActivationStorage storage; + storage.corrupt({std::byte{1}, std::byte{2}, std::byte{3}}); + ActivationJournal journal{storage}; + const auto decision = journal.reconcile(); + assert(decision.action == RecoveryAction::Diagnostic); + assert(!decision.diagnostic.empty()); + } + + void health_window_admits_reads_and_holds_writes() + { + HealthWindow window{std::chrono::milliseconds{50}}; + assert(window.admit_read()); + assert(!window.admit_write().has_value()); + + using Clock = std::chrono::steady_clock; + const auto now = Clock::now(); + assert(window.evaluate(now) == HealthWindow::Decision::Pending); + assert(window.evaluate(now + std::chrono::milliseconds{100}) == + HealthWindow::Decision::Commit); + + window.observe(false); + assert(window.evaluate(now) == HealthWindow::Decision::Rollback); + + window.finish(); + assert(!window.open()); + assert(!window.admit_read()); + } +} // namespace + +int main() +{ + encoding_roundtrip(); + encoding_rejects_corruption_and_limits(); + activation_is_reversible_until_the_healthy_marker(); + healthy_marker_is_the_commit_point(); + commit_rejects_a_foreign_transaction(); + lost_acknowledgement_is_never_a_rollback(); + failed_write_without_durable_marker_is_unknown(); + corrupt_journal_never_invents_a_release(); + health_window_admits_reads_and_holds_writes(); + std::puts("reloadable-host activation ok"); + return 0; +}