Skip to content

Commit 5ac9042

Browse files
authored
balancer/rls: allow maxAge to exceed 5m if staleAge is set (#8137)
1 parent bdba42f commit 5ac9042

File tree

2 files changed

+90
-11
lines changed

2 files changed

+90
-11
lines changed

balancer/rls/config.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,27 +220,43 @@ func parseRLSProto(rlsProto *rlspb.RouteLookupConfig) (*lbConfig, error) {
220220

221221
// Validations performed here:
222222
// - if `max_age` > 5m, it should be set to 5 minutes
223+
// only if stale age is not set
223224
// - if `stale_age` > `max_age`, ignore it
224225
// - if `stale_age` is set, then `max_age` must also be set
226+
maxAgeSet := false
225227
maxAge, err := convertDuration(rlsProto.GetMaxAge())
226228
if err != nil {
227229
return nil, fmt.Errorf("rls: failed to parse max_age in route lookup config %+v: %v", rlsProto, err)
228230
}
231+
if maxAge == 0 {
232+
maxAge = maxMaxAge
233+
} else {
234+
maxAgeSet = true
235+
}
236+
237+
staleAgeSet := false
229238
staleAge, err := convertDuration(rlsProto.GetStaleAge())
230239
if err != nil {
231240
return nil, fmt.Errorf("rls: failed to parse staleAge in route lookup config %+v: %v", rlsProto, err)
232241
}
233-
if staleAge != 0 && maxAge == 0 {
242+
if staleAge == 0 {
243+
staleAge = maxMaxAge
244+
} else {
245+
staleAgeSet = true
246+
}
247+
248+
if staleAgeSet && !maxAgeSet {
234249
return nil, fmt.Errorf("rls: stale_age is set, but max_age is not in route lookup config %+v", rlsProto)
235250
}
236-
if staleAge >= maxAge {
237-
logger.Infof("rls: stale_age %v is not less than max_age %v, ignoring it", staleAge, maxAge)
238-
staleAge = 0
251+
if staleAge > maxMaxAge {
252+
staleAge = maxMaxAge
239253
}
240-
if maxAge == 0 || maxAge > maxMaxAge {
241-
logger.Infof("rls: max_age in route lookup config is %v, using %v", maxAge, maxMaxAge)
254+
if !staleAgeSet && maxAge > maxMaxAge {
242255
maxAge = maxMaxAge
243256
}
257+
if staleAge > maxAge {
258+
staleAge = maxAge
259+
}
244260

245261
// `cache_size_bytes` field must have a value greater than 0, and if its
246262
// value is greater than 5M, we cap it at 5M

balancer/rls/config_test.go

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ func (s) TestParseConfig(t *testing.T) {
6060
// - A top-level unknown field should not fail.
6161
// - An unknown field in routeLookupConfig proto should not fail.
6262
// - lookupServiceTimeout is set to its default value, since it is not specified in the input.
63-
// - maxAge is set to maxMaxAge since the value is too large in the input.
64-
// - staleAge is ignore because it is higher than maxAge in the input.
63+
// - maxAge is clamped to maxMaxAge if staleAge is not set.
64+
// - staleAge is ignored because it is higher than maxAge in the input.
6565
// - cacheSizeBytes is greater than the hard upper limit of 5MB
6666
desc: "with transformations 1",
6767
input: []byte(`{
@@ -87,9 +87,9 @@ func (s) TestParseConfig(t *testing.T) {
8787
}`),
8888
wantCfg: &lbConfig{
8989
lookupService: ":///target",
90-
lookupServiceTimeout: 10 * time.Second, // This is the default value.
91-
maxAge: 5 * time.Minute, // This is max maxAge.
92-
staleAge: time.Duration(0), // StaleAge is ignore because it was higher than maxAge.
90+
lookupServiceTimeout: 10 * time.Second, // This is the default value.
91+
maxAge: 500 * time.Second, // Max age is not clamped when stale age is set.
92+
staleAge: 300 * time.Second, // StaleAge is clamped because it was higher than maxMaxAge.
9393
cacheSizeBytes: maxCacheSize,
9494
defaultTarget: "passthrough:///default",
9595
childPolicyName: "grpclb",
@@ -100,6 +100,69 @@ func (s) TestParseConfig(t *testing.T) {
100100
},
101101
},
102102
},
103+
{
104+
desc: "maxAge not clamped when staleAge is set",
105+
input: []byte(`{
106+
"routeLookupConfig": {
107+
"grpcKeybuilders": [{
108+
"names": [{"service": "service", "method": "method"}],
109+
"headers": [{"key": "k1", "names": ["v1"]}]
110+
}],
111+
"lookupService": ":///target",
112+
"maxAge" : "500s",
113+
"staleAge": "200s",
114+
"cacheSizeBytes": 100000000
115+
},
116+
"childPolicy": [
117+
{"grpclb": {"childPolicy": [{"pickfirst": {}}]}}
118+
],
119+
"childPolicyConfigTargetFieldName": "serviceName"
120+
}`),
121+
wantCfg: &lbConfig{
122+
lookupService: ":///target",
123+
lookupServiceTimeout: 10 * time.Second, // This is the default value.
124+
maxAge: 500 * time.Second, // Max age is not clamped when stale age is set.
125+
staleAge: 200 * time.Second, // This is stale age within maxMaxAge.
126+
cacheSizeBytes: maxCacheSize,
127+
childPolicyName: "grpclb",
128+
childPolicyTargetField: "serviceName",
129+
childPolicyConfig: map[string]json.RawMessage{
130+
"childPolicy": json.RawMessage(`[{"pickfirst": {}}]`),
131+
"serviceName": json.RawMessage(childPolicyTargetFieldVal),
132+
},
133+
},
134+
},
135+
{
136+
desc: "maxAge clamped when staleAge is not set",
137+
input: []byte(`{
138+
"routeLookupConfig": {
139+
"grpcKeybuilders": [{
140+
"names": [{"service": "service", "method": "method"}],
141+
"headers": [{"key": "k1", "names": ["v1"]}]
142+
}],
143+
"lookupService": ":///target",
144+
"maxAge" : "500s",
145+
"cacheSizeBytes": 100000000
146+
},
147+
"childPolicy": [
148+
{"grpclb": {"childPolicy": [{"pickfirst": {}}]}}
149+
],
150+
"childPolicyConfigTargetFieldName": "serviceName"
151+
}`),
152+
wantCfg: &lbConfig{
153+
lookupService: ":///target",
154+
lookupServiceTimeout: 10 * time.Second, // This is the default value.
155+
maxAge: 300 * time.Second, // Max age is clamped when stale age is not set.
156+
staleAge: 300 * time.Second,
157+
cacheSizeBytes: maxCacheSize,
158+
childPolicyName: "grpclb",
159+
childPolicyTargetField: "serviceName",
160+
childPolicyConfig: map[string]json.RawMessage{
161+
"childPolicy": json.RawMessage(`[{"pickfirst": {}}]`),
162+
"serviceName": json.RawMessage(childPolicyTargetFieldVal),
163+
},
164+
},
165+
},
103166
{
104167
desc: "without transformations",
105168
input: []byte(`{

0 commit comments

Comments
 (0)