forked from apache/incubator-uniffle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[apache#80][Part-3] feat: add REST API for decommisson (apache#684)
### What changes were proposed in this pull request? Add REST API for decommisson ### Why are the changes needed? Support shuffle server decommission. It is a part of apache#80 ### Does this PR introduce _any_ user-facing change? Env: * Server IP: 127.0.0.1 * HTTP port: 19998 * RPC port: 19999 Decommission example: ```shell curl -XPOST -H "Content-type:application/json" "http://127.0.0.1:19998/api/server/decommission" -d '{"serverIds:": ["127.0.0.1:19999"]}' ``` Cancel decommission example: ```shell curl -XPOST -H "Content-type:application/json" "http://127.0.0.1:19998/api/server/cancelDecommission" -d '{"serverIds:": ["127.0.0.1:19999"]}' ``` Get server list: ```shell # path: /api/server/nodes[?id={serverId}][?status={serverStatus}] curl "http://127.0.0.1:19998/api/server/nodes?status=DECOMMISSIONING" curl "http://127.0.0.1:19998/api/server/nodes?status=ACTIVE" ``` ### How was this patch tested? UT
- Loading branch information
1 parent
2c75c14
commit 25f5dc5
Showing
17 changed files
with
650 additions
and
21 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
71 changes: 71 additions & 0 deletions
71
coordinator/src/main/java/org/apache/uniffle/coordinator/web/Response.java
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,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; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...r/src/main/java/org/apache/uniffle/coordinator/web/request/CancelDecommissionRequest.java
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,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.Set; | ||
|
||
public class CancelDecommissionRequest { | ||
private Set<String> serverIds; | ||
|
||
public Set<String> getServerIds() { | ||
return serverIds; | ||
} | ||
|
||
public void setServerIds(Set<String> serverIds) { | ||
this.serverIds = serverIds; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...dinator/src/main/java/org/apache/uniffle/coordinator/web/request/DecommissionRequest.java
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,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.Set; | ||
|
||
public class DecommissionRequest { | ||
private Set<String> serverIds; | ||
|
||
public Set<String> getServerIds() { | ||
return serverIds; | ||
} | ||
|
||
public void setServerIds(Set<String> serverIds) { | ||
this.serverIds = serverIds; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
coordinator/src/main/java/org/apache/uniffle/coordinator/web/servlet/BaseServlet.java
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,84 @@ | ||
/* | ||
* 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!"); | ||
} | ||
|
||
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 { | ||
return mapper.readValue(req.getInputStream(), clazz); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...r/src/main/java/org/apache/uniffle/coordinator/web/servlet/CancelDecommissionServlet.java
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,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); | ||
} | ||
} |
Oops, something went wrong.