Route cellular logins to existing life owners
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 39s
Foundation / checks (ecs) (push) Successful in 2m1s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 22s
Foundation / checks (network) (push) Canceled after 1m58s
Foundation / checks (client) (push) Canceled after 2m39s

This commit is contained in:
dingo
2026-10-11 01:32:14 -04:00
parent 636740bc55
commit a6b1c62168
11 changed files with 273 additions and 106 deletions
+19 -18
View File
@@ -29,9 +29,10 @@ namespace evo::server_app {
// owner-filtered v8 perception. // owner-filtered v8 perception.
class CellularBridge { class CellularBridge {
public: public:
static std::optional<CellularBridge> static std::optional<CellularBridge> open(const M10Options& options,
open(const M10Options& options, server::CellularRegionHost& host, server::CellularRegionHost& host,
persistence::RegionKey key, std::uint64_t frame, const persistence::PgOptions& pg) { persistence::RegionKey key, std::uint64_t frame,
const persistence::PgOptions& pg) {
if (!options.complete()) if (!options.complete())
return fail("error cellular-session-config"); return fail("error cellular-session-config");
CellularBridge bridge; CellularBridge bridge;
@@ -54,16 +55,19 @@ class CellularBridge {
config.transport.port = port; config.transport.port = port;
config.transport.certificate_pem_file = options.certificate; config.transport.certificate_pem_file = options.certificate;
config.transport.key_pem_file = options.key; config.transport.key_pem_file = options.key;
config.control.environment = options.environment == "dev" config.control.environment =
? network::DeploymentEnvironment::dev options.environment == "dev" ? network::DeploymentEnvironment::dev
: options.environment == "stage" : options.environment == "stage" ? network::DeploymentEnvironment::stage
? network::DeploymentEnvironment::stage
: network::DeploymentEnvironment::world0; : network::DeploymentEnvironment::world0;
config.control.world = {key.world, frame}; config.control.world = {key.world, frame};
config.control.region = key.region; config.control.region = key.region;
config.control.epoch = 1; config.control.epoch = 1;
config.control.max_sessions = 512; config.control.max_sessions = 512;
config.transport.limits.max_connections = 576; config.transport.limits.max_connections = 576;
const auto life_routes = parse_life_owner_routes(options);
if (!life_routes || life_routes->empty())
return fail("error cellular-life-routes");
config.life_owner_routes = *life_routes;
network::GrantPublicKey public_key{}; network::GrantPublicKey public_key{};
std::copy(issuer.begin(), issuer.end(), public_key.begin()); std::copy(issuer.begin(), issuer.end(), public_key.begin());
config.issuer_keys.push_back({1, public_key}); config.issuer_keys.push_back({1, public_key});
@@ -73,19 +77,16 @@ class CellularBridge {
const auto lease = [&host] { return host.authority(); }; const auto lease = [&host] { return host.authority(); };
bridge.control_ = bridge.control_ =
std::make_unique<server::PostgresControlLedger>(*bridge.slot_, key, lease); std::make_unique<server::PostgresControlLedger>(*bridge.slot_, key, lease);
bridge.actions_ = bridge.actions_ = std::make_unique<server::PostgresActionLedger>(*bridge.slot_, key, lease);
std::make_unique<server::PostgresActionLedger>(*bridge.slot_, key, lease);
config.ledger = bridge.control_.get(); config.ledger = bridge.control_.get();
config.actions = bridge.actions_.get(); config.actions = bridge.actions_.get();
config.accept_cellular_actions = true; config.accept_cellular_actions = true;
config.create_primitive_life = [&host](const network::AccountId& account, config.create_primitive_life = [&host](const network::AccountId& account,
std::uint64_t generation, std::uint64_t generation, std::uint64_t request) {
std::uint64_t request) {
return host.create_primitive_life(account.bytes, generation, request); return host.create_primitive_life(account.bytes, generation, request);
}; };
config.query_primitive_life = [&host](const network::AccountId& account, config.query_primitive_life = [&host](const network::AccountId& account,
std::uint64_t generation, std::uint64_t generation, std::uint64_t request) {
std::uint64_t request) {
return host.query_primitive_life(account.bytes, generation, request); return host.query_primitive_life(account.bytes, generation, request);
}; };
auto sessions = server::RegionSessions::create(std::move(config)); auto sessions = server::RegionSessions::create(std::move(config));
@@ -155,12 +156,12 @@ class CellularBridge {
const auto genome = state.archive().find(life.identity.genome); const auto genome = state.archive().find(life.identity.genome);
if (!genome) if (!genome)
continue; continue;
const biology::EnvironmentState environment{ const biology::EnvironmentState environment{state.ambient_temperature_millikelvin(),
state.ambient_temperature_millikelvin(), life.reserve}; life.reserve};
auto cell = client::present_organism( auto cell = client::present_organism(
life.identity.organism, voxel, life.identity.organism, voxel, {life.position.x_um % 1'000'000},
{life.position.x_um % 1'000'000}, {life.position.y_um % 1'000'000}, *genome, {life.position.y_um % 1'000'000}, *genome, life.growth, environment,
life.growth, environment, client::OrganismState::alive); client::OrganismState::alive);
if (cell) if (cell)
result.organisms.push_back(*cell); result.organisms.push_back(*cell);
} }
+30 -1
View File
@@ -63,10 +63,14 @@ struct M10Options {
// neighbours "REGION=X0,Y0,X1,Y1=HOST:PORT=SERVER_NAME=PUBLIC_KEY_FILE". // neighbours "REGION=X0,Y0,X1,Y1=HOST:PORT=SERVER_NAME=PUBLIC_KEY_FILE".
std::string area, handoff_key; std::string area, handoff_key;
std::vector<std::string> neighbours; std::vector<std::string> neighbours;
// M11 owner routing, independent of --m10-neighbour:
// REGION=HOST:PORT=TLS_NAME, one entry per region in the world.
std::vector<std::string> life_routes;
[[nodiscard]] bool any() const noexcept { [[nodiscard]] bool any() const noexcept {
return !listen.empty() || !certificate.empty() || !key.empty() || !issuer.empty() || return !listen.empty() || !certificate.empty() || !key.empty() || !issuer.empty() ||
!environment.empty() || !session_window.empty() || max_grant_lifetime != 0 || !environment.empty() || !session_window.empty() || max_grant_lifetime != 0 ||
!area.empty() || !handoff_key.empty() || !neighbours.empty() || !load_governor; !area.empty() || !handoff_key.empty() || !neighbours.empty() ||
!life_routes.empty() || !load_governor;
} }
[[nodiscard]] bool complete() const noexcept { [[nodiscard]] bool complete() const noexcept {
return !listen.empty() && !certificate.empty() && !key.empty() && !issuer.empty() && return !listen.empty() && !certificate.empty() && !key.empty() && !issuer.empty() &&
@@ -80,6 +84,27 @@ inline constexpr std::uint64_t m10_carry_ahead_ticks = 8;
inline constexpr std::size_t m10_max_sessions = 2000; inline constexpr std::size_t m10_max_sessions = 2000;
#ifdef EVO_SERVER_M10 #ifdef EVO_SERVER_M10
inline std::optional<std::vector<server::LifeOwnerRoute>>
parse_life_owner_routes(const M10Options& options) {
std::vector<server::LifeOwnerRoute> routes;
routes.reserve(options.life_routes.size());
for (const auto& item : options.life_routes) {
const auto first = item.find('=');
const auto second =
first == std::string::npos ? std::string::npos : item.find('=', first + 1);
if (first == std::string::npos || second == std::string::npos ||
item.find('=', second + 1) != std::string::npos)
return std::nullopt;
std::uint64_t region{};
const auto [end, error] = std::from_chars(item.data(), item.data() + first, region);
if (error != std::errc{} || end != item.data() + first || region == 0)
return std::nullopt;
routes.push_back({network::RegionAddress{region, item.substr(first + 1, second - first - 1),
item.substr(second + 1)}});
}
return routes;
}
inline constexpr bool m10_available = true; inline constexpr bool m10_available = true;
template <typename Host> class M10Bridge { template <typename Host> class M10Bridge {
public: public:
@@ -185,6 +210,10 @@ template <typename Host> class M10Bridge {
config.handoff = bridge.handoff_.get(); config.handoff = bridge.handoff_.get();
std::printf("m10 handoff neighbours=%zu\n", config.neighbours.size()); std::printf("m10 handoff neighbours=%zu\n", config.neighbours.size());
} }
const auto life_routes = parse_life_owner_routes(o);
if (!life_routes)
return fail("error m10-life-routes");
config.life_owner_routes = *life_routes;
auto sessions = server::RegionSessions::create(std::move(config)); auto sessions = server::RegionSessions::create(std::move(config));
if (!sessions) if (!sessions)
return fail(sessions.error() == server::RegionSessionsError::keys ? "error m10-issuer" return fail(sessions.error() == server::RegionSessionsError::keys ? "error m10-issuer"
+16 -17
View File
@@ -171,9 +171,8 @@ std::optional<Options> parse(int argc, char** argv) {
return std::nullopt; return std::nullopt;
const std::string_view value = argv[++i]; const std::string_view value = argv[++i];
const auto n = number(value); const auto n = number(value);
if (flag == "--profile" && if (flag == "--profile" && (value == "lineage" || value == "environmental" ||
(value == "lineage" || value == "environmental" || value == "primitive" || value == "primitive" || value == "cellular"))
value == "cellular"))
o.profile = value; o.profile = value;
else if (flag == "--world" && n) else if (flag == "--world" && n)
o.host.key.world = *n; o.host.key.world = *n;
@@ -267,6 +266,8 @@ std::optional<Options> parse(int argc, char** argv) {
o.m10.handoff_key = value; o.m10.handoff_key = value;
else if (flag == "--m10-neighbour" && o.m10.neighbours.size() < 8) else if (flag == "--m10-neighbour" && o.m10.neighbours.size() < 8)
o.m10.neighbours.emplace_back(value); o.m10.neighbours.emplace_back(value);
else if (flag == "--life-route" && o.m10.life_routes.size() < 4096)
o.m10.life_routes.emplace_back(value);
else if (flag == "--m10-load-governor" && (value == "on" || value == "off")) else if (flag == "--m10-load-governor" && (value == "on" || value == "off"))
o.m10.load_governor = value == "on"; o.m10.load_governor = value == "on";
else if (flag == "--primitive-listen") else if (flag == "--primitive-listen")
@@ -321,10 +322,9 @@ std::optional<Options> parse(int argc, char** argv) {
return std::nullopt; return std::nullopt;
if (o.profile == "cellular" && if (o.profile == "cellular" &&
(!server_app::m10_available || !o.m10.complete() || o.primitive_session.any() || (!server_app::m10_available || !o.m10.complete() || o.primitive_session.any() ||
o.synthetic || !o.crash_point.empty() || o.session.any() || o.synthetic || !o.crash_point.empty() || o.session.any() || !o.m10.area.empty() ||
!o.m10.area.empty() || !o.m10.handoff_key.empty() || !o.m10.neighbours.empty() || !o.m10.handoff_key.empty() || !o.m10.neighbours.empty() || o.m10.max_grant_lifetime != 0 ||
o.m10.max_grant_lifetime != 0 || !o.m10.load_governor || !o.m10.load_governor || o.host.flush_interval != 1000ms || o.drain != 10000ms))
o.host.flush_interval != 1000ms || o.drain != 10000ms))
return std::nullopt; return std::nullopt;
if (std::ranges::find(o.host.retained_builds, o.host.build) == o.host.retained_builds.end()) if (std::ranges::find(o.host.retained_builds, o.host.build) == o.host.retained_builds.end())
o.host.retained_builds.push_back(o.host.build); o.host.retained_builds.push_back(o.host.build);
@@ -946,8 +946,7 @@ int serve_cellular(Options& o) {
set_ready(false); set_ready(false);
const auto& status = host.status(); const auto& status = host.status();
std::printf("phase %s epoch=%llu committed=%llu simulated=%llu\n", std::printf("phase %s epoch=%llu committed=%llu simulated=%llu\n",
server::phase_name(status.phase), server::phase_name(status.phase), static_cast<unsigned long long>(status.epoch),
static_cast<unsigned long long>(status.epoch),
static_cast<unsigned long long>(status.committed_tick), static_cast<unsigned long long>(status.committed_tick),
static_cast<unsigned long long>(status.simulated_tick)); static_cast<unsigned long long>(status.simulated_tick));
std::printf("stats commits=%llu store_failures=%llu\n", std::printf("stats commits=%llu store_failures=%llu\n",
@@ -964,8 +963,8 @@ int serve_cellular(Options& o) {
return finish(0); return finish(0);
} }
const auto& status = host.step(); const auto& status = host.step();
const auto wall = static_cast<std::uint64_t>( const auto wall =
std::chrono::duration_cast<std::chrono::seconds>( static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()) std::chrono::system_clock::now().time_since_epoch())
.count()); .count());
static_cast<void>(bridge->accepting(wall)); static_cast<void>(bridge->accepting(wall));
@@ -1024,8 +1023,8 @@ int serve_cellular(Options& o) {
baseline.push_back({life.identity.life, {}}); baseline.push_back({life.identity.life, {}});
std::ranges::sort(baseline, {}, &server::CellularLifeIntent::life_id); std::ranges::sort(baseline, {}, &server::CellularLifeIntent::life_id);
auto actions = bridge->carry(tick); auto actions = bridge->carry(tick);
auto builder = server::CellularSessionBatchBuilder::create( auto builder =
*state, host.committed()->head); server::CellularSessionBatchBuilder::create(*state, host.committed()->head);
if (!builder) { if (!builder) {
bridge->dropped(actions, true); bridge->dropped(actions, true);
} else { } else {
@@ -1047,8 +1046,8 @@ int serve_cellular(Options& o) {
deliver_finalized(); deliver_finalized();
} }
} }
*next_tick = std::chrono::steady_clock::now() + std::chrono::microseconds{ *next_tick = std::chrono::steady_clock::now() +
static_cast<std::chrono::microseconds::rep>( std::chrono::microseconds{static_cast<std::chrono::microseconds::rep>(
biology::cellular_motion_tick_microseconds)}; biology::cellular_motion_tick_microseconds)};
} }
} else { } else {
@@ -1066,8 +1065,7 @@ int serve_cellular(Options& o) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
auto options = parse(argc, argv); auto options = parse(argc, argv);
if (!options) { if (!options) {
std::fputs( std::fputs("usage: evo-server --profile lineage|environmental|primitive|"
"usage: evo-server --profile lineage|environmental|primitive|"
"cellular --world N --region N " "cellular --world N --region N "
"--frame N --pg-host H --pg-port P --pg-database D --pg-user U " "--frame N --pg-host H --pg-port P --pg-database D --pg-user U "
"--objects DIR --build HEX64 [--retained HEX64]... [--pg-hostaddr A " "--objects DIR --build HEX64 [--retained HEX64]... [--pg-hostaddr A "
@@ -1084,6 +1082,7 @@ int main(int argc, char** argv) {
"[--m10-max-grant-lifetime 60..14400] [--m10-area X0,Y0,X1,Y1] " "[--m10-max-grant-lifetime 60..14400] [--m10-area X0,Y0,X1,Y1] "
"[--m10-handoff-key F [--m10-neighbour " "[--m10-handoff-key F [--m10-neighbour "
"REGION=X0,Y0,X1,Y1=HOST:PORT=NAME=PUBFILE]...] " "REGION=X0,Y0,X1,Y1=HOST:PORT=NAME=PUBFILE]...] "
"[--life-route REGION=HOST:PORT=TLS_NAME ...] "
"[--m10-load-governor on|off]]\\n", "[--m10-load-governor on|off]]\\n",
stderr); stderr);
return 2; return 2;
+8 -7
View File
@@ -137,13 +137,14 @@ Local checks do not close ADR-0097's reference-worker qualification.
reservations route to their original region and a refused/cancelled-only reservations route to their original region and a refused/cancelled-only
history has no active owner. PostgreSQL and migration CI execution remain history has no active owner. PostgreSQL and migration CI execution remain
pending. pending.
- Cross-region owner routing and reconnect-to-existing-life remain unimplemented. - 11.2d-7b implementation: grant login resolves the active life owner after
[ADR-0116](../adr/0116-cellular-life-owner-routing.md) resolves the contract: signature/scope checks and before durable control claim. A different owner
resolve the durable world/account owner before claiming control, then reuse uses the v4+ SessionRedirect from an explicit `--life-route` per world region,
the existing SessionRedirect and an operator-provisioned complete region route then closes as moved. Cellular startup requires a route catalog containing
catalog. Missing owner data or route fails closed; client endpoints are never this region; malformed, duplicate, unavailable, and missing-owner routes fail
authoritative. Do not claim 11.2 complete until redirect/client switching, closed. QUIC tests cover redirect-before-claim and missing-route refusal.
process-kill qualification and restart retry pass. PostgreSQL and migration CI, client switching, and process-kill qualification
remain pending. Do not claim 11.2 complete until redirect/restart gates pass.
No deployed world has been provisioned or changed by this work. No deployed world has been provisioned or changed by this work.
+19
View File
@@ -6555,3 +6555,22 @@ because the Windows environment lacks POSIX `poll.h`. PostgreSQL/psql, CTest,
and CI have not run for this segment. The CI migration, privilege, and and CI have not run for this segment. The CI migration, privilege, and
PostgreSQL integration results remain required before closing the database PostgreSQL integration results remain required before closing the database
gate. gate.
## 2026-10-11 — M11.2d-7b owner routing (CI and runtime pending)
The QUIC session path now resolves the durable life owner after grant
verification/scope checks and before calling the durable control claim. A
different owner is looked up only in the operator-provided `--life-route`
catalog; the listener sends the existing `SessionRedirect` for v4 and later,
then `Close(moved)`. An incomplete catalog or database lookup failure does not
claim control. Cellular server startup requires at least its own route entry.
GCC 13.2 strict-warning syntax compilation passed for
`src/server/session/region_sessions.cpp`,
`src/server/session/postgres_control_ledger.cpp`, and
`tests/quic_sessions.cpp`. A translation unit including the cellular bridge
also compiled, and the Visual Studio LLVM `clang-format` check and
`git diff --check` passed. The full `evo-server` source could not be compiled
locally because this Windows environment lacks POSIX `poll.h`; the QUIC test
binary, PostgreSQL integration suite, and CI have not run. The database owner
lookup and route behavior still require CI/runtime qualification.
+6
View File
@@ -186,6 +186,12 @@ class ControlLedger {
virtual ~ControlLedger() = default; virtual ~ControlLedger() = default;
[[nodiscard]] virtual std::expected<ControlLease, ControlLedgerError> [[nodiscard]] virtual std::expected<ControlLease, ControlLedgerError>
claim(const AccountId& account, std::uint64_t serial, std::uint64_t session) = 0; claim(const AccountId& account, std::uint64_t serial, std::uint64_t session) = 0;
// Optional M11 lookup, called before claim() when the host has an operator-provisioned
// life-owner route catalog. Implementations without durable life reservations fail closed.
[[nodiscard]] virtual std::expected<std::optional<std::uint64_t>, ControlLedgerError>
primitive_life_owner(const AccountId&) {
return std::unexpected(ControlLedgerError::unavailable);
}
[[nodiscard]] virtual std::expected<std::chrono::milliseconds, ControlLedgerError> [[nodiscard]] virtual std::expected<std::chrono::milliseconds, ControlLedgerError>
renew(const AccountId& account, std::uint64_t generation) = 0; renew(const AccountId& account, std::uint64_t generation) = 0;
virtual void release(const AccountId& account, std::uint64_t generation) = 0; virtual void release(const AccountId& account, std::uint64_t generation) = 0;
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "evo/network/session_control.hpp" #include "evo/network/session_control.hpp"
#include "evo/persistence/account_control_store.hpp" #include "evo/persistence/account_control_store.hpp"
#include "evo/persistence/life_reservation_store.hpp"
#include "evo/server/pg_slot.hpp" #include "evo/server/pg_slot.hpp"
#include <chrono> #include <chrono>
#include <functional> #include <functional>
@@ -22,6 +23,8 @@ class PostgresControlLedger final : public network::ControlLedger {
: db_{&db}, key_{key}, lease_{std::move(lease)}, duration_{duration}, timeout_{timeout} {} : db_{&db}, key_{key}, lease_{std::move(lease)}, duration_{duration}, timeout_{timeout} {}
[[nodiscard]] std::expected<network::ControlLease, network::ControlLedgerError> [[nodiscard]] std::expected<network::ControlLease, network::ControlLedgerError>
claim(const network::AccountId& account, std::uint64_t serial, std::uint64_t session) override; claim(const network::AccountId& account, std::uint64_t serial, std::uint64_t session) override;
[[nodiscard]] std::expected<std::optional<std::uint64_t>, network::ControlLedgerError>
primitive_life_owner(const network::AccountId& account) override;
[[nodiscard]] std::expected<std::chrono::milliseconds, network::ControlLedgerError> [[nodiscard]] std::expected<std::chrono::milliseconds, network::ControlLedgerError>
renew(const network::AccountId& account, std::uint64_t generation) override; renew(const network::AccountId& account, std::uint64_t generation) override;
void release(const network::AccountId& account, std::uint64_t generation) override; void release(const network::AccountId& account, std::uint64_t generation) override;
+10 -2
View File
@@ -59,6 +59,11 @@ struct HandoffKey {
std::uint32_t key_id{}; std::uint32_t key_id{};
network::GrantPublicKey public_key{}; network::GrantPublicKey public_key{};
}; };
// Operator-provisioned destination for a durable primitive life. This world catalog is
// independent of physical neighbours and is consulted only before a grant claims control.
struct LifeOwnerRoute {
network::RegionAddress address;
};
struct RegionSessionsConfig { struct RegionSessionsConfig {
network::quic::ServerConfig transport; network::quic::ServerConfig transport;
network::ControlConfig control; // `epoch` is the initial ownership epoch. network::ControlConfig control; // `epoch` is the initial ownership epoch.
@@ -70,6 +75,9 @@ struct RegionSessionsConfig {
// claim, waits out another region's control before Welcome, renews every // claim, waits out another region's control before Welcome, renews every
// `renew_interval` and closes before its control can lapse (`safety_margin`). // `renew_interval` and closes before its control can lapse (`safety_margin`).
network::ControlLedger* ledger{}; network::ControlLedger* ledger{};
// When present, every entry must be supplied by the operator, including this region.
// A life in another region is redirected before durable control can move here.
std::vector<LifeOwnerRoute> life_owner_routes;
std::chrono::milliseconds renew_interval{5000}, safety_margin{2000}; std::chrono::milliseconds renew_interval{5000}, safety_margin{2000};
network::IntentLimits intents{}; // max_sessions follows control.max_sessions. network::IntentLimits intents{}; // max_sessions follows control.max_sessions.
// Durable action records (10.4b), owned by the host; null keeps outcomes in memory only. // Durable action records (10.4b), owned by the host; null keeps outcomes in memory only.
@@ -157,8 +165,8 @@ class RegionSessions {
const network::Perceives& perceives = {}); const network::Perceives& perceives = {});
// V8 reliable cellular perception is unicast to the current established session // V8 reliable cellular perception is unicast to the current established session
// for the supplied authenticated account. Empty views clear that owner's stale view. // for the supplied authenticated account. Empty views clear that owner's stale view.
[[nodiscard]] bool send_cellular_perception( [[nodiscard]] bool send_cellular_perception(const network::AccountId& account,
const network::AccountId& account, const network::CellularPerception& perception); const network::CellularPerception& perception);
void clear_cellular_perceptions(std::uint64_t tick); void clear_cellular_perceptions(std::uint64_t tick);
[[nodiscard]] network::ReplicationStats replication_stats() const; [[nodiscard]] network::ReplicationStats replication_stats() const;
// M10.9a (ADR-0097): the host's load governor output. While `refuse_logins`, a Hello // M10.9a (ADR-0097): the host's load governor output. While `refuse_logins`, a Hello
@@ -45,6 +45,33 @@ PostgresControlLedger::claim(const network::AccountId& account, std::uint64_t se
return network::ControlLease{claimed->generation, claimed->wait, claimed->granted}; return network::ControlLease{claimed->generation, claimed->wait, claimed->granted};
} }
std::expected<std::optional<std::uint64_t>, network::ControlLedgerError>
PostgresControlLedger::primitive_life_owner(const network::AccountId& account) {
const auto lease = lease_();
if (!lease)
return std::unexpected(network::ControlLedgerError::fenced);
auto* db = db_->get();
if (!db)
return std::unexpected(network::ControlLedgerError::unavailable);
const auto owner =
persistence::primitive_life_owner(*db, key_, *lease, uuid(account), timeout_);
if (!owner) {
note(*db_, owner.error().kind == persistence::LifeReservationError::store
? ControlStoreError::store
: owner.error().kind == persistence::LifeReservationError::unknown
? ControlStoreError::unknown
: owner.error().kind == persistence::LifeReservationError::fenced
? ControlStoreError::fenced
: ControlStoreError::invalid);
return std::unexpected(owner.error().kind == persistence::LifeReservationError::fenced
? network::ControlLedgerError::fenced
: owner.error().kind == persistence::LifeReservationError::invalid
? network::ControlLedgerError::fenced
: network::ControlLedgerError::unavailable);
}
return *owner;
}
std::expected<std::chrono::milliseconds, ControlLedgerError> std::expected<std::chrono::milliseconds, ControlLedgerError>
PostgresControlLedger::renew(const network::AccountId& account, std::uint64_t generation) { PostgresControlLedger::renew(const network::AccountId& account, std::uint64_t generation) {
const auto lease = lease_(); const auto lease = lease_();
+44 -6
View File
@@ -1,5 +1,6 @@
#include "evo/server/region_sessions.hpp" #include "evo/server/region_sessions.hpp"
#include <algorithm> #include <algorithm>
#include <set>
#include <utility> #include <utility>
namespace evo::server { namespace evo::server {
@@ -63,6 +64,10 @@ SessionCloseReason reason_for(SessionGrantError e) {
return e == SessionGrantError::wrong_environment ? SessionCloseReason::wrong_scope return e == SessionGrantError::wrong_environment ? SessionCloseReason::wrong_scope
: SessionCloseReason::unauthenticated; : SessionCloseReason::unauthenticated;
} }
bool valid_route_text(const std::string& value) {
return !value.empty() && value.size() <= network::session_address_bytes &&
std::ranges::all_of(value, [](char ch) { return ch > 0x20 && ch < 0x7F; });
}
} // namespace } // namespace
struct RegionSessions::Impl { struct RegionSessions::Impl {
@@ -232,6 +237,24 @@ struct RegionSessions::Impl {
c.account = network::log_label(claims.account); c.account = network::log_label(claims.account);
if (const auto checked = control.check(*grant, now_seconds); !checked) if (const auto checked = control.check(*grant, now_seconds); !checked)
return close(c, reason_for(checked.error()), now); return close(c, reason_for(checked.error()), now);
if (!config.life_owner_routes.empty()) {
if (!config.ledger)
return close(c, SessionCloseReason::capacity, now);
const auto owner = config.ledger->primitive_life_owner(claims.account);
if (!owner)
return close(c, reason_for(owner.error()), now);
if (*owner && **owner != config.control.region) {
const auto route = std::ranges::find_if(
config.life_owner_routes, [&](const LifeOwnerRoute& candidate) {
return candidate.address.region == **owner;
});
if (route == config.life_owner_routes.end())
return close(c, SessionCloseReason::capacity, now);
if (c.protocol_version >= network::session_protocol_version)
send(c, network::SessionRedirect{route->address});
return close(c, SessionCloseReason::moved, now);
}
}
// Durable control first, so the world never has two controlling regions; then the // Durable control first, so the world never has two controlling regions; then the
// local admission, which check() has just confirmed. // local admission, which check() has just confirmed.
std::optional<network::ControlLease> lease; std::optional<network::ControlLease> lease;
@@ -566,7 +589,8 @@ struct RegionSessions::Impl {
(wire_version == network::session_protocol_v5 && !primitive_v5) || (wire_version == network::session_protocol_v5 && !primitive_v5) ||
(wire_version == network::session_protocol_v6 && !cellular_v6) || (wire_version == network::session_protocol_v6 && !cellular_v6) ||
((wire_version == network::session_protocol_v7 || ((wire_version == network::session_protocol_v7 ||
wire_version == network::session_protocol_v8) && !cellular_actions) || wire_version == network::session_protocol_v8) &&
!cellular_actions) ||
wire_version > network::session_protocol_v8) wire_version > network::session_protocol_v8)
return close(c, SessionCloseReason::unsupported_version, now); return close(c, SessionCloseReason::unsupported_version, now);
} else if (wire_version != c.protocol_version) { } else if (wire_version != c.protocol_version) {
@@ -642,8 +666,22 @@ RegionSessions::~RegionSessions() = default;
std::expected<std::unique_ptr<RegionSessions>, RegionSessionsError> std::expected<std::unique_ptr<RegionSessions>, RegionSessionsError>
RegionSessions::create(RegionSessionsConfig config) { RegionSessions::create(RegionSessionsConfig config) {
bool has_own_life_route = config.life_owner_routes.empty();
std::set<std::uint64_t> life_route_regions;
for (const auto& route : config.life_owner_routes) {
const auto& address = route.address;
if (address.region == 0 || !valid_route_text(address.endpoint) ||
!valid_route_text(address.server_name) ||
!life_route_regions.insert(address.region).second)
return std::unexpected(RegionSessionsError::configuration);
has_own_life_route |= address.region == config.control.region;
}
// Connections beyond sessions are headroom for logins and closes still flushing. // Connections beyond sessions are headroom for logins and closes still flushing.
if (config.issuer_keys.empty() || (config.handoff != nullptr && config.ledger == nullptr) || if (config.issuer_keys.empty() ||
(!config.life_owner_routes.empty() &&
(config.ledger == nullptr || config.life_owner_routes.size() > 4096 ||
!has_own_life_route)) ||
(config.handoff != nullptr && config.ledger == nullptr) ||
(config.handoff_key != nullptr && config.probes == nullptr) || (config.handoff_key != nullptr && config.probes == nullptr) ||
config.ticket_lifetime == 0 || config.ticket_lifetime > network::max_ticket_lifetime || config.ticket_lifetime == 0 || config.ticket_lifetime > network::max_ticket_lifetime ||
((config.create_primitive_life || config.query_primitive_life) && ((config.create_primitive_life || config.query_primitive_life) &&
@@ -838,12 +876,12 @@ void RegionSessions::replicate(
m.send_datagram(*c, chunk); // A refused chunk leaves the snapshot unacknowledged. m.send_datagram(*c, chunk); // A refused chunk leaves the snapshot unacknowledged.
} }
bool RegionSessions::send_cellular_perception( bool RegionSessions::send_cellular_perception(const network::AccountId& account,
const network::AccountId& account, const network::CellularPerception& perception) { const network::CellularPerception& perception) {
auto& m = *impl_; auto& m = *impl_;
for (const auto& c : m.connections) { for (const auto& c : m.connections) {
if (c.state != Impl::State::established || c.protocol_version != network::session_protocol_v8 || if (c.state != Impl::State::established ||
c.account_id != account) c.protocol_version != network::session_protocol_v8 || c.account_id != account)
continue; continue;
return m.endpoint return m.endpoint
->send_message(c.id, network::encode_session(perception, c.protocol_version)) ->send_message(c.id, network::encode_session(perception, c.protocol_version))
+56 -20
View File
@@ -374,6 +374,12 @@ struct FakeLedger final : ControlLedger {
claims.push_back({account, serial, session}); claims.push_back({account, serial, session});
return ControlLease{next_generation++, wait, granted}; return ControlLease{next_generation++, wait, granted};
} }
std::expected<std::optional<std::uint64_t>, ControlLedgerError>
primitive_life_owner(const AccountId& /*account*/) override {
if (owner_error)
return std::unexpected(*owner_error);
return life_owner;
}
std::expected<std::chrono::milliseconds, ControlLedgerError> std::expected<std::chrono::milliseconds, ControlLedgerError>
renew(const AccountId& /*account*/, std::uint64_t generation) override { renew(const AccountId& /*account*/, std::uint64_t generation) override {
renewed.push_back(generation); renewed.push_back(generation);
@@ -384,13 +390,43 @@ struct FakeLedger final : ControlLedger {
void release(const AccountId& /*account*/, std::uint64_t generation) override { void release(const AccountId& /*account*/, std::uint64_t generation) override {
released.push_back(generation); released.push_back(generation);
} }
std::optional<ControlLedgerError> claim_error, renew_error; std::optional<ControlLedgerError> claim_error, renew_error, owner_error;
std::optional<std::uint64_t> life_owner;
std::chrono::milliseconds wait{}, granted{std::chrono::milliseconds{20000}}; std::chrono::milliseconds wait{}, granted{std::chrono::milliseconds{20000}};
std::uint64_t next_generation{1}; std::uint64_t next_generation{1};
std::vector<Claim> claims; std::vector<Claim> claims;
std::vector<std::uint64_t> renewed, released; std::vector<std::uint64_t> renewed, released;
}; };
void durable_life_owner_redirects_before_control_claim(const Files& f) {
FakeLedger ledger;
ledger.life_owner = 12;
Harness h{f, 4, 64, &ledger, [](server::RegionSessionsConfig& c) {
c.life_owner_routes = {
server::LifeOwnerRoute{RegionAddress{region, "region-11.example.test:7400",
"region-11.example.test"}},
server::LifeOwnerRoute{RegionAddress{12, "region-12.example.test:7400",
"region-12.example.test"}}};
}};
const auto alice = account(0xB4);
auto client = h.connect(h.issuer.grant(alice, 1), session_protocol_v8);
CHECK(h.settled(*client) && client->state() == RegionSessionState::closed);
CHECK(client->close_reason() == Reason::moved && client->redirect());
CHECK(must(client->redirect()) ==
RegionAddress{12, "region-12.example.test:7400", "region-12.example.test"});
CHECK(ledger.claims.empty()); // The redirect happened before durable control was claimed.
FakeLedger incomplete_ledger;
incomplete_ledger.life_owner = 12;
Harness incomplete{f, 4, 64, &incomplete_ledger, [](server::RegionSessionsConfig& c) {
c.life_owner_routes = {server::LifeOwnerRoute{RegionAddress{
region, "region-11.example.test:7400", "region-11.example.test"}}};
}};
auto no_route = incomplete.connect(incomplete.issuer.grant(alice, 2), session_protocol_v8);
CHECK(incomplete.settled(*no_route) && no_route->close_reason() == Reason::capacity);
CHECK(!no_route->redirect() && incomplete_ledger.claims.empty());
}
void primitive_v5_session(const Files& f) { void primitive_v5_session(const Files& f) {
FakeLedger ledger; FakeLedger ledger;
Harness h{f, 4, 64, &ledger, [](server::RegionSessionsConfig& c) { Harness h{f, 4, 64, &ledger, [](server::RegionSessionsConfig& c) {
@@ -512,12 +548,13 @@ void cellular_v7_action_session(const Files& f) {
const auto carried = h.server->carry(101); const auto carried = h.server->carry(101);
CHECK(carried.size() == 1 && carried.front().account == alice && CHECK(carried.size() == 1 && carried.front().account == alice &&
carried.front().action_id == 88 && carried.front().tick == 101 && carried.front().action_id == 88 && carried.front().tick == 101 &&
carried.front().kind == ActionKind::cellular && carried.front().cellular_input == carried.front().kind == ActionKind::cellular &&
carried.front().cellular_input ==
std::optional{encode_cellular_action_input({101, 0, 0, false, 0, 0})}); std::optional{encode_cellular_action_input({101, 0, 0, false, 0, 0})});
actions.owner = alice; actions.owner = alice;
actions.id = 88; actions.id = 88;
actions.record = DurableActionRecord{ actions.record = DurableActionRecord{ActionOutcome{88, ActionResult::committed, 101, 1},
ActionOutcome{88, ActionResult::committed, 101, 1}, carried.front().cellular_input}; carried.front().cellular_input};
h.server->committed(carried, 1); h.server->committed(carried, 1);
std::vector<ActionOutcome> outcomes; std::vector<ActionOutcome> outcomes;
CHECK(h.until([&] { return !(outcomes = client->take_action_outcomes()).empty(); })); CHECK(h.until([&] { return !(outcomes = client->take_action_outcomes()).empty(); }));
@@ -540,25 +577,23 @@ void cellular_v7_action_session(const Files& f) {
c.actions = &actions; c.actions = &actions;
c.accept_cellular_actions = true; c.accept_cellular_actions = true;
}}; }};
auto reconnected = restarted.connect(restarted.issuer.grant(alice, 2), auto reconnected = restarted.connect(restarted.issuer.grant(alice, 2), session_protocol_v7);
session_protocol_v7);
CHECK(restarted.settled(*reconnected) && CHECK(restarted.settled(*reconnected) &&
reconnected->state() == RegionSessionState::established); reconnected->state() == RegionSessionState::established);
CHECK(reconnected->send_cellular_action(88, 101, 0, 0)); CHECK(reconnected->send_cellular_action(88, 101, 0, 0));
CHECK(restarted.until( CHECK(
[&] { return !(receipts = reconnected->take_action_receipts()).empty(); })); restarted.until([&] { return !(receipts = reconnected->take_action_receipts()).empty(); }));
CHECK(receipts.front().status == AdmitStatus::duplicate); CHECK(receipts.front().status == AdmitStatus::duplicate);
CHECK(restarted.until( CHECK(
[&] { return !(outcomes = reconnected->take_action_outcomes()).empty(); })); restarted.until([&] { return !(outcomes = reconnected->take_action_outcomes()).empty(); }));
CHECK(outcomes.front() == ActionOutcome{88, ActionResult::committed, 101, 1}); CHECK(outcomes.front() == ActionOutcome{88, ActionResult::committed, 101, 1});
CHECK(reconnected->send_cellular_action(88, 101, 1, 0)); CHECK(reconnected->send_cellular_action(88, 101, 1, 0));
CHECK(restarted.until( CHECK(
[&] { return !(receipts = reconnected->take_action_receipts()).empty(); })); restarted.until([&] { return !(receipts = reconnected->take_action_receipts()).empty(); }));
CHECK(receipts.front().status == AdmitStatus::invalid_action); CHECK(receipts.front().status == AdmitStatus::invalid_action);
Harness refused{f, 4, 64, &control}; Harness refused{f, 4, 64, &control};
auto unsupported = refused.connect(refused.issuer.grant(account(0xA9), 1), auto unsupported = refused.connect(refused.issuer.grant(account(0xA9), 1), session_protocol_v7);
session_protocol_v7);
CHECK(refused.settled(*unsupported) && unsupported->state() == RegionSessionState::closed && CHECK(refused.settled(*unsupported) && unsupported->state() == RegionSessionState::closed &&
unsupported->close_reason() == SessionCloseReason::unsupported_version); unsupported->close_reason() == SessionCloseReason::unsupported_version);
} }
@@ -575,15 +610,14 @@ void cellular_v5_spawn_and_v7_action_share_region_sessions(const Files& f) {
} }
} actions; } actions;
FakeLedger control; FakeLedger control;
Harness h{f, 4, 64, &control, [&](server::RegionSessionsConfig& c) { Harness h{
f, 4, 64, &control, [&](server::RegionSessionsConfig& c) {
c.actions = &actions; c.actions = &actions;
c.accept_cellular_actions = true; c.accept_cellular_actions = true;
c.create_primitive_life = [](const AccountId&, std::uint64_t, c.create_primitive_life = [](const AccountId&, std::uint64_t, std::uint64_t request) {
std::uint64_t request) {
return PrimitiveLifeOutcome{request, PrimitiveLifeResult::born, 901, 5}; return PrimitiveLifeOutcome{request, PrimitiveLifeResult::born, 901, 5};
}; };
c.query_primitive_life = [](const AccountId&, std::uint64_t, c.query_primitive_life = [](const AccountId&, std::uint64_t, std::uint64_t request) {
std::uint64_t request) {
return PrimitiveLifeOutcome{request, PrimitiveLifeResult::pending, 0, 0}; return PrimitiveLifeOutcome{request, PrimitiveLifeResult::pending, 0, 0};
}; };
}}; }};
@@ -600,7 +634,8 @@ void cellular_v5_spawn_and_v7_action_share_region_sessions(const Files& f) {
CHECK(spawn_client->request_primitive_life(44)); CHECK(spawn_client->request_primitive_life(44));
std::vector<PrimitiveLifeOutcome> birth_outcomes; std::vector<PrimitiveLifeOutcome> birth_outcomes;
CHECK(h.until([&] { return !(birth_outcomes = spawn_client->take_primitive_life_outcomes()).empty(); })); CHECK(h.until(
[&] { return !(birth_outcomes = spawn_client->take_primitive_life_outcomes()).empty(); }));
CHECK(birth_outcomes.front() == PrimitiveLifeOutcome{44, PrimitiveLifeResult::born, 901, 5}); CHECK(birth_outcomes.front() == PrimitiveLifeOutcome{44, PrimitiveLifeResult::born, 901, 5});
CHECK(action_client->send_cellular_action(55, 101, 0, 0)); CHECK(action_client->send_cellular_action(55, 101, 0, 0));
@@ -1469,6 +1504,7 @@ void storm(const Files& f) {
int main() try { int main() try {
const auto files = credentials(); const auto files = credentials();
login_rules(files); login_rules(files);
durable_life_owner_redirects_before_control_claim(files);
primitive_v5_session(files); primitive_v5_session(files);
cellular_v6_session(files); cellular_v6_session(files);
cellular_v7_action_session(files); cellular_v7_action_session(files);