Skip to content

Commit 940c8a7

Browse files
committed
Merge branch 'master' of https://github.com/grpc/grpc-java into child-channel-plugin
2 parents 57dc064 + c8079ee commit 940c8a7

49 files changed

Lines changed: 4720 additions & 626 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

core/src/main/java/io/grpc/internal/DelayedClientCall.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
*/
5050
public class DelayedClientCall<ReqT, RespT> extends ClientCall<ReqT, RespT> {
5151
private static final Logger logger = Logger.getLogger(DelayedClientCall.class.getName());
52+
53+
/** A string describing what this call is waiting on. */
54+
private final String bufferContext;
5255
/**
5356
* A timer to monitor the initial deadline. The timer must be cancelled on transition to the real
5457
* call.
@@ -76,7 +79,11 @@ public class DelayedClientCall<ReqT, RespT> extends ClientCall<ReqT, RespT> {
7679
private DelayedListener<RespT> delayedListener;
7780

7881
protected DelayedClientCall(
79-
Executor callExecutor, ScheduledExecutorService scheduler, @Nullable Deadline deadline) {
82+
String bufferContext,
83+
Executor callExecutor,
84+
ScheduledExecutorService scheduler,
85+
@Nullable Deadline deadline) {
86+
this.bufferContext = checkNotNull(bufferContext, "bufferContext");
8087
this.callExecutor = checkNotNull(callExecutor, "callExecutor");
8188
checkNotNull(scheduler, "scheduler");
8289
context = Context.current();
@@ -143,7 +150,8 @@ public void run() {
143150
}
144151
buf.append(seconds);
145152
buf.append(String.format(Locale.US, ".%09d", nanos));
146-
buf.append("s");
153+
buf.append("s waiting for ");
154+
buf.append(bufferContext);
147155
cancel(
148156
Status.DEADLINE_EXCEEDED.withDescription(buf.toString()),
149157
// We should not cancel the call if the realCall is set because there could be a

core/src/main/java/io/grpc/internal/ManagedChannelImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -998,9 +998,12 @@ private final class PendingCall<ReqT, RespT> extends DelayedClientCall<ReqT, Res
998998
final CallOptions callOptions;
999999
private final long callCreationTime;
10001000

1001-
PendingCall(
1002-
Context context, MethodDescriptor<ReqT, RespT> method, CallOptions callOptions) {
1003-
super(getCallExecutor(callOptions), scheduledExecutor, callOptions.getDeadline());
1001+
PendingCall(Context context, MethodDescriptor<ReqT, RespT> method, CallOptions callOptions) {
1002+
super(
1003+
"name_resolver",
1004+
getCallExecutor(callOptions),
1005+
scheduledExecutor,
1006+
callOptions.getDeadline());
10041007
this.context = context;
10051008
this.method = method;
10061009
this.callOptions = callOptions;

core/src/test/java/io/grpc/internal/DelayedClientCallTest.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public class DelayedClientCallTest {
6666

6767
@Test
6868
public void allMethodsForwarded() throws Exception {
69-
DelayedClientCall<String, Integer> delayedClientCall =
70-
new DelayedClientCall<>(callExecutor, fakeClock.getScheduledExecutorService(), null);
69+
DelayedClientCall<String, Integer> delayedClientCall = new DelayedClientCall<>(
70+
"test", callExecutor, fakeClock.getScheduledExecutorService(), null);
7171
callMeMaybe(delayedClientCall.setCall(mockRealCall));
7272
ForwardingTestUtil.testMethodsForwarded(
7373
ClientCall.class,
@@ -94,18 +94,22 @@ public Object get(Method method, int argPos, Class<?> clazz) {
9494
@Test
9595
public void deadlineExceededWhileCallIsStartedButStillPending() {
9696
DelayedClientCall<String, Integer> delayedClientCall = new DelayedClientCall<>(
97-
callExecutor, fakeClock.getScheduledExecutorService(), Deadline.after(10, SECONDS));
97+
"tESt", callExecutor, fakeClock.getScheduledExecutorService(),
98+
Deadline.after(10, SECONDS, fakeClock.getDeadlineTicker()));
9899

99100
delayedClientCall.start(listener, new Metadata());
100101
fakeClock.forwardTime(10, SECONDS);
101102
verify(listener).onClose(statusCaptor.capture(), any(Metadata.class));
102103
assertThat(statusCaptor.getValue().getCode()).isEqualTo(Status.Code.DEADLINE_EXCEEDED);
104+
assertThat(statusCaptor.getValue().getDescription())
105+
.isEqualTo("Deadline CallOptions was exceeded after 10.000000000s waiting for tESt");
103106
}
104107

105108
@Test
106109
public void listenerEventsPropagated() {
107110
DelayedClientCall<String, Integer> delayedClientCall = new DelayedClientCall<>(
108-
callExecutor, fakeClock.getScheduledExecutorService(), Deadline.after(10, SECONDS));
111+
"test", callExecutor, fakeClock.getScheduledExecutorService(),
112+
Deadline.after(10, SECONDS, fakeClock.getDeadlineTicker()));
109113
delayedClientCall.start(listener, new Metadata());
110114
callMeMaybe(delayedClientCall.setCall(mockRealCall));
111115
@SuppressWarnings("unchecked")
@@ -130,7 +134,7 @@ public void listenerEventsPropagated() {
130134
@Test
131135
public void setCallThenStart() {
132136
DelayedClientCall<String, Integer> delayedClientCall = new DelayedClientCall<>(
133-
callExecutor, fakeClock.getScheduledExecutorService(), null);
137+
"test", callExecutor, fakeClock.getScheduledExecutorService(), null);
134138
callMeMaybe(delayedClientCall.setCall(mockRealCall));
135139
delayedClientCall.start(listener, new Metadata());
136140
delayedClientCall.request(1);
@@ -146,7 +150,7 @@ public void setCallThenStart() {
146150
@Test
147151
public void startThenSetCall() {
148152
DelayedClientCall<String, Integer> delayedClientCall = new DelayedClientCall<>(
149-
callExecutor, fakeClock.getScheduledExecutorService(), null);
153+
"test", callExecutor, fakeClock.getScheduledExecutorService(), null);
150154
delayedClientCall.start(listener, new Metadata());
151155
delayedClientCall.request(1);
152156
Runnable r = delayedClientCall.setCall(mockRealCall);
@@ -167,7 +171,7 @@ public void startThenSetCall() {
167171
@SuppressWarnings("unchecked")
168172
public void cancelThenSetCall() {
169173
DelayedClientCall<String, Integer> delayedClientCall = new DelayedClientCall<>(
170-
callExecutor, fakeClock.getScheduledExecutorService(), null);
174+
"test", callExecutor, fakeClock.getScheduledExecutorService(), null);
171175
delayedClientCall.start(listener, new Metadata());
172176
delayedClientCall.request(1);
173177
delayedClientCall.cancel("cancel", new StatusException(Status.CANCELLED));
@@ -183,7 +187,7 @@ public void cancelThenSetCall() {
183187
@SuppressWarnings("unchecked")
184188
public void setCallThenCancel() {
185189
DelayedClientCall<String, Integer> delayedClientCall = new DelayedClientCall<>(
186-
callExecutor, fakeClock.getScheduledExecutorService(), null);
190+
"test", callExecutor, fakeClock.getScheduledExecutorService(), null);
187191
delayedClientCall.start(listener, new Metadata());
188192
delayedClientCall.request(1);
189193
Runnable r = delayedClientCall.setCall(mockRealCall);
@@ -206,7 +210,8 @@ public void delayedCallsRunUnderContext() throws Exception {
206210
Object goldenValue = new Object();
207211
DelayedClientCall<String, Integer> delayedClientCall =
208212
Context.current().withValue(contextKey, goldenValue).call(() ->
209-
new DelayedClientCall<>(callExecutor, fakeClock.getScheduledExecutorService(), null));
213+
new DelayedClientCall<>(
214+
"test", callExecutor, fakeClock.getScheduledExecutorService(), null));
210215
AtomicReference<Context> readyContext = new AtomicReference<>();
211216
delayedClientCall.start(new ClientCall.Listener<Integer>() {
212217
@Override public void onReady() {
@@ -232,7 +237,7 @@ public void delayedCallsRunUnderContext() throws Exception {
232237
@Test
233238
public void listenerThrowsInPendingCallback_cancelsRealCall() {
234239
DelayedClientCall<String, Integer> delayedClientCall = new DelayedClientCall<>(
235-
callExecutor, fakeClock.getScheduledExecutorService(), null);
240+
"test", callExecutor, fakeClock.getScheduledExecutorService(), null);
236241
final RuntimeException boom = new RuntimeException("boom");
237242
ClientCall.Listener<Integer> throwingListener = new ClientCall.Listener<Integer>() {
238243
@Override
@@ -261,7 +266,7 @@ public void start(Listener<Integer> listener, Metadata metadata) {
261266
@Test
262267
public void listenerThrowsInPendingOnHeaders_cancelsRealCall() {
263268
DelayedClientCall<String, Integer> delayedClientCall = new DelayedClientCall<>(
264-
callExecutor, fakeClock.getScheduledExecutorService(), null);
269+
"test", callExecutor, fakeClock.getScheduledExecutorService(), null);
265270
final RuntimeException boom = new RuntimeException("boom");
266271
ClientCall.Listener<Integer> throwingListener = new ClientCall.Listener<Integer>() {
267272
@Override
@@ -286,7 +291,7 @@ public void start(Listener<Integer> listener, Metadata metadata) {
286291
@Test
287292
public void listenerThrowsInPendingOnReady_cancelsRealCall() {
288293
DelayedClientCall<String, Integer> delayedClientCall = new DelayedClientCall<>(
289-
callExecutor, fakeClock.getScheduledExecutorService(), null);
294+
"test", callExecutor, fakeClock.getScheduledExecutorService(), null);
290295
final RuntimeException boom = new RuntimeException("boom");
291296
ClientCall.Listener<Integer> throwingListener = new ClientCall.Listener<Integer>() {
292297
@Override
@@ -311,7 +316,7 @@ public void start(Listener<Integer> listener, Metadata metadata) {
311316
@Test
312317
public void onCloseExceptionCaughtAndLogged() {
313318
DelayedClientCall<String, Integer> delayedClientCall = new DelayedClientCall<>(
314-
callExecutor, fakeClock.getScheduledExecutorService(), null);
319+
"test", callExecutor, fakeClock.getScheduledExecutorService(), null);
315320
final RuntimeException boom = new RuntimeException("boom");
316321
final AtomicReference<Status> observed = new AtomicReference<>();
317322
ClientCall.Listener<Integer> throwingListener = new ClientCall.Listener<Integer>() {
@@ -339,7 +344,7 @@ public void start(Listener<Integer> listener, Metadata metadata) {
339344
@Test
340345
public void listenerThrowsInPassThroughOnMessage_cancelsRealCall() {
341346
DelayedClientCall<String, Integer> delayedClientCall = new DelayedClientCall<>(
342-
callExecutor, fakeClock.getScheduledExecutorService(), null);
347+
"test", callExecutor, fakeClock.getScheduledExecutorService(), null);
343348
final RuntimeException boom = new RuntimeException("boom");
344349
ClientCall.Listener<Integer> throwingListener = new ClientCall.Listener<Integer>() {
345350
@Override
@@ -362,7 +367,7 @@ public void onMessage(Integer msg) {
362367
@Test
363368
public void listenerThrowsInPassThroughOnHeaders_cancelsRealCall() {
364369
DelayedClientCall<String, Integer> delayedClientCall = new DelayedClientCall<>(
365-
callExecutor, fakeClock.getScheduledExecutorService(), null);
370+
"test", callExecutor, fakeClock.getScheduledExecutorService(), null);
366371
final RuntimeException boom = new RuntimeException("boom");
367372
ClientCall.Listener<Integer> throwingListener = new ClientCall.Listener<Integer>() {
368373
@Override
@@ -385,7 +390,7 @@ public void onHeaders(Metadata headers) {
385390
@Test
386391
public void listenerThrowsInPassThroughOnReady_cancelsRealCall() {
387392
DelayedClientCall<String, Integer> delayedClientCall = new DelayedClientCall<>(
388-
callExecutor, fakeClock.getScheduledExecutorService(), null);
393+
"test", callExecutor, fakeClock.getScheduledExecutorService(), null);
389394
final RuntimeException boom = new RuntimeException("boom");
390395
ClientCall.Listener<Integer> throwingListener = new ClientCall.Listener<Integer>() {
391396
@Override
@@ -408,7 +413,7 @@ public void onReady() {
408413
@Test
409414
public void listenerThrowsInPassThrough_subsequentCallbacksSwallowedAndOnCloseOverridden() {
410415
DelayedClientCall<String, Integer> delayedClientCall = new DelayedClientCall<>(
411-
callExecutor, fakeClock.getScheduledExecutorService(), null);
416+
"test", callExecutor, fakeClock.getScheduledExecutorService(), null);
412417
final RuntimeException boom = new RuntimeException("boom");
413418
final AtomicReference<Integer> lastMessage = new AtomicReference<>();
414419
final AtomicReference<Status> closeStatus = new AtomicReference<>();

xds/src/main/java/io/grpc/xds/CdsLoadBalancer2.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@
2525
import com.google.common.primitives.UnsignedInts;
2626
import com.google.errorprone.annotations.CheckReturnValue;
2727
import io.grpc.Attributes;
28+
import io.grpc.ConnectivityState;
2829
import io.grpc.EquivalentAddressGroup;
2930
import io.grpc.HttpConnectProxiedSocketAddress;
3031
import io.grpc.InternalEquivalentAddressGroup;
3132
import io.grpc.InternalLogId;
3233
import io.grpc.LoadBalancer;
3334
import io.grpc.LoadBalancerProvider;
3435
import io.grpc.LoadBalancerRegistry;
36+
import io.grpc.NameResolver;
3537
import io.grpc.Status;
3638
import io.grpc.StatusOr;
3739
import io.grpc.internal.GrpcUtil;
40+
import io.grpc.util.ForwardingLoadBalancerHelper;
3841
import io.grpc.util.GracefulSwitchLoadBalancer;
3942
import io.grpc.util.OutlierDetectionLoadBalancer.OutlierDetectionLoadBalancerConfig;
4043
import io.grpc.xds.CdsLoadBalancerProvider.CdsConfig;
@@ -83,15 +86,17 @@ final class CdsLoadBalancer2 extends LoadBalancer {
8386
private final Helper helper;
8487
private final LoadBalancerRegistry lbRegistry;
8588
private final ClusterState clusterState = new ClusterState();
89+
private final CdsLbHelper cdsLbHelper = new CdsLbHelper();
8690
private GracefulSwitchLoadBalancer delegate;
91+
private boolean addBackendServicePickDetailsLabel;
8792
// Following fields are effectively final.
8893
private String clusterName;
8994
private Subscription clusterSubscription;
9095

9196
CdsLoadBalancer2(Helper helper, LoadBalancerRegistry lbRegistry) {
9297
this.helper = checkNotNull(helper, "helper");
9398
this.lbRegistry = checkNotNull(lbRegistry, "lbRegistry");
94-
this.delegate = new GracefulSwitchLoadBalancer(helper);
99+
this.delegate = new GracefulSwitchLoadBalancer(cdsLbHelper);
95100
logger = XdsLogger.withLogId(InternalLogId.allocate("cds-lb", helper.getAuthority()));
96101
logger.log(XdsLogLevel.INFO, "Created");
97102
}
@@ -126,6 +131,7 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
126131
XdsClusterConfig clusterConfig = clusterConfigOr.getValue();
127132

128133
if (clusterConfig.getChildren() instanceof EndpointConfig) {
134+
addBackendServicePickDetailsLabel = true;
129135
StatusOr<EdsUpdate> edsUpdate = getEdsUpdate(xdsConfig, clusterName);
130136
StatusOr<ClusterResolutionResult> statusOrResult = clusterState.edsUpdateToResult(
131137
clusterName,
@@ -156,8 +162,12 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
156162
resolvedAddresses.toBuilder()
157163
.setLoadBalancingPolicyConfig(gracefulConfig)
158164
.setAddresses(Collections.unmodifiableList(addresses))
165+
.setAttributes(resolvedAddresses.getAttributes().toBuilder()
166+
.set(NameResolver.ATTR_BACKEND_SERVICE, clusterName)
167+
.build())
159168
.build());
160169
} else if (clusterConfig.getChildren() instanceof AggregateConfig) {
170+
addBackendServicePickDetailsLabel = false;
161171
Map<String, PriorityChildConfig> priorityChildConfigs = new HashMap<>();
162172
List<String> leafClusters = ((AggregateConfig) clusterConfig.getChildren()).getLeafNames();
163173
for (String childCluster: leafClusters) {
@@ -196,7 +206,8 @@ public void handleNameResolutionError(Status error) {
196206
public void shutdown() {
197207
logger.log(XdsLogLevel.INFO, "Shutdown");
198208
delegate.shutdown();
199-
delegate = new GracefulSwitchLoadBalancer(helper);
209+
delegate = new GracefulSwitchLoadBalancer(cdsLbHelper);
210+
addBackendServicePickDetailsLabel = false;
200211
if (clusterSubscription != null) {
201212
clusterSubscription.close();
202213
clusterSubscription = null;
@@ -206,6 +217,7 @@ public void shutdown() {
206217
@CheckReturnValue // don't forget to return up the stack after the fail call
207218
private Status fail(Status error) {
208219
delegate.shutdown();
220+
addBackendServicePickDetailsLabel = false;
209221
helper.updateBalancingState(
210222
TRANSIENT_FAILURE, new FixedResultPicker(PickResult.withError(error)));
211223
return Status.OK; // XdsNameResolver isn't a polling NR, so this value doesn't matter
@@ -215,6 +227,38 @@ private String errorPrefix() {
215227
return "CdsLb for " + clusterName + ": ";
216228
}
217229

230+
private final class CdsLbHelper extends ForwardingLoadBalancerHelper {
231+
@Override
232+
public void updateBalancingState(ConnectivityState newState, SubchannelPicker newPicker) {
233+
if (addBackendServicePickDetailsLabel) {
234+
newPicker = new BackendServiceMetricLabelSubchannelPicker(newPicker, clusterName);
235+
}
236+
delegate().updateBalancingState(newState, newPicker);
237+
}
238+
239+
@Override
240+
protected Helper delegate() {
241+
return helper;
242+
}
243+
}
244+
245+
private static final class BackendServiceMetricLabelSubchannelPicker extends SubchannelPicker {
246+
private final SubchannelPicker delegate;
247+
private final String backendService;
248+
249+
private BackendServiceMetricLabelSubchannelPicker(
250+
SubchannelPicker delegate, String backendService) {
251+
this.delegate = checkNotNull(delegate, "delegate");
252+
this.backendService = checkNotNull(backendService, "backendService");
253+
}
254+
255+
@Override
256+
public PickResult pickSubchannel(PickSubchannelArgs args) {
257+
args.getPickDetailsConsumer().addOptionalLabel("grpc.lb.backend_service", backendService);
258+
return delegate.pickSubchannel(args);
259+
}
260+
}
261+
218262
/**
219263
* The number of bits assigned to the fractional part of fixed-point values. We normalize weights
220264
* to a fixed-point number between 0 and 1, representing that item's proportion of traffic (1 ==

xds/src/main/java/io/grpc/xds/ClusterImplLoadBalancer.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import io.grpc.InternalLogId;
3434
import io.grpc.LoadBalancer;
3535
import io.grpc.Metadata;
36-
import io.grpc.NameResolver;
3736
import io.grpc.Status;
3837
import io.grpc.internal.ForwardingClientStreamTracer;
3938
import io.grpc.internal.GrpcUtil;
@@ -154,9 +153,6 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
154153

155154
return childSwitchLb.acceptResolvedAddresses(
156155
resolvedAddresses.toBuilder()
157-
.setAttributes(attributes.toBuilder()
158-
.set(NameResolver.ATTR_BACKEND_SERVICE, cluster)
159-
.build())
160156
.setLoadBalancingPolicyConfig(config.childConfig)
161157
.build());
162158
}
@@ -409,7 +405,6 @@ private RequestLimitingSubchannelPicker(SubchannelPicker delegate,
409405
public PickResult pickSubchannel(PickSubchannelArgs args) {
410406
args.getCallOptions().getOption(ClusterImplLoadBalancerProvider.FILTER_METADATA_CONSUMER)
411407
.accept(filterMetadata);
412-
args.getPickDetailsConsumer().addOptionalLabel("grpc.lb.backend_service", cluster);
413408
for (DropOverload dropOverload : dropPolicies) {
414409
int rand = random.nextInt(1_000_000);
415410
if (rand < dropOverload.dropsPerMillion()) {

0 commit comments

Comments
 (0)