Skip to content

Commit f930311

Browse files
committed
a89 wrr changes
1 parent acbd0fe commit f930311

4 files changed

Lines changed: 45 additions & 16 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+
"google.golang.org/grpc/internal/xds/balancer/clusterimpl"
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 cluster, ok := clusterimpl.GetBackendServiceFromState(ccs.ResolverState); !ok {
238+
b.logger.Infof("Backend service name not found in attributes.")
239+
} else {
240+
b.clusterName = cluster
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
}

internal/xds/balancer/clusterimpl/clusterimpl.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ type clusterImplBalancer struct {
123123
telemetryLabels map[string]string // Telemetry labels to set on picks, from LB config.
124124
}
125125

126+
type backendServiceKey struct{}
127+
128+
func setBackendServiceOnState(state resolver.State, backendService string) resolver.State {
129+
state.Attributes = state.Attributes.WithValue(backendServiceKey{}, backendService)
130+
return state
131+
}
132+
133+
func GetBackendServiceFromState(state resolver.State) (string, bool) {
134+
v := state.Attributes.Value(backendServiceKey{})
135+
name, ok := v.(string)
136+
return name, ok
137+
}
138+
126139
// handleDropAndRequestCountLocked compares drop and request counter in newConfig with
127140
// the one currently used by picker, and is protected by b.mu. It returns a boolean
128141
// indicating if a new picker needs to be generated.
@@ -295,9 +308,11 @@ func (b *clusterImplBalancer) UpdateClientConnState(s balancer.ClientConnState)
295308
return err
296309
}
297310

311+
newState := setBackendServiceOnState(s.ResolverState, b.clusterName)
312+
298313
// Addresses and sub-balancer config are sent to sub-balancer.
299314
err = b.child.UpdateClientConnState(balancer.ClientConnState{
300-
ResolverState: s.ResolverState,
315+
ResolverState: newState,
301316
BalancerConfig: parsedCfg,
302317
})
303318

@@ -434,6 +449,7 @@ func (scw *scWrapper) localityID() clients.Locality {
434449
}
435450

436451
func (b *clusterImplBalancer) NewSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
452+
b.logger.Infof("New Subconn from clusterimpl being invoked")
437453
clusterName := b.getClusterName()
438454
newAddrs := make([]resolver.Address, len(addrs))
439455
for i, addr := range addrs {
@@ -463,6 +479,7 @@ func (b *clusterImplBalancer) NewSubConn(addrs []resolver.Address, opts balancer
463479
scw.updateLocalityID(lID)
464480
}
465481
sc, err := b.ClientConn.NewSubConn(newAddrs, opts)
482+
b.logger.Infof("NewSubConn to client conn completed, returning")
466483
if err != nil {
467484
return nil, err
468485
}

stats/opentelemetry/e2e_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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,
@@ -761,7 +762,7 @@ func (s) TestWRRMetrics(t *testing.T) {
761762
Data: metricdata.Sum[int64]{
762763
DataPoints: []metricdata.DataPoint[int64]{
763764
{
764-
Attributes: attribute.NewSet(targetAttr, localityAttr),
765+
Attributes: attribute.NewSet(targetAttr, localityAttr, backendServiceAttr),
765766
Value: 1, // value ignored
766767
},
767768
},

0 commit comments

Comments
 (0)