Skip to content

Commit 2897b39

Browse files
temawiejona86
authored andcommittedMay 9, 2024·
xds: Include locality label in WRR metrics (#11170)
1 parent 3b6b153 commit 2897b39

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed
 

‎xds/src/main/java/io/grpc/xds/WeightedRoundRobinLoadBalancer.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -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

‎xds/src/test/java/io/grpc/xds/WeightedRoundRobinLoadBalancerTest.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public void uncaughtException(Thread t, Throwable e) {
145145
});
146146

147147
private String channelTarget = "channel-target";
148+
private String locality = "locality";
148149

149150
public WeightedRoundRobinLoadBalancerTest() {
150151
testHelperInstance = new TestHelper();
@@ -1135,9 +1136,11 @@ public void removingAddressShutsdownSubchannel() {
11351136
@Test
11361137
public void metrics() {
11371138
// Give WRR some valid addresses to work with.
1139+
Attributes attributesWithLocality = Attributes.newBuilder()
1140+
.set(WeightedTargetLoadBalancer.CHILD_NAME, locality).build();
11381141
syncContext.execute(() -> wrr.acceptResolvedAddresses(ResolvedAddresses.newBuilder()
11391142
.setAddresses(servers).setLoadBalancingPolicyConfig(weightedConfig)
1140-
.setAttributes(affinity).build()));
1143+
.setAttributes(attributesWithLocality).build()));
11411144

11421145
// Flip the three subchannels to READY state to initiate the WRR logic
11431146
Iterator<Subchannel> it = subchannels.values().iterator();
@@ -1240,7 +1243,7 @@ private void verifyLongCounterRecord(String name, int times, long value) {
12401243
public boolean matches(LongCounterMetricInstrument longCounterInstrument) {
12411244
return longCounterInstrument.getName().equals(name);
12421245
}
1243-
}), eq(value), eq(Lists.newArrayList(channelTarget)), eq(Lists.newArrayList("")));
1246+
}), eq(value), eq(Lists.newArrayList(channelTarget)), eq(Lists.newArrayList(locality)));
12441247
}
12451248

12461249
// Verifies that the MetricRecorder has been called to record a given double histogram value the
@@ -1252,7 +1255,7 @@ private void verifyDoubleHistogramRecord(String name, int times, double value) {
12521255
public boolean matches(DoubleHistogramMetricInstrument doubleHistogramInstrument) {
12531256
return doubleHistogramInstrument.getName().equals(name);
12541257
}
1255-
}), eq(value), eq(Lists.newArrayList(channelTarget)), eq(Lists.newArrayList("")));
1258+
}), eq(value), eq(Lists.newArrayList(channelTarget)), eq(Lists.newArrayList(locality)));
12561259
}
12571260

12581261
private int getNumFilteredPendingTasks() {

0 commit comments

Comments
 (0)
Please sign in to comment.