Skip to content

perf(sql): fewer PostgreSQL round trips per delegated /invoke (TC-3739 5+1) - #695

Merged
samgbafa merged 9 commits into
mainfrom
perf/tc-3739-dedupe-round-trips
Nov 22, 2026
Merged

samgbafa merged 9 commits into
mainfrom
perf/tc-3739-dedupe-round-trips

Conversation

@samgbafa

@samgbafa samgbafa commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

TC-3739 "5+1". Part 1 removes the duplicate PostgreSQL round trips on a delegated SQL /invoke. Part 5 ships the node_request fields the node already logs, pool_wait_ms, query_time_ms and query_count, through the log shipper.

Round trips, sequential statements per request, measured on local PG 14 through a wire-counting proxy with 43 requests per case. Every request in a case gave the same count.

case main 665c4dc this PR
warm, 1 hop 16 9
warm, 2 hops 18 9
cold, 1 hop 18 11
cold, 2 hops 20 11

Removed duplicates:

  1. The handler's second cutover-fence check. invoke_impl already checks the fence before invoked_abilities is persisted. The handler's check held no lock, so it closed no race.
  2. The second identity resolve. It used the same inputs as the first, which runs behind that fence check. handle_sql_invoke now takes db_name from invoke_impl.
  3. The statements-profile chain walk, a read-only transaction with 2 SELECTs per level.
    • resolve_sql_profile now reads profiles from the ability rows that authorization's proof_roots graph already loaded, under the chain guards (TransactResult.proof_abilities). That set is the same closure the walk visited: the roots plus every ancestor over parent_delegation.
    • The old walk (derive_chain_sql_profiles_with_conn) remains for paths that return no graph, such as SQL mixed with a KV write.

Kept on purpose:

  • The chain closure for lock ids. Its guards must be held before the graph is read, so that a revocation is ordered against the authorization decision (TC-324).
  • proof_roots itself.
  • The policy_v3 load_signed_delegation. Nothing earlier in the request has loaded that row.

Tests:

  • New: routes::tests::tc3739_ancestor_profile_from_authorization_graph_governs_raw_invocation.
    • Only the grandparent carries the profile; the raw query gets 403 and the named statement runs with its pinned value.
    • A deliberate regression that reads only the cited parents' rows makes this test fail with 200.
    • The test is new; existing tests are unchanged.
  • postgres_round_trip_budget passes 9/9; it uses ceilings, so fewer round trips pass.
  • These also pass:
    • tinycloud-core lib and integration tests;
    • w1_native_contract and sql_identity_cli;
    • --lib tc3739, w1_, sql and policy_v3, 227 tests at --test-threads=2.
  • Local caveat: some Postgres tests need PG 16 (collation rules, renewal precision) and fail on the local PG 14 for that reason, not because of this change. CI is the authority.

