Fix router panic when all target model weights are zero#727
Fix router panic when all target model weights are zero#727WHOIM1205 wants to merge 1 commit intovolcano-sh:mainfrom
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Summary of ChangesHello @WHOIM1205, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical vulnerability in the router's weighted model selection mechanism. Previously, if all configured target model weights were zero, the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
/assign @hzxuzhonghu |
There was a problem hiding this comment.
Code Review
This pull request effectively addresses a critical panic caused by a divide-by-zero error in selectFromWeightedSlice when all model weights are zero. The fix is minimal and correct, and the new unit tests provide good coverage for the edge cases.
I've identified a related latent issue where handling an empty slice of weights could lead to a panic in the calling function. I've provided two high-severity comments with suggestions to make the implementation more robust by explicitly handling this case and updating the corresponding test.
FAUST-BENCHOU
left a comment
There was a problem hiding this comment.
Anyway we should wait for maintainer's advice
| // Guard against divide-by-zero when all weights are zero | ||
| if totalWeight == 0 { | ||
| return 0 |
There was a problem hiding this comment.
Check 0 idea is reasonable.But i dont think we should return zero here(It will cover up configuration errors and directly select the first target.)
for example:
rules:
- name: my-rule
targetModels:
- modelServerName: server-a
weight: 0
- modelServerName: server-b
weight: 0
kthena/pkg/kthena-router/datastore/store.go
Lines 941 to 950 in a8c9193
selectDestination will select targets[0] (server-a)
Maybe we can add check in toWeightedSlice to throw an error
kthena/pkg/kthena-router/datastore/store.go
Lines 952 to 974 in a8c9193
if isWeighted && totalWeight == 0 {
return nil, fmt.Errorf("the sum of weights in targetModels must be greater than 0")
}
to inform users that their coniguration is not right
There was a problem hiding this comment.
thanks for the suggestion
for this pr i’m intentionally keeping the fix scoped to preventing the runtime panic on the request path returning a deterministic fallback here avoids crashing the router for edgecase or transitional configurations
validating or rejecting misconfiguration earlier (e.g.,, at config or controller level) makes sense but i’d prefer to keep this change defensive and minimal rather than introducing a new panic in the routing path
There was a problem hiding this comment.
rules:
- name: my-rule
targetModels:
- modelServerName: server-a
weight: 0
- modelServerName: server-b
weight: 0
We should deny this CR in validating webhook actually.
if isWeighted && totalWeight == 0 {
return nil, fmt.Errorf("the sum of weights in targetModels must be greater than 0")
}Also this defensive validating make sense to me.
There was a problem hiding this comment.
That makes sense
for this pr i kept the change focused on avoiding the runtime panic in the router path adding validation in the webhook to reject all zero weights sounds like the right place for enforcing configuration correctness and i’m happy to follow up with a separate change for that
There was a problem hiding this comment.
I agree with @YaoZengzeng YOu should verify it in validation as well
If all the weight is set to 0. Why do you choose targets[0]
There was a problem hiding this comment.
in the all zero case returning targets[0] is just a deterministic fallback to keep the router from crashing on the request path not a correct routing decision
I agree this configuration should ideally be rejected earlier via validation this pr is intentionally scoped to making the runtime path defensive
Happy to align if you’d prefer validation to be included here.
|
@YaoZengzeng is there anything i can change in this pr |
|
@hzxuzhonghu is there anything i can change in this pr |
|
/cc @YaoZengzeng PTAL |
|
@LiZhenCheng9527: GitHub didn't allow me to request PR reviews from the following users: PTAL. Note that only volcano-sh members and repo collaborators can review this PR, and authors cannot review their own PRs. DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
Summary
This PR fixes a critical router panic caused by a divide-by-zero error when selecting a target model from a weighted list where all weights are set to zero
A minimal defensive guard has been added to prevent
rand.Intn(0)from panicking, along with focused unit tests to cover the affected edge cases.Issue Description
When a
ModelRouteis configured withTargetModelentries whose weights all evaluate to0, the router crashes at runtime.This happens because
selectFromWeightedSlicecomputes a total weight of0and passes it directly torand.Intn, which panics immediately.Such configurations are valid and realistic during canary rollbacks, maintenance windows, or temporary target disablement.
Since this code runs on the request hot path, the panic causes a full router crash and impacts all traffic.
What Was Fixed
selectFromWeightedSliceto safely handle the case where the total weight is zeroThe fix is intentionally minimal and localized.
Impact of the Fix
Code Changes
Files modified:
pkg/kthena-router/datastore/store.goselectFromWeightedSliceto handletotalWeight == 0pkg/kthena-router/datastore/store_test.goTest Verification
Unit tests were added using Go’s table-driven testing pattern, covering: