Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grouping Executors related methods into a single class #9271

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -0,0 +1,69 @@
/*
* 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.
* 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
* <p>
* By moving these <em>"factories"</em> 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
* <a href="https://github.com/enso-org/enso/pull/10783#discussion_r1768000821">available in PR-10783</a>.
*/
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ListenerConfig> sockets = new HashMap<>(serverConfig.sockets());
sockets.put(DEFAULT_SOCKET_NAME, serverConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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<>();

Expand Down
Loading