Skip to content

vllm.v1.kv_offload.tiering.p2p.session.server

Server-role state machine for a single peer session.

Owns block matching (supply vs. demand), inflight RDMA transfers, store-job timeouts, abort-drain, and produces StoreResult for completed stores. The session coordinator parses wire messages and dispatches typed arguments here; this module never touches ControlConnection directly — it emits via the send callback injected by the coordinator (which gates on ConnectAck).

Protocol violations the role can detect (today: duplicate FetchMsg for the same kv_request_id) are surfaced as ValueError so the coordinator's _dispatch_message can reuse its existing _protocol_error path.

Classes:

  • ServerRole –

    Server-side store/serve state machine for one peer session.

  • StoreDrainResult –

    Failed queued stores and PD ids whose handoffs cannot be resumed.

  • StoreResult –

    Result from a session poll, server side.

ServerRole

Server-side store/serve state machine for one peer session.

The coordinator owns the connection and the send-gating; this role is given a send callback, the DataTransport, and the peer_id for transport calls and log messages.

Methods:

  • add_stored_blocks –

    New blocks stored locally — match within their fetch round.

  • close –

    Tear down. Cancels inflight, retaining what may still write.

  • collect_idle_timeouts –

    Run only the store-job timeout sweep.

  • collect_results –

    Drain timeouts, deferred results, and transport completions.

  • drain_pending_aborts –

    Re-attempt every parked abort once per poll tick.

  • drain_stores –

    Withdraw supply, keeping empty lookup rounds for their late fetch.

  • finish –

    Mark an outbound request finishing.

  • on_abort_fetch –

    Handle an AbortFetchMsg from the peer, cancelling one round.

  • on_fetch –

    Handle a FetchMsg from the peer.

  • on_lookup –

    Enqueue a LookupMsg from a symmetric-P2P consumer.

  • serve_external_requests –

    Resolve inbound peer lookups against the tiering manager.

Attributes:

has_inflight_transfers property

True if any outbound store transfer is still in flight.

_cancel_due(rnd, now) staticmethod

Rate-limit wait-mode cancels of rnd's transfers.

Shared by the store-timeout sweep and the abort drain, so a round gets at most one attempt per _CANCEL_RETRY_INTERVAL_S.

_check_stalled_transfers()

Fault the peer once a transfer outlives the stall timeout.

An RDMA WRITE posted to a live NIC completes or errors within milliseconds; one NIXL still reports PROC/PEND long after points at a wedged lane or endpoint, which nothing else would ever surface. The fault stops new WRITEs to this peer and ends the session, but releases nothing: the transfer and its pins are retained across teardown until the transport reports it stopped.

_drain_abort(kv_request_id, round_seq)

One drain attempt for a pending abort.

Cancels the aborted round, then asks the transport to cancel its inflight transfers in mode="wait". Sends AbortAckMsg only once none of them can still write to the peer: the ack lets the peer reuse the destination blocks. A transfer that NIXL does not report terminal is never force-released, however long the drain takes: neither releasing its handle nor removing the remote agent stops a WRITE that is already in flight. Past _CANCEL_DRAIN_TIMEOUT_S the drain only logs a warning.

_fail_round_jobs(rnd)

Fail a terminated round's still-pending store jobs (idempotent).

_fail_unwritten_round_jobs(rnd)

Fail a cancelled round's jobs that no inflight transfer carries.

Jobs that an inflight transfer carries keep their pins; they settle when that transfer turns terminal (_settle_xfer_jobs).

_finalize_cancelled(kv_request_id, round_key)

Finalize a cancelled round once its last transfer is terminal.

An aborted round gets no TransferDoneMsg: its parked abort answers the peer with AbortAckMsg on the next drain.

_finalize_lookup(lookup, parent)

Emit the aggregated LookupRespMsg and close the synthetic request.

Called once per lookup when pending is empty — either every key resolved to HIT or MISS, or the deadline forced remaining stragglers to MISS. Preserves the wire order of the inbound LookupMsg so the client can zip keys and hits positionally.

_finalize_outbound(kv_request_id, round_key, success=None, send_done=True)

Pop one round and emit its terminal results.

If success is None, derive it from req.remaining == 0. send_done=False skips the TransferDoneMsg (terminal empty fetch). Other rounds of the id are untouched.

_finish_inbound_lookups(kv_request_id)

Close the server-side lookup phase for kv_request_id.

