Skip to content

vllm.v1.kv_offload.tiering.p2p.session.client

Client-role state machine for a single peer session.

Handles outgoing fetch requests, abort-on-timeout, abort-ack timeout, and produces LoadResult for completed loads. 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).

Classes:

  • ClientRole

    Client-side load state machine for one peer session.

  • LoadResult

    Result from a session poll, client side.

ClientRole

Client-side load state machine for one peer session.

The coordinator owns the connection and the send-gating; this role is given a send callback and a peer_id for log messages and is otherwise self-contained.

Methods:

  • cancel

    Cancel a pending load. Sends AbortFetchMsg if still active.

  • close

    Tear down. Returns (job_id, kv_request_id) for pending loads.

  • collect_results

    Walk timeouts and drain completed loads.

  • on_abort_ack

    Handle an AbortAckMsg from the peer.

  • on_transfer_done

    Handle a TransferDoneMsg from the peer.

  • request_blocks

    Register a load request and send the FetchMsg.

Source code in vllm/v1/kv_offload/tiering/p2p/session/client.py
class ClientRole:
    """Client-side load state machine for one peer session.

    The coordinator owns the connection and the send-gating; this role
    is given a ``send`` callback and a ``peer_id`` for log messages and
    is otherwise self-contained.
    """

    def __init__(self, peer_id: str, send: Callable[[dict], None]) -> None:
        self._peer_id = peer_id
        self._send = send
        self._inbound: dict[str, _InboundRequestState] = {}
        self._completed_loads: list[LoadResult] = []

    # ------------------------------------------------------------------
    # Public API
    # ------------------------------------------------------------------

    def request_blocks(
        self,
        job_id: JobId,
        kv_request_id: str,
        keys: Sequence[bytes],
        block_ids: Sequence[int],
        send_ready: bool,
    ) -> None:
        """Register a load request and send the FetchMsg."""
        logger.debug(
            "P2PSession %s: request_blocks job_id=%d kv_request_id=%s "
            "blocks=%d ready=%s",
            self._peer_id,
            job_id,
            kv_request_id,
            len(block_ids),
            send_ready,
        )
        self._inbound[kv_request_id] = _InboundRequestState(
            job_id=job_id,
            kv_request_id=kv_request_id,
            submitted_at=time.monotonic(),
        )
        self._send(
            {
                TYPE_KEY: FetchMsg.TYPE,
                FetchMsg.KV_REQUEST_ID: kv_request_id,
                FetchMsg.BLOCK_HASHES: list(keys),
                FetchMsg.BLOCK_INDEXES: [int(idx) for idx in block_ids],
            }
        )

    def cancel(self, kv_request_id: str) -> None:
        """Cancel a pending load. Sends AbortFetchMsg if still active."""
        req = self._inbound.pop(kv_request_id, None)
        if req is not None and req.aborted_at is None:
            self._send(
                {
                    TYPE_KEY: AbortFetchMsg.TYPE,
                    AbortFetchMsg.KV_REQUEST_ID: kv_request_id,
                }
            )

    def on_transfer_done(self, kv_request_id: str, success: bool) -> None:
        """Handle a TransferDoneMsg from the peer."""
        req = self._inbound.pop(kv_request_id, None)
        if req is not None:
            self._completed_loads.append(
                LoadResult(
                    job_id=req.job_id,
                    kv_request_id=kv_request_id,
                    success=success,
                )
            )
        else:
            # No matching _inbound entry: either a duplicate
            # transfer_done from the peer (protocol violation) or a
            # benign race with a local cancel/abort/timeout that
            # already popped the entry. We don't track terminated ids,
            # so we can't tell — log so it's findable.
            logger.warning(
                "P2PSession %s: transfer_done for unknown kv_request_id=%s "
                "(duplicate from peer, or raced with local cancel/timeout)",
                self._peer_id,
                kv_request_id,
            )

    def on_abort_ack(self, kv_request_id: str) -> None:
        """Handle an AbortAckMsg from the peer."""
        req = self._inbound.pop(kv_request_id, None)
        if req is not None:
            self._completed_loads.append(
                LoadResult(
                    job_id=req.job_id,
                    kv_request_id=kv_request_id,
                    success=False,
                )
            )
        else:
            # See on_transfer_done: same ambiguity (duplicate ack
            # vs. raced with local cancel/timeout that already popped).
            logger.warning(
                "P2PSession %s: abort_ack for unknown kv_request_id=%s "
                "(duplicate from peer, or raced with local cancel/timeout)",
                self._peer_id,
                kv_request_id,
            )

    def collect_results(self) -> list[LoadResult]:
        """Walk timeouts and drain completed loads.

        Active requests past ``_LOAD_TIMEOUT_S`` get an AbortFetchMsg
        sent and enter the aborting phase. Aborting requests past
        ``_ABORT_ACK_TIMEOUT_S`` are surfaced as failed loads.
        """
        now = time.monotonic()
        to_remove: list[str] = []
        for req_id, req in self._inbound.items():
            if req.aborted_at is None:
                if now - req.submitted_at >= _LOAD_TIMEOUT_S:
                    req.aborted_at = now
                    logger.warning(
                        "P2PSession %s: %s timed out, sending abort",
                        self._peer_id,
                        req_id,
                    )
                    self._send(
                        {
                            TYPE_KEY: AbortFetchMsg.TYPE,
                            AbortFetchMsg.KV_REQUEST_ID: req_id,
                        }
                    )
            else:
                if now - req.aborted_at >= _ABORT_ACK_TIMEOUT_S:
                    to_remove.append(req_id)
                    self._completed_loads.append(
                        LoadResult(
                            job_id=req.job_id,
                            kv_request_id=req_id,
                            success=False,
                        )
                    )
                    logger.warning(
                        "P2PSession %s: abort_ack timed out for kv_request_id=%s",
                        self._peer_id,
                        req_id,
                    )
        for req_id in to_remove:
            self._inbound.pop(req_id)

        results = self._completed_loads
        self._completed_loads = []
        return results

    def close(self) -> list[tuple[int, str]]:
        """Tear down. Returns ``(job_id, kv_request_id)`` for pending loads."""
        failed = [(req.job_id, req.kv_request_id) for req in self._inbound.values()]
        self._inbound.clear()
        self._completed_loads.clear()
        return failed

