Skip to content

[Store] Opt-in reuse of foreground SSD read payload for promotion - #5944

Open
DHX98 wants to merge 1 commit into
kvcache-ai:mainfrom
DHX98:feat/5458-foreground-read-promotion
Open

DHX98 wants to merge 1 commit into
kvcache-ai:mainfrom
DHX98:feat/5458-foreground-read-promotion

Conversation

@DHX98

@DHX98 DHX98 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Description

Addresses #5458 Track 2 ("Use the foreground read payload for promotion") with an opt-in (default: false), bounded-memory handoff between foreground LOCAL_DISK reads and background promotion, with zero RPC changes.

Problem

When a cold LOCAL_DISK key is read in the foreground and subsequently promoted to MEMORY (e.g., via --promotion_on_hit=true or prefetch), the holder reads the exact same object from SSD twice: once in FileStorage::BatchGet / BatchGetLocal for the foreground reader, and a second time in FileStorage::PromoteOneKeyFromLocalDisk.

Design

  • Opt-in & bounded:
    • FileStorageConfig::enable_foreground_read_promotion_reuse (MOONCAKE_OFFLOAD_REUSE_FOREGROUND_READ_FOR_PROMOTION, default false).
    • FileStorageConfig::promotion_reuse_max_bytes (MOONCAKE_OFFLOAD_PROMOTION_REUSE_MAX_BYTES, default 64 MiB) bounds total staging bytes retained in the FIFO handoff map; AllocateBatch also evicts idle handoff entries before returning BUFFER_OVERFLOW.
  • Transfer-safety & scope:
    • AllocatedBatch::registered_for_transfer tracks whether staging memory came from the TE-registered client_buffer_allocator_ (vs. an unregistered pinned restore arena); only TE-registered whole-object BatchGet / BatchGetLocal reads publish into the handoff map. Partial BatchGetRanges never publish.
    • PromoteOneKeyFromLocalDisk() consumes matching entries (re-verifying GetObjectDataSize() + epoch) and holds std::shared_ptr<AllocatedBatch> until PromotionWrite completes (release-by-completion, independent of foreground alias release).
    • Failure isolation: promotion failures never affect foreground reads; misses fall back to LoadBatchShared.
  • Mutation & shutdown invalidation:
    • Epoch-stamped entries are invalidated on RemoveAll, UnmapKeys, and RemoveDeadBuckets.
    • BatchOffload invalidates only written/overwritten keys (plus full invalidation if eviction_handler evicts keys), and RunDiskWatermarkEviction invalidates only when eviction actually removes keys (avoiding no-op per-heartbeat cache wipes).
    • Shutdown() drains the handoff map before unregistering the staging arena.
  • Metrics: exports mooncake_ssd_promotion_payload_reused{_bytes}_total in SsdMetric.

Suggested Merge Priority

  • P0 — Urgent: Critical security vulnerability, data loss/corruption, or widespread production outage requiring immediate action
  • P1 — High: Significant correctness or stability issue in existing functionality, or a regression blocking users without a practical workaround
  • P2 — Normal: Routine bug fix, validated performance/compatibility improvement, or new feature for a confirmed use case
  • P3 — Low: Experimental feature, optional enhancement, refactoring, or cleanup with no urgent user impact

Module

  • Transfer Engine (mooncake-transfer-engine)
  • Mooncake Store (mooncake-store)
  • Mooncake Conductor (mooncake-conductor)
  • Reshard (mooncake-reshard)
  • Mooncake EP (mooncake-ep)
  • Mooncake PG (mooncake-pg)
  • Integration (mooncake-integration)
  • P2P Store (mooncake-p2p-store)
  • Python Wheel (mooncake-wheel)
  • Common (mooncake-common)
  • Mooncake RL (mooncake-rl)
  • CI/CD
  • Docs
  • Other

Type of Change

  • Bug fix
  • New feature
  • Refactor
  • Breaking change
  • Documentation update
  • Performance improvement
  • Other

How Has This Been Tested?

1. Unit Tests (CPU/TCP + -DUSE_ASCEND_DIRECT=ON)

  • file_storage_promotion_test — added 6 tests covering whole-object BatchGet reuse with mid-write foreground alias release, feature-off control, BatchGetRanges non-publication, source replacement invalidation + failure isolation, BatchGetLocal reuse, and no-op watermark check preservation — ALL PASS
  • file_storage_config_test, ssd_metrics_test, prefetch_teardown_test — ALL PASS

2. Matched Alternating A/B Benchmark on Physical Enterprise NVMe

Host: aarch64, 4× 3.5T Enterprise NVMe (HWE72P453T8L007N), live mooncake_master (--promotion_on_hit=true --promotion_admission_threshold=2 --promotion_max_per_heartbeat=64) + mooncake_client (MOONCAKE_OFFLOAD_PROMOTION_REUSE_MAX_BYTES=128 MiB).
Workload: 3 alternating matched pairs (OFF -> ON × 3) per I/O mode; each run seeds 100 × 2 MiB, evicts 40 × 2 MiB = 80 MiB cold LOCAL_DISK-only keys, runs sync && drop_caches=3, reads & promotes all 40 keys, and verifies 40/40 foreground + 40/40 post-promotion byte-exact equality.

Mode A — Default Buffered I/O (mean of 3 alternating pairs)

Metric Baseline (OFF) Reuse (ON) Delta
Mooncake ssd_read_ops_total 80.0 (80/80/80) 42.0 (46/40/40) -47.5% (up to -50.0%)
Mooncake ssd_read_bytes_total 160.0 MiB 84.0 MiB (92/80/80) -47.5%
ssd_promotion_payload_reused 0 / 40 38.0 / 40 (95%; R2/R3 100%) +76.0 MiB reused
Linux /proc/<pid>/io rchar 160.0 MiB 84.0 MiB -47.5%
Linux /proc/<pid>/io syscr 124.3 86.7 -30.3%
Foreground get() p50 / p95 1.49 ms / 2.52 ms 1.47 ms / 2.33 ms -1.3% / -7.5%
End-to-end until 40/40 promoted 721.93 ms 530.33 ms -26.5%

Mode B — MOONCAKE_OFFLOAD_USE_URING=true (io_uring + O_DIRECT, bypassing OS page cache; mean of 3 alternating pairs)

Metric Baseline (OFF) Reuse (ON) Delta
Linux /proc/<pid>/io read_bytes (NVMe DMA) 162.68 MiB (all 3 runs) 98.55 MiB (R2: 82.52 MiB) -39.42% (R2: -49.3%)
Mooncake ssd_read_ops_total 80.0 (80/80/80) 48.0 (56/40/48) -40.0% (R2: -50.0%)
Mooncake ssd_read_bytes_total 160.0 MiB 96.0 MiB (R2: 80.0 MiB) -40.0%
ssd_promotion_payload_reused 0 / 40 32.0 / 40 (R1:24 / R2:40 / R3:32) 80% mean / 100% peak
Foreground get() p50 / p99 1.35 ms / 24.97 ms 1.36 ms / 16.96 ms p99 -32.1%

(All 12 runs achieved 40/40 promoted and 100% byte-exact verification.)

Test commands:

ctest --test-dir build --output-on-failure -R "^(file_storage_promotion_test|file_storage_config_test|ssd_metrics_test|prefetch_teardown_test)$"
./scripts/code_format.sh --check --changed-lines --base origin/main

Test results:

  • Unit tests pass
  • Integration tests pass (if applicable)
  • Manual testing done (matched A/B benchmark on physical enterprise NVMe in both buffered and io_uring + O_DIRECT modes)

