-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
close #3905 ### _Why are the changes needed?_ ### _How was this patch tested?_ - [x] Add some test cases that check the changes thoroughly including negative and positive cases if possible - [ ] Add screenshots for manual tests if appropriate - [x] [Run test](https://kyuubi.apache.org/docs/latest/develop_tools/testing.html#running-tests) locally before make a pull request Closes #3977 from iodone/kyuubi-3905. Closes #3977 Closes #3905 4e4a22b [odone] trino server init and run Authored-by: odone <odone.zhang@gmail.com> Signed-off-by: ulysses-you <ulyssesyou@apache.org>
- Loading branch information
1 parent
0295c0f
commit b5590b8
Showing
14 changed files
with
610 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
100 changes: 100 additions & 0 deletions
100
kyuubi-server/src/main/scala/org/apache/kyuubi/server/KyuubiTrinoFrontendService.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,100 @@ | ||
/* | ||
* 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.kyuubi.server | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
import org.apache.kyuubi.{KyuubiException, Utils} | ||
import org.apache.kyuubi.config.KyuubiConf | ||
import org.apache.kyuubi.config.KyuubiConf.{FRONTEND_TRINO_BIND_HOST, FRONTEND_TRINO_BIND_PORT, FRONTEND_TRINO_MAX_WORKER_THREADS} | ||
import org.apache.kyuubi.server.trino.api.v1.ApiRootResource | ||
import org.apache.kyuubi.server.ui.JettyServer | ||
import org.apache.kyuubi.service.{AbstractFrontendService, Serverable, Service} | ||
|
||
/** | ||
* A frontend service based on RESTful api via HTTP protocol. | ||
* Note: Currently, it only be used in the Kyuubi Server side. | ||
*/ | ||
class KyuubiTrinoFrontendService(override val serverable: Serverable) | ||
extends AbstractFrontendService("KyuubiTrinoFrontendService") { | ||
|
||
private var server: JettyServer = _ | ||
|
||
private val isStarted = new AtomicBoolean(false) | ||
|
||
lazy val host: String = conf.get(FRONTEND_TRINO_BIND_HOST) | ||
.getOrElse { | ||
if (conf.get(KyuubiConf.FRONTEND_CONNECTION_URL_USE_HOSTNAME)) { | ||
Utils.findLocalInetAddress.getCanonicalHostName | ||
} else { | ||
Utils.findLocalInetAddress.getHostAddress | ||
} | ||
} | ||
|
||
override def initialize(conf: KyuubiConf): Unit = synchronized { | ||
this.conf = conf | ||
server = JettyServer( | ||
getName, | ||
host, | ||
conf.get(FRONTEND_TRINO_BIND_PORT), | ||
conf.get(FRONTEND_TRINO_MAX_WORKER_THREADS)) | ||
super.initialize(conf) | ||
} | ||
|
||
override def connectionUrl: String = { | ||
checkInitialized() | ||
server.getServerUri | ||
} | ||
|
||
private def startInternal(): Unit = { | ||
val contextHandler = ApiRootResource.getServletHandler(this) | ||
server.addHandler(contextHandler) | ||
|
||
server.addStaticHandler("org/apache/kyuubi/ui/static", "/static/") | ||
server.addRedirectHandler("/", "/static/") | ||
server.addRedirectHandler("/static", "/static/") | ||
server.addStaticHandler("META-INF/resources/webjars/swagger-ui/4.9.1/", "/swagger-static/") | ||
server.addStaticHandler("org/apache/kyuubi/ui/swagger", "/swagger/") | ||
server.addRedirectHandler("/docs", "/swagger/") | ||
server.addRedirectHandler("/docs/", "/swagger/") | ||
server.addRedirectHandler("/swagger", "/swagger/") | ||
} | ||
|
||
override def start(): Unit = synchronized { | ||
if (!isStarted.get) { | ||
try { | ||
server.start() | ||
isStarted.set(true) | ||
info(s"$getName has started at ${server.getServerUri}") | ||
startInternal() | ||
} catch { | ||
case e: Exception => throw new KyuubiException(s"Cannot start $getName", e) | ||
} | ||
} | ||
super.start() | ||
} | ||
|
||
override def stop(): Unit = synchronized { | ||
if (isStarted.getAndSet(false)) { | ||
server.stop() | ||
} | ||
super.stop() | ||
} | ||
|
||
override val discoveryService: Option[Service] = None | ||
} |
29 changes: 29 additions & 0 deletions
29
...bi-server/src/main/scala/org/apache/kyuubi/server/trino/api/KyuubiScalaObjectMapper.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,29 @@ | ||
/* | ||
* 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.kyuubi.server.trino.api | ||
|
||
import javax.ws.rs.ext.ContextResolver | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.scala.DefaultScalaModule | ||
|
||
class KyuubiScalaObjectMapper extends ContextResolver[ObjectMapper] { | ||
private val mapper = new ObjectMapper().registerModule(DefaultScalaModule) | ||
|
||
override def getContext(aClass: Class[_]): ObjectMapper = mapper | ||
} |
26 changes: 26 additions & 0 deletions
26
kyuubi-server/src/main/scala/org/apache/kyuubi/server/trino/api/TrinoServerConfig.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,26 @@ | ||
/* | ||
* 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.kyuubi.server.trino.api | ||
|
||
import org.glassfish.jersey.server.ResourceConfig | ||
|
||
class TrinoServerConfig extends ResourceConfig { | ||
packages("org.apache.kyuubi.server.trino.api.v1") | ||
register(classOf[KyuubiScalaObjectMapper]) | ||
register(classOf[RestExceptionMapper]) | ||
} |
70 changes: 70 additions & 0 deletions
70
kyuubi-server/src/main/scala/org/apache/kyuubi/server/trino/api/api.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,70 @@ | ||
/* | ||
* 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.kyuubi.server.trino.api | ||
|
||
import javax.servlet.ServletContext | ||
import javax.servlet.http.HttpServletRequest | ||
import javax.ws.rs.WebApplicationException | ||
import javax.ws.rs.core.{Context, MediaType, Response} | ||
import javax.ws.rs.ext.{ExceptionMapper, Provider} | ||
|
||
import org.eclipse.jetty.server.handler.ContextHandler | ||
|
||
import org.apache.kyuubi.server.KyuubiTrinoFrontendService | ||
|
||
private[api] trait ApiRequestContext { | ||
|
||
@Context | ||
protected var servletContext: ServletContext = _ | ||
|
||
@Context | ||
protected var httpRequest: HttpServletRequest = _ | ||
|
||
final protected def fe: KyuubiTrinoFrontendService = FrontendServiceContext.get(servletContext) | ||
} | ||
|
||
@Provider | ||
class RestExceptionMapper extends ExceptionMapper[Exception] { | ||
override def toResponse(exception: Exception): Response = { | ||
exception match { | ||
case e: WebApplicationException => | ||
Response.status(e.getResponse.getStatus) | ||
.`type`(e.getResponse.getMediaType) | ||
.entity(e.getMessage) | ||
.build() | ||
case e => | ||
Response.status(Response.Status.INTERNAL_SERVER_ERROR) | ||
.`type`(MediaType.APPLICATION_JSON) | ||
.entity(e.getMessage) | ||
.build() | ||
} | ||
} | ||
} | ||
|
||
private[api] object FrontendServiceContext { | ||
|
||
private val attribute = getClass.getCanonicalName | ||
|
||
def set(contextHandler: ContextHandler, fe: KyuubiTrinoFrontendService): Unit = { | ||
contextHandler.setAttribute(attribute, fe) | ||
} | ||
|
||
def get(context: ServletContext): KyuubiTrinoFrontendService = { | ||
context.getAttribute(attribute).asInstanceOf[KyuubiTrinoFrontendService] | ||
} | ||
} |
Oops, something went wrong.