From 67a516ef3afe50f5741a48b378b5ca78a4e82bc3 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Thu, 20 Feb 2020 17:45:20 -0500 Subject: [PATCH 1/2] fix: scale number of threads to scale with number of cpus This is temporary solution until something better can be worked out. Ideally we would separate the executor for gax and the transport provider. But there are some backwards compatibility concerns to workout first. --- .../google/api/gax/core/InstantiatingExecutorProvider.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/gax/src/main/java/com/google/api/gax/core/InstantiatingExecutorProvider.java b/gax/src/main/java/com/google/api/gax/core/InstantiatingExecutorProvider.java index 893f00151..2c12983a9 100644 --- a/gax/src/main/java/com/google/api/gax/core/InstantiatingExecutorProvider.java +++ b/gax/src/main/java/com/google/api/gax/core/InstantiatingExecutorProvider.java @@ -41,8 +41,6 @@ */ @AutoValue public abstract class InstantiatingExecutorProvider implements ExecutorProvider { - // The number of threads to use with the default executor. - private static final int DEFAULT_EXECUTOR_THREADS = 4; // Thread factory to use to create our worker threads private static final ThreadFactory DEFAULT_THREAD_FACTORY = new ThreadFactory() { @@ -79,8 +77,11 @@ public boolean shouldAutoClose() { public abstract Builder toBuilder(); public static Builder newBuilder() { + int numCpus = Runtime.getRuntime().availableProcessors(); + int numThreads = Math.max(4, (int) Math.ceil(numCpus * 1.5)); + return new AutoValue_InstantiatingExecutorProvider.Builder() - .setExecutorThreadCount(DEFAULT_EXECUTOR_THREADS) + .setExecutorThreadCount(numThreads) .setThreadFactory(DEFAULT_THREAD_FACTORY); } From 9864ee8c3f1e03c4d742636c932cc73d4721a387 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Thu, 20 Feb 2020 18:00:06 -0500 Subject: [PATCH 2/2] use numCpus --- .../com/google/api/gax/core/InstantiatingExecutorProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gax/src/main/java/com/google/api/gax/core/InstantiatingExecutorProvider.java b/gax/src/main/java/com/google/api/gax/core/InstantiatingExecutorProvider.java index 2c12983a9..c91b044cb 100644 --- a/gax/src/main/java/com/google/api/gax/core/InstantiatingExecutorProvider.java +++ b/gax/src/main/java/com/google/api/gax/core/InstantiatingExecutorProvider.java @@ -78,7 +78,7 @@ public boolean shouldAutoClose() { public static Builder newBuilder() { int numCpus = Runtime.getRuntime().availableProcessors(); - int numThreads = Math.max(4, (int) Math.ceil(numCpus * 1.5)); + int numThreads = Math.max(4, numCpus); return new AutoValue_InstantiatingExecutorProvider.Builder() .setExecutorThreadCount(numThreads)