Skip to content

id(worlds): #557 second pass — write the journal inline instead of via a writer thread - #12552

Merged
dezren39 merged 2 commits into
mainfrom
felix/557-more-wins
Nov 22, 2026
Merged

dezren39 merged 2 commits into
mainfrom
felix/557-more-wins

Conversation

@dezren39

@dezren39 dezren39 commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Second-pass review of the many-worlds event path for #557, per the direction to look for any other obvious wins regardless of the 50 ms target. One clean win landed; the rest of the seams were audited and are recorded below with their evidence.

Landed: write the journal inline instead of via a writer thread (ee371a0)

FileJournal handed every append to a dedicated writer thread over a channel and rendezvoused on its reply. The journal's only producer is its owning actor, which reaches it through &mut and cannot have a second append in flight while one waits for durability — so the writer's queue was empty at every dequeue by construction (the same structural reason the actor-side drain reads queue_depth_max=0 in every profile). Each append paid a channel send, a thread wake, and a rendezvous reply around the same write+sync, plus one thread per world (1,100 threads at bench scale).

Now the journal owns its File and writes inline via sync_journal_write, which runs the one write_all + one sync_data under the same block_in_place guard the first #557 fix established, so no shared Tokio worker parks for the sync. Compaction's writer-replace becomes a field assignment (ordering already guaranteed by &mut). No durability or ordering change: an append is still acknowledged only after the sync that covers its bytes completes.

Before/after (sandbox venue, TMPDIR on btrfs, full 1,000 idle / 100 active, 20 ev/s, 10 s)

run journal_append_mean p50 p99 completed/s
baseline a968bf0f0 (pair run, load avg 8.5 at start) 52,713 µs 580.966 ms 1,455.158 ms 1,733.8
this change (pair run, back-to-back, load avg 2.2 at start) 58,407 µs 1,270.688 ms 2,735.997 ms 1,549.4
this change (earlier run, load avg ~1.4–4.6) 47,713 µs 109.730 ms 1,003.589 ms 1,825.6
baseline (day's heaviest contention, load avg ~11–14) 236,445 µs 12,581.832 ms 37,764.450 ms 411.8

Verdict: not measurable in this venue. The sandbox is shared, and its load swung widely between runs; latency percentiles moved ±2× with ambient load (in the pair, the wake percentiles doubled while input_ack_p50 stayed flat at ~62–63 ms in both runs), while the mechanism metric — journal append mean — stayed in one band (47.7k–58.4 µs for this change vs 52.7k for the baseline at comparable load). No run shows a regression attributable to the change. The change is landed as a structural simplification — two fewer synchronization hops per append, one fewer thread per world, −105/+31 lines — with durability semantics unchanged; whether neutral-but-simpler is worth merging is the reviewer's call. All runs: queue_depth_max=0, checkpoints/evicted/reopened 100/100/100. The runs are also recorded in pkgs/id/benches/BASELINES.md; baseline.txt is untouched (reference machine class). For reference, the first campaign's post-fix sandbox run measured journal_append_mean 50,473 µs / p99 842.887 ms / 1,836.3 completed/s — the same band.

Tests: cargo test -p id-worlds --lib — 836 passed / 40 failed. 39 failures are the known live-network timeout class filed as #12488 (38 named *_net plus world_records peer-sync "deadline has elapsed"). The one remaining failure, write_lease_entries_require_journal_version_7, is pre-existing on a968bf0f0: the kv provider (PR #12489) moved MAX_JOURNAL_VERSION 12 → 13 without updating that test's pin; filed as #12541. Zero failures attributable to this change. cargo fmt -p id-worlds -- --check is clean for the touched file (pre-existing drift in lib.rs / world/replication.rs left alone).

Seam audit (no change)

  1. Sync call — already minimal. The append path is exactly one write_all + one sync_data (fdatasync). No sync_all, no directory sync per append (directory sync appears only in compaction, after the rename). Nothing heavier to remove.
  2. Writer-side batching — structurally impossible, by the same argument as above. Coalescing appends queued at the writer would coalesce a queue that cannot form: the sole producer is rendezvous-blocked until the in-flight append is durable. Group commit remains available only across worlds (syncfs — evaluated and rejected in the first campaign, patch preserved) or across a world's own staged batch (append_batch already concatenates to one write + one sync).
  3. Hop count. Per input after this change: hub handle → actor command channel (one send + one task wake) → actor [prepare → guest update → persist → commit] → oneshot reply. The two journal hops (channel to writer thread, rendezvous reply) are the ones removed here. No other re-queue-only hop exists in the path; the hub lease hands out an Arc share of the actor's own command sender.
  4. Append-path copies — immaterial at measured scales. One serde_json::to_vec per entry is required. persist() additionally builds a second full serialization (journal_entry_line) for the replication broadcast even when it has zero receivers; gating it on receiver_count() would be safe (late subscribers resync from snapshot; broadcast never replays pre-subscribe appends). Evaluated and rejected as unmeasurable churn: on the post-fix laptop run the whole non-append command path — prepare, guest update, records, this serialization, channel sends — fits inside the difference between command_mean_us=25692 and journal_append_mean_us=25784. Recorded here so a future CPU-bound profile can pick it up.
  5. Ack semantics — design question, deliberately not changed. input_ack waits for the journal sync in both the single and batch paths; that is the documented WorldJournal contract ("acknowledged history must never get ahead of durable history"). On the post-fix laptop run, input_ack_p50 = 18.892 ms against journal_append_mean = 25.784 ms — the ack is the sync wait plus scheduling. Any batched/async ack changes durability semantics and is a decision for @dezren39, not a clean win.

Refs #557


Run Proof
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

felix added 2 commits November 22, 2026 12:42
FileJournal handed every append to a dedicated writer thread over a
channel and rendezvoused on its reply. The journal's only producer is
its owning actor, which reaches it through &mut and cannot have a
second append in flight while one waits for durability, so the
writer's queue was empty at every dequeue by construction (the same
structural reason the actor-side drain reads queue_depth_max=0 in the
many-worlds profile): each append paid a channel send, a thread wake,
and a rendezvous reply around the same write+sync, plus one thread
per world.

The journal now owns its File and writes inline through
sync_journal_write, which runs the write_all + sync_data under the
same block_in_place guard the #557 worker-parking fix established, so
no shared Tokio worker parks for the sync. Compaction's writer
replace becomes a field assignment. No durability or ordering change:
an append is still acknowledged only after the sync that covers its
bytes completes.
@dezren39
dezren39 requested a review from a team as a code owner November 22, 2026 13:26
@coderabbitai

coderabbitai Bot commented Nov 22, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 42 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1d8b3186-8716-4983-9e99-65058d3c6284
Commits

Reviewing files that changed from the base of the PR and between 2045c65 and 3a15e11.

Files selected for processing: 2

  • pkgs/id/benches/BASELINES.md
  • pkgs/id/crates/id-worlds/src/world_store.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T13:28:04.869916Z 3a15e11 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@dezren39
dezren39 merged commit e683fad into main Nov 22, 2026
2 checks passed
@dezren39
dezren39 deleted the felix/557-more-wins branch November 22, 2026 13:28
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