Repository navigation
mmap_cache: fix roster-divergence cascade + add put_many bulk path - #70704
Merged
dwoz merged 1 commit intoNov 6, 2026
Merged
Conversation
**Correctness: roster_version header counter + inode-aware wfd** At 10K+ concurrent put() calls through the flock, _roster_recover fired dozens of "Roster/index divergence detected ... rebuilding roster" warnings per _register_resources call. Each rebuild was an O(hwm) scan held under the write flock, cascading when multiple MWorkers each ran their own rebuild and blocking all register handlers. Two root causes: 1. _roster_wfd pointed at the unlinked inode after atomic_rebuild / _roster_recover ran os.replace() on the roster file. Sibling processes kept writing through their stale append-mode fd into a deleted file; their appends never reached the live roster, so the next observer saw len(roster) < header.occupied and fired a rebuild, which replaced the file again, and so on. 2. _roster_read keyed its in-memory cache on st_mtime_ns. On filesystems with coarse mtime resolution (ext3, HFS+, Windows FAT, some NFS, Docker-on-Mac bind mounts), two roster writes inside a single mtime tick produced identical keys and readers saw a stale cached list. Fix: add a monotonic roster_version uint64 at header offset 25 (previously reserved/zero), bumped under the flock on every roster mutation (_roster_append, _roster_remove, _roster_recover rebuild path, atomic_rebuild). _roster_read now caches (list, version) and compares the mmap-resident value — no stat, no mtime race. _maybe_recover_roster gates the per-open recover call on the version it last stamped into _recovered_for, so steady-state writes don't re-trigger the O(hwm) scan. _roster_wfd_open stats the live path on every call and reopens the wfd against the current inode when replaces happen. Backward-compat: the new header field is additive. Old files have 0x00..00 at bytes 25-32 (previously reserved and already zero-filled on fresh writes), interpreted as "version 0, never advanced"; old binaries reading new files ignore bytes 25+. _HDR_MIN_SLOT_SIZE bumped 25 -> 33; all in-tree callers pass slot_size >= 64. **Performance: put_many with batched flush** MmapCache.put_many(items) acquires the flock once, enters _batch_mode (so inner put calls skip flock reacquisition and per-entry heap fsync / index msync), tracks touched heap segments, and issues one fsync per touched segment + one _flush_index_mm at commit time. _lock(), _flush_index_mm, and _append_to_heap all short-circuit on _batch_mode. _roster_append's per-append _fsync_fd_maybe is dropped unconditionally — the roster is a derived index recoverable via _roster_recover, so wfd.flush() to the OS page cache is sufficient for cross-process visibility. Measured: single-process 10K bulk put jumped from ~144/s (after the roster-fsync drop) to ~18,653/s via put_many (72x). 100K reached ~86K/s. _ResourceIndexStore.put_many now routes the whole batch through MmapCache.put_many when the primary exposes it (keeps _invalidate_version_cache / _touch_primary_mtime after a successful bulk put), with a looped-put fallback for backends without it. Note: the one-paragraph _LocalFsPrimary / _RESOURCE_INDEX_BACKEND experimental escape-hatch referenced in the design notes was never landed on 3008.x; no code removal needed.
twangboy
approved these changes
Nov 6, 2026
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
put()fixed via a monotonicroster_versionheader counter (bumped under the write flock on every roster mutation) and an inode-aware_roster_wfdthat reopens itself whenever_roster_recover/atomic_rebuildreplaces the roster file. Mtime-based cache invalidation is retired — it was unsafe on filesystems with coarse mtime resolution._maybe_recover_rostergates the O(hwm) recover scan on the last reconciled version so steady-state writes don't retrigger it.MmapCache.put_manyholds the flock once, defers heap fsyncs + index msync to a single batched commit, and drops the roster-append fsync (roster is a derived index — recoverable at open time). Measured 72x speedup on single-process 10K bulk put (~144/s -> ~18.6 K/s); 100K reaches ~86 K/s._ResourceIndexStore.put_manyroutes bulk inserts through it with a looped-put fallback.roster_versionuint64 lives at a previously-reserved zero-filled region, so existing on-disk files are compatible and old binaries ignore the added bytes.Test plan
tests/pytests/unit/utils/test_mmap_cache.py(32 passed)tests/pytests/unit/utils/test_mmap_cache_errors.py,test_mmap_cache_enterprise.py,test_mmap_cache_segments.py(95 passed)tests/pytests/unit/cache/test_mmap_cache.py,test_mmap_cache_errors.py,test_mmap_key.py(105 passed)tests/pytests/unit/utils/test_resource_registry.py(28 passed)tests/pytests/functional/utils/test_mmap_cache.py(20 passed — 4 new tests cover concurrentput_many, mixedput+put_many, committed-batch reader visibility, and the no-divergence-warning regression)tests/pytests/functional/cache/test_mmap_key.py,test_mmap_cache_driver.py(50 passed)