ReloadableApplications: durable activation journal, crash recovery and read-only health window (P-RELOADABLE-04)
This commit is contained in:
1 parent
03542d0d2f
commit
50595bdabc
4 files changed
+852
No files matched your search
@@ -0,0 +1,184 @@
|
||||
#include "reloadable_host/activation.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::byte>{
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user