Expand description
§Offload Module
The offload module manages the asynchronous transfer of KV cache blocks between storage tiers. It provides a pipeline-based architecture for evaluating, batching, and executing block transfers with full cancellation support.
§Overview
Offloading moves blocks from a source tier (e.g., GPU memory) to a destination tier (e.g., host memory, remote storage, or object storage). The pipeline ensures:
- Policy-based filtering: Only blocks meeting criteria are transferred
- Batched execution: Blocks are grouped for efficient transfer
- Cancellation support: Transfers can be cancelled at any point before commitment
- Precondition synchronization: Transfers wait for forward pass completion
§Pipeline Architecture
┌─────────────────┐ ┌─────────────────────┐ ┌─────────────────────┐ ┌──────────────────┐
│ PolicyEvaluator │────►│ PreconditionAwaiter │────►│ Batcher │────►│ TransferExecutor │
└─────────────────┘ └─────────────────────┘ └─────────────────────┘ └──────────────────┘
▲ ▲
│ │
CancellableQueue CancellableQueue
│ │
└──────── CancelSweeper ───┘§Stages
| Stage | Purpose |
|---|---|
| PolicyEvaluator | Filters blocks based on configured policies (frequency, presence, etc.) |
| PreconditionAwaiter | Waits for forward pass completion before proceeding |
| Batcher | Groups containers into batches based on total block count |
| TransferExecutor | Upgrades blocks and executes the actual transfer |
§Container Data Model
The fundamental unit flowing through the pipeline is an OffloadContainer:
struct OffloadContainer<T: BlockMetadata> {
/// The blocks to offload
blocks: Vec<SourceBlock<T>>,
/// Precondition event (forward pass completion)
precondition: Option<EventHandle>,
/// Cancellation token
cancel_token: CancellationToken,
}Containers are grouped into batches for efficient transfer:
struct OffloadBatch<T: BlockMetadata> {
/// Multiple containers, each independently cancellable
containers: Vec<OffloadContainer<T>>,
}§P1: Container is the Unit of Cancellation
Individual blocks within a container are not independently cancellable. When a container is cancelled, all its blocks are cancelled together.
§P2: Token Travels with Container
Each container carries its own CancellationToken, cloned from the TransferHandle at enqueue time. The token travels with the container through all pipeline stages until upgrade.
§P3: Upgrade is the Commitment Boundary
The upgrade step (Weak → Strong) is the point of no return:
- Before upgrade: Containers can be cancelled via sweep or token check
- After upgrade: We own the blocks; cancellation no longer applies
§P4: Sweep Before Upgrade
The last cancellation check occurs immediately before upgrade. The TransferExecutor calls batch.sweep_cancelled() to remove cancelled containers before committing.
§P5: Flat Map After Upgrade
After upgrade, all blocks from all containers are consolidated into a single Vec<ImmutableBlock<T>> for efficient batch transfer. Per-container identity is lost at this point.
§P6: PreconditionAwaiter Uses Select
The precondition awaiter can be cancelled via select! on both the precondition event and the cancellation token. If cancelled while waiting, the container is dropped immediately.
§Configuration
Pipeline behavior is controlled via PipelineConfig:
| Option | Default | Description |
|---|---|---|
batch_config.max_batch_size | 64 | Maximum blocks per batch |
batch_config.min_batch_size | 8 | Minimum blocks before flush |
batch_config.flush_interval | 10ms | Time before flushing partial batch |
policy_timeout | 100ms | Timeout for policy evaluation |
sweep_interval | 10ms | Interval for cancel sweeper |
max_concurrent_transfers | 1 | Concurrent transfer batches |
§Usage
§Enqueueing Blocks
let handle = pipeline.enqueue(source_blocks, precondition_event);
// Track progress
println!("Status: {:?}", handle.status());
// Wait for completion
let result = handle.wait().await?;§Cancelling a Transfer
// Request cancellation and wait for confirmation
handle.cancel().await;
// All blocks are now released§Related Documentation
- offload-developer.md - Implementation details and extension rules
Offload Engine for asynchronous block transfers between storage tiers.
The offload engine provides a policy-based, cancellable pipeline for moving blocks from higher-performance tiers (G1/G2) to lower-cost tiers (G3/G4).
§Architecture
┌─────────────────────────────────────────────────────────────────┐
│ OffloadEngine │
│ │
│ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │
│ │G1→G2 Pipeline │────│ G2→G3 Pipeline│ │ G2→G4 Pipeline│ │
│ └───────────────┘ └───────────────┘ └───────────────┘ │
│ │ │ │ │
│ └─────────auto_chain──┘ │ │
│ │
└─────────────────────────────────────────────────────────────────┘
Pipeline stages:
┌─────────────┐ ┌────────────────┐ ┌──────────────────┐
│ Policy │───▶│ Batch │───▶│ Transfer │
│ Evaluator │ │ Collector │ │ Executor │
└─────────────┘ └────────────────┘ └──────────────────┘
│ │ │
▼ ▼ ▼
cancel check cancel check wait for in-flight§Features
- Policy-based filtering: Blocks pass through configurable policies (presence checks, LFU thresholds) before transfer
- Batched transfers: Blocks are accumulated into batches for efficient bulk transfers
- Cancellation: Clean cancellation with confirmation that all blocks are released and no outstanding operations remain
- Pipeline chaining: G1→G2 completions can automatically feed G2→G3
See also: Developer Guide for implementation details and extension rules.
§Example
use kvbm::v2::distributed::offload::{
OffloadEngine, PipelineBuilder, PresenceFilter, PresenceAndLFUFilter,
};
// Build engine with pipelines
let engine = OffloadEngine::builder(leader.clone())
.with_registry(registry.clone())
.with_g2_manager(g2_manager.clone())
.with_g3_manager(g3_manager.clone())
.with_g2_to_g3_pipeline(
PipelineBuilder::<G2, G3>::new()
.policy(Arc::new(PresenceAndLFUFilter::with_default_threshold(registry.clone())))
.batch_size(64)
.build()
)
.build()?;
// Enqueue blocks for offload
let handle = engine.enqueue_g2_to_g3(blocks)?;
// Wait for completion or cancel
tokio::select! {
result = handle.wait() => {
println!("Completed: {:?}", result?.completed_blocks);
}
_ = shutdown_signal => {
handle.cancel().wait().await;
println!("Cancelled");
}
}See also: Developer Guide
Structs§
- AllOf
Policy - Composite policy that requires ALL sub-policies to pass (AND logic).
- AnyOf
Policy - Composite policy that requires ANY sub-policy to pass (OR logic).
- Batch
Config - Configuration for batch collection.
- Cancel
Confirmation - Future that resolves when cancellation is fully confirmed.
- Cancellable
Queue - A lock-free queue that supports active cancellation via sweeping.
- Cancellation
Token - Token for requesting and tracking cancellation.
- Eval
Context - Context provided to policies for block evaluation.
- External
Block - External block reference with sequence hash for registration.
- Object
Lock Presence Filter - G2→G4 filter with distributed locking: check meta, acquire lock, track acquired locks.
- Object
Pipeline - A running pipeline instance for object storage destinations.
- Object
Pipeline Builder - Builder for object pipeline configuration.
- Object
Pipeline Config - Configuration for an object storage pipeline.
- Object
Presence Filter - G2→G4 filter: async presence check for object storage destinations.
- Offload
Engine - Central coordinator for offload pipelines.
- Offload
Engine Builder - Builder for OffloadEngine.
- Pass
AllPolicy - A pass-all policy (no filtering).
- Pending
Guard - RAII guard that removes a sequence hash from the pending set on drop.
- Pending
Tracker - Tracks sequence hashes that are currently pending transfer.
- Pipeline
- A running pipeline instance.
- Pipeline
Builder - Builder for pipeline configuration.
- Pipeline
Config - Configuration for a pipeline.
- Pipeline
Failure - Terminal executor failure observed while waiting for causal settlement.
- Presence
AndLFU Filter - G2→G3 filter: presence check + LFU count threshold.
- Presence
Filter - G1→G2 filter: skip blocks already present in destination tier.
- Resolved
Batch - A batch of resolved blocks ready for transfer.
- Resolved
Block - A resolved block ready for transfer execution.
- S3Presence
Checker - S3/Object storage presence checker.
- Settlement
Target - Expected completed transfer batches for each pipeline lane.
- Settlement
Token - Opaque checkpoint captured before an external completion source is fired.
- Timing
Trace - Timing trace for tracking block progression through pipeline stages.
- Transfer
Handle - Handle for tracking and controlling an offload transfer.
- Transfer
Id - Unique identifier for a transfer operation.
- Transfer
Progress Counts - Monotonic block counts for a transfer.
- Transfer
Progress Cursor - Per-consumer cursor for incrementally reading transfer progress.
- Transfer
Progress Delta - Blocks appended since a
TransferProgressCursorwas last consumed. - Transfer
Result - Result of a completed transfer.
Enums§
- Cancel
State - State of a cancellation request.
- Pipeline
Failure Kind - Stable failure categories for pipeline settlement.
- Pipeline
Lane - An offload pipeline whose executor can participate in causal settlement.
- Settlement
Error - Error returned when a causal settlement boundary cannot be established.
- Source
Block - Represents a single block source for offloading.
- Source
Blocks - Collection of source blocks for batch operations.
- Transfer
Status - Status of a transfer operation.
Traits§
- Offload
Policy - Trait for offload policies that filter blocks.
- Presence
Checker - Async presence checker for object storage or other external destinations.
Functions§
- async_
batch_ result - Create an async batch policy result (boxes the future).
- async_
result - Create an async policy result (boxes the future).
- create_
policy_ from_ config - Create a composite policy from tier configuration.
- sync_
batch_ result - Create a synchronous batch policy result (zero allocation).
- sync_
result - Create a synchronous policy result (zero allocation).
- upgrade_
batch - Upgrade a batch of queued blocks by resolving weak references.
Type Aliases§
- BoxFuture
- Boxed future type for async policy evaluation.
- Policy
Batch Future - Future type for batch policy evaluation.
- Policy
Future - Future type for single-block policy evaluation.