Skip to content

Commit 718c4d8

Browse files
authored
xds: Make locality ID string representation consistent with A78 (#8256)
1 parent eb4b687 commit 718c4d8

File tree

12 files changed

+28
-63
lines changed

12 files changed

+28
-63
lines changed

stats/opentelemetry/e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ func (s) TestWRRMetrics(t *testing.T) {
698698
}()
699699

700700
targetAttr := attribute.String("grpc.target", target)
701-
localityAttr := attribute.String("grpc.lb.locality", `{"region":"region-1","zone":"zone-1","subZone":"subzone-1"}`)
701+
localityAttr := attribute.String("grpc.lb.locality", `{region="region-1", zone="zone-1", sub_zone="subzone-1"}`)
702702

703703
wantMetrics := []metricdata.Metrics{
704704
{

test/xds/xds_telemetry_labels_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const serviceNameValue = "grpc-service"
4646
const serviceNamespaceValue = "grpc-service-namespace"
4747

4848
const localityKey = "grpc.lb.locality"
49-
const localityValue = `{"region":"region-1","zone":"zone-1","subZone":"subzone-1"}`
49+
const localityValue = `{region="region-1", zone="zone-1", sub_zone="subzone-1"}`
5050

5151
// TestTelemetryLabels tests that telemetry labels from CDS make their way to
5252
// the stats handler. The stats handler sets the mutable context value that the

xds/internal/balancer/clusterimpl/balancer_test.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (s) TestDropByCategory(t *testing.T) {
185185
TotalDrops: dropCount,
186186
Drops: map[string]uint64{dropReason: dropCount},
187187
LocalityStats: map[string]load.LocalityData{
188-
assertString(xdsinternal.LocalityID{}.ToString): {RequestStats: load.RequestData{
188+
xdsinternal.LocalityID{}.ToString(): {RequestStats: load.RequestData{
189189
Succeeded: (rpcCount - dropCount) * 3 / 4,
190190
Errored: (rpcCount - dropCount) / 4,
191191
Issued: rpcCount - dropCount,
@@ -251,7 +251,7 @@ func (s) TestDropByCategory(t *testing.T) {
251251
TotalDrops: dropCount2,
252252
Drops: map[string]uint64{dropReason2: dropCount2},
253253
LocalityStats: map[string]load.LocalityData{
254-
assertString(xdsinternal.LocalityID{}.ToString): {RequestStats: load.RequestData{
254+
xdsinternal.LocalityID{}.ToString(): {RequestStats: load.RequestData{
255255
Succeeded: rpcCount - dropCount2,
256256
Issued: rpcCount - dropCount2,
257257
}},
@@ -373,7 +373,7 @@ func (s) TestDropCircuitBreaking(t *testing.T) {
373373
Service: testServiceName,
374374
TotalDrops: uint64(maxRequest),
375375
LocalityStats: map[string]load.LocalityData{
376-
assertString(xdsinternal.LocalityID{}.ToString): {RequestStats: load.RequestData{
376+
xdsinternal.LocalityID{}.ToString(): {RequestStats: load.RequestData{
377377
Succeeded: uint64(rpcCount - maxRequest),
378378
Errored: 50,
379379
Issued: uint64(rpcCount - maxRequest + 50),
@@ -708,8 +708,8 @@ func (s) TestLoadReporting(t *testing.T) {
708708
if sd.Cluster != testClusterName || sd.Service != testServiceName {
709709
t.Fatalf("got unexpected load for %q, %q, want %q, %q", sd.Cluster, sd.Service, testClusterName, testServiceName)
710710
}
711-
testLocalityJSON, _ := testLocality.ToString()
712-
localityData, ok := sd.LocalityStats[testLocalityJSON]
711+
testLocalityStr := testLocality.ToString()
712+
localityData, ok := sd.LocalityStats[testLocalityStr]
713713
if !ok {
714714
t.Fatalf("loads for %v not found in store", testLocality)
715715
}
@@ -1016,11 +1016,3 @@ func (s) TestPickerUpdatedSynchronouslyOnConfigUpdate(t *testing.T) {
10161016
t.Fatal("Timed out waiting for client conn update to be completed.")
10171017
}
10181018
}
1019-
1020-
func assertString(f func() (string, error)) string {
1021-
s, err := f()
1022-
if err != nil {
1023-
panic(err.Error())
1024-
}
1025-
return s
1026-
}

xds/internal/balancer/clusterimpl/picker.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,9 @@ func (d *picker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
139139
// This OK check also covers the case err!=nil, because SubConn will be
140140
// nil.
141141
pr.SubConn = scw.SubConn
142-
var e error
143142
// If locality ID isn't found in the wrapper, an empty locality ID will
144143
// be used.
145-
lIDStr, e = scw.localityID().ToString()
146-
if e != nil {
147-
logger.Infof("failed to marshal LocalityID: %#v, loads won't be reported", scw.localityID())
148-
}
144+
lIDStr = scw.localityID().ToString()
149145
}
150146

151147
if err != nil {

xds/internal/balancer/clusterresolver/configbuilder.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,7 @@ func priorityLocalitiesToClusterImpl(localities []xdsresource.Locality, priority
257257
if locality.Weight != 0 {
258258
lw = locality.Weight
259259
}
260-
localityStr, err := locality.ID.ToString()
261-
if err != nil {
262-
localityStr = fmt.Sprintf("%+v", locality.ID)
263-
}
260+
localityStr := locality.ID.ToString()
264261
for _, endpoint := range locality.Endpoints {
265262
// Filter out all "unhealthy" endpoints (unknown and healthy are
266263
// both considered to be healthy:

xds/internal/balancer/clusterresolver/configbuilder_test.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -633,22 +633,14 @@ func TestPriorityLocalitiesToClusterImpl(t *testing.T) {
633633
}
634634
}
635635

636-
func assertString(f func() (string, error)) string {
637-
s, err := f()
638-
if err != nil {
639-
panic(err.Error())
640-
}
641-
return s
642-
}
643-
644636
func testEndpointWithAttrs(addrStrs []string, localityWeight, endpointWeight uint32, priority string, lID *internal.LocalityID) resolver.Endpoint {
645637
endpoint := resolver.Endpoint{}
646638
for _, a := range addrStrs {
647639
endpoint.Addresses = append(endpoint.Addresses, resolver.Address{Addr: a})
648640
}
649641
path := []string{priority}
650642
if lID != nil {
651-
path = append(path, assertString(lID.ToString))
643+
path = append(path, lID.ToString())
652644
endpoint = internal.SetLocalityIDInEndpoint(endpoint, *lID)
653645
}
654646
endpoint = hierarchy.SetInEndpoint(endpoint, path)

xds/internal/balancer/wrrlocality/balancer.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,7 @@ func (b *wrrLocalityBalancer) UpdateClientConnState(s balancer.ClientConnState)
167167
// shouldn't happen though (this attribute that is set actually gets
168168
// used to build localities in the first place), and thus don't error
169169
// out, and just build a weighted target with undefined behavior.
170-
locality, err := internal.GetLocalityID(addr).ToString()
171-
if err != nil {
172-
// Should never happen.
173-
logger.Errorf("Failed to marshal LocalityID: %v, skipping this locality in weighted target")
174-
}
170+
locality := internal.GetLocalityID(addr).ToString()
175171
ai, ok := getAddrInfo(addr)
176172
if !ok {
177173
return fmt.Errorf("xds_wrr_locality: missing locality weight information in address %q", addr)

xds/internal/balancer/wrrlocality/balancer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,13 @@ func (s) TestUpdateClientConnState(t *testing.T) {
218218
// child layer.
219219
wantWtCfg := &weightedtarget.LBConfig{
220220
Targets: map[string]weightedtarget.Target{
221-
"{\"region\":\"region-1\",\"zone\":\"zone-1\",\"subZone\":\"subzone-1\"}": {
221+
"{region=\"region-1\", zone=\"zone-1\", sub_zone=\"subzone-1\"}": {
222222
Weight: 2,
223223
ChildPolicy: &internalserviceconfig.BalancerConfig{
224224
Name: "round_robin",
225225
},
226226
},
227-
"{\"region\":\"region-2\",\"zone\":\"zone-2\",\"subZone\":\"subzone-2\"}": {
227+
"{region=\"region-2\", zone=\"zone-2\", sub_zone=\"subzone-2\"}": {
228228
Weight: 1,
229229
ChildPolicy: &internalserviceconfig.BalancerConfig{
230230
Name: "round_robin",

xds/internal/internal.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package internal
2121

2222
import (
23-
"encoding/json"
2423
"fmt"
2524

2625
"google.golang.org/grpc/resolver"
@@ -36,14 +35,10 @@ type LocalityID struct {
3635
SubZone string `json:"subZone,omitempty"`
3736
}
3837

39-
// ToString generates a string representation of LocalityID by marshalling it into
40-
// json. Not calling it String() so printf won't call it.
41-
func (l LocalityID) ToString() (string, error) {
42-
b, err := json.Marshal(l)
43-
if err != nil {
44-
return "", err
45-
}
46-
return string(b), nil
38+
// ToString generates a string representation of LocalityID in the format
39+
// specified in gRFC A76. Not calling it String() so printf won't call it.
40+
func (l LocalityID) ToString() string {
41+
return fmt.Sprintf("{region=%q, zone=%q, sub_zone=%q}", l.Region, l.Zone, l.SubZone)
4742
}
4843

4944
// Equal allows the values to be compared by Attributes.Equal.
@@ -60,10 +55,10 @@ func (l LocalityID) Empty() bool {
6055
return l.Region == "" && l.Zone == "" && l.SubZone == ""
6156
}
6257

63-
// LocalityIDFromString converts a json representation of locality, into a
64-
// LocalityID struct.
58+
// LocalityIDFromString converts a string representation of locality as
59+
// specified in gRFC A76, into a LocalityID struct.
6560
func LocalityIDFromString(s string) (ret LocalityID, _ error) {
66-
err := json.Unmarshal([]byte(s), &ret)
61+
_, err := fmt.Sscanf(s, "{region=%q, zone=%q, sub_zone=%q}", &ret.Region, &ret.Zone, &ret.SubZone)
6762
if err != nil {
6863
return LocalityID{}, fmt.Errorf("%s is not a well formatted locality ID, error: %v", s, err)
6964
}

xds/internal/internal_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (s) TestLocalityMatchProtoMessage(t *testing.T) {
7171
}
7272
}
7373

74-
func TestLocalityToAndFromJSON(t *testing.T) {
74+
func TestLocalityToAndFromString(t *testing.T) {
7575
tests := []struct {
7676
name string
7777
localityID LocalityID
@@ -81,25 +81,22 @@ func TestLocalityToAndFromJSON(t *testing.T) {
8181
{
8282
name: "3 fields",
8383
localityID: LocalityID{Region: "r:r", Zone: "z#z", SubZone: "s^s"},
84-
str: `{"region":"r:r","zone":"z#z","subZone":"s^s"}`,
84+
str: `{region="r:r", zone="z#z", sub_zone="s^s"}`,
8585
},
8686
{
8787
name: "2 fields",
8888
localityID: LocalityID{Region: "r:r", Zone: "z#z"},
89-
str: `{"region":"r:r","zone":"z#z"}`,
89+
str: `{region="r:r", zone="z#z", sub_zone=""}`,
9090
},
9191
{
9292
name: "1 field",
9393
localityID: LocalityID{Region: "r:r"},
94-
str: `{"region":"r:r"}`,
94+
str: `{region="r:r", zone="", sub_zone=""}`,
9595
},
9696
}
9797
for _, tt := range tests {
9898
t.Run(tt.name, func(t *testing.T) {
99-
gotStr, err := tt.localityID.ToString()
100-
if err != nil {
101-
t.Errorf("failed to marshal LocalityID: %#v", tt.localityID)
102-
}
99+
gotStr := tt.localityID.ToString()
103100
if gotStr != tt.str {
104101
t.Errorf("%#v.String() = %q, want %q", tt.localityID, gotStr, tt.str)
105102
}

xds/internal/xdsclient/tests/loadreport_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ import (
4444
)
4545

4646
const (
47-
testLocality1 = `{"region":"test-region1"}`
48-
testLocality2 = `{"region":"test-region2"}`
47+
testLocality1 = `{region="test-region1", zone="", sub_zone=""}`
48+
testLocality2 = `{region="test-region2", zone="", sub_zone=""}`
4949
testKey1 = "test-key1"
5050
testKey2 = "test-key2"
5151
)

xds/internal/xdsclient/xdsresource/unmarshal_eds.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func parseEDSRespProto(m *v3endpointpb.ClusterLoadAssignment) (EndpointsUpdate,
169169
Zone: l.Zone,
170170
SubZone: l.SubZone,
171171
}
172-
lidStr, _ := lid.ToString()
172+
lidStr := lid.ToString()
173173

174174
// "Since an xDS configuration can place a given locality under multiple
175175
// priorities, it is possible to see locality weight attributes with

0 commit comments

Comments
 (0)