WIP: feat(server): move the eviction write-back diff onto the worker pool #28

Draft
Serkyo wants to merge 5 commits from feat/async-write-back into feat/entity-chunk-lifecycle
Owner

What does this PR do?

Eviction in ServerWorld::reconcile no longer diffs the chunk on the tick thread. I added a Job::WriteBack { pos, chunk, worldgen_version }. reconcile hands the evicted chunk to the pool by value, and the worker resolves the baseline through the shared ChunkCache, builds the ChunkData::from_diff, and sends SaveRequest::Write or Remove to the save actor itself. The diff moved into a free function, write_back_chunk, which keeps the existing behaviour of stamping the diff with the chunk's own worldgen version. A chunk with load_failed is still skipped before dispatch.

Because the write now happens after reconcile returns, a fast evict and re-request could read the old record. I added a guard:

  • ServerWorld has a pending_write_backs set and a write_back_rx channel. A worker reports the position on that channel only after its SaveRequest has been sent, so the actor's in-order handling places the write before any later read.
  • reconcile drains write_back_rx first. It does not dispatch a Load for a position still in the set, and dispatches it on a later pass. Deferred positions count toward StreamStats::in_flight, so a caller looping until it reaches zero still sees every wanted chunk resident.
  • StreamStats gained write_backs_in_flight, ServerWorld gained write_backs_in_flight(), and streaming_idle() now also requires an empty pending set.

shutdown evicts everything and then closes the job channel, so the workers finish every queued write-back before they are joined and before the final flush. It now returns SaveError::Invalid if a worker panicked, since a write-back may have been lost. flush does not wait for the pool, so the autosave cannot stall the tick behind queued work. A write-back still on a worker lands after that flush and is covered by the next one or by shutdown.

I also removed the current_version field from ServerWorld, which nothing reads any more. Test-only Job::Panic and Job::Park hold the pool back or kill a worker.

Docs: docs/save_format.md ("Concurrency and the save actor") and docs/chunk_streaming.md (the drain, evict and dispatch phases) describe where the diff runs and the guard.

Why is this change necessary?

An eviction pass regenerated or fetched the baseline and ran ChunkData::from_diff on the tick thread for every evicted chunk. Crossing a chunk boundary evicts a whole edge slab at once, so that cost landed on a single tick. Chunk loads and delivery encodes already run on the worker pool, and the eviction diff was the remaining heavy step on the tick thread. This is a performance change and not a correctness fix.

Scope of Changes

server, workspace

Testing

Automated, run from this worktree. renderer and client cannot build on this machine because ninja is missing, so every command excludes them (--exclude renderer --exclude client):

  • cargo fmt --all -- --check: clean.
  • cargo clippy --all-targets --all-features --workspace --exclude renderer --exclude client -- -D warnings: clean.
  • cargo test -p server: 113 passed, 0 failed.
  • cargo test --workspace --exclude renderer --exclude client --all-features --profile ci: all suites passed (server 113, plus 48, 25 and 360 in the other crates).

New tests in crates/server/src/tests/world_server.rs:

  • a_chunk_desired_again_waits_for_its_write_back: evicts an edited chunk with the pool parked, desires it again on the next pass, and checks the load is held back and the reloaded chunk holds the edit.
  • shutdown_lands_every_pending_write_back_before_the_final_flush
  • a_panicked_worker_is_reported_by_shutdown_and_never_stalls_flush
  • flush_does_not_wait_for_parked_write_backs

The existing flush test helper now takes &mut ServerWorld and drains the pool before it flushes. No existing test body changed.

Tick time on an eviction-heavy pass:

mean tick (ms) max tick (ms)
before (feat/entity-chunk-lifecycle) TODO: maintainer TODO: maintainer
after (this branch) TODO: maintainer TODO: maintainer

TODO: maintainer. Fill in the pass used (for example the radius-16 walk) and the machine. tick_stats.rs records the tick body per window but the existing tests do not drive an eviction pass through it, so I did not take this measurement.

Manual check, not yet done:

  • Place or break a block, walk out of radius and straight back in, and confirm the edit is still there.
  • Stop the server with SIGINT and confirm the edit survives a restart.

