Skip to content

Commit

Permalink
core: Use offloadExecutor for CallCredentials (#9263)
Browse files Browse the repository at this point in the history
Change the construction of CallCredentialsApplyingTransportFactory in
ManagedChannelImpl to use the offloadExecutor as indicated in
#6279 (comment) .
  • Loading branch information
larry-safran committed Jun 15, 2022
1 parent 75aeccd commit 8e77936
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
27 changes: 13 additions & 14 deletions core/src/main/java/io/grpc/internal/ManagedChannelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,12 @@ ClientStream newSubstream(
this.executor = checkNotNull(executorPool.getObject(), "executor");
this.originalChannelCreds = builder.channelCredentials;
this.originalTransportFactory = clientTransportFactory;
this.offloadExecutorHolder =
new ExecutorHolder(checkNotNull(builder.offloadExecutorPool, "offloadExecutorPool"));
this.transportFactory = new CallCredentialsApplyingTransportFactory(
clientTransportFactory, builder.callCredentials, this.executor);
clientTransportFactory, builder.callCredentials, this.offloadExecutorHolder);
this.oobTransportFactory = new CallCredentialsApplyingTransportFactory(
clientTransportFactory, null, this.executor);
clientTransportFactory, null, this.offloadExecutorHolder);
this.scheduledExecutor =
new RestrictedScheduledExecutor(transportFactory.getScheduledExecutorService());
maxTraceEvents = builder.maxTraceEvents;
Expand All @@ -636,9 +638,6 @@ ClientStream newSubstream(
builder.proxyDetector != null ? builder.proxyDetector : GrpcUtil.DEFAULT_PROXY_DETECTOR;
this.retryEnabled = builder.retryEnabled;
this.loadBalancerFactory = new AutoConfiguredLoadBalancerFactory(builder.defaultLbPolicy);
this.offloadExecutorHolder =
new ExecutorHolder(
checkNotNull(builder.offloadExecutorPool, "offloadExecutorPool"));
this.nameResolverRegistry = builder.nameResolverRegistry;
ScParser serviceConfigParser =
new ScParser(
Expand All @@ -654,14 +653,7 @@ ClientStream newSubstream(
.setScheduledExecutorService(scheduledExecutor)
.setServiceConfigParser(serviceConfigParser)
.setChannelLogger(channelLogger)
.setOffloadExecutor(
// Avoid creating the offloadExecutor until it is first used
new Executor() {
@Override
public void execute(Runnable command) {
offloadExecutorHolder.getExecutor().execute(command);
}
})
.setOffloadExecutor(this.offloadExecutorHolder)
.build();
this.authorityOverride = builder.authorityOverride;
this.nameResolverFactory = builder.nameResolverFactory;
Expand Down Expand Up @@ -2219,8 +2211,10 @@ protected void handleNotInUse() {

/**
* Lazily request for Executor from an executor pool.
* Also act as an Executor directly to simply run a cmd
*/
private static final class ExecutorHolder {
@VisibleForTesting
static final class ExecutorHolder implements Executor {
private final ObjectPool<? extends Executor> pool;
private Executor executor;

Expand All @@ -2240,6 +2234,11 @@ synchronized void release() {
executor = pool.returnObject(executor);
}
}

@Override
public void execute(Runnable command) {
getExecutor().execute(command);
}
}

private static final class RestrictedScheduledExecutor implements ScheduledExecutorService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2396,9 +2396,12 @@ public ClientStream answer(InvocationOnMock in) throws Throwable {
updateBalancingStateSafely(helper, READY, mockPicker);
executor.runDueTasks();
ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<Executor> executorArgumentCaptor = ArgumentCaptor.forClass(null);
ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
verify(creds).applyRequestMetadata(infoCaptor.capture(),
same(executor.getScheduledExecutorService()), applierCaptor.capture());
executorArgumentCaptor.capture(), applierCaptor.capture());
assertSame(offloadExecutor,
((ManagedChannelImpl.ExecutorHolder) executorArgumentCaptor.getValue()).getExecutor());
assertEquals("testValue", testKey.get(credsApplyContexts.poll()));
assertEquals(AUTHORITY, infoCaptor.getValue().getAuthority());
assertEquals(SecurityLevel.NONE, infoCaptor.getValue().getSecurityLevel());
Expand All @@ -2423,7 +2426,9 @@ public ClientStream answer(InvocationOnMock in) throws Throwable {
call.start(mockCallListener, new Metadata());

verify(creds, times(2)).applyRequestMetadata(infoCaptor.capture(),
same(executor.getScheduledExecutorService()), applierCaptor.capture());
executorArgumentCaptor.capture(), applierCaptor.capture());
assertSame(offloadExecutor,
((ManagedChannelImpl.ExecutorHolder) executorArgumentCaptor.getValue()).getExecutor());
assertEquals("testValue", testKey.get(credsApplyContexts.poll()));
assertEquals(AUTHORITY, infoCaptor.getValue().getAuthority());
assertEquals(SecurityLevel.NONE, infoCaptor.getValue().getSecurityLevel());
Expand Down

0 comments on commit 8e77936

Please sign in to comment.