|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.ilm.actions; |
| 9 | + |
| 10 | +import org.elasticsearch.client.Request; |
| 11 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 12 | +import org.elasticsearch.common.Strings; |
| 13 | +import org.elasticsearch.common.settings.Settings; |
| 14 | +import org.elasticsearch.common.unit.TimeValue; |
| 15 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 16 | +import org.elasticsearch.xpack.core.ilm.LifecycleAction; |
| 17 | +import org.elasticsearch.xpack.core.ilm.LifecyclePolicy; |
| 18 | +import org.elasticsearch.xpack.core.ilm.LifecycleSettings; |
| 19 | +import org.elasticsearch.xpack.core.ilm.Phase; |
| 20 | +import org.elasticsearch.xpack.core.ilm.PhaseCompleteStep; |
| 21 | +import org.elasticsearch.xpack.core.ilm.ReadOnlyAction; |
| 22 | +import org.elasticsearch.xpack.core.ilm.RolloverAction; |
| 23 | +import org.junit.Before; |
| 24 | + |
| 25 | +import java.util.Locale; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +import static org.elasticsearch.xpack.TimeSeriesRestDriver.createIndexWithSettings; |
| 29 | +import static org.elasticsearch.xpack.TimeSeriesRestDriver.createNewSingletonPolicy; |
| 30 | +import static org.elasticsearch.xpack.TimeSeriesRestDriver.getOnlyIndexSettings; |
| 31 | +import static org.elasticsearch.xpack.TimeSeriesRestDriver.getStepKeyForIndex; |
| 32 | +import static org.elasticsearch.xpack.TimeSeriesRestDriver.index; |
| 33 | +import static org.elasticsearch.xpack.TimeSeriesRestDriver.updatePolicy; |
| 34 | +import static org.hamcrest.Matchers.equalTo; |
| 35 | +import static org.hamcrest.Matchers.nullValue; |
| 36 | + |
| 37 | +public class ReadonlyActionIT extends ESRestTestCase { |
| 38 | + private static final String FAILED_STEP_RETRY_COUNT_FIELD = "failed_step_retry_count"; |
| 39 | + |
| 40 | + private String policy; |
| 41 | + private String index; |
| 42 | + private String alias; |
| 43 | + |
| 44 | + @Before |
| 45 | + public void refreshAbstractions() { |
| 46 | + policy = "policy-" + randomAlphaOfLength(5); |
| 47 | + index = "index-" + randomAlphaOfLength(10).toLowerCase(Locale.ROOT); |
| 48 | + alias = "alias-" + randomAlphaOfLength(5); |
| 49 | + logger.info("--> running [{}] with index [{}], alias [{}] and policy [{}]", getTestName(), index, alias, policy); |
| 50 | + } |
| 51 | + |
| 52 | + public void testReadOnly() throws Exception { |
| 53 | + createIndexWithSettings(client(), index, alias, Settings.builder() |
| 54 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) |
| 55 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); |
| 56 | + String phaseName = randomFrom("warm", "cold", "frozen"); |
| 57 | + createNewSingletonPolicy(client(), policy, phaseName, new ReadOnlyAction()); |
| 58 | + updatePolicy(client(), index, policy); |
| 59 | + assertBusy(() -> { |
| 60 | + Map<String, Object> settings = getOnlyIndexSettings(client(), index); |
| 61 | + assertThat(getStepKeyForIndex(client(), index), equalTo(PhaseCompleteStep.finalStep(phaseName).getKey())); |
| 62 | + assertThat(settings.get(IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.getKey()), equalTo("true")); |
| 63 | + assertThat(settings.get(IndexMetadata.INDEX_BLOCKS_METADATA_SETTING.getKey()), nullValue()); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + public void testReadOnlyInTheHotPhase() throws Exception { |
| 68 | + String originalIndex = index + "-000001"; |
| 69 | + |
| 70 | + // add a policy |
| 71 | + Map<String, LifecycleAction> hotActions = Map.of( |
| 72 | + RolloverAction.NAME, new RolloverAction(null, null, 1L), |
| 73 | + ReadOnlyAction.NAME, new ReadOnlyAction()); |
| 74 | + Map<String, Phase> phases = Map.of( |
| 75 | + "hot", new Phase("hot", TimeValue.ZERO, hotActions)); |
| 76 | + LifecyclePolicy lifecyclePolicy = new LifecyclePolicy(policy, phases); |
| 77 | + Request createPolicyRequest = new Request("PUT", "_ilm/policy/" + policy); |
| 78 | + createPolicyRequest.setJsonEntity("{ \"policy\":" + Strings.toString(lifecyclePolicy) + "}"); |
| 79 | + client().performRequest(createPolicyRequest); |
| 80 | + |
| 81 | + // then create the index and index a document to trigger rollover |
| 82 | + createIndexWithSettings(client(), originalIndex, alias, Settings.builder() |
| 83 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) |
| 84 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) |
| 85 | + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) |
| 86 | + .put(LifecycleSettings.LIFECYCLE_NAME, policy)); |
| 87 | + index(client(), originalIndex, "_id", "foo", "bar"); |
| 88 | + |
| 89 | + assertBusy(() -> { |
| 90 | + Map<String, Object> settings = getOnlyIndexSettings(client(), originalIndex); |
| 91 | + assertThat(getStepKeyForIndex(client(), originalIndex), equalTo(PhaseCompleteStep.finalStep("hot").getKey())); |
| 92 | + assertThat(settings.get(IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.getKey()), equalTo("true")); |
| 93 | + }); |
| 94 | + } |
| 95 | +} |
0 commit comments