@@ -82,6 +82,7 @@ final class WeightedRoundRobinLoadBalancer extends RoundRobinLoadBalancer {
8282 private final AtomicInteger sequence ;
8383 private final long infTime ;
8484 private final Ticker ticker ;
85+ private String locality = "" ;
8586
8687 // The metric instruments are only registered once and shared by all instances of this LB.
8788 static {
@@ -147,6 +148,12 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
147148 handleNameResolutionError (unavailableStatus );
148149 return unavailableStatus ;
149150 }
151+ String locality = resolvedAddresses .getAttributes ().get (WeightedTargetLoadBalancer .CHILD_NAME );
152+ if (locality != null ) {
153+ this .locality = locality ;
154+ } else {
155+ this .locality = "" ;
156+ }
150157 config =
151158 (WeightedRoundRobinLoadBalancerConfig ) resolvedAddresses .getLoadBalancingPolicyConfig ();
152159 AcceptResolvedAddrRetVal acceptRetVal ;
@@ -179,7 +186,8 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
179186 @ Override
180187 public SubchannelPicker createReadyPicker (Collection <ChildLbState > activeList ) {
181188 return new WeightedRoundRobinPicker (ImmutableList .copyOf (activeList ),
182- config .enableOobLoadReport , config .errorUtilizationPenalty , sequence , getHelper ());
189+ config .enableOobLoadReport , config .errorUtilizationPenalty , sequence , getHelper (),
190+ locality );
183191 }
184192
185193 @ VisibleForTesting
@@ -373,10 +381,12 @@ static final class WeightedRoundRobinPicker extends SubchannelPicker {
373381 private final AtomicInteger sequence ;
374382 private final int hashCode ;
375383 private final LoadBalancer .Helper helper ;
384+ private final String locality ;
376385 private volatile StaticStrideScheduler scheduler ;
377386
378387 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 ) {
380390 checkNotNull (children , "children" );
381391 Preconditions .checkArgument (!children .isEmpty (), "empty child list" );
382392 this .children = children ;
@@ -391,6 +401,7 @@ static final class WeightedRoundRobinPicker extends SubchannelPicker {
391401 this .errorUtilizationPenalty = errorUtilizationPenalty ;
392402 this .sequence = checkNotNull (sequence , "sequence" );
393403 this .helper = helper ;
404+ this .locality = checkNotNull (locality , "locality" );
394405
395406 // For equality we treat children as a set; use hash code as defined by Set
396407 int sum = 0 ;
@@ -434,29 +445,29 @@ private void updateWeight() {
434445 helper .getMetricRecorder ()
435446 .recordDoubleHistogram (ENDPOINT_WEIGHTS_HISTOGRAM , newWeight ,
436447 ImmutableList .of (helper .getChannelTarget ()),
437- ImmutableList .of ("" ));
448+ ImmutableList .of (locality ));
438449 newWeights [i ] = newWeight > 0 ? (float ) newWeight : 0.0f ;
439450 }
440451 if (staleEndpoints .get () > 0 ) {
441452 // TODO: add locality label once available
442453 helper .getMetricRecorder ()
443454 .addLongCounter (ENDPOINT_WEIGHT_STALE_COUNTER , staleEndpoints .get (),
444455 ImmutableList .of (helper .getChannelTarget ()),
445- ImmutableList .of ("" ));
456+ ImmutableList .of (locality ));
446457 }
447458 if (notYetUsableEndpoints .get () > 0 ) {
448459 // TODO: add locality label once available
449460 helper .getMetricRecorder ()
450461 .addLongCounter (ENDPOINT_WEIGHT_NOT_YET_USEABLE_COUNTER , notYetUsableEndpoints .get (),
451- ImmutableList .of (helper .getChannelTarget ()), ImmutableList .of ("" ));
462+ ImmutableList .of (helper .getChannelTarget ()), ImmutableList .of (locality ));
452463 }
453464
454465 this .scheduler = new StaticStrideScheduler (newWeights , sequence );
455466 if (this .scheduler .usesRoundRobin ()) {
456467 // TODO: locality label once available
457468 helper .getMetricRecorder ()
458469 .addLongCounter (RR_FALLBACK_COUNTER , 1 , ImmutableList .of (helper .getChannelTarget ()),
459- ImmutableList .of ("" ));
470+ ImmutableList .of (locality ));
460471 }
461472 }
462473
0 commit comments