Part 5, the log shipper:

  • The canonical ops/logging/fields.json (samgbafa/tinycloud-dev#364) adds pool_wait_ms and query_time_ms as numbers (0 to 86,400,000) and query_count as an integer. It is vendored into log-shipper/fields.json, and canonical.json is re-pinned to revision 29ee97f9… (sha 100f5294…).
  • sanitize.vrl copies the three fields only on node_request lines with status < 400 (Naomi's ruling A, 05:22Z), and only as plain numbers within the bounds. Anything else is dropped and the event still ships. The capped 4xx and 5xx classes carry none of them, and cases.mjs adds Vector cases for status 399 and for 599.
  • budget.mjs:
    • node is the status-399 line with the DB fields, 551 B;
    • nodeCapped is the status-599 line without them, and it sizes the capped node classes;
    • the capped 30-day reserve is 585.4 MB, unchanged; the 600 MB cap is unchanged.
  • The compose VECTOR_CONFIG_JSON and the tc959-n0-compose.yaml fixture are regenerated with scripts/tc959-logship.mjs on main 291c6ad (caveats + T3).
  • N0 template c6d05e1e… → cc219eb5a05deda365a9683b6c6dc407eb872cb77d9f3954a819d1805ddd2249 (raw 02d907552b74066b0d49f1088cc0b7ba9ca3e842eb9c1444b3cd778474498eb9). This updates the docker.yml pin and comment, docs/releasing.md in both places, and the CHANGELOG.
  • Checks: all pass.
    • The logship node tests: 43/43.
    • tc959-check.
    • native-check (5 request events and 1 db_failure, wire-safe, 6 positive controls) and cap-check with the local Vector binary.
    • scripts/ci/test-deploy-recovery.py.
    • cargo check -p tinycloud-node --tests after merging main.
  • Test edits in log-shipper/budget.test.mjs:
    • one new test: DB accounting sizes only the status < 400 node line;
    • the two capped node-class assertions now size from nodeCapped instead of node, because a capped (status ≥ 400) line no longer has the same shape as the status < 400 representative.
    • Each edit carries an in-code test-ruling line. Naomi confirmed both rulings as written at 07:36Z. The capped classes stay within the 600 MB reserve.
  • nodeCapped carries none of the three DB fields. Two places prove this:
    • The new budget test asserts Object.hasOwn(nodeCapped, key) === false for pool_wait_ms, query_time_ms and query_count (log-shipper/budget.test.mjs:37).
    • The new Vector cases in log-shipper/cases.mjs run the real sanitizer for statuses 399, 400, 404, 499, 500, 503 and 599. The fields ship at 399, and for every status ≥ 400 the case requires !exists(.pool_wait_ms) && !exists(.query_time_ms) && !exists(.query_count). CI runs them with vector test on the output of scripts/tc959-logship.mjs render-tests (logship.yml:52-55). Locally all 7 status cases pass in both profiles (errors_slow, all_requests), and so does every other case.
  • Release: this merges only after 1.26.19 is live, under a GO naming its head at that time. Parts 1 and 5 ride 1.26.20 together: one deploy, one N0 (Naomi, 07:36Z).

test-ruling: TC-3739

Details: TC-3739

…walk per delegated /invoke

- invoke_impl's pre-check fence and resolve are the request's only ones; handle_sql_invoke takes its db_name (the handler's second check held no lock and closed no race).
- resolve_sql_profile reads the statements profile from the ability rows the authorization graph already loaded (TransactResult.proof_abilities); it walks the chain only when authorization returned none (writes).
- Kept on purpose: the chain closure for lock ids (TC-324 ordering), proof_roots, the policy_v3 load_signed_delegation.
- Warm 1-hop SELECT 1: 16 -> 9 sequential statements; 2-hop 18 -> 9; cold 18/20 -> 11.

test-ruling: TC-3739 tinycloud-node-server/src/routes/mod.rs new test only (2026-11-22)

Details: TC-3739
… part 5)

- Re-pin canonical fields.json to tinycloud-dev 29ee97f9 (#364): pool_wait_ms, query_time_ms (number 0..86400000), query_count (integer 0..2^53-1).
- sanitize.vrl copies the three fields only on node_request with status < 400, type- and bound-guarded; anything else is dropped and the event still ships (Naomi ruling A, 05:22Z).
- budget: node (status 399 + DB fields) sizes the request allocation (774 B pair, 14.36 rpm); nodeCapped (no DB fields) sizes the capped classes, reserve unchanged at 585.4 MB.
- Vector cases: valid, maxima, mistyped, out-of-bounds, and statuses 399..599 (4xx/5xx carry none). Compose embed regeneration follows after #682 and T3.
…TC-3739)

- Byte-identical to ops/logging/fields.json at 29ee97f9 (sha256 100f5294…); brings the 10 canonical commits since 4fc32a2f plus #364.
…ing fields

- VECTOR_CONFIG_JSON regenerated from scripts/tc959-logship.mjs buildConfig({runtimeProfile:true}); tc959-check parity passes. The TC-959 N0 fixture stays byte-identical to the compose.
- N0 pin follows once T3 (#696) is on main.
N0 template c6d05e1e -> cc219eb5 (raw 02d90755): main's compose (caveats + T3) plus the regenerated Vector embed.
docker.yml pin + comment, docs/releasing.md (both places), CHANGELOG entry.

Details: TC-3739
@samgbafa
samgbafa marked this pull request as ready for review November 22, 2026 07:36
@samgbafa
samgbafa merged commit ccc14ec into main Nov 22, 2026
33 checks passed
@samgbafa
samgbafa deleted the perf/tc-3739-dedupe-round-trips branch November 22, 2026 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant