Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m46s
Foundation / checks (postgres) (push) Canceled after 0s
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 (checks) (push) Canceled after 3m44s
Foundation / checks (client) (push) Canceled after 3m49s
Foundation / checks (network) (push) Canceled after 1m59s
evo-server gains an opt-in M10 listener (--m10-*, QUIC builds only): RegionSessions with the PostgreSQL control and action ledgers on their own connection under the host's lease, carried actions submitted to the host and its acknowledgements and drops turned into outcomes. evo-session-probe is a dev-only headless client. Real process tests cover intentions, durable action outcomes, SIGKILL before and after an action's commit, duplicate retries and the probe. The simulated watermark now reaches every session every tick, so clients always know the server tick. ADR-0092. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
192 lines
8.8 KiB
C++
192 lines
8.8 KiB
C++
#pragma once
|
|
// M10.4c QUIC session listener for evo-server (ADR-0091, ADR-0092). Compiled only with
|
|
// EVO_SERVER_M10 (EVO_BUILD_QUIC with the PostgreSQL build); otherwise the flags are refused
|
|
// at parse time. Opt-in, development and test only: nothing deploys it yet.
|
|
//
|
|
// Each loop iteration after the host's step: the sessions see the host's authority; actions
|
|
// scheduled for upcoming ticks are submitted to the host, which commits them with the batch
|
|
// of the tick they ride; the host's action acknowledgements and drops become outcomes.
|
|
// Session control and action records use their own connection under the host's lease.
|
|
#include "evo/persistence/postgres.hpp"
|
|
#include "evo/persistence/region_lease.hpp"
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <string>
|
|
|
|
#ifdef EVO_SERVER_M10
|
|
#include "evo/server/pg_slot.hpp"
|
|
#include "evo/server/postgres_action_ledger.hpp"
|
|
#include "evo/server/postgres_control_ledger.hpp"
|
|
#include "evo/server/region_host.hpp"
|
|
#include "evo/server/region_sessions.hpp"
|
|
#include <algorithm>
|
|
#include <charconv>
|
|
#include <chrono>
|
|
#include <cstdio>
|
|
#include <fstream>
|
|
#include <iterator>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <vector>
|
|
#endif
|
|
|
|
namespace evo::server_app {
|
|
struct M10Options {
|
|
std::string listen, certificate, key, issuer, environment;
|
|
[[nodiscard]] bool any() const noexcept {
|
|
return !listen.empty() || !certificate.empty() || !key.empty() || !issuer.empty() ||
|
|
!environment.empty();
|
|
}
|
|
[[nodiscard]] bool complete() const noexcept {
|
|
return !listen.empty() && !certificate.empty() && !key.empty() && !issuer.empty() &&
|
|
!environment.empty();
|
|
}
|
|
};
|
|
// Actions are handed to the host as soon as they are scheduled (at most 8 ticks ahead).
|
|
inline constexpr std::uint64_t m10_carry_ahead_ticks = 8;
|
|
|
|
#ifdef EVO_SERVER_M10
|
|
inline constexpr bool m10_available = true;
|
|
template <typename Host> class M10Bridge {
|
|
public:
|
|
// Empty bridge without M10 options; nullopt on a startup failure (the caller exits).
|
|
static std::optional<M10Bridge> open(const M10Options& o, Host& host,
|
|
persistence::RegionKey key, std::uint64_t frame,
|
|
const persistence::PgOptions& pg) {
|
|
M10Bridge bridge;
|
|
if (!o.any())
|
|
return bridge;
|
|
const auto colon = o.listen.rfind(':');
|
|
std::uint16_t port = 0;
|
|
if (colon == std::string::npos ||
|
|
std::from_chars(o.listen.data() + colon + 1, o.listen.data() + o.listen.size(), port)
|
|
.ec != std::errc{})
|
|
return fail("error m10-listen");
|
|
const auto issuer = read(o.issuer);
|
|
if (issuer.size() != network::grant_public_key_bytes)
|
|
return fail("error m10-issuer");
|
|
server::RegionSessionsConfig config;
|
|
config.transport.listen_address = o.listen.substr(0, colon);
|
|
config.transport.port = port;
|
|
config.transport.certificate_pem_file = o.certificate;
|
|
config.transport.key_pem_file = o.key;
|
|
config.control.environment = o.environment == "dev" ? network::DeploymentEnvironment::dev
|
|
: o.environment == "stage"
|
|
? network::DeploymentEnvironment::stage
|
|
: network::DeploymentEnvironment::world0;
|
|
config.control.world = {key.world, frame};
|
|
config.control.region = key.region;
|
|
config.control.epoch = 1; // Follows the host's epoch from the first poll.
|
|
server::IssuerKey issuer_key;
|
|
issuer_key.key_id = 1;
|
|
std::copy(issuer.begin(), issuer.end(), issuer_key.public_key.begin());
|
|
config.issuer_keys.push_back(issuer_key);
|
|
bridge.host_ = &host;
|
|
bridge.slot_ = std::make_unique<server::PgSlot>(pg, std::chrono::seconds{2});
|
|
const auto lease = [h = &host] { return h->authority(); };
|
|
bridge.control_ =
|
|
std::make_unique<server::PostgresControlLedger>(*bridge.slot_, key, lease);
|
|
bridge.actions_ = std::make_unique<server::PostgresActionLedger>(*bridge.slot_, key, lease);
|
|
config.ledger = bridge.control_.get();
|
|
config.actions = bridge.actions_.get();
|
|
auto sessions = server::RegionSessions::create(std::move(config));
|
|
if (!sessions)
|
|
return fail(sessions.error() == server::RegionSessionsError::keys ? "error m10-issuer"
|
|
: sessions.error() == server::RegionSessionsError::transport
|
|
? "error m10-listen"
|
|
: "error m10-config");
|
|
bridge.sessions_ = std::move(*sessions);
|
|
std::printf("m10 listening %u\n", static_cast<unsigned>(bridge.sessions_->port()));
|
|
return bridge;
|
|
}
|
|
[[nodiscard]] bool enabled() const noexcept {
|
|
return sessions_ != nullptr;
|
|
}
|
|
void after_step() {
|
|
if (!sessions_)
|
|
return;
|
|
auto& host = *host_;
|
|
const auto& s = host.status();
|
|
const server::HostFacts facts{s.ready && s.phase == server::HostPhase::running, s.epoch,
|
|
s.simulated_tick};
|
|
sessions_->poll(facts, wall_seconds(), std::chrono::steady_clock::now());
|
|
if (facts.ready)
|
|
for (const auto& a : sessions_->carry(s.simulated_tick + m10_carry_ahead_ticks)) {
|
|
const persistence::ActionRecord record{a.account.bytes, a.action_id, a.tick,
|
|
static_cast<std::uint16_t>(a.kind)};
|
|
if (!host.submit_action(record)) // Never reached the host: final.
|
|
sessions_->dropped(std::span{&a, 1}, true);
|
|
}
|
|
std::map<std::uint64_t, std::vector<network::CarriedAction>> committed;
|
|
for (const auto& ack : host.take_action_acks())
|
|
committed[ack.commit_sequence].push_back(carried(ack.record));
|
|
for (const auto& [sequence, actions] : committed) {
|
|
sessions_->committed(actions, sequence);
|
|
std::printf("m10 committed actions=%zu commit=%llu\n", actions.size(),
|
|
static_cast<unsigned long long>(sequence));
|
|
}
|
|
for (const auto& d : host.take_dropped_actions()) {
|
|
const auto a = carried(d.record);
|
|
sessions_->dropped(std::span{&a, 1}, d.final);
|
|
std::printf("m10 dropped action=%llu final=%d\n",
|
|
static_cast<unsigned long long>(d.record.action_id), d.final ? 1 : 0);
|
|
}
|
|
for (const auto& e : sessions_->take_events())
|
|
std::printf("m10 %s session=%llu account=%s reason=%u\n",
|
|
e.kind == server::RegionSessionEvent::Kind::opened ? "opened"
|
|
: e.kind == server::RegionSessionEvent::Kind::closed ? "closed"
|
|
: "refused",
|
|
static_cast<unsigned long long>(e.session), e.account.c_str(),
|
|
static_cast<unsigned>(e.reason));
|
|
}
|
|
void shutdown() {
|
|
if (sessions_)
|
|
sessions_->shutdown(std::chrono::steady_clock::now());
|
|
}
|
|
|
|
private:
|
|
static network::CarriedAction carried(const persistence::ActionRecord& r) {
|
|
network::CarriedAction a;
|
|
a.account.bytes = r.account;
|
|
a.action_id = r.action_id;
|
|
a.tick = r.tick;
|
|
a.kind = network::ActionKind::probe;
|
|
return a;
|
|
}
|
|
static std::uint64_t wall_seconds() {
|
|
return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(
|
|
std::chrono::system_clock::now().time_since_epoch())
|
|
.count());
|
|
}
|
|
static std::optional<M10Bridge> fail(const char* message) {
|
|
std::puts(message);
|
|
return std::nullopt;
|
|
}
|
|
static std::string read(const std::string& path) {
|
|
std::ifstream file{path, std::ios::binary};
|
|
return {std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
|
|
}
|
|
Host* host_{};
|
|
// Heap-held so the sessions' pointers stay valid when the bridge moves.
|
|
std::unique_ptr<server::PgSlot> slot_;
|
|
std::unique_ptr<server::PostgresControlLedger> control_;
|
|
std::unique_ptr<server::PostgresActionLedger> actions_;
|
|
std::unique_ptr<server::RegionSessions> sessions_; // Destroyed first: uses the ledgers.
|
|
};
|
|
#else
|
|
inline constexpr bool m10_available = false;
|
|
template <typename Host> class M10Bridge {
|
|
public:
|
|
static std::optional<M10Bridge> open(const M10Options&, Host&, persistence::RegionKey,
|
|
std::uint64_t, const persistence::PgOptions&) {
|
|
return M10Bridge{};
|
|
}
|
|
[[nodiscard]] bool enabled() const noexcept {
|
|
return false;
|
|
}
|
|
void after_step() {}
|
|
void shutdown() {}
|
|
};
|
|
#endif
|
|
} // namespace evo::server_app
|