[KVConnector][P2P] Register peers off the scheduler thread (#57139)

Signed-off-by: Liran Schour <lirans@il.ibm.com>
Co-authored-by: Itay Etelis <92247226+Etelis@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Or Ozeri <oro@il.ibm.com>
This commit is contained in:
liranschour
2026-10-14 08:21:57 +03:00
committed by GitHub
co-authored by Itay Etelis mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Or Ozeri
parent 77154ca611
commit a2fb482732
6 changed files with 416 additions and 21 deletions
@@ -5,9 +5,11 @@
from __future__ import annotations
import ctypes
import threading
from unittest.mock import MagicMock, patch
import numpy as np
import pytest
from vllm.v1.kv_offload.tiering.p2p.data.base import PollResult
from vllm.v1.kv_offload.tiering.p2p.data.nixl import NixlTransport
@@ -107,6 +109,59 @@ class TestNixlTransportWithMockedAgent:
transport = self._make_transport()
assert transport.get_agent_metadata() == b"test-metadata"
def test_add_remote_peer_async_registers_off_the_caller_thread(self):
"""The O(num_blocks) registration must not run on the caller thread.
This is the whole point of the async path: the scheduler thread
hands the work off rather than paying prep_xfer_dlist inline.
"""
transport = self._make_transport()
caller = threading.get_ident()
worker_threads: list[int] = []
def record_thread(*args, **kwargs):
worker_threads.append(threading.get_ident())
return MagicMock(name="remote_dlist")
transport._agent.prep_xfer_dlist.side_effect = record_thread
future = transport.add_remote_peer_async("peer:1", b"meta", 0x1000, 8, 1024)
future.result(timeout=5)
assert worker_threads and caller not in worker_threads
assert transport.write_blocks("peer:1", [0], [1]) is not None
def test_registration_landing_after_remove_is_discarded(self):
"""A late registration must not revive a peer that was reaped.
The session is torn down and remove_remote_peer runs while the
worker is still building descriptors; publishing afterwards would
leave a registered peer with no session and leak its handles.
"""
transport = self._make_transport()
building = threading.Event()
release = threading.Event()
def blocking_prep(*args, **kwargs):
building.set()
release.wait(timeout=5)
return MagicMock(name="remote_dlist")
transport._agent.prep_xfer_dlist.side_effect = blocking_prep
future = transport.add_remote_peer_async("peer:1", b"meta", 0x1000, 8, 1024)
assert building.wait(timeout=5)
transport.remove_remote_peer("peer:1")
release.set()
with pytest.raises(RuntimeError, match="superseded"):
future.result(timeout=5)
assert "peer:1" not in transport._remote_dlists
assert transport.write_blocks("peer:1", [0], [1]) is None
# The handles the worker built were released rather than leaked.
assert transport._agent.release_dlist_handle.called
assert transport._agent.remove_remote_agent.called
def test_write_blocks_returns_none_for_unknown_peer(self):
"""write_blocks returns None if peer not registered."""
transport = self._make_transport()
@@ -10,6 +10,7 @@ from __future__ import annotations
import time
import uuid
from concurrent.futures import Future
from types import SimpleNamespace
import numpy as np
@@ -1111,6 +1112,14 @@ class _FakeData:
"block_len": block_len,
}
def add_remote_peer_async(
self, peer_id, agent_metadata, base_addr, num_blocks, block_len
):
future: Future[None] = Future()
self.add_remote_peer(peer_id, agent_metadata, base_addr, num_blocks, block_len)
future.set_result(None)
return future
def remove_remote_peer(self, peer_id: str) -> None:
self._remote_peers.pop(peer_id, None)
@@ -1368,6 +1377,11 @@ class TestAcceptNewPeers:
def add_remote_peer(self, *args, **kwargs):
pass
def add_remote_peer_async(self, *args, **kwargs):
future: Future[None] = Future()
future.set_result(None)
return future
mgr = _make_manager()
mgr._data = FakeData()
peer_id = "10.0.0.1:8000"
@@ -13,7 +13,8 @@ completes its own load.
from __future__ import annotations
import time
from collections.abc import Sequence
from collections.abc import Callable, Sequence
from concurrent.futures import Future
import numpy as np
import pytest
@@ -74,7 +75,13 @@ class FakeDataTransport:
num_blocks: int = 16,
block_len: int = 4096,
config_fingerprint: str = "",
defer_registration: bool = False,
) -> None:
# When True, add_remote_peer_async parks the registration until
# complete_registrations() resolves it, standing in for the real
# transport's worker thread.
self.defer_registration = defer_registration
self._deferred: list[tuple[Future[None], Callable[[], None]]] = []
self._base_addr = base_addr
self._num_blocks = num_blocks
self._block_len = block_len
@@ -116,6 +123,38 @@ class FakeDataTransport:
"block_len": block_len,
}
def add_remote_peer_async(
self, peer_id, agent_metadata, base_addr, num_blocks, block_len
) -> Future[None]:
future: Future[None] = Future()
def apply() -> None:
self.add_remote_peer(
peer_id, agent_metadata, base_addr, num_blocks, block_len
)
if self.defer_registration:
self._deferred.append((future, apply))
return future
apply()
future.set_result(None)
return future
def complete_registrations(self, exc: Exception | None = None) -> int:
"""Resolve parked registrations the way the worker thread would.
Returns the number resolved so callers can assert one was actually
in flight rather than silently testing nothing.
"""
deferred, self._deferred = self._deferred, []
for future, apply in deferred:
if exc is not None:
future.set_exception(exc)
else:
apply()
future.set_result(None)
return len(deferred)
def remove_remote_peer(self, peer_id: str) -> None:
self._remote_peers.pop(peer_id, None)
@@ -390,6 +429,67 @@ class TestConnectHandshake:
ack = next(m for m in conn._sent if m[TYPE_KEY] == ConnectAckMsg.TYPE)
assert ack[ConnectAckMsg.PEER_ID] == "local:9000"
def test_ack_withheld_until_registration_completes(self):
"""ConnectAck must not precede registration.
The peer treats ConnectAck as permission to send, so acking while
registration is still in flight would let a FetchMsg arrive for
blocks this side cannot yet write to.
"""
transport = FakeDataTransport(defer_registration=True)
session, conn, _ = _make_session(transport=transport)
conn.enqueue(_peer_connect_msg())
session.poll()
assert "peer:8000" not in transport._remote_peers
assert not any(m[TYPE_KEY] == ConnectAckMsg.TYPE for m in conn._sent)
assert transport.complete_registrations() == 1
session.poll()
assert "peer:8000" in transport._remote_peers
ack = next(m for m in conn._sent if m[TYPE_KEY] == ConnectAckMsg.TYPE)
assert ack[ConnectAckMsg.PEER_ID] == "local:9000"
def test_pending_registration_is_pending_work(self):
"""An in-flight registration keeps the engine ticking.
Nothing else drives poll(), so if the session reported idle the
ack would never be sent.
"""
transport = FakeDataTransport(defer_registration=True)
session, conn, _ = _make_session(transport=transport)
conn.enqueue(_peer_connect_msg())
session.poll()
assert session.has_pending_work
transport.complete_registrations()
session.poll()
assert not session.has_pending_work
def test_failed_registration_rejects_peer(self):
"""A registration that fails kills the connection instead of acking."""
transport = FakeDataTransport(defer_registration=True)
session, conn, _ = _make_session(transport=transport)
conn.enqueue(_peer_connect_msg())
session.poll()
transport.complete_registrations(exc=RuntimeError("nixl exploded"))
session.poll()
assert not conn.alive
assert not any(m[TYPE_KEY] == ConnectAckMsg.TYPE for m in conn._sent)
def test_duplicate_connect_during_registration_is_rejected(self):
"""A second ConnectMsg mid-registration is a protocol violation."""
transport = FakeDataTransport(defer_registration=True)
session, conn, _ = _make_session(transport=transport)
conn.enqueue(_peer_connect_msg())
session.poll()
conn.enqueue(_peer_connect_msg())
session.poll()
assert not conn.alive
def test_connect_ack_makes_session_ready(self):
"""Session.ready becomes True after ConnectAckMsg."""
session, conn, _ = _make_session()