[KYUUBI #1031] Implement api: /sessions/count

<!--
Thanks for sending a pull request!

Here are some tips for you:
  1. If this is your first time, please read our contributor guidelines: https://kyuubi.readthedocs.io/en/latest/community/contributions.html
  2. If the PR is related to an issue in https://github.com/apache/incubator-kyuubi/issues, add '[KYUUBI #XXXX]' in your PR title, e.g., '[KYUUBI #XXXX] Your PR title ...'.
  3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][KYUUBI #XXXX] Your PR title ...'.
-->

### _Why are the changes needed?_
<!--
Please clarify why the changes are needed. For instance,
  1. If you add a feature, you can talk about the use case of it.
  2. If you fix a bug, you can clarify why it is a bug.
-->

### _How was this patch tested?_
- [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible

- [ ] Add screenshots for manual tests if appropriate

- [ ] [Run test](https://kyuubi.readthedocs.io/en/latest/develop_tools/testing.html#running-tests) locally before make a pull request

Closes #1038 from yanghua/KYUUBI-1031.

Closes #1031

36ad5b97 [yanghua] Addressed the review concerns
66d8b0fe [yanghua] [KYUUBI #1031] Implement api: /sessions/count

Authored-by: yanghua <yanghua1127@gmail.com>
Signed-off-by: ulysses-you <ulyssesyou18@gmail.com>
This commit is contained in:
yanghua 2021-09-07 12:10:48 +08:00 committed by ulysses-you
parent 59d9f06a46
commit 10d8ab90dd
9 changed files with 149 additions and 12 deletions

View File

@ -33,6 +33,7 @@ htrace-core4/4.1.0-incubating//htrace-core4-4.1.0-incubating.jar
jackson-annotations/2.11.4//jackson-annotations-2.11.4.jar
jackson-core/2.11.4//jackson-core-2.11.4.jar
jackson-databind/2.11.4//jackson-databind-2.11.4.jar
jackson-module-jaxb-annotations/2.9.9//jackson-module-jaxb-annotations-2.9.9.jar
jackson-module-paranamer/2.11.4//jackson-module-paranamer-2.11.4.jar
jackson-module-scala_2.12/2.11.4//jackson-module-scala_2.12-2.11.4.jar
jakarta.annotation-api/1.3.5//jakarta.annotation-api-1.3.5.jar
@ -46,8 +47,10 @@ jcl-over-slf4j/1.7.30//jcl-over-slf4j-1.7.30.jar
jersey-client/2.30//jersey-client-2.30.jar
jersey-common/2.30//jersey-common-2.30.jar
jersey-container-servlet-core/2.30//jersey-container-servlet-core-2.30.jar
jersey-entity-filtering/2.30//jersey-entity-filtering-2.30.jar
jersey-hk2/2.30//jersey-hk2-2.30.jar
jersey-media-jaxb/2.30//jersey-media-jaxb-2.30.jar
jersey-media-json-jackson/2.30//jersey-media-json-jackson-2.30.jar
jersey-server/2.30//jersey-server-2.30.jar
jetty-http/9.4.41.v20210516//jetty-http-9.4.41.v20210516.jar
jetty-io/9.4.41.v20210516//jetty-io-9.4.41.v20210516.jar

View File

@ -81,6 +81,11 @@
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.apache.kyuubi</groupId>
<artifactId>kyuubi-common_${scala.binary.version}</artifactId>

View File

@ -0,0 +1,36 @@
/*
* 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.api.v1.dto;
import java.io.Serializable;
public class SessionOpenedCount implements Serializable {
private int openSessionCount;
public SessionOpenedCount() {
}
public int getOpenSessionCount() {
return openSessionCount;
}
public void setOpenSessionCount(int openSessionCount) {
this.openSessionCount = openSessionCount;
}
}

View File

@ -113,6 +113,16 @@ private[server] class RestFrontendService private(name: String, be: BackendServi
private def stopHttpServer(): Unit = {
if (jettyServer != null) {
try {
connector.stop()
info("Rest frontend service server connector has stopped.")
} catch {
case err: Exception =>
error("Cannot safely stop rest frontend service server connector", err)
} finally {
connector = null
}
try {
jettyServer.stop()
info("Rest frontend service jetty server has stopped.")

View File

@ -71,5 +71,3 @@ private[server] object ApiUtils {
}
}

View File

@ -30,4 +30,7 @@ private[v1] class ApiRootResource extends ApiRequestContext {
@Produces(Array(MediaType.TEXT_PLAIN))
def ping(): String = "pong"
@Path("sessions")
def sessions: Class[SessionsResource] = classOf[SessionsResource]
}

View File

@ -0,0 +1,39 @@
/*
* 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.api.v1
import javax.ws.rs.{GET, Path, Produces}
import javax.ws.rs.core.MediaType
import org.apache.kyuubi.server.api.ApiRequestContext
import org.apache.kyuubi.server.api.v1.dto.SessionOpenedCount
@Produces(Array(MediaType.APPLICATION_JSON))
private[v1] class SessionsResource extends ApiRequestContext {
@GET
@Path("count")
def sessionCount(): SessionOpenedCount = {
val sessionOpenedCount = new SessionOpenedCount()
sessionOpenedCount.setOpenSessionCount(
backendService.sessionManager.getOpenSessionCount
)
sessionOpenedCount
}
}

View File

@ -19,13 +19,16 @@ package org.apache.kyuubi.server
import java.util.Locale
import com.fasterxml.jackson.databind.ObjectMapper
import org.scalatest.time.SpanSugar._
import scala.io.Source
import org.apache.kyuubi.KyuubiFunSuite
import org.apache.kyuubi.config.KyuubiConf
import org.apache.kyuubi.service.NoopServer
import org.apache.kyuubi.server.api.v1.dto.SessionOpenedCount
import org.apache.kyuubi.service.{NoopBackendService, NoopServer}
import org.apache.kyuubi.service.ServiceState._
import org.apache.kyuubi.session.{NoopSessionImpl, NoopSessionManager, SessionManager}
class RestFrontendServiceSuite extends KyuubiFunSuite{
@ -61,29 +64,63 @@ class RestFrontendServiceSuite extends KyuubiFunSuite{
}
test("kyuubi rest frontend service http basic") {
withKyuubiRestServer {
(_, host, port) =>
eventually(timeout(10.seconds), interval(50.milliseconds)) {
val html = Source.fromURL(s"http://$host:$port/api/v1/ping").mkString
assert(html.toLowerCase(Locale.ROOT).equals("pong"))
}
}
}
test("kyuubi rest frontend service for sessions resource") {
withKyuubiRestServer {
(_, host, port) =>
val expectedCount = new SessionOpenedCount()
expectedCount.setOpenSessionCount(1)
val expectedStr = new ObjectMapper().writeValueAsString(expectedCount)
eventually(timeout(10.seconds), interval(50.milliseconds)) {
val html = Source.fromURL(s"http://$host:$port/api/v1/sessions/count").mkString
assert(html.toLowerCase(Locale.ROOT).equalsIgnoreCase(expectedStr))
}
}
}
def withKyuubiRestServer(f: (RestFrontendService, String, Int) => Unit): Unit = {
val server = new RestNoopServer()
server.stop()
val conf = KyuubiConf()
conf.set(KyuubiConf.FRONTEND_REST_BIND_HOST, "localhost")
server.initialize(conf)
val frontendService = server.getServices(0).asInstanceOf[RestFrontendService]
server.start()
assert(server.getServiceState === STARTED)
assert(frontendService.getServiceState == STARTED)
eventually(timeout(10.seconds), interval(50.milliseconds)) {
val html = Source.fromURL("http://localhost:10099/api/v1/ping").mkString
assert(html.toLowerCase(Locale.ROOT).equals("pong"))
val frontendService = server.getServices(0).asInstanceOf[RestFrontendService]
try {
f(frontendService, conf.get(KyuubiConf.FRONTEND_REST_BIND_HOST).get,
conf.get(KyuubiConf.FRONTEND_REST_BIND_PORT))
} finally {
server.stop()
}
server.stop()
}
class RestNoopServer extends NoopServer {
override val backendService: NoopBackendService = new RestMockedBeService
override val frontendService = new RestFrontendService(backendService)
}
class RestMockedBeService extends NoopBackendService {
override val sessionManager: SessionManager = new RestMockedSessionManager()
}
class RestMockedSessionManager extends NoopSessionManager {
// It's a ugly and temporally implementation will replace it via creation rest API.
var session = new NoopSessionImpl(null, null, null, null, Map(), this)
setSession(session.handle, session)
override protected def isServer: Boolean = true
}
}

View File

@ -1185,6 +1185,12 @@
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
</dependencyManagement>