Checklist

  • Every commit I contribute includes my Signed-off-by trailer certifying the DCO
  • I have performed a self-review of my own code
  • I have formatted my code using ./scripts/code_format.sh
  • I have run pre-commit on the files changed in this PR and all hooks pass
  • I have updated the documentation (if applicable)
  • I have added tests to prove my changes are effective
  • For changes >500 LOC: I have filed an RFC issue ([RFC]: [Store] SSD/DFS performance optimization roadmap #5458)

AI Assistance Disclosure

  • AI tools were used. The human submitter has reviewed and can defend every changed line.

…cache-ai#5458)

Implements Track 2 cold-read reuse for kvcache-ai#5458:
- Add opt-in FileStorageConfig::enable_foreground_read_promotion_reuse
  (default false, MOONCAKE_OFFLOAD_REUSE_FOREGROUND_READ_FOR_PROMOTION) and
  bounded byte cap promotion_reuse_max_bytes (default 64 MiB,
  MOONCAKE_OFFLOAD_PROMOTION_REUSE_MAX_BYTES).
- Publish whole-object foreground BatchGet / BatchGetLocal staging payloads
  (only when allocated from the TE-registered client_buffer_allocator_) into
  an epoch-stamped FIFO handoff cache; partial BatchGetRanges never publish.
- Consume matching staging payload in PromoteOneKeyFromLocalDisk (re-verifying
  object size) to skip the redundant second SSD read, holding shared_ptr
  ownership until PromotionWrite finishes.
- Use selective key invalidation on BatchOffload and evict-only invalidation
  in RunDiskWatermarkEviction so no-op heartbeat checks do not clear reuse.

Signed-off-by: dhx98 <haox.dai@gmail.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T04:11:38.365602Z 4ed6325 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions github-actions Bot added Store Common size/M 100-1000 changed lines (additions + deletions) labels Nov 29, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4ed6325aac

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1116 to +1118
ColdReadInvalidation invalidate(*this,
/*invalidate_reuse=*/false);
InvalidatePromotionReuseKeys(offload_storage_keys);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Invalidate read generations when offloading a written key

With reuse enabled, a BatchGet can finish loading an old value and pause before publication while a non-evicting kFilePerKey offload overwrites that key. Both per-key invalidations only erase existing entries and leave promotion_reuse_epoch_ unchanged, allowing the delayed reader to republish old bytes with a still-current epoch. A subsequent promotion of the same-sized replacement then reuses those bytes and commits stale data into MEMORY. Advance/check a generation for written keys as well as erasing existing entries, so pre-write reads cannot refill the cache after invalidation.

Useful? React with 👍 / 👎.

.epoch = read_epoch,
.retained_bytes = entry_bytes,
.expires_at = expires_at});
promotion_reuse_fifo_.push_back(keys[i]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Bound FIFO metadata when replacing cached keys

With reuse enabled, repeated reads of the same key replace its map entry but append another FIFO record without removing the previous one. If reads refresh the entry before its TTL and no full invalidation occurs—for example, on a restarted holder where reclamation is disabled—the byte count stays below the cap and GC never empties the map, so the deque grows indefinitely despite retaining only one payload. This can exhaust process memory independently of promotion_reuse_max_bytes. Remove or compact stale FIFO records when entries are replaced, consumed, or expired.

Useful? React with 👍 / 👎.

Comment on lines +653 to +655
const uint64_t entry_bytes = static_cast<uint64_t>(sizes[i]);
if (entry_bytes > max_bytes) {
continue;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Account for all buffers retained by each cached batch

With reuse enabled and a 64 MiB limit, a foreground batch containing a 128 MiB value and a 1-byte value skips caching the large key but still creates a 1-byte entry holding a shared_ptr to the entire AllocatedBatch. All of that batch's buffer handles therefore remain alive after the foreground alias is released, while the counter reports only one retained byte. Consuming or evicting part of a multi-key batch causes the same undercount, allowing the cache to retain far more staging memory than configured. Account for each retained batch/handle once, or retain only the individual key's allocation.

Useful? React with 👍 / 👎.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Common size/M 100-1000 changed lines (additions + deletions) Store

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant