Skip to content

Commit e78745f

Browse files
committed
a89 wrr changes
1 parent f323f0c commit e78745f

3 files changed

Lines changed: 27 additions & 15 deletions

File tree

balancer/weightedroundrobin/balancer.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"google.golang.org/grpc/internal/grpclog"
4747
"google.golang.org/grpc/internal/grpcsync"
4848
iserviceconfig "google.golang.org/grpc/internal/serviceconfig"
49+
xdsinternal "google.golang.org/grpc/internal/xds"
4950
"google.golang.org/grpc/orca"
5051
"google.golang.org/grpc/resolver"
5152
"google.golang.org/grpc/serviceconfig"
@@ -62,7 +63,7 @@ var (
6263
Description: "EXPERIMENTAL. Number of scheduler updates in which there were not enough endpoints with valid weight, which caused the WRR policy to fall back to RR behavior.",
6364
Unit: "{update}",
6465
Labels: []string{"grpc.target"},
65-
OptionalLabels: []string{"grpc.lb.locality"},
66+
OptionalLabels: []string{"grpc.lb.locality", "grpc.lb.backend_service"},
6667
Default: false,
6768
})
6869

@@ -71,7 +72,7 @@ var (
7172
Description: "EXPERIMENTAL. Number of endpoints from each scheduler update that don't yet have usable weight information (i.e., either the load report has not yet been received, or it is within the blackout period).",
7273
Unit: "{endpoint}",
7374
Labels: []string{"grpc.target"},
74-
OptionalLabels: []string{"grpc.lb.locality"},
75+
OptionalLabels: []string{"grpc.lb.locality", "grpc.lb.backend_service"},
7576
Default: false,
7677
})
7778

