Encapsulate the pending-flush bookkeeping in a small wrapper (#25727)

This commit is contained in:
fzyzcjy
2026-05-19 09:23:10 +08:00
committed by GitHub
parent 5067da3eb6
commit 170fe57cf0
3 changed files with 111 additions and 73 deletions
+10 -49
View File
@@ -103,7 +103,6 @@ from sglang.srt.managers.io_struct import (
ExpertDistributionReqOutput,
ExpertDistributionReqType,
FlushCacheReqInput,
FlushCacheReqOutput,
FreezeGCReq,
GetInternalStateReq,
GetInternalStateReqOutput,
@@ -165,6 +164,9 @@ from sglang.srt.managers.scheduler_components.batch_result_processor import (
from sglang.srt.managers.scheduler_components.dp_attn import (
SchedulerDPAttnAdapter,
)
from sglang.srt.managers.scheduler_components.flush_wrapper import (
SchedulerFlushWrapper,
)
from sglang.srt.managers.scheduler_components.idle_sleeper import IdleSleeper
from sglang.srt.managers.scheduler_components.invariant_checker import (
SchedulerInvariantChecker,
@@ -991,7 +993,11 @@ class Scheduler(
self.last_batch: Optional[ScheduleBatch] = None
self.forward_ct = 0
self.return_health_check_ipcs: Deque[Optional[str]] = deque()
self._pending_flush: Optional[Tuple[FlushCacheReqInput, float]] = None
self.flush_wrapper = SchedulerFlushWrapper(
flush_cache=self.flush_cache,
is_fully_idle=self.is_fully_idle,
ipc_channels=self.ipc_channels,
)
self.session_controller = SessionController(self.tree_cache)
self.forward_sleep_time = None
self._engine_paused = False
@@ -1355,7 +1361,7 @@ class Scheduler(
(TokenizedEmbeddingReqInput, self.handle_embedding_request),
(BatchTokenizedGenerateReqInput, self.handle_batch_generate_request),
(BatchTokenizedEmbeddingReqInput, self.handle_batch_embedding_request),
(FlushCacheReqInput, self.flush_cache_wrapped),
(FlushCacheReqInput, self.flush_wrapper.handle),
(ClearHiCacheReqInput, self.clear_hicache_storage_wrapped),
(AttachHiCacheStorageReqInput, self.attach_hicache_storage_wrapped),
(DetachHiCacheStorageReqInput, self.detach_hicache_storage_wrapped),
@@ -1629,7 +1635,7 @@ class Scheduler(
if self.ipc_channels.recv_from_rpc is not None:
self.ipc_channels.recv_from_rpc.send_pyobj(output)
self._check_pending_flush()
self.flush_wrapper.check_pending()
if self.external_corpus_manager is not None:
self.external_corpus_manager.check_pending_load()
@@ -3010,32 +3016,6 @@ class Scheduler(
)
)
def _check_pending_flush(self):
if self._pending_flush is None:
return
pending_req, deadline = self._pending_flush
if self.is_fully_idle():
success = self.flush_cache()
self._pending_flush = None
self.ipc_channels.send_to_tokenizer.send_output(
FlushCacheReqOutput(success=success), pending_req
)
return
if time.monotonic() >= deadline:
logging.warning(
"Deferred flush_cache timed out while waiting for idle state."
)
self._pending_flush = None
self.ipc_channels.send_to_tokenizer.send_output(
FlushCacheReqOutput(
success=False, message="Timed out waiting for idle state."
),
pending_req,
)
def add_external_corpus(
self, recv_req: AddExternalCorpusReqInput
) -> Optional[AddExternalCorpusReqOutput]:
@@ -3066,25 +3046,6 @@ class Scheduler(
)
return self.external_corpus_manager.list(recv_req)
def flush_cache_wrapped(
self, recv_req: FlushCacheReqInput
) -> Optional[FlushCacheReqOutput]:
if self._pending_flush is not None:
return FlushCacheReqOutput(
success=False,
message="Another flush_cache is already in progress.",
)
timeout_s = float(recv_req.timeout_s or 0.0)
if timeout_s <= 0.0:
return FlushCacheReqOutput(success=self.flush_cache())
if self.is_fully_idle():
return FlushCacheReqOutput(success=self.flush_cache())
self._pending_flush = (recv_req, time.monotonic() + timeout_s)
return None
def clear_hicache_storage_wrapped(self, recv_req: ClearHiCacheReqInput):
if self.enable_hierarchical_cache:
self.tree_cache.clear_storage_backend()
@@ -0,0 +1,65 @@
import logging
import time
from typing import Callable, Optional, Tuple
from sglang.srt.managers.io_struct import FlushCacheReqInput, FlushCacheReqOutput
from sglang.srt.managers.scheduler_components.ipc_channels import (
SchedulerIpcChannels,
)
class SchedulerFlushWrapper:
def __init__(
self,
*,
flush_cache: Callable[[], bool],
is_fully_idle: Callable[[], bool],
ipc_channels: SchedulerIpcChannels,
) -> None:
self._flush_cache = flush_cache
self._is_fully_idle = is_fully_idle
self._ipc_channels = ipc_channels
self._pending: Optional[Tuple[FlushCacheReqInput, float]] = None
def handle(self, recv_req: FlushCacheReqInput) -> Optional[FlushCacheReqOutput]:
if self._pending is not None:
return FlushCacheReqOutput(
success=False,
message="Another flush_cache is already in progress.",
)
timeout_s = float(recv_req.timeout_s or 0.0)
if timeout_s <= 0.0:
return FlushCacheReqOutput(success=self._flush_cache())
if self._is_fully_idle():
return FlushCacheReqOutput(success=self._flush_cache())
self._pending = (recv_req, time.monotonic() + timeout_s)
return None
def check_pending(self) -> None:
if self._pending is None:
return
pending_req, deadline = self._pending
if self._is_fully_idle():
success = self._flush_cache()
self._pending = None
self._ipc_channels.send_to_tokenizer.send_output(
FlushCacheReqOutput(success=success), pending_req
)
return
if time.monotonic() >= deadline:
logging.warning(
"Deferred flush_cache timed out while waiting for idle state."
)
self._pending = None
self._ipc_channels.send_to_tokenizer.send_output(
FlushCacheReqOutput(
success=False, message="Timed out waiting for idle state."
),
pending_req,
)