Pops every parked _ActiveLookup for this id (so _resolve_pending_lookups cannot promote a HIT_PENDING / RETRY key into a fresh parent.create_store_job after this point) and queues each lookup.ctx for parent.on_request_finished (fired by the next serve_external_requests, since no parent handle is available during dispatch) so the TieringManager can release per-lookup bookkeeping. Any still-unprocessed raw LookupMsg for this id is dropped — it never got on_new_request, so nothing is owed. The aggregated LookupRespMsg is skipped — the client already knows the request is over (it just sent a terminal FetchMsg, or is finishing locally).

Called on the two events that mean "no more lookup traffic for kv_request_id is expected on this session": the terminal empty FetchMsg from the peer and a local finish. Whichever fires second is a no-op.

_get_or_create_request(kv_request_id)

Get or create the state entry for a kv_request_id.

_inflight_add(tid, xfer)

Insert an inflight transfer and record it on the request.

_inflight_pop(tid)

Pop an inflight transfer and drop it from the request's set.

Callers are responsible for the _maybe_prune that may follow once the request's other state has also cleared.

_maybe_prune(kv_request_id)

Drop the entry once it holds no live state.

_pin_and_register_hits(lookup, keys, parent)

Pin primary slots for HIT keys and park them as the lookup's round supply via add_stored_blocks.

Caller has already confirmed every key is HIT, so the JobMetadata returned by parent.create_store_job carries parallel keys/block_ids of length len(keys). Nothing can evict between that confirmation and the pin because the whole serve_external_requests call runs inside one hold of the tiering manager's lock -- which is why that call must never release it partway through.

_poll_lookup_keys(lookup, keys, parent)

Poll keys against the tiering manager and pin any HITs.

For each key not already definitively resolved, query parent.lookup and record the outcome on lookup: HIT / MISS land in resolved and clear pending; HIT_PENDING / RETRY park in pending for a later serve. Newly-HIT keys are pinned in one batch via _pin_and_register_hits and also returned (for caller logging).

Shared by the first-sighting pass (_process_inbound_lookup) and the re-poll pass (_resolve_pending_lookups); callers pass a de-duplicated keys collection.

_process_inbound_lookup(kv_request_id, keys, enqueued_at, round_seq, parent)

Resolve one enqueued LookupMsg against parent.

For each key, query the tiering manager via parent.lookup and pin any HITs immediately via parent.create_store_job (plumbed into the existing add_stored_blocks matching path so the eventual FetchMsg finds them). HIT_PENDING / RETRY keys park in _ActiveLookup for re-polling by _resolve_pending_lookups.

The outbound LookupRespMsg is deferred until every key has settled to HIT or MISS (or the batch-level deadline fires, forcing any stragglers to MISS). One LookupRespMsg goes out per LookupMsg — carrying every key in wire order — after which parent.on_request_finished fires and the entry is dropped.

_read_job_ids(rnd=None)

Jobs whose blocks an inflight transfer (of rnd, if given) still reads; their pins must be held.

_resolve_pending_lookups(kv_request_id, parent)

Re-poll a request's deferred LookupMsg keys; finalize when ready.

Walks every parked _ActiveLookup for kv_request_id and re-calls parent.lookup per still-pending key, moving HIT/MISS results into resolved. If deadline has passed, remaining pending keys are force-resolved to MISS so the consumer can fall back instead of waiting on a stuck producer. Newly-HIT keys are pinned via parent.create_store_job in one call per affected lookup. Lookups whose pending empties out get their aggregated LookupRespMsg sent by _finalize_lookup.

_settle_xfer_jobs(xfer, success)

Emit StoreResults for a completed transfer's store jobs.

Pops each attached job from _store_jobs and clears it from its round's pending set. A job already popped (via timeout, cancel, etc.) is skipped so we never double-emit a contradictory result.

add_stored_blocks(kv_request_id, keys, block_ids, job_id, round_seq=0, *, from_lookup=False)

New blocks stored locally — match within their fetch round.

Lookup pins carry the round they were probed under; PD submit_store batches share PD's single round 0.

close()

Tear down. Cancels inflight, retaining what may still write.

Returns (failed_store_job_ids, failed_serves, retained). failed_serves are synthetic lookup ctxs still owing a parent.on_request_finished. The session is going away with no parent handle in hand, so the manager flushes these in its next serve_external_requests. retained maps each transfer NIXL does not report terminal to the store jobs it still reads: like the abort drain, close never releases such a transfer, so those jobs stay pinned and are left out of failed_store_job_ids until the transport reports the transfer stopped.

collect_idle_timeouts()

Run only the store-job timeout sweep.

Used by the coordinator's no-conn poll path: a pending session cannot have inflight transfers (no peer registered yet), so we skip the transport poll and the deferred-result drain.

collect_results()

Drain timeouts, deferred results, and transport completions.

Inbound LookupMsg resolution (including re-polling HIT_PENDING / RETRY keys) is NOT done here — it runs in serve_external_requests where the ParentManager is available.

drain_pending_aborts()

Re-attempt every parked abort once per poll tick.

drain_stores(kv_request_ids=None)

Withdraw supply, keeping empty lookup rounds for their late fetch.

finish(kv_request_id)

Mark an outbound request finishing.

No more submit_store calls will arrive for this id. Any blocks the peer demanded but we never stored will never come; tell the peer to stop waiting (TransferDoneMsg success=False) instead of letting it hit its load timeout.

Also drops any in-flight lookups for this kv_request_id and queues their ctxs for parent.on_request_finished so the TieringManager can release per-request bookkeeping. (For symmetric P2P this path is rarely hit since the producer has no local request lifecycle for the consumer's id; this cleanup is mostly active on the PD side.)

If the decoder hasn't sent fetch yet (no demand received), defer — on_fetch will finalize once demand arrives.

If inflight transfers exist for this id, defer — the last completing transfer in collect_results will fire the message.

on_abort_fetch(kv_request_id, round_seq=0)

Handle an AbortFetchMsg from the peer, cancelling one round.

on_fetch(kv_request_id, keys, block_indexes, round_seq=0)

Handle a FetchMsg from the peer.

A non-empty fetch binds and closes its round, leaving lookup state alone (the next round's LookupMsg may already be in flight). The terminal empty fetch closes the id: parked lookups are popped and every remaining round drained. A second fetch for a round already holding demand raises ValueError (protocol-error disconnect).

on_lookup(kv_request_id, keys, round_seq=0)

Enqueue a LookupMsg from a symmetric-P2P consumer.

Dispatch runs during session.poll() where the ParentManager handle is not available, so this only records the raw request. It is resolved — querying the tiering manager and emitting the aggregated LookupRespMsg — by the next serve_external_requests, the sole window in which parent calls are valid.

serve_external_requests(parent)

Resolve inbound peer lookups against the tiering manager.

Called once per scheduler step with a parent handle valid only for this call. Drains newly-enqueued LookupMsgs, re-polls any parked HIT_PENDING / RETRY keys, and releases the bookkeeping for lookups closed since the last serve.

StoreDrainResult

Bases: NamedTuple

Failed queued stores and PD ids whose handoffs cannot be resumed.

StoreResult

Bases: NamedTuple

Result from a session poll, server side.

_ActiveLookup dataclass

In-flight state for one inbound LookupMsg.

Aggregates per-key HIT/MISS resolutions and defers the single outbound LookupRespMsg until every key has been resolved or the deadline fires (remaining pending keys then force-MISS). Exactly one LookupRespMsg is emitted per LookupMsg, carrying every key in the original wire order.

_InflightXfer dataclass

Metadata for a single inflight RDMA transfer, keyed by transfer_id.

_MatchResult

Bases: NamedTuple

Result of block matching: pairs ready for transfer.

_OutboundRequestState dataclass

Server-role state for a single fetch round of a peer request.

A kv_request_id may run several lookup→fetch rounds; rounds live in _ServerRequestState.outbound keyed by the wire round_seq, so terminals touch only their own round.

Methods:

add_fetch_demand(keys, block_indexes)

Register the peer's fetch demand. Returns matched pairs.

add_stored_blocks(keys, block_ids, job_id)

Add locally-stored blocks. Returns matched pairs.

_PendingLookup

Bases: NamedTuple

A raw inbound LookupMsg awaiting resolution.

Enqueued by on_lookup during dispatch and drained by the next serve_external_requests. The deadline for any resulting HIT_PENDING / RETRY key is measured from enqueued_at.

_ServerRequestState dataclass

Per-kv_request_id server-side state.

Consolidates the outbound serve/transfer state, the symmetric-P2P inbound-lookup state, abort bookkeeping, and the inflight-transfer count for one kv_request_id. The owning id is the _requests dict key and is not duplicated here. An entry is dropped once every field is idle — see ServerRole._maybe_prune.