Skip to content

[Store] POSIX support read DFS replicas straight into host buffers - #5201

Merged
ykwd merged 1 commit into
kvcache-ai:mainfrom
codenaut-919:dfs_read_without_staging
Nov 9, 2026
Merged

ykwd merged 1 commit into
kvcache-ai:mainfrom
codenaut-919:dfs_read_without_staging

Conversation

@codenaut-919

@codenaut-919 codenaut-919 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

RealClient read APIs (get_into, get_into_range(s), batch_get_into, batch_get_into_multi_buffers) write an object into a caller-owned buffer. How the bytes get there depends on the type of replica that SelectBestReplica picks:

  • MEMORY / NOF replicas are read zero-copy: the slices handed to Client::Get / Client::BatchGet point at the caller's buffer and the transfer lands there directly.
  • DFS replicas (DistributedStorageBackend) were always staged: RealClient allocated a temporary buffer from client_buffer_allocator_, read the object into it, then copied it into the caller's buffer with scatter_host_to_maybe_device.

Memory and NOF reads go through the transfer engine. The transport writes each slice to exactly the address and length it is given. It also already handles device memory, either through GPUDirect or through registered buffers. Nothing between the replica and the caller's buffer requires a particular buffer shape.

DFS reads were handled together with DISK replicas as "file-backed" reads. For that whole group, a generic read path cannot assume the backend can write into arbitrary caller memory:

  • Device destinations. preadv() and the file system SDKs cannot write into GPU or NPU memory. The bytes must land in host memory first and then be copied H2D.
  • Alignment. DISK replicas (file_storage) read with O_DIRECT. That requires aligned buffers and rounds the read up past the object size.
  • Bounded scatter-gather. A single contiguous staging buffer keeps the I/O request simple no matter how the caller splits its destination (e.g. many per-layer buffers in batch_get_into_multi_buffers).

Staging was the conservative choice that works for all of these cases at once. None of the constraints above apply when the DFS backend uses the POSIX fs adapter and the destination is host memory:

  • No intermediate buffer and no alignment. PosixFsAdapter opens shard files without O_DIRECT (O_RDWR | O_CLOEXEC). ReadAt is a buffered ::preadv() that can target any host address, at any alignment.
  • The iovecs never exceed the object bytes. DistributedStorageBackend::BatchRead builds iovecs from the caller's slices and clamps each one to min(slice.size, remaining). A full read therefore covers exactly object_size bytes, and a ranged read covers exactly the requested window. The request is rejected (INVALID_PARAMS) if the slices do not cover the object, or if any slice has a null pointer with a nonzero size. The on-disk padding up to aligned_size is never written into the caller's buffer.
  • Short reads are retried. TransferAll keeps issuing preadv() until the full length has been read, and fails on zero progress.
  • The same checks still run. The direct path calls the same Client::Get / Client::BatchGet / BatchReadFileReplicaRanges entry points as the staged path. Object checksum verification (VerifyObjectChecksum, bounded to object_size and reading host pointers directly) and the lease-expiry check run exactly as before. The read is reported successful only after those checks pass.

Two cases still need staging, and this PR keeps it for them:

  • Device destinations, detected per buffer with FindDeviceForPointer.
  • More than IOV_MAX destination buffers for one key. Each buffer becomes one iovec, and preadv() rejects more than IOV_MAX iovecs with EINVAL.

Benchmark. RealClient reads of DFS-only objects into host buffers. Local NVMe, 1 GiB read per pass, 3 passes, batch 16, 8 chunks for multi_buffers, warm page cache, 0 errors. Values are GB/s, baseline → this PR.

1 thread:

value size get_into batch_get_into multi_buffers
64 KiB 1.36 → 1.57 2.82 → 3.90 2.68 → 3.81
256 KiB 3.11 → 3.44 4.00 → 5.32 3.87 → 5.28
1 MiB 3.69 → 4.78 4.22 → 5.53 4.20 → 5.79
4 MiB 3.49 → 3.82 3.03 → 4.26 2.60 → 4.18
16 MiB 2.53 → 3.02 2.54 → 3.88 2.18 → 3.94

8 threads:

value size get_into batch_get_into multi_buffers
64 KiB 7.69 → 7.92 12.9 → 15.8 6.44 → 15.3
256 KiB 12.1 → 15.9 15.0 → 19.6 14.4 → 19.4
1 MiB 12.8 → 13.3 12.9 → 17.1 12.6 → 16.7
4 MiB 10.0 → 10.9 11.1 → 14.3 10.1 → 13.6
16 MiB 4.02 → 4.81 5.60 → 6.42 5.88 → 6.39

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?

Test commands:

ninja -C build
cd build/mooncake-store/tests
./pybind_client_test
./dfs_posix_test
./object_storage_adapter_test
./dfs_sync_client_test
./real_client_direct_read_test

Test results:

  • Unit tests pass
  • Integration tests pass (if applicable)
  • Manual testing done (describe below)

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

AI Assistance Disclosure

  • No AI tools were used
  • AI tools were used (specify below)

@github-actions github-actions Bot added size/M 100-1000 changed lines (additions + deletions) run-ci Store labels Nov 3, 2026
@codenaut-919
codenaut-919 force-pushed the dfs_read_without_staging branch 2 times, most recently from 5d3aae4 to d06af54 Compare November 4, 2026 06:15
@fcczzz

fcczzz commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

