From 24e65d7e3c8b857a7a7b47669d5aee517220de8a Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Tue, 30 Jan 2024 17:13:38 -0800 Subject: [PATCH] fix bug in delete empty memoty (#1964) (#1966) --- .../ml/memory/index/InteractionsIndex.java | 4 +++ .../memory/index/InteractionsIndexTests.java | 32 +++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/memory/src/main/java/org/opensearch/ml/memory/index/InteractionsIndex.java b/memory/src/main/java/org/opensearch/ml/memory/index/InteractionsIndex.java index 13c00753f7..fdb8d0c282 100644 --- a/memory/src/main/java/org/opensearch/ml/memory/index/InteractionsIndex.java +++ b/memory/src/main/java/org/opensearch/ml/memory/index/InteractionsIndex.java @@ -467,6 +467,10 @@ public void deleteConversation(String conversationId, ActionListener li try (ThreadContext.StoredContext threadContext = client.threadPool().getThreadContext().stashContext()) { ActionListener internalListener = ActionListener.runBefore(listener, () -> threadContext.restore()); ActionListener> searchListener = ActionListener.wrap(interactions -> { + if (interactions.size() == 0) { + internalListener.onResponse(true); + return; + } BulkRequest request = Requests.bulkRequest(); for (Interaction interaction : interactions) { DeleteRequest delRequest = Requests.deleteRequest(INTERACTIONS_INDEX_NAME).id(interaction.getId()); diff --git a/memory/src/test/java/org/opensearch/ml/memory/index/InteractionsIndexTests.java b/memory/src/test/java/org/opensearch/ml/memory/index/InteractionsIndexTests.java index 2ca66a1abe..6d5086761d 100644 --- a/memory/src/test/java/org/opensearch/ml/memory/index/InteractionsIndexTests.java +++ b/memory/src/test/java/org/opensearch/ml/memory/index/InteractionsIndexTests.java @@ -106,6 +106,8 @@ public class InteractionsIndexTests extends OpenSearchTestCase { InteractionsIndex interactionsIndex; + Interaction interaction; + @Before public void setup() { this.client = mock(Client.class); @@ -123,6 +125,16 @@ public void setup() { doReturn(indicesAdminClient).when(adminClient).indices(); doReturn(threadPool).when(client).threadPool(); doReturn(new ThreadContext(Settings.EMPTY)).when(threadPool).getThreadContext(); + interaction = new Interaction( + "iid", + Instant.ofEpochMilli(1234), + "cid", + "inp", + "pt", + "rsp", + "ogn", + Collections.singletonMap("metadata", "some meta") + ); this.interactionsIndex = spy(new InteractionsIndex(client, clusterService, conversationMetaIndex)); } @@ -573,7 +585,7 @@ public void testDelete_BulkHasFailures_ReturnFalse() { }).when(client).bulk(any(), any()); doAnswer(invocation -> { ActionListener> al = invocation.getArgument(2); - al.onResponse(List.of()); + al.onResponse(List.of(interaction)); return null; }).when(interactionsIndex).getAllInteractions(anyString(), anyInt(), any()); @SuppressWarnings("unchecked") @@ -594,7 +606,7 @@ public void testDelete_BulkFails_ThenFail() { }).when(client).bulk(any(), any()); doAnswer(invocation -> { ActionListener> al = invocation.getArgument(2); - al.onResponse(List.of()); + al.onResponse(List.of(interaction)); return null; }).when(interactionsIndex).getAllInteractions(anyString(), anyInt(), any()); @SuppressWarnings("unchecked") @@ -621,6 +633,22 @@ public void testDelete_SearchFails_ThenFail() { assert (argCaptor.getValue().getMessage().equals("Failure during GetAllInteractions")); } + public void testDelete_SearchReturnEmpty_ThenPass() { + doReturn(true).when(metadata).hasIndex(anyString()); + setupGrantAccess(); + doAnswer(invocation -> { + ActionListener> al = invocation.getArgument(2); + al.onResponse(List.of()); + return null; + }).when(interactionsIndex).getAllInteractions(anyString(), anyInt(), any()); + @SuppressWarnings("unchecked") + ActionListener deleteConversationListener = mock(ActionListener.class); + interactionsIndex.deleteConversation("cid", deleteConversationListener); + ArgumentCaptor argCaptor = ArgumentCaptor.forClass(Boolean.class); + verify(deleteConversationListener, times(1)).onResponse(argCaptor.capture()); + assert (argCaptor.getValue()); + } + public void testDelete_NoAccessNoUser_ThenFail() { doReturn(true).when(metadata).hasIndex(anyString()); setupDenyAccess(null);