forked from opensearch-project/ml-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLExecuteTaskRunnerTests.java
160 lines (139 loc) · 6.21 KB
/
MLExecuteTaskRunnerTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.ml.task;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.opensearch.ml.settings.MLCommonsSettings.*;
import static org.opensearch.ml.settings.MLCommonsSettings.ML_COMMONS_MAX_DEPLOY_MODEL_TASKS_PER_NODE;
import static org.opensearch.ml.utils.TestHelper.clusterSetting;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.opensearch.client.Client;
import org.opensearch.cluster.service.ClusterApplierService;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.action.ActionListener;
import org.opensearch.ml.breaker.MLCircuitBreakerService;
import org.opensearch.ml.cluster.DiscoveryNodeHelper;
import org.opensearch.ml.common.FunctionName;
import org.opensearch.ml.common.input.execute.samplecalculator.LocalSampleCalculatorInput;
import org.opensearch.ml.common.transport.execute.MLExecuteTaskRequest;
import org.opensearch.ml.common.transport.execute.MLExecuteTaskResponse;
import org.opensearch.ml.engine.MLEngine;
import org.opensearch.ml.engine.encryptor.Encryptor;
import org.opensearch.ml.engine.encryptor.EncryptorImpl;
import org.opensearch.ml.engine.indices.MLInputDatasetHandler;
import org.opensearch.ml.stats.MLNodeLevelStat;
import org.opensearch.ml.stats.MLStat;
import org.opensearch.ml.stats.MLStats;
import org.opensearch.ml.stats.suppliers.CounterSupplier;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.threadpool.ThreadPool;
public class MLExecuteTaskRunnerTests extends OpenSearchTestCase {
@Mock
ThreadPool threadPool;
@Mock
Client client;
@Mock
MLTaskManager mlTaskManager;
@Mock
ExecutorService executorService;
@Mock
MLTaskDispatcher mlTaskDispatcher;
@Mock
MLCircuitBreakerService mlCircuitBreakerService;
@Mock
ActionListener<MLExecuteTaskResponse> listener;
@Mock
DiscoveryNodeHelper nodeHelper;
@Mock
ClusterApplierService clusterApplierService;
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
MLInputDatasetHandler mlInputDatasetHandler;
MLExecuteTaskRunner taskRunner;
MLStats mlStats;
MLExecuteTaskRequest mlExecuteTaskRequest;
MLEngine mlEngine;
private Encryptor encryptor;
Settings settings;
ClusterService clusterService;
@Before
public void setup() {
MockitoAnnotations.openMocks(this);
encryptor = new EncryptorImpl("m+dWmfmnNRiNlOdej/QelEkvMTyH//frS2TBeS2BP4w=");
mlEngine = new MLEngine(Path.of("/tmp/djl-cache/" + randomAlphaOfLength(10)), encryptor);
when(threadPool.executor(anyString())).thenReturn(executorService);
doAnswer(invocation -> {
Runnable runnable = invocation.getArgument(0);
runnable.run();
return null;
}).when(executorService).execute(any(Runnable.class));
settings = Settings.builder().put(ML_COMMONS_MAX_MODELS_PER_NODE.getKey(), 10).build();
settings = Settings.builder().put(ML_COMMONS_MAX_REGISTER_MODEL_TASKS_PER_NODE.getKey(), 10).build();
settings = Settings.builder().put(ML_COMMONS_MONITORING_REQUEST_COUNT.getKey(), 10).build();
settings = Settings.builder().put(ML_COMMONS_MAX_DEPLOY_MODEL_TASKS_PER_NODE.getKey(), 10).build();
settings = Settings.builder().put(ML_COMMONS_ENABLE_INHOUSE_PYTHON_MODEL.getKey(), false).build();
ClusterSettings clusterSettings = clusterSetting(
settings,
ML_COMMONS_MAX_MODELS_PER_NODE,
ML_COMMONS_MAX_REGISTER_MODEL_TASKS_PER_NODE,
ML_COMMONS_MONITORING_REQUEST_COUNT,
ML_COMMONS_MAX_DEPLOY_MODEL_TASKS_PER_NODE,
ML_COMMONS_ENABLE_INHOUSE_PYTHON_MODEL
);
clusterService = spy(new ClusterService(settings, clusterSettings, null, clusterApplierService));
when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
Map<Enum, MLStat<?>> stats = new ConcurrentHashMap<>();
stats.put(MLNodeLevelStat.ML_EXECUTING_TASK_COUNT, new MLStat<>(false, new CounterSupplier()));
stats.put(MLNodeLevelStat.ML_REQUEST_COUNT, new MLStat<>(false, new CounterSupplier()));
stats.put(MLNodeLevelStat.ML_FAILURE_COUNT, new MLStat<>(false, new CounterSupplier()));
stats.put(MLNodeLevelStat.ML_DEPLOYED_MODEL_COUNT, new MLStat<>(false, new CounterSupplier()));
this.mlStats = new MLStats(stats);
mlInputDatasetHandler = spy(new MLInputDatasetHandler(client));
taskRunner = spy(
new MLExecuteTaskRunner(
threadPool,
clusterService,
client,
mlTaskManager,
mlStats,
mlInputDatasetHandler,
mlTaskDispatcher,
mlCircuitBreakerService,
nodeHelper,
mlEngine
)
);
mlExecuteTaskRequest = new MLExecuteTaskRequest(
FunctionName.LOCAL_SAMPLE_CALCULATOR,
new LocalSampleCalculatorInput("sum", Arrays.asList(1.0, 2.0))
);
}
public void testExecuteTask_Success() {
taskRunner.executeTask(mlExecuteTaskRequest, listener);
verify(listener).onResponse(any(MLExecuteTaskResponse.class));
}
public void testExecuteTask_NoExecutorService() {
exceptionRule.expect(IllegalArgumentException.class);
when(threadPool.executor(anyString())).thenThrow(new IllegalArgumentException());
taskRunner.executeTask(mlExecuteTaskRequest, listener);
verify(listener, never()).onResponse(any(MLExecuteTaskResponse.class));
}
}