Complete M7.5 durable batch outcomes and commit resolution
ADR-0063: manifest v2 (360 bytes) binds a per-tick validation trace object and a canonical decision block by SHA-256. Publication replays the trace from the committed predecessor's verified payload and requires every report and the candidate bytes, derives deaths deterministically, uploads payload and trace with verified read-back, then sends manifest and decisions to one fenced statement. RegionBatch seals at a terminal transition or 64 ticks and refuses ticks until durable confirmation. Schema v5 records the checkpoint, its decisions and outbox rows atomically; a unique index keeps one terminal outcome per actor (EVL08), triggers make commits/decisions immutable (EVL09), an exact retry of a committed ID returns the original receipt, changed bytes conflict, and read-only resolve_region_commit answers committed/absent/ conflict. Outbox pending/acknowledge give idempotent at-least-once delivery. Recovery reconciles durable deaths with restored lifecycle (and archive deaths for the lineage profile). Validated: Windows CTest 58/58; Linux GCC Debug/Release, clang-tidy and ASan/UBSan CTest 58/58 each; PostgreSQL suites Release, analysis, ASan (lost response, committed-after-timeout, terminated orphan retry, changed bytes, repeated/partial/reordered decisions, gap, outbox redelivery, history quarantine); five mutations each caught. No Docker/Gitea CI, process kill, broker or deployment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,7 +8,30 @@ namespace evo::persistence {
|
||||
namespace {
|
||||
using std::chrono::milliseconds;
|
||||
|
||||
// Per-profile payload operations; the publication and recovery paths are shared.
|
||||
struct Organisms {
|
||||
std::uint64_t producer{}, consumer{};
|
||||
};
|
||||
// Lifecycle agreement shared by both profiles: exactly one durable death per
|
||||
// terminal actor, none for an active one, never after the committed tick.
|
||||
bool lifecycle_agrees(const processes::EnvironmentalProgress& progress, Organisms organisms,
|
||||
std::span<const CommittedDecision> decisions, const RegionHead& head) {
|
||||
for (const auto actor : {Actor::producer, Actor::consumer}) {
|
||||
const auto terminal =
|
||||
actor == Actor::producer ? progress.producer_terminal : progress.consumer_terminal;
|
||||
const auto count = std::ranges::count_if(
|
||||
decisions, [&](const CommittedDecision& d) { return d.decision.actor == actor; });
|
||||
if (count > 1 || (terminal == 1) != (count == 1))
|
||||
return false;
|
||||
}
|
||||
return std::ranges::all_of(decisions, [&](const CommittedDecision& d) {
|
||||
const auto organism =
|
||||
d.decision.actor == Actor::producer ? organisms.producer : organisms.consumer;
|
||||
return d.decision.tick >= 1 && d.decision.tick <= head.end_tick &&
|
||||
d.commit_sequence <= head.sequence && d.decision.organism == organism;
|
||||
});
|
||||
}
|
||||
|
||||
// Per-profile payload operations; publication and recovery paths are shared.
|
||||
struct EnvironmentalCodec {
|
||||
using State = processes::EnvironmentalCommunity;
|
||||
using Recovered = RecoveredCheckpoint;
|
||||
@@ -22,10 +45,17 @@ struct EnvironmentalCodec {
|
||||
static std::uint64_t quantum(const State& s) {
|
||||
return s.tick_microseconds();
|
||||
}
|
||||
static Organisms organisms(const State&) {
|
||||
return {};
|
||||
}
|
||||
static auto restore(std::span<const std::uint8_t> payload, const CheckpointManifest& m,
|
||||
std::size_t budget) {
|
||||
return restore_environmental_payload(payload, m, budget);
|
||||
}
|
||||
static bool reconcile(const State& s, std::span<const CommittedDecision> d,
|
||||
const RegionHead& head) {
|
||||
return lifecycle_agrees(s.progress(), organisms(s), d, head);
|
||||
}
|
||||
};
|
||||
struct LineageCodec {
|
||||
using State = processes::LineageRegion;
|
||||
@@ -40,44 +70,61 @@ struct LineageCodec {
|
||||
static std::uint64_t quantum(const State& s) {
|
||||
return s.community().tick_microseconds();
|
||||
}
|
||||
static Organisms organisms(const State& s) {
|
||||
return {s.binding().producer, s.binding().consumer};
|
||||
}
|
||||
static auto restore(std::span<const std::uint8_t> payload, const CheckpointManifest& m,
|
||||
std::size_t budget) {
|
||||
return restore_lineage_payload(payload, m, budget);
|
||||
}
|
||||
// Plus: each durable death is the archive's death, at the same tick.
|
||||
static bool reconcile(const State& s, std::span<const CommittedDecision> d,
|
||||
const RegionHead& head) {
|
||||
if (!lifecycle_agrees(s.community().progress(), organisms(s), d, head))
|
||||
return false;
|
||||
return std::ranges::all_of(d, [&](const CommittedDecision& x) {
|
||||
const auto record = s.archive().organism(x.decision.organism);
|
||||
return record && !record->alive && record->died.value == x.decision.tick;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
PublicationFailure fail(PublicationError kind) {
|
||||
return {kind, {}, {}};
|
||||
PublicationFailure fail(PublicationError kind, ObjectError object = {}) {
|
||||
return {kind, {}, object, {}};
|
||||
}
|
||||
// Validates the exact payload bytes as recovery would, uploads and reads back.
|
||||
// Returns the manifest bytes ready for the database.
|
||||
template <typename Codec>
|
||||
std::expected<std::vector<std::uint8_t>, PublicationFailure>
|
||||
stage(const FilesystemObjectStore& objects, const std::vector<std::uint8_t>& payload,
|
||||
const CheckpointManifest& manifest, std::size_t budget) {
|
||||
auto bytes = encode_manifest(manifest);
|
||||
if (!bytes || manifest.profile != Codec::profile || !Codec::restore(payload, manifest, budget))
|
||||
return std::unexpected{fail(PublicationError::candidate)};
|
||||
auto stored = objects.put(payload);
|
||||
std::string hex_parameter(std::span<const std::uint8_t> bytes) {
|
||||
constexpr char digits[] = "0123456789abcdef";
|
||||
std::string out = "\\x";
|
||||
for (const auto b : bytes) {
|
||||
out.push_back(digits[b >> 4U]);
|
||||
out.push_back(digits[b & 0x0FU]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
std::expected<void, PublicationFailure> put_verified(const FilesystemObjectStore& objects,
|
||||
const std::vector<std::uint8_t>& bytes,
|
||||
const VerifiedObject& expected) {
|
||||
auto stored = objects.put(bytes);
|
||||
if (!stored)
|
||||
return std::unexpected{PublicationFailure{PublicationError::upload, {}, stored.error()}};
|
||||
return std::unexpected{fail(PublicationError::upload, stored.error())};
|
||||
auto back = objects.get(*stored);
|
||||
if (!back || *stored != VerifiedObject{manifest.payload_sha256, manifest.payload_bytes} ||
|
||||
!std::ranges::equal(*back, payload))
|
||||
return std::unexpected{PublicationFailure{
|
||||
PublicationError::verify, {}, back ? ObjectError::corrupt : back.error()}};
|
||||
return std::move(*bytes);
|
||||
if (!back || *stored != expected || !std::ranges::equal(*back, bytes))
|
||||
return std::unexpected{
|
||||
fail(PublicationError::verify, back ? ObjectError::corrupt : back.error())};
|
||||
return {};
|
||||
}
|
||||
// The receipt must agree with the local manifest the database digested.
|
||||
std::expected<CommittedCheckpoint, PublicationFailure>
|
||||
confirm(const std::expected<RegionHead, LeaseStoreFailure>& published,
|
||||
std::vector<std::uint8_t> manifest_bytes, const CheckpointManifest& manifest) {
|
||||
std::vector<std::uint8_t> manifest_bytes, const CheckpointManifest& manifest,
|
||||
PreparedCommit prepared) {
|
||||
if (!published) {
|
||||
const auto kind = published.error().kind == LeaseStoreError::unknown
|
||||
? PublicationError::unknown
|
||||
: PublicationError::rejected;
|
||||
return std::unexpected{PublicationFailure{kind, published.error(), {}}};
|
||||
return std::unexpected{
|
||||
PublicationFailure{kind, published.error(), {}, std::move(prepared)}};
|
||||
}
|
||||
// The database digested the bytes independently; the receipt must agree.
|
||||
const auto digest = sha256(manifest_bytes);
|
||||
if (!digest || published->digest != *digest || published->sequence != manifest.sequence ||
|
||||
published->commit != manifest.commit || published->end_tick != manifest.end_tick ||
|
||||
@@ -99,67 +146,111 @@ provision(Postgres& owner, const FilesystemObjectStore& objects, RegionKey key,
|
||||
return std::unexpected{fail(PublicationError::digest)};
|
||||
auto manifest = genesis_manifest(key, frame, commit, Codec::quantum(opening), build,
|
||||
payload->size(), *digest, Codec::profile);
|
||||
if (!manifest)
|
||||
auto bytes = manifest ? encode_manifest(*manifest)
|
||||
: std::expected<std::vector<std::uint8_t>, CheckpointError>{
|
||||
std::unexpected{CheckpointError::format}};
|
||||
if (!bytes || !Codec::restore(*payload, *manifest, budget))
|
||||
return std::unexpected{fail(PublicationError::candidate)};
|
||||
auto bytes = stage<Codec>(objects, *payload, *manifest, budget);
|
||||
if (!bytes)
|
||||
return std::unexpected{bytes.error()};
|
||||
constexpr char digits[] = "0123456789abcdef";
|
||||
std::string parameter = "\\x";
|
||||
for (const auto b : *bytes) {
|
||||
parameter.push_back(digits[b >> 4U]);
|
||||
parameter.push_back(digits[b & 0x0FU]);
|
||||
}
|
||||
const std::array<std::string, 1> p{parameter};
|
||||
if (auto stored = put_verified(objects, *payload, {*digest, payload->size()}); !stored)
|
||||
return std::unexpected{stored.error()};
|
||||
const std::array<std::string, 1> p{hex_parameter(*bytes)};
|
||||
auto done = owner.query("SELECT evolution.provision_region_checkpoint($1::bytea)", p, timeout);
|
||||
if (!done) {
|
||||
const bool unknown = done.error().kind == PgError::unknown;
|
||||
return std::unexpected{PublicationFailure{
|
||||
unknown ? PublicationError::unknown : PublicationError::rejected,
|
||||
{unknown ? LeaseStoreError::unknown : LeaseStoreError::store, done.error()},
|
||||
{},
|
||||
{}}};
|
||||
}
|
||||
auto committed = load_region_checkpoint(owner, key, timeout);
|
||||
if (!committed)
|
||||
return std::unexpected{
|
||||
PublicationFailure{PublicationError::unknown, committed.error(), {}}};
|
||||
return confirm(committed->head, std::move(*bytes), *manifest);
|
||||
PublicationFailure{PublicationError::unknown, committed.error(), {}, {}}};
|
||||
return confirm(committed->head, std::move(*bytes), *manifest, {});
|
||||
}
|
||||
|
||||
template <typename Codec>
|
||||
std::expected<CommittedCheckpoint, PublicationFailure>
|
||||
publish(Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
|
||||
const CommittedCheckpoint& predecessor, const typename Codec::State& state, Uuid commit,
|
||||
const Sha256Digest& build, std::size_t budget, milliseconds timeout) {
|
||||
std::expected<PublishedBatch, PublicationFailure>
|
||||
publish_batch(Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
|
||||
const CommittedCheckpoint& predecessor,
|
||||
const RegionBatch<typename Codec::State>& batch, Uuid commit,
|
||||
const Sha256Digest& build, std::size_t budget, milliseconds timeout) {
|
||||
const auto previous = decode_manifest(predecessor.manifest);
|
||||
auto payload = Codec::snapshot(state);
|
||||
if (!previous || !payload || previous->profile != Codec::profile)
|
||||
if (!previous || previous->profile != Codec::profile || !batch.sealed() ||
|
||||
batch.steps().empty() || batch.begin_tick() != previous->end_tick)
|
||||
return std::unexpected{fail(PublicationError::candidate)};
|
||||
const auto digest = sha256(*payload);
|
||||
if (!digest)
|
||||
// Replay the recorded inputs from the committed predecessor's verified bytes.
|
||||
auto base = objects.get({previous->payload_sha256, previous->payload_bytes});
|
||||
if (!base)
|
||||
return std::unexpected{fail(PublicationError::predecessor, base.error())};
|
||||
auto replay = Codec::restore(*base, *previous, budget);
|
||||
if (!replay)
|
||||
return std::unexpected{fail(PublicationError::predecessor)};
|
||||
for (const auto& step : batch.steps()) {
|
||||
auto report = (*replay)->advance(step.report.tick, step.input);
|
||||
if (!report || *report != step.report)
|
||||
return std::unexpected{fail(PublicationError::candidate)};
|
||||
}
|
||||
auto payload = Codec::snapshot(batch.state());
|
||||
const auto replayed = Codec::snapshot(**replay);
|
||||
if (!payload || !replayed || *payload != *replayed)
|
||||
return std::unexpected{fail(PublicationError::candidate)};
|
||||
|
||||
const auto organisms = Codec::organisms(batch.state());
|
||||
const auto decisions = derive_decisions(batch.steps(), organisms.producer, organisms.consumer);
|
||||
auto decision_bytes = encode_decisions(decisions, batch.begin_tick(), batch.end_tick());
|
||||
const TraceHeader header{static_cast<std::uint32_t>(Codec::profile),
|
||||
previous->key,
|
||||
previous->commit,
|
||||
predecessor.head.digest,
|
||||
batch.begin_tick(),
|
||||
batch.end_tick()};
|
||||
auto trace = encode_trace(header, batch.steps());
|
||||
if (!decision_bytes || !trace)
|
||||
return std::unexpected{fail(PublicationError::candidate)};
|
||||
const auto payload_digest = sha256(*payload);
|
||||
const auto trace_digest = sha256(*trace);
|
||||
const auto decisions_digest = sha256(*decision_bytes);
|
||||
if (!payload_digest || !trace_digest || !decisions_digest)
|
||||
return std::unexpected{fail(PublicationError::digest)};
|
||||
auto manifest = successor_manifest(*previous, predecessor.head.digest, commit, lease,
|
||||
Codec::tick(state), build, payload->size(), *digest);
|
||||
if (!manifest)
|
||||
const BatchDigests digests{trace->size(), *trace_digest,
|
||||
static_cast<std::uint32_t>(decisions.size()),
|
||||
decisions.empty() ? Sha256Digest{} : *decisions_digest};
|
||||
auto manifest =
|
||||
successor_manifest(*previous, predecessor.head.digest, commit, lease, batch.end_tick(),
|
||||
build, payload->size(), *payload_digest, digests);
|
||||
auto bytes = manifest ? encode_manifest(*manifest)
|
||||
: std::expected<std::vector<std::uint8_t>, CheckpointError>{
|
||||
std::unexpected{CheckpointError::format}};
|
||||
if (!bytes || !Codec::restore(*payload, *manifest, budget))
|
||||
return std::unexpected{fail(PublicationError::candidate)};
|
||||
auto bytes = stage<Codec>(objects, *payload, *manifest, budget);
|
||||
if (!bytes)
|
||||
return std::unexpected{bytes.error()};
|
||||
auto published = publish_region_checkpoint(db, *bytes, timeout);
|
||||
return confirm(published, std::move(*bytes), *manifest);
|
||||
if (auto stored = put_verified(objects, *payload, {*payload_digest, payload->size()}); !stored)
|
||||
return std::unexpected{stored.error()};
|
||||
if (auto stored = put_verified(objects, *trace, {*trace_digest, trace->size()}); !stored)
|
||||
return std::unexpected{stored.error()};
|
||||
|
||||
PreparedCommit prepared{previous->key, commit, *bytes, *decision_bytes};
|
||||
auto published = publish_region_checkpoint(db, *bytes, *decision_bytes, timeout);
|
||||
auto committed = confirm(published, std::move(*bytes), *manifest, std::move(prepared));
|
||||
if (!committed)
|
||||
return std::unexpected{committed.error()};
|
||||
return PublishedBatch{std::move(*committed), decisions};
|
||||
}
|
||||
|
||||
template <typename Codec>
|
||||
std::expected<typename Codec::Recovered, RecoveryFailure>
|
||||
recover(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
|
||||
std::span<const Sha256Digest> retained_builds, std::size_t budget, milliseconds timeout) {
|
||||
const auto store_failure = [](const LeaseStoreFailure& f) {
|
||||
return std::unexpected{RecoveryFailure{f.kind == LeaseStoreError::not_provisioned
|
||||
? RecoveryError::not_provisioned
|
||||
: RecoveryError::store,
|
||||
f}};
|
||||
};
|
||||
auto committed = load_region_checkpoint(db, key, timeout);
|
||||
if (!committed)
|
||||
return std::unexpected{
|
||||
RecoveryFailure{committed.error().kind == LeaseStoreError::not_provisioned
|
||||
? RecoveryError::not_provisioned
|
||||
: RecoveryError::store,
|
||||
committed.error()}};
|
||||
return store_failure(committed.error());
|
||||
const auto fail_with = [](RecoveryError kind) {
|
||||
return std::unexpected{RecoveryFailure{kind, {}}};
|
||||
};
|
||||
@@ -187,7 +278,14 @@ recover(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
|
||||
auto state = Codec::restore(*payload, *manifest, budget);
|
||||
if (!state)
|
||||
return fail_with(RecoveryError::payload);
|
||||
return typename Codec::Recovered{std::move(*committed), *manifest, std::move(*state)};
|
||||
// Durable permanent outcomes must agree with the restored state they finalized.
|
||||
auto decisions = load_region_decisions(db, key, timeout);
|
||||
if (!decisions)
|
||||
return store_failure(decisions.error());
|
||||
if (!Codec::reconcile(**state, *decisions, head))
|
||||
return fail_with(RecoveryError::history);
|
||||
return typename Codec::Recovered{std::move(*committed), *manifest, std::move(*state),
|
||||
std::move(*decisions)};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@@ -198,12 +296,14 @@ std::expected<CommittedCheckpoint, PublicationFailure> provision_environmental_r
|
||||
return provision<EnvironmentalCodec>(owner, objects, key, frame, opening, commit, build,
|
||||
restore_budget, timeout);
|
||||
}
|
||||
std::expected<CommittedCheckpoint, PublicationFailure> publish_environmental_checkpoint(
|
||||
Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
|
||||
const CommittedCheckpoint& predecessor, const processes::EnvironmentalCommunity& state,
|
||||
Uuid commit, const Sha256Digest& build, std::size_t restore_budget, milliseconds timeout) {
|
||||
return publish<EnvironmentalCodec>(db, objects, lease, predecessor, state, commit, build,
|
||||
restore_budget, timeout);
|
||||
std::expected<PublishedBatch, PublicationFailure>
|
||||
publish_environmental_batch(Postgres& db, const FilesystemObjectStore& objects,
|
||||
const LeaseGrant& lease, const CommittedCheckpoint& predecessor,
|
||||
const RegionBatch<processes::EnvironmentalCommunity>& batch,
|
||||
Uuid commit, const Sha256Digest& build, std::size_t restore_budget,
|
||||
milliseconds timeout) {
|
||||
return publish_batch<EnvironmentalCodec>(db, objects, lease, predecessor, batch, commit, build,
|
||||
restore_budget, timeout);
|
||||
}
|
||||
std::expected<RecoveredCheckpoint, RecoveryFailure>
|
||||
recover_environmental_checkpoint(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
|
||||
@@ -220,12 +320,13 @@ provision_lineage_region(Postgres& owner, const FilesystemObjectStore& objects,
|
||||
return provision<LineageCodec>(owner, objects, key, frame, opening, commit, build,
|
||||
restore_budget, timeout);
|
||||
}
|
||||
std::expected<CommittedCheckpoint, PublicationFailure> publish_lineage_checkpoint(
|
||||
Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
|
||||
const CommittedCheckpoint& predecessor, const processes::LineageRegion& state, Uuid commit,
|
||||
const Sha256Digest& build, std::size_t restore_budget, milliseconds timeout) {
|
||||
return publish<LineageCodec>(db, objects, lease, predecessor, state, commit, build,
|
||||
restore_budget, timeout);
|
||||
std::expected<PublishedBatch, PublicationFailure>
|
||||
publish_lineage_batch(Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
|
||||
const CommittedCheckpoint& predecessor,
|
||||
const RegionBatch<processes::LineageRegion>& batch, Uuid commit,
|
||||
const Sha256Digest& build, std::size_t restore_budget, milliseconds timeout) {
|
||||
return publish_batch<LineageCodec>(db, objects, lease, predecessor, batch, commit, build,
|
||||
restore_budget, timeout);
|
||||
}
|
||||
std::expected<RecoveredLineageCheckpoint, RecoveryFailure>
|
||||
recover_lineage_checkpoint(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
|
||||
@@ -233,4 +334,14 @@ recover_lineage_checkpoint(Postgres& db, const FilesystemObjectStore& objects, R
|
||||
std::size_t restore_budget, milliseconds timeout) {
|
||||
return recover<LineageCodec>(db, objects, key, retained_builds, restore_budget, timeout);
|
||||
}
|
||||
|
||||
std::expected<ResolvedCommit, LeaseStoreFailure>
|
||||
resolve_prepared(Postgres& db, const PreparedCommit& prepared, milliseconds timeout) {
|
||||
return resolve_region_commit(db, prepared.key, prepared.commit, prepared.manifest,
|
||||
prepared.decisions, timeout);
|
||||
}
|
||||
std::expected<RegionHead, LeaseStoreFailure>
|
||||
retry_prepared(Postgres& db, const PreparedCommit& prepared, milliseconds timeout) {
|
||||
return publish_region_checkpoint(db, prepared.manifest, prepared.decisions, timeout);
|
||||
}
|
||||
} // namespace evo::persistence
|
||||
|
||||
@@ -0,0 +1,226 @@
|
||||
#include "evo/persistence/region_batch.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
namespace evo::persistence {
|
||||
namespace {
|
||||
constexpr std::uint64_t trace_magic = 0x314352544F564541ULL; // "AEVOTRC1"
|
||||
constexpr std::uint32_t trace_format = 1;
|
||||
constexpr std::uint64_t max_trace_ticks = 64;
|
||||
|
||||
template <std::size_t N> bool zero(const std::array<std::uint8_t, N>& bytes) {
|
||||
return std::ranges::all_of(bytes, [](std::uint8_t b) { return b == 0; });
|
||||
}
|
||||
// Fills an exactly sized buffer; callers check the final cursor.
|
||||
class Writer {
|
||||
public:
|
||||
explicit Writer(std::vector<std::uint8_t>& out) : out_(out) {}
|
||||
void u(std::uint64_t value, unsigned width) {
|
||||
for (unsigned i = 0; i < width; ++i, value >>= 8U)
|
||||
out_[at_++] = static_cast<std::uint8_t>(value & 0xFFU);
|
||||
}
|
||||
template <std::size_t N> void bytes(const std::array<std::uint8_t, N>& value) {
|
||||
std::ranges::copy(value, out_.begin() + static_cast<std::ptrdiff_t>(at_));
|
||||
at_ += N;
|
||||
}
|
||||
template <std::size_t N> void all(const std::array<std::uint64_t, N>& values) {
|
||||
for (const auto v : values)
|
||||
u(v, 8);
|
||||
}
|
||||
[[nodiscard]] std::size_t at() const noexcept {
|
||||
return at_;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::uint8_t>& out_;
|
||||
std::size_t at_{};
|
||||
};
|
||||
class Reader {
|
||||
public:
|
||||
explicit Reader(std::span<const std::uint8_t> in) : in_(in) {}
|
||||
std::uint64_t u(unsigned width) {
|
||||
std::uint64_t value{};
|
||||
for (unsigned i = 0; i < width; ++i)
|
||||
value |= static_cast<std::uint64_t>(in_[at_ + i]) << (8U * i);
|
||||
at_ += width;
|
||||
return value;
|
||||
}
|
||||
template <std::size_t N> std::array<std::uint8_t, N> bytes() {
|
||||
std::array<std::uint8_t, N> value{};
|
||||
std::copy_n(in_.begin() + static_cast<std::ptrdiff_t>(at_), N, value.begin());
|
||||
at_ += N;
|
||||
return value;
|
||||
}
|
||||
template <std::size_t N> void all(std::array<std::uint64_t, N>& values) {
|
||||
for (auto& v : values)
|
||||
v = u(8);
|
||||
}
|
||||
|
||||
private:
|
||||
std::span<const std::uint8_t> in_;
|
||||
std::size_t at_{};
|
||||
};
|
||||
|
||||
bool valid(const TraceHeader& h) noexcept {
|
||||
return h.profile != 0 && h.key.world != 0 && h.key.region != 0 && !zero(h.predecessor_commit) &&
|
||||
!zero(h.predecessor_digest) && h.end_tick > h.begin_tick &&
|
||||
h.end_tick - h.begin_tick <= max_trace_ticks;
|
||||
}
|
||||
bool contiguous(const TraceHeader& h, std::span<const TraceStep> steps) noexcept {
|
||||
if (steps.size() != h.end_tick - h.begin_tick)
|
||||
return false;
|
||||
for (std::size_t i = 0; i < steps.size(); ++i)
|
||||
if (steps[i].report.tick != h.begin_tick + i + 1)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<std::vector<std::uint8_t>, BatchError>
|
||||
encode_trace(const TraceHeader& h, std::span<const TraceStep> steps) {
|
||||
if (!valid(h) || !contiguous(h, steps))
|
||||
return std::unexpected{BatchError::format};
|
||||
std::vector<std::uint8_t> out(trace_bytes_for(steps.size()));
|
||||
Writer w{out};
|
||||
w.u(trace_magic, 8);
|
||||
w.u(trace_format, 4);
|
||||
w.u(h.profile, 4);
|
||||
w.u(h.key.world, 8);
|
||||
w.u(h.key.region, 8);
|
||||
w.bytes(h.predecessor_commit);
|
||||
w.bytes(h.predecessor_digest);
|
||||
w.u(h.begin_tick, 8);
|
||||
w.u(h.end_tick, 8);
|
||||
for (const auto& s : steps) {
|
||||
const auto& in = s.input;
|
||||
const auto& r = s.report;
|
||||
w.u(in.temperature_mK, 8);
|
||||
w.u(in.source_enabled, 8);
|
||||
w.all(in.bite);
|
||||
w.all(in.terminal);
|
||||
for (const auto v :
|
||||
{r.tick, r.captured, r.retained, r.capture_heat, r.grown, r.unmet, r.source_status})
|
||||
w.u(v, 8);
|
||||
w.all(r.assimilated);
|
||||
w.all(r.waste);
|
||||
w.u(r.terminal_transition, 8);
|
||||
w.u(r.consumer_transition, 8);
|
||||
w.all(r.decomposed);
|
||||
}
|
||||
if (w.at() != out.size())
|
||||
return std::unexpected{BatchError::format};
|
||||
return out;
|
||||
}
|
||||
|
||||
std::expected<DecodedTrace, BatchError> decode_trace(std::span<const std::uint8_t> bytes) {
|
||||
if (bytes.size() < trace_header_bytes)
|
||||
return std::unexpected{BatchError::format};
|
||||
Reader r{bytes};
|
||||
if (r.u(8) != trace_magic || r.u(4) != trace_format)
|
||||
return std::unexpected{BatchError::format};
|
||||
DecodedTrace t;
|
||||
auto& h = t.header;
|
||||
h.profile = static_cast<std::uint32_t>(r.u(4));
|
||||
h.key.world = r.u(8);
|
||||
h.key.region = r.u(8);
|
||||
h.predecessor_commit = r.bytes<16>();
|
||||
h.predecessor_digest = r.bytes<32>();
|
||||
h.begin_tick = r.u(8);
|
||||
h.end_tick = r.u(8);
|
||||
// Size is fixed by the header before any step is allocated.
|
||||
if (!valid(h) || bytes.size() != trace_bytes_for(h.end_tick - h.begin_tick))
|
||||
return std::unexpected{BatchError::format};
|
||||
t.steps.resize(static_cast<std::size_t>(h.end_tick - h.begin_tick));
|
||||
for (auto& s : t.steps) {
|
||||
auto& in = s.input;
|
||||
auto& rep = s.report;
|
||||
in.temperature_mK = r.u(8);
|
||||
in.source_enabled = r.u(8);
|
||||
r.all(in.bite);
|
||||
r.all(in.terminal);
|
||||
for (auto* v : {&rep.tick, &rep.captured, &rep.retained, &rep.capture_heat, &rep.grown,
|
||||
&rep.unmet, &rep.source_status})
|
||||
*v = r.u(8);
|
||||
r.all(rep.assimilated);
|
||||
r.all(rep.waste);
|
||||
rep.terminal_transition = r.u(8);
|
||||
rep.consumer_transition = r.u(8);
|
||||
r.all(rep.decomposed);
|
||||
}
|
||||
if (!contiguous(h, t.steps))
|
||||
return std::unexpected{BatchError::format};
|
||||
return t;
|
||||
}
|
||||
|
||||
namespace {
|
||||
bool valid(std::span<const BatchDecision> d, std::uint64_t begin, std::uint64_t end) noexcept {
|
||||
if (d.size() > max_batch_decisions)
|
||||
return false;
|
||||
for (std::size_t i = 0; i < d.size(); ++i) {
|
||||
const auto& x = d[i];
|
||||
if (x.outcome != Outcome::death ||
|
||||
(x.actor != Actor::producer && x.actor != Actor::consumer) || x.tick <= begin ||
|
||||
x.tick > end)
|
||||
return false;
|
||||
for (std::size_t j = 0; j < i; ++j)
|
||||
if (d[j].actor == x.actor)
|
||||
return false; // Terminal: at most one death per actor.
|
||||
if (i > 0 &&
|
||||
(d[i - 1].tick > x.tick || (d[i - 1].tick == x.tick && d[i - 1].actor >= x.actor)))
|
||||
return false; // Canonical order (tick, actor).
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<std::vector<std::uint8_t>, BatchError>
|
||||
encode_decisions(std::span<const BatchDecision> decisions, std::uint64_t begin_tick,
|
||||
std::uint64_t end_tick) {
|
||||
if (!valid(decisions, begin_tick, end_tick))
|
||||
return std::unexpected{BatchError::format};
|
||||
std::vector<std::uint8_t> out(decisions.size() * decision_bytes);
|
||||
Writer w{out};
|
||||
for (const auto& d : decisions) {
|
||||
w.u(d.tick, 8);
|
||||
w.u(static_cast<std::uint32_t>(d.actor), 4);
|
||||
w.u(static_cast<std::uint32_t>(d.outcome), 4);
|
||||
w.u(d.organism, 8);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::expected<std::vector<BatchDecision>, BatchError>
|
||||
decode_decisions(std::span<const std::uint8_t> bytes, std::uint64_t begin_tick,
|
||||
std::uint64_t end_tick) {
|
||||
if (bytes.size() % decision_bytes != 0 || bytes.size() > max_batch_decisions * decision_bytes)
|
||||
return std::unexpected{BatchError::format};
|
||||
std::vector<BatchDecision> out(bytes.size() / decision_bytes);
|
||||
Reader r{bytes};
|
||||
for (auto& d : out) {
|
||||
d.tick = r.u(8);
|
||||
const auto actor = r.u(4);
|
||||
const auto outcome = r.u(4);
|
||||
// Unknown values are refused, never truncated onto known ones.
|
||||
if (actor > 1 || outcome != 1)
|
||||
return std::unexpected{BatchError::format};
|
||||
d.actor = static_cast<Actor>(actor);
|
||||
d.outcome = Outcome::death;
|
||||
d.organism = r.u(8);
|
||||
}
|
||||
if (!valid(out, begin_tick, end_tick))
|
||||
return std::unexpected{BatchError::format};
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<BatchDecision> derive_decisions(std::span<const TraceStep> steps,
|
||||
std::uint64_t producer_organism,
|
||||
std::uint64_t consumer_organism) {
|
||||
std::vector<BatchDecision> out;
|
||||
for (const auto& s : steps) {
|
||||
if (s.report.terminal_transition == 1)
|
||||
out.push_back({s.report.tick, Actor::producer, Outcome::death, producer_organism});
|
||||
if (s.report.consumer_transition == 1)
|
||||
out.push_back({s.report.tick, Actor::consumer, Outcome::death, consumer_organism});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
} // namespace evo::persistence
|
||||
@@ -5,7 +5,6 @@
|
||||
namespace evo::persistence {
|
||||
namespace {
|
||||
constexpr std::uint64_t manifest_magic = 0x314D43524F564541ULL; // "AEVORCM1"
|
||||
constexpr std::uint32_t manifest_version = 1;
|
||||
constexpr std::uint64_t maximum = ~std::uint64_t{0};
|
||||
|
||||
template <std::size_t N> bool zero(const std::array<std::uint8_t, N>& bytes) {
|
||||
@@ -58,12 +57,23 @@ bool structural(const CheckpointManifest& m) noexcept {
|
||||
m.tick_microseconds == 0 || zero(m.build) || m.payload_bytes == 0 ||
|
||||
m.payload_bytes > limit || zero(m.payload_sha256))
|
||||
return false;
|
||||
const auto& b = m.batch;
|
||||
if (m.manifest_version != 1 && m.manifest_version != 2)
|
||||
return false;
|
||||
const bool no_batch = b == BatchDigests{};
|
||||
if (m.sequence == 0)
|
||||
return zero(m.predecessor_commit) && zero(m.predecessor_digest) && m.begin_tick == 0 &&
|
||||
m.end_tick == 0 && zero(m.holder) && m.epoch == 0;
|
||||
return !zero(m.predecessor_commit) && m.predecessor_commit != m.commit &&
|
||||
!zero(m.predecessor_digest) && !zero(m.holder) && m.epoch != 0 &&
|
||||
m.end_tick > m.begin_tick && m.end_tick - m.begin_tick <= max_checkpoint_ticks;
|
||||
m.end_tick == 0 && zero(m.holder) && m.epoch == 0 && no_batch;
|
||||
const bool link = !zero(m.predecessor_commit) && m.predecessor_commit != m.commit &&
|
||||
!zero(m.predecessor_digest) && !zero(m.holder) && m.epoch != 0 &&
|
||||
m.end_tick > m.begin_tick &&
|
||||
m.end_tick - m.begin_tick <= max_checkpoint_ticks;
|
||||
if (m.manifest_version == 1)
|
||||
return link && no_batch;
|
||||
// v2: the trace has exactly one step per tick; decisions are bounded.
|
||||
return link && b.trace_bytes == trace_bytes_for(m.end_tick - m.begin_tick) &&
|
||||
!zero(b.trace_sha256) && b.decision_count <= max_batch_decisions &&
|
||||
(b.decision_count == 0) == zero(b.decisions_sha256);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@@ -78,13 +88,13 @@ bool supported(const CheckpointManifest& m) noexcept {
|
||||
|
||||
std::expected<std::vector<std::uint8_t>, CheckpointError>
|
||||
encode_manifest(const CheckpointManifest& m) {
|
||||
if (!structural(m))
|
||||
if (!structural(m) || m.manifest_version != 2)
|
||||
return std::unexpected{CheckpointError::format};
|
||||
std::vector<std::uint8_t> out;
|
||||
out.reserve(checkpoint_manifest_bytes);
|
||||
Writer w{out};
|
||||
w.u(manifest_magic, 8);
|
||||
w.u(manifest_version, 4);
|
||||
w.u(m.manifest_version, 4);
|
||||
w.u(static_cast<std::uint8_t>(m.profile), 4);
|
||||
w.u(m.key.world, 8);
|
||||
w.u(m.key.region, 8);
|
||||
@@ -108,19 +118,28 @@ encode_manifest(const CheckpointManifest& m) {
|
||||
w.u(static_cast<std::uint8_t>(m.durability), 4);
|
||||
w.u(m.payload_bytes, 8);
|
||||
w.bytes(m.payload_sha256);
|
||||
w.u(m.batch.trace_bytes, 8);
|
||||
w.bytes(m.batch.trace_sha256);
|
||||
w.u(m.batch.decision_count, 4);
|
||||
w.u(0, 4);
|
||||
w.bytes(m.batch.decisions_sha256);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::expected<CheckpointManifest, CheckpointError>
|
||||
decode_manifest(std::span<const std::uint8_t> bytes) {
|
||||
if (bytes.size() != checkpoint_manifest_bytes)
|
||||
if (bytes.size() < 12)
|
||||
return std::unexpected{CheckpointError::format};
|
||||
Reader r{bytes};
|
||||
if (r.u(8) != manifest_magic)
|
||||
return std::unexpected{CheckpointError::format};
|
||||
if (r.u(4) != manifest_version)
|
||||
return std::unexpected{CheckpointError::unsupported};
|
||||
CheckpointManifest m;
|
||||
m.manifest_version = static_cast<std::uint32_t>(r.u(4));
|
||||
if (m.manifest_version != 1 && m.manifest_version != 2)
|
||||
return std::unexpected{CheckpointError::unsupported};
|
||||
if (bytes.size() !=
|
||||
(m.manifest_version == 1 ? checkpoint_manifest_v1_bytes : checkpoint_manifest_bytes))
|
||||
return std::unexpected{CheckpointError::format};
|
||||
const auto profile = r.u(4);
|
||||
m.profile =
|
||||
profile <= 0xFFU ? static_cast<CheckpointProfile>(profile) : CheckpointProfile::unknown;
|
||||
@@ -148,6 +167,14 @@ decode_manifest(std::span<const std::uint8_t> bytes) {
|
||||
durability <= 0xFFU ? static_cast<DurabilityPolicy>(durability) : DurabilityPolicy::unknown;
|
||||
m.payload_bytes = r.u(8);
|
||||
m.payload_sha256 = r.bytes<32>();
|
||||
if (m.manifest_version == 2) {
|
||||
m.batch.trace_bytes = r.u(8);
|
||||
m.batch.trace_sha256 = r.bytes<32>();
|
||||
m.batch.decision_count = static_cast<std::uint32_t>(r.u(4));
|
||||
if (r.u(4) != 0)
|
||||
return std::unexpected{CheckpointError::format}; // Reserved.
|
||||
m.batch.decisions_sha256 = r.bytes<32>();
|
||||
}
|
||||
if (!structural(m))
|
||||
return std::unexpected{CheckpointError::format};
|
||||
return m;
|
||||
@@ -176,7 +203,8 @@ genesis_manifest(RegionKey key, std::uint64_t frame, Uuid commit, std::uint64_t
|
||||
std::expected<CheckpointManifest, CheckpointError>
|
||||
successor_manifest(const CheckpointManifest& p, const Sha256Digest& predecessor_digest, Uuid commit,
|
||||
const LeaseGrant& lease, std::uint64_t end_tick, const Sha256Digest& build,
|
||||
std::uint64_t payload_bytes, const Sha256Digest& payload_sha256) {
|
||||
std::uint64_t payload_bytes, const Sha256Digest& payload_sha256,
|
||||
const BatchDigests& batch) {
|
||||
// This build writes only its own versions; a change requires a migration (M7.9).
|
||||
if (!supported(p))
|
||||
return std::unexpected{CheckpointError::unsupported};
|
||||
@@ -184,6 +212,8 @@ successor_manifest(const CheckpointManifest& p, const Sha256Digest& predecessor_
|
||||
end_tick <= p.end_tick || end_tick - p.end_tick > max_checkpoint_ticks)
|
||||
return std::unexpected{CheckpointError::chain};
|
||||
CheckpointManifest m = p;
|
||||
m.manifest_version = 2;
|
||||
m.batch = batch;
|
||||
m.commit = commit;
|
||||
m.sequence = p.sequence + 1;
|
||||
m.predecessor_commit = p.commit;
|
||||
|
||||
@@ -32,6 +32,10 @@ LeaseStoreFailure classify(const PgFailure& f) {
|
||||
kind = LeaseStoreError::unsupported;
|
||||
else if (state == "EVL07")
|
||||
kind = LeaseStoreError::commit_conflict;
|
||||
else if (state == "EVL08")
|
||||
kind = LeaseStoreError::outcome_final;
|
||||
else if (state == "EVL09")
|
||||
kind = LeaseStoreError::immutable;
|
||||
return {kind, f};
|
||||
}
|
||||
LeaseStoreFailure malformed(bool mutation) {
|
||||
@@ -214,10 +218,10 @@ load_region_checkpoint(Postgres& db, RegionKey key, milliseconds timeout) {
|
||||
|
||||
std::expected<RegionHead, LeaseStoreFailure>
|
||||
publish_region_checkpoint(Postgres& db, std::span<const std::uint8_t> manifest,
|
||||
milliseconds timeout) {
|
||||
const std::array<std::string, 1> p{bytea(manifest)};
|
||||
auto rows =
|
||||
db.query("SELECT * FROM evolution.publish_region_checkpoint($1::bytea)", p, timeout);
|
||||
std::span<const std::uint8_t> decisions, milliseconds timeout) {
|
||||
const std::array<std::string, 2> p{bytea(manifest), bytea(decisions)};
|
||||
auto rows = db.query("SELECT * FROM evolution.publish_region_checkpoint($1::bytea, $2::bytea)",
|
||||
p, timeout);
|
||||
if (!rows)
|
||||
return std::unexpected{classify(rows.error())};
|
||||
auto head = parse_head(*rows);
|
||||
@@ -225,4 +229,97 @@ publish_region_checkpoint(Postgres& db, std::span<const std::uint8_t> manifest,
|
||||
return std::unexpected{malformed(true)};
|
||||
return *head;
|
||||
}
|
||||
|
||||
std::expected<ResolvedCommit, LeaseStoreFailure>
|
||||
resolve_region_commit(Postgres& db, RegionKey key, const Uuid& commit,
|
||||
std::span<const std::uint8_t> manifest,
|
||||
std::span<const std::uint8_t> decisions, milliseconds timeout) {
|
||||
const std::array<std::string, 5> p{std::to_string(key.world), std::to_string(key.region),
|
||||
uuid_text(commit), bytea(manifest), bytea(decisions)};
|
||||
auto rows = db.query("SELECT * FROM evolution.resolve_region_commit($1::evolution.u64, "
|
||||
"$2::evolution.u64, $3::uuid, $4::bytea, $5::bytea)",
|
||||
p, timeout);
|
||||
if (!rows)
|
||||
return std::unexpected{classify(rows.error())};
|
||||
if (rows->size() != 1 || (*rows)[0].size() != 6)
|
||||
return std::unexpected{malformed(false)};
|
||||
const auto& status_text = (*rows)[0][0];
|
||||
if (!status_text)
|
||||
return std::unexpected{malformed(false)};
|
||||
const auto status = *status_text;
|
||||
if (status == "absent")
|
||||
return ResolvedCommit{Resolution::absent, {}};
|
||||
if (status == "conflict")
|
||||
return ResolvedCommit{Resolution::conflict, {}};
|
||||
const PgRows receipt{
|
||||
std::vector<std::optional<std::string>>((*rows)[0].begin() + 1, (*rows)[0].end())};
|
||||
auto head = status == "committed" ? parse_head(receipt) : std::nullopt;
|
||||
if (!head)
|
||||
return std::unexpected{malformed(false)};
|
||||
return ResolvedCommit{Resolution::committed, *head};
|
||||
}
|
||||
|
||||
namespace {
|
||||
std::expected<std::vector<CommittedDecision>, LeaseStoreFailure>
|
||||
decisions_from(const PgRows& rows) {
|
||||
std::vector<CommittedDecision> out;
|
||||
out.reserve(rows.size());
|
||||
for (const auto& r : rows) {
|
||||
if (r.size() != 5)
|
||||
return std::unexpected{malformed(false)};
|
||||
const auto sequence = parse_integer<std::uint64_t>(r[0]);
|
||||
const auto tick = parse_integer<std::uint64_t>(r[1]);
|
||||
const auto actor = parse_integer<std::uint32_t>(r[2]);
|
||||
const auto outcome = parse_integer<std::uint32_t>(r[3]);
|
||||
const auto organism = parse_integer<std::uint64_t>(r[4]);
|
||||
if (!sequence || !tick || !actor || !outcome || !organism || *actor > 1 || *outcome != 1)
|
||||
return std::unexpected{malformed(false)};
|
||||
out.push_back({*sequence, {*tick, static_cast<Actor>(*actor), Outcome::death, *organism}});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<std::vector<CommittedDecision>, LeaseStoreFailure>
|
||||
load_region_decisions(Postgres& db, RegionKey key, milliseconds timeout) {
|
||||
const std::array<std::string, 2> p{std::to_string(key.world), std::to_string(key.region)};
|
||||
auto rows = db.query(
|
||||
"SELECT * FROM evolution.load_region_decisions($1::evolution.u64, $2::evolution.u64)", p,
|
||||
timeout);
|
||||
if (!rows)
|
||||
return std::unexpected{classify(rows.error())};
|
||||
return decisions_from(*rows);
|
||||
}
|
||||
|
||||
std::expected<std::vector<CommittedDecision>, LeaseStoreFailure>
|
||||
pending_region_outbox(Postgres& db, RegionKey key, std::uint32_t limit, milliseconds timeout) {
|
||||
const std::array<std::string, 3> p{std::to_string(key.world), std::to_string(key.region),
|
||||
std::to_string(limit)};
|
||||
auto rows = db.query("SELECT * FROM evolution.pending_region_outbox($1::evolution.u64, "
|
||||
"$2::evolution.u64, $3::integer)",
|
||||
p, timeout);
|
||||
if (!rows)
|
||||
return std::unexpected{classify(rows.error())};
|
||||
return decisions_from(*rows);
|
||||
}
|
||||
|
||||
std::expected<bool, LeaseStoreFailure> acknowledge_region_outbox(Postgres& db, RegionKey key,
|
||||
const BatchDecision& d,
|
||||
milliseconds timeout) {
|
||||
const std::array<std::string, 5> p{std::to_string(key.world), std::to_string(key.region),
|
||||
std::to_string(static_cast<std::uint32_t>(d.outcome)),
|
||||
std::to_string(static_cast<std::uint32_t>(d.actor)),
|
||||
std::to_string(d.tick)};
|
||||
auto rows = db.query("SELECT evolution.acknowledge_region_outbox($1::evolution.u64, "
|
||||
"$2::evolution.u64, $3::integer, $4::integer, $5::evolution.u64)::text",
|
||||
p, timeout);
|
||||
if (!rows)
|
||||
return std::unexpected{classify(rows.error())};
|
||||
if (rows->size() != 1 || (*rows)[0].size() != 1)
|
||||
return std::unexpected{malformed(true)};
|
||||
const auto& marked = (*rows)[0][0];
|
||||
if (!marked)
|
||||
return std::unexpected{malformed(true)};
|
||||
return *marked == "true";
|
||||
}
|
||||
} // namespace evo::persistence
|
||||
|
||||
Reference in New Issue
Block a user