From b2939ebe5a3a81cdfb2a29cc23b8c8b65d56fc71 Mon Sep 17 00:00:00 2001 From: weizijun Date: Thu, 21 Oct 2021 11:32:50 +0800 Subject: [PATCH] add validExistsRollupIndex check --- .../rollup/v2/TransportRollupAction.java | 17 +++++++++ .../v2/RollupActionSingleNodeTests.java | 38 ++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/v2/TransportRollupAction.java b/x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/v2/TransportRollupAction.java index f5e0b7fb5ace8..e24956f04455b 100644 --- a/x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/v2/TransportRollupAction.java +++ b/x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/v2/TransportRollupAction.java @@ -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; @@ -116,6 +117,7 @@ protected void masterOperation( } else { rollupIndexName = request.getRollupIndex(); } + validExistsRollupIndex(state, rollupIndexName); String tmpIndexName = ".rolluptmp-" + rollupIndexName; @@ -198,6 +200,7 @@ public ClusterState execute(ClusterState currentState) throws Exception { ); } + @Override public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { // index created // 3. @@ -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"); + } + } } diff --git a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/v2/RollupActionSingleNodeTests.java b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/v2/RollupActionSingleNodeTests.java index d86d7e0cf204e..b4d4a2580f8a1 100644 --- a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/v2/RollupActionSingleNodeTests.java +++ b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/v2/RollupActionSingleNodeTests.java @@ -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; @@ -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; @@ -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() {