Skip to content

mmap_cache: fix roster-divergence cascade + add put_many bulk path - #70704

Merged
dwoz merged 1 commit into
saltstack:3008.xfrom
dwoz:dwoz/fix/mmap-cache-roster-version-and-put-many
Nov 6, 2026
Merged

dwoz merged 1 commit into
saltstack:3008.xfrom
dwoz:dwoz/fix/mmap-cache-roster-version-and-put-many

Conversation

@dwoz

@dwoz dwoz commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Correctness: roster-divergence cascade under 10K+ concurrent put() fixed via a monotonic roster_version header counter (bumped under the write flock on every roster mutation) and an inode-aware _roster_wfd that reopens itself whenever _roster_recover / atomic_rebuild replaces the roster file. Mtime-based cache invalidation is retired — it was unsafe on filesystems with coarse mtime resolution. _maybe_recover_roster gates the O(hwm) recover scan on the last reconciled version so steady-state writes don't retrigger it.
  • Performance: new MmapCache.put_many holds 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_many routes bulk inserts through it with a looped-put fallback.
  • Header-layout change is additive: new roster_version uint64 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 concurrent put_many, mixed put+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)
  • Total 330 passed.

**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.
@dwoz
dwoz requested a review from a team as a code owner November 5, 2026 21:58
@dwoz dwoz added the test:full Run the full test suite label Nov 5, 2026
@dwoz dwoz added this to the Argon v3008.7 milestone Nov 5, 2026
@dwoz
dwoz merged commit c77d6df into saltstack:3008.x Nov 6, 2026
965 of 972 checks passed
@dwoz
dwoz deleted the dwoz/fix/mmap-cache-roster-version-and-put-many branch November 13, 2026 08:30

This branch was previously deployed

1 inactive deployment
ci — a5a9218e Deployed Nov 6, 2026 by dwoz via Build Onedir Packages / DEB (arm64) #28488
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:full Run the full test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants