WIP: feat(server): load, save and unload entities with their chunk #24

Draft
Serkyo wants to merge 7 commits from feat/entity-chunk-lifecycle into dev
Owner

What does this PR do?

I gave the entity region store a caller. Persistent non-player entities now load, save and unload with the chunk they stand in.

  • region_actor.rs: the save actor also owns the entity region files under entities/, in a map separate from the chunk regions. It has entity read and write requests (a write with an empty list removes the record), and Flush flushes entity regions as well as chunk regions. Entity requests share the actor's queue with chunk requests, so a record written on eviction is read back by that chunk's next load.
  • entity_file.rs: load rejects a record holding an entity outside its own chunk, and a new merge adds entities to the stored record of a chunk whose record was not read, replacing by id and keeping every other stored entity. build_record stays the only place a record is built, so players are never included. The dead_code expectation is gone.
  • world_server.rs: a Load job also reads the chunk's entity record and returns it with the chunk. reconcile now reports the chunks it loaded (with their entity lists) and the chunks it evicted through StreamStats. A record that fails to decode is logged, the chunk loads with no entities, and its stored record is left alone on eviction.
  • New entity_store.rs: EntityStore spawns loaded entities with PersistentId, EntityKind and Position, writes a chunk's record through build_record and despawns its entities on eviction, and tracks which chunks have a non-empty stored record so that a chunk whose last entity left gets its record removed. autosave_entities writes every resident chunk whose entity set or positions changed, and save_all_entities writes everything on stop. Each write reports its outcome back, and a failed write is retried on the next pass.
  • main.rs: wiring only. stream_chunks hands its stats to the store, sync_entities runs after it in the schedule, the autosave block calls autosave_entities before the flush, and shutdown calls save_all_entities before the final flush.
  • save.rs module doc now describes the incremental flush as the common path and the whole-file rewrite as the fallback.
  • docs/save_format.md describes entity regions on the save actor, the entity read in the load pipeline, and when records are written.

Why is this change necessary?

The entity region store and the persistent entity id already existed, but nothing called them, so an entity that was in a chunk when the chunk left memory would have been lost. A chunk that holds a persistent entity also has to be written back whatever its voxel diff, and a chunk whose last entity left has to have its record removed, otherwise the departed entity comes back on the next load. Entity regions live on the save actor so their reads and writes are ordered with the chunk requests on the same thread. No non-player entity kind exists yet, so the round trip is covered by tests that spawn a test entity carrying PersistentId, EntityKind and Position.

Scope of Changes

server, workspace

Testing

Automated, all run on this branch:

  • cargo fmt --all -- --check: clean.
  • cargo clippy --all-targets --all-features -- -D warnings: clean.
  • cargo test -p server: 109 passed, 0 failed.
  • cargo test --workspace --all-features --profile ci: all suites passed, 0 failed.
  • cargo deny check: advisories, bans, licenses and sources ok.

The new tests in tests/entity_store.rs and tests/region_actor.rs cover: an entity evicted and reloaded keeps its EntityId, kind and position and is despawned in between; a chunk holding only a player gets no entity record; moving the only entity out of a chunk removes the old chunk's record; a chunk whose voxels equal the baseline but which holds an entity still gets its record written; an entity that moved before an autosave is found at its new position when the entity region is read from disk; and failed writes are retried and reported by the flush.

Manual: not run. Maintainer to run it: start the server, join, walk out of the view radius and back, then quit. Expect no entity-record errors in the server log and an empty entities/ directory in the world, since no non-player entity exists and players never land there.

