1010
1111import java .time .Instant ;
1212import java .util .ArrayList ;
13+ import java .util .EnumSet ;
1314import java .util .List ;
1415import java .util .Set ;
1516
17+ import org .opensearch .OpenSearchException ;
1618import org .opensearch .OpenSearchStatusException ;
1719import org .opensearch .action .ActionRequest ;
1820import org .opensearch .action .admin .indices .delete .DeleteIndexRequest ;
@@ -157,6 +159,14 @@ protected void doExecute(Task task, ActionRequest request, ActionListener<Delete
157159 );
158160 }
159161 }, error -> {
162+ // Preserve client errors (4XX) with their detailed messages
163+ if (error instanceof OpenSearchException ) {
164+ OpenSearchException osException = (OpenSearchException ) error ;
165+ if (osException .status ().getStatus () >= 400 && osException .status ().getStatus () < 500 ) {
166+ actionListener .onFailure (error );
167+ return ;
168+ }
169+ }
160170 log .error ("Failed to retrieve memory container: {} for deletion" , memoryContainerId , error );
161171 actionListener .onFailure (new OpenSearchStatusException ("Internal server error" , RestStatus .INTERNAL_SERVER_ERROR ));
162172 }));
@@ -227,18 +237,8 @@ private void handleDeleteResponse(
227237 // Note: Shared prefix validation already done BEFORE container deletion
228238 if (deleteAllMemories || (deleteMemories != null && !deleteMemories .isEmpty ())) {
229239 MemoryConfiguration configuration = container .getConfiguration ();
230- if (deleteAllMemories ) {
231- deleteAllMemoryIndices (memoryContainerId , configuration , user , deleteResponse , actionListener );
232- } else {
233- deleteSelectiveMemoryIndices (
234- memoryContainerId ,
235- configuration ,
236- deleteMemories ,
237- user ,
238- deleteResponse ,
239- actionListener
240- );
241- }
240+ Set <MemoryType > typesToDelete = deleteAllMemories ? EnumSet .allOf (MemoryType .class ) : deleteMemories ;
241+ deleteSelectiveMemoryIndices (memoryContainerId , configuration , typesToDelete , user , deleteResponse , actionListener );
242242 } else {
243243 // No index deletion requested
244244 actionListener .onResponse (deleteResponse );
@@ -250,58 +250,6 @@ private void handleDeleteResponse(
250250 }
251251 }
252252
253- private void deleteAllMemoryIndices (
254- String memoryContainerId ,
255- MemoryConfiguration configuration ,
256- User user ,
257- DeleteResponse deleteResponse ,
258- ActionListener <DeleteResponse > actionListener
259- ) {
260- // Don't use index pattern here to avoid delete other user's index by mistake
261- DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest (
262- configuration .getSessionIndexName (),
263- configuration .getWorkingMemoryIndexName (),
264- configuration .getLongMemoryIndexName (),
265- configuration .getLongMemoryHistoryIndexName ()
266- );
267-
268- log
269- .debug (
270- "Attempting to delete all memory indices for container {}: [{}, {}, {}, {}]" ,
271- memoryContainerId ,
272- configuration .getSessionIndexName (),
273- configuration .getWorkingMemoryIndexName (),
274- configuration .getLongMemoryIndexName (),
275- configuration .getLongMemoryHistoryIndexName ()
276- );
277- memoryContainerHelper .deleteIndex (configuration , deleteIndexRequest , ActionListener .wrap (r -> {
278- log
279- .info (
280- "Delete memory container - Event: ALL_INDICES_DELETED, Container ID: {}, Indices: [{}, {}, {}, {}], User: {}, Timestamp: {}" ,
281- memoryContainerId ,
282- configuration .getSessionIndexName (),
283- configuration .getWorkingMemoryIndexName (),
284- configuration .getLongMemoryIndexName (),
285- configuration .getLongMemoryHistoryIndexName (),
286- user != null ? user .getName () : "unknown" ,
287- Instant .now ()
288- );
289- actionListener .onResponse (deleteResponse );
290- }, e -> {
291- log
292- .error (
293- "Failed to delete memory indices for container: {}. Indices: [{}, {}, {}, {}]" ,
294- memoryContainerId ,
295- configuration .getSessionIndexName (),
296- configuration .getWorkingMemoryIndexName (),
297- configuration .getLongMemoryIndexName (),
298- configuration .getLongMemoryHistoryIndexName (),
299- e
300- );
301- actionListener .onFailure (new OpenSearchStatusException ("Internal server error" , RestStatus .INTERNAL_SERVER_ERROR ));
302- }));
303- }
304-
305253 private void deleteSelectiveMemoryIndices (
306254 String memoryContainerId ,
307255 MemoryConfiguration configuration ,
@@ -334,19 +282,14 @@ private void deleteSelectiveMemoryIndices(
334282 }
335283
336284 if (!indicesToDelete .isEmpty ()) {
337- log
338- .debug (
339- "Attempting selective deletion of memory indices for container {}: {}, requested types: {}" ,
340- memoryContainerId ,
341- indicesToDelete ,
342- deleteMemories
343- );
344-
285+ boolean isDeleteAll = deleteMemories .containsAll (EnumSet .allOf (MemoryType .class ));
345286 DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest (indicesToDelete .toArray (new String [0 ]));
346287 memoryContainerHelper .deleteIndex (configuration , deleteIndexRequest , ActionListener .wrap (r -> {
288+ String event = isDeleteAll ? "ALL_INDICES_DELETED" : "SELECTIVE_INDICES_DELETED" ;
347289 log
348290 .info (
349- "Delete memory container - Event: SELECTIVE_INDICES_DELETED, Container ID: {}, Indices: {}, Memory Types: {}, User: {}, Timestamp: {}" ,
291+ "Delete memory container - Event: {}, Container ID: {}, Indices: {}, Memory Types: {}, User: {}, Timestamp: {}" ,
292+ event ,
350293 memoryContainerId ,
351294 indicesToDelete ,
352295 deleteMemories ,
@@ -355,6 +298,14 @@ private void deleteSelectiveMemoryIndices(
355298 );
356299 actionListener .onResponse (deleteResponse );
357300 }, e -> {
301+ // Preserve client errors (4XX) with their detailed messages
302+ if (e instanceof OpenSearchException ) {
303+ OpenSearchException osException = (OpenSearchException ) e ;
304+ if (osException .status ().getStatus () >= 400 && osException .status ().getStatus () < 500 ) {
305+ actionListener .onFailure (e );
306+ return ;
307+ }
308+ }
358309 log .error ("Failed to delete selective memory indices [{}] for container: {}." , memoryContainerId , indicesToDelete , e );
359310 actionListener .onFailure (new OpenSearchStatusException ("Internal server error" , RestStatus .INTERNAL_SERVER_ERROR ));
360311 }));
0 commit comments