-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Implement grpc.lb.backend_service optional label #11990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,32 +102,44 @@ final class WeightedRoundRobinLoadBalancer extends MultiChildLoadBalancer { | |
private final long infTime; | ||
private final Ticker ticker; | ||
private String locality = ""; | ||
private String backendService = ""; | ||
private SubchannelPicker currentPicker = new FixedResultPicker(PickResult.withNoResult()); | ||
|
||
// The metric instruments are only registered once and shared by all instances of this LB. | ||
static { | ||
MetricInstrumentRegistry metricInstrumentRegistry | ||
= MetricInstrumentRegistry.getDefaultRegistry(); | ||
RR_FALLBACK_COUNTER = metricInstrumentRegistry.registerLongCounter("grpc.lb.wrr.rr_fallback", | ||
RR_FALLBACK_COUNTER = metricInstrumentRegistry.registerLongCounter( | ||
"grpc.lb.wrr.rr_fallback", | ||
"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", | ||
"{update}", Lists.newArrayList("grpc.target"), Lists.newArrayList("grpc.lb.locality"), | ||
"{update}", | ||
Lists.newArrayList("grpc.target"), | ||
Lists.newArrayList("grpc.lb.locality", "grpc.lb.backend_service"), | ||
false); | ||
ENDPOINT_WEIGHT_NOT_YET_USEABLE_COUNTER = metricInstrumentRegistry.registerLongCounter( | ||
"grpc.lb.wrr.endpoint_weight_not_yet_usable", "EXPERIMENTAL. Number of endpoints " | ||
+ "from each scheduler update that don't yet have usable weight information", | ||
"{endpoint}", Lists.newArrayList("grpc.target"), Lists.newArrayList("grpc.lb.locality"), | ||
"grpc.lb.wrr.endpoint_weight_not_yet_usable", | ||
"EXPERIMENTAL. Number of endpoints from each scheduler update that don't yet have usable " | ||
+ "weight information", | ||
"{endpoint}", | ||
Lists.newArrayList("grpc.target"), | ||
Lists.newArrayList("grpc.lb.locality", "grpc.lb.backend_service"), | ||
false); | ||
ENDPOINT_WEIGHT_STALE_COUNTER = metricInstrumentRegistry.registerLongCounter( | ||
"grpc.lb.wrr.endpoint_weight_stale", | ||
"EXPERIMENTAL. Number of endpoints from each scheduler update whose latest weight is " | ||
+ "older than the expiration period", "{endpoint}", Lists.newArrayList("grpc.target"), | ||
Lists.newArrayList("grpc.lb.locality"), false); | ||
+ "older than the expiration period", | ||
"{endpoint}", | ||
Lists.newArrayList("grpc.target"), | ||
Lists.newArrayList("grpc.lb.locality", "grpc.lb.backend_service"), | ||
false); | ||
ENDPOINT_WEIGHTS_HISTOGRAM = metricInstrumentRegistry.registerDoubleHistogram( | ||
"grpc.lb.wrr.endpoint_weights", | ||
"EXPERIMENTAL. The histogram buckets will be endpoint weight ranges.", | ||
"{weight}", Lists.newArrayList(), Lists.newArrayList("grpc.target"), | ||
Lists.newArrayList("grpc.lb.locality"), | ||
"{weight}", | ||
Lists.newArrayList(), | ||
Lists.newArrayList("grpc.target"), | ||
Lists.newArrayList("grpc.lb.locality", "grpc.lb.backend_service"), | ||
false); | ||
} | ||
|
||
|
@@ -168,6 +180,13 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) { | |
} else { | ||
this.locality = ""; | ||
} | ||
String backendService | ||
= resolvedAddresses.getAttributes().get(NameResolver.ATTR_BACKEND_SERVICE); | ||
if (backendService != null) { | ||
this.backendService = backendService; | ||
} else { | ||
this.backendService = ""; | ||
} | ||
Comment on lines
+183
to
+189
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can think of exposing a getOrDefault() from the Attributes class There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically |
||
config = | ||
(WeightedRoundRobinLoadBalancerConfig) resolvedAddresses.getLoadBalancingPolicyConfig(); | ||
|
||
|
@@ -232,26 +251,27 @@ private void updateWeight(WeightedRoundRobinPicker picker) { | |
helper.getMetricRecorder() | ||
.recordDoubleHistogram(ENDPOINT_WEIGHTS_HISTOGRAM, newWeight, | ||
ImmutableList.of(helper.getChannelTarget()), | ||
ImmutableList.of(locality)); | ||
ImmutableList.of(locality, backendService)); | ||
newWeights[i] = newWeight > 0 ? (float) newWeight : 0.0f; | ||
} | ||
|
||
if (staleEndpoints.get() > 0) { | ||
helper.getMetricRecorder() | ||
.addLongCounter(ENDPOINT_WEIGHT_STALE_COUNTER, staleEndpoints.get(), | ||
ImmutableList.of(helper.getChannelTarget()), | ||
ImmutableList.of(locality)); | ||
ImmutableList.of(locality, backendService)); | ||
} | ||
if (notYetUsableEndpoints.get() > 0) { | ||
helper.getMetricRecorder() | ||
.addLongCounter(ENDPOINT_WEIGHT_NOT_YET_USEABLE_COUNTER, notYetUsableEndpoints.get(), | ||
ImmutableList.of(helper.getChannelTarget()), ImmutableList.of(locality)); | ||
ImmutableList.of(helper.getChannelTarget()), | ||
ImmutableList.of(locality, backendService)); | ||
} | ||
boolean weightsEffective = picker.updateWeight(newWeights); | ||
if (!weightsEffective) { | ||
helper.getMetricRecorder() | ||
.addLongCounter(RR_FALLBACK_COUNTER, 1, ImmutableList.of(helper.getChannelTarget()), | ||
ImmutableList.of(locality)); | ||
ImmutableList.of(locality, backendService)); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can add a constant for "grpc.lb.backend_service"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What goal do you have in mind? I'm generally only a fan of constants in tests for things that don't matter to the test or to share between positive and negative tests. This string is part of the API and can't change. And if it is misspelled we want the test to fail and the test is more obvious with the literal. I'd use a constant if the "should-be-overwritten" was further away from the "the-moon", as misspelling the first prevents part of the test from working.
There would actually need to be two constants here: one for this line and the assertion, and a different constant (with same value) for the
addOptionalLabel()
. Those just happen to be the same string.This did point out to me that the test shouldn't be using
BACKEND_SERVICE_KEY
, so I've removed the usage of that constant.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wanted it to be a constant so that any modification to the literal could be done easily.