@@ -82,6 +82,7 @@ final class WeightedRoundRobinLoadBalancer extends RoundRobinLoadBalancer {
82
82
private final AtomicInteger sequence ;
83
83
private final long infTime ;
84
84
private final Ticker ticker ;
85
+ private String locality = "" ;
85
86
86
87
// The metric instruments are only registered once and shared by all instances of this LB.
87
88
static {
@@ -147,6 +148,12 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
147
148
handleNameResolutionError (unavailableStatus );
148
149
return unavailableStatus ;
149
150
}
151
+ String locality = resolvedAddresses .getAttributes ().get (WeightedTargetLoadBalancer .CHILD_NAME );
152
+ if (locality != null ) {
153
+ this .locality = locality ;
154
+ } else {
155
+ this .locality = "" ;
156
+ }
150
157
config =
151
158
(WeightedRoundRobinLoadBalancerConfig ) resolvedAddresses .getLoadBalancingPolicyConfig ();
152
159
AcceptResolvedAddrRetVal acceptRetVal ;
@@ -179,7 +186,8 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
179
186
@ Override
180
187
public SubchannelPicker createReadyPicker (Collection <ChildLbState > activeList ) {
181
188
return new WeightedRoundRobinPicker (ImmutableList .copyOf (activeList ),
182
- config .enableOobLoadReport , config .errorUtilizationPenalty , sequence , getHelper ());
189
+ config .enableOobLoadReport , config .errorUtilizationPenalty , sequence , getHelper (),
190
+ locality );
183
191
}
184
192
185
193
@ VisibleForTesting
@@ -373,10 +381,12 @@ static final class WeightedRoundRobinPicker extends SubchannelPicker {
373
381
private final AtomicInteger sequence ;
374
382
private final int hashCode ;
375
383
private final LoadBalancer .Helper helper ;
384
+ private final String locality ;
376
385
private volatile StaticStrideScheduler scheduler ;
377
386
378
387
WeightedRoundRobinPicker (List <ChildLbState > children , boolean enableOobLoadReport ,
379
- float errorUtilizationPenalty , AtomicInteger sequence , LoadBalancer .Helper helper ) {
388
+ float errorUtilizationPenalty , AtomicInteger sequence , LoadBalancer .Helper helper ,
389
+ String locality ) {
380
390
checkNotNull (children , "children" );
381
391
Preconditions .checkArgument (!children .isEmpty (), "empty child list" );
382
392
this .children = children ;
@@ -391,6 +401,7 @@ static final class WeightedRoundRobinPicker extends SubchannelPicker {
391
401
this .errorUtilizationPenalty = errorUtilizationPenalty ;
392
402
this .sequence = checkNotNull (sequence , "sequence" );
393
403
this .helper = helper ;
404
+ this .locality = checkNotNull (locality , "locality" );
394
405
395
406
// For equality we treat children as a set; use hash code as defined by Set
396
407
int sum = 0 ;
@@ -434,29 +445,29 @@ private void updateWeight() {
434
445
helper .getMetricRecorder ()
435
446
.recordDoubleHistogram (ENDPOINT_WEIGHTS_HISTOGRAM , newWeight ,
436
447
ImmutableList .of (helper .getChannelTarget ()),
437
- ImmutableList .of ("" ));
448
+ ImmutableList .of (locality ));
438
449
newWeights [i ] = newWeight > 0 ? (float ) newWeight : 0.0f ;
439
450
}
440
451
if (staleEndpoints .get () > 0 ) {
441
452
// TODO: add locality label once available
442
453
helper .getMetricRecorder ()
443
454
.addLongCounter (ENDPOINT_WEIGHT_STALE_COUNTER , staleEndpoints .get (),
444
455
ImmutableList .of (helper .getChannelTarget ()),
445
- ImmutableList .of ("" ));
456
+ ImmutableList .of (locality ));
446
457
}
447
458
if (notYetUsableEndpoints .get () > 0 ) {
448
459
// TODO: add locality label once available
449
460
helper .getMetricRecorder ()
450
461
.addLongCounter (ENDPOINT_WEIGHT_NOT_YET_USEABLE_COUNTER , notYetUsableEndpoints .get (),
451
- ImmutableList .of (helper .getChannelTarget ()), ImmutableList .of ("" ));
462
+ ImmutableList .of (helper .getChannelTarget ()), ImmutableList .of (locality ));
452
463
}
453
464
454
465
this .scheduler = new StaticStrideScheduler (newWeights , sequence );
455
466
if (this .scheduler .usesRoundRobin ()) {
456
467
// TODO: locality label once available
457
468
helper .getMetricRecorder ()
458
469
.addLongCounter (RR_FALLBACK_COUNTER , 1 , ImmutableList .of (helper .getChannelTarget ()),
459
- ImmutableList .of ("" ));
470
+ ImmutableList .of (locality ));
460
471
}
461
472
}
462
473
0 commit comments