|
19 | 19 | import org.elasticsearch.xpack.core.ml.MlTasks;
|
20 | 20 | import org.elasticsearch.xpack.core.ml.action.OpenJobAction;
|
21 | 21 | import org.elasticsearch.xpack.core.ml.action.StartDataFrameAnalyticsAction;
|
| 22 | +import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; |
22 | 23 | import org.elasticsearch.xpack.core.ml.job.config.AnalysisLimits;
|
23 | 24 | import org.elasticsearch.xpack.core.ml.job.config.Job;
|
24 | 25 | import org.elasticsearch.xpack.ml.dataframe.persistence.DataFrameAnalyticsConfigProvider;
|
|
32 | 33 | import java.util.List;
|
33 | 34 | import java.util.Map;
|
34 | 35 | import java.util.concurrent.ExecutorService;
|
| 36 | +import java.util.concurrent.atomic.AtomicBoolean; |
35 | 37 | import java.util.concurrent.atomic.AtomicReference;
|
36 | 38 | import java.util.function.Consumer;
|
37 | 39 |
|
38 | 40 | import static org.hamcrest.CoreMatchers.instanceOf;
|
39 | 41 | import static org.mockito.Matchers.any;
|
| 42 | +import static org.mockito.Matchers.anyBoolean; |
40 | 43 | import static org.mockito.Matchers.eq;
|
41 | 44 | import static org.mockito.Mockito.anyString;
|
42 | 45 | import static org.mockito.Mockito.doAnswer;
|
@@ -125,6 +128,66 @@ public void testRefreshAll() {
|
125 | 128 | }
|
126 | 129 | }
|
127 | 130 |
|
| 131 | + public void testRefreshAllFailure() { |
| 132 | + |
| 133 | + Map<String, PersistentTasksCustomMetaData.PersistentTask<?>> tasks = new HashMap<>(); |
| 134 | + |
| 135 | + int numAnomalyDetectorJobTasks = randomIntBetween(2, 5); |
| 136 | + for (int i = 1; i <= numAnomalyDetectorJobTasks; ++i) { |
| 137 | + String jobId = "job" + i; |
| 138 | + PersistentTasksCustomMetaData.PersistentTask<?> task = makeTestAnomalyDetectorTask(jobId); |
| 139 | + tasks.put(task.getId(), task); |
| 140 | + } |
| 141 | + |
| 142 | + int numDataFrameAnalyticsTasks = randomIntBetween(2, 5); |
| 143 | + for (int i = 1; i <= numDataFrameAnalyticsTasks; ++i) { |
| 144 | + String id = "analytics" + i; |
| 145 | + PersistentTasksCustomMetaData.PersistentTask<?> task = makeTestDataFrameAnalyticsTask(id); |
| 146 | + tasks.put(task.getId(), task); |
| 147 | + } |
| 148 | + |
| 149 | + PersistentTasksCustomMetaData persistentTasks = |
| 150 | + new PersistentTasksCustomMetaData(numAnomalyDetectorJobTasks + numDataFrameAnalyticsTasks, tasks); |
| 151 | + |
| 152 | + doAnswer(invocation -> { |
| 153 | + @SuppressWarnings("unchecked") |
| 154 | + Consumer<Long> listener = (Consumer<Long>) invocation.getArguments()[3]; |
| 155 | + listener.accept(randomLongBetween(1000, 1000000)); |
| 156 | + return null; |
| 157 | + }).when(jobResultsProvider).getEstablishedMemoryUsage(anyString(), any(), any(), any(Consumer.class), any()); |
| 158 | + |
| 159 | + // First run a refresh using a component that calls the onFailure method of the listener |
| 160 | + |
| 161 | + doAnswer(invocation -> { |
| 162 | + @SuppressWarnings("unchecked") |
| 163 | + ActionListener<List<DataFrameAnalyticsConfig>> listener = |
| 164 | + (ActionListener<List<DataFrameAnalyticsConfig>>) invocation.getArguments()[2]; |
| 165 | + listener.onFailure(new IllegalArgumentException("computer says no")); |
| 166 | + return null; |
| 167 | + }).when(configProvider).getMultiple(anyString(), anyBoolean(), any(ActionListener.class)); |
| 168 | + |
| 169 | + AtomicBoolean gotErrorResponse = new AtomicBoolean(false); |
| 170 | + memoryTracker.refresh(persistentTasks, |
| 171 | + ActionListener.wrap(aVoid -> fail("Expected error response"), e -> gotErrorResponse.set(true))); |
| 172 | + assertTrue(gotErrorResponse.get()); |
| 173 | + |
| 174 | + // Now run another refresh using a component that calls the onResponse method of the listener - this |
| 175 | + // proves that the ML memory tracker has not been permanently blocked up by the previous failure |
| 176 | + |
| 177 | + doAnswer(invocation -> { |
| 178 | + @SuppressWarnings("unchecked") |
| 179 | + ActionListener<List<DataFrameAnalyticsConfig>> listener = |
| 180 | + (ActionListener<List<DataFrameAnalyticsConfig>>) invocation.getArguments()[2]; |
| 181 | + listener.onResponse(Collections.emptyList()); |
| 182 | + return null; |
| 183 | + }).when(configProvider).getMultiple(anyString(), anyBoolean(), any(ActionListener.class)); |
| 184 | + |
| 185 | + AtomicBoolean gotSuccessResponse = new AtomicBoolean(false); |
| 186 | + memoryTracker.refresh(persistentTasks, |
| 187 | + ActionListener.wrap(aVoid -> gotSuccessResponse.set(true), e -> fail("Expected success response"))); |
| 188 | + assertTrue(gotSuccessResponse.get()); |
| 189 | + } |
| 190 | + |
128 | 191 | public void testRefreshOneAnomalyDetectorJob() {
|
129 | 192 |
|
130 | 193 | boolean isMaster = randomBoolean();
|
|
0 commit comments