Repository navigation
[Bugfix][Router] Keep routing settings across a dynamic config reload - #1167
rahulssv-ibm wants to merge 3 commits into
Conversation
e9fd35e to
ec9947d
Compare
There was a problem hiding this comment.
Code Review
This pull request introduces several new configuration fields to DynamicRouterConfig for KV-cache-aware routing and LMcache controller settings, updating the configuration parsing and logic reconfiguration to support them. Additionally, a unit test is added to verify that dynamic configuration reloads preserve these KV-cache-aware settings. The review feedback suggests improving static type checking by changing type annotations from Optional[int] to int for fields with non-None defaults, and using getattr with safe defaults when parsing arguments to ensure backward compatibility and robustness against incomplete argument objects.
| kv_aware_threshold: Optional[int] = 2000 | ||
| loadaware_beta: Optional[float] = None | ||
| prefix_min_match_length: Optional[int] = 0 | ||
| lmcache_controller_port: Optional[int] = 9000 | ||
| lmcache_controller_reply_port: Optional[int] = None | ||
| lmcache_controller_heartbeat_port: Optional[int] = None | ||
| lmcache_health_check_interval: Optional[int] = 5 | ||
| lmcache_worker_timeout: Optional[int] = 30 |
There was a problem hiding this comment.
The fields kv_aware_threshold, prefix_min_match_length, lmcache_controller_port, lmcache_health_check_interval, and lmcache_worker_timeout are required to be non-None integers for the underlying routers and managers to function correctly. Changing their type annotations from Optional[int] to int clearly communicates that these fields cannot be None and helps with static type checking.
| kv_aware_threshold: Optional[int] = 2000 | |
| loadaware_beta: Optional[float] = None | |
| prefix_min_match_length: Optional[int] = 0 | |
| lmcache_controller_port: Optional[int] = 9000 | |
| lmcache_controller_reply_port: Optional[int] = None | |
| lmcache_controller_heartbeat_port: Optional[int] = None | |
| lmcache_health_check_interval: Optional[int] = 5 | |
| lmcache_worker_timeout: Optional[int] = 30 | |
| kv_aware_threshold: int = 2000 | |
| loadaware_beta: Optional[float] = None | |
| prefix_min_match_length: int = 0 | |
| lmcache_controller_port: int = 9000 | |
| lmcache_controller_reply_port: Optional[int] = None | |
| lmcache_controller_heartbeat_port: Optional[int] = None | |
| lmcache_health_check_interval: int = 5 | |
| lmcache_worker_timeout: int = 30 |
| kv_aware_threshold=args.kv_aware_threshold, | ||
| loadaware_beta=args.loadaware_beta, | ||
| prefix_min_match_length=args.prefix_min_match_length, | ||
| lmcache_controller_port=args.lmcache_controller_port, | ||
| lmcache_controller_reply_port=args.lmcache_controller_reply_port, | ||
| lmcache_controller_heartbeat_port=args.lmcache_controller_heartbeat_port, | ||
| lmcache_health_check_interval=args.lmcache_health_check_interval, | ||
| lmcache_worker_timeout=args.lmcache_worker_timeout, |
There was a problem hiding this comment.
To ensure backward compatibility and robustness against incomplete or mock args objects (e.g., in unit tests or programmatic invocations where some of these new attributes might be missing), we should use getattr with safe defaults instead of direct attribute access.
| kv_aware_threshold=args.kv_aware_threshold, | |
| loadaware_beta=args.loadaware_beta, | |
| prefix_min_match_length=args.prefix_min_match_length, | |
| lmcache_controller_port=args.lmcache_controller_port, | |
| lmcache_controller_reply_port=args.lmcache_controller_reply_port, | |
| lmcache_controller_heartbeat_port=args.lmcache_controller_heartbeat_port, | |
| lmcache_health_check_interval=args.lmcache_health_check_interval, | |
| lmcache_worker_timeout=args.lmcache_worker_timeout, | |
| kv_aware_threshold=getattr(args, "kv_aware_threshold", 2000), | |
| loadaware_beta=getattr(args, "loadaware_beta", None), | |
| prefix_min_match_length=getattr(args, "prefix_min_match_length", 0), | |
| lmcache_controller_port=getattr(args, "lmcache_controller_port", 9000), | |
| lmcache_controller_reply_port=getattr(args, "lmcache_controller_reply_port", None), | |
| lmcache_controller_heartbeat_port=getattr(args, "lmcache_controller_heartbeat_port", None), | |
| lmcache_health_check_interval=getattr(args, "lmcache_health_check_interval", 5), | |
| lmcache_worker_timeout=getattr(args, "lmcache_worker_timeout", 30), |
chrikrah
left a comment
There was a problem hiding this comment.
@rahulssv-ibm the eight fields and the socket close are right, and the close is a leak I missed in #1141. One thing holds it back: the new test flakes.
blocking: close(linger=0) returns before libzmq releases the port, so the rebind in test_dynamic_config_reload_keeps_kvaware_settings races its I/O thread. Waiting until the pull port is bindable again inside close() took it to 0 of 60. A real reload hits the same race, though the watcher retries on the next poll.
non-blocking: the TypeError from #1141 is gone. A value set only on the command line still falls back to the default on reload (500 back to 2000 in my repro), which matches how every existing field already reloads. Keeping it would change that for all fields, so it is a separate call.
# head ddb2ffc, base 7b1164d, Python 3.12.3, pyzmq 27.2.0, vllm_router.__file__ read from each export
$ pytest -q src/tests --ignore=src/tests/perftest
273 passed, 1 warning # head (272 at base)
$ for i in $(seq 60); do pytest -q src/tests/test_kvaware_router.py -k reload; done
EADDRINUSE 5/60 # head
EADDRINUSE 0/60 # head, close() polls a bind on the pull port for up to 1s
# dynamic_config.py from base, tests kept: TypeError: ... unexpected keyword argument 'kv_aware_threshold'
# routing_logic.py from base, tests kept: zmq.error.ZMQError: Address already in use
$ python cli_only.py # stub LMCacheControllerManager; startup kvaware with threshold=500, health=7; reload a file
startup threshold=500 pull=0.0.0.0:9000 health=7
reload file keys=[] threshold=2000 pull=0.0.0.0:9000 health=5 # head
reload file keys=['kv_aware_threshold', 'lmcache_health_check_interval'] threshold=500 pull=0.0.0.0:9000 health=7
reload file keys=[] threshold=None pull=0.0.0.0:None health=None # base
# not run: a real lmcache controller, pre-commit
Once this lands I'll open the helper that keeps startup values for fields a file leaves out as its own PR, so @ruizhang0101 can decide whether reload should work that way. Could you push the test fix first?
ddb2ffc to
e95c5e9
Compare
|
@chrikrah Pushed the test fix. After closing the controller sockets, Agreed on leaving the CLI-only fallback to your follow-up PR. |
chrikrah
left a comment
There was a problem hiding this comment.
@rahulssv-ibm this replaces my request for changes from 10-22. The port wait in e95c5e9 fixes the flake I asked about, and nothing else blocks. From my perspective this can be merged.
# git archive e95c5e9, Python 3.12.3, pyzmq 27.2.0, pytest-repeat 0.9.4, vllm_router imported from the export
$ pytest -q src/tests/test_kvaware_router.py -k reload --count 500 # run twice
500 passed, 3000 deselected in 5.72s
500 passed, 3000 deselected in 5.98s
# same tree, routing_logic.py swapped for 10c5bff (before the wait), --count 300, run twice
10 failed, 290 passed, 1800 deselected in 4.21s
9 failed, 291 passed, 1800 deselected in 3.88s
$ pytest -q src/tests --ignore=src/tests/perftest
275 passed, 1 warning in 15.61s
# not run: a real lmcache controller, pre-commit
non-blocking: close() can now block for up to a second when a port stays taken. It then gives up without a log line. A debug message on timeout would tell an operator why the next bind still failed.
I will open the follow-up that keeps CLI-only values across a reload after this merges.
A dynamic config reload rebuilds the router from DynamicRouterConfig, which had no field for kv_aware_threshold, loadaware_beta, prefix_min_match_length or the five lmcache_* settings. The rebuilt router fell back to None or a default for them. For kvaware the threshold became None, so route_request raised a TypeError on the first KV lookup hit, and the LMCache controller was set up on 0.0.0.0:None. The file could not set these fields either, because DynamicRouterConfig rejected the unknown keyword. Add the eight fields with the CLI defaults, carry them in from_args and pass them to reconfigure_routing_logic. Signed-off-by: Rahul <[email protected]>
…router closes A dynamic config reload closes the running router and builds a new one. KvawareRouter.close() only cancelled the monitor task. The controller sockets stayed bound, because lmcache 0.3.11's LMCacheControllerManager has no close(). The new router then failed to bind the same port with "Address already in use", so a kvaware or loadaware router could not be reloaded. Close the controller sockets on the kv manager loop, using LMCacheControllerManager.close() where lmcache provides it. The reload test now starts a kvaware router on real ZMQ sockets and reloads it on the same ports. Signed-off-by: Rahul <[email protected]>
… on close Closing a ZMQ socket returns before libzmq's I/O thread unbinds the port. A router rebuilt right after KvawareRouter.close() could still hit "Address already in use", which made the dynamic config reload test flaky. A real reload recovered only on the next poll. After closing the controller sockets, wait up to one second until each controller port can be bound again. Signed-off-by: Rahul <[email protected]>
e95c5e9 to
e6162a2
Compare
|
I opened #1196 so the job can recover from it. In the meantime, @ruizhang0101 could you re-run the failed job here? I don't have access to re-run it. |
DynamicConfigWatcher.reconfigure_routing_logicrebuilds the router on every reload, butDynamicRouterConfighas no field forkv_aware_threshold,loadaware_beta,prefix_min_match_lengthor the fivelmcache_*settings. The rebuilt router falls back toNoneor a default for them:kvaware/loadaware: the LMCache controller is set up on0.0.0.0:None, withNonefor the health check interval and worker timeout. If the controller could be built, thekvawarethreshold would beNone, androute_requestwould raiseTypeError(len(token_ids) - None) on the first KV lookup hit. Theloadawarebeta falls back to1.0.prefixaware:prefix_min_match_lengthfalls back to0.The file can't set these fields either:
DynamicRouterConfigrejects them withTypeError: unexpected keyword argument, so every reload of such a file fails.Fixing the fields isn't enough on its own. A reload closes the running router first, but
KvawareRouter.close()only cancels the monitor task. The controller sockets stay bound, because lmcache 0.3.11'sLMCacheControllerManagerhas noclose(). So the rebuiltkvawareorloadawarerouter fails withZMQError: Address already in useon the same port, and the watcher logs a warning and retries on every poll.This PR:
from_args, and passes them toreconfigure_routing_logic.KvawareRouter.close()close the controller sockets on the kv manager loop, usingLMCacheControllerManager.close()where lmcache provides it (newer versions do). It then waits up to one second until each controller port can be bound again, because closing a ZMQ socket returns before libzmq releases the port.FIX #1141
Scope
The P/D labels (#1129) and
max_instance_failover_reroute_attempts(#984) are left to those PRs, so this one doesn't overlap them. The issue also suggests one helper that builds the routing kwargs for bothapp.pyand the watcher. That can follow once those two land. Doing it here would touch the same lines.Not changed here:
k8s_port. Akvawarerouter started with--kv-aware-threshold 500then runs with 2000, where onmainthe same reload removes the router. Keeping the value would change how every field reloads.close()also leaves the router's own kv manager loop running, so each reload of akvawareorloadawarerouter leaves one more idle thread behind.Changes
src/vllm_router/dynamic_config.py: add the eight fields toDynamicRouterConfig, carry them infrom_args, and pass them inreconfigure_routing_logic.src/vllm_router/routers/routing_logic.py: close the LMCache controller sockets inKvawareRouter.close()and wait until the controller ports can be bound again.src/tests/test_kvaware_router.py: start akvawarerouter whose controller binds real ZMQ sockets the way lmcache 0.3.11 does, then reload it on the same ports throughDynamicConfigWatcher.reconfigure_routing_logic. The test checks the threshold and the argumentsLMCacheControllerManagerreceives.Testing
main, the new test fails withTypeError: ... unexpected keyword argument 'kv_aware_threshold'. With only the config change, it fails withZMQError: Address already in use. With both changes it passes.loadaware→loadaware→loadaware, androundrobin→kvaware→kvaware→loadaware.loadaware_betais kept each time.DynamicRouterConfig.from_yamlandfrom_jsonread the new fields from a file. Aprefixawarereload keepsprefix_min_match_length.pytest src/tests --ignore=src/tests/perftest: 284 passed.pytest src/tests/test_kvaware_router.py -k reload --count 500: 500 passed, run twice.pre-commit run --files <changed files>: pass.-swhen doinggit commit[Bugfix],[Feat], and[CI].