cancel(kv_request_id)

Cancel a pending load. Sends AbortFetchMsg if still active.

Source code in vllm/v1/kv_offload/tiering/p2p/session/client.py
def cancel(self, kv_request_id: str) -> None:
    """Cancel a pending load. Sends AbortFetchMsg if still active."""
    req = self._inbound.pop(kv_request_id, None)
    if req is not None and req.aborted_at is None:
        self._send(
            {
                TYPE_KEY: AbortFetchMsg.TYPE,
                AbortFetchMsg.KV_REQUEST_ID: kv_request_id,
            }
        )

close()

Tear down. Returns (job_id, kv_request_id) for pending loads.

Source code in vllm/v1/kv_offload/tiering/p2p/session/client.py
def close(self) -> list[tuple[int, str]]:
    """Tear down. Returns ``(job_id, kv_request_id)`` for pending loads."""
    failed = [(req.job_id, req.kv_request_id) for req in self._inbound.values()]
    self._inbound.clear()
    self._completed_loads.clear()
    return failed

collect_results()

Walk timeouts and drain completed loads.

Active requests past _LOAD_TIMEOUT_S get an AbortFetchMsg sent and enter the aborting phase. Aborting requests past _ABORT_ACK_TIMEOUT_S are surfaced as failed loads.

Source code in vllm/v1/kv_offload/tiering/p2p/session/client.py
def collect_results(self) -> list[LoadResult]:
    """Walk timeouts and drain completed loads.

    Active requests past ``_LOAD_TIMEOUT_S`` get an AbortFetchMsg
    sent and enter the aborting phase. Aborting requests past
    ``_ABORT_ACK_TIMEOUT_S`` are surfaced as failed loads.
    """
    now = time.monotonic()
    to_remove: list[str] = []
    for req_id, req in self._inbound.items():
        if req.aborted_at is None:
            if now - req.submitted_at >= _LOAD_TIMEOUT_S:
                req.aborted_at = now
                logger.warning(
                    "P2PSession %s: %s timed out, sending abort",
                    self._peer_id,
                    req_id,
                )
                self._send(
                    {
                        TYPE_KEY: AbortFetchMsg.TYPE,
                        AbortFetchMsg.KV_REQUEST_ID: req_id,
                    }
                )
        else:
            if now - req.aborted_at >= _ABORT_ACK_TIMEOUT_S:
                to_remove.append(req_id)
                self._completed_loads.append(
                    LoadResult(
                        job_id=req.job_id,
                        kv_request_id=req_id,
                        success=False,
                    )
                )
                logger.warning(
                    "P2PSession %s: abort_ack timed out for kv_request_id=%s",
                    self._peer_id,
                    req_id,
                )
    for req_id in to_remove:
        self._inbound.pop(req_id)

    results = self._completed_loads
    self._completed_loads = []
    return results

