-
Notifications
You must be signed in to change notification settings - Fork 156
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
feat: Enhance virtual thread support #1724
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.pekko.dispatch | ||
|
||
import com.typesafe.config.ConfigFactory | ||
|
||
import org.apache.pekko | ||
import pekko.actor.{ Actor, Props } | ||
import pekko.testkit.{ ImplicitSender, PekkoSpec } | ||
import pekko.util.JavaVersion | ||
|
||
object ForkJoinPoolVirtualThreadSpec { | ||
val config = ConfigFactory.parseString(""" | ||
|virtual { | ||
| task-dispatcher { | ||
| mailbox-type = "org.apache.pekko.dispatch.SingleConsumerOnlyUnboundedMailbox" | ||
| throughput = 5 | ||
| fork-join-executor { | ||
| parallelism-factor = 2 | ||
| parallelism-max = 2 | ||
| parallelism-min = 2 | ||
| virtualize = on | ||
| } | ||
| } | ||
|} | ||
""".stripMargin) | ||
|
||
class ThreadNameActor extends Actor { | ||
|
||
override def receive = { | ||
case "ping" => | ||
sender() ! Thread.currentThread().getName | ||
} | ||
} | ||
|
||
} | ||
|
||
class ForkJoinPoolVirtualThreadSpec extends PekkoSpec(ForkJoinPoolVirtualThreadSpec.config) with ImplicitSender { | ||
import ForkJoinPoolVirtualThreadSpec._ | ||
|
||
"PekkoForkJoinPool" must { | ||
|
||
"support virtualization with Virtual Thread" in { | ||
val actor = system.actorOf(Props(new ThreadNameActor).withDispatcher("virtual.task-dispatcher")) | ||
for (_ <- 1 to 1000) { | ||
actor ! "ping" | ||
expectMsgPF() { case name: String => | ||
name should include("ForkJoinPoolVirtualThreadSpec-virtual.task-dispatcher-virtual-thread-") | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -487,6 +487,19 @@ pekko { | |
# This config is new in Pekko v1.1.0 and only has an effect if you are running with JDK 9 and above. | ||
# Read the documentation on `java.util.concurrent.ForkJoinPool` to find out more. Default in hex is 0x7fff. | ||
maximum-pool-size = 32767 | ||
|
||
# This config is new in Pekko v1.2.0 and only has an effect if you are running with JDK 21 and above, | ||
# When set to `on` but underlying runtime does not support virtual threads, an Exception will throw. | ||
# Virtualize this dispatcher as a virtual-thread-executor | ||
# Valid values are: `on`, `off` | ||
# | ||
# NOTE: THIS IS AN ADVANCED OPTION, USE WITH CAUTION. | ||
# REQUIREMENTS: | ||
# 1. JDK 21+ | ||
# 2. add options to the JVM: | ||
# --add-opens=java.base/jdk.internal.misc=ALL-UNNAMED | ||
# --add-opens=java.base/java.lang=ALL-UNNAMED | ||
virtualize = off | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. behind an option |
||
} | ||
|
||
# This will be used if you have set "executor = "thread-pool-executor"" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,12 @@ | |
|
||
package org.apache.pekko.dispatch | ||
|
||
import org.apache.pekko.annotation.InternalApi | ||
import org.apache.pekko.util.JavaVersion | ||
import org.apache.pekko | ||
import pekko.annotation.InternalApi | ||
import pekko.util.JavaVersion | ||
|
||
import java.lang.invoke.{ MethodHandles, MethodType } | ||
import java.util.concurrent.{ ExecutorService, ForkJoinPool, ThreadFactory } | ||
import java.util.concurrent.{ ExecutorService, ForkJoinPool, ForkJoinWorkerThread, ThreadFactory } | ||
import scala.util.control.NonFatal | ||
|
||
@InternalApi | ||
|
@@ -34,8 +35,26 @@ private[dispatch] object VirtualThreadSupport { | |
val isSupported: Boolean = JavaVersion.majorVersion >= 21 | ||
|
||
/** | ||
* Create a virtual thread factory with a executor, the executor will be used as the scheduler of | ||
* virtual thread. | ||
* Create a newThreadPerTaskExecutor with the specified thread factory. | ||
*/ | ||
def newThreadPerTaskExecutor(threadFactory: ThreadFactory): ExecutorService = { | ||
require(threadFactory != null, "threadFactory should not be null.") | ||
try { | ||
val executorsClazz = ClassLoader.getSystemClassLoader.loadClass("java.util.concurrent.Executors") | ||
val newThreadPerTaskExecutorMethod = lookup.findStatic( | ||
executorsClazz, | ||
"newThreadPerTaskExecutor", | ||
MethodType.methodType(classOf[ExecutorService], classOf[ThreadFactory])) | ||
newThreadPerTaskExecutorMethod.invoke(threadFactory).asInstanceOf[ExecutorService] | ||
} catch { | ||
case NonFatal(e) => | ||
// --add-opens java.base/java.lang=ALL-UNNAMED | ||
throw new UnsupportedOperationException("Failed to create newThreadPerTaskExecutor.", e) | ||
} | ||
} | ||
|
||
/** | ||
* Create a virtual thread factory with the default Virtual Thread executor. | ||
*/ | ||
def newVirtualThreadFactory(prefix: String): ThreadFactory = { | ||
require(isSupported, "Virtual thread is not supported.") | ||
|
@@ -57,19 +76,38 @@ private[dispatch] object VirtualThreadSupport { | |
} | ||
} | ||
|
||
def newThreadPerTaskExecutor(threadFactory: ThreadFactory): ExecutorService = { | ||
require(threadFactory != null, "threadFactory should not be null.") | ||
/** | ||
* Create a virtual thread factory with the specified executor as the scheduler of virtual thread. | ||
*/ | ||
def newVirtualThreadFactory(prefix: String, executor: ExecutorService): ThreadFactory = | ||
try { | ||
val executorsClazz = ClassLoader.getSystemClassLoader.loadClass("java.util.concurrent.Executors") | ||
val newThreadPerTaskExecutorMethod = lookup.findStatic( | ||
executorsClazz, | ||
"newThreadPerTaskExecutor", | ||
MethodType.methodType(classOf[ExecutorService], classOf[ThreadFactory])) | ||
newThreadPerTaskExecutorMethod.invoke(threadFactory).asInstanceOf[ExecutorService] | ||
val builderClass = ClassLoader.getSystemClassLoader.loadClass("java.lang.Thread$Builder") | ||
val ofVirtualClass = ClassLoader.getSystemClassLoader.loadClass("java.lang.Thread$Builder$OfVirtual") | ||
val ofVirtualMethod = classOf[Thread].getDeclaredMethod("ofVirtual") | ||
var builder = ofVirtualMethod.invoke(null) | ||
if (executor != null) { | ||
val clazz = builder.getClass | ||
val field = clazz.getDeclaredField("scheduler") | ||
field.setAccessible(true) | ||
field.set(builder, executor) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use java 8 reflect for now. |
||
val nameMethod = ofVirtualClass.getDeclaredMethod("name", classOf[String], classOf[Long]) | ||
val factoryMethod = builderClass.getDeclaredMethod("factory") | ||
val zero = java.lang.Long.valueOf(0L) | ||
builder = nameMethod.invoke(builder, prefix + "-virtual-thread-", zero) | ||
factoryMethod.invoke(builder).asInstanceOf[ThreadFactory] | ||
} catch { | ||
case NonFatal(e) => | ||
// --add-opens java.base/java.lang=ALL-UNNAMED | ||
throw new UnsupportedOperationException("Failed to create newThreadPerTaskExecutor.", e) | ||
throw new UnsupportedOperationException("Failed to create virtual thread factory", e) | ||
} | ||
|
||
object CarrierThreadFactory extends ForkJoinPool.ForkJoinWorkerThreadFactory { | ||
private val clazz = ClassLoader.getSystemClassLoader.loadClass("jdk.internal.misc.CarrierThread") | ||
// TODO lookup.findClass is only available in Java 9 | ||
private val constructor = clazz.getDeclaredConstructor(classOf[ForkJoinPool]) | ||
override def newThread(pool: ForkJoinPool): ForkJoinWorkerThread = { | ||
constructor.newInstance(pool).asInstanceOf[ForkJoinWorkerThread] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use java 8 reflect for now. |
||
} | ||
} | ||
|
||
|
@@ -79,7 +117,7 @@ private[dispatch] object VirtualThreadSupport { | |
def getVirtualThreadDefaultScheduler: ForkJoinPool = | ||
try { | ||
require(isSupported, "Virtual thread is not supported.") | ||
val clazz = Class.forName("java.lang.VirtualThread") | ||
val clazz = ClassLoader.getSystemClassLoader.loadClass("java.lang.VirtualThread") | ||
val fieldName = "DEFAULT_SCHEDULER" | ||
val field = clazz.getDeclaredField(fieldName) | ||
field.setAccessible(true) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it run on a virtual thread, and virtual thread is running on a
CarrierThread