Skip to content

Commit b660d2c

Browse files
committed
[ML] More advanced post-test cleanup of ML indices (#39049)
The .ml-annotations index is created asynchronously when some other ML index exists. This can interfere with the post-test index deletion, as the .ml-annotations index can be created after all other indices have been deleted. This change adds an ML specific post-test cleanup step that runs before the main cleanup and: 1. Checks if any ML indices exist 2. If so, waits for the .ml-annotations index to exist 3. Deletes the other ML indices found in step 1. 4. Calls the super class cleanup This means that by the time the main post-test index cleanup code runs: 1. The only ML index it has to delete will be the .ml-annotations index 2. No other ML indices will exist that could trigger recreation of the .ml-annotations index Fixes #38952
1 parent e8ea85d commit b660d2c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlSingleNodeTestCase.java

+30
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
import org.elasticsearch.xpack.core.XPackSettings;
1616
import org.elasticsearch.xpack.core.ml.MachineLearningField;
1717

18+
import java.util.Arrays;
1819
import java.util.Collection;
1920
import java.util.concurrent.CountDownLatch;
2021
import java.util.concurrent.atomic.AtomicReference;
2122
import java.util.function.Consumer;
2223

24+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
25+
2326
/**
2427
* An extension to {@link ESSingleNodeTestCase} that adds node settings specifically needed for ML test cases.
2528
*/
@@ -46,6 +49,33 @@ protected Collection<Class<? extends Plugin>> getPlugins() {
4649
return pluginList(LocalStateMachineLearning.class);
4750
}
4851

52+
/**
53+
* This cleanup is to fix the problem described in
54+
* https://github.com/elastic/elasticsearch/issues/38952
55+
*/
56+
@Override
57+
public void tearDown() throws Exception {
58+
try {
59+
logger.trace("[{}#{}]: ML-specific after test cleanup", getTestClass().getSimpleName(), getTestName());
60+
String[] nonAnnotationMlIndices;
61+
boolean mlAnnotationsIndexExists;
62+
do {
63+
String[] mlIndices = client().admin().indices().prepareGetIndex().addIndices(".ml-*").get().indices();
64+
nonAnnotationMlIndices = Arrays.stream(mlIndices).filter(name -> name.startsWith(".ml-annotations") == false)
65+
.toArray(String[]::new);
66+
mlAnnotationsIndexExists = mlIndices.length > nonAnnotationMlIndices.length;
67+
} while (nonAnnotationMlIndices.length > 0 && mlAnnotationsIndexExists == false);
68+
if (nonAnnotationMlIndices.length > 0) {
69+
// Delete the ML indices apart from the annotations index. The annotations index will be deleted by the
70+
// base class cleanup. We want to delete all the others first so that the annotations index doesn't get
71+
// automatically recreated.
72+
assertAcked(client().admin().indices().prepareDelete(nonAnnotationMlIndices).get());
73+
}
74+
} finally {
75+
super.tearDown();
76+
}
77+
}
78+
4979
protected void waitForMlTemplates() throws Exception {
5080
// block until the templates are installed
5181
assertBusy(() -> {

0 commit comments

Comments
 (0)