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

[SPARK-50383][CORE] Support Virtual Threads in REST Submission API #48923

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -18,6 +18,7 @@
package org.apache.spark.deploy.rest

import java.util.EnumSet
import java.util.concurrent.{Executors, ExecutorService}

import scala.io.Source

Expand All @@ -33,7 +34,7 @@ import org.json4s.jackson.JsonMethods._
import org.apache.spark.{SPARK_VERSION => sparkVersion, SparkConf}
import org.apache.spark.internal.{Logging, MDC}
import org.apache.spark.internal.LogKeys._
import org.apache.spark.internal.config.{MASTER_REST_SERVER_FILTERS, MASTER_REST_SERVER_MAX_THREADS}
import org.apache.spark.internal.config.{MASTER_REST_SERVER_FILTERS, MASTER_REST_SERVER_MAX_THREADS, MASTER_REST_SERVER_VIRTUAL_THREADS}
import org.apache.spark.util.Utils

/**
Expand Down Expand Up @@ -93,6 +94,12 @@ private[spark] abstract class RestSubmissionServer(
*/
private def doStart(startPort: Int): (Server, Int) = {
val threadPool = new QueuedThreadPool(masterConf.get(MASTER_REST_SERVER_MAX_THREADS))
if (Utils.isJavaVersionAtLeast21 && masterConf.get(MASTER_REST_SERVER_VIRTUAL_THREADS)) {
val newVirtualThreadPerTaskExecutor =
classOf[Executors].getMethod("newVirtualThreadPerTaskExecutor")
val service = newVirtualThreadPerTaskExecutor.invoke(null).asInstanceOf[ExecutorService]
threadPool.setVirtualThreadsExecutor(service)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thread number configured for the threadPool also determines the number of virtual threads?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example #48921.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, AFAIK, this is orthogonally handled in Jetty layer, @viirya .

}
threadPool.setDaemon(true)
val server = new Server(threadPool)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2000,6 +2000,13 @@ package object config {
.toSequence
.createWithDefault(Nil)

private[spark] val MASTER_REST_SERVER_VIRTUAL_THREADS =
ConfigBuilder("spark.master.rest.virtualThread.enabled")
.doc("If true, Spark master tries to use Java 21 virtual thread for REST API.")
.version("4.0.0")
.booleanConf
.createWithDefault(false)

private[spark] val MASTER_UI_PORT = ConfigBuilder("spark.master.ui.port")
.version("1.1.0")
.intConf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import java.util.Base64
import scala.collection.mutable

import jakarta.servlet.http.HttpServletResponse
import org.eclipse.jetty.util.thread.QueuedThreadPool
import org.eclipse.jetty.util.thread.ThreadPool.SizedThreadPool
import org.json4s.JsonAST._
import org.json4s.jackson.JsonMethods._
Expand All @@ -34,7 +35,7 @@ import org.apache.spark.deploy.{SparkSubmit, SparkSubmitArguments}
import org.apache.spark.deploy.DeployMessages._
import org.apache.spark.deploy.master.DriverState._
import org.apache.spark.deploy.master.RecoveryState
import org.apache.spark.internal.config.{MASTER_REST_SERVER_FILTERS, MASTER_REST_SERVER_MAX_THREADS}
import org.apache.spark.internal.config.{MASTER_REST_SERVER_FILTERS, MASTER_REST_SERVER_MAX_THREADS, MASTER_REST_SERVER_VIRTUAL_THREADS}
import org.apache.spark.rpc._
import org.apache.spark.util.ArrayImplicits._
import org.apache.spark.util.Utils
Expand Down Expand Up @@ -559,6 +560,23 @@ class StandaloneRestSubmitSuite extends SparkFunSuite {
assert(pool.getMaxThreads === 2000)
}

test("SPARK-50383: Support spark.master.rest.virtualThread.enabled") {
val conf = new SparkConf()
val localhost = Utils.localHostName()
val securityManager = new SecurityManager(conf)
rpcEnv = Some(RpcEnv.create("rest-with-virtualThreads", localhost, 0, conf, securityManager))
val fakeMasterRef = rpcEnv.get.setupEndpoint("fake-master", new DummyMaster(rpcEnv.get))
conf.set(MASTER_REST_SERVER_VIRTUAL_THREADS, true)
server = Some(new StandaloneRestServer(localhost, 0, conf, fakeMasterRef, "spark://fake:7077"))
server.get.start()
val pool = server.get._server.get.getThreadPool.asInstanceOf[QueuedThreadPool]
if (Utils.isJavaVersionAtLeast21) {
assert(pool.getVirtualThreadsExecutor != null)
} else {
assert(pool.getVirtualThreadsExecutor == null)
}
}

/* --------------------- *
| Helper methods |
* --------------------- */
Expand Down