Skip to content

Commit

Permalink
Scale the system executor provider with the number of pull channels o…
Browse files Browse the repository at this point in the history
…pened. (#4592)

Make the SubscriberStubSettings refer to the user provided executor provider instead of a fixed instantiation of it.  If the user provides an InstantiatingExecutorProvider instead of a FixedExecutorProvider, this will actually instantiate more than one as the user would expect.  It will still only instantiate one for all connections to share, and will do so until the next PR which will make them have different stub instantiations.
  • Loading branch information
dpcollins-google authored and sduskis committed Feb 28, 2019
1 parent b240857 commit f5fae7e
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -93,9 +94,6 @@ public class Subscriber extends AbstractApiService {
private static final Duration UNARY_TIMEOUT = Duration.ofSeconds(60);
private static final Duration ACK_EXPIRATION_PADDING = Duration.ofSeconds(5);

private static final ScheduledExecutorService SHARED_SYSTEM_EXECUTOR =
InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(6).build().getExecutor();

private static final Logger logger = Logger.getLogger(Subscriber.class.getName());

private final String subscriptionName;
Expand Down Expand Up @@ -132,6 +130,8 @@ private Subscriber(Builder builder) {
.setLimitExceededBehavior(LimitExceededBehavior.ThrowException)
.build());

this.numPullers = builder.parallelPullCount;

executor = builder.executorProvider.getExecutor();
if (builder.executorProvider.shouldAutoClose()) {
closeables.add(
Expand All @@ -142,8 +142,16 @@ public void close() throws IOException {
}
});
}
alarmsExecutor = builder.systemExecutorProvider.getExecutor();
if (builder.systemExecutorProvider.shouldAutoClose()) {

ExecutorProvider systemExecutorProvider = builder.systemExecutorProvider;
if (systemExecutorProvider == null) {
systemExecutorProvider =
FixedExecutorProvider.create(
Executors.newScheduledThreadPool(Math.max(6, 2 * numPullers)));
}

alarmsExecutor = systemExecutorProvider.getExecutor();
if (systemExecutorProvider.shouldAutoClose()) {
closeables.add(
new AutoCloseable() {
@Override
Expand All @@ -153,7 +161,6 @@ public void close() throws IOException {
});
}

this.numPullers = builder.parallelPullCount;
TransportChannelProvider channelProvider = builder.channelProvider;
if (channelProvider.acceptsPoolSize()) {
channelProvider = channelProvider.withPoolSize(numPullers);
Expand All @@ -162,7 +169,7 @@ public void close() throws IOException {
try {
this.subStubSettings =
SubscriberStubSettings.newBuilder()
.setExecutorProvider(FixedExecutorProvider.create(alarmsExecutor))
.setExecutorProvider(systemExecutorProvider)
.setCredentialsProvider(builder.credentialsProvider)
.setTransportChannelProvider(channelProvider)
.setHeaderProvider(builder.headerProvider)
Expand Down Expand Up @@ -404,7 +411,7 @@ public static final class Builder {
FlowControlSettings.newBuilder().setMaxOutstandingElementCount(1000L).build();

ExecutorProvider executorProvider = DEFAULT_EXECUTOR_PROVIDER;
ExecutorProvider systemExecutorProvider = FixedExecutorProvider.create(SHARED_SYSTEM_EXECUTOR);
ExecutorProvider systemExecutorProvider = null;
TransportChannelProvider channelProvider =
SubscriptionAdminSettings.defaultGrpcTransportProviderBuilder()
.setMaxInboundMessageSize(MAX_INBOUND_MESSAGE_SIZE)
Expand Down

0 comments on commit f5fae7e

Please sign in to comment.