diff --git a/crates/coa-core/examples/portable_smoke.rs b/crates/coa-core/examples/portable_smoke.rs index b60b59d..995693b 100644 --- a/crates/coa-core/examples/portable_smoke.rs +++ b/crates/coa-core/examples/portable_smoke.rs @@ -15,12 +15,21 @@ //! update bring the realm's own character to the newest revision, in place //! recover finish or abort interrupted imports and updates //! show revisions and realm bindings +//! session-offer (owner store) offer a character and open its runtime session +//! session-import (host store: --host-store) keep the copy, import it with the session +//! armed (online through the core's import service when --ra-port and --job-dir are given, else offline) +//! host-run (host store) run the Host: automatic baseline, a checkpoint every +//! --interval seconds (default 60), the final checkpoint at logout; messages go straight to the owner store +//! dump the canonical JSON of the current revision (what a core import job carries) //! ``` use std::path::PathBuf; use coa_core::db::Db; use coa_core::portable::realm::{self, ImportOptions}; +use coa_core::portable::session::live::LiveBridge; +use coa_core::portable::session::protocol::SessionOffer; +use coa_core::portable::session::{HostConfig, HostService, OwnerService}; use coa_core::portable::{CharacterId, Store}; use coa_core::realms::Mode; @@ -63,6 +72,11 @@ fn run() -> Result<(), String> { let db = Db::with_tools(tools, port, user, &password, realm_mode); let mut store = Store::open(&store_dir).map_err(|e| e.to_string())?; let opts = ImportOptions { game_server_users: vec![user.to_string(), "acore".to_string()], ..ImportOptions::default() }; + let host_dir = PathBuf::from(take("--host-store", Some(&format!("{}-host", store_dir.display())))?); + let ra_port: Option = take("--ra-port", Some("0"))?.parse().ok().filter(|p| *p != 0); + let ra_user = take("--ra-user", Some("local"))?; + let job_dir = take("--job-dir", Some(""))?; + let interval: u64 = take("--interval", Some("60"))?.parse().map_err(|_| "--interval is not a number".to_string())?; let id = |s: &str| -> Result { s.parse().map_err(|_| format!("{s:?} is not a character id")) }; let command = if args.is_empty() { usage() } else { args.remove(0) }; @@ -102,6 +116,63 @@ fn run() -> Result<(), String> { println!("{}: {:?}", r.import_id, r.resolution); } } + ("session-offer", [character, server, out]) => { + let offer = OwnerService::new(&mut store).offer(id(character)?, server).map_err(|e| e.to_string())?; + std::fs::write(out, serde_json::to_vec_pretty(&offer).map_err(|e| e.to_string())?).map_err(|e| e.to_string())?; + println!("session {} offered at canonical revision {} -> {out}", offer.session_id, offer.canonical_revision); + } + ("session-import", [file, account, server]) => { + let offer: SessionOffer = serde_json::from_slice(&std::fs::read(file).map_err(|e| e.to_string())?).map_err(|e| e.to_string())?; + let mut host = Store::open(&host_dir).map_err(|e| e.to_string())?; + let profile = host.default_profile().map_err(|e| e.to_string())?; + HostService::new(&mut host, server, HostConfig::default()).accept_offer(profile, &offer).map_err(|e| e.to_string())?; + let account: u32 = account.parse().map_err(|_| "account")?; + let outcome = match (ra_port, job_dir.is_empty()) { + (Some(port), false) => { + let mut ra = coa_core::ra::Ra::connect_to(port, &ra_user, &std::env::var("COA_RA_PASSWORD").map_err(|_| "set COA_RA_PASSWORD".to_string())?).map_err(|e| e.to_string())?; + realm::online::import_character_online(&mut ra, &mut host, offer.character_id, server, account, &opts, std::path::Path::new(&job_dir), Some(offer.session_id)).map_err(|e| e.to_string())? + } + _ => realm::import_character_in_session(&db, &mut host, offer.character_id, server, account, &opts, Some(offer.session_id)).map_err(|e| e.to_string())?, + }; + HostService::new(&mut host, server, HostConfig::default()).bind(offer.session_id, outcome.local_guid).map_err(|e| e.to_string())?; + println!("imported as local character {} \"{}\"; session {} is armed: the realm takes the baseline at its first load", outcome.local_guid, outcome.final_name, offer.session_id); + } + ("host-run", [owner_dir, server]) => { + let mut owner = Store::open(&PathBuf::from(owner_dir)).map_err(|e| e.to_string())?; + let mut host = Store::open(&host_dir).map_err(|e| e.to_string())?; + let mut service = HostService::new(&mut host, server, HostConfig { checkpoint_interval_secs: interval }); + let start = std::time::Instant::now(); + println!("host running for {server}; Ctrl-C to stop"); + loop { + let ra = ra_port.and_then(|port| std::env::var("COA_RA_PASSWORD").ok().and_then(|pw| coa_core::ra::Ra::connect_to(port, &ra_user, &pw).ok())); + let mut bridge = match ra { + Some(ra) => LiveBridge::new(&db, ra), + None => LiveBridge::without_console(&db), + }; + match service.tick(&mut bridge, start.elapsed().as_secs()) { + Ok(events) => events.iter().for_each(|e| println!("{e:?}")), + Err(e) => eprintln!("tick failed: {e}"), + } + for m in service.outbox().map_err(|e| e.to_string())? { + let ack = if m.started { OwnerService::new(&mut owner).handle_started(&m.bytes) } else { OwnerService::new(&mut owner).handle_checkpoint(&m.bytes) }; + match ack { + Ok(ack) => { + println!("owner: session {} #{}: {:?} (canonical revision {})", ack.session_id, ack.sequence, ack.outcome, ack.canonical_revision); + if let Err(e) = service.receive_ack(&mut bridge, &ack) { + eprintln!("acknowledgement failed: {e}"); + } + } + Err(e) => eprintln!("owner refused a message: {e}"), + } + } + std::thread::sleep(std::time::Duration::from_secs(1)); + } + } + ("dump", [character]) => { + let model = store.load_current(id(character)?).map_err(|e| e.to_string())?; + let json = coa_core::portable::snapshot::canonical_json(&model).map_err(|e| e.to_string())?; + println!("{}", String::from_utf8_lossy(&json)); + } ("show", [character]) => { let c = id(character)?; let r = store.character(c).map_err(|e| e.to_string())?; diff --git a/crates/coa-core/src/portable/realm/export.rs b/crates/coa-core/src/portable/realm/export.rs index 3a71364..df38354 100644 --- a/crates/coa-core/src/portable/realm/export.rs +++ b/crates/coa-core/src/portable/realm/export.rs @@ -16,6 +16,7 @@ use super::super::versions::PORTABLE_CHARACTER_FORMAT_VERSION; use super::blockers::{is_internal_account, Blocker}; use super::policy::{classify_setting, Disposition, QUARANTINE_EXTENSION, SETTINGS_POLICY_VERSION}; use super::script::{RawExport, Row}; +use super::super::session::bridge::{RowState, SessionRow}; /// Only the cosmetic bits of `playerFlags` are carried: hide helm (0x400) and hide cloak (0x800). pub const COSMETIC_FLAG_MASK: u32 = 0x0C00; @@ -31,6 +32,8 @@ pub struct ExportRequest<'a> { pub prior_items: &'a HashMap, /// Active pet mappings of that character on this realm: local pet number -> (portable pet id, identity). pub prior_pets: &'a HashMap, + /// A character with a portable session row may be read while it is online: the core saved it together with its marker. + pub allow_online_session: bool, } #[derive(Debug)] @@ -43,6 +46,8 @@ pub struct Exported { pub observations: Vec, /// One entry per exported pet, ready for the pet mappings. pub pet_observations: Vec, + /// The portable session marker of the same snapshot, when the realm has one for this character. + pub session: Option, /// Things that were left out or looked wrong; shown to the user, never silently dropped. pub warnings: Vec, } @@ -53,10 +58,15 @@ fn corrupt(msg: impl Into) -> Result { /// Why this character cannot be exported now. Empty = it can. pub fn blockers(raw: &RawExport) -> Result> { + blockers_with(raw, false) +} + +pub fn blockers_with(raw: &RawExport, allow_online_session: bool) -> Result> { let chars = raw.section("chars")?; let row = chars.iter().next().ok_or_else(|| PortableError::NoSuchRealmCharacter(0))?; let mut out = Vec::new(); - if row.u64("online")? != 0 { + let has_session = raw.has("portable_session") && raw.section("portable_session")?.iter().next().is_some(); + if row.u64("online")? != 0 && !(allow_online_session && has_session) { out.push(Blocker::Online); } if row.u64("deleted")? != 0 { @@ -91,7 +101,7 @@ pub fn build(raw: &RawExport, req: &ExportRequest<'_>) -> Result { if c.u32("guid")? != req.local_guid { return corrupt("the realm answered for another character"); } - let blocked = blockers(raw)?; + let blocked = blockers_with(raw, req.allow_online_session)?; if !blocked.is_empty() { return Err(PortableError::NotExportable(blocked)); } @@ -222,7 +232,27 @@ pub fn build(raw: &RawExport, req: &ExportRequest<'_>) -> Result { .normalized(); model.validate()?; - Ok(Exported { model, local_guid: req.local_guid, account: c.u32("account")?, observations, pet_observations, warnings }) + let session = session_row(raw)?; + Ok(Exported { model, local_guid: req.local_guid, account: c.u32("account")?, observations, pet_observations, session, warnings }) +} + +/// The core's marker row of this character, if it has one. +pub fn session_row(raw: &RawExport) -> Result> { + if !raw.has("portable_session") { + return Ok(None); + } + let Some(r) = raw.section("portable_session")?.iter().next() else { return Ok(None) }; + let state = RowState::from_code(r.u64("state")?).ok_or_else(|| PortableError::CorruptSnapshot("the portable session row has an unknown state".into()))?; + Ok(Some(SessionRow { + guid: r.u32("guid")?, + session_id: r.text("session_id")?.parse()?, + character_id: r.text("character_id")?.parse()?, + imported_revision: r.u64("imported_revision")?, + generation: r.u32("generation")?, + state, + checkpoint_seq: r.u64("checkpoint_seq")?, + save_seq: r.u64("save_seq")?, + })) } fn pairs(raw: &RawExport, section: &str) -> Result> { diff --git a/crates/coa-core/src/portable/realm/import.rs b/crates/coa-core/src/portable/realm/import.rs index 3107e25..99a317c 100644 --- a/crates/coa-core/src/portable/realm/import.rs +++ b/crates/coa-core/src/portable/realm/import.rs @@ -22,10 +22,10 @@ use std::time::Duration; use crate::db::Db; use super::super::error::{PortableError, Result}; -use super::super::ids::{CharacterId, ContentId, ImportId}; +use super::super::ids::{CharacterId, ContentId, ImportId, SessionId}; use super::super::model::{PortableCharacter, Ruleset}; use super::super::store::{pet_identity, ImportAllocation, ImportState, JournalEntry, JournalKind, PlannedItem, PlannedPet, Store}; -use super::plan::{build_plan, parse_report, Allocation, ImportPlan, PlanContext, IMPORT_LOCK}; +use super::plan::{build_plan, parse_report, Allocation, ImportPlan, PlanContext, SessionArm, IMPORT_LOCK}; use super::script::{self, parse_output, Query}; use super::sqlenc::Val; use super::{realm_error, ruleset_of}; @@ -251,6 +251,11 @@ pub fn run_realm_import(db: &Db, plan: &ImportPlan) -> Result<(Allocation, bool) /// Import the character's current canonical revision into a stopped realm. pub fn import_character(db: &Db, store: &mut Store, character_id: CharacterId, server_id: &str, account: u32, opts: &ImportOptions) -> Result { + import_character_in_session(db, store, character_id, server_id, account, opts, None) +} + +/// The same, arming the runtime portable session `session` on the arrival (the core takes the baseline at its first load). +pub fn import_character_in_session(db: &Db, store: &mut Store, character_id: CharacterId, server_id: &str, account: u32, opts: &ImportOptions, session: Option) -> Result { let record = store.character(character_id)?; let model = store.load_snapshot(character_id, record.revision)?; if store.server_mappings(character_id)?.iter().any(|m| m.server_id == server_id) { @@ -267,7 +272,7 @@ pub fn import_character(db: &Db, store: &mut Store, character_id: CharacterId, s let pets = planned_pets(&model); let ticket = store.begin_import(character_id, server_id, record.revision, &items, &pets)?; - let plan = match build_plan(&model, &PlanContext { ruleset: ruleset_of(db), account, revision: record.revision, nonce: ticket.nonce, max_characters_per_account: opts.max_characters_per_account, game_server_users: &opts.game_server_users, probe: &probe }) { + let plan = match build_plan(&model, &PlanContext { ruleset: ruleset_of(db), account, revision: record.revision, nonce: ticket.nonce, max_characters_per_account: opts.max_characters_per_account, game_server_users: &opts.game_server_users, probe: &probe, session: session.map(|session_id| SessionArm { session_id, character_id, generation: 1 }) }) { Ok(plan) => plan, Err(e) => { store.abort_import(ticket.import_id, &format!("the import plan could not be built: {e}"))?; diff --git a/crates/coa-core/src/portable/realm/import_tests.rs b/crates/coa-core/src/portable/realm/import_tests.rs index 19816e2..828397e 100644 --- a/crates/coa-core/src/portable/realm/import_tests.rs +++ b/crates/coa-core/src/portable/realm/import_tests.rs @@ -16,7 +16,7 @@ fn probe_with(tables: &[&str]) -> SchemaProbe { } fn ctx<'a>(probe: &'a SchemaProbe, users: &'a [String]) -> PlanContext<'a> { - PlanContext { ruleset: Ruleset::Coa, account: 5001, revision: 3, nonce: [11, 22, 33, 44], max_characters_per_account: 10, game_server_users: users, probe } + PlanContext { ruleset: Ruleset::Coa, account: 5001, revision: 3, nonce: [11, 22, 33, 44], max_characters_per_account: 10, game_server_users: users, probe, session: None } } fn users() -> Vec { diff --git a/crates/coa-core/src/portable/realm/live_import.rs b/crates/coa-core/src/portable/realm/live_import.rs index 8e9354b..7709ebc 100644 --- a/crates/coa-core/src/portable/realm/live_import.rs +++ b/crates/coa-core/src/portable/realm/live_import.rs @@ -22,24 +22,24 @@ use super::import::*; use super::plan::{build_plan, PlanContext}; use super::*; -pub(super) const B: &str = "realm-b"; -pub(super) const ACCOUNT: u32 = 7001; +pub(crate) const B: &str = "realm-b"; +pub(crate) const ACCOUNT: u32 = 7001; -pub(super) fn tools(var: &str) -> Option<(PathBuf, u16, String)> { +pub(crate) fn tools(var: &str) -> Option<(PathBuf, u16, String)> { let spec = std::env::var(var).ok()?; let p: Vec<&str> = spec.split('|').collect(); assert_eq!(p.len(), 3, "{var} = ||"); Some((PathBuf::from(p[0]), p[1].parse().expect("port"), p[2].to_string())) } -pub(super) struct Realms { - pub(super) a: Db, - pub(super) b: Db, +pub(crate) struct Realms { + pub(crate) a: Db, + pub(crate) b: Db, /// A session of the game server's database user, to stand in for a running realm. - pub(super) server_user: Db, + pub(crate) server_user: Db, } -pub(super) fn realms() -> Option { +pub(crate) fn realms() -> Option { let (bin_a, port_a, pw_a) = tools("COA_PORTABLE_LIVE")?; let (bin_b, port_b, pw_b) = tools("COA_PORTABLE_LIVE_B")?; Some(Realms { @@ -49,14 +49,14 @@ pub(super) fn realms() -> Option { }) } -pub(super) fn opts() -> ImportOptions { +pub(crate) fn opts() -> ImportOptions { ImportOptions { recovery_grace: Duration::ZERO, lock_wait_seconds: 2, ..ImportOptions::default() } } /// Put realm B back to its baseline (the characters of `realm-b-fixture.sql` and the template characters): removes every /// character with a guid above the fixture's and what hangs off it. Every live test starts with this, so the tests do not /// depend on each other or on their order. -pub(super) fn reset_b(db: &Db) { +pub(crate) fn reset_b(db: &Db) { let mut sql = vec!["DROP TRIGGER IF EXISTS acore_characters.coa_test_fail;".to_string()]; for (table, column) in super::plan::CHARACTER_KEYED { sql.push(format!("DELETE FROM acore_characters.`{table}` WHERE `{column}` > 3010;")); @@ -70,20 +70,20 @@ pub(super) fn reset_b(db: &Db) { ")).unwrap(); } -pub(super) fn on_b(store: &Store, id: CharacterId) -> Vec { +pub(crate) fn on_b(store: &Store, id: CharacterId) -> Vec { store.server_mappings(id).unwrap().into_iter().filter(|m| m.server_id == B).collect() } -pub(super) fn first(db: &Db, sql: &str) -> String { +pub(crate) fn first(db: &Db, sql: &str) -> String { db.query(sql).unwrap().lines().next().unwrap_or("").to_string() } -pub(super) fn number(db: &Db, sql: &str) -> u64 { +pub(crate) fn number(db: &Db, sql: &str) -> u64 { first(db, sql).trim().parse().unwrap_or_else(|_| panic!("not a number: {sql}")) } /// Row counts of every table of the characters schema: the "nothing changed" proof. -pub(super) fn counts(db: &Db) -> BTreeMap { +pub(crate) fn counts(db: &Db) -> BTreeMap { let tables = db.tables("acore_characters").unwrap(); let sql = tables.iter().map(|t| format!("SELECT '{t}', COUNT(*) FROM acore_characters.`{t}`")).collect::>().join(" UNION ALL "); db.query(&sql).unwrap().lines().map(|l| { @@ -92,18 +92,18 @@ pub(super) fn counts(db: &Db) -> BTreeMap { }).collect() } -pub(super) fn fresh_store() -> (Store, crate::portable::ids::ProfileId) { +pub(crate) fn fresh_store() -> (Store, crate::portable::ids::ProfileId) { let mut store = Store::open_in_memory().unwrap(); let profile = store.default_profile().unwrap(); (store, profile) } -pub(super) fn make(r: &Realms, store: &mut Store, profile: crate::portable::ids::ProfileId, guid: u32) -> CharacterId { +pub(crate) fn make(r: &Realms, store: &mut Store, profile: crate::portable::ids::ProfileId, guid: u32) -> CharacterId { make_portable(&r.a, store, profile, "realm-a", guid).unwrap().character_id } /// What a trip through a realm legitimately does not preserve. Everything else must come back identical. -pub(super) fn expected_after_a_trip(canonical: &PortableCharacter, back: &PortableCharacter) -> PortableCharacter { +pub(crate) fn expected_after_a_trip(canonical: &PortableCharacter, back: &PortableCharacter) -> PortableCharacter { let mut c = canonical.clone(); c.identity.name = back.identity.name.clone(); // a taken or reserved name arrives under a temporary one for item in &mut c.items { @@ -117,7 +117,7 @@ pub(super) fn expected_after_a_trip(canonical: &PortableCharacter, back: &Portab c.normalized() } -pub(super) fn read_back(r: &Realms, store: &Store, id: CharacterId) -> PortableCharacter { +pub(crate) fn read_back(r: &Realms, store: &Store, id: CharacterId) -> PortableCharacter { let guid = store.server_mappings(id).unwrap().iter().find(|m| m.server_id == B).unwrap().local_guid; let prior = store.active_item_lookup(id, B).unwrap(); let pets = store.active_pet_lookup(id, B).unwrap(); @@ -298,7 +298,7 @@ fn imports_are_refused_before_anything_is_written() { // ... and if the server starts between the preflight and the transaction, the transaction's own guard stops it let probe = super::probe(&r.b).unwrap(); let users = vec!["acore".to_string()]; - let plan = build_plan(&model, &PlanContext { ruleset: Ruleset::Coa, account: ACCOUNT, revision: 1, nonce: [1, 2, 3, 4], max_characters_per_account: 10, game_server_users: &users, probe: &probe }).unwrap(); + let plan = build_plan(&model, &PlanContext { ruleset: Ruleset::Coa, account: ACCOUNT, revision: 1, nonce: [1, 2, 3, 4], max_characters_per_account: 10, game_server_users: &users, probe: &probe, session: None }).unwrap(); assert!(run_realm_import(&r.b, &plan).is_err(), "the in-transaction guard must refuse while the game server's session exists"); session.join().unwrap(); @@ -352,7 +352,7 @@ fn a_crash_between_the_two_commits_is_recovered_from_the_marker_in_the_realm() { // the realm commits ... and the Manager "dies" before it records anything let ticket = store.begin_import(id, B, 1, &import::planned_items(&model), &[]).unwrap(); let users = vec!["acore".to_string()]; - let plan = build_plan(&model, &PlanContext { ruleset: Ruleset::Coa, account: ACCOUNT, revision: 1, nonce: ticket.nonce, max_characters_per_account: 10, game_server_users: &users, probe: &probe }).unwrap(); + let plan = build_plan(&model, &PlanContext { ruleset: Ruleset::Coa, account: ACCOUNT, revision: 1, nonce: ticket.nonce, max_characters_per_account: 10, game_server_users: &users, probe: &probe, session: None }).unwrap(); let (alloc, committed) = run_realm_import(&r.b, &plan).unwrap(); assert!(committed); assert_eq!(store.import_entry(ticket.import_id).unwrap().state, ImportState::Prepared); @@ -418,7 +418,7 @@ fn a_realm_that_no_longer_matches_the_plan_is_flagged_not_guessed_about() { let probe = super::probe(&r.b).unwrap(); let ticket = store.begin_import(id, B, 1, &import::planned_items(&model), &[]).unwrap(); let users = vec!["acore".to_string()]; - let plan = build_plan(&model, &PlanContext { ruleset: Ruleset::Coa, account: ACCOUNT, revision: 1, nonce: ticket.nonce, max_characters_per_account: 10, game_server_users: &users, probe: &probe }).unwrap(); + let plan = build_plan(&model, &PlanContext { ruleset: Ruleset::Coa, account: ACCOUNT, revision: 1, nonce: ticket.nonce, max_characters_per_account: 10, game_server_users: &users, probe: &probe, session: None }).unwrap(); let (alloc, _) = run_realm_import(&r.b, &plan).unwrap(); // someone deletes an item of the new character before the Manager recovers r.b.query(&format!("DELETE FROM acore_characters.item_instance WHERE guid = {}", alloc.item_base + 3)).unwrap(); @@ -518,7 +518,7 @@ fn staged() -> bool { } /// Paths at which two JSON values differ (capped). -pub(super) fn json_diff(path: &str, a: &serde_json::Value, b: &serde_json::Value, out: &mut Vec) { +pub(crate) fn json_diff(path: &str, a: &serde_json::Value, b: &serde_json::Value, out: &mut Vec) { use serde_json::Value::{Array, Null, Object}; if out.len() >= 60 { return; diff --git a/crates/coa-core/src/portable/realm/mod.rs b/crates/coa-core/src/portable/realm/mod.rs index 2ceeff2..e85b4ec 100644 --- a/crates/coa-core/src/portable/realm/mod.rs +++ b/crates/coa-core/src/portable/realm/mod.rs @@ -12,6 +12,7 @@ pub mod blockers; pub mod export; pub mod import; +pub mod online; pub mod plan; pub mod policy; pub mod reconcile; @@ -31,8 +32,8 @@ use super::model::Ruleset; use super::store::{RealmRegistration, Store}; pub use blockers::Blocker; -pub use export::{build, ExportRequest, Exported}; -pub use import::{import_character, preflight, recover_imports, resolve_import, ImportOptions, ImportOutcome, ImportProblem, PreflightReport, Resolution}; +pub use export::{build, session_row, ExportRequest, Exported}; +pub use import::{import_character, import_character_in_session, preflight, recover_imports, resolve_import, ImportOptions, ImportOutcome, ImportProblem, PreflightReport, Resolution}; pub use reconcile::{begin_session, reconcile_session, resolve_import_update, update_realm_character, ReconcileOutcome, SessionStart, UpdateOutcome}; pub use script::SchemaProbe; @@ -155,7 +156,15 @@ pub fn export_character(db: &Db, local_guid: u32, character_id: Option, prior_items: &HashMap, prior_pets: &HashMap) -> Result { let probe = probe(db)?; let raw = read_raw(db, local_guid, &probe)?; - export::build(&raw, &ExportRequest { ruleset: ruleset_of(db), local_guid, character_id, prior_items, prior_pets }) + export::build(&raw, &ExportRequest { ruleset: ruleset_of(db), local_guid, character_id, prior_items, prior_pets, allow_online_session: false }) +} + +/// Read a character that may be online, because the core holds a portable session row for it: the character as it was last saved, +/// and the marker of the same consistent snapshot. +pub fn read_session_character(db: &Db, local_guid: u32, character_id: CharacterId, prior_items: &HashMap, prior_pets: &HashMap) -> Result { + let probe = probe(db)?; + let raw = read_raw(db, local_guid, &probe)?; + export::build(&raw, &ExportRequest { ruleset: ruleset_of(db), local_guid, character_id: Some(character_id), prior_items, prior_pets, allow_online_session: true }) } #[derive(Debug)] @@ -194,7 +203,7 @@ mod import_tests; #[cfg(test)] mod live; #[cfg(test)] -mod live_import; +pub(crate) mod live_import; #[cfg(test)] mod live_roundtrip; #[cfg(test)] diff --git a/crates/coa-core/src/portable/realm/online.rs b/crates/coa-core/src/portable/realm/online.rs new file mode 100644 index 0000000..f132297 --- /dev/null +++ b/crates/coa-core/src/portable/realm/online.rs @@ -0,0 +1,196 @@ +//! The production importer, Manager side: into a **running** realm, by the core's `PortableImportService`. +//! +//! ```text +//! begin_import (journal "prepared") -> /.job -> RA `portable import ` -> .result +//! (the core imports in one transaction and answers) +//! -> finish_import (mappings + journal "committed") +//! ``` +//! +//! Only the job id travels through RA. The job file is written to a fixed directory (the realm's `PortableImport.JobDir`) +//! under a temporary name and renamed, and holds exactly what the realm needs: the character without extensions and without +//! settings the policy does not carry. + +use std::path::{Path, PathBuf}; + +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; + +use crate::ra::Ra; + +use super::super::error::{PortableError, Result}; +use super::super::ids::{CharacterId, ImportId, SessionId}; +use super::super::model::PortableCharacter; +use super::super::snapshot; +use super::super::store::{ImportAllocation, Store}; +use super::import::{planned_items, planned_pets, ImportOptions, ImportOutcome}; +use super::policy::{classify_setting, Disposition}; + +/// The header line of a job file. +#[derive(Debug, Serialize)] +struct JobHeader { + job_id: ImportId, + nonce: [u32; 4], + account: u32, + revision: u64, + max_characters_per_account: u32, + snapshot_sha256: String, + character_id: CharacterId, + session: Option, +} + +#[derive(Debug, Serialize)] +struct SessionHeader { + session_id: SessionId, + generation: u32, +} + +/// What the realm needs of a character: no extensions (opaque module payloads the realm cannot apply), only the settings the +/// policy carries. +pub fn job_model(model: &PortableCharacter) -> PortableCharacter { + let mut m = model.clone(); + m.extensions.clear(); + m.settings.retain(|source, _| classify_setting(source, m.ruleset) == Disposition::Carry); + m +} + +/// The bytes of a job file: the header, a newline, the canonical JSON of the character. +pub fn job_bytes(job_id: ImportId, nonce: [u32; 4], account: u32, revision: u64, max_characters: u32, model: &PortableCharacter, session: Option<(SessionId, u32)>) -> Result> { + let snapshot_json = snapshot::canonical_json(&job_model(model))?; + let header = JobHeader { + job_id, + nonce, + account, + revision, + max_characters_per_account: max_characters, + snapshot_sha256: hex::encode(Sha256::digest(&snapshot_json)), + character_id: model.character_id, + session: session.map(|(session_id, generation)| SessionHeader { session_id, generation }), + }; + let mut out = serde_json::to_vec(&header)?; + out.push(b'\n'); + out.extend_from_slice(&snapshot_json); + Ok(out) +} + +fn job_paths(dir: &Path, job_id: ImportId) -> (PathBuf, PathBuf) { + (dir.join(format!("{job_id}.job")), dir.join(format!("{job_id}.result"))) +} + +/// What the core wrote into `.result`. +#[derive(Debug, Deserialize)] +struct JobResult { + status: String, + #[serde(default)] + local_guid: u32, + #[serde(default)] + item_base: u32, + #[serde(default)] + pet_base: u32, + #[serde(default)] + renamed: bool, + #[serde(default)] + final_name: String, + #[serde(default)] + items: usize, + #[serde(default)] + pets: usize, + #[serde(default)] + not_applied: Vec, + #[serde(default)] + problems: Vec, + #[serde(default)] + repeated: bool, +} + +#[derive(Debug, Deserialize)] +struct JobProblem { + code: String, + detail: String, +} + +/// Import the character's current canonical revision into a **running** realm through the core's import service. +pub fn import_character_online(ra: &mut Ra, store: &mut Store, character_id: CharacterId, server_id: &str, account: u32, opts: &ImportOptions, job_dir: &Path, session: Option) -> Result { + let record = store.character(character_id)?; + let model = store.load_snapshot(character_id, record.revision)?; + if store.server_mappings(character_id)?.iter().any(|m| m.server_id == server_id) { + return Err(PortableError::AlreadyOnRealm { character: character_id, server_id: server_id.to_string() }); + } + let ticket = store.begin_import(character_id, server_id, record.revision, &planned_items(&model), &planned_pets(&model))?; + let (job_file, result_file) = job_paths(job_dir, ticket.import_id); + let write = || -> Result<()> { + let bytes = job_bytes(ticket.import_id, ticket.nonce, account, record.revision, opts.max_characters_per_account, &model, session.map(|s| (s, 1)))?; + let temp = job_file.with_extension("job.tmp"); + std::fs::write(&temp, bytes)?; + std::fs::rename(&temp, &job_file)?; + Ok(()) + }; + if let Err(e) = write() { + store.abort_import(ticket.import_id, &format!("the job file could not be written: {e}"))?; + return Err(e); + } + + let reply = ra.portable_import(ticket.import_id); + let result: Option = std::fs::read(&result_file).ok().and_then(|b| serde_json::from_slice(&b).ok()); + let cleanup = || { + let _ = std::fs::remove_file(&job_file); + let _ = std::fs::remove_file(&result_file); + }; + match result { + Some(r) if r.status == "ok" && r.local_guid != 0 => { + let allocation = ImportAllocation { local_guid: r.local_guid, item_base: r.item_base, pet_base: r.pet_base }; + if r.repeated { + // the core found this job's marker: the allocation is read back from the realm by recovery + cleanup(); + return finish_by_recovery(store, ticket.import_id, opts); + } + store.finish_import(ticket.import_id, allocation)?; + cleanup(); + Ok(ImportOutcome { import_id: ticket.import_id, local_guid: r.local_guid, final_name: r.final_name, renamed: r.renamed, items: r.items, pets: r.pets, not_applied: r.not_applied, warnings: vec![] }) + } + Some(r) if r.status == "refused" => { + let detail = r.problems.iter().map(|p| format!("{}: {}", p.code, p.detail)).collect::>().join("; "); + store.abort_import(ticket.import_id, &format!("the realm refused the import: {detail}"))?; + cleanup(); + Err(PortableError::ImportRefused(vec![super::import::ImportProblem::UnsupportedContent(detail)])) + } + _ => { + // no readable result: the marker in the realm decides, never the reply + let why = reply.err().map(|e| e.to_string()).unwrap_or_else(|| "the realm wrote no result".into()); + Err(PortableError::ImportNeedsAttention { import_id: ticket.import_id, detail: format!("the outcome in the realm is not known yet ({why}); run recovery") }) + } + } +} + +fn finish_by_recovery(_store: &mut Store, import_id: ImportId, _opts: &ImportOptions) -> Result { + Err(PortableError::ImportNeedsAttention { import_id, detail: "the core repeated an earlier job; run recovery to record it".into() }) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::portable::fixtures::geared_level_eighty; + use crate::portable::model::Extension; + + #[test] + fn a_job_carries_what_the_realm_needs_and_nothing_else() { + let mut model = geared_level_eighty(); + model.extensions.insert("coa:unlisted-settings".into(), Extension::new("1", 1, vec![1, 2, 3])); + model.settings.insert("core.spell_charge.1".into(), vec![9]); + model.settings.insert("core.ascension_build.54".into(), vec![1, 2]); + let id = ImportId::new(); + let bytes = job_bytes(id, [1, 2, 3, 4], 7001, 5, 10, &model, Some((SessionId::new(), 1))).unwrap(); + let split = bytes.iter().position(|b| *b == b'\n').unwrap(); + let header: serde_json::Value = serde_json::from_slice(&bytes[..split]).unwrap(); + let body = &bytes[split + 1..]; + assert_eq!(header["job_id"], id.to_string()); + assert_eq!(header["snapshot_sha256"], hex::encode(Sha256::digest(body)), "the hash is of the exact bytes of the snapshot line"); + assert_eq!(header["nonce"], serde_json::json!([1, 2, 3, 4])); + assert!(header["session"]["session_id"].is_string()); + assert!(!body.contains(&b'\n'), "the snapshot is one line"); + let sent: PortableCharacter = serde_json::from_slice(body).unwrap(); + assert!(sent.extensions.is_empty(), "opaque module payloads are not shipped to the realm"); + assert!(sent.settings.contains_key("core.ascension_build.54") && !sent.settings.contains_key("core.spell_charge.1")); + assert_eq!(sent.items.len(), model.items.len()); + assert!(job_bytes(id, [0; 4], 1, 1, 10, &model, None).unwrap().len() > 100); + } +} diff --git a/crates/coa-core/src/portable/realm/plan.rs b/crates/coa-core/src/portable/realm/plan.rs index 2a7f8ee..f5ced35 100644 --- a/crates/coa-core/src/portable/realm/plan.rs +++ b/crates/coa-core/src/portable/realm/plan.rs @@ -96,6 +96,7 @@ pub const CHARACTER_KEYED: &[(&str, &str)] = &[ ("corpse", "guid"), ("item_instance", "owner_guid"), ("mod_craftsmans_codex", "guid"), + ("coa_portable_session", "guid"), ]; /// Tables that name **item guids**. The new item guids start above the highest of all of them, so a stale row (a mail, @@ -137,6 +138,16 @@ pub struct PlanContext<'a> { /// Database users of a running game server; any other session of one of these users means the realm is running. pub game_server_users: &'a [String], pub probe: &'a SchemaProbe, + /// Arm a runtime portable session for the arrival: the core takes the baseline at its first load. + pub session: Option, +} + +/// The portable session a character arrives with (`coa_portable_session` state 0). +#[derive(Debug, Clone, Copy)] +pub struct SessionArm { + pub session_id: super::super::ids::SessionId, + pub character_id: super::super::ids::CharacterId, + pub generation: u32, } /// What the plan will write, for assertions and reports. @@ -474,6 +485,25 @@ pub fn build_plan(model: &PortableCharacter, ctx: &PlanContext<'_>) -> Result) -> Result = SCHEMA_TABLES.lines().map(str::trim).filter(|l| !l.is_empty()).collect(); - assert_eq!(schema.len(), 163); + assert_eq!(schema.len(), 164); let missing: Vec<_> = schema.iter().filter(|t| classify(t).is_none()).collect(); assert!(missing.is_empty(), "unclassified tables: {missing:?}"); let stale: Vec<_> = TABLES.iter().map(|(t, _)| *t).filter(|t| !schema.contains(t)).collect(); diff --git a/crates/coa-core/src/portable/realm/script.rs b/crates/coa-core/src/portable/realm/script.rs index e32491f..c2ec321 100644 --- a/crates/coa-core/src/portable/realm/script.rs +++ b/crates/coa-core/src/portable/realm/script.rs @@ -261,6 +261,25 @@ pub fn queries(guid: u32, probe: &SchemaProbe) -> Result> { query("macros", &[("time", "d.`time`"), ("data", hex!("d.data"))], "acore_characters.character_account_data d WHERE d.guid = {guid} AND d.`type` = 5", guid), ]; + // The portable session marker of the core: read in the same snapshot as the character, so the marker says which save the rows are. + if probe.has("coa_portable_session") { + out.push(query( + "portable_session", + &[ + ("guid", "s.guid"), + ("session_id", hex!("s.session_id")), + ("character_id", hex!("s.character_id")), + ("imported_revision", "s.imported_revision"), + ("generation", "s.baseline_generation"), + ("state", "s.state"), + ("checkpoint_seq", "s.checkpoint_seq"), + ("save_seq", "s.save_seq"), + ], + "acore_characters.coa_portable_session s WHERE s.guid = {guid}", + guid, + )); + } + // Blockers of optional modules: only asked for when the module's table exists. if probe.has("coa_character_challenge") { out.push(query("block:challenge", &[("n", "COUNT(*)")], "acore_characters.coa_character_challenge WHERE guid = {guid}", guid)); diff --git a/crates/coa-core/src/portable/realm/testdata/characters-tables.txt b/crates/coa-core/src/portable/realm/testdata/characters-tables.txt index ad5f8e3..f197be2 100644 --- a/crates/coa-core/src/portable/realm/testdata/characters-tables.txt +++ b/crates/coa-core/src/portable/realm/testdata/characters-tables.txt @@ -85,6 +85,7 @@ coa_custom_trial_completion coa_custom_trial_entry coa_custom_trial_vote coa_keepers_scroll_blessing +coa_portable_session coa_squid_migrations coa_wildcard_skill_card coa_wildcard_skill_card_account diff --git a/crates/coa-core/src/portable/realm/testdata/golden/export-1001.txt b/crates/coa-core/src/portable/realm/testdata/golden/export-1001.txt index d174fb7..bb16cb6 100644 --- a/crates/coa-core/src/portable/realm/testdata/golden/export-1001.txt +++ b/crates/coa-core/src/portable/realm/testdata/golden/export-1001.txt @@ -16,6 +16,7 @@ #T:pet_declined #T:settings #T:macros +#T:portable_session #T:block:challenge 0 #T:block:gamemode diff --git a/crates/coa-core/src/portable/realm/testdata/golden/export-1002.txt b/crates/coa-core/src/portable/realm/testdata/golden/export-1002.txt index ddca24a..1a7f4d8 100644 --- a/crates/coa-core/src/portable/realm/testdata/golden/export-1002.txt +++ b/crates/coa-core/src/portable/realm/testdata/golden/export-1002.txt @@ -222,6 +222,7 @@ #T:pet_declined #T:settings #T:macros +#T:portable_session #T:block:challenge 0 #T:block:gamemode diff --git a/crates/coa-core/src/portable/realm/testdata/golden/export-1003.txt b/crates/coa-core/src/portable/realm/testdata/golden/export-1003.txt index 05d80c1..0b8d7aa 100644 --- a/crates/coa-core/src/portable/realm/testdata/golden/export-1003.txt +++ b/crates/coa-core/src/portable/realm/testdata/golden/export-1003.txt @@ -48,6 +48,7 @@ #T:pet_declined #T:settings #T:macros +#T:portable_session #T:block:challenge 0 #T:block:gamemode diff --git a/crates/coa-core/src/portable/realm/testdata/golden/export-1004.txt b/crates/coa-core/src/portable/realm/testdata/golden/export-1004.txt index f19ced2..ce40289 100644 --- a/crates/coa-core/src/portable/realm/testdata/golden/export-1004.txt +++ b/crates/coa-core/src/portable/realm/testdata/golden/export-1004.txt @@ -344,6 +344,7 @@ x636F72652E77696C64636172642E6361726473 x31203220 x6D6F642E6578616D706C652E6E65775F66656174757265 x392038203720 #T:macros 1790000000 xFD2992936477103705783B2983ECDABC6F022B53FF446C4155EA1E0E31DF205D8B3FC6F87451B67413FD007D6B323F14D2FE5614DAB33BA9139D96F75EFE4E174239ED1136FC75F79A48242521E33AEE5A6F823526D45D0BCE04EA37BE3AF121DD56A5FB7426D3F9E8B71B94D4206BE32D2ACF91F3DD3AE245F707FD03E94EE0DAB632509D06D513DC0F8890EA7C3948CC225BDCBB0C8AF86CD220B053D96EDAF1447490F93185D89F27D586C80F528A4A487656EF877AF17D72DBC23E00ED38E64E2AA1CB0016697726654CBAE0465CCC0004677C123BC01ED87E7238E14295CDBBBF62DE4B7FAEDC776E0E9C0D6A047568D3719575223E440FAA36F1C5EF21FF2EFD915A8B3AB8234101E50332C8379D4E27FC28857EC526B00C50CBE696AE954521913EA5066031762940E310A5A4277DDAABB670AFCC984E06B3C55F3E0BEAD7E755F14A3E9FE98C46E9A32B2640668918AB1097C40FA5471006036F036469813F251E49C242543C82523BAB6E3CC5F165786319C50034233D86B4ADBB9CC7E105754FFF50808B70322090CCF883C207B250E0388124AA04FD08124D1F5F720DE546CA8E73DDC9E71BB1C5A0DF67B65CCC8AFCEDE46054CFD44B452B322D76B54CB041D9BB49F0CCC5EA584C30CD67BFE1DC599924483F019E05500EB6584A2F7AFCF838555047C3C901C74DD155E9EC10D3EFDB7A63CED430E92B6478676B56BDB7C204D131962AA83F14C7490D31847CAD304928DD3BF148BAE93DD0D82D1A361A455F6CAA3DBD1705F0281CFFF36CA6488F367560221D2C1BF44DBF05A48B9AB810F4A870E5E64A53181AAD6AB9AC484363468D130001FF20656E64 +#T:portable_session #T:block:challenge 0 #T:block:gamemode diff --git a/crates/coa-core/src/portable/realm/testdata/golden/export-1005.txt b/crates/coa-core/src/portable/realm/testdata/golden/export-1005.txt index 0ef2c99..0a1569a 100644 --- a/crates/coa-core/src/portable/realm/testdata/golden/export-1005.txt +++ b/crates/coa-core/src/portable/realm/testdata/golden/export-1005.txt @@ -27,6 +27,7 @@ 3002 xD0A4D0B0D0BDD0B3D0B0 xD0A4D0B0D0BDD0B3D183 xD0A4D0B0D0BDD0B3 xD0A4D0B0D0BDD0B3D0BED0BC xD0A4D0B0D0BDD0B3D0B5 #T:settings #T:macros +#T:portable_session #T:block:challenge 0 #T:block:gamemode diff --git a/crates/coa-core/src/portable/realm/testdata/golden/export-1006.txt b/crates/coa-core/src/portable/realm/testdata/golden/export-1006.txt index 7887802..f3c6909 100644 --- a/crates/coa-core/src/portable/realm/testdata/golden/export-1006.txt +++ b/crates/coa-core/src/portable/realm/testdata/golden/export-1006.txt @@ -16,6 +16,7 @@ #T:pet_declined #T:settings #T:macros +#T:portable_session #T:block:challenge 0 #T:block:gamemode diff --git a/crates/coa-core/src/portable/realm/testdata/golden/export-1007.txt b/crates/coa-core/src/portable/realm/testdata/golden/export-1007.txt index 103e2be..759650a 100644 --- a/crates/coa-core/src/portable/realm/testdata/golden/export-1007.txt +++ b/crates/coa-core/src/portable/realm/testdata/golden/export-1007.txt @@ -16,6 +16,7 @@ #T:pet_declined #T:settings #T:macros +#T:portable_session #T:block:challenge 1 #T:block:gamemode diff --git a/crates/coa-core/src/portable/realm/testdata/golden/export-1008.txt b/crates/coa-core/src/portable/realm/testdata/golden/export-1008.txt index e89253e..f5c2b18 100644 --- a/crates/coa-core/src/portable/realm/testdata/golden/export-1008.txt +++ b/crates/coa-core/src/portable/realm/testdata/golden/export-1008.txt @@ -16,6 +16,7 @@ #T:pet_declined #T:settings #T:macros +#T:portable_session #T:block:challenge 0 #T:block:gamemode diff --git a/crates/coa-core/src/portable/realm/testdata/golden/export-1009.txt b/crates/coa-core/src/portable/realm/testdata/golden/export-1009.txt index 797be41..2277294 100644 --- a/crates/coa-core/src/portable/realm/testdata/golden/export-1009.txt +++ b/crates/coa-core/src/portable/realm/testdata/golden/export-1009.txt @@ -16,6 +16,7 @@ #T:pet_declined #T:settings #T:macros +#T:portable_session #T:block:challenge 0 #T:block:gamemode diff --git a/crates/coa-core/src/portable/realm/testdata/golden/export-1010.txt b/crates/coa-core/src/portable/realm/testdata/golden/export-1010.txt index f247c6c..1c264b9 100644 --- a/crates/coa-core/src/portable/realm/testdata/golden/export-1010.txt +++ b/crates/coa-core/src/portable/realm/testdata/golden/export-1010.txt @@ -16,6 +16,7 @@ #T:pet_declined #T:settings #T:macros +#T:portable_session #T:block:challenge 0 #T:block:gamemode diff --git a/crates/coa-core/src/portable/realm/testdata/golden/export-1011.txt b/crates/coa-core/src/portable/realm/testdata/golden/export-1011.txt index 8ecdabf..d10d76b 100644 --- a/crates/coa-core/src/portable/realm/testdata/golden/export-1011.txt +++ b/crates/coa-core/src/portable/realm/testdata/golden/export-1011.txt @@ -16,6 +16,7 @@ #T:pet_declined #T:settings #T:macros +#T:portable_session #T:block:challenge 0 #T:block:gamemode diff --git a/crates/coa-core/src/portable/realm/testdata/golden/export-1012.txt b/crates/coa-core/src/portable/realm/testdata/golden/export-1012.txt index 6e69c42..7701d96 100644 --- a/crates/coa-core/src/portable/realm/testdata/golden/export-1012.txt +++ b/crates/coa-core/src/portable/realm/testdata/golden/export-1012.txt @@ -16,6 +16,7 @@ #T:pet_declined #T:settings #T:macros +#T:portable_session #T:block:challenge 0 #T:block:gamemode diff --git a/crates/coa-core/src/portable/realm/testdata/golden/probe.txt b/crates/coa-core/src/portable/realm/testdata/golden/probe.txt index 30875f2..300c994 100644 --- a/crates/coa-core/src/portable/realm/testdata/golden/probe.txt +++ b/crates/coa-core/src/portable/realm/testdata/golden/probe.txt @@ -64,6 +64,8 @@ C coa_custom_trial_active guid C coa_custom_trial_completion guid C coa_custom_trial_entry guid C coa_custom_trial_vote guid +C coa_portable_session character_id +C coa_portable_session guid C corpse guid C creature_respawn guid C gameobject_respawn guid @@ -186,6 +188,7 @@ T coa_custom_trial_completion - T coa_custom_trial_entry - T coa_custom_trial_vote - T coa_keepers_scroll_blessing - +T coa_portable_session - T coa_squid_migrations - T coa_wildcard_skill_card - T coa_wildcard_skill_card_account - diff --git a/crates/coa-core/src/portable/realm/testdata/portable-session-table.sql b/crates/coa-core/src/portable/realm/testdata/portable-session-table.sql new file mode 100644 index 0000000..0f21e2a --- /dev/null +++ b/crates/coa-core/src/portable/realm/testdata/portable-session-table.sql @@ -0,0 +1,15 @@ +-- The runtime session table of the core (data/sql/updates/pending_db_characters/rev_20261006_01_coa_portable_session.sql), for the +-- disposable realms of the live tests. Applied after the realm fixture; idempotent. +CREATE TABLE IF NOT EXISTS acore_characters.`coa_portable_session` ( + `guid` INT UNSIGNED NOT NULL, + `session_id` CHAR(36) NOT NULL, + `character_id` CHAR(36) NOT NULL, + `imported_revision` INT UNSIGNED NOT NULL, + `baseline_generation` INT UNSIGNED NOT NULL DEFAULT 1, + `state` TINYINT UNSIGNED NOT NULL DEFAULT 0, + `checkpoint_seq` INT UNSIGNED NOT NULL DEFAULT 0, + `save_seq` INT UNSIGNED NOT NULL DEFAULT 0, + `updated_at` INT UNSIGNED NOT NULL DEFAULT 0, + PRIMARY KEY (`guid`), + UNIQUE KEY `idx_session_id` (`session_id`) +); diff --git a/crates/coa-core/src/portable/realm/tests.rs b/crates/coa-core/src/portable/realm/tests.rs index 5541b68..8f9fcc1 100644 --- a/crates/coa-core/src/portable/realm/tests.rs +++ b/crates/coa-core/src/portable/realm/tests.rs @@ -43,7 +43,7 @@ fn raw(guid: u32) -> RawExport { } fn export_as(guid: u32, ruleset: Ruleset, prior: &HashMap) -> Result { - build(&raw(guid), &ExportRequest { ruleset, local_guid: guid, character_id: None, prior_items: prior, prior_pets: &HashMap::new() }) + build(&raw(guid), &ExportRequest { ruleset, local_guid: guid, character_id: None, prior_items: prior, prior_pets: &HashMap::new(), allow_online_session: false }) } fn export(guid: u32) -> Exported { @@ -313,7 +313,7 @@ fn items_keep_their_portable_ids_but_a_recycled_guid_gets_a_new_one() { #[test] fn a_reexport_keeps_the_portable_character_id() { let id = CharacterId::new(); - let e = build(&raw(1002), &ExportRequest { ruleset: Ruleset::Coa, local_guid: 1002, character_id: Some(id), prior_items: &HashMap::new(), prior_pets: &HashMap::new() }).unwrap(); + let e = build(&raw(1002), &ExportRequest { ruleset: Ruleset::Coa, local_guid: 1002, character_id: Some(id), prior_items: &HashMap::new(), prior_pets: &HashMap::new(), allow_online_session: false }).unwrap(); assert_eq!(e.model.character_id, id); } @@ -355,7 +355,7 @@ fn windows_line_endings_do_not_matter() { let crlf = answer(1002).replace("\r\n", "\n").replace('\n', "\r\n"); let queries = script::queries(1002, &probe()).unwrap(); let parsed = script::parse_output(&crlf, &queries).unwrap(); - let e = build(&parsed, &ExportRequest { ruleset: Ruleset::Coa, local_guid: 1002, character_id: None, prior_items: &HashMap::new(), prior_pets: &HashMap::new() }).unwrap(); + let e = build(&parsed, &ExportRequest { ruleset: Ruleset::Coa, local_guid: 1002, character_id: None, prior_items: &HashMap::new(), prior_pets: &HashMap::new(), allow_online_session: false }).unwrap(); assert_eq!((e.model.items.len(), e.model.identity.name.as_str()), (60, "Geared")); } @@ -379,7 +379,7 @@ fn damaged_answers_never_panic() { bytes[at] = [b'Z', b'\t', b'\n', b'x', b'-', b'9', b'0', b'~'][next() as usize % 8]; } let Ok(text) = String::from_utf8(bytes) else { continue }; - match script::parse_output(&text, &queries).and_then(|raw| build(&raw, &ExportRequest { ruleset: Ruleset::Coa, local_guid: 1002, character_id: None, prior_items: &HashMap::new(), prior_pets: &HashMap::new() })) { + match script::parse_output(&text, &queries).and_then(|raw| build(&raw, &ExportRequest { ruleset: Ruleset::Coa, local_guid: 1002, character_id: None, prior_items: &HashMap::new(), prior_pets: &HashMap::new(), allow_online_session: false })) { Ok(e) => { e.model.validate().unwrap(); oks += 1; @@ -428,7 +428,7 @@ fn make_portable_registers_everything_in_one_step_and_refuses_twice() { // a re-export with the store's mappings keeps every item id, so the next revision is a plain update let lookup = store.active_item_lookup(made.character_id, "realm-a").unwrap(); - let re = build(&raw(1002), &ExportRequest { ruleset: Ruleset::Coa, local_guid: 1002, character_id: Some(made.character_id), prior_items: &lookup, prior_pets: &HashMap::new() }).unwrap(); + let re = build(&raw(1002), &ExportRequest { ruleset: Ruleset::Coa, local_guid: 1002, character_id: Some(made.character_id), prior_items: &lookup, prior_pets: &HashMap::new(), allow_online_session: false }).unwrap(); assert_eq!(re.model, expected_model); let revision = store.commit_snapshot(made.character_id, 1, re.model, "realm-a", Some("re-export")).unwrap(); let report = store.reconcile_item_mappings(made.character_id, "realm-a", revision, &re.observations).unwrap(); diff --git a/crates/coa-core/src/portable/session/fake.rs b/crates/coa-core/src/portable/session/fake.rs index 9ad5d3d..76df2e8 100644 --- a/crates/coa-core/src/portable/session/fake.rs +++ b/crates/coa-core/src/portable/session/fake.rs @@ -215,7 +215,7 @@ impl RealmBridge for FakeRealm { } // the realm's tables are keyed by guid: a later read finds the same rows under the same internal ids let model = model.normalized(); - Ok(RealmRead { exported: Exported { model, local_guid: self.guid, account: 1, observations, pet_observations, warnings: vec![] }, session: self.row.clone(), online: self.online }) + Ok(RealmRead { exported: Exported { model, local_guid: self.guid, account: 1, observations, pet_observations, session: self.row.clone(), warnings: vec![] }, session: self.row.clone(), online: self.online }) } fn request_checkpoint(&mut self, local_guid: u32, session: SessionId, sequence: u64) -> Result { diff --git a/crates/coa-core/src/portable/session/live.rs b/crates/coa-core/src/portable/session/live.rs new file mode 100644 index 0000000..94b125b --- /dev/null +++ b/crates/coa-core/src/portable/session/live.rs @@ -0,0 +1,98 @@ +//! The real realm behind `RealmBridge`: the Manager's `Db` for reads and for arming, RA for the typed checkpoint and release. + +use std::collections::HashMap; + +use crate::db::Db; +use crate::ra::Ra; + +use super::super::error::{PortableError, Result}; +use super::super::ids::{CharacterId, PortableItemId, PortablePetId, SessionId}; +use super::super::realm::{probe, read_session_character}; +use super::bridge::*; + +pub struct LiveBridge<'a> { + db: &'a Db, + ra: Option, +} + +impl<'a> LiveBridge<'a> { + pub fn new(db: &'a Db, ra: Ra) -> Self { + Self { db, ra: Some(ra) } + } + + pub fn without_console(db: &'a Db) -> Self { + Self { db, ra: None } + } + + fn console(&mut self) -> Result<&mut Ra> { + self.ra.as_mut().ok_or_else(|| PortableError::RealmRead("the realm's console is not connected".into())) + } +} + +fn realm_error(e: crate::Error) -> PortableError { + PortableError::RealmRead(e.to_string()) +} + +/// The core's session row of one character, read straight from the realm's database. +pub fn read_session_row(db: &Db, local_guid: u32) -> Result> { + let sql = format!( + "SELECT guid, CONCAT('x', HEX(session_id)), CONCAT('x', HEX(character_id)), imported_revision, baseline_generation, state, checkpoint_seq, save_seq FROM acore_characters.coa_portable_session WHERE guid = {local_guid}" + ); + let out = db.query(&sql).map_err(realm_error)?; + let Some(line) = out.lines().find(|l| !l.trim().is_empty()) else { return Ok(None) }; + let cells: Vec<&str> = line.trim_end().split('\t').collect(); + if cells.len() != 8 { + return Err(PortableError::CorruptSnapshot("the portable session row has an unexpected shape".into())); + } + let text = |cell: &str| -> Result { + let hex = cell.strip_prefix('x').ok_or_else(|| PortableError::CorruptSnapshot("a session cell is not hex".into()))?; + String::from_utf8(hex::decode(hex).map_err(|_| PortableError::CorruptSnapshot("a session cell is not hex".into()))?).map_err(|_| PortableError::CorruptSnapshot("a session cell is not UTF-8".into())) + }; + let num = |i: usize| -> Result { cells[i].trim().parse().map_err(|_| PortableError::CorruptSnapshot(format!("{:?} is not a number", cells[i]))) }; + Ok(Some(SessionRow { + guid: num(0)? as u32, + session_id: text(cells[1])?.parse()?, + character_id: text(cells[2])?.parse()?, + imported_revision: num(3)?, + generation: num(4)? as u32, + state: RowState::from_code(num(5)?).ok_or_else(|| PortableError::CorruptSnapshot("unknown session state".into()))?, + checkpoint_seq: num(6)?, + save_seq: num(7)?, + })) +} + +impl RealmBridge for LiveBridge<'_> { + fn session_row(&mut self, local_guid: u32) -> Result> { + read_session_row(self.db, local_guid) + } + + fn read(&mut self, local_guid: u32, prior_items: &HashMap, prior_pets: &HashMap) -> Result { + let row = self.session_row(local_guid)?; + let character = row.as_ref().map(|r| r.character_id).ok_or_else(|| PortableError::Invalid("the character has no portable session row".into()))?; + let _ = probe(self.db)?; + let exported = read_session_character(self.db, local_guid, character, prior_items, prior_pets)?; + let session = exported.session.clone(); + Ok(RealmRead { exported, session, online: true }) + } + + fn request_checkpoint(&mut self, local_guid: u32, session: SessionId, sequence: u64) -> Result { + self.console()?.portable_checkpoint(local_guid, session, sequence).map_err(|e| PortableError::RealmRead(e.to_string())) + } + + fn release(&mut self, session: SessionId) -> Result<()> { + self.console()?.portable_release(session).map_err(|e| PortableError::RealmRead(e.to_string())) + } + + fn arm(&mut self, local_guid: u32, session: SessionId, character: CharacterId, revision: u64, generation: u32) -> Result<()> { + let sql = format!( + "START TRANSACTION; +DO IF((SELECT COUNT(*) FROM acore_characters.characters WHERE guid = {local_guid} AND online = 0) = 1, 1, (SELECT 1 UNION ALL SELECT 2)); + INSERT INTO acore_characters.coa_portable_session (guid, session_id, character_id, imported_revision, baseline_generation, state, checkpoint_seq, save_seq, updated_at) + VALUES ({local_guid}, '{session}', '{character}', {revision}, {generation}, 0, 0, 0, UNIX_TIMESTAMP()) + ON DUPLICATE KEY UPDATE session_id = VALUES(session_id), character_id = VALUES(character_id), imported_revision = VALUES(imported_revision), baseline_generation = VALUES(baseline_generation), state = 0, checkpoint_seq = 0, updated_at = VALUES(updated_at); +COMMIT;" + ); + self.db.query(&sql).map_err(realm_error)?; + Ok(()) + } +} diff --git a/crates/coa-core/src/portable/session/live_session.rs b/crates/coa-core/src/portable/session/live_session.rs new file mode 100644 index 0000000..b22dfc4 --- /dev/null +++ b/crates/coa-core/src/portable/session/live_session.rs @@ -0,0 +1,410 @@ +//! Phase 5 against a **real worldserver** (the core fork's `feat/portable-session-bridge` build with `PortableSession.Enable = 1`) +//! and two real disposable MySQL servers. `#[ignore]`d. +//! +//! ```text +//! COA_PORTABLE_LIVE="|13998|" COA_PORTABLE_LIVE_B="|13997|" +//! COA_PORTABLE_LIVE_SERVER_B="||" +//! cargo test -p coa-core portable::session::live_session -- --ignored --test-threads=1 --nocapture +//! ``` +//! +//! The test starts the worldserver itself (a child process it owns and stops), logs the character in through the bot module +//! (`botcmd spawnbot`, the real `HandlePlayerLoginFromDB`), and never uses `.saveall`. + +use std::path::PathBuf; +use std::process::{Child, Command, Stdio}; +use std::time::{Duration, Instant}; + +use crate::db::Db; +use crate::portable::ids::ImportId; +use crate::portable::store::Store; +use crate::portable::realm::live_import::{make, number, opts, realms, reset_b, ACCOUNT, B}; +use crate::portable::realm::import_character_in_session; +use crate::ra::Ra; + +use super::bridge::*; +use super::live::{read_session_row, LiveBridge}; +use super::protocol::*; +use super::*; + +struct Spec { + core: PathBuf, + ra_port: u16, + user: String, + password: String, +} + +fn spec() -> Option { + let raw = std::env::var("COA_PORTABLE_LIVE_SERVER_B").ok()?; + let p: Vec<&str> = raw.split('|').collect(); + assert_eq!(p.len(), 3, "COA_PORTABLE_LIVE_SERVER_B = ||"); + let text = std::fs::read_to_string(p[2]).expect("repack.json"); + let json: serde_json::Value = serde_json::from_str(text.trim_start_matches('\u{feff}')).unwrap(); + Some(Spec { core: PathBuf::from(p[0]), ra_port: p[1].parse().unwrap(), user: json["raUsername"].as_str().unwrap().to_string(), password: json["raPassword"].as_str().unwrap().to_string() }) +} + +/// A worldserver this test started and owns. +struct Server { + child: Child, + spec: Spec, +} + +impl Server { + fn start(spec: Spec) -> Server { + let child = Command::new(spec.core.join("worldserver.exe")) + .args(["-c", spec.core.join("configs/worldserver.conf").to_str().unwrap()]) + .current_dir(&spec.core) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .expect("the worldserver starts"); + let mut server = Server { child, spec }; + let end = Instant::now() + Duration::from_secs(240); + loop { + if let Some(code) = server.child.try_wait().unwrap() { + panic!("the worldserver exited early with {code}"); + } + if Ra::connect_to(server.spec.ra_port, &server.spec.user, &server.spec.password).is_ok() { + return server; + } + assert!(Instant::now() < end, "the worldserver did not open its console in time"); + std::thread::sleep(Duration::from_secs(2)); + } + } + + fn ra(&self) -> Ra { + Ra::connect_to(self.spec.ra_port, &self.spec.user, &self.spec.password).expect("RA") + } + + fn crash(mut self) { + let _ = self.child.kill(); + let _ = self.child.wait(); + } + + fn stop(mut self) { + let _ = self.ra().run("server shutdown 1"); + let end = Instant::now() + Duration::from_secs(120); + while self.child.try_wait().unwrap().is_none() { + assert!(Instant::now() < end, "the worldserver did not stop in time"); + std::thread::sleep(Duration::from_secs(1)); + } + } +} + +impl Drop for Server { + fn drop(&mut self) { + if matches!(self.child.try_wait(), Ok(None)) { + let _ = self.child.kill(); + let _ = self.child.wait(); + } + } +} + +fn wait_until(what: &str, seconds: u64, mut f: impl FnMut() -> bool) { + let end = Instant::now() + Duration::from_secs(seconds); + while !f() { + assert!(Instant::now() < end, "timed out waiting for {what}"); + std::thread::sleep(Duration::from_millis(300)); + } +} + +fn sql(db: &Db, text: &str) -> String { + db.query(text).unwrap_or_else(|e| panic!("{e}\n{text}")) +} + + +fn name_of(db: &Db, guid: u32) -> String { + sql(db, &format!("SELECT name FROM acore_characters.characters WHERE guid = {guid}")).trim().to_string() +} + +fn state_of(db: &Db, guid: u32) -> Option { + read_session_row(db, guid).unwrap() +} + +fn online(db: &Db, guid: u32) -> bool { + number(db, &format!("SELECT online FROM acore_characters.characters WHERE guid = {guid}")) != 0 +} + +fn deliver(host: &mut HostService, owner: &mut OwnerService, bridge: &mut LiveBridge) -> Vec { + let mut acks = Vec::new(); + for m in host.outbox().unwrap() { + let ack = if m.started { owner.handle_started(&m.bytes) } else { owner.handle_checkpoint(&m.bytes) }.unwrap(); + host.receive_ack(bridge, &ack).unwrap(); + acks.push(ack); + } + acks +} + +fn tick_until(host: &mut HostService, bridge: &mut LiveBridge, clock: &Instant, what: &str, want: impl Fn(&HostEvent) -> bool) -> Vec { + let end = Instant::now() + Duration::from_secs(60); + let mut all = Vec::new(); + loop { + let events = host.tick(bridge, clock.elapsed().as_secs()).unwrap(); + let hit = events.iter().any(&want); + all.extend(events); + if hit { + return all; + } + assert!(Instant::now() < end, "timed out waiting for {what}; events so far: {all:?}"); + std::thread::sleep(Duration::from_millis(250)); + } +} + +fn characters_written(log: &str) -> std::collections::BTreeSet { + let mut out = std::collections::BTreeSet::new(); + for line in log.lines() { + if !line.contains("UPDATE characters SET") { + continue; + } + if let Some(at) = line.rfind("WHERE guid") { + let digits: String = line[at + "WHERE guid".len()..].trim_start_matches([' ', '=']).chars().take_while(|c| c.is_ascii_digit()).collect(); + if let Ok(g) = digits.parse() { + out.insert(g); + } + } + } + out +} + +struct Rig { + owner: Store, + host: Store, + id: crate::portable::ids::CharacterId, + session: crate::portable::ids::SessionId, + guid: u32, + _dir: tempfile::TempDir, +} + +fn arrive(r: &crate::portable::realm::live_import::Realms) -> Rig { + reset_b(&r.b); + let dir = tempfile::tempdir().unwrap(); + let mut owner = Store::open_file(&dir.path().join("owner.db")).unwrap(); + let mut host = Store::open_file(&dir.path().join("host.db")).unwrap(); + let profile = owner.default_profile().unwrap(); + let host_profile = host.default_profile().unwrap(); + let id = make(r, &mut owner, profile, 1005); + let offer = OwnerService::new(&mut owner).offer(id, B).unwrap(); + HostService::new(&mut host, B, HostConfig::default()).accept_offer(host_profile, &offer).unwrap(); + let imported = import_character_in_session(&r.b, &mut host, id, B, ACCOUNT, &opts(), Some(offer.session_id)).unwrap(); + HostService::new(&mut host, B, HostConfig::default()).bind(offer.session_id, imported.local_guid).unwrap(); + assert_eq!(state_of(&r.b, imported.local_guid).unwrap().state, RowState::WaitingBaseline, "the import armed the session"); + Rig { owner, host, id, session: offer.session_id, guid: imported.local_guid, _dir: dir } +} + +#[test] +#[ignore] +fn a_running_worldserver_takes_its_own_baseline_and_checkpoints_one_character_without_a_logout() { + let (Some(r), Some(spec)) = (realms(), spec()) else { return }; + let mut rig = arrive(&r); + let guid = rig.guid; + let name = name_of(&r.b, guid); + let c0 = rig.owner.load_current(rig.id).unwrap(); + let log = tempfile::tempdir().unwrap().keep().join("general.log"); + let server = Server::start(spec); + let ra = &mut server.ra(); + let clock = Instant::now(); + + for other in [112u32, 113] { + ra.run(&format!("botcmd spawnbot {other}")).unwrap(); + } + ra.run(&format!("botcmd spawnbot {guid}")).unwrap(); + wait_until("the core's baseline marker", 90, || state_of(&r.b, guid).is_some_and(|row| row.state == RowState::BaselineReady)); + assert!(ra.run(&format!("portable status {guid}")).unwrap().contains("gated 1"), "the core holds the player until the Host has taken B0"); + + let mut host = HostService::new(&mut rig.host, B, HostConfig { checkpoint_interval_secs: 1 }); + let mut owner = OwnerService::new(&mut rig.owner); + let mut bridge = LiveBridge::new(&r.b, server.ra()); + tick_until(&mut host, &mut bridge, &clock, "the baseline", |e| matches!(e, HostEvent::BaselineTaken { .. })); + let acks = deliver(&mut host, &mut owner, &mut bridge); + assert_eq!(acks[0].outcome, AckOutcome::Applied); + assert!(ra.run(&format!("portable status {guid}")).unwrap().contains("gated 0"), "released after the baseline was persisted"); + assert_eq!(state_of(&r.b, guid).unwrap().state, RowState::Active); + let id = rig.id; + let revision = |o: &OwnerService| o.store().character(id).unwrap().revision; + assert_eq!(revision(&owner), 1, "a baseline changes nothing canonical"); + let level = c0.progression.level; + + sql(&r.b, &format!("SET GLOBAL log_output = 'FILE'; SET GLOBAL general_log_file = '{}'; SET GLOBAL general_log = 'ON'", log.to_str().unwrap().replace('\\', "/"))); + for step in 1..=3u8 { + ra.run(&format!("character level {name} {}", level + step)).unwrap(); + let before = std::fs::metadata(&log).map(|m| m.len()).unwrap_or(0); + let events = tick_until(&mut host, &mut bridge, &clock, "a checkpoint", |e| matches!(e, HostEvent::CheckpointQueued { .. })); + let written = std::fs::read(&log).map(|b| String::from_utf8_lossy(&b[before as usize..]).into_owned()).unwrap_or_default(); + let guids = characters_written(&written); + assert!(guids.contains(&guid), "step {step}: the checkpoint saved the character ({guids:?}); events {events:?}"); + assert_eq!(guids.len(), 1, "step {step}: one checkpoint writes one character, not everybody: {guids:?}"); + let acks = deliver(&mut host, &mut owner, &mut bridge); + assert!(acks.iter().all(|a| a.outcome == AckOutcome::Applied), "{acks:?}"); + let canonical = owner.store().load_current(id).unwrap(); + assert_eq!(canonical.progression.level, level + step, "step {step}"); + assert_eq!(revision(&owner), 1 + step as u64); + assert!(online(&r.b, guid), "the character was online for every checkpoint"); + } + sql(&r.b, "SET GLOBAL general_log = 'OFF'"); + + let canonical = owner.store().load_current(id).unwrap(); + assert_eq!(canonical.progression.chosen_title, c0.progression.chosen_title); + assert!(c0.build.spells.iter().all(|s| canonical.build.spells.contains(s)), "nothing the realm deleted at load was deleted from the canonical character"); + + ra.run(&format!("botcmd despawn {guid}")).unwrap(); + wait_until("the logout save", 60, || state_of(&r.b, guid).is_some_and(|row| row.state == RowState::Ended)); + wait_until("the character to be offline", 60, || !online(&r.b, guid)); + tick_until(&mut host, &mut bridge, &clock, "the final checkpoint", |e| matches!(e, HostEvent::CheckpointQueued { final_checkpoint: true, .. })); + let acks = deliver(&mut host, &mut owner, &mut bridge); + let next = acks[0].next_session.clone().expect("the final acknowledgement names the next session"); + assert_eq!(owner.session(rig.session).unwrap().unwrap().0, "closed"); + let row = state_of(&r.b, guid).unwrap(); + assert_eq!((row.session_id, row.state), (next.session_id, RowState::WaitingBaseline), "the realm is armed for the next login"); + + ra.run(&format!("botcmd spawnbot {guid}")).unwrap(); + wait_until("the second baseline marker", 90, || state_of(&r.b, guid).is_some_and(|row| row.state == RowState::BaselineReady)); + tick_until(&mut host, &mut bridge, &clock, "the second baseline", |e| matches!(e, HostEvent::BaselineTaken { .. })); + deliver(&mut host, &mut owner, &mut bridge); + assert_eq!(owner.session(next.session_id).unwrap().unwrap().0, "open"); + assert_eq!(owner.store().load_current(id).unwrap().progression.level, level + 3); + + ra.run(&format!("character level {name} {}", level + 4)).unwrap(); + tick_until(&mut host, &mut bridge, &clock, "a checkpoint before the restart", |e| matches!(e, HostEvent::CheckpointQueued { .. })); + deliver(&mut host, &mut owner, &mut bridge); + drop(bridge); + server.crash(); + + let server = Server::start(self::spec().unwrap()); + let mut bridge = LiveBridge::new(&r.b, server.ra()); + sql(&r.b, "UPDATE acore_characters.characters SET online = 0"); + server.ra().run(&format!("botcmd spawnbot {guid}")).unwrap(); + wait_until("the character to continue", 90, || online(&r.b, guid)); + wait_until("the core to register the session", 60, || server.ra().run(&format!("portable status {guid}")).unwrap().contains("SESSION")); + let status = server.ra().run(&format!("portable status {guid}")).unwrap(); + assert!(status.contains("gated 0"), "a running session is not held again: {status}"); + assert_eq!(state_of(&r.b, guid).unwrap().session_id, next.session_id); + server.ra().run(&format!("character level {name} {}", level + 5)).unwrap(); + tick_until(&mut host, &mut bridge, &clock, "a checkpoint after the restart", |e| matches!(e, HostEvent::CheckpointQueued { .. })); + let acks = deliver(&mut host, &mut owner, &mut bridge); + assert!(acks.iter().all(|a| a.outcome == AckOutcome::Applied)); + assert_eq!(owner.store().load_current(id).unwrap().progression.level, level + 5); + + server.ra().run(&format!("character level {name} {}", level + 6)).unwrap(); + drop(bridge); + server.stop(); + + let mut bridge = LiveBridge::without_console(&r.b); + wait_until("the shutdown logout save", 30, || state_of(&r.b, guid).is_some_and(|row| row.state == RowState::Ended)); + tick_until(&mut host, &mut bridge, &clock, "the final checkpoint of a clean shutdown", |e| matches!(e, HostEvent::CheckpointQueued { final_checkpoint: true, .. })); + let acks = deliver(&mut host, &mut owner, &mut bridge); + let third = acks[0].next_session.clone().expect("a clean shutdown ends the session like a logout"); + assert_eq!(owner.store().load_current(id).unwrap().progression.level, level + 6); + assert_eq!(state_of(&r.b, guid).unwrap().session_id, third.session_id); +} + +#[test] +#[ignore] +fn a_character_whose_host_never_answers_is_never_released_and_the_core_disconnects_it() { + let (Some(r), Some(spec)) = (realms(), spec()) else { return }; + let rig = arrive(&r); + let guid = rig.guid; + let server_log = spec.core.join("Logs/Server.log"); + let server = Server::start(spec); + server.ra().run(&format!("botcmd spawnbot {guid}")).unwrap(); + wait_until("the baseline marker", 90, || state_of(&r.b, guid).is_some_and(|row| row.state == RowState::BaselineReady)); + wait_until("the core to give up on the Host", 90, || std::fs::read_to_string(&server_log).is_ok_and(|log| log.contains(&format!("the baseline of character {guid} was not confirmed in time")))); + let row = state_of(&r.b, guid).unwrap(); + assert_eq!(row.state, RowState::BaselineReady, "the session was never released"); + assert_eq!(row.checkpoint_seq, 0); + assert!(server.ra().run(&format!("portable status {guid}")).unwrap().contains("gated 1"), "the character stays held"); + server.stop(); +} + +// ---- the production importer: into a RUNNING realm, through the core's PortableImportService ------------------------------------ + +fn job_dir() -> Option { + std::env::var("COA_PORTABLE_LIVE_JOBDIR").ok().map(PathBuf::from) +} + +fn read_back_online(r: &crate::portable::realm::live_import::Realms, store: &Store, id: crate::portable::ids::CharacterId) -> crate::portable::model::PortableCharacter { + let guid = store.server_mappings(id).unwrap().into_iter().find(|m| m.server_id == B).unwrap().local_guid; + crate::portable::realm::export_character_with_pets(&r.b, guid, Some(id), &store.active_item_lookup(id, B).unwrap(), &store.active_pet_lookup(id, B).unwrap()).unwrap().model +} + +#[test] +#[ignore] +fn the_core_imports_characters_into_a_running_realm_like_the_offline_importer_does() { + let (Some(r), Some(spec), Some(jobs)) = (realms(), spec(), job_dir()) else { return }; + reset_b(&r.b); + std::fs::create_dir_all(&jobs).unwrap(); + let dir = tempfile::tempdir().unwrap(); + let mut store = Store::open_file(&dir.path().join("portable.db")).unwrap(); + let profile = store.default_profile().unwrap(); + let server = Server::start(spec); + let mut ra = server.ra(); + let chars_before = number(&r.b, "SELECT COUNT(*) FROM acore_characters.characters"); + let next_guid = number(&r.b, "SELECT MAX(guid) + 1 FROM acore_characters.characters"); + let numchars_before = number(&r.b, &format!("SELECT IFNULL((SELECT numchars FROM acore_auth.realmcharacters WHERE acctid = {ACCOUNT} AND realmid = 1), 0)")); + + let mut imported = Vec::new(); + for a_guid in [1002u32, 1004, 1005, 1001] { + let id = make(&r, &mut store, profile, a_guid); + let canonical = store.load_current(id).unwrap(); + let outcome = crate::portable::realm::online::import_character_online(&mut ra, &mut store, id, B, ACCOUNT, &opts(), &jobs, None).unwrap_or_else(|e| panic!("import of {a_guid}: {e}")); + assert_eq!(outcome.local_guid as u64, next_guid + imported.len() as u64, "the core allocates from its own live generator"); + let back = read_back_online(&r, &store, id); + assert_eq!(back, crate::portable::realm::live_import::expected_after_a_trip(&canonical, &back), "character {a_guid} is what the offline importer would have made of it"); + imported.push((id, outcome.local_guid, outcome.final_name, canonical)); + } + assert!(std::fs::read_dir(&jobs).unwrap().next().is_none(), "job and result files are removed after each import"); + assert_eq!(number(&r.b, "SELECT COUNT(*) FROM acore_characters.characters"), chars_before + 4); + assert!(store.open_imports(B).unwrap().is_empty()); + + // the realm itself knows them now: its name cache (lookups by name), the account's character count, its id generators + let (id, guid, name, canonical) = &imported[2]; + let reply = ra.run(&format!("character level {name} {}", canonical.progression.level + 1)).unwrap(); + assert!(reply.contains("changed level"), "the name cache knows the new character: {reply}"); + assert_eq!(number(&r.b, &format!("SELECT level FROM acore_characters.characters WHERE guid = {guid}")), canonical.progression.level as u64 + 1); + let numchars = number(&r.b, &format!("SELECT numchars FROM acore_auth.realmcharacters WHERE acctid = {ACCOUNT} AND realmid = 1")); + assert_eq!(numchars, numchars_before + 4, "the realm character count of the account was updated"); + let _ = id; + ra.run(&format!("botcmd spawnbot {guid}")).unwrap(); + wait_until("the imported character to log in", 90, || online(&r.b, *guid)); + ra.run(&format!("botcmd despawn {guid}")).unwrap(); + wait_until("the logout", 60, || !online(&r.b, *guid)); + + // the same job id again changes nothing (idempotent), a hostile or impossible job is refused whole + let id = make(&r, &mut store, profile, 1003); + let record = store.character(id).unwrap(); + let model = store.load_snapshot(id, record.revision).unwrap(); + let ticket = store.begin_import(id, B, record.revision, &crate::portable::realm::import::planned_items(&model), &crate::portable::realm::import::planned_pets(&model)).unwrap(); + let bytes = crate::portable::realm::online::job_bytes(ticket.import_id, ticket.nonce, ACCOUNT, record.revision, 10, &model, None).unwrap(); + let file = jobs.join(format!("{}.job", ticket.import_id)); + std::fs::write(&file, &bytes).unwrap(); + let before = number(&r.b, "SELECT COUNT(*) FROM acore_characters.characters"); + assert!(ra.portable_import(ticket.import_id).is_ok()); + assert_eq!(number(&r.b, "SELECT COUNT(*) FROM acore_characters.characters"), before + 1); + assert!(ra.portable_import(ticket.import_id).is_ok(), "the same job id again is answered, not repeated"); + assert_eq!(number(&r.b, "SELECT COUNT(*) FROM acore_characters.characters"), before + 1, "no second character"); + let _ = std::fs::remove_file(&file); + let _ = std::fs::remove_file(jobs.join(format!("{}.result", ticket.import_id))); + store.abort_import(ticket.import_id, "test").ok(); + + let mut refused = |bytes: Vec, why: &str| { + let job = ImportId::new(); + let mut text = String::from_utf8(bytes).unwrap(); + text = text.replacen(&ticket.import_id.to_string(), &job.to_string(), 1); + std::fs::write(jobs.join(format!("{job}.job")), text).unwrap(); + let counts = number(&r.b, "SELECT COUNT(*) FROM acore_characters.characters") + number(&r.b, "SELECT COUNT(*) FROM acore_characters.item_instance"); + let reply = ra.portable_import(job); + assert!(reply.is_err(), "{why}: {reply:?}"); + assert_eq!(number(&r.b, "SELECT COUNT(*) FROM acore_characters.characters") + number(&r.b, "SELECT COUNT(*) FROM acore_characters.item_instance"), counts, "{why}: nothing was written"); + let _ = std::fs::remove_file(jobs.join(format!("{job}.job"))); + let _ = std::fs::remove_file(jobs.join(format!("{job}.result"))); + }; + let mut impossible = model.clone(); + impossible.items[0].entry = crate::portable::ids::ContentId::new("coa", "item", 4_000_000_000).unwrap(); + refused(crate::portable::realm::online::job_bytes(ticket.import_id, [9, 9, 9, 9], ACCOUNT, 1, 10, &impossible, None).unwrap(), "an item the realm does not know"); + refused(crate::portable::realm::online::job_bytes(ticket.import_id, [9, 9, 9, 8], 99_999_999, 1, 10, &model, None).unwrap(), "an account that does not exist"); + let mut tampered = crate::portable::realm::online::job_bytes(ticket.import_id, [9, 9, 9, 7], ACCOUNT, 1, 10, &model, None).unwrap(); + let last = tampered.len() - 3; + tampered[last] ^= 1; + refused(tampered, "a snapshot that does not match its hash"); + refused(b"{\"job_id\":\"x\"}\nnot json".to_vec(), "garbage"); + server.stop(); +} diff --git a/crates/coa-core/src/portable/session/mod.rs b/crates/coa-core/src/portable/session/mod.rs index ddc8545..8e1ae7b 100644 --- a/crates/coa-core/src/portable/session/mod.rs +++ b/crates/coa-core/src/portable/session/mod.rs @@ -1,6 +1,7 @@ pub mod protocol; pub mod bridge; pub mod host; +pub mod live; pub mod owner; pub use host::{HostConfig, HostEvent, HostService}; @@ -10,3 +11,5 @@ pub use owner::OwnerService; mod fake; #[cfg(test)] mod tests; +#[cfg(test)] +mod live_session; diff --git a/crates/coa-core/src/ra.rs b/crates/coa-core/src/ra.rs index b1fcb7c..1f269b4 100644 --- a/crates/coa-core/src/ra.rs +++ b/crates/coa-core/src/ra.rs @@ -91,6 +91,43 @@ impl Ra { } } + /// `portable checkpoint `: the core saves this one character with a checkpoint marker. Only a + /// number, a session id and a number are sent. The reply is a request status, **not** proof that anything was written. + pub fn portable_checkpoint(&mut self, guid: u32, session: crate::portable::SessionId, sequence: u64) -> Result { + use crate::portable::session::bridge::CheckpointReply; + let sequence = u32::try_from(sequence).map_err(|_| Error::Invalid("The checkpoint sequence is out of range.".into()))?; + let out = self.command(&format!("portable checkpoint {guid} {session} {sequence}"))?; + let first = out.lines().map(str::trim).find(|l| !l.is_empty()).unwrap_or(""); + Ok(match first.split_whitespace().next() { + Some("QUEUED") => CheckpointReply::Queued, + Some("BUSY") => CheckpointReply::Busy, + Some("NOT_ONLINE") => CheckpointReply::NotOnline, + Some("REFUSED") => CheckpointReply::Refused(first.trim_start_matches("REFUSED").trim().to_string()), + _ => CheckpointReply::Refused(format!("unexpected answer: {first}")), + }) + } + + /// `portable import `: the core imports the job file of that id from its fixed job directory. Only the id travels. + pub fn portable_import(&mut self, job_id: crate::portable::ImportId) -> Result { + let out = self.command(&format!("portable import {job_id}"))?; + let first = out.lines().map(str::trim).find(|l| !l.is_empty()).unwrap_or("").to_string(); + if first.starts_with("OK") { + Ok(first) + } else { + Err(Error::Invalid(format!("The server did not import the job: {first}"))) + } + } + + /// `portable release `: lets a held portable character play. Sent only after its baseline was persisted. + pub fn portable_release(&mut self, session: crate::portable::SessionId) -> Result<()> { + let out = self.command(&format!("portable release {session}"))?; + if out.lines().any(|l| l.trim().starts_with("OK")) { + Ok(()) + } else { + Err(Error::Invalid(format!("The server did not release the session: {}", out.lines().last().unwrap_or("")))) + } + } + /// World update timing from the server's own `server info` report (mean/median/percentiles of the last 500 updates). pub fn performance(&mut self) -> Result> { let out = self.command("server info")?; diff --git a/docs/PORTABLE_IMPORT_SERVICE.md b/docs/PORTABLE_IMPORT_SERVICE.md index 4ce3dd0..96e6584 100644 --- a/docs/PORTABLE_IMPORT_SERVICE.md +++ b/docs/PORTABLE_IMPORT_SERVICE.md @@ -1,7 +1,8 @@ # PortableImportService (production importer): design -Status: **design only, approved, not implemented.** It is a core-side workstream (the fork's `src/server/game/Tools/`, next to -`PlayerDump`). The offline importer ([`PORTABLE_IMPORT.md`](PORTABLE_IMPORT.md)) stays the tool for stopped realms and for +Status: **implemented in Phase 5** (core fork branch `feat/portable-session-bridge`: `src/server/coa/CoAPortableImport.{h,cpp}`, +`CoAPortableJson.{h,cpp}`; Manager side `portable/realm/online.rs`). Differences from this design are listed at the end. It is a core-side +workstream next to `PlayerDump`. The offline importer ([`PORTABLE_IMPORT.md`](PORTABLE_IMPORT.md)) stays the tool for stopped realms and for tests. ## Why in-core @@ -75,3 +76,21 @@ the level-cap projection hooks of Phase 8. Exports from a running realm (checkpoints need a per-character save command, `.character save `, tracked separately), a payload over RA, a path argument, a remote caller of any kind. + +## Implementation notes (Phase 5) + +* **Job format.** The job file is `
+`, not zstd: the core has neither a JSON nor a zstd + dependency, and a small strict parser (`CoAPortableJson`: bounded depth/nodes/string size, integers only, duplicate and unknown + fields refused, UTF-8 checked) is easier to audit than a new dependency. The header carries `snapshot_sha256` of the exact bytes of the + second line; the file size is capped at 24 MiB before it is read. The Manager ships the character **without extensions** and without + settings the policy does not carry. +* **Writes.** Every row is written by a prepared `INSERT` (`CHAR_INS_PORTABLE_*`); the only string-built statements are the + `DELETE ... WHERE = ` that clear leftovers of a deleted character with the same guid. +* **Commit.** `DirectCommitTransaction`, then the marker is read back; the name cache entry, the item/pet/player generators and + `UpdateRealmCharCount` change only after that read confirms the commit. +* **Talents.** Stock talent rows are written only when the spell is a talent of the realm's own `TalentSpellPos` (the check the worldserver + asserts on); everything else is reported in `not_applied`. +* **Idempotency.** The marker `coa.portable.import = ` is looked up first; a repeated job id answers `OK` and creates nothing. +* **Session.** With a `session` in the header the same transaction writes the `coa_portable_session` row in state 0, so the character's + first load takes its baseline (see `PORTABLE_SESSIONS.md`). diff --git a/docs/PORTABLE_SESSIONS.md b/docs/PORTABLE_SESSIONS.md index 53695ac..b562822 100644 --- a/docs/PORTABLE_SESSIONS.md +++ b/docs/PORTABLE_SESSIONS.md @@ -184,6 +184,21 @@ Manager writes /.job (temp name + rename) No path, SQL or snapshot travels through RA. Idempotency: the marker row carries the job nonce; re-running a finished job rewrites the same result. Never touches a character that is online or already exists. +## 7a. Behaviour of the implementation worth knowing + +* The gate holds the **session's packet queue** (`WorldSession::Update` skips it while gated) and makes the character non-attackable + and rooted. The timeout is enforced from a world-update hook, not from the session, so sessions without a socket are covered too; a + real client is kicked, a bot session (no socket) is only reported. +* A **clean shutdown logs every player out**: the logout save of each portable character carries `state = 3`, exactly like a normal + logout, and the Host sends the final checkpoint and arms the next session. A **crash** leaves `state = 2`: the next login continues the + same session with no new baseline. +* A character that logs in while its row is still `state = 3` (the Host has not closed the session yet) is disconnected with a + "try again in a moment" message: a baseline would otherwise be taken against a session the Owner already closed. +* The session table is read at login by the login query holder (`CHAR_SEL_PORTABLE_SESSION`), so a realm whose database lacks + `coa_portable_session` refuses to start like any other missing table: the pending SQL ships with the core. +* One checkpoint writes one character: verified on a real worldserver with two other characters online by counting the `characters` + rows the database was asked to write. + ## 8. What is not in this phase VPS Registry/Relay and any network transport, public server list, remote account provisioning, Wildcard, vanity/wardrobe diff --git a/tools/reset_live_realm.ps1 b/tools/reset_live_realm.ps1 index 2c3653f..388900a 100644 --- a/tools/reset_live_realm.ps1 +++ b/tools/reset_live_realm.ps1 @@ -19,7 +19,8 @@ param( [Parameter(Mandatory)] [int] $Port, [Parameter(Mandatory)] [string] $Fixture, [Parameter(Mandatory)] [string] $Sql, - [string] $RootPassword = 'portable-test' + [string] $RootPassword = 'portable-test', + [string] $ExtraSql = '' ) $ErrorActionPreference = 'Stop' $dir = Join-Path $Root $Name @@ -52,4 +53,5 @@ for ($i = 0; $i -lt 60; $i++) { } $env:MYSQL_PWD = $RootPassword Get-Content $Sql -Raw | & (Join-Path $bin 'bin\mysql.exe') --protocol=tcp --host=127.0.0.1 "--port=$Port" --user=root --default-character-set=utf8mb4 --max-allowed-packet=64M +if ($ExtraSql) { Get-Content $ExtraSql -Raw | & (Join-Path $bin 'bin\mysql.exe') --protocol=tcp --host=127.0.0.1 "--port=$Port" --user=root --default-character-set=utf8mb4 } "realm $Name reset: pid $($p.Id), port $Port"