Skip to content

Commit a1033b1

Browse files
xds: add LRS named metrics support (#7027)
1 parent 4f43d2e commit a1033b1

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed

xds/internal/balancer/clusterimpl/balancer_test.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ import (
4343
"google.golang.org/grpc/xds/internal/xdsclient"
4444
"google.golang.org/grpc/xds/internal/xdsclient/bootstrap"
4545
"google.golang.org/grpc/xds/internal/xdsclient/load"
46+
47+
v3orcapb "github.com/cncf/xds/go/xds/data/orca/v3"
4648
)
4749

4850
const (
@@ -51,6 +53,9 @@ const (
5153

5254
testClusterName = "test-cluster"
5355
testServiceName = "test-eds-service"
56+
57+
testNamedMetricsKey1 = "test-named1"
58+
testNamedMetricsKey2 = "test-named2"
5459
)
5560

5661
var (
@@ -68,6 +73,7 @@ var (
6873
cmpopts.EquateEmpty(),
6974
cmpopts.IgnoreFields(load.Data{}, "ReportInterval"),
7075
}
76+
toleranceCmpOpt = cmpopts.EquateApprox(0, 1e-5)
7177
)
7278

7379
type s struct {
@@ -629,7 +635,10 @@ func (s) TestLoadReporting(t *testing.T) {
629635
if gotSCSt.SubConn != sc1 {
630636
return fmt.Errorf("picker.Pick, got %v, %v, want SubConn=%v", gotSCSt, err, sc1)
631637
}
632-
gotSCSt.Done(balancer.DoneInfo{})
638+
lr := &v3orcapb.OrcaLoadReport{
639+
NamedMetrics: map[string]float64{testNamedMetricsKey1: 3.14, testNamedMetricsKey2: 2.718},
640+
}
641+
gotSCSt.Done(balancer.DoneInfo{ServerLoad: lr})
633642
}
634643
for i := 0; i < errorCount; i++ {
635644
gotSCSt, err := p.Pick(balancer.PickInfo{})
@@ -671,7 +680,13 @@ func (s) TestLoadReporting(t *testing.T) {
671680
if reqStats.InProgress != 0 {
672681
t.Errorf("got inProgress %v, want %v", reqStats.InProgress, 0)
673682
}
674-
683+
wantLoadStats := map[string]load.ServerLoadData{
684+
testNamedMetricsKey1: {Count: 5, Sum: 15.7}, // aggregation of 5 * 3.14 = 15.7
685+
testNamedMetricsKey2: {Count: 5, Sum: 13.59}, // aggregation of 5 * 2.718 = 13.59
686+
}
687+
if diff := cmp.Diff(wantLoadStats, localityData.LoadStats, toleranceCmpOpt); diff != "" {
688+
t.Errorf("localityData.LoadStats returned unexpected diff (-want +got):\n%s", diff)
689+
}
675690
b.Close()
676691
if err := xdsC.WaitForCancelReportLoad(ctx); err != nil {
677692
t.Fatalf("unexpected error waiting form load report to be canceled: %v", err)

xds/internal/balancer/clusterimpl/picker.go

+1-11
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ func (d *dropper) drop() (ret bool) {
6868
return d.w.Next().(bool)
6969
}
7070

71-
const (
72-
serverLoadCPUName = "cpu_utilization"
73-
serverLoadMemoryName = "mem_utilization"
74-
)
75-
7671
// loadReporter wraps the methods from the loadStore that are used here.
7772
type loadReporter interface {
7873
CallStarted(locality string)
@@ -163,12 +158,7 @@ func (d *picker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
163158
if !ok || load == nil {
164159
return
165160
}
166-
d.loadStore.CallServerLoad(lIDStr, serverLoadCPUName, load.CpuUtilization)
167-
d.loadStore.CallServerLoad(lIDStr, serverLoadMemoryName, load.MemUtilization)
168-
for n, c := range load.RequestCost {
169-
d.loadStore.CallServerLoad(lIDStr, n, c)
170-
}
171-
for n, c := range load.Utilization {
161+
for n, c := range load.NamedMetrics {
172162
d.loadStore.CallServerLoad(lIDStr, n, c)
173163
}
174164
}

xds/internal/xdsclient/transport/loadreport_test.go

+40-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
2626
"github.com/google/go-cmp/cmp"
27+
"github.com/google/go-cmp/cmp/cmpopts"
2728
"github.com/google/uuid"
2829
"google.golang.org/grpc/internal/testutils/xds/fakeserver"
2930
"google.golang.org/grpc/xds/internal/testutils"
@@ -35,6 +36,22 @@ import (
3536
v3lrspb "github.com/envoyproxy/go-control-plane/envoy/service/load_stats/v3"
3637
)
3738

39+
const (
40+
testLocality1 = `{"region":"test-region1"}`
41+
testLocality2 = `{"region":"test-region2"}`
42+
testKey1 = "test-key1"
43+
testKey2 = "test-key2"
44+
)
45+
46+
var (
47+
toleranceCmpOpt = cmpopts.EquateApprox(0, 1e-5)
48+
ignoreOrderCmpOpt = protocmp.FilterField(&v3endpointpb.ClusterStats{}, "upstream_locality_stats",
49+
cmpopts.SortSlices(func(a, b protocmp.Message) bool {
50+
return a.String() < b.String()
51+
}),
52+
)
53+
)
54+
3855
func (s) TestReportLoad(t *testing.T) {
3956
// Create a fake xDS management server listening on a local port.
4057
mgmtServer, cleanup := startFakeManagementServer(t)
@@ -74,6 +91,13 @@ func (s) TestReportLoad(t *testing.T) {
7491

7592
// Push some loads on the received store.
7693
store1.PerCluster("cluster1", "eds1").CallDropped("test")
94+
store1.PerCluster("cluster1", "eds1").CallStarted(testLocality1)
95+
store1.PerCluster("cluster1", "eds1").CallServerLoad(testLocality1, testKey1, 3.14)
96+
store1.PerCluster("cluster1", "eds1").CallServerLoad(testLocality1, testKey1, 2.718)
97+
store1.PerCluster("cluster1", "eds1").CallFinished(testLocality1, nil)
98+
store1.PerCluster("cluster1", "eds1").CallStarted(testLocality2)
99+
store1.PerCluster("cluster1", "eds1").CallServerLoad(testLocality2, testKey2, 1.618)
100+
store1.PerCluster("cluster1", "eds1").CallFinished(testLocality2, nil)
77101

78102
// Ensure the initial request is received.
79103
req, err := mgmtServer.LRSRequestChan.Receive(ctx)
@@ -115,8 +139,23 @@ func (s) TestReportLoad(t *testing.T) {
115139
ClusterServiceName: "eds1",
116140
TotalDroppedRequests: 1,
117141
DroppedRequests: []*v3endpointpb.ClusterStats_DroppedRequests{{Category: "test", DroppedCount: 1}},
142+
UpstreamLocalityStats: []*v3endpointpb.UpstreamLocalityStats{
143+
{
144+
Locality: &v3corepb.Locality{Region: "test-region1"},
145+
LoadMetricStats: []*v3endpointpb.EndpointLoadMetricStats{
146+
// TotalMetricValue is the aggregation of 3.14 + 2.718 = 5.858
147+
{MetricName: testKey1, NumRequestsFinishedWithMetric: 2, TotalMetricValue: 5.858}},
148+
TotalSuccessfulRequests: 1,
149+
},
150+
{
151+
Locality: &v3corepb.Locality{Region: "test-region2"},
152+
LoadMetricStats: []*v3endpointpb.EndpointLoadMetricStats{
153+
{MetricName: testKey2, NumRequestsFinishedWithMetric: 1, TotalMetricValue: 1.618}},
154+
TotalSuccessfulRequests: 1,
155+
},
156+
},
118157
}
119-
if diff := cmp.Diff(wantLoad, gotLoad[0], protocmp.Transform()); diff != "" {
158+
if diff := cmp.Diff(wantLoad, gotLoad[0], protocmp.Transform(), toleranceCmpOpt, ignoreOrderCmpOpt); diff != "" {
120159
t.Fatalf("Unexpected diff in LRS request (-got, +want):\n%s", diff)
121160
}
122161

0 commit comments

Comments
 (0)