From c8bb627619e340c3e98319644f44c173efbe4924 Mon Sep 17 00:00:00 2001 From: Andrei Dan Date: Tue, 23 Mar 2021 09:27:05 +0000 Subject: [PATCH] ILM: Rollup action uses GenerateUniqueIndexNameStep (#70626) ILM: Rollup action uses GenerateUniqueIndexNameStep --- .../core/ilm/GenerateRollupIndexNameStep.java | 80 ------------------- .../xpack/core/ilm/RollupILMAction.java | 7 +- .../ilm/GenerateRollupIndexNameStepTests.java | 76 ------------------ .../xpack/core/ilm/RollupILMActionTests.java | 5 +- .../xpack/ilm/actions/RollupActionIT.java | 2 +- 5 files changed, 9 insertions(+), 161 deletions(-) delete mode 100644 x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateRollupIndexNameStep.java delete mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/GenerateRollupIndexNameStepTests.java diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateRollupIndexNameStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateRollupIndexNameStep.java deleted file mode 100644 index 136ac8ca654a0..0000000000000 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateRollupIndexNameStep.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ -package org.elasticsearch.xpack.core.ilm; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.elasticsearch.cluster.ClusterState; -import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.common.UUIDs; -import org.elasticsearch.index.Index; - -import java.util.Locale; -import java.util.Objects; - -import static org.elasticsearch.xpack.core.ilm.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; -import static org.elasticsearch.xpack.core.ilm.LifecycleExecutionState.fromIndexMetadata; - -/** - * Generates a new rollup index name for the current managed index. - *

- * The generated rollup index name will be in the format {rollup-indexName-randomUUID} - * eg.: rollup-myindex-cmuce-qfvmn_dstqw-ivmjc1etsa - */ -public class GenerateRollupIndexNameStep extends ClusterStateActionStep { - - public static final String NAME = "generate-rollup-name"; - - private static final Logger logger = LogManager.getLogger(GenerateRollupIndexNameStep.class); - - public GenerateRollupIndexNameStep(StepKey key, StepKey nextStepKey) { - super(key, nextStepKey); - } - - @Override - public ClusterState performAction(Index index, ClusterState clusterState) { - IndexMetadata indexMetaData = clusterState.metadata().index(index); - if (indexMetaData == null) { - // Index must have been since deleted, ignore it - logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().getAction(), index.getName()); - return clusterState; - } - ClusterState.Builder newClusterStateBuilder = ClusterState.builder(clusterState); - LifecycleExecutionState lifecycleState = fromIndexMetadata(indexMetaData); - assert lifecycleState.getRollupIndexName() == null : "index " + index.getName() + - " should have a rollup index name generated by the ilm policy but has " + lifecycleState.getRollupIndexName(); - LifecycleExecutionState.Builder newCustomData = LifecycleExecutionState.builder(lifecycleState); - String rollupIndexName = generateRollupIndexName(index.getName()); - newCustomData.setRollupIndexName(rollupIndexName); - IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexMetaData); - indexMetadataBuilder.putCustom(ILM_CUSTOM_METADATA_KEY, newCustomData.build().asMap()); - newClusterStateBuilder.metadata(Metadata.builder(clusterState.getMetadata()).put(indexMetadataBuilder)); - return newClusterStateBuilder.build(); - } - - // TODO(talevy): find alternative to lowercasing UUID? kind of defeats the expectation of the UUID here. index names must lowercase - public static String generateRollupIndexName(String indexName) { - return "rollup-" + indexName + "-" + UUIDs.randomBase64UUID().toLowerCase(Locale.ROOT); - } - - @Override - public int hashCode() { - return Objects.hash(super.hashCode()); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - return super.equals(obj); - } -} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RollupILMAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RollupILMAction.java index 54aa625e21e6a..209581887742c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RollupILMAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RollupILMAction.java @@ -35,6 +35,8 @@ public class RollupILMAction implements LifecycleAction { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, a -> new RollupILMAction((RollupActionConfig) a[0], (String) a[1])); + public static final String ROLLUP_INDEX_PREFIX = "rollup-"; + public static final String GENERATE_ROLLUP_STEP_NAME = "generate-rollup-name"; private final RollupActionConfig config; private final String rollupPolicy; @@ -97,12 +99,13 @@ public boolean isSafeAction() { public List toSteps(Client client, String phase, StepKey nextStepKey) { StepKey checkNotWriteIndex = new StepKey(phase, NAME, CheckNotDataStreamWriteIndexStep.NAME); StepKey readOnlyKey = new StepKey(phase, NAME, ReadOnlyStep.NAME); - StepKey generateRollupIndexNameKey = new StepKey(phase, NAME, GenerateRollupIndexNameStep.NAME); + StepKey generateRollupIndexNameKey = new StepKey(phase, NAME, GENERATE_ROLLUP_STEP_NAME); StepKey rollupKey = new StepKey(phase, NAME, NAME); CheckNotDataStreamWriteIndexStep checkNotWriteIndexStep = new CheckNotDataStreamWriteIndexStep(checkNotWriteIndex, readOnlyKey); ReadOnlyStep readOnlyStep = new ReadOnlyStep(readOnlyKey, generateRollupIndexNameKey, client); - GenerateRollupIndexNameStep generateRollupIndexNameStep = new GenerateRollupIndexNameStep(generateRollupIndexNameKey, rollupKey); + GenerateUniqueIndexNameStep generateRollupIndexNameStep = new GenerateUniqueIndexNameStep(generateRollupIndexNameKey, rollupKey, + ROLLUP_INDEX_PREFIX, (rollupIndexName, lifecycleStateBuilder) -> lifecycleStateBuilder.setRollupIndexName(rollupIndexName)); if (rollupPolicy == null) { Step rollupStep = new RollupStep(rollupKey, nextStepKey, client, config); return List.of(checkNotWriteIndexStep, readOnlyStep, generateRollupIndexNameStep, rollupStep); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/GenerateRollupIndexNameStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/GenerateRollupIndexNameStepTests.java deleted file mode 100644 index 23d372beb37be..0000000000000 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/GenerateRollupIndexNameStepTests.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ -package org.elasticsearch.xpack.core.ilm; - -import org.elasticsearch.Version; -import org.elasticsearch.cluster.ClusterState; -import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.cluster.metadata.Metadata; - -import java.util.Locale; - -import static org.elasticsearch.xpack.core.ilm.AbstractStepMasterTimeoutTestCase.emptyClusterState; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.Matchers.startsWith; - -public class GenerateRollupIndexNameStepTests extends AbstractStepTestCase { - - @Override - protected GenerateRollupIndexNameStep createRandomInstance() { - return new GenerateRollupIndexNameStep(randomStepKey(), randomStepKey()); - } - - @Override - protected GenerateRollupIndexNameStep mutateInstance(GenerateRollupIndexNameStep instance) { - Step.StepKey key = instance.getKey(); - Step.StepKey nextKey = instance.getNextStepKey(); - - switch (between(0, 1)) { - case 0: - key = new Step.StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 1: - nextKey = new Step.StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - default: - throw new AssertionError("Illegal randomisation branch"); - } - return new GenerateRollupIndexNameStep(key, nextKey); - } - - @Override - protected GenerateRollupIndexNameStep copyInstance(GenerateRollupIndexNameStep instance) { - return new GenerateRollupIndexNameStep(instance.getKey(), instance.getNextStepKey()); - } - - public void testPerformAction() { - String indexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT); - String policyName = "test-ilm-policy"; - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); - - final IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, false).build()).build(); - - GenerateRollupIndexNameStep generateRollupIndexNameStep = createRandomInstance(); - ClusterState newClusterState = generateRollupIndexNameStep.performAction(indexMetadata.getIndex(), clusterState); - - LifecycleExecutionState executionState = LifecycleExecutionState.fromIndexMetadata(newClusterState.metadata().index(indexName)); - assertThat("the " + GenerateRollupIndexNameStep.NAME + " step must generate a rollup index name", - executionState.getRollupIndexName(), notNullValue()); - assertThat(executionState.getRollupIndexName(), containsString("rollup-" + indexName + "-")); - } - - public void testGenerateRollupIndexName() { - String indexName = randomAlphaOfLength(5); - String generated = GenerateRollupIndexNameStep.generateRollupIndexName(indexName); - assertThat(generated, startsWith("rollup-" + indexName + "-")); - } -} diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RollupILMActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RollupILMActionTests.java index 9de65a07b4ce0..020269c64d0b1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RollupILMActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RollupILMActionTests.java @@ -18,6 +18,7 @@ import java.util.Collections; import java.util.List; +import static org.elasticsearch.xpack.core.ilm.RollupILMAction.GENERATE_ROLLUP_STEP_NAME; import static org.hamcrest.Matchers.equalTo; public class RollupILMActionTests extends AbstractActionTestCase { @@ -59,8 +60,8 @@ public void testToSteps() { assertThat(steps.get(0).getKey().getName(), equalTo(CheckNotDataStreamWriteIndexStep.NAME)); assertThat(steps.get(0).getNextStepKey().getName(), equalTo(ReadOnlyStep.NAME)); assertThat(steps.get(1).getKey().getName(), equalTo(ReadOnlyStep.NAME)); - assertThat(steps.get(1).getNextStepKey().getName(), equalTo(GenerateRollupIndexNameStep.NAME)); - assertThat(steps.get(2).getKey().getName(), equalTo(GenerateRollupIndexNameStep.NAME)); + assertThat(steps.get(1).getNextStepKey().getName(), equalTo(GENERATE_ROLLUP_STEP_NAME)); + assertThat(steps.get(2).getKey().getName(), equalTo(GENERATE_ROLLUP_STEP_NAME)); assertThat(steps.get(2).getNextStepKey().getName(), equalTo(RollupStep.NAME)); assertThat(steps.get(3).getKey().getName(), equalTo(RollupStep.NAME)); assertThat(steps.get(3).getNextStepKey(), equalTo(nextStepKey)); diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RollupActionIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RollupActionIT.java index a707e739504d5..80d316a90ccc5 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RollupActionIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RollupActionIT.java @@ -92,7 +92,7 @@ public void testRollupIndexAndSetNewRollupPolicy() throws Exception { * @throws IOException if request fails */ private String getRollupIndexName(String index) throws IOException { - Response response = client().performRequest(new Request("GET", "/rollup-" + index + "-*")); + Response response = client().performRequest(new Request("GET", "/rollup-*-" + index)); Map asMap = responseAsMap(response); if (asMap.size() == 1) { return (String) asMap.keySet().toArray()[0];