Skip to content

Commit 9ec86c7

Browse files
authored
Delete data stream in ILM delete action if last index in data stream (#69637)
If an index happens to be the last index in the data stream (such as a user testing ILM out) when it comes to the `delete` action, we attempt to delete it, however, a data stream cannot be empty, so it throws an exception and prevents the deletion. This commit changes the behavior of the `delete` action such that deleting the very last index in a data stream deletes the entire data stream.
1 parent 529c622 commit 9ec86c7

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.cluster.ClusterState;
1515
import org.elasticsearch.cluster.metadata.IndexAbstraction;
1616
import org.elasticsearch.cluster.metadata.IndexMetadata;
17+
import org.elasticsearch.xpack.core.action.DeleteDataStreamAction;
1718

1819
import java.util.Locale;
1920

@@ -38,7 +39,14 @@ public void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState cu
3839

3940
if (dataStream != null) {
4041
assert dataStream.getWriteIndex() != null : dataStream.getName() + " has no write index";
41-
if (dataStream.getWriteIndex().getIndex().getName().equals(indexName)) {
42+
if (dataStream.getIndices().size() == 1 && dataStream.getIndices().get(0).equals(indexMetadata)) {
43+
// This is the last index in the data stream, the entire stream
44+
// needs to be deleted, because we can't have an empty data stream
45+
DeleteDataStreamAction.Request deleteReq = new DeleteDataStreamAction.Request(new String[]{dataStream.getName()});
46+
getClient().execute(DeleteDataStreamAction.INSTANCE, deleteReq,
47+
ActionListener.wrap(response -> listener.onResponse(true), listener::onFailure));
48+
return;
49+
} else if (dataStream.getWriteIndex().getIndex().getName().equals(indexName)) {
4250
String errorMessage = String.format(Locale.ROOT, "index [%s] is the write index for data stream [%s]. " +
4351
"stopping execution of lifecycle [%s] as a data stream's write index cannot be deleted. manually rolling over the" +
4452
" index will resume the execution of the policy as the index will not be the data stream's write index anymore",

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.elasticsearch.index.IndexSettings;
2020
import org.elasticsearch.test.rest.ESRestTestCase;
2121
import org.elasticsearch.xpack.core.ilm.CheckNotDataStreamWriteIndexStep;
22+
import org.elasticsearch.xpack.core.ilm.DeleteAction;
2223
import org.elasticsearch.xpack.core.ilm.ForceMergeAction;
2324
import org.elasticsearch.xpack.core.ilm.FreezeAction;
2425
import org.elasticsearch.xpack.core.ilm.LifecycleSettings;
@@ -46,6 +47,7 @@
4647
import static org.elasticsearch.xpack.TimeSeriesRestDriver.getStepKeyForIndex;
4748
import static org.elasticsearch.xpack.TimeSeriesRestDriver.indexDocument;
4849
import static org.elasticsearch.xpack.TimeSeriesRestDriver.rolloverMaxOneDocCondition;
50+
import static org.hamcrest.Matchers.containsString;
4951
import static org.hamcrest.Matchers.equalTo;
5052
import static org.hamcrest.Matchers.is;
5153

@@ -238,6 +240,18 @@ public void testGetDataStreamReturnsILMPolicy() throws Exception {
238240
assertThat(logsDataStream.get("ilm_policy"), is(policyName));
239241
}
240242

243+
public void testDeleteOnlyIndexInDataStreamDeletesDataStream() throws Exception {
244+
createNewSingletonPolicy(client(), policyName, "delete", new DeleteAction(false));
245+
createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName));
246+
indexDocument(client(), dataStream, true);
247+
248+
assertBusy(() -> {
249+
Request r = new Request("GET", "/_data_stream/" + dataStream);
250+
Exception e = expectThrows(Exception.class, () -> client().performRequest(r));
251+
assertThat(e.getMessage(), containsString("no such index [" + dataStream + "]"));
252+
});
253+
}
254+
241255
private static Template getTemplate(String policyName) throws IOException {
242256
return new Template(getLifecycleSettings(policyName), null, null);
243257
}

0 commit comments

Comments
 (0)