Admit v10 mutation choices through the cellular host and sessions
The region host checks a choice against the committed offer, reserves the successor for the next tick, and publishes it with that tick's batch; the owner's v10 session gets the receipt and final outcome. Schema v35 now refuses a checkpoint that skips a reserved choice, and the host reloads pending reservations after uncertainty, refusal or recovery (ADR-0121). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -289,6 +289,20 @@ bool RegionSessionClient::send_cellular_action(
|
||||
intake_target ? intake_target->second : 0, engulf});
|
||||
}
|
||||
|
||||
bool RegionSessionClient::send_cellular_mutation(std::uint64_t action_id, std::uint64_t tick,
|
||||
std::uint64_t life_identity,
|
||||
std::uint64_t event_index,
|
||||
std::uint64_t offer_tick,
|
||||
std::uint16_t operator_id) {
|
||||
auto& m = *impl_;
|
||||
return m.protocol_version == network::session_protocol_v10 &&
|
||||
m.state == RegionSessionState::established && m.welcome && action_id != 0 &&
|
||||
offer_tick != 0 && tick > offer_tick &&
|
||||
m.send(network::CellularMutationAction{m.welcome->session, m.welcome->epoch, action_id,
|
||||
tick, life_identity, event_index, offer_tick,
|
||||
operator_id});
|
||||
}
|
||||
|
||||
bool RegionSessionClient::query_action(std::uint64_t action_id) {
|
||||
auto& m = *impl_;
|
||||
return (m.protocol_version == network::session_protocol_version ||
|
||||
|
||||
@@ -14,6 +14,7 @@ struct CellularRegionHost::Pending {
|
||||
std::unique_ptr<CellularLives> candidate;
|
||||
std::vector<CellularActionRecord> actions;
|
||||
std::vector<CellularTickIntents::Birth> births;
|
||||
std::vector<CellularMutationActionRecord> mutation_actions;
|
||||
};
|
||||
|
||||
namespace {
|
||||
@@ -76,6 +77,31 @@ std::optional<persistence::Uuid> commit_id(const persistence::CommittedCheckpoin
|
||||
}
|
||||
} // namespace
|
||||
|
||||
network::AdmitStatus admit_status(CellularMutationAdmission admission) noexcept {
|
||||
switch (admission) {
|
||||
case CellularMutationAdmission::admitted:
|
||||
return network::AdmitStatus::admitted;
|
||||
case CellularMutationAdmission::not_ready:
|
||||
return network::AdmitStatus::not_ready;
|
||||
case CellularMutationAdmission::busy:
|
||||
return network::AdmitStatus::too_many_unresolved;
|
||||
case CellularMutationAdmission::final:
|
||||
return network::AdmitStatus::duplicate;
|
||||
case CellularMutationAdmission::invalid:
|
||||
case CellularMutationAdmission::conflict:
|
||||
break;
|
||||
}
|
||||
return network::AdmitStatus::invalid_action;
|
||||
}
|
||||
|
||||
network::ActionOutcome action_outcome(const FinalCellularMutation& mutation) noexcept {
|
||||
return {mutation.action_id,
|
||||
mutation.result == CellularMutationResult::applied
|
||||
? network::ActionResult::committed
|
||||
: network::ActionResult::committed_no_effect,
|
||||
mutation.tick, mutation.commit_sequence};
|
||||
}
|
||||
|
||||
CellularRegionHost::CellularRegionHost(CellularHostConfig config, CellularConnect connect,
|
||||
std::unique_ptr<persistence::FilesystemObjectStore> objects)
|
||||
: config_{std::move(config)}, connect_{std::move(connect)}, objects_{std::move(objects)},
|
||||
@@ -139,6 +165,8 @@ void CellularRegionHost::acquire() {
|
||||
}
|
||||
state_ = std::move(recovered->state);
|
||||
committed_ = std::move(recovered->committed);
|
||||
queued_mutations_.clear();
|
||||
mutations_uncertain_ = true; // Reserved choices are reloaded under the new lease.
|
||||
status_.phase = HostPhase::running;
|
||||
status_.simulated_tick = committed_->head.end_tick;
|
||||
status_.committed_tick = committed_->head.end_tick;
|
||||
@@ -261,6 +289,7 @@ void CellularRegionHost::resolve() {
|
||||
for (const auto& action : pending_->actions)
|
||||
finalized_actions_.push_back({action, committed_->head.sequence});
|
||||
finalize_births(pending_->births);
|
||||
finalize_mutations(pending_->mutation_actions, pending_->prepared.results);
|
||||
pending_.reset();
|
||||
++status_.commits;
|
||||
status_.committed_sequence = committed_->head.sequence;
|
||||
@@ -290,15 +319,142 @@ const HostStatus& CellularRegionHost::step() {
|
||||
resolve();
|
||||
else if (!state_)
|
||||
acquire();
|
||||
if (!pending_ && state_ && mutations_uncertain_ && status_.phase == HostPhase::running)
|
||||
reload_mutations();
|
||||
refresh();
|
||||
return status_;
|
||||
}
|
||||
|
||||
void CellularRegionHost::reload_mutations() {
|
||||
const auto grant = authority_.grant();
|
||||
if (!grant || !committed_ || !database())
|
||||
return;
|
||||
auto pending = persistence::load_pending_cellular_mutations(*db_, config_.key, *grant,
|
||||
config_.lease.commit_timeout);
|
||||
if (!pending) {
|
||||
if (pending.error().kind == persistence::LifeReservationError::fenced) {
|
||||
authority_.fence();
|
||||
status_.ready = false;
|
||||
}
|
||||
db_.reset();
|
||||
++status_.store_failures;
|
||||
return;
|
||||
}
|
||||
// Every reservation targets the tick after the head it was made at, and SQL refuses a
|
||||
// checkpoint that skips one, so an unbound choice is always for the next tick.
|
||||
queued_mutations_.clear();
|
||||
for (const auto& reservation : *pending)
|
||||
if (reservation.target_tick == committed_->head.end_tick + 1)
|
||||
queued_mutations_.push_back(reservation);
|
||||
mutations_uncertain_ = false;
|
||||
}
|
||||
|
||||
void CellularRegionHost::finalize_mutations(std::span<const CellularMutationActionRecord> actions,
|
||||
std::span<const std::uint8_t> results) {
|
||||
if (!committed_)
|
||||
return;
|
||||
const auto sequence = committed_->head.sequence;
|
||||
for (std::size_t i = 0; i < actions.size() && i < results.size(); ++i) {
|
||||
const auto& action = actions[i];
|
||||
std::erase_if(queued_mutations_, [&](const auto& queued) {
|
||||
return queued.account == action.account && queued.action_id == action.action_id;
|
||||
});
|
||||
const FinalCellularMutation done{action.account, action.action_id, action.tick, sequence,
|
||||
static_cast<CellularMutationResult>(results[i])};
|
||||
finalized_mutations_.push_back(done);
|
||||
std::erase_if(recent_mutations_,
|
||||
[&](const auto& recent) { return recent.account == done.account; });
|
||||
if (recent_mutations_.size() >= 1024)
|
||||
recent_mutations_.erase(recent_mutations_.begin());
|
||||
recent_mutations_.push_back(done);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<FinalCellularMutation> CellularRegionHost::take_finalized_mutations() {
|
||||
return std::exchange(finalized_mutations_, {});
|
||||
}
|
||||
|
||||
CellularMutationReply
|
||||
CellularRegionHost::select_mutation(const persistence::Uuid& account, std::uint64_t generation,
|
||||
std::uint64_t action_id,
|
||||
const CellularMutationActionInput& input) {
|
||||
using enum CellularMutationAdmission;
|
||||
if (account == persistence::Uuid{} || generation == 0 || action_id == 0)
|
||||
return {invalid, 0, {}};
|
||||
if (const auto recent = std::ranges::find_if(recent_mutations_,
|
||||
[&](const auto& done) {
|
||||
return done.account == account &&
|
||||
done.action_id == action_id;
|
||||
});
|
||||
recent != recent_mutations_.end())
|
||||
return {final, recent->tick, *recent};
|
||||
if (!status_.ready || mutations_uncertain_ || pending_ || !state_ || !committed_ ||
|
||||
!authority_.may_begin_commit(LeaseClock::now()) || !database())
|
||||
return {not_ready, 0, {}};
|
||||
const auto tick = committed_->head.end_tick + 1;
|
||||
const CellularMutationActionRecord record{account, action_id, tick, input};
|
||||
const auto bytes = encode_cellular_mutation_input(record);
|
||||
for (const auto& queued : queued_mutations_) {
|
||||
if (queued.account != account)
|
||||
continue;
|
||||
if (queued.action_id != action_id)
|
||||
return {busy, queued.target_tick, {}};
|
||||
return queued.input == bytes ? CellularMutationReply{admitted, tick, {}}
|
||||
: CellularMutationReply{conflict, 0, {}};
|
||||
}
|
||||
// The choice must name this account's living cell and its exact outstanding offer.
|
||||
const auto life = std::ranges::find(state_->lives(), account, &CellularOwnedLife::account);
|
||||
if (life == state_->lives().end() || life->identity.life != input.life_id ||
|
||||
input.event_index == 0 || life->mutation.event_index != input.event_index ||
|
||||
life->mutation.offer_tick != input.offer_tick || input.offer_tick >= tick)
|
||||
return {invalid, 0, {}};
|
||||
const auto& offer = life->mutation.offer;
|
||||
if (!offer || !std::ranges::any_of(offer->candidates, [&](const auto& candidate) {
|
||||
return candidate.operator_id == input.operator_id;
|
||||
}))
|
||||
return {invalid, 0, {}};
|
||||
const auto grant = authority_.grant();
|
||||
if (!grant)
|
||||
return {not_ready, 0, {}};
|
||||
auto reserved =
|
||||
persistence::reserve_cellular_mutation(*db_, config_.key, *grant, account, generation,
|
||||
action_id, bytes, config_.lease.commit_timeout);
|
||||
if (!reserved) {
|
||||
switch (reserved.error().kind) {
|
||||
case persistence::LifeReservationError::pending_mutation:
|
||||
return {busy, 0, {}};
|
||||
case persistence::LifeReservationError::identity_conflict:
|
||||
return {conflict, 0, {}};
|
||||
case persistence::LifeReservationError::fenced:
|
||||
authority_.fence();
|
||||
status_.ready = false;
|
||||
return {not_ready, 0, {}};
|
||||
case persistence::LifeReservationError::unknown:
|
||||
case persistence::LifeReservationError::store:
|
||||
// The reservation may have committed: nothing publishes until it is reloaded.
|
||||
mutations_uncertain_ = true;
|
||||
db_.reset();
|
||||
++status_.store_failures;
|
||||
return {not_ready, 0, {}};
|
||||
default:
|
||||
return {invalid, 0, {}};
|
||||
}
|
||||
}
|
||||
if (reserved->region != config_.key.region || reserved->target_tick != tick ||
|
||||
reserved->input != bytes) {
|
||||
mutations_uncertain_ = true;
|
||||
return {not_ready, 0, {}};
|
||||
}
|
||||
queued_mutations_.push_back(*reserved);
|
||||
return {admitted, tick, {}};
|
||||
}
|
||||
|
||||
std::expected<persistence::PublishedCellularBatch, CellularPublishFailure>
|
||||
CellularRegionHost::publish(const CellularBatch& batch,
|
||||
std::span<const CellularActionRecord> actions) {
|
||||
std::span<const CellularActionRecord> actions,
|
||||
std::span<const CellularMutationActionRecord> mutation_actions) {
|
||||
const auto grant = authority_.grant();
|
||||
if (!status_.ready || !state_ || !committed_ || !grant ||
|
||||
if (!status_.ready || mutations_uncertain_ || !state_ || !committed_ || !grant ||
|
||||
!authority_.may_begin_commit(LeaseClock::now()))
|
||||
return std::unexpected{CellularPublishFailure{CellularHostError::not_ready, {}, {}, {}}};
|
||||
auto candidate = replay_cellular_batch(*state_, batch);
|
||||
@@ -315,13 +471,15 @@ CellularRegionHost::publish(const CellularBatch& batch,
|
||||
CellularPublishFailure{CellularHostError::invalid_batch, {}, {}, {}}};
|
||||
auto published = persistence::publish_cellular_batch(
|
||||
*db_, *objects_, grant.value_or(persistence::LeaseGrant{}), *committed_, batch, **candidate,
|
||||
*commit, config_.build, config_.lease.commit_timeout, {}, actions);
|
||||
*commit, config_.build, config_.lease.commit_timeout, {}, actions, mutation_actions);
|
||||
if (!published) {
|
||||
if (published.error().kind == persistence::PublicationError::unknown) {
|
||||
pending_ = std::make_unique<Pending>(Pending{published.error().prepared,
|
||||
std::move(*candidate),
|
||||
{actions.begin(), actions.end()},
|
||||
births_in(batch)});
|
||||
pending_ = std::make_unique<Pending>(
|
||||
Pending{published.error().prepared,
|
||||
std::move(*candidate),
|
||||
{actions.begin(), actions.end()},
|
||||
births_in(batch),
|
||||
{mutation_actions.begin(), mutation_actions.end()}});
|
||||
status_.phase = HostPhase::resolving;
|
||||
status_.ready = false;
|
||||
++status_.unknown_outcomes;
|
||||
@@ -333,6 +491,8 @@ CellularRegionHost::publish(const CellularBatch& batch,
|
||||
status_.phase = HostPhase::acquiring;
|
||||
status_.ready = false;
|
||||
++status_.fences;
|
||||
} else {
|
||||
mutations_uncertain_ = true; // A refusal may name a choice the queue lacks.
|
||||
}
|
||||
return std::unexpected{
|
||||
CellularPublishFailure{CellularHostError::publication, published.error().kind,
|
||||
@@ -344,6 +504,11 @@ CellularRegionHost::publish(const CellularBatch& batch,
|
||||
finalized_actions_.push_back({action, committed_->head.sequence});
|
||||
const auto births = births_in(batch);
|
||||
finalize_births(births);
|
||||
std::vector<std::uint8_t> results;
|
||||
results.reserve(published->mutation_results.size());
|
||||
for (const auto result : published->mutation_results)
|
||||
results.push_back(static_cast<std::uint8_t>(result));
|
||||
finalize_mutations(mutation_actions, results);
|
||||
++status_.commits;
|
||||
status_.committed_sequence = committed_->head.sequence;
|
||||
status_.committed_tick = committed_->head.end_tick;
|
||||
|
||||
@@ -638,6 +638,26 @@ struct RegionSessions::Impl {
|
||||
answer(c, *receipt);
|
||||
return;
|
||||
}
|
||||
if (const auto* m = std::get_if<network::CellularMutationAction>(&*decoded)) {
|
||||
if (c.protocol_version != network::session_protocol_v10 ||
|
||||
!config.select_cellular_mutation)
|
||||
return close(c, SessionCloseReason::protocol, now);
|
||||
CellularMutationAnswer answer{
|
||||
m->session != c.session ? network::AdmitStatus::unknown_session
|
||||
: m->epoch != control.epoch() ? network::AdmitStatus::stale_epoch
|
||||
: m->action_id == 0 ? network::AdmitStatus::invalid_action
|
||||
: !facts.ready || c.generation == 0 ? network::AdmitStatus::not_ready
|
||||
: network::AdmitStatus::admitted,
|
||||
0,
|
||||
{}};
|
||||
if (answer.status == network::AdmitStatus::admitted)
|
||||
answer = config.select_cellular_mutation(c.account_id, c.generation, *m);
|
||||
send(c, network::ActionReceipt{m->action_id, answer.status, facts.simulated_tick,
|
||||
answer.tick});
|
||||
if (answer.outcome)
|
||||
send(c, *answer.outcome);
|
||||
return;
|
||||
}
|
||||
if (const auto* m = std::get_if<network::ActionQuery>(&*decoded)) {
|
||||
if (c.protocol_version == network::session_protocol_v5 ||
|
||||
c.protocol_version == network::session_protocol_v6)
|
||||
@@ -915,6 +935,19 @@ bool RegionSessions::send_cellular_perception(const network::AccountId& account,
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RegionSessions::send_mutation_outcome(const network::AccountId& account,
|
||||
const network::ActionOutcome& outcome) {
|
||||
auto& m = *impl_;
|
||||
for (const auto& c : m.connections) {
|
||||
if (c.state != Impl::State::established ||
|
||||
c.protocol_version != network::session_protocol_v10 || c.account_id != account)
|
||||
continue;
|
||||
return m.endpoint->send_message(c.id, network::encode_session(outcome, c.protocol_version))
|
||||
.has_value();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void RegionSessions::clear_cellular_perceptions(std::uint64_t tick) {
|
||||
if (tick == 0)
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user