Skip to content

add rollup index check before doing rollup #79596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.elasticsearch.index.Index;
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.elasticsearch.index.mapper.NumberFieldMapper;
import org.elasticsearch.indices.InvalidIndexNameException;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
Expand Down Expand Up @@ -116,6 +117,7 @@ protected void masterOperation(
} else {
rollupIndexName = request.getRollupIndex();
}
validExistsRollupIndex(state, rollupIndexName);

String tmpIndexName = ".rolluptmp-" + rollupIndexName;

Expand Down Expand Up @@ -198,6 +200,7 @@ public ClusterState execute(ClusterState currentState) throws Exception {
);
}

@Override
public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
// index created
// 3.
Expand Down Expand Up @@ -395,4 +398,18 @@ public void onFailure(Exception deleteException) {
}
});
}

private void validExistsRollupIndex(ClusterState state, String rollupIndex) {
if (state.metadata().hasIndex(rollupIndex)) {
throw new InvalidIndexNameException(rollupIndex, "rollup index already exists");
}

if (state.metadata().hasAlias(rollupIndex)) {
throw new InvalidIndexNameException(rollupIndex, "rollup index already exists as alias");
}

if (state.metadata().dataStreams().containsKey(rollupIndex)) {
throw new InvalidIndexNameException(rollupIndex, "rollup index already exists as data stream");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.elasticsearch.ResourceAlreadyExistsException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
Expand All @@ -33,6 +34,7 @@
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.indices.InvalidIndexNameException;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder;
Expand Down Expand Up @@ -186,8 +188,40 @@ public void testCannotRollupToExistingIndex() throws Exception {
bulkIndex(sourceSupplier);
rollup(index, rollupIndex, config);
assertRollupIndex(config, index, rollupIndex);
ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> rollup(index, rollupIndex, config));
assertThat(exception.getMessage(), containsString("Unable to rollup index [" + index + "]"));
InvalidIndexNameException exception = expectThrows(InvalidIndexNameException.class, () -> rollup(index, rollupIndex, config));
assertThat(exception.getMessage(), equalTo("Invalid index name [" + rollupIndex + "], rollup index already exists"));
}

public void testCannotRollupToExistingAlias() {
RollupActionDateHistogramGroupConfig dateHistogramGroupConfig = randomRollupActionDateHistogramGroupConfig("date_1");
RollupActionConfig config = new RollupActionConfig(
new RollupActionGroupConfig(dateHistogramGroupConfig, null, new TermsGroupConfig("categorical_1")),
Collections.singletonList(new MetricConfig("numeric_1", Collections.singletonList("max")))
);
String aliasName = randomAlphaOfLength(6).toLowerCase(Locale.ROOT);
client().admin()
.indices()
.prepareCreate(randomAlphaOfLength(6).toLowerCase(Locale.ROOT))
.setSettings(Settings.builder().put("index.number_of_shards", 1).build())
.addAlias(new Alias(aliasName))
.get();

InvalidIndexNameException exception = expectThrows(InvalidIndexNameException.class, () -> rollup(index, aliasName, config));
assertThat(exception.getMessage(), equalTo("Invalid index name [" + aliasName + "], rollup index already exists as alias"));
}

public void testCannotRollupToExistingDataStream() throws Exception {
RollupActionDateHistogramGroupConfig dateHistogramGroupConfig = randomRollupActionDateHistogramGroupConfig("date_1");
RollupActionConfig config = new RollupActionConfig(
new RollupActionGroupConfig(dateHistogramGroupConfig, null, new TermsGroupConfig("categorical_1")),
Collections.singletonList(new MetricConfig("numeric_1", Collections.singletonList("max")))
);
String datsStreamName = createDataStream();
InvalidIndexNameException exception = expectThrows(InvalidIndexNameException.class, () -> rollup(index, datsStreamName, config));
assertThat(
exception.getMessage(),
equalTo("Invalid index name [" + datsStreamName + "], rollup index already exists as data stream")
);
}

public void testTemporaryIndexCannotBeCreatedAlreadyExists() {
Expand Down