Skip to content

Commit 9104cb8

Browse files
committed
Exit early when no nodes service the model
Now when entering this method its guaranteed to write to index first before sending back the MLUndeploy response. And will also send back a exception if the write back fails Signed-off-by: Brian Flores <[email protected]>
1 parent 12e6aa7 commit 9104cb8

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.opensearch.ml.common.transport.deploy.MLDeployModelRequest;
4141
import org.opensearch.ml.common.transport.undeploy.MLUndeployModelAction;
4242
import org.opensearch.ml.common.transport.undeploy.MLUndeployModelNodesRequest;
43+
import org.opensearch.ml.common.transport.undeploy.MLUndeployModelNodesResponse;
4344
import org.opensearch.ml.common.transport.undeploy.MLUndeployModelsAction;
4445
import org.opensearch.ml.common.transport.undeploy.MLUndeployModelsRequest;
4546
import org.opensearch.ml.common.transport.undeploy.MLUndeployModelsResponse;
@@ -162,15 +163,20 @@ protected void doExecute(Task task, ActionRequest request, ActionListener<MLUnde
162163
private void undeployModels(String[] targetNodeIds, String[] modelIds, ActionListener<MLUndeployModelsResponse> listener) {
163164
MLUndeployModelNodesRequest mlUndeployModelNodesRequest = new MLUndeployModelNodesRequest(targetNodeIds, modelIds);
164165

165-
client.execute(MLUndeployModelAction.INSTANCE, mlUndeployModelNodesRequest, ActionListener.wrap(r -> {
166-
if (r.getNodes().isEmpty()) {
167-
bulkSetModelIndexToUndeploy(modelIds);
166+
client.execute(MLUndeployModelAction.INSTANCE, mlUndeployModelNodesRequest, ActionListener.wrap(response -> {
167+
if (response.getNodes().isEmpty()) {
168+
bulkSetModelIndexToUndeploy(modelIds, listener, response);
169+
return;
168170
}
169-
listener.onResponse(new MLUndeployModelsResponse(r));
171+
listener.onResponse(new MLUndeployModelsResponse(response));
170172
}, listener::onFailure));
171173
}
172174

173-
private void bulkSetModelIndexToUndeploy(String[] modelIds) {
175+
private void bulkSetModelIndexToUndeploy(
176+
String[] modelIds,
177+
ActionListener<MLUndeployModelsResponse> listener,
178+
MLUndeployModelNodesResponse response
179+
) {
174180
BulkRequest bulkUpdateRequest = new BulkRequest();
175181
for (String modelId : modelIds) {
176182
UpdateRequest updateRequest = new UpdateRequest();
@@ -186,10 +192,16 @@ private void bulkSetModelIndexToUndeploy(String[] modelIds) {
186192
updateRequest.index(ML_MODEL_INDEX).id(modelId).doc(builder.build());
187193
bulkUpdateRequest.add(updateRequest);
188194
}
195+
189196
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 -> {
197+
log.info("No nodes service: {}", modelIds.toString());
198+
199+
client.bulk(bulkUpdateRequest, ActionListener.wrap(br -> {
200+
log.debug("Successfully set modelIds to UNDEPLOY in index");
201+
listener.onResponse(new MLUndeployModelsResponse(response));
202+
}, e -> {
192203
log.error("Failed to set modelIds to UNDEPLOY in index", e);
204+
listener.onFailure(e);
193205
}));
194206
}
195207

0 commit comments

Comments
 (0)