Additional Context

Stacks on #24 (feat/entity-chunk-lifecycle); review that one first.

This PR stacks on the open pull request for feat/entity-chunk-lifecycle, not on dev. The diff against that branch is what is described here, and it will need a retarget to dev once that pull request merges. It builds on the loaded and evicted reporting in StreamStats from that branch.

Entity records are still written from the tick thread on eviction, which keeps their ordering trivially safe. The ChunkCache is still keyed by position alone; the existing note about that moved with the code into write_back_chunk.

The one visible behaviour change is that a chunk desired again right after eviction loads one or more passes later than before, while its write-back is pending.

None.

Checklist

  • I have branched from dev (or a feature branch off dev) and my PR targets dev. (Branched from the feature branch feat/entity-chunk-lifecycle, which is itself off dev; the PR targets that branch until it merges.)
  • I have kept my changes focused to a single concept.
  • I have added or updated documentation (/// doc comments for Rust) where necessary.
  • I have tested my changes and described any relevant automated or manual testing above. (Automated checks passed; the tick measurement and the manual check are still open, so leave unticked until they are filled in.)
### What does this PR do? Eviction in `ServerWorld::reconcile` no longer diffs the chunk on the tick thread. I added a `Job::WriteBack { pos, chunk, worldgen_version }`. `reconcile` hands the evicted chunk to the pool by value, and the worker resolves the baseline through the shared `ChunkCache`, builds the `ChunkData::from_diff`, and sends `SaveRequest::Write` or `Remove` to the save actor itself. The diff moved into a free function, `write_back_chunk`, which keeps the existing behaviour of stamping the diff with the chunk's own worldgen version. A chunk with `load_failed` is still skipped before dispatch. Because the write now happens after `reconcile` returns, a fast evict and re-request could read the old record. I added a guard: - `ServerWorld` has a `pending_write_backs` set and a `write_back_rx` channel. A worker reports the position on that channel only after its `SaveRequest` has been sent, so the actor's in-order handling places the write before any later read. - `reconcile` drains `write_back_rx` first. It does not dispatch a `Load` for a position still in the set, and dispatches it on a later pass. Deferred positions count toward `StreamStats::in_flight`, so a caller looping until it reaches zero still sees every wanted chunk resident. - `StreamStats` gained `write_backs_in_flight`, `ServerWorld` gained `write_backs_in_flight()`, and `streaming_idle()` now also requires an empty pending set. `shutdown` evicts everything and then closes the job channel, so the workers finish every queued write-back before they are joined and before the final flush. It now returns `SaveError::Invalid` if a worker panicked, since a write-back may have been lost. `flush` does not wait for the pool, so the autosave cannot stall the tick behind queued work. A write-back still on a worker lands after that flush and is covered by the next one or by `shutdown`. I also removed the `current_version` field from `ServerWorld`, which nothing reads any more. Test-only `Job::Panic` and `Job::Park` hold the pool back or kill a worker. Docs: `docs/save_format.md` ("Concurrency and the save actor") and `docs/chunk_streaming.md` (the drain, evict and dispatch phases) describe where the diff runs and the guard. ### Why is this change necessary? An eviction pass regenerated or fetched the baseline and ran `ChunkData::from_diff` on the tick thread for every evicted chunk. Crossing a chunk boundary evicts a whole edge slab at once, so that cost landed on a single tick. Chunk loads and delivery encodes already run on the worker pool, and the eviction diff was the remaining heavy step on the tick thread. This is a performance change and not a correctness fix. ### Scope of Changes server, workspace ### Testing Automated, run from this worktree. `renderer` and `client` cannot build on this machine because `ninja` is missing, so every command excludes them (`--exclude renderer --exclude client`): - `cargo fmt --all -- --check`: clean. - `cargo clippy --all-targets --all-features --workspace --exclude renderer --exclude client -- -D warnings`: clean. - `cargo test -p server`: 113 passed, 0 failed. - `cargo test --workspace --exclude renderer --exclude client --all-features --profile ci`: all suites passed (server 113, plus 48, 25 and 360 in the other crates). New tests in `crates/server/src/tests/world_server.rs`: - `a_chunk_desired_again_waits_for_its_write_back`: evicts an edited chunk with the pool parked, desires it again on the next pass, and checks the load is held back and the reloaded chunk holds the edit. - `shutdown_lands_every_pending_write_back_before_the_final_flush` - `a_panicked_worker_is_reported_by_shutdown_and_never_stalls_flush` - `flush_does_not_wait_for_parked_write_backs` The existing `flush` test helper now takes `&mut ServerWorld` and drains the pool before it flushes. No existing test body changed. Tick time on an eviction-heavy pass: | | mean tick (ms) | max tick (ms) | | --- | --- | --- | | before (`feat/entity-chunk-lifecycle`) | TODO: maintainer | TODO: maintainer | | after (this branch) | TODO: maintainer | TODO: maintainer | TODO: maintainer. Fill in the pass used (for example the radius-16 walk) and the machine. `tick_stats.rs` records the tick body per window but the existing tests do not drive an eviction pass through it, so I did not take this measurement. Manual check, not yet done: - [ ] Place or break a block, walk out of radius and straight back in, and confirm the edit is still there. - [ ] Stop the server with SIGINT and confirm the edit survives a restart. ### Additional Context Stacks on #24 (feat/entity-chunk-lifecycle); review that one first. This PR stacks on the open pull request for `feat/entity-chunk-lifecycle`, not on `dev`. The diff against that branch is what is described here, and it will need a retarget to `dev` once that pull request merges. It builds on the loaded and evicted reporting in `StreamStats` from that branch. Entity records are still written from the tick thread on eviction, which keeps their ordering trivially safe. The `ChunkCache` is still keyed by position alone; the existing note about that moved with the code into `write_back_chunk`. The one visible behaviour change is that a chunk desired again right after eviction loads one or more passes later than before, while its write-back is pending. ### Related Issues None. ### Checklist - [x] I have branched from `dev` (or a feature branch off `dev`) and my PR targets `dev`. (Branched from the feature branch `feat/entity-chunk-lifecycle`, which is itself off `dev`; the PR targets that branch until it merges.) - [x] I have kept my changes focused to a single concept. - [x] I have added or updated documentation (`///` doc comments for Rust) where necessary. - [ ] I have tested my changes and described any relevant automated or manual testing above. (Automated checks passed; the tick measurement and the manual check are still open, so leave unticked until they are filled in.)
The autosave flush no longer blocks the tick thread behind the worker backlog. Write-backs still on a worker reach the actor after it and are covered by the next flush or by shutdown. Shutdown now reports a panicked worker instead of returning success.
docs(workspace): describe the deferred reload and the flush contract
All checks were successful
Auto Labeler / label-scope (pull_request_target) Successful in 2s
CLA Signed All authors have signed the CLA.
CLA Check / cla-check (pull_request_target) Successful in 2s
6cb7cedb96
All checks were successful
Auto Labeler / label-scope (pull_request_target) Successful in 2s
CLA Signed All authors have signed the CLA.
CLA Check / cla-check (pull_request_target) Successful in 2s
This pull request is marked as a work in progress.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin feat/async-write-back:feat/async-write-back
git switch feat/async-write-back

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch feat/entity-chunk-lifecycle
git merge --no-ff feat/async-write-back
git switch feat/async-write-back
git rebase feat/entity-chunk-lifecycle
git switch feat/entity-chunk-lifecycle
git merge --ff-only feat/async-write-back
git switch feat/async-write-back
git rebase feat/entity-chunk-lifecycle
git switch feat/entity-chunk-lifecycle
git merge --no-ff feat/async-write-back
git switch feat/entity-chunk-lifecycle
git merge --squash feat/async-write-back
git switch feat/entity-chunk-lifecycle
git merge --ff-only feat/async-write-back
git switch feat/entity-chunk-lifecycle
git merge feat/async-write-back
git push origin feat/entity-chunk-lifecycle
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Synvael/synvael!28
No description provided.