Additional Context

  • Cross-region write ordering and load deduplication by id are left to a follow-up. Until then a chunk load skips spawning an entity whose id is already live. An entity that moves to a chunk that is not resident is merged by id into that chunk's stored record, and the source record is not guaranteed to drop it only after the destination write succeeded.
  • An eviction write that fails is unrecoverable, because the entities were already despawned. It is kept in a set that nothing clears, so it fails every later flush for the life of the actor, including the final one on stop. A failed write of live entities is instead retried and cleared by the next successful write of that chunk.
  • A record whose entity stands outside its own chunk is treated as unreadable: its chunk loads with no entities and the stored record is left untouched.
  • SYNE and SYNR keep their versions and no migrator is added.
  • The branch touches crates/server and one doc, and is a single concept in seven commits.
  • Running the net tests in parallel on dev can fail intermittently because they share a trust store. They passed here.

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 gave the entity region store a caller. Persistent non-player entities now load, save and unload with the chunk they stand in. - `region_actor.rs`: the save actor also owns the entity region files under `entities/`, in a map separate from the chunk regions. It has entity read and write requests (a write with an empty list removes the record), and `Flush` flushes entity regions as well as chunk regions. Entity requests share the actor's queue with chunk requests, so a record written on eviction is read back by that chunk's next load. - `entity_file.rs`: `load` rejects a record holding an entity outside its own chunk, and a new `merge` adds entities to the stored record of a chunk whose record was not read, replacing by id and keeping every other stored entity. `build_record` stays the only place a record is built, so players are never included. The `dead_code` expectation is gone. - `world_server.rs`: a `Load` job also reads the chunk's entity record and returns it with the chunk. `reconcile` now reports the chunks it loaded (with their entity lists) and the chunks it evicted through `StreamStats`. A record that fails to decode is logged, the chunk loads with no entities, and its stored record is left alone on eviction. - New `entity_store.rs`: `EntityStore` spawns loaded entities with `PersistentId`, `EntityKind` and `Position`, writes a chunk's record through `build_record` and despawns its entities on eviction, and tracks which chunks have a non-empty stored record so that a chunk whose last entity left gets its record removed. `autosave_entities` writes every resident chunk whose entity set or positions changed, and `save_all_entities` writes everything on stop. Each write reports its outcome back, and a failed write is retried on the next pass. - `main.rs`: wiring only. `stream_chunks` hands its stats to the store, `sync_entities` runs after it in the schedule, the autosave block calls `autosave_entities` before the flush, and shutdown calls `save_all_entities` before the final flush. - `save.rs` module doc now describes the incremental flush as the common path and the whole-file rewrite as the fallback. - `docs/save_format.md` describes entity regions on the save actor, the entity read in the load pipeline, and when records are written. ### Why is this change necessary? The entity region store and the persistent entity id already existed, but nothing called them, so an entity that was in a chunk when the chunk left memory would have been lost. A chunk that holds a persistent entity also has to be written back whatever its voxel diff, and a chunk whose last entity left has to have its record removed, otherwise the departed entity comes back on the next load. Entity regions live on the save actor so their reads and writes are ordered with the chunk requests on the same thread. No non-player entity kind exists yet, so the round trip is covered by tests that spawn a test entity carrying `PersistentId`, `EntityKind` and `Position`. ### Scope of Changes server, workspace ### Testing Automated, all run on this branch: - `cargo fmt --all -- --check`: clean. - `cargo clippy --all-targets --all-features -- -D warnings`: clean. - `cargo test -p server`: 109 passed, 0 failed. - `cargo test --workspace --all-features --profile ci`: all suites passed, 0 failed. - `cargo deny check`: advisories, bans, licenses and sources ok. The new tests in `tests/entity_store.rs` and `tests/region_actor.rs` cover: an entity evicted and reloaded keeps its `EntityId`, kind and position and is despawned in between; a chunk holding only a player gets no entity record; moving the only entity out of a chunk removes the old chunk's record; a chunk whose voxels equal the baseline but which holds an entity still gets its record written; an entity that moved before an autosave is found at its new position when the entity region is read from disk; and failed writes are retried and reported by the flush. Manual: not run. Maintainer to run it: start the server, join, walk out of the view radius and back, then quit. Expect no entity-record errors in the server log and an empty `entities/` directory in the world, since no non-player entity exists and players never land there. ### Additional Context - Cross-region write ordering and load deduplication by id are left to a follow-up. Until then a chunk load skips spawning an entity whose id is already live. An entity that moves to a chunk that is not resident is merged by id into that chunk's stored record, and the source record is not guaranteed to drop it only after the destination write succeeded. - An eviction write that fails is unrecoverable, because the entities were already despawned. It is kept in a set that nothing clears, so it fails every later flush for the life of the actor, including the final one on stop. A failed write of live entities is instead retried and cleared by the next successful write of that chunk. - A record whose entity stands outside its own chunk is treated as unreadable: its chunk loads with no entities and the stored record is left untouched. - `SYNE` and `SYNR` keep their versions and no migrator is added. - The branch touches `crates/server` and one doc, and is a single concept in seven commits. - Running the `net` tests in parallel on `dev` can fail intermittently because they share a trust store. They passed here. ### 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. - [ ] I have tested my changes and described any relevant automated or manual testing above.
fix(server): never clear a lost eviction write from the flush report
All checks were successful
Auto Labeler / label-scope (pull_request_target) Successful in 3s
CLA Signed All authors have signed the CLA.
CLA Check / cla-check (pull_request_target) Successful in 2s
CI / Rust Check, Lint & Test (pull_request) Successful in 37m39s
CI / Dependency Licenses & Advisories (pull_request) Successful in 29s
CI / Lua Lint & Format (pull_request) Successful in 7s
CI / Commit Message Lint (pull_request) Successful in 2s
CI / LFS Pointer Guard (pull_request) Successful in 6s
9763524a7c
All checks were successful
Auto Labeler / label-scope (pull_request_target) Successful in 3s
CLA Signed All authors have signed the CLA.
Required
CLA Check / cla-check (pull_request_target) Successful in 2s
CI / Rust Check, Lint & Test (pull_request) Successful in 37m39s
Required
Details
CI / Dependency Licenses & Advisories (pull_request) Successful in 29s
CI / Lua Lint & Format (pull_request) Successful in 7s
Required
Details
CI / Commit Message Lint (pull_request) Successful in 2s
CI / LFS Pointer Guard (pull_request) Successful in 6s
Required
Details
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/entity-chunk-lifecycle:feat/entity-chunk-lifecycle
git switch 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!24
No description provided.