The scope looks reasonable: removing RealClient staging for host reads through the current buffered POSIX adapter is a useful, self-contained optimization. Extending this to 3FS or adding O_DIRECT support can be handled separately.

One non-blocking suggestion: could we query an adapter capability instead of checking GetName() == "posix"? The capability should describe whether reads accept arbitrary writable host addresses, lengths, and file offsets without caller-provided alignment or padding. Only the current POSIX implementation needs to opt in for this PR.

This would preserve flexibility for a future O_DIRECT mode, where the adapter name stays the same but buffer requirements may change. The adapter could handle alignment internally or expose the relevant constraints. It would also let other adapters opt in later without adding backend-specific checks to RealClient.

@codenaut-919
codenaut-919 force-pushed the dfs_read_without_staging branch from d06af54 to 6856416 Compare November 6, 2026 08:30
@codenaut-919

codenaut-919 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

The scope looks reasonable: removing RealClient staging for host reads through the current buffered POSIX adapter is a useful, self-contained optimization. Extending this to 3FS or adding O_DIRECT support can be handled separately.

One non-blocking suggestion: could we query an adapter capability instead of checking GetName() == "posix"? The capability should describe whether reads accept arbitrary writable host addresses, lengths, and file offsets without caller-provided alignment or padding. Only the current POSIX implementation needs to opt in for this PR.

This would preserve flexibility for a future O_DIRECT mode, where the adapter name stays the same but buffer requirements may change. The adapter could handle alignment internally or expose the relevant constraints. It would also let other adapters opt in later without adding backend-specific checks to RealClient.

Thanks, agreed. Switching from the name check to a capability query makes sense. I've added SupportsArbitraryHostReadBuffers() to FileSystemAdapter, and only PosixFsAdapter opts in.
We have also implemented our own direct I/O mode for the POSIX adapter. It doesn't expect callers to align their buffers, so reads still accept arbitrary buffers and lengths. Internally, the adapter checks whether the buffer and size qualify for the zero-copy path; if they don't, it falls back to an internal aligned staging buffer and copies into the user buffer.
Because alignment is handled inside the adapter, the direct I/O POSIX adapter can keep reporting true, and this capability doesn't need to change.If there's no direct I/O work currently in progress, we could probably go ahead and submit our changes in next pr.

@codenaut-919
codenaut-919 force-pushed the dfs_read_without_staging branch from 6856416 to c278aea Compare November 6, 2026 09:03

@fcczzz fcczzz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, this addresses my suggestion. The capability describes the caller-facing contract, so keeping it true makes sense if the adapter handles alignment internally. An internal staging fallback is compatible with that contract and still avoids redundant staging in RealClient.

LGTM for this PR, pending successful CI.

A separate PR for the direct I/O implementation sounds good. We can review the alignment handling and fallback paths there, including unaligned buffer addresses, lengths, and file offsets.

Signed-off-by: sunbaoshan <sunbaoshan@baidu.com>
@codenaut-919
codenaut-919 force-pushed the dfs_read_without_staging branch from c278aea to b2f827d Compare November 6, 2026 12:27
@ykwd
ykwd merged commit 3701148 into kvcache-ai:main Nov 9, 2026
32 checks passed
@codenaut-919
codenaut-919 deleted the dfs_read_without_staging branch November 9, 2026 07:09
jfeng18 added a commit to jfeng18/Mooncake that referenced this pull request Nov 9, 2026
* upstream/main: (28 commits)
  [Store] Remove unreachable dead code in reserved OpLog paths (kvcache-ai#5043)
  [Store][BUGFIX]: Avoid repeated NoF cleanup for unregistered buffers (kvcache-ai#5301)
  [Store] Fence the local disk tier on the first unrecoverable disk error (kvcache-ai#4490)
  [Store] Add opt-in aligned O_DIRECT bucket writes (kvcache-ai#4820)
  [Store] posix support Read DFS replicas straight into host buffers (kvcache-ai#5201)
  [TENT] Pipeline RDMA slice preparation with worker posting (kvcache-ai#5277)
  [TE] HipTransport: submit copies asynchronously, one stream and event per device (kvcache-ai#4953)
  [Misc] Add staryxchen as a HIP transport code owner (kvcache-ai#5351)
  [Doc] nvlink_intra works over PCIe P2P, not only NVLink (kvcache-ai#5266)
  [Bugfix][TENT] Propagate io_uring staging copy failures (kvcache-ai#5335)
  [Bugfix][TENT] Make UB batch submission all-or-nothing (kvcache-ai#5274)
  Add store rs tone tests (kvcache-ai#5282)
  [EP] Validate XPU Buffer constructor arguments (kvcache-ai#5329)
  [EP] Decouple PG builds and add TCPStore bootstrap (kvcache-ai#5312)
  [Bugfix][PG] Serialize RPC socket operations and drain shutdown (kvcache-ai#5098)
  [EP] Enable the native XPU EP over RDMA (kvcache-ai#5207)
  [Store] Use the AWS default credential chain when S3 static keys are unset (kvcache-ai#5303)
  [Bugfix][Store] Register dummy query RPC handlers in mooncake_client (kvcache-ai#5200)
  [Store] Preserve variadic RPC compatible fields with NDEBUG (kvcache-ai#5269)
  [TransferEngine] Avoid false CQ timeout diagnostics after idle periods (kvcache-ai#5281)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants