-
Notifications
You must be signed in to change notification settings - Fork 4.7k
xds: fix uint32 overflow scaling rds route runtime_fraction #9308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nvxbug
wants to merge
2
commits into
grpc:master
Choose a base branch
from
nvxbug:rds-runtime-fraction-overflow
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * | ||
| * Copyright 2026 gRPC authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package xdsresource | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| v3typepb "github.com/envoyproxy/go-control-plane/envoy/type/v3" | ||
| ) | ||
|
|
||
| // FractionalPercent is the internal representation of the xDS FractionalPercent | ||
| // proto. | ||
| type FractionalPercent struct { | ||
| Numerator uint32 | ||
| Denominator uint32 | ||
| PPM uint32 // Pre-computed and capped at 1,000,000 (100%). | ||
| } | ||
|
|
||
| // NewFractionalPercent converts the given FractionalPercent proto to its | ||
| // internal representation. A nil proto is treated as 0 out of 100. An error is | ||
| // returned for an unrecognized denominator. | ||
| func NewFractionalPercent(fp *v3typepb.FractionalPercent) (FractionalPercent, error) { | ||
| if fp == nil { | ||
| return FractionalPercent{Numerator: 0, Denominator: 100, PPM: 0}, nil | ||
| } | ||
|
|
||
| var den uint32 | ||
| switch fp.GetDenominator() { | ||
| case v3typepb.FractionalPercent_HUNDRED: | ||
| den = 100 | ||
| case v3typepb.FractionalPercent_TEN_THOUSAND: | ||
| den = 10000 | ||
| case v3typepb.FractionalPercent_MILLION: | ||
| den = 1000000 | ||
| default: | ||
| return FractionalPercent{}, fmt.Errorf("unsupported denominator: %v", fp.GetDenominator()) | ||
| } | ||
|
|
||
| num := fp.GetNumerator() | ||
| // The numerator comes from the control plane, so perform the | ||
| // multiplication in uint64 to prevent overflowing a uint32, and cap the | ||
| // result at 100%. | ||
| ppm := uint64(num) * 1000000 / uint64(den) | ||
| if ppm > 1000000 { | ||
| ppm = 1000000 | ||
| } | ||
|
|
||
| return FractionalPercent{ | ||
| Numerator: num, | ||
| Denominator: den, | ||
| PPM: uint32(ppm), | ||
| }, nil | ||
| } |
90 changes: 90 additions & 0 deletions
90
internal/xds/xdsclient/xdsresource/fractional_percent_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * | ||
| * Copyright 2026 gRPC authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package xdsresource | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| v3typepb "github.com/envoyproxy/go-control-plane/envoy/type/v3" | ||
| ) | ||
|
|
||
| func (s) TestNewFractionalPercent(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| fp *v3typepb.FractionalPercent | ||
| want FractionalPercent | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "nil is zero out of hundred", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Could you please make the subtest names look more like identifiers (using underscores or dashes instead of spaces would work). See: https://google.github.io/styleguide/go/decisions#subtest-names Thanks. |
||
| fp: nil, | ||
| want: FractionalPercent{Numerator: 0, Denominator: 100, PPM: 0}, | ||
| }, | ||
| { | ||
| name: "five percent hundred", | ||
| fp: &v3typepb.FractionalPercent{Numerator: 5, Denominator: v3typepb.FractionalPercent_HUNDRED}, | ||
| want: FractionalPercent{Numerator: 5, Denominator: 100, PPM: 50000}, | ||
| }, | ||
| { | ||
| name: "half percent ten thousand", | ||
| fp: &v3typepb.FractionalPercent{Numerator: 50, Denominator: v3typepb.FractionalPercent_TEN_THOUSAND}, | ||
| want: FractionalPercent{Numerator: 50, Denominator: 10000, PPM: 5000}, | ||
| }, | ||
| { | ||
| name: "fifty percent million", | ||
| fp: &v3typepb.FractionalPercent{Numerator: 500000, Denominator: v3typepb.FractionalPercent_MILLION}, | ||
| want: FractionalPercent{Numerator: 500000, Denominator: 1000000, PPM: 500000}, | ||
| }, | ||
| { | ||
| // numerator*million overflows a uint32 once the numerator reaches | ||
| // 4295 with the million denominator, which is only a 0.43% | ||
| // fraction. | ||
| name: "point four three percent million", | ||
| fp: &v3typepb.FractionalPercent{Numerator: 4295, Denominator: v3typepb.FractionalPercent_MILLION}, | ||
| want: FractionalPercent{Numerator: 4295, Denominator: 1000000, PPM: 4295}, | ||
| }, | ||
| { | ||
| // 429497*10000 wraps a uint32 to 2704, so a fraction meant to be | ||
| // above 100% would become ~0.27% if computed in uint32. | ||
| name: "overflowing numerator hundred capped", | ||
| fp: &v3typepb.FractionalPercent{Numerator: 429497, Denominator: v3typepb.FractionalPercent_HUNDRED}, | ||
| want: FractionalPercent{Numerator: 429497, Denominator: 100, PPM: 1000000}, | ||
| }, | ||
| { | ||
| name: "over hundred percent capped", | ||
| fp: &v3typepb.FractionalPercent{Numerator: 150, Denominator: v3typepb.FractionalPercent_HUNDRED}, | ||
| want: FractionalPercent{Numerator: 150, Denominator: 100, PPM: 1000000}, | ||
| }, | ||
| { | ||
| name: "unsupported denominator", | ||
| fp: &v3typepb.FractionalPercent{Numerator: 1, Denominator: v3typepb.FractionalPercent_DenominatorType(7)}, | ||
| wantErr: true, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := NewFractionalPercent(tt.fp) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Fatalf("NewFractionalPercent(%v) returned err %v, wantErr %v", tt.fp, err, tt.wantErr) | ||
| } | ||
| if got != tt.want { | ||
| t.Errorf("NewFractionalPercent(%v) = %+v, want %+v", tt.fp, got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mind adding a TODO here to say that this validation should happen at parsing time instead and the filter configuration must be NACKed if an unrecognized denominator is seen.
I don't want to ask you to do this change as part of this PR since it would involve a bunch of refactoring in the fault filter to actually convert the proto into an internal representation at config parsing time (and perform all validation checks) and then only use the internal representation during runtime.