Skip to content

[Store] Add immutable bucket allocator for DFS replicas - #3902

Closed
wp-bc wants to merge 15 commits into
kvcache-ai:mainfrom
wp-bc:dfs/immutable-bucket-allocator
Closed

wp-bc wants to merge 15 commits into
kvcache-ai:mainfrom
wp-bc:dfs/immutable-bucket-allocator

Conversation

@wp-bc

@wp-bc wp-bc commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR implements the DFS immutable Bucket allocator proposed in:

It introduces a pluggable GlobalAllocatorInterface for DFS replica allocation while preserving the existing DfsGlobalAllocator as the default shard implementation.

A new ImmutableBucketAllocator can be selected with:

MOONCAKE_DFS_ALLOCATOR_TYPE=bucket

The Bucket allocator stores objects as append-only immutable entries. Bucket files are created on demand, sealed metadata is persisted for standalone restart recovery, and physical space is reclaimed by evicting complete Buckets in LRU order.

The existing DistributedFSDescriptor wire format is unchanged. In Bucket mode, the existing shard_idx field carries the Bucket ID, preserving descriptor serialization compatibility.

Main changes

Pluggable DFS allocator

Immutable Bucket allocation

  • Create Bucket files dynamically as capacity is needed.
  • Store each key and value as one aligned, append-only entry.
  • Never overwrite or reuse an entry in place.
  • Allow a batch allocation to be packed across multiple Buckets.
  • Roll back incomplete batch reservations without publishing invalid descriptors.
  • Reclaim Bucket space when allocation is exhausted.

Metadata and recovery

  • Persist one metadata snapshot for each sealed Bucket.
  • Restore committed entries from sealed Bucket metadata after a standalone Master restart.
  • Exclude pending and tombstoned entries from recovery.
  • Remove the former append-only metadata log and rename-based publication flow.
  • Conservatively handle corrupt metadata, orphan files, interrupted eviction, and incompatible configuration.

The active Bucket is intentionally not persisted. After an abrupt restart, it is discarded, while committed entries in sealed Buckets are recovered.

Bucket-level LRU eviction

  • Track access at Bucket granularity.
  • Exclude the active Bucket and Buckets containing processing entries from eviction.
  • Validate leases and pin state before removing any replica.
  • Remove all eligible replicas from Master metadata before deleting Bucket files.
  • Abort the entire Bucket eviction if any entry is still protected.
  • Trigger reclamation both at configured watermarks and on allocation exhaustion.

Batch I/O and direct reads

  • Add packed Bucket BatchWrite.
  • Group BatchRead requests by Bucket and process independent Buckets in parallel.
  • Optionally merge adjacent entries into bounded reads.
  • Add POSIX O_DIRECT reads with aligned pooled staging buffers.
  • Fall back to buffered POSIX reads when the filesystem rejects O_DIRECT.
  • Treat short reads, zero progress, EOF before the requested range, and partial failures as errors.
  • Preserve the existing HF3FS adapter-specific read path.

Master and Client integration

  • Integrate allocator selection into Master and Client initialization.
  • Add Bucket batch reservation and asynchronous write completion.
  • Copy or stage caller-owned buffers before asynchronous writes so they can be safely reused.
  • Keep DFS replicas in PROCESSING until the write succeeds and the Master commits them.
  • Revoke failed writes so incomplete data is never published as readable.
  • Drain queued writes during Client shutdown.

Configuration and administration

The following settings are introduced or documented:

MOONCAKE_DFS_ALLOCATOR_TYPE=shard|bucket
MOONCAKE_DFS_BUCKET_CAPACITY
MOONCAKE_DFS_MAX_BUCKET_COUNT
MOONCAKE_DFS_BATCH_READ_THREADS
MOONCAKE_DFS_BATCH_READ_MERGE_ENABLED
MOONCAKE_DFS_DIRECT_READ_ENABLED

The active Bucket limit can be changed online through:

PUT /api/v1/dfs/max_bucket_count
Content-Type: application/json

{"max_bucket_count": 512}

The endpoint validates the JSON type and range. Values must be in [1, INT32_MAX], and the endpoint returns a conflict response when the active allocator is not the Bucket allocator.

Lowering the limit below the current Bucket count does not delete existing Buckets immediately. It prevents additional Bucket creation until the count falls below the configured limit.