on_abort_ack(kv_request_id)

Handle an AbortAckMsg from the peer.

Source code in vllm/v1/kv_offload/tiering/p2p/session/client.py
def on_abort_ack(self, kv_request_id: str) -> None:
    """Handle an AbortAckMsg from the peer."""
    req = self._inbound.pop(kv_request_id, None)
    if req is not None:
        self._completed_loads.append(
            LoadResult(
                job_id=req.job_id,
                kv_request_id=kv_request_id,
                success=False,
            )
        )
    else:
        # See on_transfer_done: same ambiguity (duplicate ack
        # vs. raced with local cancel/timeout that already popped).
        logger.warning(
            "P2PSession %s: abort_ack for unknown kv_request_id=%s "
            "(duplicate from peer, or raced with local cancel/timeout)",
            self._peer_id,
            kv_request_id,
        )

on_transfer_done(kv_request_id, success)

Handle a TransferDoneMsg from the peer.

Source code in vllm/v1/kv_offload/tiering/p2p/session/client.py
def on_transfer_done(self, kv_request_id: str, success: bool) -> None:
    """Handle a TransferDoneMsg from the peer."""
    req = self._inbound.pop(kv_request_id, None)
    if req is not None:
        self._completed_loads.append(
            LoadResult(
                job_id=req.job_id,
                kv_request_id=kv_request_id,
                success=success,
            )
        )
    else:
        # No matching _inbound entry: either a duplicate
        # transfer_done from the peer (protocol violation) or a
        # benign race with a local cancel/abort/timeout that
        # already popped the entry. We don't track terminated ids,
        # so we can't tell — log so it's findable.
        logger.warning(
            "P2PSession %s: transfer_done for unknown kv_request_id=%s "
            "(duplicate from peer, or raced with local cancel/timeout)",
            self._peer_id,
            kv_request_id,
        )

request_blocks(job_id, kv_request_id, keys, block_ids, send_ready)

Register a load request and send the FetchMsg.

Source code in vllm/v1/kv_offload/tiering/p2p/session/client.py
def request_blocks(
    self,
    job_id: JobId,
    kv_request_id: str,
    keys: Sequence[bytes],
    block_ids: Sequence[int],
    send_ready: bool,
) -> None:
    """Register a load request and send the FetchMsg."""
    logger.debug(
        "P2PSession %s: request_blocks job_id=%d kv_request_id=%s "
        "blocks=%d ready=%s",
        self._peer_id,
        job_id,
        kv_request_id,
        len(block_ids),
        send_ready,
    )
    self._inbound[kv_request_id] = _InboundRequestState(
        job_id=job_id,
        kv_request_id=kv_request_id,
        submitted_at=time.monotonic(),
    )
    self._send(
        {
            TYPE_KEY: FetchMsg.TYPE,
            FetchMsg.KV_REQUEST_ID: kv_request_id,
            FetchMsg.BLOCK_HASHES: list(keys),
            FetchMsg.BLOCK_INDEXES: [int(idx) for idx in block_ids],
        }
    )

LoadResult

Bases: NamedTuple

Result from a session poll, client side.

Source code in vllm/v1/kv_offload/tiering/p2p/session/client.py
class LoadResult(NamedTuple):
    """Result from a session poll, client side."""

    job_id: int
    kv_request_id: str
    success: bool

_InboundRequestState dataclass

Client-role state for a single load request.

Source code in vllm/v1/kv_offload/tiering/p2p/session/client.py
@dataclass
class _InboundRequestState:
    """Client-role state for a single load request."""

    job_id: int  # opaque ID assigned by the manager to this load request
    kv_request_id: str
    submitted_at: float
    aborted_at: float | None = None