Skip to content

Commit 0c1d799

Browse files
authored
Fix testDeleteActionDeletesSearchableSnapshot (#68751)
It could happen for ILM to run so fast the test did not get to pick up the snapshot name from the ILM execution state. This changes the implementation of the test to not rely on that snapshot name, but to assert that the test repository is empty after ILM completes the cycle for the first generation backing index.
1 parent 1e29fb3 commit 0c1d799

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/SearchableSnapshotActionIT.java

+21-29
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
import org.apache.http.entity.ContentType;
1111
import org.apache.http.entity.StringEntity;
12-
import org.apache.http.util.EntityUtils;
13-
import org.elasticsearch.client.Client;
1412
import org.elasticsearch.client.Request;
1513
import org.elasticsearch.client.Response;
1614
import org.elasticsearch.cluster.metadata.DataStream;
@@ -22,23 +20,20 @@
2220
import org.elasticsearch.common.xcontent.XContentBuilder;
2321
import org.elasticsearch.common.xcontent.XContentHelper;
2422
import org.elasticsearch.common.xcontent.XContentType;
25-
import org.elasticsearch.test.client.NoOpClient;
2623
import org.elasticsearch.test.rest.ESRestTestCase;
2724
import org.elasticsearch.xpack.core.ilm.DeleteAction;
2825
import org.elasticsearch.xpack.core.ilm.ForceMergeAction;
2926
import org.elasticsearch.xpack.core.ilm.FreezeAction;
3027
import org.elasticsearch.xpack.core.ilm.LifecycleAction;
3128
import org.elasticsearch.xpack.core.ilm.LifecyclePolicy;
3229
import org.elasticsearch.xpack.core.ilm.LifecycleSettings;
33-
import org.elasticsearch.xpack.core.ilm.MountSnapshotStep;
3430
import org.elasticsearch.xpack.core.ilm.Phase;
3531
import org.elasticsearch.xpack.core.ilm.PhaseCompleteStep;
3632
import org.elasticsearch.xpack.core.ilm.RolloverAction;
3733
import org.elasticsearch.xpack.core.ilm.SearchableSnapshotAction;
3834
import org.elasticsearch.xpack.core.ilm.SetPriorityAction;
3935
import org.elasticsearch.xpack.core.ilm.ShrinkAction;
4036
import org.elasticsearch.xpack.core.ilm.Step;
41-
import org.elasticsearch.xpack.core.ilm.StepKeyTests;
4237
import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotRequest;
4338
import org.junit.Before;
4439

@@ -163,8 +158,8 @@ public void testSearchableSnapshotForceMergesIndexToOneSegment() throws Exceptio
163158
TimeUnit.SECONDS);
164159
}
165160

166-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/pull/54433")
167-
public void testDeleteActionDeletesSearchableSnapshot() throws Exception {
161+
@SuppressWarnings("unchecked")
162+
public void testDeleteActionDeletesSearchableSnapshot() throws Exception {
168163
createSnapshotRepo(client(), snapshotRepo, randomBoolean());
169164

170165
// create policy with cold and delete phases
@@ -192,32 +187,29 @@ public void testDeleteActionDeletesSearchableSnapshot() throws Exception {
192187
// rolling over the data stream so we can apply the searchable snapshot policy to a backing index that's not the write index
193188
rolloverMaxOneDocCondition(client(), dataStream);
194189

195-
String[] snapshotName = new String[1];
196190
String backingIndexName = DataStream.getDefaultBackingIndexName(dataStream, 1L);
197191
String restoredIndexName = SearchableSnapshotAction.FULL_RESTORED_INDEX_PREFIX + backingIndexName;
198-
assertTrue(waitUntil(() -> {
199-
try {
200-
Map<String, Object> explainIndex = explainIndex(client(), backingIndexName);
201-
if (explainIndex == null) {
202-
// in case we missed the original index and it was deleted
203-
explainIndex = explainIndex(client(), restoredIndexName);
204-
}
205-
snapshotName[0] = (String) explainIndex.get("snapshot_name");
206-
return snapshotName[0] != null;
207-
} catch (IOException e) {
208-
return false;
209-
}
210-
}, 30, TimeUnit.SECONDS));
211-
assertBusy(() -> assertFalse(indexExists(restoredIndexName)));
192+
193+
// let's wait for ILM to finish
194+
assertBusy(() -> assertFalse(indexExists(backingIndexName)), 60, TimeUnit.SECONDS);
195+
assertBusy(() -> assertFalse(indexExists(restoredIndexName)), 60, TimeUnit.SECONDS);
212196

213197
assertTrue("the snapshot we generate in the cold phase should be deleted by the delete phase", waitUntil(() -> {
214-
try {
215-
Request getSnapshotsRequest = new Request("GET", "_snapshot/" + snapshotRepo + "/" + snapshotName[0]);
216-
Response getSnapshotsResponse = client().performRequest(getSnapshotsRequest);
217-
return EntityUtils.toString(getSnapshotsResponse.getEntity()).contains("snapshot_missing_exception");
218-
} catch (IOException e) {
219-
return false;
220-
}
198+
try {
199+
Request getSnapshotsRequest = new Request("GET", "_snapshot/" + snapshotRepo + "/_all");
200+
Response getSnapshotsResponse = client().performRequest(getSnapshotsRequest);
201+
202+
Map<String, Object> responseMap;
203+
try (InputStream is = getSnapshotsResponse.getEntity().getContent()) {
204+
responseMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true);
205+
}
206+
List<Object> responses = (List<Object>) responseMap.get("responses");
207+
Object snapshots = ((Map<String, Object>) responses.get(0)).get("snapshots");
208+
return ((List<Map<String, Object>>) snapshots).size() == 0;
209+
} catch (Exception e) {
210+
logger.error(e.getMessage(), e);
211+
return false;
212+
}
221213
}, 30, TimeUnit.SECONDS));
222214
}
223215

0 commit comments

Comments
 (0)