Compatibility

  • shard remains the default allocator.
  • Existing shard allocation semantics are preserved.
  • DistributedFSDescriptor serialization is unchanged.
  • This PR builds on the DFS replica support already present in the community tree; it does not reintroduce the original DFS POSIX implementation.
  • DFS prefetch, session object cache, monitoring metrics, persistent asynchronous H2D streams, Transfer Engine changes, and hardware-specific adaptations are intentionally out of scope.

Known limitations

  • Recovery covers sealed Buckets only; the active Bucket is discarded after an abrupt restart.
  • Bucket recovery is for a standalone Master reopening the same DFS root.
  • DFS remains incompatible with Master snapshot restore, oplog recovery, standby restore, and HA failover.
  • Asynchronous Bucket writes do not currently provide a persistent retry queue, request cancellation, DFS-specific timeout, or fsync durability guarantee.
  • A complete object must fit within one Bucket.

Module

  • Transfer Engine (mooncake-transfer-engine)
  • Mooncake Store (mooncake-store)
  • 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?

Validation was performed in a Linux build environment. The host contains vendor-modified RDMA headers, so an upstream-compatible verbs include path was supplied through:

CPLUS_INCLUDE_PATH=/tmp/mooncake-standard-rdma/include

Build command:

cmake --build build-no-shca --target \
  mooncake_store \
  dfs_immutable_bucket_allocator_test \
  dfs_immutable_bucket_backend_test \
  dfs_immutable_bucket_client_test \
  master_service_test \
  master_admin_server_test \
  dfs_sync_client_test

Test results:

  • dfs_immutable_bucket_allocator_test: 51/51 passed
  • dfs_immutable_bucket_backend_test: 50/50 passed
  • dfs_immutable_bucket_client_test: 7/7 passed
  • dfs_posix_test: 27/27 passed
  • Master DFS regression filter: 9/9 passed
  • Master admin DFS filter: 2/2 passed
  • dfs_sync_client_test: 7 passed, 1 checksum test skipped because MOONCAKE_STORE_CHECKSUM was disabled
  • git diff --check 2b3cefbac4d571bcb001db3aa702772b41b8c647...HEAD: passed

Coverage includes:

  • Shard allocator regression behavior
  • Bucket allocation and cross-Bucket packing
  • Metadata persistence and restart recovery
  • LRU eviction and allocation-exhaustion recovery
  • Dynamic max_bucket_count updates and validation
  • BatchWrite and BatchRead
  • POSIX direct-read alignment and staging reuse
  • Short reads and partial error propagation
  • Master and Client integration
  • Asynchronous write completion, revoke, and shutdown behavior

Not yet run in the current environment:

  • Service-backed 3FS/HF3FS integration tests
  • Documentation HTML build
  • Pre-commit hooks
  • LLVM 20 formatting check

The required 3FS/HF3FS service, Sphinx toolchain, pre-commit, and clang-format-20 are not available on the validation host.

  • Unit tests pass
  • Integration tests pass (in-process POSIX integration)
  • Manual testing done (describe below)

Checklist

  • 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: #3641

AI Assistance Disclosure

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

Codex was used to audit the internal implementation against the community baseline, identify already-upstream functionality, port and adapt the Bucket-specific changes, prepare tests and documentation, and assist with local validation.

The human submitter is responsible for reviewing, understanding, and defending every changed line before this PR is marked ready for review.

@github-actions github-actions Bot added documentation Improvements or additions to documentation run-ci Store Common labels Sep 7, 2026
@wp-bc
wp-bc force-pushed the dfs/immutable-bucket-allocator branch from 04b37c8 to 9430f51 Compare September 11, 2026 11:16
@fcczzz

fcczzz commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

I think this PR combines four substantial changes:

  1. Introducing the bucket backend, including data layout, allocation, and reclamation.
  2. Adding bucket metadata persistence and recovery.
  3. Introducing asynchronous DFS writes with deferred PutEnd/PutRevoke.
  4. Adding O_DIRECT reads and related read optimizations.

Would you be open to splitting these into separate PRs? We could start with a functional bucket backend using synchronous writes and regular reads, then follow up with separate PRs for recovery, asynchronous writes, and direct I/O optimizations.

For recovery, there is an existing RFC: #3819. Could we discuss the alternatives there and agree on which approach to adopt, particularly what the allocator should restore versus what the Master should restore?

Asynchronous writes change what a successful Put means and would benefit from a separate discussion. Ideally, this behavior should be independent of the allocator type. O_DIRECT and the other read optimizations could also be evaluated separately with correctness tests and benchmarks.

This would make each change easier to review and validate. Are there dependencies between these pieces that you think require them to land together?

@wp-bc

wp-bc commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor Author

I think this PR combines four substantial changes:

  1. Introducing the bucket backend, including data layout, allocation, and reclamation.
  2. Adding bucket metadata persistence and recovery.
  3. Introducing asynchronous DFS writes with deferred PutEnd/PutRevoke.
  4. Adding O_DIRECT reads and related read optimizations.

Would you be open to splitting these into separate PRs? We could start with a functional bucket backend using synchronous writes and regular reads, then follow up with separate PRs for recovery, asynchronous writes, and direct I/O optimizations.

For recovery, there is an existing RFC: #3819. Could we discuss the alternatives there and agree on which approach to adopt, particularly what the allocator should restore versus what the Master should restore?

Asynchronous writes change what a successful Put means and would benefit from a separate discussion. Ideally, this behavior should be independent of the allocator type. O_DIRECT and the other read optimizations could also be evaluated separately with correctness tests and benchmarks.

This would make each change easier to review and validate. Are there dependencies between these pieces that you think require them to land together?

Thanks for the detailed feedback. That makes sense, and we are happy to split this PR along the boundaries you suggested.
Our proposed sequence is:

  1. A minimal functional bucket backend: the allocator interface, bucket data layout, append-only allocation, whole-bucket reclamation and eviction, together with the minimum Master/Client integration. This version will use synchronous DFS writes and regular buffered reads.
  2. Bucket metadata persistence and restart recovery, after aligning on the recovery model in [RFC]: [Store] Persist and Recover DFS Allocator Metadata #3819.
  3. Asynchronous DFS writes and deferred completion as a separate, allocator-independent change.
  4. O_DIRECT, merged reads, parallel batch-read optimizations, and their correctness tests and benchmarks as a separate performance PR.

Regarding #3819, is anyone currently implementing it? If so, could you share the current progress or an implementation branch? We would be glad to align the bucket recovery work with it and avoid introducing a competing recovery model.
On asynchronous writes, the current code keeps the DFS replica in PROCESSING state while the background write is running. It calls PutEnd(DFS) only after BatchWrite succeeds, and calls PutRevoke(DFS) if the write fails, so an incomplete DFS replica is not exposed as readable.

However, you are right that the caller-visible semantics still change: BatchPut may return success after the synchronous replica has completed but before the deferred DFS write and its finalization have completed. Therefore, a successful return no longer necessarily means that all requested replica writes have finished. We will remove this behavior from the initial bucket PR and discuss it separately as an allocator-independent feature, including whether it should use an explicit async API/configuration and how background failures, retries, cancellation, and shutdown should be reported.

We will prepare the first reduced PR around the synchronous, buffered-I/O bucket backend and follow up with the other pieces separately. Thanks again for helping us make the review scope clearer.

@fcczzz

fcczzz commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

@wp-bc Thanks, the proposed split sounds good.

Regarding #3819, yes—we have an implementation in progress in draft PR #4018. It covers shard allocator metadata persistence and reconciliation with the Master snapshot.

The bucket backend may need a different persistence mechanism from the shard allocator's checkpoint/WAL. Let's discuss the alternatives in #3819 and agree on the recovery semantics, particularly how to handle entries present on only one side.

@wp-bc

wp-bc commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor Author

@wp-bc Thanks, the proposed split sounds good.

Regarding #3819, yes—we have an implementation in progress in draft PR #4018. It covers shard allocator metadata persistence and reconciliation with the Master snapshot.

The bucket backend may need a different persistence mechanism from the shard allocator's checkpoint/WAL. Let's discuss the alternatives in #3819 and agree on the recovery semantics, particularly how to handle entries present on only one side.

Following the submission plan we discussed, I’ve opened PR #4124 covering the basic Bucket functionality. When you have a chance, could you please help review it?

@fcczzz

fcczzz commented Sep 16, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for opening PR #4124! I’ll take a look as soon as I can. Since the split is now tracked in #4124, could you close this PR and link the follow-ups?

@wp-bc wp-bc closed this Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Common documentation Improvements or additions to documentation run-ci Store

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants