forked from deepjavalibrary/djl-serving
-
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.
[serving] Adds mutliple node cluster configuration support (deepjaval…
- Loading branch information
Showing
7 changed files
with
251 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
inference_address=http://0.0.0.0:8080 | ||
management_address=http://0.0.0.0:8080 | ||
cluster_address=http://0.0.0.0:8888 | ||
model_store=/opt/ml/model | ||
load_models=ALL | ||
#model_url_pattern=.* |
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
105 changes: 105 additions & 0 deletions
105
serving/src/main/java/ai/djl/serving/http/ClusterRequestHandler.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,105 @@ | ||
/* | ||
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance | ||
* with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file 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 ai.djl.serving.http; | ||
|
||
import ai.djl.ModelException; | ||
import ai.djl.serving.util.ClusterConfig; | ||
import ai.djl.serving.util.NettyUtils; | ||
import ai.djl.util.Utils; | ||
|
||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.http.FullHttpRequest; | ||
import io.netty.handler.codec.http.QueryStringDecoder; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
/** A class handling inbound HTTP requests for the cluster management API. */ | ||
public class ClusterRequestHandler extends HttpRequestHandler { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ClusterRequestHandler.class); | ||
|
||
private ClusterConfig config = ClusterConfig.getInstance(); | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public boolean acceptInboundMessage(Object msg) throws Exception { | ||
if (super.acceptInboundMessage(msg)) { | ||
FullHttpRequest req = (FullHttpRequest) msg; | ||
return req.uri().startsWith("/cluster/"); | ||
} | ||
return false; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
protected void handleRequest( | ||
ChannelHandlerContext ctx, | ||
FullHttpRequest req, | ||
QueryStringDecoder decoder, | ||
String[] segments) | ||
throws ModelException { | ||
switch (segments[2]) { | ||
case "sshkey": | ||
Path home = Paths.get(System.getProperty("user.home")).resolve(".ssh"); | ||
Path file = home.resolve("id_rsa.pub"); | ||
if (Files.notExists(file)) { | ||
sshkeygen(home.resolve("id_rsa").toString()); | ||
} | ||
NettyUtils.sendFile(ctx, file, false); | ||
return; | ||
case "status": | ||
List<String> messages = decoder.parameters().get("message"); | ||
if (messages.size() != 1) { | ||
NettyUtils.sendJsonResponse(ctx, new StatusResponse("Invalid request")); | ||
return; | ||
} else if (!"OK".equals(messages.get(0))) { | ||
config.setError(messages.get(0)); | ||
} | ||
config.countDown(); | ||
NettyUtils.sendJsonResponse(ctx, new StatusResponse("OK")); | ||
return; | ||
default: | ||
throw new ResourceNotFoundException(); | ||
} | ||
} | ||
|
||
private void sshkeygen(String rsaFile) { | ||
try { | ||
String[] commands = {"ssh-keygen", "-q", "-t", "rsa", "-N", "''", "-f", rsaFile}; | ||
Process exec = new ProcessBuilder(commands).redirectErrorStream(true).start(); | ||
String logOutput; | ||
try (InputStream is = exec.getInputStream()) { | ||
logOutput = Utils.toString(is); | ||
} | ||
int exitCode = exec.waitFor(); | ||
if (0 != exitCode) { | ||
logger.error("Generate ssh key failed: {}", logOutput); | ||
config.setError(logOutput); | ||
throw new IllegalStateException("Generate ssh key failed"); | ||
} else { | ||
logger.debug(logOutput); | ||
} | ||
} catch (IOException | InterruptedException e) { | ||
config.setError("Generate ssh key failed"); | ||
throw new IllegalStateException("Generate ssh key failed", e); | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
serving/src/main/java/ai/djl/serving/util/ClusterConfig.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,87 @@ | ||
/* | ||
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance | ||
* with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file 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 ai.djl.serving.util; | ||
|
||
import ai.djl.util.Utils; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** A class that holds cluster configurations. */ | ||
public final class ClusterConfig { | ||
|
||
private static final ClusterConfig INSTANCE = new ClusterConfig(); | ||
|
||
private int clusterSize; | ||
private CountDownLatch latch; | ||
private String error; | ||
|
||
private ClusterConfig() { | ||
clusterSize = Integer.parseInt(Utils.getenv("DJL_CLUSTER_SIZE", "1")); | ||
latch = new CountDownLatch(clusterSize); | ||
} | ||
|
||
/** | ||
* Returns the {@code ClusterConfig} singleton object. | ||
* | ||
* @return the {@code ClusterConfig} singleton object | ||
*/ | ||
public static ClusterConfig getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
/** | ||
* Returns the cluster size. | ||
* | ||
* @return the cluster size | ||
*/ | ||
public int getClusterSize() { | ||
return clusterSize; | ||
} | ||
|
||
/** | ||
* Returns the error status message. | ||
* | ||
* @return the error status message | ||
*/ | ||
public String getError() { | ||
return error; | ||
} | ||
|
||
/** | ||
* Sets the error status message. | ||
* | ||
* @param error the error status message | ||
*/ | ||
public void setError(String error) { | ||
this.error = error; | ||
} | ||
|
||
/** Decreases the number of waiting workers. */ | ||
public void countDown() { | ||
latch.countDown(); | ||
} | ||
|
||
/** | ||
* Causes current threads to wait until all workers are ready. | ||
* | ||
* @throws InterruptedException if current thread is interrupted | ||
*/ | ||
public void await() throws InterruptedException { | ||
// TODO: support per model timeout | ||
int timeout = Integer.parseInt(Utils.getenv("MODEL_LOADING_TIMEOUT", "240")); | ||
if (!latch.await(timeout, TimeUnit.SECONDS)) { | ||
error = "Worker nodes timed out"; | ||
} | ||
} | ||
} |
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