From 60ef0fd6dfeda18729f1dc2ec6ea9c0418c6ff69 Mon Sep 17 00:00:00 2001 From: Bhavana Ramaram Date: Wed, 11 Oct 2023 09:01:18 -0700 Subject: [PATCH] fix for delete model group API throwing incorrect error when model index not created (#1485) * fix for delete model group API throwing incorrect error when model index not created Signed-off-by: Bhavana Ramaram --- .../DeleteModelGroupTransportAction.java | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/plugin/src/main/java/org/opensearch/ml/action/model_group/DeleteModelGroupTransportAction.java b/plugin/src/main/java/org/opensearch/ml/action/model_group/DeleteModelGroupTransportAction.java index 0fb5ff55c4..27cb5dd55c 100644 --- a/plugin/src/main/java/org/opensearch/ml/action/model_group/DeleteModelGroupTransportAction.java +++ b/plugin/src/main/java/org/opensearch/ml/action/model_group/DeleteModelGroupTransportAction.java @@ -25,7 +25,6 @@ import org.opensearch.index.IndexNotFoundException; import org.opensearch.index.query.BoolQueryBuilder; import org.opensearch.index.query.TermQueryBuilder; -import org.opensearch.ml.common.exception.MLResourceNotFoundException; import org.opensearch.ml.common.exception.MLValidationException; import org.opensearch.ml.common.transport.model_group.MLModelGroupDeleteAction; import org.opensearch.ml.common.transport.model_group.MLModelGroupDeleteRequest; @@ -84,26 +83,14 @@ protected void doExecute(Task task, ActionRequest request, ActionListener { if (mlModels == null || mlModels.getHits().getTotalHits() == null || mlModels.getHits().getTotalHits().value == 0) { - client.delete(deleteRequest, new ActionListener() { - @Override - public void onResponse(DeleteResponse deleteResponse) { - log.debug("Completed Delete Model Group Request, task id:{} deleted", modelGroupId); - wrappedListener.onResponse(deleteResponse); - } - - @Override - public void onFailure(Exception e) { - log.error("Failed to delete ML Model Group " + modelGroupId, e); - wrappedListener.onFailure(e); - } - }); + deleteModelGroup(deleteRequest, modelGroupId, wrappedListener); } else { throw new MLValidationException("Cannot delete the model group when it has associated model versions"); } }, e -> { if (e instanceof IndexNotFoundException) { - wrappedListener.onFailure(new MLResourceNotFoundException("Fail to find model group")); + deleteModelGroup(deleteRequest, modelGroupId, wrappedListener); } else { log.error("Failed to search models with the specified Model Group Id " + modelGroupId, e); wrappedListener.onFailure(e); @@ -116,4 +103,20 @@ public void onFailure(Exception e) { })); } } + + private void deleteModelGroup(DeleteRequest deleteRequest, String modelGroupId, ActionListener actionListener) { + client.delete(deleteRequest, new ActionListener() { + @Override + public void onResponse(DeleteResponse deleteResponse) { + log.debug("Completed Delete Model Group Request, task id:{} deleted", modelGroupId); + actionListener.onResponse(deleteResponse); + } + + @Override + public void onFailure(Exception e) { + log.error("Failed to delete ML Model Group " + modelGroupId, e); + actionListener.onFailure(e); + } + }); + } }