Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#80][Part-3] feat: add REST API for decommisson #684

Merged
merged 25 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void test() throws Exception {
fail(e.getMessage());
}
}
for (int i = 0; i < serverStatuses.size() - 1; i++) {
for (int i = 0; i < serverStatuses.size(); i++) {
assertEquals(protoServerStatuses.get(i), serverStatuses.get(i).toProto());
assertEquals(ServerStatus.fromProto(protoServerStatuses.get(i)), serverStatuses.get(i));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

Expand All @@ -28,7 +29,7 @@ public class TestUtils {
private TestUtils() {
}

public static String httpGetMetrics(String urlString) throws IOException {
public static String httpGet(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
Expand All @@ -42,4 +43,22 @@ public static String httpGetMetrics(String urlString) throws IOException {
in.close();
return content.toString();
}

public static String httpPost(String urlString, String postData) throws IOException {
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
OutputStream outputStream = con.getOutputStream();
outputStream.write(postData.getBytes());
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
xianjingfeng marked this conversation as resolved.
Show resolved Hide resolved
return content.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void test() throws Exception {
fail(e.getMessage());
}
}
for (int i = 0; i < statusCodes.size() - 1; i++) {
for (int i = 0; i < statusCodes.size(); i++) {
assertEquals(protoStatusCode.get(i), statusCodes.get(i).toProto());
assertEquals(StatusCode.fromProto(protoStatusCode.get(i)), statusCodes.get(i));
}
Expand Down
1 change: 1 addition & 0 deletions coordinator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
<includes>
<include>com.google.protobuf:protobuf-java-util</include>
<include>com.google.guava:guava</include>
<include>com.google.guava:failureaccess</include>
<include>com.fasterxml.jackson.core:jackson-databind</include>
<include>com.fasterxml.jackson.core:jackson-core</include>
</includes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
import org.apache.uniffle.coordinator.strategy.assignment.AssignmentStrategy;
import org.apache.uniffle.coordinator.strategy.assignment.AssignmentStrategyFactory;
import org.apache.uniffle.coordinator.util.CoordinatorUtils;
import org.apache.uniffle.coordinator.web.servlet.CancelDecommissionServlet;
import org.apache.uniffle.coordinator.web.servlet.DecommissionServlet;
import org.apache.uniffle.coordinator.web.servlet.NodesServlet;

import static org.apache.uniffle.common.config.RssBaseConf.RSS_SECURITY_HADOOP_KERBEROS_ENABLE;
import static org.apache.uniffle.common.config.RssBaseConf.RSS_SECURITY_HADOOP_KERBEROS_KEYTAB_FILE;
Expand Down Expand Up @@ -147,6 +150,7 @@ private void initialization() throws Exception {
id = ip + "-" + port;
LOG.info("Start to initialize coordinator {}", id);
jettyServer = new JettyServer(coordinatorConf);
registerRESTAPI();
// register metrics first to avoid NPE problem when add dynamic metrics
registerMetrics();
coordinatorConf.setString(CoordinatorUtils.COORDINATOR_ID, id);
Expand Down Expand Up @@ -179,6 +183,19 @@ private void initialization() throws Exception {
server = coordinatorFactory.getServer();
}

private void registerRESTAPI() throws Exception {
LOG.info("Register REST API");
jettyServer.addServlet(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you think to struct REST api as follows:

GET /api/v1/servers # list all servers. 
GET /api/v1/servers/:id # get the specific server with id.
POST /api/v1/servers/:id/decommission # start decommission of server with id
POST /api/v1/servers/:id/cancelDecommission #...
POST /api/v1/servers/decommission # batch decommission. similar to the current impl
POST /api/v1/servers/cancelDecommission # batch cancel decommission.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is troublesome to get parameters from path in the current framework. Should we introduce other REST frameworks?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we introduce other REST frameworks?

If it's too much trouble, I'm OK with current route path. If it's simple, I prefer the above REST path, which is more idiomatic. It's up to you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will be more and more rest apis in the future. It is still troublesome to add an api under the current framework. So I think we can consider it later. How about introduce Jersey? @advancedxy @jerqi @zuston @kaijchen

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with Jersey. Is it lightweight enough? Is it used widely?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with Jersey. Is it lightweight enough? Is it used widely?

Yes. Hadoop and Spark use Jersey either.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if Jersey is lightweight. I hope the restful framework could be modern and lightweight.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will be more and more rest apis in the future. It is still troublesome to add an api under the current framework. So I think we can consider it later. How about introduce Jersey? @advancedxy @jerqi @zuston @kaijchen

I'm ok with current status. Let's make the new REST server framework a followup PR. Jersey itself seems a bit complex, but the path annotation https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/user-guide.html#d0e2081 is quite elegant.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can refer to Apache Livy, it use scalatra. We can use similar Java framework.
https://www.51cto.com/article/311366.html

new NodesServlet(this),
"/api/server/nodes");
jettyServer.addServlet(
new DecommissionServlet(this),
"/api/server/decommission");
jettyServer.addServlet(
new CancelDecommissionServlet(this),
"/api/server/cancelDecommission");
xianjingfeng marked this conversation as resolved.
Show resolved Hide resolved
}

private void registerMetrics() throws Exception {
LOG.info("Register metrics");
CollectorRegistry coordinatorCollectorRegistry = new CollectorRegistry(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.uniffle.coordinator.web;

public class Response<T> {
private static final int SUCCESS_CODE = 0;
private static final int ERROR_CODE = -1;
private int code;
private T data;
private String errMsg;

public Response() {
}

public Response(int code, T data, String errMsg) {
this.code = code;
this.data = data;
this.errMsg = errMsg;
}

public static <T> Response<T> success(T data) {
return new Response<>(SUCCESS_CODE, data, null);
}

public static <T> Response<T> fail(String msg) {
return new Response<>(ERROR_CODE, null, msg);
}

public static <T> Response<T> fail(String msg, int code) {
return new Response<>(code, null, msg);
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}

public String getErrMsg() {
return errMsg;
}

public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.uniffle.coordinator.web.request;

import java.util.List;

public class CancelDecommissionRequest {
private List<String> serverIds;

public List<String> getServerIds() {
return serverIds;
}

public void setServerIds(List<String> serverIds) {
this.serverIds = serverIds;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.uniffle.coordinator.web.request;

import java.util.List;

public class DecommissionRequest {
private List<String> serverIds;
xianjingfeng marked this conversation as resolved.
Show resolved Hide resolved

public List<String> getServerIds() {
return serverIds;
}

public void setServerIds(List<String> serverIds) {
this.serverIds = serverIds;
}

xianjingfeng marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.uniffle.coordinator.web.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.Callable;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.uniffle.coordinator.web.Response;

public abstract class BaseServlet extends HttpServlet {
public static final String JSON_MIME_TYPE = "application/json";
final ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
writeJSON(resp, handlerRequest(() -> handleGet(req, resp)));
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
writeJSON(resp, handlerRequest(() -> handlePost(req, resp)));
}

private Response handlerRequest(
Callable<Response> function) {
Response response;
try {
// todo: Do something for authentication
response = function.call();
} catch (Exception e) {
response = Response.fail(e.getMessage());
}
return response;
}

protected Response handleGet(
HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {
throw new IOException("Method not support!");
}


xianjingfeng marked this conversation as resolved.
Show resolved Hide resolved
protected Response handlePost(
HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {
throw new IOException("Method not support!");
}

protected void writeJSON(final HttpServletResponse resp, final Object obj)
throws IOException {
if (obj == null) {
return;
}
resp.setContentType(JSON_MIME_TYPE);
final OutputStream stream = resp.getOutputStream();
mapper.writeValue(stream, obj);
}

protected <T> T parseParamsFromJson(HttpServletRequest req, Class<T> clazz) throws IOException {
advancedxy marked this conversation as resolved.
Show resolved Hide resolved
return mapper.readValue(req.getInputStream(), clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.uniffle.coordinator.web.servlet;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;

import org.apache.uniffle.coordinator.ClusterManager;
import org.apache.uniffle.coordinator.CoordinatorServer;
import org.apache.uniffle.coordinator.web.Response;
import org.apache.uniffle.coordinator.web.request.CancelDecommissionRequest;

public class CancelDecommissionServlet extends BaseServlet {
private final CoordinatorServer coordinator;

public CancelDecommissionServlet(CoordinatorServer coordinator) {
this.coordinator = coordinator;
}

@Override
protected Response handlePost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
CancelDecommissionRequest params = parseParamsFromJson(req, CancelDecommissionRequest.class);
if (CollectionUtils.isEmpty(params.getServerIds())) {
return Response.fail("Parameter[serverIds] should not be null!");
}
ClusterManager clusterManager = coordinator.getClusterManager();
params.getServerIds().forEach((serverId) -> {
clusterManager.cancelDecommission(serverId);
});
return Response.success(null);
}
}