Skip to content

Commit 66d8e2b

Browse files
authored
Revert inadvertent migration of deleteController to SdkClient (opensearch-project#2961)
Signed-off-by: Daniel Widdis <widdis@gmail.com>
1 parent b26d8b4 commit 66d8e2b

File tree

2 files changed

+104
-48
lines changed

2 files changed

+104
-48
lines changed

plugin/src/main/java/org/opensearch/ml/action/models/DeleteModelTransportAction.java

+34-39
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package org.opensearch.ml.action.models;
77

8+
import static org.opensearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE;
89
import static org.opensearch.common.xcontent.json.JsonXContent.jsonXContent;
910
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
1011
import static org.opensearch.ml.common.CommonValue.ML_CONTROLLER_INDEX;
@@ -25,12 +26,12 @@
2526
import org.opensearch.OpenSearchStatusException;
2627
import org.opensearch.ResourceNotFoundException;
2728
import org.opensearch.action.ActionRequest;
29+
import org.opensearch.action.delete.DeleteRequest;
2830
import org.opensearch.action.delete.DeleteResponse;
2931
import org.opensearch.action.get.GetResponse;
3032
import org.opensearch.action.support.ActionFilters;
3133
import org.opensearch.action.support.HandledTransportAction;
3234
import org.opensearch.client.Client;
33-
import org.opensearch.client.opensearch._types.OpenSearchException;
3435
import org.opensearch.cluster.service.ClusterService;
3536
import org.opensearch.common.inject.Inject;
3637
import org.opensearch.common.settings.Settings;
@@ -383,46 +384,40 @@ private void deleteModelChunksAndController(
383384
* @param modelId model ID
384385
*/
385386
private void deleteController(String modelId, Boolean isHidden, ActionListener<Boolean> actionListener) {
386-
DeleteDataObjectRequest deleteDataObjectRequest = DeleteDataObjectRequest.builder().index(ML_CONTROLLER_INDEX).id(modelId).build();
387-
sdkClient
388-
.deleteDataObjectAsync(deleteDataObjectRequest, client.threadPool().executor(GENERAL_THREAD_POOL))
389-
.whenComplete((r, throwable) -> {
390-
if (throwable == null) {
391-
try {
392-
DeleteResponse deleteResponse = DeleteResponse.fromXContent(r.parser());
393-
log
394-
.info(
395-
getErrorMessage(
396-
"Model controller for the provided model successfully deleted from index, result: {}.",
397-
modelId,
398-
isHidden
399-
),
400-
deleteResponse.getResult()
401-
);
402-
actionListener.onResponse(true);
403-
} catch (Exception e) {
404-
actionListener.onFailure(e);
405-
}
387+
DeleteRequest deleteRequest = new DeleteRequest(ML_CONTROLLER_INDEX, modelId).setRefreshPolicy(IMMEDIATE);
388+
client.delete(deleteRequest, new ActionListener<>() {
389+
@Override
390+
public void onResponse(DeleteResponse deleteResponse) {
391+
log
392+
.info(
393+
getErrorMessage(
394+
"Model controller for the provided model successfully deleted from index, result: {}.",
395+
modelId,
396+
isHidden
397+
),
398+
deleteResponse.getResult()
399+
);
400+
actionListener.onResponse(true);
401+
}
402+
403+
@Override
404+
public void onFailure(Exception e) {
405+
if (e instanceof ResourceNotFoundException) {
406+
log
407+
.info(
408+
getErrorMessage(
409+
"Model controller not deleted due to no model controller found for the given model.",
410+
modelId,
411+
isHidden
412+
)
413+
);
414+
actionListener.onResponse(true); // we consider this as success
406415
} else {
407-
Exception e = SdkClientUtils.unwrapAndConvertToException(throwable);
408-
if (e instanceof ResourceNotFoundException // Local client
409-
|| e instanceof OpenSearchException && // Remote client
410-
((OpenSearchException) e).status() == RestStatus.NOT_FOUND.getStatus()) {
411-
log
412-
.info(
413-
getErrorMessage(
414-
"Model controller not deleted due to no model controller found for the given model.",
415-
modelId,
416-
isHidden
417-
)
418-
);
419-
actionListener.onResponse(true); // we consider this as success
420-
} else {
421-
log.error(getErrorMessage("Failed to delete model controller for the given model.", modelId, isHidden), e);
422-
actionListener.onFailure(e);
423-
}
416+
log.error(getErrorMessage("Failed to delete model controller for the given model.", modelId, isHidden), e);
417+
actionListener.onFailure(e);
424418
}
425-
});
419+
}
420+
});
426421
}
427422

428423
private Boolean isModelNotDeployed(MLModelState mlModelState) {

plugin/src/test/java/org/opensearch/ml/action/models/DeleteModelTransportActionTests.java

+70-9
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ public static void cleanup() {
179179
}
180180

181181
public void testDeleteModel_Success() throws IOException, InterruptedException {
182+
// For controller
183+
doAnswer(invocation -> {
184+
ActionListener<DeleteResponse> listener = invocation.getArgument(1);
185+
listener.onResponse(deleteResponse);
186+
return null;
187+
}).when(client).delete(any(), any());
188+
// For model
182189
PlainActionFuture<DeleteResponse> future = PlainActionFuture.newFuture();
183190
future.onResponse(deleteResponse);
184191
when(client.delete(any())).thenReturn(future);
@@ -207,6 +214,13 @@ public void testDeleteModel_Success() throws IOException, InterruptedException {
207214
}
208215

209216
public void testDeleteRemoteModel_Success() throws IOException, InterruptedException {
217+
// For controller
218+
doAnswer(invocation -> {
219+
ActionListener<DeleteResponse> listener = invocation.getArgument(1);
220+
listener.onResponse(deleteResponse);
221+
return null;
222+
}).when(client).delete(any(), any());
223+
// For model
210224
PlainActionFuture<DeleteResponse> future = PlainActionFuture.newFuture();
211225
future.onResponse(deleteResponse);
212226
when(client.delete(any())).thenReturn(future);
@@ -228,11 +242,16 @@ public void testDeleteRemoteModel_Success() throws IOException, InterruptedExcep
228242
}
229243

230244
public void testDeleteRemoteModel_deleteModelController_failed() throws IOException, InterruptedException {
245+
// For controller
246+
doAnswer(invocation -> {
247+
ActionListener<DeleteResponse> listener = invocation.getArgument(1);
248+
listener.onFailure(new RuntimeException("runtime exception"));
249+
return null;
250+
}).when(client).delete(any(), any());
251+
// For model
231252
PlainActionFuture<DeleteResponse> future = PlainActionFuture.newFuture();
232253
future.onResponse(deleteResponse);
233-
PlainActionFuture<DeleteResponse> failFuture = PlainActionFuture.newFuture();
234-
failFuture.onFailure(new RuntimeException("runtime exception"));
235-
when(client.delete(any())).thenReturn(future).thenReturn(failFuture);
254+
when(client.delete(any())).thenReturn(future);
236255

237256
doAnswer(invocation -> {
238257
ActionListener<BulkByScrollResponse> listener = invocation.getArgument(2);
@@ -257,11 +276,16 @@ public void testDeleteRemoteModel_deleteModelController_failed() throws IOExcept
257276
}
258277

259278
public void testDeleteLocalModel_deleteModelController_failed() throws IOException, InterruptedException {
279+
// For controller
280+
doAnswer(invocation -> {
281+
ActionListener<DeleteResponse> listener = invocation.getArgument(1);
282+
listener.onFailure(new RuntimeException("runtime exception"));
283+
return null;
284+
}).when(client).delete(any(), any());
285+
// For model
260286
PlainActionFuture<DeleteResponse> future = PlainActionFuture.newFuture();
261287
future.onResponse(deleteResponse);
262-
PlainActionFuture<DeleteResponse> failFuture = PlainActionFuture.newFuture();
263-
failFuture.onFailure(new RuntimeException("runtime exception"));
264-
when(client.delete(any())).thenReturn(future).thenReturn(failFuture);
288+
when(client.delete(any())).thenReturn(future);
265289

266290
doAnswer(invocation -> {
267291
ActionListener<BulkByScrollResponse> listener = invocation.getArgument(2);
@@ -286,6 +310,13 @@ public void testDeleteLocalModel_deleteModelController_failed() throws IOExcepti
286310
}
287311

288312
public void testDeleteRemoteModel_deleteModelChunks_failed() throws IOException, InterruptedException {
313+
// For controller
314+
doAnswer(invocation -> {
315+
ActionListener<DeleteResponse> listener = invocation.getArgument(1);
316+
listener.onResponse(deleteResponse);
317+
return null;
318+
}).when(client).delete(any(), any());
319+
// For model
289320
PlainActionFuture<DeleteResponse> future = PlainActionFuture.newFuture();
290321
future.onResponse(deleteResponse);
291322
when(client.delete(any())).thenReturn(future);
@@ -312,6 +343,13 @@ public void testDeleteRemoteModel_deleteModelChunks_failed() throws IOException,
312343
}
313344

314345
public void testDeleteHiddenModel_Success() throws IOException, InterruptedException {
346+
// For controller
347+
doAnswer(invocation -> {
348+
ActionListener<DeleteResponse> listener = invocation.getArgument(1);
349+
listener.onResponse(deleteResponse);
350+
return null;
351+
}).when(client).delete(any(), any());
352+
// For model
315353
PlainActionFuture<DeleteResponse> future = PlainActionFuture.newFuture();
316354
future.onResponse(deleteResponse);
317355
when(client.delete(any())).thenReturn(future);
@@ -371,6 +409,13 @@ public void testDeleteHiddenModel_NoSuperAdminPermission() throws IOException, I
371409
}
372410

373411
public void testDeleteModel_Success_AlgorithmNotNull() throws IOException, InterruptedException {
412+
// For controller
413+
doAnswer(invocation -> {
414+
ActionListener<DeleteResponse> listener = invocation.getArgument(1);
415+
listener.onResponse(deleteResponse);
416+
return null;
417+
}).when(client).delete(any(), any());
418+
// For model
374419
PlainActionFuture<DeleteResponse> future = PlainActionFuture.newFuture();
375420
future.onResponse(deleteResponse);
376421
when(client.delete(any())).thenReturn(future);
@@ -455,6 +500,13 @@ public void testDeleteModel_ModelNotFoundException() throws IOException, Interru
455500
}
456501

457502
public void testDeleteModel_deleteModelController_ResourceNotFoundException() throws IOException, InterruptedException {
503+
// For controller
504+
doAnswer(invocation -> {
505+
ActionListener<DeleteResponse> listener = invocation.getArgument(1);
506+
listener.onFailure(new ResourceNotFoundException("errorMessage"));
507+
return null;
508+
}).when(client).delete(any(), any());
509+
// For model
458510
PlainActionFuture<DeleteResponse> future = PlainActionFuture.newFuture();
459511
future.onResponse(deleteResponse);
460512
PlainActionFuture<DeleteResponse> failFuture = PlainActionFuture.newFuture();
@@ -505,6 +557,13 @@ public void test_ValidationFailedException() throws IOException, InterruptedExce
505557
}
506558

507559
public void testDeleteRemoteModel_modelNotFound_ResourceNotFoundException() throws IOException, InterruptedException {
560+
// For controller
561+
doAnswer(invocation -> {
562+
ActionListener<DeleteResponse> listener = invocation.getArgument(1);
563+
listener.onResponse(deleteResponse);
564+
return null;
565+
}).when(client).delete(any(), any());
566+
// For model
508567
PlainActionFuture<DeleteResponse> failFuture = PlainActionFuture.newFuture();
509568
failFuture.onFailure(new ResourceNotFoundException("resource not found"));
510569
PlainActionFuture<DeleteResponse> future = PlainActionFuture.newFuture();
@@ -567,9 +626,11 @@ public void testModelNotFound_modelChunks_modelController_delete_success() throw
567626
getFuture.onResponse(null);
568627
when(client.get(any())).thenReturn(getFuture);
569628

570-
PlainActionFuture<DeleteResponse> future = PlainActionFuture.newFuture();
571-
future.onResponse(deleteResponse);
572-
when(client.delete(any())).thenReturn(future);
629+
doAnswer(invocation -> {
630+
ActionListener<DeleteResponse> listener = invocation.getArgument(1);
631+
listener.onResponse(deleteResponse);
632+
return null;
633+
}).when(client).delete(any(), any());
573634

574635
doAnswer(invocation -> {
575636
ActionListener<BulkByScrollResponse> listener = invocation.getArgument(2);

0 commit comments

Comments
 (0)