Skip to content

Commit

Permalink
xds/xdsclient: add sum of EDS locality weights check (#5703)
Browse files Browse the repository at this point in the history
  • Loading branch information
erni27 committed Oct 10, 2022
1 parent c03925d commit c672451
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
16 changes: 11 additions & 5 deletions xds/internal/xdsclient/xdsresource/unmarshal_eds.go
Expand Up @@ -19,6 +19,7 @@ package xdsresource

import (
"fmt"
"math"
"net"
"strconv"

Expand Down Expand Up @@ -118,6 +119,7 @@ func parseEDSRespProto(m *v3endpointpb.ClusterLoadAssignment, logger *grpclog.Pr
ret.Drops = append(ret.Drops, parseDropPolicy(dropPolicy))
}
priorities := make(map[uint32]map[string]bool)
sumOfWeights := make(map[uint32]uint64)
for _, locality := range m.Endpoints {
l := locality.GetLocality()
if l == nil {
Expand All @@ -128,17 +130,21 @@ func parseEDSRespProto(m *v3endpointpb.ClusterLoadAssignment, logger *grpclog.Pr
logger.Warningf("Ignoring locality %s with weight 0", pretty.ToJSON(l))
continue
}
lid := internal.LocalityID{
Region: l.Region,
Zone: l.Zone,
SubZone: l.SubZone,
}
priority := locality.GetPriority()
sumOfWeights[priority] += uint64(weight)
if sumOfWeights[priority] > math.MaxUint32 {
return EndpointsUpdate{}, fmt.Errorf("sum of weights of localities at the same priority %d exceeded maximal value", priority)
}
localitiesWithPriority := priorities[priority]
if localitiesWithPriority == nil {
localitiesWithPriority = make(map[string]bool)
priorities[priority] = localitiesWithPriority
}
lid := internal.LocalityID{
Region: l.Region,
Zone: l.Zone,
SubZone: l.SubZone,
}
lidStr, _ := lid.ToString()
if localitiesWithPriority[lidStr] {
return EndpointsUpdate{}, fmt.Errorf("duplicate locality %s with the same priority %v", lidStr, priority)
Expand Down
12 changes: 12 additions & 0 deletions xds/internal/xdsclient/xdsresource/unmarshal_eds_test.go
Expand Up @@ -99,6 +99,18 @@ func (s) TestEDSParseRespProto(t *testing.T) {
}(),
want: EndpointsUpdate{},
},
{
name: "max sum of weights at the same priority exceeded",
m: func() *v3endpointpb.ClusterLoadAssignment {
clab0 := newClaBuilder("test", nil)
clab0.addLocality("locality-1", 1, 0, []string{"addr1:314"}, nil)
clab0.addLocality("locality-2", 4294967295, 1, []string{"addr2:159"}, nil)
clab0.addLocality("locality-3", 1, 1, []string{"addr2:88"}, nil)
return clab0.Build()
}(),
want: EndpointsUpdate{},
wantErr: true,
},
{
name: "good",
m: func() *v3endpointpb.ClusterLoadAssignment {
Expand Down

0 comments on commit c672451

Please sign in to comment.