[KV Offload] Fix failed-load livelock by marking the lookup verdict as a miss (#49328)
Signed-off-by: Robbie J <RobbieJ@users.noreply.github.com>
This commit is contained in:
@@ -34,7 +34,7 @@ lookup() is a pure OrderedDict operation.
|
||||
import queue
|
||||
import threading
|
||||
from abc import ABC, abstractmethod
|
||||
from collections.abc import Iterable
|
||||
from collections.abc import Collection, Iterable
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from vllm.logger import init_logger
|
||||
@@ -170,8 +170,26 @@ class AsyncLookupManager(ABC):
|
||||
for key, result in batch:
|
||||
state = self._lookup_state.get(key)
|
||||
if state is not None:
|
||||
# A key is enqueued for probing exactly once, so a decided
|
||||
# verdict must never receive a second result. Enforcing it
|
||||
# keeps a late/duplicate result from resurrecting a stale
|
||||
# True and reopening the failed-load livelock.
|
||||
assert state.result is None, (
|
||||
"cached key received a second lookup result; the "
|
||||
"enqueue-once invariant is broken and could reopen the "
|
||||
"failed-load livelock"
|
||||
)
|
||||
state.result = result
|
||||
|
||||
def mark_miss(self, keys: Collection[OffloadKey]) -> None:
|
||||
"""Force the cached verdict for ``keys`` to False after a failed load, so
|
||||
the scheduler stops re-issuing the doomed promotion (livelock, #49176).
|
||||
Keys with no cached entry are skipped."""
|
||||
for key in keys:
|
||||
state = self._lookup_state.get(key)
|
||||
if state is not None:
|
||||
state.result = False
|
||||
|
||||
def cleanup(self, req_id: str) -> None:
|
||||
"""Remove entries no longer needed by any active request.
|
||||
|
||||
|
||||
@@ -139,9 +139,9 @@ def _load_block(
|
||||
block_size: int,
|
||||
use_o_direct: bool = True,
|
||||
) -> None:
|
||||
"""
|
||||
Load callback: read one KV block from disk. Remove the file on failure.
|
||||
"""
|
||||
"""Read one KV block from disk; remove the file only on a provable short
|
||||
read (a too-short file is genuine corruption) and leave it untouched on any
|
||||
other error."""
|
||||
fd: int | None = None
|
||||
view_slice = view.cast("B")[offset : offset + block_size]
|
||||
o_direct = O_DIRECT if use_o_direct else 0
|
||||
@@ -150,15 +150,16 @@ def _load_block(
|
||||
fd = os.open(source_path, os.O_RDONLY | o_direct)
|
||||
bytes_read = os.readv(fd, [view_slice])
|
||||
if bytes_read < block_size:
|
||||
# A failure to remove must not mask the short-read error below.
|
||||
try:
|
||||
os.remove(source_path)
|
||||
except OSError as cleanup_exc:
|
||||
logger.warning(
|
||||
"Failed to remove short-read file %s: %s",
|
||||
source_path,
|
||||
cleanup_exc,
|
||||
)
|
||||
raise OSError(f"Short read: expected {block_size} bytes, read {bytes_read}")
|
||||
except Exception:
|
||||
try:
|
||||
os.remove(source_path)
|
||||
except OSError as cleanup_exc:
|
||||
logger.warning(
|
||||
"Failed to remove unreadable file %s: %s", source_path, cleanup_exc
|
||||
)
|
||||
raise
|
||||
finally:
|
||||
if fd is not None:
|
||||
os.close(fd)
|
||||
@@ -200,7 +201,9 @@ def batch_load_block(
|
||||
Load a batch of KV blocks from disk into a shared buffer in one call.
|
||||
|
||||
Block i is read from source_paths[i] into view[offsets[i] : offsets[i]+block_size].
|
||||
Raises on first error and removes the offending file.
|
||||
Raises on first error (see _load_block for the delete-on-short-read policy).
|
||||
On failure the raised OSError carries ``num_succeeded`` = the number of
|
||||
blocks loaded before the failing one, so the tier can keep them.
|
||||
"""
|
||||
_validate_offsets(view, offsets, block_size)
|
||||
|
||||
@@ -209,5 +212,11 @@ def batch_load_block(
|
||||
view_slices = [view_B[x : x + block_size] for x in offsets]
|
||||
return batch_load_block_C(paths, view_slices, use_o_direct)
|
||||
else:
|
||||
for path, offset in zip(paths, offsets):
|
||||
_load_block(path, view, offset, block_size, use_o_direct)
|
||||
for i, (path, offset) in enumerate(zip(paths, offsets)):
|
||||
try:
|
||||
_load_block(path, view, offset, block_size, use_o_direct)
|
||||
except OSError as exc:
|
||||
# Blocks 0..i-1 loaded fine; record the count for partial keep.
|
||||
# The C path sets the same attribute via PyObject_SetAttrString.
|
||||
exc.num_succeeded = i # type: ignore[attr-defined]
|
||||
raise
|
||||
|
||||
@@ -148,6 +148,16 @@ class FileSystemTierManager(SecondaryTierManager):
|
||||
)
|
||||
# Keys of in-flight store jobs, tracked only when events are enabled.
|
||||
self._store_job_keys: dict[JobId, list[OffloadKey]] = {}
|
||||
# Keys of in-flight load (promotion) jobs, so a failed load can mark
|
||||
# its own cached lookup verdicts False (see get_finished_jobs).
|
||||
self._load_job_keys: dict[JobId, list[OffloadKey]] = {}
|
||||
# Per load job: how many blocks loaded before a failure (partial keep).
|
||||
# Written by the pool worker inside the load task before it raises (so
|
||||
# before task_done publishes the job); read on the scheduler thread in
|
||||
# get_finished_jobs only for job ids the finished queue returned. Under
|
||||
# the GIL that read cannot observe the finished job without the prior
|
||||
# write, so no extra lock is needed (get_finished is itself lock-free).
|
||||
self._load_progress: dict[JobId, int] = {}
|
||||
|
||||
# Extract block size from primary view
|
||||
assert primary_kv_view.strides is not None, (
|
||||
@@ -219,22 +229,47 @@ class FileSystemTierManager(SecondaryTierManager):
|
||||
|
||||
@override
|
||||
def submit_load(self, job_metadata: JobMetadata) -> None:
|
||||
task = functools.partial(
|
||||
batch_load_block,
|
||||
[self.file_mapper.get_file_name(key) for key in job_metadata.keys],
|
||||
self._primary_kv_view,
|
||||
[int(bid) * self._block_size for bid in job_metadata.block_ids],
|
||||
self._block_size,
|
||||
self._use_o_direct,
|
||||
)
|
||||
job_id = job_metadata.job_id
|
||||
# Track this load's keys so a failed promotion can mark only its failed
|
||||
# keys as a miss (see get_finished_jobs).
|
||||
keys = list(job_metadata.keys)
|
||||
self._load_job_keys[job_id] = keys
|
||||
paths = [self.file_mapper.get_file_name(key) for key in keys]
|
||||
offsets = [int(bid) * self._block_size for bid in job_metadata.block_ids]
|
||||
|
||||
self._pool.enqueue_load(job_metadata.job_id, 1, [task])
|
||||
def load_task() -> None:
|
||||
try:
|
||||
batch_load_block(
|
||||
paths,
|
||||
self._primary_kv_view,
|
||||
offsets,
|
||||
self._block_size,
|
||||
self._use_o_direct,
|
||||
)
|
||||
except OSError as exc:
|
||||
# Runs on the pool worker thread. Record how many blocks loaded
|
||||
# before the failure so get_finished_jobs can keep them; this
|
||||
# write precedes task_done, so the scheduler reads it safely
|
||||
# under the GIL once the finished queue hands back this job.
|
||||
num_succeeded = getattr(exc, "num_succeeded", 0)
|
||||
self._load_progress[job_id] = num_succeeded
|
||||
# Surfaces errno (e.g. EMFILE "Too many open files") for both
|
||||
# the C and Python load paths.
|
||||
logger.debug(
|
||||
"Load of %d blocks for job %s failed at block %d: %s",
|
||||
len(paths),
|
||||
job_id,
|
||||
num_succeeded,
|
||||
exc,
|
||||
)
|
||||
raise
|
||||
|
||||
self._pool.enqueue_load(job_id, 1, [load_task])
|
||||
|
||||
@override
|
||||
def get_finished_jobs(self) -> Iterable[JobResult]:
|
||||
"""
|
||||
Collect completed jobs from the finished-jobs queue.
|
||||
"""
|
||||
"""Collect finished jobs; a failed promotion marks only its failed keys
|
||||
as a miss here (scheduler thread)."""
|
||||
results = []
|
||||
for job_id, success in self._pool.get_finished():
|
||||
if self.events is not None:
|
||||
@@ -248,7 +283,25 @@ class FileSystemTierManager(SecondaryTierManager):
|
||||
locality=self.locality,
|
||||
)
|
||||
)
|
||||
results.append(JobResult(job_id=job_id, success=success))
|
||||
load_keys = self._load_job_keys.pop(job_id, None)
|
||||
num_succeeded = self._load_progress.pop(job_id, 0)
|
||||
if load_keys is not None and not success:
|
||||
# A batched load stops at the first bad block and reports how
|
||||
# many loaded before it. Those earlier blocks are kept in the
|
||||
# primary tier (reported via successful_keys); only this block
|
||||
# and the ones after it are marked a miss and recomputed.
|
||||
successful = load_keys[:num_succeeded]
|
||||
failed = load_keys[num_succeeded:]
|
||||
self._lookup_manager.mark_miss(failed)
|
||||
results.append(
|
||||
JobResult(
|
||||
job_id=job_id,
|
||||
success=False,
|
||||
successful_keys=tuple(successful) if successful else None,
|
||||
)
|
||||
)
|
||||
else:
|
||||
results.append(JobResult(job_id=job_id, success=success))
|
||||
return results
|
||||
|
||||
@override
|
||||
|
||||
@@ -141,6 +141,9 @@ class ObjectStoreSecondaryTierManager(SecondaryTierManager):
|
||||
)
|
||||
# Keys of in-flight store jobs, tracked only when events are enabled.
|
||||
self._store_job_keys: dict[JobId, list[OffloadKey]] = {}
|
||||
# Keys of in-flight load (promotion) jobs, so a failed download can
|
||||
# mark its own cached lookup verdicts False (see get_finished_jobs).
|
||||
self._load_job_keys: dict[JobId, list[OffloadKey]] = {}
|
||||
|
||||
agent_config = nixl_agent_config(backends=[])
|
||||
self._agent = nixl_agent("ObjAgent", agent_config)
|
||||
@@ -281,6 +284,7 @@ class ObjectStoreSecondaryTierManager(SecondaryTierManager):
|
||||
)
|
||||
|
||||
def submit_load(self, job_metadata: JobMetadata) -> None:
|
||||
self._load_job_keys[job_metadata.job_id] = list(job_metadata.keys)
|
||||
obj_keys = (self._file_mapper.get_file_name(k) for k in job_metadata.keys)
|
||||
self._submit_transfer(
|
||||
job_metadata.job_id, job_metadata.block_ids, obj_keys, NIXL_READ
|
||||
@@ -340,12 +344,13 @@ class ObjectStoreSecondaryTierManager(SecondaryTierManager):
|
||||
self._pending_results.append(JobResult(job_id=job_id, success=success))
|
||||
|
||||
def get_finished_jobs(self) -> Iterable[JobResult]:
|
||||
"""Poll in-flight transfers; return completed (job_id, success) pairs."""
|
||||
"""Poll transfers; a failed promotion marks its cached verdicts False
|
||||
here (scheduler thread)."""
|
||||
self._poll_active_transfers()
|
||||
results = self._pending_results
|
||||
self._pending_results = []
|
||||
if self.events is not None:
|
||||
for result in results:
|
||||
for result in results:
|
||||
if self.events is not None:
|
||||
keys = self._store_job_keys.pop(result.job_id, None)
|
||||
if result.success and keys:
|
||||
self.events.append(
|
||||
@@ -356,6 +361,17 @@ class ObjectStoreSecondaryTierManager(SecondaryTierManager):
|
||||
locality=self.locality,
|
||||
)
|
||||
)
|
||||
# Mark only the keys that did not load as a miss; the request
|
||||
# recomputes them. The miss is per-request (cleared when the request
|
||||
# finishes), so other requests still HIT the blocks that loaded
|
||||
# fine. nixl reports the batch as a whole (successful_keys is None),
|
||||
# so today this marks all keys; the subtraction keeps it correct if
|
||||
# partial results are ever reported.
|
||||
load_keys = self._load_job_keys.pop(result.job_id, None)
|
||||
if load_keys is not None and not result.success:
|
||||
successful = set(result.successful_keys or ())
|
||||
failed = [k for k in load_keys if k not in successful]
|
||||
self._lookup_manager.mark_miss(failed)
|
||||
return results
|
||||
|
||||
def take_events(self) -> Iterable[OffloadingEvent]:
|
||||
|
||||
Reference in New Issue
Block a user