@@ -80,15 +81,15 @@ var (
8081
Description: "EXPERIMENTAL. Number of endpoints from each scheduler update whose latest weight is older than the expiration period.",
8182
Unit: "{endpoint}",
8283
Labels: []string{"grpc.target"},
83-
OptionalLabels: []string{"grpc.lb.locality"},
84+
OptionalLabels: []string{"grpc.lb.locality", "grpc.lb.backend_service"},
8485
Default: false,
8586
})
8687
endpointWeightsMetric = estats.RegisterFloat64Histo(estats.MetricDescriptor{
8788
Name: "grpc.lb.wrr.endpoint_weights",
8889
Description: "EXPERIMENTAL. Weight of each endpoint, recorded on every scheduler update. Endpoints without usable weights will be recorded as weight 0.",
8990
Unit: "{endpoint}",
9091
Labels: []string{"grpc.target"},
91-
OptionalLabels: []string{"grpc.lb.locality"},
92+
OptionalLabels: []string{"grpc.lb.locality", "grpc.lb.backend_service"},
9293
Default: false,
9394
})
9495
)
@@ -173,6 +174,7 @@ func (b *wrrBalancer) updateEndpointsLocked(endpoints []resolver.Endpoint) {
173174
metricsRecorder: b.metricsRecorder,
174175
target: b.target,
175176
locality: b.locality,
177+
cluster: b.clusterName,
176178
}
177179
for _, addr := range endpoint.Addresses {
178180
b.addressWeights.Set(addr, ew)
@@ -211,6 +213,7 @@ type wrrBalancer struct {
211213
mu sync.Mutex
212214
cfg *lbConfig // active config
213215
locality string
216+
clusterName string
214217
stopPicker *grpcsync.Event
215218
addressWeights *resolver.AddressMapV2[*endpointWeight]
216219
endpointToWeight *resolver.EndpointMap[*endpointWeight]
@@ -231,6 +234,11 @@ func (b *wrrBalancer) UpdateClientConnState(ccs balancer.ClientConnState) error
231234
b.mu.Lock()
232235
b.cfg = cfg
233236
b.locality = weightedtarget.LocalityFromResolverState(ccs.ResolverState)
237+
if ccs.ResolverState.Addresses != nil {
238+
if cluster, ok := xdsinternal.GetXDSHandshakeClusterName(ccs.ResolverState.Addresses[0].Attributes); ok {
239+
b.clusterName = cluster
240+
}
241+
}
234242
b.updateEndpointsLocked(ccs.ResolverState.Endpoints)
235243
b.mu.Unlock()
236244

@@ -288,6 +296,7 @@ func (b *wrrBalancer) UpdateState(state balancer.State) {
288296
metricsRecorder: b.metricsRecorder,
289297
locality: b.locality,
290298
target: b.target,
299+
clusterName: b.clusterName,
291300
}
292301

293302
b.stopPicker = grpcsync.NewEvent()
@@ -420,6 +429,7 @@ type picker struct {
420429
// The following fields are immutable.
421430
target string
422431
locality string
432+
clusterName string
423433
metricsRecorder estats.MetricsRecorder
424434
}
425435

@@ -499,6 +509,7 @@ type endpointWeight struct {
499509
target string
500510
metricsRecorder estats.MetricsRecorder
501511
locality string
512+
cluster string
502513

503514
// The following fields are only accessed on calls into the LB policy, and
504515
// do not need a mutex.
@@ -602,14 +613,14 @@ func (w *endpointWeight) weight(now time.Time, weightExpirationPeriod, blackoutP
602613

603614
if recordMetrics {
604615
defer func() {
605-
endpointWeightsMetric.Record(w.metricsRecorder, weight, w.target, w.locality)
616+
endpointWeightsMetric.Record(w.metricsRecorder, weight, w.target, w.locality, w.cluster)
606617
}()
607618
}
608619

609620
// The endpoint has not received a load report (i.e. just turned READY with
610621
// no load report).
611622
if w.lastUpdated.Equal(time.Time{}) {
612-
endpointWeightNotYetUsableMetric.Record(w.metricsRecorder, 1, w.target, w.locality)
623+
endpointWeightNotYetUsableMetric.Record(w.metricsRecorder, 1, w.target, w.locality, w.cluster)
613624
return 0
614625
}
615626

@@ -618,7 +629,7 @@ func (w *endpointWeight) weight(now time.Time, weightExpirationPeriod, blackoutP
618629
// start getting data again in the future, and return 0.
619630
if now.Sub(w.lastUpdated) >= weightExpirationPeriod {
620631
if recordMetrics {
621-
endpointWeightStaleMetric.Record(w.metricsRecorder, 1, w.target, w.locality)
632+
endpointWeightStaleMetric.Record(w.metricsRecorder, 1, w.target, w.locality, w.cluster)
622633
}
623634
w.nonEmptySince = time.Time{}
624635
return 0
@@ -627,7 +638,7 @@ func (w *endpointWeight) weight(now time.Time, weightExpirationPeriod, blackoutP
627638
// If we don't have at least blackoutPeriod worth of data, return 0.
628639
if blackoutPeriod != 0 && (w.nonEmptySince.Equal(time.Time{}) || now.Sub(w.nonEmptySince) < blackoutPeriod) {
629640
if recordMetrics {
630-
endpointWeightNotYetUsableMetric.Record(w.metricsRecorder, 1, w.target, w.locality)
641+
endpointWeightNotYetUsableMetric.Record(w.metricsRecorder, 1, w.target, w.locality, w.cluster)
631642
}
632643
return 0
633644
}

balancer/weightedroundrobin/scheduler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (p *picker) newScheduler(recordMetrics bool) scheduler {
3939
}
4040
if n == 1 {
4141
if recordMetrics {
42-
rrFallbackMetric.Record(p.metricsRecorder, 1, p.target, p.locality)
42+
rrFallbackMetric.Record(p.metricsRecorder, 1, p.target, p.locality, p.clusterName)
4343
}
4444
return &rrScheduler{numSCs: 1, inc: p.inc}
4545
}
@@ -58,7 +58,7 @@ func (p *picker) newScheduler(recordMetrics bool) scheduler {
5858

5959
if numZero >= n-1 {
6060
if recordMetrics {
61-
rrFallbackMetric.Record(p.metricsRecorder, 1, p.target, p.locality)
61+
rrFallbackMetric.Record(p.metricsRecorder, 1, p.target, p.locality, p.clusterName)
6262
}
6363
return &rrScheduler{numSCs: uint32(n), inc: p.inc}
6464
}

stats/opentelemetry/e2e_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ func metricsDataFromReader(ctx context.Context, reader *metric.ManualReader) map
577577
// sleeps for a bit to allow weight to expire. It then asserts OpenTelemetry
578578
// metrics atoms are eventually present for all four WRR Metrics, alongside the
579579
// correct target and locality label for each metric.
580-
func (s) TestWRRMetrics(t *testing.T) {
580+
func TestWRRMetrics(t *testing.T) {
581581
cmr := orca.NewServerMetricsRecorder().(orca.CallMetricsRecorder)
582582
backend1 := stubserver.StartTestService(t, &stubserver.StubServer{
583583
EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
@@ -673,7 +673,7 @@ func (s) TestWRRMetrics(t *testing.T) {
673673
mo := opentelemetry.MetricsOptions{
674674
MeterProvider: provider,
675675
Metrics: opentelemetry.DefaultMetrics().Add("grpc.lb.wrr.rr_fallback", "grpc.lb.wrr.endpoint_weight_not_yet_usable", "grpc.lb.wrr.endpoint_weight_stale", "grpc.lb.wrr.endpoint_weights"),
676-
OptionalLabels: []string{"grpc.lb.locality"},
676+
OptionalLabels: []string{"grpc.lb.locality", "grpc.lb.backend_service"},
677677
}
678678

679679
target := fmt.Sprintf("xds:///%s", serviceName)
@@ -699,6 +699,7 @@ func (s) TestWRRMetrics(t *testing.T) {
699699

700700
targetAttr := attribute.String("grpc.target", target)
701701
localityAttr := attribute.String("grpc.lb.locality", `{region="region-1", zone="zone-1", sub_zone="subzone-1"}`)
702+
backendServiceAttr := attribute.String("grpc.lb.backend_service", clusterName)
702703

703704
wantMetrics := []metricdata.Metrics{
704705
{
@@ -708,7 +709,7 @@ func (s) TestWRRMetrics(t *testing.T) {
708709
Data: metricdata.Sum[int64]{
709710
DataPoints: []metricdata.DataPoint[int64]{
710711
{
711-
Attributes: attribute.NewSet(targetAttr, localityAttr),
712+
Attributes: attribute.NewSet(targetAttr, localityAttr, backendServiceAttr),
712713
Value: 1, // value ignored
713714
},
714715
},
@@ -724,7 +725,7 @@ func (s) TestWRRMetrics(t *testing.T) {
724725
Data: metricdata.Sum[int64]{
725726
DataPoints: []metricdata.DataPoint[int64]{
726727
{
727-
Attributes: attribute.NewSet(targetAttr, localityAttr),
728+
Attributes: attribute.NewSet(targetAttr, localityAttr, backendServiceAttr),
728729
Value: 1, // value ignored
729730
},
730731
},
@@ -739,7 +740,7 @@ func (s) TestWRRMetrics(t *testing.T) {
739740
Data: metricdata.Histogram[float64]{
740741
DataPoints: []metricdata.HistogramDataPoint[float64]{
741742
{
742-
Attributes: attribute.NewSet(targetAttr, localityAttr),
743+
Attributes: attribute.NewSet(targetAttr, localityAttr, backendServiceAttr),
743744
},
744745
},
745746
Temporality: metricdata.CumulativeTemporality,

0 commit comments

Comments
 (0)