From 5705643a5dcecb25e6cfa9066f9f7238396fcdf7 Mon Sep 17 00:00:00 2001 From: Xun Zhang Date: Wed, 12 Jul 2023 16:10:38 -0700 Subject: [PATCH] delete connector successfully if model index is missing Signed-off-by: Xun Zhang (cherry picked from commit 63762db5f14596c3365578146a689ba252578f4d) --- .../DeleteConnectorTransportAction.java | 5 ++++ .../DeleteConnectorTransportActionTests.java | 27 +++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/plugin/src/main/java/org/opensearch/ml/action/connector/DeleteConnectorTransportAction.java b/plugin/src/main/java/org/opensearch/ml/action/connector/DeleteConnectorTransportAction.java index cd49b335f4..b1db55a3db 100644 --- a/plugin/src/main/java/org/opensearch/ml/action/connector/DeleteConnectorTransportAction.java +++ b/plugin/src/main/java/org/opensearch/ml/action/connector/DeleteConnectorTransportAction.java @@ -20,6 +20,7 @@ import org.opensearch.common.inject.Inject; import org.opensearch.common.util.concurrent.ThreadContext; import org.opensearch.core.xcontent.NamedXContentRegistry; +import org.opensearch.index.IndexNotFoundException; import org.opensearch.index.query.QueryBuilders; import org.opensearch.ml.common.MLModel; import org.opensearch.ml.common.exception.MLValidationException; @@ -85,6 +86,10 @@ protected void doExecute(Task task, ActionRequest request, ActionListener { + if (e instanceof IndexNotFoundException) { + deleteConnector(deleteRequest, connectorId, actionListener); + return; + } log.error("Failed to delete ML connector: " + connectorId, e); actionListener.onFailure(e); })); diff --git a/plugin/src/test/java/org/opensearch/ml/action/connector/DeleteConnectorTransportActionTests.java b/plugin/src/test/java/org/opensearch/ml/action/connector/DeleteConnectorTransportActionTests.java index b4e9842170..85900dde05 100644 --- a/plugin/src/test/java/org/opensearch/ml/action/connector/DeleteConnectorTransportActionTests.java +++ b/plugin/src/test/java/org/opensearch/ml/action/connector/DeleteConnectorTransportActionTests.java @@ -30,7 +30,6 @@ import org.opensearch.action.search.ShardSearchFailure; import org.opensearch.action.support.ActionFilters; import org.opensearch.client.Client; -import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.bytes.BytesReference; import org.opensearch.common.settings.Settings; import org.opensearch.common.util.concurrent.ThreadContext; @@ -38,13 +37,13 @@ import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.core.xcontent.ToXContent; import org.opensearch.core.xcontent.XContentBuilder; +import org.opensearch.index.IndexNotFoundException; import org.opensearch.index.get.GetResult; import org.opensearch.ml.common.MLModel; import org.opensearch.ml.common.connector.HttpConnector; import org.opensearch.ml.common.exception.MLValidationException; import org.opensearch.ml.common.transport.connector.MLConnectorDeleteRequest; import org.opensearch.ml.helper.ConnectorAccessControlHelper; -import org.opensearch.ml.model.MLModelManager; import org.opensearch.search.SearchHit; import org.opensearch.search.SearchHits; import org.opensearch.search.aggregations.InternalAggregations; @@ -74,15 +73,9 @@ public class DeleteConnectorTransportActionTests extends OpenSearchTestCase { @Mock NamedXContentRegistry xContentRegistry; - @Mock - private MLModelManager mlModelManager; - @Rule public ExpectedException exceptionRule = ExpectedException.none(); - @Mock - ClusterService clusterService; - DeleteConnectorTransportAction deleteConnectorTransportAction; MLConnectorDeleteRequest mlConnectorDeleteRequest; ThreadContext threadContext; @@ -131,6 +124,24 @@ public void testDeleteConnector_Success() throws IOException { verify(actionListener).onResponse(deleteResponse); } + public void testDeleteConnector_ModelIndexNotFoundSuccess() throws IOException { + doAnswer(invocation -> { + ActionListener listener = invocation.getArgument(1); + listener.onResponse(deleteResponse); + return null; + }).when(client).delete(any(), any()); + + SearchResponse searchResponse = getEmptySearchResponse(); + doAnswer(invocation -> { + ActionListener actionListener = invocation.getArgument(1); + actionListener.onFailure(new IndexNotFoundException("ml_model index not found!")); + return null; + }).when(client).search(any(), any()); + + deleteConnectorTransportAction.doExecute(null, mlConnectorDeleteRequest, actionListener); + verify(actionListener).onResponse(deleteResponse); + } + public void testDeleteConnector_ConnectorNotFound() throws IOException { when(deleteResponse.getResult()).thenReturn(DocWriteResponse.Result.NOT_FOUND);