feat(server): keep an entity on disk while it crosses regions #27

Open
Serkyo wants to merge 10 commits from feat/entity-region-migration into feat/entity-chunk-lifecycle
Owner

What does this PR do?

I changed EntityStore in crates/server/src/entity_store.rs so an entity that moves between chunks is never absent from disk, including when the two chunks live in different region files.

  • A save pass now writes the destination record of every moved entity first. When a move crosses a region, I flush once and only then write the source records. One flush covers the whole pass however many entities moved. Moves inside one region need no mid-pass flush.
  • A source record keeps listing an entity that stands in another chunk until the record that now holds it is accepted by the save actor and, across regions, flushed. EntityStore::held_back and EntityStore::droppable decide this, using a new per-entity Home (chunk, save stamp, whether the write succeeded).
  • A failed destination write or merge leaves the entity in its source record.
  • Stamps come from EntityStore::next_stamp, which is strictly increasing within a run even if the wall clock stalls or steps back.
  • EntityStore::spawn resolves an id that is already live, or that a newer stored copy elsewhere holds, by save stamp. The greater saved_unix_ms wins, and on a tie the live copy stays. Players count as the newest.
  • Ticks that move no entity do not wait on the save actor. Only a pass that removes an entity from a record settles pending writes.

I added 10 tests in crates/server/src/tests/entity_store.rs covering the write order, a source-first write losing the entity, flush counts, failed destination writes and merges, duplicate resolution, and stale entries in unloaded chunks. docs/save_format.md describes the write order, the duplicate resolution and the known limits below.

Why is this change necessary?

Before this, a record dropped an entity as soon as the entity stood in another chunk. If the source region reached disk and the destination had not, a crash or a failed write in between lost the entity. Across two region files there is no single write that covers both, so the order has to be destination first, flush, then source. A side effect of that order is that a crash between the two flushes can leave the entity in both records, so loading now has to resolve duplicates by id and save stamp.

Scope of Changes

server, workspace

Testing

Automated, run from the branch worktree on Windows. There is no ninja here, so workspace-wide cargo commands exclude renderer and client (they fail on shaderc-sys):

  • cargo check -p server: passes.
  • cargo test -p server: 119 passed, 0 failed. Under parallel load, save::level_file::tests::a_block_the_base_game_does_not_register_stays_an_orphan occasionally fails its log-capture assert (about 1 run in 20 to 30); it predates this branch, which does not touch it, and passes 20 of 20 alone.
  • cargo clippy --workspace --exclude renderer --exclude client --all-targets --all-features -- -D warnings: clean.
  • cargo fmt --all -- --check: clean.

No Lua changed, so selene and stylua were not run.

Manual: none. I did not run the server and move entities across regions in game.

Additional Context

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

This branch is stacked on feat/entity-chunk-lifecycle, which is not merged yet. The diff against that branch is 10 commits touching entity_store.rs, its tests and docs/save_format.md. Until that branch lands, the diff against dev also contains its commits. No save format version changes.

Known limits. The store resolves duplicates only from what it has seen in the current run, so four cases stay unresolved. They are documented in docs/save_format.md and in the EntityStore::spawn rustdoc:

  1. After a restart, a stale duplicate that is loaded and saved before the newer copy is loaded gets a fresh stamp and wins over it.
  2. Despawning the winning copy lets an older duplicate in a region that is not loaded yet come back when that region loads.
  3. The stamp is wall-clock based, so a clock set behind stamps already on disk can make a new save lose to a copy that has not been loaded yet.
  4. A live entity's unsaved changes lose to a newer stored copy that loads later, because the comparison uses the stamp of its last save.

Fixing these needs persisted deletions and a stamp floor that survives restarts, or a reconciliation scan at startup. That is left for a later change to crash recovery.

None.

Checklist

  • I have branched from dev (or a feature branch off dev) and my PR targets dev.
  • 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.
