-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for virtual thread support (#1436)
* feat: Add virtual thread support * use public lookup * try to add test * Update VirtualThreadPoolDispatcherSpec.scala * Update VirtualThreadPoolDispatcherSpec.scala * Apply suggestions from code review Co-authored-by: Andy(Jingzhang)Chen <[email protected]> --------- Co-authored-by: He-Pin <[email protected]> Co-authored-by: Andy(Jingzhang)Chen <[email protected]>
- Loading branch information
1 parent
d737c44
commit 2219453
Showing
5 changed files
with
184 additions
and
5 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...src/test/scala-jdk21-only/org/apache/pekko/dispatch/VirtualThreadPoolDispatcherSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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 } | ||
|
||
object VirtualThreadPoolDispatcherSpec { | ||
val config = ConfigFactory.parseString(""" | ||
|virtual-thread-dispatcher { | ||
| executor = virtual-thread-executor | ||
|} | ||
""".stripMargin) | ||
|
||
class InnocentActor extends Actor { | ||
|
||
override def receive = { | ||
case "ping" => | ||
sender() ! "All fine" | ||
} | ||
} | ||
|
||
} | ||
|
||
class VirtualThreadPoolDispatcherSpec extends PekkoSpec(VirtualThreadPoolDispatcherSpec.config) with ImplicitSender { | ||
import VirtualThreadPoolDispatcherSpec._ | ||
|
||
val Iterations = 1000 | ||
|
||
"VirtualThreadPool support" must { | ||
|
||
"handle simple dispatch" in { | ||
val innocentActor = system.actorOf(Props(new InnocentActor).withDispatcher("virtual-thread-dispatcher")) | ||
innocentActor ! "ping" | ||
expectMsg("All fine") | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
actor/src/main/scala/org/apache/pekko/dispatch/VirtualThreadSupport.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* 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 org.apache.pekko.annotation.InternalApi | ||
import org.apache.pekko.util.JavaVersion | ||
|
||
import java.lang.invoke.{ MethodHandles, MethodType } | ||
import java.util.concurrent.{ ExecutorService, ThreadFactory } | ||
import scala.util.control.NonFatal | ||
|
||
@InternalApi | ||
private[dispatch] object VirtualThreadSupport { | ||
private val lookup = MethodHandles.publicLookup() | ||
|
||
/** | ||
* Is virtual thread supported | ||
*/ | ||
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. | ||
*/ | ||
def newVirtualThreadFactory(prefix: String): ThreadFactory = { | ||
require(isSupported, "Virtual thread is not supported.") | ||
try { | ||
val builderClass = ClassLoader.getSystemClassLoader.loadClass("java.lang.Thread$Builder") | ||
val ofVirtualClass = ClassLoader.getSystemClassLoader.loadClass("java.lang.Thread$Builder$OfVirtual") | ||
val ofVirtualMethod = lookup.findStatic(classOf[Thread], "ofVirtual", MethodType.methodType(ofVirtualClass)) | ||
var builder = ofVirtualMethod.invoke() | ||
val nameMethod = lookup.findVirtual(ofVirtualClass, "name", | ||
MethodType.methodType(ofVirtualClass, classOf[String], classOf[Long])) | ||
// TODO support replace scheduler when we drop Java 8 support | ||
val factoryMethod = lookup.findVirtual(builderClass, "factory", MethodType.methodType(classOf[ThreadFactory])) | ||
builder = nameMethod.invoke(builder, prefix + "-virtual-thread-", 0L) | ||
factoryMethod.invoke(builder).asInstanceOf[ThreadFactory] | ||
} catch { | ||
case NonFatal(e) => | ||
// --add-opens java.base/java.lang=ALL-UNNAMED | ||
throw new UnsupportedOperationException("Failed to create virtual thread factory", e) | ||
} | ||
} | ||
|
||
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) => | ||
throw new UnsupportedOperationException("Failed to create newThreadPerTaskExecutor.", e) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters