diff --git a/prototype-server-vision/README.md b/prototype-server-vision/README.md index 58f24aa..5f53507 100644 --- a/prototype-server-vision/README.md +++ b/prototype-server-vision/README.md @@ -23,7 +23,7 @@ Browser rendering was NOT verified (no browser in the build environment): only H | `devbox run smoke` | fresh DB, server+nginx, auth/cookie/tile/steal/garbage checks | | `devbox run leaktest` | runs twice (SUBJECT=A, SUBJECT=B), fresh DB each; subject walks ~240 steps through rooms, corridor, open/closed doors while GM + B act; Nim checker (`tools/leakcheck.nim`) replays the transcript. Output per subject: `results/leak-A.log`, `results/leak-B.log` (committed), transcripts `results/leak-{A,B}.jsonl` (1.1-1.3 MB, gitignored) | | `devbox run faulttest` | deliberate leaks: builds the server with `-d:leakfault` (hook absent from the normal build, asserted by grepping the clean binary for `LEAK_FAULT`), injects 8 faults one at a time (`LEAK_FAULT=`): hidden token sent (`hidden`), wall data to player (`walls`), unexplored tile 200 (`tile`), visible token withheld (`withhold`), B's vision / explored / ownership sent to A (`other-vision`, `other-explored`, `other-owner`), missing `remove` (`noremove`). Passes only if the checker fails each with exactly the expected failure-class set (classes: HIDDEN_TOKEN WALLDATA TILE WITHHELD OTHER_PLAYER NO_REMOVE, plus collateral OWN_VIEW / EXPLORED) and the clean build still passes. Details in `results/faults.log` (gitignored) | -| `devbox run restart` | isolated server, moves A/B/monsters, toggles doors, A explores a new area; stops with SIGTERM then (second round of changes) SIGKILL, restarts on the same DB; asserts invite secrets, token positions, doors, vision, explored grids per role and the full hello+state a reconnecting client receives are identical (gseq excluded: in-memory). Note: the server has no SIGTERM handler, so SIGTERM is as abrupt as SIGKILL; durability rests on SQLite WAL commit before ack | +| `devbox run restart` | isolated server, moves A/B/monsters, toggles doors, A explores a new area; stops with SIGTERM then (second round of changes) SIGKILL, restarts on the same DB; asserts invite secrets, token positions, doors, vision, explored grids per role and the full hello+state a reconnecting client receives are identical (gseq excluded: in-memory). Note: the server has no SIGTERM handler, so SIGTERM is as abrupt as SIGKILL; durability rests on the SQLite WAL commit before ack (`synchronous=NORMAL`: survives process kill, may lose the last commits on OS crash/power loss; see `results/spike-investigation.md`) | | `devbox run concurrency` | 60 s (`DUR=` secs) A, B, GM(6 monsters + doors) at 20-50 ms per token; logs `results/concurrency-s.log` + `...-server.log` (server STATS) | | `devbox run mutation` | geometry mutation check on a temp copy (`tools/mutation.sh`): (a) no ±RayEps rays, (b) upper segment end exclusive, (c) lower end exclusive; control must pass, each mutation must fail a corner test. Logs `results/mutation-{a,b,c}.log` | | `devbox run bench` | vision microbench, log `results/bench.log` | diff --git a/prototype-server-vision/results/concurrency-fixed-run1-server.log b/prototype-server-vision/results/concurrency-fixed-run1-server.log new file mode 100644 index 0000000..6c18cf1 --- /dev/null +++ b/prototype-server-vision/results/concurrency-fixed-run1-server.log @@ -0,0 +1,15 @@ +STATS bytes/delivery door gm: n=58 avg=44.9 worst=46. +STATS bytes/delivery move-monster A: n=754 avg=146.4 worst=154. +STATS bytes/delivery move-monster B: n=400 avg=137.6 worst=155. +STATS bytes/delivery move-monster gm: n=10266 avg=242.8 worst=252. +STATS bytes/delivery move-player A: n=1707 avg=3854.9 worst=5028. +STATS bytes/delivery move-player B: n=1710 avg=3394.4 worst=4724. +STATS bytes/delivery move-player gm: n=3409 avg=144.5 worst=149. +STATS timing_ms lock_wait: n=13741 avg=0.148 p50=0.000 p99=2.687 worst=16.594 +STATS timing_ms send: n=13741 avg=0.063 p50=0.040 p99=0.199 worst=0.394 +STATS timing_ms sqlite: n=13741 avg=0.167 p50=0.137 p99=0.314 worst=17.739 +STATS timing_ms total: n=13741 avg=0.792 p50=0.288 p99=4.203 worst=21.053 +STATS timing_ms vision: n=13741 avg=0.351 p50=0.000 p99=3.172 worst=6.325 +STATS vision_ms door: n=58 avg=0.236 worst=0.344 +STATS vision_ms move-monster: n=10266 avg=0.000 worst=0.081 +STATS vision_ms move-player: n=3417 avg=1.405 worst=6.325 diff --git a/prototype-server-vision/results/concurrency-fixed-run1.log b/prototype-server-vision/results/concurrency-fixed-run1.log new file mode 100644 index 0000000..403bdec --- /dev/null +++ b/prototype-server-vision/results/concurrency-fixed-run1.log @@ -0,0 +1,6 @@ +gm: sent 10281 moves (164/s), acks 10281 (nack 15), recv 24018 msgs 2925 KiB, final gseq 13741, tokens in view 8 +A: sent 1715 moves (27/s), acks 1715 (nack 8), recv 6041 msgs 6540 KiB, final gseq 13738, tokens in view 1 +B: sent 1710 moves (27/s), acks 1710 (nack 0), recv 5687 msgs 5727 KiB, final gseq 13737, tokens in view 1 +latency intent->ack (sender): n=13706 avg=1.51ms p95=3.48ms p99=5.09ms worst=21.33ms +latency intent->arrival at other clients: n=4490 avg=2.33ms p95=4.37ms p99=5.79ms worst=21.36ms +concurrency: PASS diff --git a/prototype-server-vision/results/concurrency-fixed-run2-server.log b/prototype-server-vision/results/concurrency-fixed-run2-server.log new file mode 100644 index 0000000..8683681 --- /dev/null +++ b/prototype-server-vision/results/concurrency-fixed-run2-server.log @@ -0,0 +1,15 @@ +STATS bytes/delivery door gm: n=66 avg=44.7 worst=46. +STATS bytes/delivery move-monster A: n=649 avg=146.1 worst=153. +STATS bytes/delivery move-monster B: n=1210 avg=147.0 worst=154. +STATS bytes/delivery move-monster gm: n=10238 avg=242.6 worst=252. +STATS bytes/delivery move-player A: n=1691 avg=3626.7 worst=4956. +STATS bytes/delivery move-player B: n=1703 avg=3336.6 worst=4459. +STATS bytes/delivery move-player gm: n=3394 avg=144.9 worst=149. +STATS timing_ms lock_wait: n=13698 avg=0.123 p50=0.000 p99=2.178 worst=18.542 +STATS timing_ms send: n=13698 avg=0.062 p50=0.040 p99=0.185 worst=0.480 +STATS timing_ms sqlite: n=13698 avg=0.170 p50=0.138 p99=0.315 worst=18.178 +STATS timing_ms total: n=13698 avg=0.715 p50=0.287 p99=3.432 worst=20.612 +STATS timing_ms vision: n=13698 avg=0.295 p50=0.000 p99=2.385 worst=4.037 +STATS vision_ms door: n=66 avg=0.222 worst=0.345 +STATS vision_ms move-monster: n=10238 avg=0.000 worst=0.029 +STATS vision_ms move-player: n=3394 avg=1.187 worst=4.037 diff --git a/prototype-server-vision/results/concurrency-fixed-run2.log b/prototype-server-vision/results/concurrency-fixed-run2.log new file mode 100644 index 0000000..0548213 --- /dev/null +++ b/prototype-server-vision/results/concurrency-fixed-run2.log @@ -0,0 +1,6 @@ +gm: sent 10281 moves (166/s), acks 10281 (nack 43), recv 23979 msgs 2918 KiB, final gseq 13698, tokens in view 8 +A: sent 1714 moves (28/s), acks 1714 (nack 23), recv 5871 msgs 6089 KiB, final gseq 13693, tokens in view 2 +B: sent 1703 moves (28/s), acks 1703 (nack 0), recv 6444 msgs 5728 KiB, final gseq 13694, tokens in view 2 +latency intent->ack (sender): n=13698 avg=1.43ms p95=3.04ms p99=4.28ms worst=20.83ms +latency intent->arrival at other clients: n=5194 avg=2.03ms p95=3.61ms p99=4.93ms worst=20.95ms +concurrency: PASS diff --git a/prototype-server-vision/results/concurrency-fixed-run3-server.log b/prototype-server-vision/results/concurrency-fixed-run3-server.log new file mode 100644 index 0000000..bce990c --- /dev/null +++ b/prototype-server-vision/results/concurrency-fixed-run3-server.log @@ -0,0 +1,15 @@ +STATS bytes/delivery door gm: n=64 avg=44.8 worst=46. +STATS bytes/delivery move-monster A: n=384 avg=142.5 worst=154. +STATS bytes/delivery move-monster B: n=1018 avg=148.1 worst=154. +STATS bytes/delivery move-monster gm: n=10300 avg=243.1 worst=252. +STATS bytes/delivery move-player A: n=1708 avg=3746.7 worst=4879. +STATS bytes/delivery move-player B: n=1725 avg=3242.8 worst=4766. +STATS bytes/delivery move-player gm: n=3429 avg=144.6 worst=149. +STATS timing_ms lock_wait: n=13797 avg=0.126 p50=0.000 p99=2.384 worst=18.662 +STATS timing_ms send: n=13797 avg=0.061 p50=0.039 p99=0.185 worst=0.369 +STATS timing_ms sqlite: n=13797 avg=0.169 p50=0.136 p99=0.312 worst=18.797 +STATS timing_ms total: n=13797 avg=0.724 p50=0.282 p99=3.789 worst=19.906 +STATS timing_ms vision: n=13797 avg=0.305 p50=0.000 p99=2.841 worst=4.198 +STATS vision_ms door: n=64 avg=0.217 worst=0.304 +STATS vision_ms move-monster: n=10300 avg=0.000 worst=0.039 +STATS vision_ms move-player: n=3433 avg=1.221 worst=4.198 diff --git a/prototype-server-vision/results/concurrency-fixed-run3.log b/prototype-server-vision/results/concurrency-fixed-run3.log new file mode 100644 index 0000000..3d1c8c3 --- /dev/null +++ b/prototype-server-vision/results/concurrency-fixed-run3.log @@ -0,0 +1,6 @@ +gm: sent 10311 moves (166/s), acks 10311 (nack 11), recv 24109 msgs 2939 KiB, final gseq 13797, tokens in view 8 +A: sent 1715 moves (28/s), acks 1715 (nack 7), recv 5671 msgs 6309 KiB, final gseq 13793, tokens in view 2 +B: sent 1725 moves (28/s), acks 1725 (nack 0), recv 6322 msgs 5615 KiB, final gseq 13792, tokens in view 2 +latency intent->ack (sender): n=13751 avg=1.43ms p95=3.20ms p99=4.49ms worst=20.95ms +latency intent->arrival at other clients: n=4774 avg=2.10ms p95=3.91ms p99=4.78ms worst=21.13ms +concurrency: PASS diff --git a/prototype-server-vision/results/postfix-faulttest.log b/prototype-server-vision/results/postfix-faulttest.log new file mode 100644 index 0000000..9bb7362 --- /dev/null +++ b/prototype-server-vision/results/postfix-faulttest.log @@ -0,0 +1,11 @@ +Info: Running script "faulttest" on /home/davide/Projects/GDR/FFF_vtt/prototype-server-vision +PASS run/test/server-clean has no fault hook +PASS fault hidden caught as: HIDDEN_TOKEN +PASS fault walls caught as: WALLDATA +PASS fault tile caught as: TILE +PASS fault withhold caught as: WITHHELD +PASS fault other-vision caught as: OTHER_PLAYER OWN_VIEW +PASS fault other-explored caught as: EXPLORED OTHER_PLAYER +PASS fault other-owner caught as: OTHER_PLAYER +PASS fault noremove caught as: NO_REMOVE +PASS clean build passes diff --git a/prototype-server-vision/results/postfix-leaktest.log b/prototype-server-vision/results/postfix-leaktest.log new file mode 100644 index 0000000..0d4b63c --- /dev/null +++ b/prototype-server-vision/results/postfix-leaktest.log @@ -0,0 +1,7 @@ +Info: Running script "leaktest" on /home/davide/Projects/GDR/FFF_vtt/prototype-server-vision +== SUBJECT=A +driver done: 237 actions, 0 unexpected acks, final gseq 217 +leakcheck: 218 gseq groups, subject msgs 467, tokens seen 4 others, removes 4, tiles 9984 (200:1709 403:8275 other:0), explored cells 2142, failures 0 +== SUBJECT=B +driver done: 237 actions, 0 unexpected acks, final gseq 217 +leakcheck: 218 gseq groups, subject msgs 217, tokens seen 3 others, removes 3, tiles 7424 (200:686 403:6738 other:0), explored cells 1114, failures 0 diff --git a/prototype-server-vision/results/postfix-restart.log b/prototype-server-vision/results/postfix-restart.log new file mode 100644 index 0000000..498e2d7 --- /dev/null +++ b/prototype-server-vision/results/postfix-restart.log @@ -0,0 +1,28 @@ +Info: Running script "restart" on /home/davide/Projects/GDR/FFF_vtt/prototype-server-vision +PASS move A -> 400,800 +PASS move A -> 800,800 +PASS move A -> 800,640 +PASS move A -> 1000,640 +PASS move A -> 1510,640 +PASS move B -> 700,2300 +PASS move M1 -> 900,640 +PASS move M2 -> 2200,300 +PASS move A -> 1700,640 +PASS cycle1: invite secrets identical +PASS cycle1: gm state identical after SIGTERM restart +PASS cycle1: A state identical after SIGTERM restart +PASS cycle1: B state identical after SIGTERM restart +PASS A explored new area (292 -> 1145) +PASS cycle1: token positions are the moved ones +PASS cycle1: door states are the toggled ones +PASS cycle1: doors differ from initial (test is not vacuous) +PASS move A -> 1900,640 +PASS move B -> 1000,2300 +PASS move M1 -> 1200,640 +PASS cycle2: invite secrets identical +PASS cycle2: gm state identical after SIGKILL restart +PASS cycle2: A state identical after SIGKILL restart +PASS cycle2: B state identical after SIGKILL restart +PASS cycle2: A explored more before kill -9 +PASS cycle2: B explored more before kill -9 +restart: PASS diff --git a/prototype-server-vision/results/spike-investigation.md b/prototype-server-vision/results/spike-investigation.md new file mode 100644 index 0000000..24ed8da --- /dev/null +++ b/prototype-server-vision/results/spike-investigation.md @@ -0,0 +1,32 @@ +# 583 ms spike investigation (results/concurrency-60s.log: worst intent->ack 583.13 ms, avg 6.33 ms) + +Setup: DUR=60, isolated stack (run/test, :18088/:19000), CPU powersave governor untouched, runs strictly sequential. +Server now logs per mutation: lock_wait / vision / sqlite / send / total, `SLOW` line when total > `SLOW_MS` (default 50), percentiles in `STATS timing_ms` (end of run + every 5000 mutations). +Client p99 added to `tools/concurrency.mjs`. Logs: `results/concurrency-run{1,2,3}[-server].log` (baseline), `results/concurrency-fixed-run{1,2,3}[-server].log` (after fix). + +## Runs (client intent->ack, ms | server total per mutation, ms) +| run | avg | p99 | worst | srv avg | srv p99 | srv worst | srv sqlite avg / worst | srv lock_wait worst | +|---|---|---|---|---|---|---|---|---| +| original (no timing) | 6.33 | n/a | 583.13 | - | - | - | - | - | +| base 1 | 1.84 | 5.17 | 73.08 | 1.143 | 4.450 | 72.870 | 0.616 / 62.928 | 71.704 | +| base 2 | 1.84 | 5.48 | 21.83 | 1.207 | 4.874 | 21.348 | 0.592 / 21.248 | 11.928 | +| base 3 | 1.78 | 4.51 | 18.14 | 1.133 | 3.934 | 14.178 | 0.609 / 9.628 | 13.060 | +| fixed 1 | 1.51 | 5.09 | 21.33 | 0.792 | 4.203 | 21.053 | 0.167 / 17.739 | 16.594 | +| fixed 2 | 1.43 | 4.28 | 20.83 | 0.715 | 3.432 | 20.612 | 0.170 / 18.178 | 18.542 | +| fixed 3 | 1.43 | 4.49 | 20.95 | 0.724 | 3.789 | 19.906 | 0.169 / 18.797 | 18.662 | + +(base runs' sqlite worst: run 2 file shows 21.2, run 3 file 9.6; table taken per file.) + +## Slowest mutations (baseline; only one exceeded 50 ms in 3 runs, `SLOW` lines in `*-server.log`) +- run1 gseq=136 move-player total=63.8: sqlite=62.9 (COMMIT), vision 0.7, send 0.1, lock_wait 0. +- run1 gseq=138 move-monster total=56.6: lock_wait=55.9 (queued behind gseq=136's commit). +- run1 gseq=152 move-player total=72.9: lock_wait=71.7 (queued behind another stalled holder), rest < 1. +- run2/3 worst 21 / 14 ms: also sqlite commit (21.2 / 9.6), others queue behind it as lock_wait (12 / 13). +Vision (worst 4-5 ms) and send enqueue (worst < 0.6 ms) never matter. The global lock turns every stalled COMMIT into lock_wait for the mutations queued behind it. + +## Verdict +- 583 ms does NOT reproduce: worst 73 / 22 / 18 ms over 3 runs, avg 1.8 ms vs 6.33 ms in the original run (original run was ~3.5x slower on average too, i.e. the whole run was degraded, not one event; the original server had no timing, so its breakdown is unrecoverable). Most likely host-wide (power cap / scheduler / disk) or node harness stall at that time; client-side not excluded: the 583.13 (ack) and 583.22 (other clients) worst are the same event seen from the same node process, which fits either a server/nginx stall or a node event-loop/GC pause. Not proven either way. +- What the breakdown does show: server-side tail (>10 ms) is the SQLite COMMIT (fsync, synchronous=FULL in WAL) + lock queueing behind it. Compute (vision) and send are not the tail. +- Fix: `PRAGMA synchronous=NORMAL` (WAL), 1 line in `src/server.nim`. Effect (before -> after, mean of 3 runs): sqlite avg 0.60 -> 0.17 ms, p50 0.60 -> 0.14 ms, server total avg 1.16 -> 0.74 ms, client avg 1.82 -> 1.46 ms. Tail is NOT fixed: server worst stays ~20 ms (sqlite worst ~18 ms still, now likely WAL auto-checkpoint / OS scheduling rather than per-commit fsync), client p99 ~4.6-5.1 ms unchanged within noise. The 73 ms outlier of base run 1 did not recur in fixed runs, but n=3, not evidence. +- Tradeoff: with synchronous=NORMAL in WAL, a commit is not fsynced; process crash/SIGKILL loses nothing (restart test passes), OS crash / power loss may drop the last few committed mutations (never corrupts). Ack-before-durable on power loss. Not done: moving persist out of the lock (would break gseq order == send order / ack-after-commit). +- After the change leaktest, faulttest, restart re-run: all PASS (`results/postfix-{leaktest,faulttest,restart}.log`; leak-A/B logs byte-identical to committed). diff --git a/prototype-server-vision/src/server.nim b/prototype-server-vision/src/server.nim index f688bfc..acfcced 100644 --- a/prototype-server-vision/src/server.nim +++ b/prototype-server-vision/src/server.nim @@ -328,6 +328,7 @@ proc loadState() = createDir runDir db = open(runDir / "state.db", "", "", "") discard db.getAllRows(sql"PRAGMA journal_mode=WAL") + db.exec(sql"PRAGMA synchronous=NORMAL") # WAL: commit no longer fsyncs (power loss may drop the last commits, never corrupts; process kill loses nothing) for s in ["CREATE TABLE IF NOT EXISTS meta (k TEXT PRIMARY KEY, v TEXT)", "CREATE TABLE IF NOT EXISTS tok (id TEXT PRIMARY KEY, x REAL, y REAL)", "CREATE TABLE IF NOT EXISTS door (id INTEGER PRIMARY KEY, open INTEGER)",