From c333f1fa0721653501d258e967d54f20f932b882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Fri, 30 Aug 2024 08:17:06 +0200 Subject: [PATCH] fix: use core pool size 1 for maintainer The multiplexed session maintainer used a ScheduledExecutorService with a core pool size of zero. This can cause high CPU usage on Java 8 due to https://bugs.openjdk.org/browse/JDK-8129861. Also on higher versions of Java, it is better to use an executor with at least one core thread, instead of letting the executor create a new thread every time a task needs to be executed. Fixes #3313 Fixes https://github.com/GoogleCloudPlatform/pgadapter/issues/2249 Fixes https://github.com/googleapis/java-spanner-jdbc/issues/1736 --- .../cloud/spanner/MultiplexedSessionDatabaseClient.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClient.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClient.java index e742481be2c..e15fdaf6393 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClient.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClient.java @@ -390,11 +390,14 @@ public ReadOnlyTransaction readOnlyTransaction(TimestampBound bound) { /** * It is enough with one executor to maintain the multiplexed sessions in all the clients, as they - * do not need to be updated often, and the maintenance task is light. + * do not need to be updated often, and the maintenance task is light. The core pool size is set + * to 1 to prevent continuous creating and tearing down threads, and to avoid high CPU usage when + * running on Java 8 due to + * https://bugs.openjdk.org/browse/JDK-8129861. */ private static final ScheduledExecutorService MAINTAINER_SERVICE = Executors.newScheduledThreadPool( - /* corePoolSize = */ 0, + /* corePoolSize = */ 1, ThreadFactoryUtil.createVirtualOrPlatformDaemonThreadFactory( "multiplexed-session-maintainer", /* tryVirtual = */ false));