Skip to content

Commit 12e6aa7

Browse files
committed
undeploy models with no WorkerNodes
This commit aims to undeploy modelIds that have no nodes associated to them so as to keep the intention of undeploy truthful. Signed-off-by: Brian Flores <[email protected]>
1 parent 7e8b253 commit 12e6aa7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

plugin/src/main/java/org/opensearch/ml/action/undeploy/TransportUndeployModelsAction.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77

88
import static org.opensearch.ml.common.CommonValue.ML_MODEL_INDEX;
99

10+
import java.time.Instant;
1011
import java.util.Arrays;
1112
import java.util.List;
1213
import java.util.stream.Collectors;
1314

1415
import org.opensearch.OpenSearchStatusException;
1516
import org.opensearch.action.ActionRequest;
17+
import org.opensearch.action.bulk.BulkRequest;
1618
import org.opensearch.action.search.SearchRequest;
1719
import org.opensearch.action.search.SearchResponse;
1820
import org.opensearch.action.support.ActionFilters;
1921
import org.opensearch.action.support.HandledTransportAction;
22+
import org.opensearch.action.support.WriteRequest;
23+
import org.opensearch.action.update.UpdateRequest;
2024
import org.opensearch.client.Client;
2125
import org.opensearch.cluster.service.ClusterService;
2226
import org.opensearch.common.inject.Inject;
@@ -32,6 +36,7 @@
3236
import org.opensearch.index.query.TermsQueryBuilder;
3337
import org.opensearch.ml.cluster.DiscoveryNodeHelper;
3438
import org.opensearch.ml.common.MLModel;
39+
import org.opensearch.ml.common.model.MLModelState;
3540
import org.opensearch.ml.common.transport.deploy.MLDeployModelRequest;
3641
import org.opensearch.ml.common.transport.undeploy.MLUndeployModelAction;
3742
import org.opensearch.ml.common.transport.undeploy.MLUndeployModelNodesRequest;
@@ -51,6 +56,7 @@
5156
import org.opensearch.transport.TransportService;
5257

5358
import com.google.common.annotations.VisibleForTesting;
59+
import com.google.common.collect.ImmutableMap;
5460

5561
import lombok.extern.log4j.Log4j2;
5662

@@ -157,10 +163,36 @@ private void undeployModels(String[] targetNodeIds, String[] modelIds, ActionLis
157163
MLUndeployModelNodesRequest mlUndeployModelNodesRequest = new MLUndeployModelNodesRequest(targetNodeIds, modelIds);
158164

159165
client.execute(MLUndeployModelAction.INSTANCE, mlUndeployModelNodesRequest, ActionListener.wrap(r -> {
166+
if (r.getNodes().isEmpty()) {
167+
bulkSetModelIndexToUndeploy(modelIds);
168+
}
160169
listener.onResponse(new MLUndeployModelsResponse(r));
161170
}, listener::onFailure));
162171
}
163172

173+
private void bulkSetModelIndexToUndeploy(String[] modelIds) {
174+
BulkRequest bulkUpdateRequest = new BulkRequest();
175+
for (String modelId : modelIds) {
176+
UpdateRequest updateRequest = new UpdateRequest();
177+
Instant now = Instant.now();
178+
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
179+
builder.put(MLModel.MODEL_STATE_FIELD, MLModelState.UNDEPLOYED.name());
180+
181+
builder.put(MLModel.PLANNING_WORKER_NODES_FIELD, List.of());
182+
builder.put(MLModel.PLANNING_WORKER_NODE_COUNT_FIELD, 0);
183+
184+
builder.put(MLModel.LAST_UPDATED_TIME_FIELD, now.toEpochMilli());
185+
builder.put(MLModel.CURRENT_WORKER_NODE_COUNT_FIELD, 0);
186+
updateRequest.index(ML_MODEL_INDEX).id(modelId).doc(builder.build());
187+
bulkUpdateRequest.add(updateRequest);
188+
}
189+
bulkUpdateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
190+
log.info("No models service: {}", modelIds.toString());
191+
client.bulk(bulkUpdateRequest, ActionListener.wrap(br -> { log.debug("Successfully set modelIds to UNDEPLOY in index"); }, e -> {
192+
log.error("Failed to set modelIds to UNDEPLOY in index", e);
193+
}));
194+
}
195+
164196
private void validateAccess(String modelId, ActionListener<Boolean> listener) {
165197
User user = RestActionUtils.getUserContext(client);
166198
boolean isSuperAdmin = isSuperAdminUserWrapper(clusterService, client);

0 commit comments

Comments
 (0)