From ca2e6ae300ba47ce0ee6baaeb464ad87dc6774fd Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Mon, 23 Sep 2024 07:30:17 +0200 Subject: [PATCH 1/3] Grouping Executors related methods into a single class --- .../helidon/webserver/ExecutorsFactory.java | 68 +++++++++++++++++++ .../java/io/helidon/webserver/LoomServer.java | 2 +- .../io/helidon/webserver/ServerListener.java | 7 +- 3 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 webserver/webserver/src/main/java/io/helidon/webserver/ExecutorsFactory.java diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/ExecutorsFactory.java b/webserver/webserver/src/main/java/io/helidon/webserver/ExecutorsFactory.java new file mode 100644 index 00000000000..148dcc80f30 --- /dev/null +++ b/webserver/webserver/src/main/java/io/helidon/webserver/ExecutorsFactory.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.webserver; + +import io.helidon.common.task.HelidonTaskExecutor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadFactory; + +/** Encapsulates operations with {@link Executors}. Helps to workaround + * limitations of GraalVM for JDK21 which doesn't support execution of + * virtual threads and Graal.js code together. New versions of GraalVM + * don't have this limitation, but for those who stick with JDK21, this + * is a serious limitations in using Helidon 4.0.x + *

+ * By moving these "factories" into separate class, it is easier + * to use GraalVM's `@Substitute` mechanism and get Helidon and Graal.js working + * on GraalVM for JDK21. More info + * available in PR-10783. + */ +final class ExecutorsFactory { + + private ExecutorsFactory() { + } + + /** Used by {@link LoomServer} to allocate its executor service. + * + * @return {@link Executors#newVirtualThreadPerTaskExecutor()} + */ + static ExecutorService newLoomServerVirtualThreadPerTaskExecutor() { + return Executors.newVirtualThreadPerTaskExecutor(); + } + + /** Used by {@link ServerListener} to allocate its reader executor. + * + * @return {@link ThreadPerTaskExecutor#create(java.util.concurrent.ThreadFactory)} + */ + static HelidonTaskExecutor newServerListenerReaderExecutor() { + return ThreadPerTaskExecutor.create(virtualThreadFactory()); + } + + /** Used by {@link ServerListener} to allocate its shared executor. + * + * @return {@link Executors#newThreadPerTaskExecutor(java.util.concurrent.ThreadFactory)}. + */ + static ExecutorService newServerListenerSharedExecutor() { + return Executors.newThreadPerTaskExecutor(virtualThreadFactory()); + } + + + private static ThreadFactory virtualThreadFactory() { + return Thread.ofVirtual().factory(); + } +} diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/LoomServer.java b/webserver/webserver/src/main/java/io/helidon/webserver/LoomServer.java index a9d0bebf800..52bfb9d3202 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/LoomServer.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/LoomServer.java @@ -71,7 +71,7 @@ class LoomServer implements WebServer { .id("web-" + WEBSERVER_COUNTER.getAndIncrement()) .build()); this.serverConfig = serverConfig; - this.executorService = Executors.newVirtualThreadPerTaskExecutor(); + this.executorService = ExecutorsFactory.newLoomServerVirtualThreadPerTaskExecutor(); Map sockets = new HashMap<>(serverConfig.sockets()); sockets.put(DEFAULT_SOCKET_NAME, serverConfig); diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/ServerListener.java b/webserver/webserver/src/main/java/io/helidon/webserver/ServerListener.java index c5e0761f7a1..118ee8ac54b 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/ServerListener.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/ServerListener.java @@ -60,6 +60,7 @@ import static java.lang.System.Logger.Level.ERROR; import static java.lang.System.Logger.Level.INFO; import static java.lang.System.Logger.Level.TRACE; +import java.util.concurrent.ThreadFactory; class ServerListener implements ListenerContext { private static final System.Logger LOGGER = System.getLogger(ServerListener.class.getName()); @@ -143,12 +144,10 @@ class ServerListener implements ListenerContext { .unstarted(this::listen); // to read requests and execute tasks - this.readerExecutor = ThreadPerTaskExecutor.create(Thread.ofVirtual() - .factory()); + this.readerExecutor = ExecutorsFactory.newServerListenerReaderExecutor(); // to do anything else (writers etc.) - this.sharedExecutor = Executors.newThreadPerTaskExecutor(Thread.ofVirtual() - .factory()); + this.sharedExecutor = ExecutorsFactory.newServerListenerSharedExecutor(); this.closeFuture = new CompletableFuture<>(); From 4613606385e64fcde1b7bc95ee2099541dff8f65 Mon Sep 17 00:00:00 2001 From: Tomas Langer Date: Mon, 23 Sep 2024 16:43:55 +0200 Subject: [PATCH 2/3] Update ExecutorsFactory.java Copyright and javadoc fix. --- .../src/main/java/io/helidon/webserver/ExecutorsFactory.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/ExecutorsFactory.java b/webserver/webserver/src/main/java/io/helidon/webserver/ExecutorsFactory.java index 148dcc80f30..12256bb016d 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/ExecutorsFactory.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/ExecutorsFactory.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Oracle and/or its affiliates. + * Copyright (c) 2024 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; -/** Encapsulates operations with {@link Executors}. Helps to workaround +/** + * Encapsulates operations with {@link Executors}. Helps to workaround * limitations of GraalVM for JDK21 which doesn't support execution of * virtual threads and Graal.js code together. New versions of GraalVM * don't have this limitation, but for those who stick with JDK21, this From b4eb0e5e4af3d63011cb3b3b5567fbd64fe72276 Mon Sep 17 00:00:00 2001 From: Tomas Langer Date: Mon, 23 Sep 2024 17:23:18 +0200 Subject: [PATCH 3/3] Checkstyle and copyright fixes Signed-off-by: Tomas Langer --- .../io/helidon/webserver/ExecutorsFactory.java | 15 +++++++++------ .../java/io/helidon/webserver/LoomServer.java | 1 - .../java/io/helidon/webserver/ServerListener.java | 4 +--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/ExecutorsFactory.java b/webserver/webserver/src/main/java/io/helidon/webserver/ExecutorsFactory.java index 12256bb016d..07bf2a26aef 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/ExecutorsFactory.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/ExecutorsFactory.java @@ -16,12 +16,13 @@ package io.helidon.webserver; -import io.helidon.common.task.HelidonTaskExecutor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; -/** +import io.helidon.common.task.HelidonTaskExecutor; + +/** * Encapsulates operations with {@link Executors}. Helps to workaround * limitations of GraalVM for JDK21 which doesn't support execution of * virtual threads and Graal.js code together. New versions of GraalVM @@ -38,7 +39,8 @@ final class ExecutorsFactory { private ExecutorsFactory() { } - /** Used by {@link LoomServer} to allocate its executor service. + /** + * Used by {@link LoomServer} to allocate its executor service. * * @return {@link Executors#newVirtualThreadPerTaskExecutor()} */ @@ -46,7 +48,8 @@ static ExecutorService newLoomServerVirtualThreadPerTaskExecutor() { return Executors.newVirtualThreadPerTaskExecutor(); } - /** Used by {@link ServerListener} to allocate its reader executor. + /** + * Used by {@link ServerListener} to allocate its reader executor. * * @return {@link ThreadPerTaskExecutor#create(java.util.concurrent.ThreadFactory)} */ @@ -54,7 +57,8 @@ static HelidonTaskExecutor newServerListenerReaderExecutor() { return ThreadPerTaskExecutor.create(virtualThreadFactory()); } - /** Used by {@link ServerListener} to allocate its shared executor. + /** + * Used by {@link ServerListener} to allocate its shared executor. * * @return {@link Executors#newThreadPerTaskExecutor(java.util.concurrent.ThreadFactory)}. */ @@ -62,7 +66,6 @@ static ExecutorService newServerListenerSharedExecutor() { return Executors.newThreadPerTaskExecutor(virtualThreadFactory()); } - private static ThreadFactory virtualThreadFactory() { return Thread.ofVirtual().factory(); } diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/LoomServer.java b/webserver/webserver/src/main/java/io/helidon/webserver/LoomServer.java index 52bfb9d3202..ee5e28c6f6b 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/LoomServer.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/LoomServer.java @@ -25,7 +25,6 @@ import java.util.Timer; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/ServerListener.java b/webserver/webserver/src/main/java/io/helidon/webserver/ServerListener.java index 118ee8ac54b..31e3fd9e799 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/ServerListener.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/ServerListener.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2023 Oracle and/or its affiliates. + * Copyright (c) 2022, 2024 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,7 +34,6 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; @@ -60,7 +59,6 @@ import static java.lang.System.Logger.Level.ERROR; import static java.lang.System.Logger.Level.INFO; import static java.lang.System.Logger.Level.TRACE; -import java.util.concurrent.ThreadFactory; class ServerListener implements ListenerContext { private static final System.Logger LOGGER = System.getLogger(ServerListener.class.getName());