Skip to content
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

ringhash: allow overriding max ringhash size via environment variable #5884

Merged
merged 4 commits into from Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions internal/envconfig/envconfig.go
Expand Up @@ -21,6 +21,7 @@ package envconfig

import (
"os"
"strconv"
"strings"
)

Expand All @@ -37,3 +38,17 @@ var (
// ("GRPC_GO_ADVERTISE_COMPRESSORS" is not "false").
AdvertiseCompressors = !strings.EqualFold(os.Getenv(advertiseCompressorsStr), "false")
)

func uint64FromEnv(envVar string, def, min, max uint64) uint64 {
v, err := strconv.ParseUint(os.Getenv(envVar), 10, 64)
if err != nil {
return def
}
if v < min {
return min
}
if v > max {
return max
}
return v
}
74 changes: 74 additions & 0 deletions internal/envconfig/envconfig_test.go
@@ -0,0 +1,74 @@
/*
*
* Copyright 2022 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 envconfig

import (
"os"
"testing"

"google.golang.org/grpc/internal/grpctest"
)

type s struct {
grpctest.Tester
}

func Test(t *testing.T) {
grpctest.RunSubTests(t, s{})
}

func (s) TestUint64FromEnv(t *testing.T) {

var testCases = []struct {
name string
val string
def, min, max uint64
want uint64
}{
{
name: "error parsing; want default",
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we change the test names to be in snake_case instead, as the go test command would do that anyways, and will ;s in there, the test name might look weird.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is fine IMO. AFAICT there's no real guidance given on this by the Go team, and the name is essentially just a convenience & informational. No matter what we name things, they will be copy-pasteable to re-run. That said, the "want"s are pretty obvious, especially given the "want" declaration, so I just removed them.

val: "asdf", def: 5, want: 5,
}, {
name: "unset; want default",
val: "", def: 5, want: 5,
}, {
name: "too low; want min",
val: "5", min: 10, want: 10,
}, {
name: "too high; want max",
val: "5", max: 2, want: 2,
}, {
name: "in range; good",
val: "17391", def: 13000, min: 12000, max: 18000, want: 17391,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
const testVar = "testvar"
if tc.val == "" {
os.Unsetenv(testVar)
Comment on lines +64 to +65
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need this special case for empty string? Since the code which reads the env var uses os.GetEnv(), and does not distinguish between whether the env var is set to empty string vs being unset, can't we simply set it to the empty string here (for the appropriate test case)?

Copy link
Member Author

Choose a reason for hiding this comment

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

From a blackbox testing perspective, it's an interesting, specific test. But from a whitebox perspective, it's no different. So I could go either way, but I'm inclined to keep it with the former in mind.

} else {
os.Setenv(testVar, tc.val)
}
if got := uint64FromEnv(testVar, tc.def, tc.min, tc.max); got != tc.want {
t.Errorf("uint64FromEnv(%q(=%q), %v, %v, %v) = %v; want %v", testVar, tc.val, tc.def, tc.min, tc.max, got, tc.want)
}
})
}
}
6 changes: 6 additions & 0 deletions internal/envconfig/xds.go
Expand Up @@ -99,3 +99,9 @@ var (
// C2PResolverTestOnlyTrafficDirectorURI is the TD URI for testing.
C2PResolverTestOnlyTrafficDirectorURI = os.Getenv(c2pResolverTestOnlyTrafficDirectorURIEnv)
)

// XDSRingHashLimit indicates the maximum ring size which defaults to 4096 but
// may be overridden by setting the environment variable
// "GRPC_XDS_RING_HASH_LIMIT". A hard maximum limit of 8MB and minimum limit
Copy link
Contributor

Choose a reason for hiding this comment

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

RING_HASH_LIMIT is somewhat ambiguous, since this limit really controls the max number of ring hash entries, and only indirectly controls memory (e.g. per-entry memory consumption could be reduced with a revised layout).

Note too the A hard maximum limit of 8MB should say something like A hard maximum limit of 8 million entries, which can consume ~256MB.

Copy link
Member Author

@dfawley dfawley Dec 21, 2022

Choose a reason for hiding this comment

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

RING_HASH_LIMIT

Can you suggest something better? I wanted to avoid something too verbose. GRPC_XDS_RING_SIZE_LIMIT is probably too vague, and GRPC_XDS_RING_HASH_RING_SIZE_LIMIT is getting pretty long. Is GRPC_XDS_RING_HASH_SIZE_LIMIT still too ambiguous?

Note too the A hard maximum limit of 8MB should say something like A hard maximum limit of 8 million entries, which can consume ~256MB

Well, it's apparently not 8 million either, it's 8,388,608. Why did we choose that number (for Java) btw?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh wait.. we actually NACK for configs > 8M. So probably this is irrelevant to even mention here?

Copy link
Contributor

Choose a reason for hiding this comment

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

In C++, their's a channel option called GRPC_ARG_RING_HASH_LB_RING_SIZE_CAP.

In Java, the global is setRingSizeCap.

So maybe use GRPC_XDS_RING_HASH_CAP ?

Oh wait.. we actually NACK for configs > 8M. So probably this is irrelevant to even mention here?

Yeah I think it's not really relevant. Though I suppose we could mention that GRPC_XDS_RING_HASH_CAP does not influence the validation of max ring size configuration.

Copy link
Member Author

Choose a reason for hiding this comment

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

So maybe use GRPC_XDS_RING_HASH_CAP?

This is fine since it matches the other languages re: cap/limit, though I find "cap" to be less formal/precise than "limit".

Changed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, should we remove "xds" from the name, too, since the LB policy name doesn't have "xds" in it?

Copy link
Member Author

Choose a reason for hiding this comment

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

(I went ahead and did that in another commit; LMK.)

// of 1 is also enforced.
var XDSRingHashLimit = uint64FromEnv("GRPC_XDS_RING_HASH_LIMIT", 4096, 1, 8*1024*1024)
15 changes: 7 additions & 8 deletions xds/internal/balancer/ringhash/config.go
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"fmt"

"google.golang.org/grpc/internal/envconfig"
"google.golang.org/grpc/serviceconfig"
)

Expand All @@ -36,8 +37,6 @@ type LBConfig struct {
const (
defaultMinSize = 1024
defaultMaxSize = 4096
// TODO(apolcyn): make makeRingSizeCap configurable, with either a dial option or global setting
maxRingSizeCap = 4096
)

func parseConfig(c json.RawMessage) (*LBConfig, error) {
Expand All @@ -51,14 +50,14 @@ func parseConfig(c json.RawMessage) (*LBConfig, error) {
if cfg.MaxRingSize == 0 {
cfg.MaxRingSize = defaultMaxSize
}
if cfg.MinRingSize > maxRingSizeCap {
cfg.MinRingSize = maxRingSizeCap
}
if cfg.MaxRingSize > maxRingSizeCap {
cfg.MaxRingSize = maxRingSizeCap
}
if cfg.MinRingSize > cfg.MaxRingSize {
return nil, fmt.Errorf("min %v is greater than max %v", cfg.MinRingSize, cfg.MaxRingSize)
}
if cfg.MinRingSize > envconfig.XDSRingHashLimit {
cfg.MinRingSize = envconfig.XDSRingHashLimit
}
if cfg.MaxRingSize > envconfig.XDSRingHashLimit {
cfg.MaxRingSize = envconfig.XDSRingHashLimit
}
return &cfg, nil
}
43 changes: 39 additions & 4 deletions xds/internal/balancer/ringhash/config_test.go
Expand Up @@ -22,14 +22,16 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"google.golang.org/grpc/internal/envconfig"
)

func (s) TestParseConfig(t *testing.T) {
tests := []struct {
name string
js string
want *LBConfig
wantErr bool
name string
js string
envConfigLimit uint64
want *LBConfig
wantErr bool
}{
{
name: "OK",
Expand All @@ -52,9 +54,42 @@ func (s) TestParseConfig(t *testing.T) {
want: nil,
wantErr: true,
},
{
name: "min greater than max greater than global limit",
js: `{"minRingSize": 6000, "maxRingSize": 5000}`,
want: nil,
wantErr: true,
},
{
name: "max greater than global limit",
js: `{"minRingSize": 1, "maxRingSize": 6000}`,
want: &LBConfig{MinRingSize: 1, MaxRingSize: 4096},
},
{
name: "min and max greater than global limit",
js: `{"minRingSize": 5000, "maxRingSize": 6000}`,
want: &LBConfig{MinRingSize: 4096, MaxRingSize: 4096},
},
{
name: "min and max less than raised global limit",
js: `{"minRingSize": 5000, "maxRingSize": 6000}`,
envConfigLimit: 8000,
want: &LBConfig{MinRingSize: 5000, MaxRingSize: 6000},
},
{
name: "min and max greater than raised global limit",
js: `{"minRingSize": 10000, "maxRingSize": 10000}`,
envConfigLimit: 8000,
want: &LBConfig{MinRingSize: 8000, MaxRingSize: 8000},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envConfigLimit != 0 {
old := envconfig.XDSRingHashLimit
defer func() { envconfig.XDSRingHashLimit = old }()
envconfig.XDSRingHashLimit = tt.envConfigLimit
}
got, err := parseConfig([]byte(tt.js))
if (err != nil) != tt.wantErr {
t.Errorf("parseConfig() error = %v, wantErr %v", err, tt.wantErr)
Expand Down