Skip to content

fix: reject invalid token tracker weights#1030

Open
pm-ju wants to merge 1 commit into
volcano-sh:mainfrom
pm-ju:fix-token-tracker-weight-validation
Open

fix: reject invalid token tracker weights#1030
pm-ju wants to merge 1 commit into
volcano-sh:mainfrom
pm-ju:fix-token-tracker-weight-validation

Conversation

@pm-ju
Copy link
Copy Markdown
Contributor

@pm-ju pm-ju commented May 12, 2026

/kind bug

Validate token tracker weights before applying them.

Previously FAIRNESS_INPUT_TOKEN_WEIGHT, FAIRNESS_OUTPUT_TOKEN_WEIGHT, and WithTokenWeights rejected negative values but still accepted non-finite values like NaN and Inf. That could poison token accounting and fairness/rate-limit calculations with non-finite totals.

This change requires token weights to be finite and non-negative. Invalid env values fall back to defaults, while valid values are still applied.

Verification:
go test ./pkg/kthena-router/datastore

image

Signed-off-by: pm-ju <pmdevops29@gmail.com>
Copilot AI review requested due to automatic review settings May 12, 2026 17:56
@volcano-sh-bot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign hzxuzhonghu for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances token weight validation by ensuring values are finite and non-negative, specifically handling NaN and Infinite values. It introduces a helper function isValidTokenWeight, updates the configuration logic to use it, and adds comprehensive unit tests. Review feedback suggests improving log observability by including default values in warning messages and recommends deduplicating the validation logic by merging the new helper function with an existing identical one in the same package.

inputWeight = w
} else {
klog.Warningf("Invalid FAIRNESS_INPUT_TOKEN_WEIGHT: %v, using default", err)
klog.Warningf("Invalid FAIRNESS_INPUT_TOKEN_WEIGHT: %q, using default", inputWeightStr)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better observability and consistency with other configuration parsing logic in this file (such as createFairnessQueueConfig), the warning message should include the default value being used.

Suggested change
klog.Warningf("Invalid FAIRNESS_INPUT_TOKEN_WEIGHT: %q, using default", inputWeightStr)
klog.Warningf("Invalid FAIRNESS_INPUT_TOKEN_WEIGHT: %q, using default %v", inputWeightStr, defaultInputTokenWeight)

outputWeight = w
} else {
klog.Warningf("Invalid FAIRNESS_OUTPUT_TOKEN_WEIGHT: %v, using default", err)
klog.Warningf("Invalid FAIRNESS_OUTPUT_TOKEN_WEIGHT: %q, using default", outputWeightStr)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better observability and consistency with other configuration parsing logic in this file, the warning message should include the default value being used.

Suggested change
klog.Warningf("Invalid FAIRNESS_OUTPUT_TOKEN_WEIGHT: %q, using default", outputWeightStr)
klog.Warningf("Invalid FAIRNESS_OUTPUT_TOKEN_WEIGHT: %q, using default %v", outputWeightStr, defaultOutputTokenWeight)

Comment on lines +97 to +99
func isValidTokenWeight(value float64) bool {
return !math.IsNaN(value) && !math.IsInf(value, 0) && value >= 0
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The function isValidTokenWeight is identical to isValidFairnessWeight already defined in pkg/kthena-router/datastore/store.go. Since both functions perform the same validation (checking for finite, non-negative values) and reside in the same package, they should be unified into a single helper function to improve maintainability and ensure consistency across the package.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Tightens validation for token-tracker weight configuration so non-finite values (NaN/Inf) no longer poison fairness/token accounting.

Changes:

  • Add isValidTokenWeight to require weights be finite and non-negative.
  • Validate env-provided weights (FAIRNESS_INPUT_TOKEN_WEIGHT, FAIRNESS_OUTPUT_TOKEN_WEIGHT) before applying; otherwise fall back to defaults.
  • Extend tests to cover NaN/Inf and mixed-validity scenarios.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
pkg/kthena-router/datastore/token_tracker.go Adds shared finite/non-negative validation and applies it in WithTokenWeights.
pkg/kthena-router/datastore/store.go Validates env-derived weights with the new helper and adjusts warning logs.
pkg/kthena-router/datastore/token_tracker_test.go Adds unit tests covering NaN/Inf weight handling in options/config.
pkg/kthena-router/datastore/store_test.go Adds integration-style test ensuring env invalid weights fall back to defaults.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

inputWeight = w
} else {
klog.Warningf("Invalid FAIRNESS_INPUT_TOKEN_WEIGHT: %v, using default", err)
klog.Warningf("Invalid FAIRNESS_INPUT_TOKEN_WEIGHT: %q, using default", inputWeightStr)
outputWeight = w
} else {
klog.Warningf("Invalid FAIRNESS_OUTPUT_TOKEN_WEIGHT: %v, using default", err)
klog.Warningf("Invalid FAIRNESS_OUTPUT_TOKEN_WEIGHT: %q, using default", outputWeightStr)
t.Setenv("FAIRNESS_INPUT_TOKEN_WEIGHT", "NaN")
t.Setenv("FAIRNESS_OUTPUT_TOKEN_WEIGHT", strconv.FormatFloat(math.Inf(1), 'f', -1, 64))

tracker := createTokenTracker().(*InMemorySlidingWindowTokenTracker)
t.Setenv("FAIRNESS_INPUT_TOKEN_WEIGHT", "1.5")
t.Setenv("FAIRNESS_OUTPUT_TOKEN_WEIGHT", "-2")

tracker = createTokenTracker().(*InMemorySlidingWindowTokenTracker)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants