diff --git a/plugin/src/main/java/org/opensearch/ml/action/models/DeleteModelTransportAction.java b/plugin/src/main/java/org/opensearch/ml/action/models/DeleteModelTransportAction.java index 414aed4576..1ff015a6ea 100644 --- a/plugin/src/main/java/org/opensearch/ml/action/models/DeleteModelTransportAction.java +++ b/plugin/src/main/java/org/opensearch/ml/action/models/DeleteModelTransportAction.java @@ -15,7 +15,6 @@ import static org.opensearch.ml.utils.RestActionUtils.getFetchSourceContext; import org.opensearch.OpenSearchStatusException; -import org.opensearch.ResourceNotFoundException; import org.opensearch.action.ActionRequest; import org.opensearch.action.DocWriteResponse; import org.opensearch.action.delete.DeleteRequest; @@ -39,6 +38,7 @@ import org.opensearch.index.reindex.BulkByScrollResponse; import org.opensearch.index.reindex.DeleteByQueryAction; import org.opensearch.index.reindex.DeleteByQueryRequest; +import org.opensearch.ml.common.FunctionName; import org.opensearch.ml.common.MLModel; import org.opensearch.ml.common.model.MLModelState; import org.opensearch.ml.common.transport.model.MLModelDeleteAction; @@ -113,6 +113,7 @@ protected void doExecute(Task task, ActionRequest request, ActionListener { wrappedListener.onFailure(e); })); + }, wrappedListener::onFailure)); } catch (Exception e) { log.error("Failed to delete ML model " + modelId, e); actionListener.onFailure(e); @@ -183,13 +184,7 @@ void deleteModelChunks(String modelId, DeleteResponse deleteResponse, ActionList if ((r.getBulkFailures() == null || r.getBulkFailures().size() == 0) && (r.getSearchFailures() == null || r.getSearchFailures().size() == 0)) { log.debug("All model chunks are deleted for model {}", modelId); - if (deleteResponse != null) { - // If model metaData not found and deleteResponse is null, do not return here. - // ResourceNotFound is returned to notify that this model was deleted. - // This is a walk around to avoid cleaning up model leftovers. Will revisit if - // necessary. - actionListener.onResponse(deleteResponse); - } + actionListener.onResponse(deleteResponse); } else { returnFailure(r, modelId, actionListener); } @@ -212,23 +207,22 @@ private void returnFailure(BulkByScrollResponse response, String modelId, Action actionListener.onFailure(new OpenSearchStatusException(errorMessage, RestStatus.INTERNAL_SERVER_ERROR)); } - private void deleteModel(String modelId, ActionListener actionListener) { + private void deleteModel(String modelId, FunctionName functionName, ActionListener actionListener) { DeleteRequest deleteRequest = new DeleteRequest(ML_MODEL_INDEX, modelId).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); client.delete(deleteRequest, new ActionListener<>() { @Override public void onResponse(DeleteResponse deleteResponse) { - deleteModelChunks(modelId, deleteResponse, actionListener); + if (FunctionName.REMOTE != functionName) { + deleteModelChunks(modelId, deleteResponse, actionListener); + } else { + actionListener.onResponse(deleteResponse); + } deleteModelController(modelId); } @Override public void onFailure(Exception e) { log.error("Failed to delete model meta data for model: " + modelId, e); - if (e instanceof ResourceNotFoundException) { - deleteModelChunks(modelId, null, actionListener); - deleteModelController(modelId); - actionListener.onFailure(new OpenSearchStatusException("MODEL_NOT_FOUND" + modelId, RestStatus.NOT_FOUND)); - } actionListener.onFailure(e); } });