Skip to content

Commit

Permalink
[KYUUBI #3977] [KYUUBI #3905] Add basic TrinoResource
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
iodone authored and ulysses-you committed Dec 15, 2022
1 parent 0295c0f commit b5590b8
Show file tree
Hide file tree
Showing 14 changed files with 610 additions and 2 deletions.
4 changes: 4 additions & 0 deletions dev/dependencyList
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ jackson-annotations/2.14.1//jackson-annotations-2.14.1.jar
jackson-core/2.14.1//jackson-core-2.14.1.jar
jackson-databind/2.14.1//jackson-databind-2.14.1.jar
jackson-dataformat-yaml/2.14.1//jackson-dataformat-yaml-2.14.1.jar
jackson-datatype-jdk8/2.12.3//jackson-datatype-jdk8-2.12.3.jar
jackson-datatype-jsr310/2.14.1//jackson-datatype-jsr310-2.14.1.jar
jackson-jaxrs-base/2.14.1//jackson-jaxrs-base-2.14.1.jar
jackson-jaxrs-json-provider/2.14.1//jackson-jaxrs-json-provider-2.14.1.jar
Expand Down Expand Up @@ -148,6 +149,7 @@ netty-transport-native-epoll/4.1.84.Final/linux-aarch_64/netty-transport-native-
netty-transport-native-epoll/4.1.84.Final/linux-x86_64/netty-transport-native-epoll-4.1.84.Final-linux-x86_64.jar
netty-transport-native-unix-common/4.1.84.Final//netty-transport-native-unix-common-4.1.84.Final.jar
netty-transport/4.1.84.Final//netty-transport-4.1.84.Final.jar
okhttp-urlconnection/3.14.9//okhttp-urlconnection-3.14.9.jar
okhttp/3.12.12//okhttp-3.12.12.jar
okio/1.15.0//okio-1.15.0.jar
osgi-resource-locator/1.0.3//osgi-resource-locator-1.0.3.jar
Expand All @@ -174,6 +176,8 @@ swagger-integration/2.2.1//swagger-integration-2.2.1.jar
swagger-jaxrs2/2.2.1//swagger-jaxrs2-2.2.1.jar
swagger-models/2.2.1//swagger-models-2.2.1.jar
swagger-ui/4.9.1//swagger-ui-4.9.1.jar
trino-client/363//trino-client-363.jar
units/1.6//units-1.6.jar
vertx-core/4.3.2//vertx-core-4.3.2.jar
vertx-grpc/4.3.2//vertx-grpc-4.3.2.jar
zjsonpatch/0.3.0//zjsonpatch-0.3.0.jar
Expand Down
5 changes: 4 additions & 1 deletion docs/deployment/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ kyuubi.frontend.mysql.max.worker.threads|999|Maximum number of threads in the co
kyuubi.frontend.mysql.min.worker.threads|9|Minimum number of threads in the command execution thread pool for the MySQL frontend service|int|1.4.0
kyuubi.frontend.mysql.netty.worker.threads|&lt;undefined&gt;|Number of thread in the netty worker event loop of MySQL frontend service. Use min(cpu_cores, 8) in default.|int|1.4.0
kyuubi.frontend.mysql.worker.keepalive.time|PT1M|Time(ms) that an idle async thread of the command execution thread pool will wait for a new task to arrive before terminating in MySQL frontend service|duration|1.4.0
kyuubi.frontend.protocols|THRIFT_BINARY|A comma separated list for all frontend protocols <ul> <li>THRIFT_BINARY - HiveServer2 compatible thrift binary protocol.</li> <li>THRIFT_HTTP - HiveServer2 compatible thrift http protocol.</li> <li>REST - Kyuubi defined REST API(experimental).</li> <li>MYSQL - MySQL compatible text protocol(experimental).</li> </ul>|seq|1.4.0
kyuubi.frontend.protocols|THRIFT_BINARY|A comma separated list for all frontend protocols <ul> <li>THRIFT_BINARY - HiveServer2 compatible thrift binary protocol.</li> <li>THRIFT_HTTP - HiveServer2 compatible thrift http protocol.</li> <li>REST - Kyuubi defined REST API(experimental).</li> <li>MYSQL - MySQL compatible text protocol(experimental).</li> <li>TRINO - Trino compatible http protocol(experimental).</li> </ul>|seq|1.4.0
kyuubi.frontend.proxy.http.client.ip.header|X-Real-IP|The http header to record the real client ip address. If your server is behind a load balancer or other proxy, the server will see this load balancer or proxy IP address as the client IP address, to get around this common issue, most load balancers or proxies offer the ability to record the real remote IP address in an HTTP header that will be added to the request for other devices to use. Note that, because the header value can be specified to any ip address, so it will not be used for authentication.|string|1.6.0
kyuubi.frontend.rest.bind.host|&lt;undefined&gt;|Hostname or IP of the machine on which to run the REST frontend service.|string|1.4.0
kyuubi.frontend.rest.bind.port|10099|Port of the machine on which to run the REST frontend service.|int|1.4.0
Expand Down Expand Up @@ -338,6 +338,9 @@ kyuubi.frontend.thrift.max.message.size|104857600|Maximum message size in bytes
kyuubi.frontend.thrift.max.worker.threads|999|Maximum number of threads in the of frontend worker thread pool for the thrift frontend service|int|1.4.0
kyuubi.frontend.thrift.min.worker.threads|9|Minimum number of threads in the of frontend worker thread pool for the thrift frontend service|int|1.4.0
kyuubi.frontend.thrift.worker.keepalive.time|PT1M|Keep-alive time (in milliseconds) for an idle worker thread|duration|1.4.0
kyuubi.frontend.trino.bind.host|&lt;undefined&gt;|Hostname or IP of the machine on which to run the TRINO frontend service.|string|1.7.0
kyuubi.frontend.trino.bind.port|10999|Port of the machine on which to run the TRINO frontend service.|int|1.7.0
kyuubi.frontend.trino.max.worker.threads|999|Maximum number of threads in the of frontend worker thread pool for the trino frontend service|int|1.7.0
kyuubi.frontend.worker.keepalive.time|PT1M|(deprecated) Keep-alive time (in milliseconds) for an idle worker thread|duration|1.0.0


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ object KyuubiConf {

object FrontendProtocols extends Enumeration {
type FrontendProtocol = Value
val THRIFT_BINARY, THRIFT_HTTP, REST, MYSQL = Value
val THRIFT_BINARY, THRIFT_HTTP, REST, MYSQL, TRINO = Value
}

val FRONTEND_PROTOCOLS: ConfigEntry[Seq[String]] =
Expand All @@ -373,6 +373,7 @@ object KyuubiConf {
" <li>THRIFT_HTTP - HiveServer2 compatible thrift http protocol.</li>" +
" <li>REST - Kyuubi defined REST API(experimental).</li> " +
" <li>MYSQL - MySQL compatible text protocol(experimental).</li> " +
" <li>TRINO - Trino compatible http protocol(experimental).</li> " +
"</ul>")
.version("1.4.0")
.stringConf
Expand Down Expand Up @@ -928,6 +929,28 @@ object KyuubiConf {
.version("1.4.0")
.fallbackConf(FRONTEND_WORKER_KEEPALIVE_TIME)

val FRONTEND_TRINO_BIND_HOST: ConfigEntry[Option[String]] =
buildConf("kyuubi.frontend.trino.bind.host")
.doc("Hostname or IP of the machine on which to run the TRINO frontend service.")
.version("1.7.0")
.serverOnly
.fallbackConf(FRONTEND_BIND_HOST)

val FRONTEND_TRINO_BIND_PORT: ConfigEntry[Int] = buildConf("kyuubi.frontend.trino.bind.port")
.doc("Port of the machine on which to run the TRINO frontend service.")
.version("1.7.0")
.serverOnly
.intConf
.checkValue(p => p == 0 || (p > 1024 && p < 65535), "Invalid Port number")
.createWithDefault(10999)

val FRONTEND_TRINO_MAX_WORKER_THREADS: ConfigEntry[Int] =
buildConf("kyuubi.frontend.trino.max.worker.threads")
.doc("Maximum number of threads in the of frontend worker thread pool for the trino " +
"frontend service")
.version("1.7.0")
.fallbackConf(FRONTEND_MAX_WORKER_THREADS)

val KUBERNETES_CONTEXT: OptionalConfigEntry[String] =
buildConf("kyuubi.kubernetes.context")
.doc("The desired context from your kubernetes config file used to configure the K8S " +
Expand Down
5 changes: 5 additions & 0 deletions kyuubi-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@
<artifactId>derby</artifactId>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-client</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ class KyuubiServer(name: String) extends Serverable(name) {
case MYSQL =>
warn("MYSQL frontend protocol is experimental.")
new KyuubiMySQLFrontendService(this)
case TRINO =>
warn("Trio frontend protocol is experimental.")
new KyuubiTrinoFrontendService(this)
case other =>
throw new UnsupportedOperationException(s"Frontend protocol $other is not supported yet.")
}
Expand Down
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
}
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
}
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])
}
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]
}
}

0 comments on commit b5590b8

Please sign in to comment.