-
Notifications
You must be signed in to change notification settings - Fork 68
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
[serving] Adds mutliple node cluster configuration support #2190
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be slightly nicer to configure this using the config manager rather than just environment variables There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: any reason for choosing 8888? Should it be a closer port like 8081?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
User may choose management port as 8081