@@ -171,7 +171,6 @@ protected void doExecute(Task task, ActionRequest request, ActionListener<Delete
171
171
);
172
172
} else if (isModelNotDeployed (mlModelState )) {
173
173
checkDownstreamTaskBeforeDeleteModel (modelId , isHidden , actionListener );
174
- ;
175
174
} else {
176
175
wrappedListener
177
176
.onFailure (
@@ -291,29 +290,26 @@ private void checkAgentBeforeDeleteModel(String modelId, ActionListener<Boolean>
291
290
private void checkIngestPipelineBeforeDeleteModel (String modelId , ActionListener <Boolean > actionListener ) {
292
291
GetPipelineRequest getPipelineRequest = new GetPipelineRequest ();
293
292
client .execute (GetPipelineAction .INSTANCE , getPipelineRequest , ActionListener .wrap (ingestPipelineResponse -> {
294
- if (!isPipelineContainsModel (
295
- ingestPipelineResponse .pipelines (),
296
- modelId ,
297
- org .opensearch .ingest .PipelineConfiguration ::getConfigAsMap
298
- )) {
299
- actionListener .onResponse (true );
300
- } else {
301
- List <String > searchPipelineIds = getAllPipelineIds (
293
+ List <String > allRelevantPipelineIds = findRelevantPipelines (
302
294
ingestPipelineResponse .pipelines (),
295
+ modelId ,
296
+ org .opensearch .ingest .PipelineConfiguration ::getConfigAsMap ,
303
297
org .opensearch .ingest .PipelineConfiguration ::getId
304
- );
298
+ );
299
+ if (allRelevantPipelineIds .isEmpty ()) {
300
+ actionListener .onResponse (true );
301
+ }
302
+ else {
305
303
actionListener
306
- .onFailure (
307
- new OpenSearchStatusException (
308
- searchPipelineIds .size ()
309
- + " ingest pipelines are still using this model, please delete or update the pipelines first: "
310
- + Arrays .toString (searchPipelineIds .toArray (new String [0 ])),
311
- RestStatus .CONFLICT
312
- )
313
- );
314
-
304
+ .onFailure (
305
+ new OpenSearchStatusException (
306
+ allRelevantPipelineIds .size ()
307
+ + " ingest pipelines are still using this model, please delete or update the pipelines first: "
308
+ + Arrays .toString (allRelevantPipelineIds .toArray (new String [0 ])),
309
+ RestStatus .CONFLICT
310
+ )
311
+ );
315
312
}
316
-
317
313
}, e -> {
318
314
log .error ("Failed to delete ML Model: " + modelId , e );
319
315
actionListener .onFailure (e );
@@ -325,29 +321,26 @@ private void checkIngestPipelineBeforeDeleteModel(String modelId, ActionListener
325
321
private void checkSearchPipelineBeforeDeleteModel (String modelId , ActionListener <Boolean > actionListener ) {
326
322
GetSearchPipelineRequest getSearchPipelineRequest = new GetSearchPipelineRequest ();
327
323
client .execute (GetSearchPipelineAction .INSTANCE , getSearchPipelineRequest , ActionListener .wrap (searchPipelineResponse -> {
328
- if (!isPipelineContainsModel (
329
- searchPipelineResponse .pipelines (),
330
- modelId ,
331
- org .opensearch .search .pipeline .PipelineConfiguration ::getConfigAsMap
332
- )) {
333
- actionListener .onResponse (true );
334
- } else {
335
- List <String > searchPipelineIds = getAllPipelineIds (
324
+ List <String > allRelevantPipelineIds = findRelevantPipelines (
336
325
searchPipelineResponse .pipelines (),
326
+ modelId ,
327
+ org .opensearch .search .pipeline .PipelineConfiguration ::getConfigAsMap ,
337
328
org .opensearch .search .pipeline .PipelineConfiguration ::getId
338
- );
329
+ );
330
+ if (allRelevantPipelineIds .isEmpty ()) {
331
+ actionListener .onResponse (true );
332
+ }
333
+ else {
339
334
actionListener
340
- .onFailure (
341
- new OpenSearchStatusException (
342
- searchPipelineIds .size ()
343
- + " search pipelines are still using this model, please delete or update the pipelines first: "
344
- + Arrays .toString (searchPipelineIds .toArray (new String [0 ])),
345
- RestStatus .CONFLICT
346
- )
347
- );
348
-
335
+ .onFailure (
336
+ new OpenSearchStatusException (
337
+ allRelevantPipelineIds .size ()
338
+ + " search pipelines are still using this model, please delete or update the pipelines first: "
339
+ + Arrays .toString (allRelevantPipelineIds .toArray (new String [0 ])),
340
+ RestStatus .CONFLICT
341
+ )
342
+ );
349
343
}
350
-
351
344
}, e -> {
352
345
log .error ("Failed to delete ML Model: " + modelId , e );
353
346
actionListener .onFailure (e );
@@ -484,18 +477,20 @@ private Boolean isModelNotDeployed(MLModelState mlModelState) {
484
477
&& !mlModelState .equals (MLModelState .PARTIALLY_DEPLOYED );
485
478
}
486
479
487
- private <T > Boolean isPipelineContainsModel (
480
+ private <T > List < String > findRelevantPipelines (
488
481
List <T > pipelineConfigurations ,
489
482
String candidateModelId ,
490
- Function <T , Map <String , Object >> getConfigFunction
483
+ Function <T , Map <String , Object >> getConfigFunction ,
484
+ Function <T , String > getIdFunction
491
485
) {
486
+ List <String > relevantPipelineConfigurations = new ArrayList <>();
492
487
for (T pipelineConfiguration : pipelineConfigurations ) {
493
488
Map <String , Object > config = getConfigFunction .apply (pipelineConfiguration );
494
489
if (searchThroughConfig (config , candidateModelId , "" )) {
495
- return true ;
490
+ relevantPipelineConfigurations . add ( getIdFunction . apply ( pipelineConfiguration )) ;
496
491
}
497
492
}
498
- return false ;
493
+ return relevantPipelineConfigurations ;
499
494
}
500
495
501
496
private Boolean searchThroughConfig (Object searchCandidate , String candidateId , String targetModelKey ) {
@@ -518,13 +513,6 @@ private Boolean searchThroughConfig(Object searchCandidate, String candidateId,
518
513
return flag ;
519
514
}
520
515
521
- private <T > List <String > getAllPipelineIds (List <T > pipelineConfigurations , Function <T , String > getIdFunction ) {
522
- List <String > pipelineIds = new ArrayList <>();
523
- for (T pipelineConfiguration : pipelineConfigurations ) {
524
- pipelineIds .add (getIdFunction .apply (pipelineConfiguration ));
525
- }
526
- return pipelineIds ;
527
- }
528
516
529
517
// this method is only to stub static method.
530
518
@ VisibleForTesting
0 commit comments