Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -16,7 +16,8 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -178,9 +179,9 @@ public Builder setStorage(@Nonnull Storage storage) {

/**
* Maximum number of threads in the adapter's built-in thread pool. Defaults to 400, matching
* the worker's default 200 workflow task slots and 200 activity slots. Threads start on demand
* and stop after a minute without work. Calls beyond the limit wait in a queue. Cannot be
* combined with {@link #setExecutor(Executor)}.
* the worker's default 200 workflow task slots and 200 activity slots. Idle threads are reused,
* new threads start only when none is idle, and threads stop after a minute without work. Calls
* beyond the limit wait in a queue. Cannot be combined with {@link #setExecutor(Executor)}.
*/
public Builder setThreadPoolMaxSize(int threadPoolMaxSize) {
if (threadPoolMaxSize <= 0) {
Expand Down Expand Up @@ -220,25 +221,40 @@ public GcsStorageClientAdapter build() {
}

private static ThreadPoolExecutor newThreadPool(int maxSize) {
// Core and maximum sizes match because the pool only grows past its core size when the
// queue is full, and this queue is unbounded. Core threads time out when idle.
ThreadPoolExecutor pool =
new ThreadPoolExecutor(
maxSize,
maxSize,
THREAD_KEEP_ALIVE_SECONDS,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
runnable -> {
Thread thread =
new Thread(
runnable, "temporal-gcs-storage-" + POOL_THREAD_COUNTER.incrementAndGet());
// Daemon threads do not keep the JVM alive if the adapter is never closed.
thread.setDaemon(true);
return thread;
});
pool.allowCoreThreadTimeOut(true);
return pool;
HandOffQueue queue = new HandOffQueue();
// With no core threads, every thread stops after the keep-alive without work.
return new ThreadPoolExecutor(
0,
maxSize,
THREAD_KEEP_ALIVE_SECONDS,
TimeUnit.SECONDS,
queue,
runnable -> {
Thread thread =
new Thread(
runnable, "temporal-gcs-storage-" + POOL_THREAD_COUNTER.incrementAndGet());
// Daemon threads do not keep the JVM alive if the adapter is never closed.
thread.setDaemon(true);
return thread;
},
(task, pool) -> {
if (pool.isShutdown()) {
throw new RejectedExecutionException("GCS storage adapter is closed");
}
// Every thread is busy and the pool is at its maximum size, so the call waits.
queue.put(task);
});
}
}

/**
* Gives a task to an idle thread if one is waiting. Otherwise {@code offer} fails, so the pool
* starts a new thread, or, at its maximum size, the rejection handler queues the task.
*/
private static final class HandOffQueue extends LinkedTransferQueue<Runnable> {
@Override
public boolean offer(Runnable task) {
return tryTransfer(task);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,36 @@ public void builtInPoolRunsCallsOnNamedDaemonThreads() throws Exception {
assertTrue(threads.get(0).isDaemon());
}

@Test
public void builtInPoolReusesIdleThreads() throws Exception {
List<Thread> threads = new CopyOnWriteArrayList<>();
when(storage.readAllBytes(any(BlobId.class)))
.thenAnswer(
invocation -> {
threads.add(Thread.currentThread());
return new byte[] {1};
});
try (GcsStorageClientAdapter pooled =
GcsStorageClientAdapter.newBuilder().setStorage(storage).build()) {
for (int i = 0; i < 20; i++) {
if (i > 0) {
awaitIdle(threads.get(0));
}
pooled.getObject("bucket", "object-" + i).get(5, TimeUnit.SECONDS);
}
}
assertEquals(Collections.nCopies(20, threads.get(0)), threads);
}

/** Waits until a pool thread is parked waiting for its next task. */
private static void awaitIdle(Thread thread) throws InterruptedException {
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
while (thread.getState() != Thread.State.TIMED_WAITING) {
assertTrue("pool thread did not become idle", System.nanoTime() < deadline);
Thread.sleep(1);
}
}

@Test
public void builtInPoolQueuesCallsBeyondMaxSize() throws Exception {
AtomicInteger running = new AtomicInteger();
Expand Down
Loading