Skip to content

Commit

Permalink
[KYUUBI #3653][REST] AdminResource add list kyuubi server api
Browse files Browse the repository at this point in the history
### _Why are the changes needed?_

Add List Kyuubi Server Api for `AdminResource`

### _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

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

Closes #4670 from zwangsheng/KYUUBI_3653.

Closes #3653

b91a6c6 [zwangsheng] fxi
4271d0f [zwangsheng] fix comments
e14f8cd [zwangsheng] [KYUUBI #3653][REST] AdminResource add list server api

Authored-by: zwangsheng <2213335496@qq.com>
Signed-off-by: fwang12 <fwang12@ebay.com>
  • Loading branch information
zwangsheng authored and turboFei committed Apr 18, 2023
1 parent 8c7b457 commit f6331a2
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* 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.client.api.v1.dto;

import java.util.Map;
import java.util.Objects;

public class ServerData {
private String nodeName;
private String namespace;
private String instance;
private String host;
private int port;
private Map<String, String> attributes;
private String status;

public ServerData(
String nodeName,
String namespace,
String instance,
String host,
int port,
Map<String, String> attributes,
String status) {
this.nodeName = nodeName;
this.namespace = namespace;
this.instance = instance;
this.host = host;
this.port = port;
this.attributes = attributes;
this.status = status;
}

public String getNodeName() {
return nodeName;
}

public ServerData setNodeName(String nodeName) {
this.nodeName = nodeName;
return this;
}

public String getNamespace() {
return namespace;
}

public ServerData setNamespace(String namespace) {
this.namespace = namespace;
return this;
}

public String getInstance() {
return instance;
}

public ServerData setInstance(String instance) {
this.instance = instance;
return this;
}

public String getHost() {
return host;
}

public ServerData setHost(String host) {
this.host = host;
return this;
}

public int getPort() {
return port;
}

public ServerData setPort(int port) {
this.port = port;
return this;
}

public Map<String, String> getAttributes() {
return attributes;
}

public ServerData setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
return this;
}

public String getStatus() {
return status;
}

public ServerData setStatus(String status) {
this.status = status;
return this;
}

@Override
public int hashCode() {
return Objects.hash(nodeName, namespace, instance, port, attributes, status);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
ServerData server = (ServerData) obj;
return port == server.port
&& Objects.equals(nodeName, server.nodeName)
&& Objects.equals(namespace, server.namespace)
&& Objects.equals(instance, server.instance)
&& Objects.equals(host, server.host)
&& Objects.equals(status, server.status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ package org.apache.kyuubi.server.api
import scala.collection.JavaConverters._

import org.apache.kyuubi.Utils
import org.apache.kyuubi.client.api.v1.dto.{OperationData, SessionData}
import org.apache.kyuubi.client.api.v1.dto.{OperationData, ServerData, SessionData}
import org.apache.kyuubi.events.KyuubiOperationEvent
import org.apache.kyuubi.ha.client.ServiceNodeInfo
import org.apache.kyuubi.operation.KyuubiOperation
import org.apache.kyuubi.session.KyuubiSession

Expand Down Expand Up @@ -58,4 +59,15 @@ object ApiUtils {
opEvent.sessionType,
operation.getSession.asInstanceOf[KyuubiSession].connectionUrl)
}

def serverData(nodeInfo: ServiceNodeInfo): ServerData = {
new ServerData(
nodeInfo.nodeName,
nodeInfo.namespace,
nodeInfo.instance,
nodeInfo.host,
nodeInfo.port,
nodeInfo.attributes.asJava,
"Running")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import org.apache.commons.lang3.StringUtils
import org.apache.zookeeper.KeeperException.NoNodeException

import org.apache.kyuubi.{KYUUBI_VERSION, Logging, Utils}
import org.apache.kyuubi.client.api.v1.dto.{Engine, OperationData, SessionData}
import org.apache.kyuubi.client.api.v1.dto.{Engine, OperationData, ServerData, SessionData}
import org.apache.kyuubi.config.KyuubiConf
import org.apache.kyuubi.config.KyuubiConf._
import org.apache.kyuubi.ha.HighAvailabilityConf.HA_NAMESPACE
Expand Down Expand Up @@ -296,6 +296,35 @@ private[v1] class AdminResource extends ApiRequestContext with Logging {
node.attributes.asJava))
}

@ApiResponse(
responseCode = "200",
content = Array(
new Content(
mediaType = MediaType.APPLICATION_JSON,
array = new ArraySchema(schema = new Schema(implementation =
classOf[OperationData])))),
description = "list all live kyuubi servers")
@GET
@Path("server")
def listServers(): Seq[ServerData] = {
val userName = fe.getSessionUser(Map.empty[String, String])
val ipAddress = fe.getIpAddress
info(s"Received list all live kyuubi servers request from $userName/$ipAddress")
if (!isAdministrator(userName)) {
throw new NotAllowedException(
s"$userName is not allowed to list all live kyuubi servers")
}
val kyuubiConf = fe.getConf
val servers = ListBuffer[ServerData]()
val serverSpec = DiscoveryPaths.makePath(null, kyuubiConf.get(HA_NAMESPACE))
withDiscoveryClient(kyuubiConf) { discoveryClient =>
discoveryClient.getServiceNodesInfo(serverSpec).map(nodeInfo => {
servers += ApiUtils.serverData(nodeInfo)
})
}
servers
}

private def getEngine(
userName: String,
engineType: String,
Expand Down
Loading

0 comments on commit f6331a2

Please sign in to comment.