Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.core.Distribution;
import com.google.api.gax.core.ExecutorProvider;
import com.google.api.gax.core.FixedExecutorProvider;
import com.google.api.gax.core.InstantiatingExecutorProvider;
import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.NoHeaderProvider;
Expand All @@ -41,12 +40,15 @@
import com.google.cloud.pubsub.v1.stub.SubscriberStubSettings;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.pubsub.v1.ProjectSubscriptionName;
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.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -138,19 +140,16 @@ private Subscriber(Builder builder) {
executorProvider = builder.executorProvider;

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
public void close() {
alarmsExecutor.shutdown();
// Force a shut down, since there are some jobs like the Watcher and MessageDispatcher
// that don't shut down cleanly, but don't need to stay on.
alarmsExecutor.shutdownNow();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will cause issue for all inflight messages as well as nacking of any pending message since this same system executor is used to all message.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack. We'll have to manually shut down a couple of scheduled threads, which will cause some difficulty. I'll revert the shutdownNow(), but keep the thread pool setup for this PR.

}
});
}
Expand Down Expand Up @@ -405,6 +404,7 @@ public static final class Builder {
InstantiatingExecutorProvider.newBuilder()
.setExecutorThreadCount(THREADS_PER_CHANNEL)
.build();
private static final AtomicInteger SYSTEM_EXECUTOR_COUNTER = new AtomicInteger();

private String subscriptionName;
private MessageReceiver receiver;
Expand Down Expand Up @@ -543,6 +543,28 @@ Builder setClock(ApiClock clock) {
}

public Subscriber build() {
if (systemExecutorProvider == null) {
ThreadFactory threadFactory =
new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("Subscriber-SE-" + SYSTEM_EXECUTOR_COUNTER.incrementAndGet() + "-%d")
.build();
int threadCount = Math.max(6, 2 * parallelPullCount);
final ScheduledExecutorService executor =
Executors.newScheduledThreadPool(threadCount, threadFactory);
systemExecutorProvider =
new ExecutorProvider() {
@Override
public boolean shouldAutoClose() {
return true;
}

@Override
public ScheduledExecutorService getExecutor() {
return executor;
}
};
}
return new Subscriber(this);
}
}
Expand Down