|
80 | 80 | import org.opensearch.common.settings.Settings;
|
81 | 81 | import org.opensearch.common.util.concurrent.ThreadContext;
|
82 | 82 | import org.opensearch.core.action.ActionListener;
|
| 83 | +import org.opensearch.core.common.breaker.CircuitBreaker; |
| 84 | +import org.opensearch.core.common.breaker.CircuitBreakingException; |
83 | 85 | import org.opensearch.core.xcontent.NamedXContentRegistry;
|
84 | 86 | import org.opensearch.ml.breaker.MLCircuitBreakerService;
|
85 | 87 | import org.opensearch.ml.breaker.ThresholdCircuitBreaker;
|
|
98 | 100 | import org.opensearch.ml.common.model.TextEmbeddingModelConfig;
|
99 | 101 | import org.opensearch.ml.common.transport.deploy.MLDeployModelAction;
|
100 | 102 | import org.opensearch.ml.common.transport.register.MLRegisterModelInput;
|
| 103 | +import org.opensearch.ml.common.transport.register.MLRegisterModelResponse; |
101 | 104 | import org.opensearch.ml.common.transport.upload_chunk.MLRegisterModelMetaInput;
|
102 | 105 | import org.opensearch.ml.engine.MLEngine;
|
103 | 106 | import org.opensearch.ml.engine.ModelHelper;
|
@@ -318,7 +321,7 @@ public void testRegisterMLModel_CircuitBreakerOpen() {
|
318 | 321 | when(mlCircuitBreakerService.checkOpenCB()).thenReturn(thresholdCircuitBreaker);
|
319 | 322 | when(thresholdCircuitBreaker.getName()).thenReturn("Disk Circuit Breaker");
|
320 | 323 | when(thresholdCircuitBreaker.getThreshold()).thenReturn(87);
|
321 |
| - expectedEx.expect(MLException.class); |
| 324 | + expectedEx.expect(CircuitBreakingException.class); |
322 | 325 | expectedEx.expectMessage("Disk Circuit Breaker is open, please check your resources!");
|
323 | 326 | modelManager.registerMLModel(registerModelInput, mlTask);
|
324 | 327 | verify(mlTaskManager).updateMLTask(anyString(), anyMap(), anyLong(), anyBoolean());
|
@@ -409,6 +412,55 @@ public void testRegisterMLModel_RegisterPreBuildModel() throws PrivilegedActionE
|
409 | 412 | );
|
410 | 413 | }
|
411 | 414 |
|
| 415 | + public void testRegisterMLRemoteModel() throws PrivilegedActionException { |
| 416 | + ActionListener<MLRegisterModelResponse> listener = mock(ActionListener.class); |
| 417 | + doNothing().when(mlTaskManager).checkLimitAndAddRunningTask(any(), any()); |
| 418 | + when(mlCircuitBreakerService.checkOpenCB()).thenReturn(null); |
| 419 | + when(threadPool.executor(REGISTER_THREAD_POOL)).thenReturn(taskExecutorService); |
| 420 | + when(modelHelper.downloadPrebuiltModelMetaList(any(), any())).thenReturn(Collections.singletonList("demo")); |
| 421 | + when(modelHelper.isModelAllowed(any(), any())).thenReturn(true); |
| 422 | + MLRegisterModelInput pretrainedInput = mockRemoteModelInput(true); |
| 423 | + MLTask pretrainedTask = MLTask.builder().taskId("pretrained").modelId("pretrained").functionName(FunctionName.REMOTE).build(); |
| 424 | + mock_MLIndicesHandler_initModelIndex(mlIndicesHandler, true); |
| 425 | + doAnswer(invocation -> { |
| 426 | + ActionListener<IndexResponse> indexResponseActionListener = (ActionListener<IndexResponse>) invocation.getArguments()[1]; |
| 427 | + indexResponseActionListener.onResponse(indexResponse); |
| 428 | + return null; |
| 429 | + }).when(client).index(any(), any()); |
| 430 | + when(indexResponse.getId()).thenReturn("mockIndexId"); |
| 431 | + modelManager.registerMLRemoteModel(pretrainedInput, pretrainedTask, listener); |
| 432 | + assertEquals(pretrainedTask.getFunctionName(), FunctionName.REMOTE); |
| 433 | + verify(mlTaskManager).updateMLTask(anyString(), anyMap(), anyLong(), anyBoolean()); |
| 434 | + } |
| 435 | + |
| 436 | + public void testRegisterMLRemoteModel_SkipMemoryCBOpen() { |
| 437 | + ActionListener<MLRegisterModelResponse> listener = mock(ActionListener.class); |
| 438 | + doNothing().when(mlTaskManager).checkLimitAndAddRunningTask(any(), any()); |
| 439 | + when(mlCircuitBreakerService.checkOpenCB()) |
| 440 | + .thenThrow( |
| 441 | + new CircuitBreakingException( |
| 442 | + "Memory Circuit Breaker is open, please check your resources!", |
| 443 | + CircuitBreaker.Durability.TRANSIENT |
| 444 | + ) |
| 445 | + ); |
| 446 | + when(threadPool.executor(REGISTER_THREAD_POOL)).thenReturn(taskExecutorService); |
| 447 | + when(modelHelper.isModelAllowed(any(), any())).thenReturn(true); |
| 448 | + |
| 449 | + MLRegisterModelInput pretrainedInput = mockRemoteModelInput(true); |
| 450 | + MLTask pretrainedTask = MLTask.builder().taskId("pretrained").modelId("pretrained").functionName(FunctionName.REMOTE).build(); |
| 451 | + mock_MLIndicesHandler_initModelIndex(mlIndicesHandler, true); |
| 452 | + doAnswer(invocation -> { |
| 453 | + ActionListener<IndexResponse> indexResponseActionListener = (ActionListener<IndexResponse>) invocation.getArguments()[1]; |
| 454 | + indexResponseActionListener.onResponse(indexResponse); |
| 455 | + return null; |
| 456 | + }).when(client).index(any(), any()); |
| 457 | + when(indexResponse.getId()).thenReturn("mockIndexId"); |
| 458 | + modelManager.registerMLRemoteModel(pretrainedInput, pretrainedTask, listener); |
| 459 | + |
| 460 | + assertEquals(pretrainedTask.getFunctionName(), FunctionName.REMOTE); |
| 461 | + verify(mlTaskManager).updateMLTask(anyString(), anyMap(), anyLong(), anyBoolean()); |
| 462 | + } |
| 463 | + |
412 | 464 | @Ignore
|
413 | 465 | public void testRegisterMLModel_DownloadModelFile() throws IOException {
|
414 | 466 | doNothing().when(mlTaskManager).checkLimitAndAddRunningTask(any(), any());
|
@@ -963,4 +1015,16 @@ private MLRegisterModelInput mockPretrainedInput() {
|
963 | 1015 | .functionName(FunctionName.SPARSE_ENCODING)
|
964 | 1016 | .build();
|
965 | 1017 | }
|
| 1018 | + |
| 1019 | + private MLRegisterModelInput mockRemoteModelInput(boolean isHidden) { |
| 1020 | + return MLRegisterModelInput |
| 1021 | + .builder() |
| 1022 | + .modelName(modelName) |
| 1023 | + .version(version) |
| 1024 | + .modelGroupId("modelGroupId") |
| 1025 | + .modelFormat(modelFormat) |
| 1026 | + .functionName(FunctionName.REMOTE) |
| 1027 | + .deployModel(true) |
| 1028 | + .build(); |
| 1029 | + } |
966 | 1030 | }
|
0 commit comments