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

Add WorkLoadManager.unregisterModel #33

Merged
merged 1 commit into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions serving/src/main/java/ai/djl/serving/models/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -180,10 +181,11 @@ public boolean unregisterWorkflow(String workflowName, String version) {
logger.warn("Model not found: " + workflowName);
return false;
}
Set<ModelInfo> candidateModelsToUnregister = new HashSet<>();
if (version == null) {
// unregister all versions
for (WorkflowInfo workflow : endpoint.getWorkflows()) {
scaleWorkers(workflow, null, 0, 0);
candidateModelsToUnregister.addAll(workflow.getWorkflow().getModels());
workflow.getWorkflow().close();
}
startupModels.remove(workflowName);
Expand All @@ -195,13 +197,20 @@ public boolean unregisterWorkflow(String workflowName, String version) {
logger.warn("Workflow not found: " + workflowName + ':' + version);
return false;
}
scaleWorkers(workflow, null, 0, 0);
candidateModelsToUnregister.addAll(workflow.getWorkflow().getModels());
workflow.getWorkflow().close();
startupModels.remove(workflowName);
}
if (endpoint.getWorkflows().isEmpty()) {
endpoints.remove(workflowName);
}

// Unregister candidate models if they are not used for a remaining endpoint
candidateModelsToUnregister.removeAll(getModels());
for (ModelInfo model : candidateModelsToUnregister) {
wlm.unregisterModel(model);
}

return true;
}

Expand Down Expand Up @@ -250,6 +259,20 @@ public Map<String, Endpoint> getEndpoints() {
return endpoints;
}

/**
* Returns all models in an endpoint.
*
* @return all models in an endpoint
*/
public Set<ModelInfo> getModels() {
return getEndpoints()
.values()
.stream()
.flatMap(e -> e.getWorkflows().stream())
.flatMap(w -> w.getWorkflow().getModels().stream())
.collect(Collectors.toSet());
}

/**
* Returns a version of workflow.
*
Expand Down
16 changes: 12 additions & 4 deletions wlm/src/main/java/ai/djl/serving/wlm/WorkLoadManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ public List<WorkerThread> getWorkers(ModelInfo modelInfo) {
return list;
}

/**
* Removes a model from management.
*
* @param model the model to remove
*/
public void unregisterModel(ModelInfo model) {
WorkerPool pool = getWorkerPoolForModel(model);
pool.scaleWorkers(null, 0, 0);
workerPools.remove(model);
}

/**
* Adds an inference job to the job queue of the next free worker. scales up worker if
* necessary.
Expand Down Expand Up @@ -236,9 +247,6 @@ public WorkerPool scaleWorkers(String deviceName, int newMinWorkers, int newMaxW
cleanup();

List<WorkerThread> threads;
if (minWorkers == 0) {
workerPools.remove(model);
}

threads = getWorkers();
List<WorkerThread> fixedPoolThread =
Expand Down Expand Up @@ -274,7 +282,7 @@ private void addThreads(ModelInfo model, int count, boolean permanent) {
WorkerThread thread =
WorkerThread.builder()
.setModel(model)
.setJobQueue(getWorkerPoolForModel(model).getJobQueue())
.setJobQueue(jobQueue)
.optFixPoolThread(permanent)
.build();

Expand Down