M10.9d: batch durable action lookups (one per poll, one per commit)
Foundation / checks (deploy) (push) Successful in 3m27s
Foundation / checks (ecs) (push) Successful in 1m57s
Foundation / checks (client) (push) Successful in 12m41s
Foundation / checks (network) (push) Successful in 13m33s
Foundation / checks (tsan) (push) Successful in 7m54s
Foundation / checks (checks) (push) Successful in 32m56s
Foundation / checks (postgres) (push) Successful in 46m10s
Foundation / postgres-tools (push) Successful in 18s
Foundation / image (push) Successful in 2m33s
Foundation / server-image (push) Successful in 4m24s

The fourth dev drill showed a synchronous PostgreSQL round trip per action on the
simulation thread: at admission (uniqueness) and again at commit (first-record rule).
About 12 lookups landed on each commit tick (commit ticks 58-70 ms, session p99 30-39 ms).

- ActionLedger::find_all; persistence::find_region_actions applies the schema v11
  find_region_action to many pairs in one statement (no migration).
- IntentAdmission::admit/verify: actions only the ledger can refuse wait for one verify
  per poll; receipts still go out in the same poll. committed() asks once per commit.
- Tests: deferred_actions (network_control), find_region_actions and find_all (Postgres).
- ADR-0097 fourth run, ADR-0092 amendment, VALIDATION; refresh the dev session window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-08 21:35:43 -04:00
co-authored by Claude Opus 5.5
parent 3ee22694cc
commit 07d12a4d82
13 changed files with 430 additions and 45 deletions
+137 -32
View File
@@ -108,19 +108,15 @@ IntentionReceipt IntentAdmission::intention(std::uint64_t session, const Session
return r;
}
ActionReceipt IntentAdmission::action(std::uint64_t session, const SessionAction& m,
const AdmissionFacts& facts) {
ActionReceipt IntentAdmission::screen(Session& s, const SessionAction& m,
const AdmissionFacts& facts, bool& check) {
ActionReceipt r{m.action_id, AdmitStatus::admitted, facts.server_tick, m.tick};
auto* s = find(session);
if (!s) {
r.status = AdmitStatus::unknown_session;
return r;
}
count_window(*s, facts.server_tick);
const bool over = s->action_attempts >= limits_.max_actions_per_window;
check = false;
count_window(s, facts.server_tick);
const bool over = s.action_attempts >= limits_.max_actions_per_window;
if (!over)
++s->action_attempts;
const auto& account = s->account;
++s.action_attempts;
const auto& account = s.account;
const auto known_action = std::ranges::find_if(actions_, [&](const Action& a) {
return a.carried.account == account && a.carried.action_id == m.action_id;
});
@@ -129,7 +125,7 @@ ActionReceipt IntentAdmission::action(std::uint64_t session, const SessionAction
});
if (over)
r.status = AdmitStatus::rate_limited;
else if (m.session != session)
else if (m.session != s.id)
r.status = AdmitStatus::unknown_session;
else if (!facts.ready)
r.status = AdmitStatus::not_ready;
@@ -144,24 +140,107 @@ ActionReceipt IntentAdmission::action(std::uint64_t session, const SessionAction
known_action != actions_.end() ? known_action->carried.tick : known_final->outcome.tick;
} else if (!in_window(m.tick, facts.server_tick, limits_.intent_window))
r.status = AdmitStatus::outside_window;
else if (unresolved_of(*s) >= limits_.max_unresolved)
else if (unresolved_of(s) >= limits_.max_unresolved)
r.status = AdmitStatus::too_many_unresolved;
else if (ledger_) {
// Committed earlier (another epoch, process or region): never admitted again.
const auto found = lookup(account, m.action_id);
if (found.state == Record::unavailable) {
r.status = AdmitStatus::not_ready; // Uniqueness cannot be confirmed now.
} else if (found.state == Record::committed) {
r.status = AdmitStatus::duplicate;
r.tick = found.outcome.tick;
remember(account, found.outcome);
}
}
if (r.status == AdmitStatus::admitted)
actions_.push_back({{account, m.action_id, m.tick, ActionKind::probe}, Stage::scheduled});
else
check = ledger_ != nullptr;
return r;
}
void IntentAdmission::decide(const AccountId& account, ActionReceipt& r, const Found& found) {
// Committed earlier (another epoch, process or region): never admitted again.
if (found.state == Record::unavailable) {
r.status = AdmitStatus::not_ready; // Uniqueness cannot be confirmed now.
} else if (found.state == Record::committed) {
r.status = AdmitStatus::duplicate;
r.tick = found.outcome.tick;
remember(account, found.outcome);
}
}
ActionReceipt IntentAdmission::action(std::uint64_t session, const SessionAction& m,
const AdmissionFacts& facts) {
auto* s = find(session);
if (!s)
return {m.action_id, AdmitStatus::unknown_session, facts.server_tick, m.tick};
bool check = false;
auto r = screen(*s, m, facts, check);
if (check)
decide(s->account, r, lookup(s->account, m.action_id));
if (r.status == AdmitStatus::admitted)
actions_.push_back(
{{s->account, m.action_id, m.tick, ActionKind::probe}, Stage::scheduled});
return r;
}
std::optional<ActionReceipt> IntentAdmission::admit(std::uint64_t session, const SessionAction& m,
const AdmissionFacts& facts) {
auto* s = find(session);
if (!s)
return ActionReceipt{m.action_id, AdmitStatus::unknown_session, facts.server_tick, m.tick};
bool check = false;
const auto r = screen(*s, m, facts, check);
if (r.status != AdmitStatus::admitted)
return r;
// Held like an admitted action (duplicates and the unresolved limit see it), but never
// carried until `verify` confirms it.
actions_.push_back({{s->account, m.action_id, m.tick, ActionKind::probe},
check ? Stage::verifying : Stage::scheduled,
false,
session,
facts.epoch,
facts.server_tick});
if (check)
return std::nullopt;
return r;
}
std::vector<std::pair<std::uint64_t, ActionReceipt>>
IntentAdmission::verify(const AdmissionFacts& facts) {
std::vector<std::pair<std::uint64_t, ActionReceipt>> out;
std::vector<ActionKey> keys;
for (const auto& a : actions_)
if (a.stage == Stage::verifying)
keys.push_back({a.carried.account, a.carried.action_id});
if (keys.empty())
return out;
// Authority lost or moved on since admission: refused as admission would refuse it now.
const bool current = facts.ready;
std::expected<std::vector<std::optional<ActionOutcome>>, ActionLedgerError> answers =
std::unexpected(ActionLedgerError::unavailable);
if (current && ledger_ != nullptr)
answers = ledger_->find_all(keys);
if (answers.has_value() && answers->size() != keys.size())
answers = std::unexpected(ActionLedgerError::unavailable);
std::size_t i = 0;
for (auto& a : actions_) {
if (a.stage != Stage::verifying)
continue;
const auto index = i++;
ActionReceipt r{a.carried.action_id, AdmitStatus::admitted, a.admitted_at, a.carried.tick};
if (!current) {
r.status = AdmitStatus::not_ready;
} else if (a.epoch != facts.epoch) {
r.status = AdmitStatus::stale_epoch;
} else {
Found found;
if (answers.has_value()) {
const auto& record = (*answers)[index];
found.state = record.has_value() ? Record::committed : Record::absent;
if (record.has_value())
found.outcome = *record;
}
decide(a.carried.account, r, found);
}
if (r.status == AdmitStatus::admitted)
a.stage = Stage::scheduled;
out.emplace_back(a.session, r);
}
// Every refused one leaves (a duplicate is remembered among the finals by `decide`).
std::erase_if(actions_, [](const Action& a) { return a.stage == Stage::verifying; });
return out;
}
ActionOutcome IntentAdmission::query(std::uint64_t session, const ActionQuery& m,
const AdmissionFacts& facts) {
ActionOutcome o{m.action_id, ActionResult::unknown, 0, 0};
@@ -242,13 +321,26 @@ void IntentAdmission::remember(const AccountId& account, const ActionOutcome& ou
std::vector<AccountOutcome> IntentAdmission::committed(std::span<const CarriedAction> actions,
std::uint64_t commit_sequence) {
std::vector<AccountOutcome> out;
for (const auto& c : actions) {
// The durable record is the authority: an id carried by two batches (an account that
// moved regions with the action pending) keeps the record of the first commit. One
// request for the whole commit; unanswered, the host's own report stands.
std::expected<std::vector<std::optional<ActionOutcome>>, ActionLedgerError> records =
std::unexpected(ActionLedgerError::unavailable);
if (ledger_ != nullptr && !actions.empty()) {
std::vector<ActionKey> keys;
keys.reserve(actions.size());
for (const auto& c : actions)
keys.push_back({c.account, c.action_id});
records = ledger_->find_all(keys);
if (records.has_value() && records->size() != actions.size())
records = std::unexpected(ActionLedgerError::unavailable);
}
for (std::size_t i = 0; i < actions.size(); ++i) {
const auto& c = actions[i];
ActionOutcome o{c.action_id, ActionResult::committed_no_effect, c.tick, commit_sequence};
// The durable record is the authority: an id carried by two batches (an account that
// moved regions with the action pending) keeps the record of the first commit.
if (ledger_)
if (const auto found = lookup(c.account, c.action_id); found.state == Record::committed)
o = found.outcome;
if (records.has_value())
if (const auto& record = (*records)[i]; record.has_value())
o = *record;
std::erase_if(actions_, [&](const Action& a) {
return a.carried.account == c.account && a.carried.action_id == c.action_id;
});
@@ -297,6 +389,19 @@ std::vector<AccountOutcome> IntentAdmission::abandon() {
return out;
}
std::expected<std::vector<std::optional<ActionOutcome>>, ActionLedgerError>
ActionLedger::find_all(std::span<const ActionKey> keys) {
std::vector<std::optional<ActionOutcome>> out;
out.reserve(keys.size());
for (const auto& k : keys) {
auto found = find(k.account, k.action_id);
if (!found.has_value())
return std::unexpected(found.error());
out.push_back(*found);
}
return out;
}
IntentAdmission::Found IntentAdmission::lookup(const AccountId& account,
std::uint64_t action_id) const {
Found f;
+44
View File
@@ -353,6 +353,50 @@ find_region_action(Postgres& db, RegionKey key, const LeaseGrant& lease, const U
return std::optional<CommittedAction>{CommittedAction{*region, *tick, *sequence}};
}
std::expected<std::vector<std::optional<CommittedAction>>, LeaseStoreFailure>
find_region_actions(Postgres& db, RegionKey key, const LeaseGrant& lease,
std::span<const ActionRef> actions, milliseconds timeout) {
std::vector<std::optional<CommittedAction>> out(actions.size());
if (actions.empty())
return out;
// Array literals of canonical uuids and decimal ids: no quoting is needed.
std::string accounts = "{", ids = "{";
for (std::size_t i = 0; i < actions.size(); ++i) {
accounts += (i == 0 ? "" : ",") + uuid_text(actions[i].account);
ids += (i == 0 ? "" : ",") + std::to_string(actions[i].action_id);
}
accounts += "}";
ids += "}";
const std::array<std::string, 6> p{std::to_string(key.world),
std::to_string(key.region),
uuid_text(lease.holder),
std::to_string(lease.epoch),
accounts,
ids};
// The schema v11 function per pair (its lease check included); rows name their pair.
auto rows = db.query("SELECT x.n, f.action_region, f.action_tick, f.action_commit "
"FROM unnest($5::uuid[], $6::numeric[]) WITH ORDINALITY AS x(a, i, n) "
"CROSS JOIN LATERAL evolution.find_region_action($1::evolution.u64, "
"$2::evolution.u64, $3::uuid, $4::evolution.u64, x.a, "
"x.i::evolution.u64) AS f",
p, timeout);
if (!rows)
return std::unexpected{classify(rows.error())};
for (const auto& r : *rows) {
if (r.size() != 4)
return std::unexpected{malformed(false)};
const auto n = parse_integer<std::uint64_t>(r[0]);
const auto region = parse_integer<std::uint64_t>(r[1]);
const auto tick = parse_integer<std::uint64_t>(r[2]);
const auto sequence = parse_integer<std::uint64_t>(r[3]);
if (!n || *n == 0 || *n > actions.size() || out[*n - 1].has_value() || !region || !tick ||
!sequence)
return std::unexpected{malformed(false)};
out[*n - 1] = CommittedAction{*region, *tick, *sequence};
}
return out;
}
namespace {
std::expected<std::vector<CommittedDecision>, LeaseStoreFailure>
decisions_from(const PgRows& rows) {
@@ -25,4 +25,38 @@ PostgresActionLedger::find(const network::AccountId& account, std::uint64_t acti
network::ActionOutcome{action_id, network::ActionResult::committed_no_effect,
record.value().tick, record.value().commit_sequence}};
}
std::expected<std::vector<std::optional<network::ActionOutcome>>, network::ActionLedgerError>
PostgresActionLedger::find_all(std::span<const network::ActionKey> keys) {
const auto lease = lease_();
if (!lease)
return std::unexpected(network::ActionLedgerError::unavailable);
auto* db = db_->get();
if (!db)
return std::unexpected(network::ActionLedgerError::unavailable);
std::vector<persistence::ActionRef> refs;
refs.reserve(keys.size());
for (const auto& k : keys)
refs.push_back({k.account.bytes, k.action_id});
const auto found = persistence::find_region_actions(*db, key_, *lease, refs, timeout_);
if (!found.has_value()) {
const auto kind = found.error().kind;
if (kind == persistence::LeaseStoreError::store ||
kind == persistence::LeaseStoreError::unknown)
db_->broken(); // Reconnect next time.
return std::unexpected(network::ActionLedgerError::unavailable);
}
std::vector<std::optional<network::ActionOutcome>> out;
out.reserve(keys.size());
for (std::size_t i = 0; i < keys.size(); ++i) {
const auto& record = (*found)[i];
if (record.has_value())
out.emplace_back(network::ActionOutcome{keys[i].action_id,
network::ActionResult::committed_no_effect,
record->tick, record->commit_sequence});
else
out.emplace_back();
}
return out;
}
} // namespace evo::server
+17 -8
View File
@@ -504,6 +504,16 @@ struct RegionSessions::Impl {
markers.set_ghosts(ghosts);
}
void answer(Connection& c, const network::ActionReceipt& receipt) {
send(c, receipt);
// A duplicate of a final action: answer with the outcome as well.
if (receipt.status == network::AdmitStatus::duplicate)
if (const auto o = intents.query(c.session, {receipt.action_id}, admission());
o.result == network::ActionResult::committed_no_effect ||
o.result == network::ActionResult::not_committed)
send(c, o);
}
void deliver(const std::vector<network::AccountOutcome>& outcomes) {
for (const auto& o : outcomes)
if (const auto session = intents.session_of(o.account))
@@ -532,14 +542,9 @@ struct RegionSessions::Impl {
if (const auto* m = std::get_if<network::SessionRefresh>(&*decoded))
return refresh(c, *m, now_seconds, now);
if (const auto* m = std::get_if<network::SessionAction>(&*decoded)) {
const auto receipt = intents.action(c.session, *m, admission());
send(c, receipt);
// A duplicate of a final action: answer with the outcome as well.
if (receipt.status == network::AdmitStatus::duplicate)
if (const auto o = intents.query(c.session, {m->action_id}, admission());
o.result == network::ActionResult::committed_no_effect ||
o.result == network::ActionResult::not_committed)
send(c, o);
// A durable uniqueness check waits for this poll's single `verify` (M10.9d).
if (const auto receipt = intents.admit(c.session, *m, admission()))
answer(c, *receipt);
return;
}
if (const auto* m = std::get_if<network::ActionQuery>(&*decoded))
@@ -676,6 +681,10 @@ void RegionSessions::poll(const HostFacts& facts, std::uint64_t now_seconds,
m.datagram(*c, e.bytes, now);
}
}
// One ledger request for every action admitted by this poll's messages.
for (const auto& [session, receipt] : m.intents.verify(m.admission()))
if (auto* c = m.by_session(session); c && c->state == Impl::State::established)
m.answer(*c, receipt);
for (const auto& [session, watermark] : m.intents.simulated(m.admission()))
if (auto* c = m.by_session(session); c && c->state == Impl::State::established)
m.send_datagram(*c, watermark);