From 0cc52d035cf2011e6f9fa9f4fd44394485b756b3 Mon Sep 17 00:00:00 2001 From: "Wang, Fei" Date: Wed, 9 Apr 2025 10:27:09 -0700 Subject: [PATCH] [KYUUBI #7017] Using mutable JettyServer uri to prevent batch kyuubi instance mismatch ### Why are the changes needed? To fix the batch kyuubi instance port is negative issue. image It happen after I stop the kyuubi service. We should use variable instead of function for jetty server serverUri. After the server connector stopped, the localPort would be `-2`. ![image](https://github.com/user-attachments/assets/5152293d-9c2c-4979-bdcb-322f02928813) ### How was this patch tested? Existing UT. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #7017 from turboFei/server_port_negative. Closes #7017 3d34c4031 [Wang, Fei] warn e58298646 [Wang, Fei] mutable server uri 2cbaf772a [Wang, Fei] Revert "hard code the server uri" b64d91b32 [Wang, Fei] hard code the server uri Authored-by: Wang, Fei Signed-off-by: Wang, Fei --- .../org/apache/kyuubi/server/ui/JettyServer.scala | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/kyuubi-server/src/main/scala/org/apache/kyuubi/server/ui/JettyServer.scala b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/ui/JettyServer.scala index e74a2f3c4..c1bac1275 100644 --- a/kyuubi-server/src/main/scala/org/apache/kyuubi/server/ui/JettyServer.scala +++ b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/ui/JettyServer.scala @@ -22,18 +22,22 @@ import org.eclipse.jetty.server.handler.{ContextHandlerCollection, ErrorHandler} import org.eclipse.jetty.util.component.LifeCycle import org.eclipse.jetty.util.thread.{QueuedThreadPool, ScheduledExecutorScheduler} +import org.apache.kyuubi.Logging import org.apache.kyuubi.util.JavaUtils private[kyuubi] class JettyServer( server: Server, connector: ServerConnector, - rootHandler: ContextHandlerCollection) { + rootHandler: ContextHandlerCollection) extends Logging { def start(): Unit = synchronized { try { server.start() connector.start() server.addConnector(connector) + val localPort = connector.getLocalPort + require(localPort > 0, "Jetty server port should be positive, but got " + localPort) + _serverUri = connector.getHost + ":" + localPort } catch { case e: Exception => stop() @@ -49,7 +53,13 @@ private[kyuubi] class JettyServer( case _ => } } - def getServerUri: String = connector.getHost + ":" + connector.getLocalPort + + @volatile private var _serverUri: String = _ + def getServerUri: String = Option(_serverUri).getOrElse { + val uri = connector.getHost + ":" + connector.getLocalPort + warn("Jetty server is not started yet, returning " + uri) + uri + } def addHandler(handler: Handler): Unit = synchronized { rootHandler.addHandler(handler)