Skip to content

Commit ea8cc3d

Browse files
committed
fix bug in delete empty memoty
Signed-off-by: Xun Zhang <xunzh@amazon.com>
1 parent c5225de commit ea8cc3d

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

memory/src/main/java/org/opensearch/ml/memory/index/InteractionsIndex.java

+4
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,10 @@ public void deleteConversation(String conversationId, ActionListener<Boolean> li
467467
try (ThreadContext.StoredContext threadContext = client.threadPool().getThreadContext().stashContext()) {
468468
ActionListener<Boolean> internalListener = ActionListener.runBefore(listener, () -> threadContext.restore());
469469
ActionListener<List<Interaction>> searchListener = ActionListener.wrap(interactions -> {
470+
if (interactions.size() == 0) {
471+
internalListener.onResponse(true);
472+
return;
473+
}
470474
BulkRequest request = Requests.bulkRequest();
471475
for (Interaction interaction : interactions) {
472476
DeleteRequest delRequest = Requests.deleteRequest(INTERACTIONS_INDEX_NAME).id(interaction.getId());

memory/src/test/java/org/opensearch/ml/memory/index/InteractionsIndexTests.java

+30-2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ public class InteractionsIndexTests extends OpenSearchTestCase {
106106

107107
InteractionsIndex interactionsIndex;
108108

109+
Interaction interaction;
110+
109111
@Before
110112
public void setup() {
111113
this.client = mock(Client.class);
@@ -123,6 +125,16 @@ public void setup() {
123125
doReturn(indicesAdminClient).when(adminClient).indices();
124126
doReturn(threadPool).when(client).threadPool();
125127
doReturn(new ThreadContext(Settings.EMPTY)).when(threadPool).getThreadContext();
128+
interaction = new Interaction(
129+
"iid",
130+
Instant.ofEpochMilli(1234),
131+
"cid",
132+
"inp",
133+
"pt",
134+
"rsp",
135+
"ogn",
136+
Collections.singletonMap("metadata", "some meta")
137+
);
126138
this.interactionsIndex = spy(new InteractionsIndex(client, clusterService, conversationMetaIndex));
127139
}
128140

@@ -573,7 +585,7 @@ public void testDelete_BulkHasFailures_ReturnFalse() {
573585
}).when(client).bulk(any(), any());
574586
doAnswer(invocation -> {
575587
ActionListener<List<Interaction>> al = invocation.getArgument(2);
576-
al.onResponse(List.of());
588+
al.onResponse(List.of(interaction));
577589
return null;
578590
}).when(interactionsIndex).getAllInteractions(anyString(), anyInt(), any());
579591
@SuppressWarnings("unchecked")
@@ -594,7 +606,7 @@ public void testDelete_BulkFails_ThenFail() {
594606
}).when(client).bulk(any(), any());
595607
doAnswer(invocation -> {
596608
ActionListener<List<Interaction>> al = invocation.getArgument(2);
597-
al.onResponse(List.of());
609+
al.onResponse(List.of(interaction));
598610
return null;
599611
}).when(interactionsIndex).getAllInteractions(anyString(), anyInt(), any());
600612
@SuppressWarnings("unchecked")
@@ -621,6 +633,22 @@ public void testDelete_SearchFails_ThenFail() {
621633
assert (argCaptor.getValue().getMessage().equals("Failure during GetAllInteractions"));
622634
}
623635

636+
public void testDelete_SearchReturnEmpty_ThenPass() {
637+
doReturn(true).when(metadata).hasIndex(anyString());
638+
setupGrantAccess();
639+
doAnswer(invocation -> {
640+
ActionListener<List<Interaction>> al = invocation.getArgument(2);
641+
al.onResponse(List.of());
642+
return null;
643+
}).when(interactionsIndex).getAllInteractions(anyString(), anyInt(), any());
644+
@SuppressWarnings("unchecked")
645+
ActionListener<Boolean> deleteConversationListener = mock(ActionListener.class);
646+
interactionsIndex.deleteConversation("cid", deleteConversationListener);
647+
ArgumentCaptor<Boolean> argCaptor = ArgumentCaptor.forClass(Boolean.class);
648+
verify(deleteConversationListener, times(1)).onResponse(argCaptor.capture());
649+
assert (argCaptor.getValue());
650+
}
651+
624652
public void testDelete_NoAccessNoUser_ThenFail() {
625653
doReturn(true).when(metadata).hasIndex(anyString());
626654
setupDenyAccess(null);

0 commit comments

Comments
 (0)