### What does this PR do? I changed `EntityStore` in `crates/server/src/entity_store.rs` so an entity that moves between chunks is never absent from disk, including when the two chunks live in different region files. - A save pass now writes the destination record of every moved entity first. When a move crosses a region, I flush once and only then write the source records. One flush covers the whole pass however many entities moved. Moves inside one region need no mid-pass flush. - A source record keeps listing an entity that stands in another chunk until the record that now holds it is accepted by the save actor and, across regions, flushed. `EntityStore::held_back` and `EntityStore::droppable` decide this, using a new per-entity `Home` (chunk, save stamp, whether the write succeeded). - A failed destination write or merge leaves the entity in its source record. - Stamps come from `EntityStore::next_stamp`, which is strictly increasing within a run even if the wall clock stalls or steps back. - `EntityStore::spawn` resolves an id that is already live, or that a newer stored copy elsewhere holds, by save stamp. The greater `saved_unix_ms` wins, and on a tie the live copy stays. Players count as the newest. - Ticks that move no entity do not wait on the save actor. Only a pass that removes an entity from a record settles pending writes. I added 10 tests in `crates/server/src/tests/entity_store.rs` covering the write order, a source-first write losing the entity, flush counts, failed destination writes and merges, duplicate resolution, and stale entries in unloaded chunks. `docs/save_format.md` describes the write order, the duplicate resolution and the known limits below. ### Why is this change necessary? Before this, a record dropped an entity as soon as the entity stood in another chunk. If the source region reached disk and the destination had not, a crash or a failed write in between lost the entity. Across two region files there is no single write that covers both, so the order has to be destination first, flush, then source. A side effect of that order is that a crash between the two flushes can leave the entity in both records, so loading now has to resolve duplicates by id and save stamp. ### Scope of Changes server, workspace ### Testing Automated, run from the branch worktree on Windows. There is no `ninja` here, so workspace-wide cargo commands exclude `renderer` and `client` (they fail on `shaderc-sys`): - `cargo check -p server`: passes. - `cargo test -p server`: 119 passed, 0 failed. Under parallel load, `save::level_file::tests::a_block_the_base_game_does_not_register_stays_an_orphan` occasionally fails its log-capture assert (about 1 run in 20 to 30); it predates this branch, which does not touch it, and passes 20 of 20 alone. - `cargo clippy --workspace --exclude renderer --exclude client --all-targets --all-features -- -D warnings`: clean. - `cargo fmt --all -- --check`: clean. No Lua changed, so `selene` and `stylua` were not run. Manual: none. I did not run the server and move entities across regions in game. ### Additional Context Stacks on #24 (feat/entity-chunk-lifecycle); review that one first. This branch is stacked on `feat/entity-chunk-lifecycle`, which is not merged yet. The diff against that branch is 10 commits touching `entity_store.rs`, its tests and `docs/save_format.md`. Until that branch lands, the diff against `dev` also contains its commits. No save format version changes. Known limits. The store resolves duplicates only from what it has seen in the current run, so four cases stay unresolved. They are documented in `docs/save_format.md` and in the `EntityStore::spawn` rustdoc: 1. After a restart, a stale duplicate that is loaded and saved before the newer copy is loaded gets a fresh stamp and wins over it. 2. Despawning the winning copy lets an older duplicate in a region that is not loaded yet come back when that region loads. 3. The stamp is wall-clock based, so a clock set behind stamps already on disk can make a new save lose to a copy that has not been loaded yet. 4. A live entity's unsaved changes lose to a newer stored copy that loads later, because the comparison uses the stamp of its last save. Fixing these needs persisted deletions and a stamp floor that survives restarts, or a reconciliation scan at startup. That is left for a later change to crash recovery. ### Related Issues None. ### Checklist - [x] I have branched from `dev` (or a feature branch off `dev`) and my PR targets `dev`. - [x] I have kept my changes focused to a single concept. - [x] I have added or updated documentation (`///` doc comments for Rust) where necessary. - [x] I have tested my changes and described any relevant automated or manual testing above.
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 can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

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/entity-region-migration
git switch feat/entity-region-migration
git rebase feat/entity-chunk-lifecycle
git switch feat/entity-chunk-lifecycle
git merge --ff-only feat/entity-region-migration
git switch feat/entity-region-migration
git rebase feat/entity-chunk-lifecycle
git switch feat/entity-chunk-lifecycle
git merge --no-ff feat/entity-region-migration
git switch feat/entity-chunk-lifecycle
git merge --squash feat/entity-region-migration
git switch feat/entity-chunk-lifecycle
git merge --ff-only feat/entity-region-migration
git switch feat/entity-chunk-lifecycle
git merge feat/entity-region-migration
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!27
No description provided.