|
| 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 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.ilm; |
| 8 | + |
| 9 | +import org.elasticsearch.action.admin.indices.alias.Alias; |
| 10 | +import org.elasticsearch.action.admin.indices.template.put.PutComposableIndexTemplateAction; |
| 11 | +import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; |
| 12 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 13 | +import org.elasticsearch.cluster.metadata.Template; |
| 14 | +import org.elasticsearch.common.Strings; |
| 15 | +import org.elasticsearch.common.settings.Settings; |
| 16 | +import org.elasticsearch.common.unit.TimeValue; |
| 17 | +import org.elasticsearch.index.mapper.MapperService; |
| 18 | +import org.elasticsearch.plugins.Plugin; |
| 19 | +import org.elasticsearch.test.ESIntegTestCase; |
| 20 | +import org.elasticsearch.xpack.cluster.routing.allocation.DataTierAllocationDecider; |
| 21 | +import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; |
| 22 | +import org.elasticsearch.xpack.core.XPackSettings; |
| 23 | +import org.elasticsearch.xpack.core.ilm.ExplainLifecycleRequest; |
| 24 | +import org.elasticsearch.xpack.core.ilm.ExplainLifecycleResponse; |
| 25 | +import org.elasticsearch.xpack.core.ilm.IndexLifecycleExplainResponse; |
| 26 | +import org.elasticsearch.xpack.core.ilm.LifecyclePolicy; |
| 27 | +import org.elasticsearch.xpack.core.ilm.LifecycleSettings; |
| 28 | +import org.elasticsearch.xpack.core.ilm.Phase; |
| 29 | +import org.elasticsearch.xpack.core.ilm.RolloverAction; |
| 30 | +import org.elasticsearch.xpack.core.ilm.ShrinkAction; |
| 31 | +import org.elasticsearch.xpack.core.ilm.action.ExplainLifecycleAction; |
| 32 | +import org.elasticsearch.xpack.core.ilm.action.PutLifecycleAction; |
| 33 | + |
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.Collection; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.HashMap; |
| 38 | +import java.util.Map; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | + |
| 41 | +import static org.hamcrest.Matchers.equalTo; |
| 42 | + |
| 43 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0) |
| 44 | +public class ILMMultiNodeIT extends ESIntegTestCase { |
| 45 | + private static final String index = "myindex"; |
| 46 | + |
| 47 | + @Override |
| 48 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 49 | + return Arrays.asList(LocalStateCompositeXPackPlugin.class, IndexLifecycle.class); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected Collection<Class<? extends Plugin>> transportClientPlugins() { |
| 54 | + return nodePlugins(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected Settings nodeSettings(int nodeOrdinal) { |
| 59 | + Settings.Builder settings = Settings.builder().put(super.nodeSettings(nodeOrdinal)); |
| 60 | + settings.put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), false); |
| 61 | + settings.put(XPackSettings.SECURITY_ENABLED.getKey(), false); |
| 62 | + settings.put(XPackSettings.WATCHER_ENABLED.getKey(), false); |
| 63 | + settings.put(XPackSettings.GRAPH_ENABLED.getKey(), false); |
| 64 | + settings.put(LifecycleSettings.LIFECYCLE_POLL_INTERVAL, "1s"); |
| 65 | + |
| 66 | + // This is necessary to prevent ILM and SLM installing a lifecycle policy, these tests assume a blank slate |
| 67 | + settings.put(LifecycleSettings.LIFECYCLE_HISTORY_INDEX_ENABLED, false); |
| 68 | + settings.put(LifecycleSettings.SLM_HISTORY_INDEX_ENABLED_SETTING.getKey(), false); |
| 69 | + return settings.build(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + protected boolean ignoreExternalCluster() { |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected Settings transportClientSettings() { |
| 79 | + Settings.Builder settings = Settings.builder().put(super.transportClientSettings()); |
| 80 | + settings.put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), false); |
| 81 | + settings.put(XPackSettings.SECURITY_ENABLED.getKey(), false); |
| 82 | + settings.put(XPackSettings.WATCHER_ENABLED.getKey(), false); |
| 83 | + settings.put(XPackSettings.GRAPH_ENABLED.getKey(), false); |
| 84 | + return settings.build(); |
| 85 | + } |
| 86 | + |
| 87 | + public void testShrinkOnTiers() throws Exception { |
| 88 | + startHotOnlyNode(); |
| 89 | + startWarmOnlyNode(); |
| 90 | + ensureGreen(); |
| 91 | + |
| 92 | + RolloverAction rolloverAction = new RolloverAction(null, null, 1L); |
| 93 | + Phase hotPhase = new Phase("hot", TimeValue.ZERO, Collections.singletonMap(rolloverAction.getWriteableName(), rolloverAction)); |
| 94 | + ShrinkAction shrinkAction = new ShrinkAction(1); |
| 95 | + Phase warmPhase = new Phase("warm", TimeValue.ZERO, Collections.singletonMap(shrinkAction.getWriteableName(), shrinkAction)); |
| 96 | + Map<String, Phase> phases = new HashMap<>(); |
| 97 | + phases.put(hotPhase.getName(), hotPhase); |
| 98 | + phases.put(warmPhase.getName(), warmPhase); |
| 99 | + LifecyclePolicy lifecyclePolicy = new LifecyclePolicy("shrink-policy", phases); |
| 100 | + client().execute(PutLifecycleAction.INSTANCE, new PutLifecycleAction.Request(lifecyclePolicy)).get(); |
| 101 | + |
| 102 | + Template t = new Template(Settings.builder() |
| 103 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2) |
| 104 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) |
| 105 | + .put(LifecycleSettings.LIFECYCLE_NAME, "shrink-policy") |
| 106 | + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, "shrink-alias") |
| 107 | + .put(DataTierAllocationDecider.INDEX_ROUTING_PREFER, "data_hot") |
| 108 | + .build(), null, null); |
| 109 | + |
| 110 | + ComposableIndexTemplate template = new ComposableIndexTemplate( |
| 111 | + Collections.singletonList(index + "*"), |
| 112 | + t, |
| 113 | + null, |
| 114 | + null, |
| 115 | + null, |
| 116 | + null |
| 117 | + ); |
| 118 | + client().execute( |
| 119 | + PutComposableIndexTemplateAction.INSTANCE, |
| 120 | + new PutComposableIndexTemplateAction.Request("template").indexTemplate(template) |
| 121 | + ).actionGet(); |
| 122 | + client().admin().indices().prepareCreate(index + "-000001") |
| 123 | + .addAlias(new Alias("shrink-alias").writeIndex(true)).get(); |
| 124 | + client().prepareIndex(index + "-000001", MapperService.SINGLE_MAPPING_NAME) |
| 125 | + .setCreate(true).setId("1").setSource("@timestamp", "2020-09-09").get(); |
| 126 | + |
| 127 | + assertBusy(() -> { |
| 128 | + String name = "shrink-" + index + "-000001"; |
| 129 | + ExplainLifecycleResponse explain = |
| 130 | + client().execute(ExplainLifecycleAction.INSTANCE, new ExplainLifecycleRequest().indices("*")).get(); |
| 131 | + logger.info("--> explain: {}", Strings.toString(explain)); |
| 132 | + |
| 133 | + IndexLifecycleExplainResponse indexResp = explain.getIndexResponses().get(name); |
| 134 | + assertNotNull(indexResp); |
| 135 | + assertThat(indexResp.getPhase(), equalTo("warm")); |
| 136 | + assertThat(indexResp.getStep(), equalTo("complete")); |
| 137 | + }, 60, TimeUnit.SECONDS); |
| 138 | + } |
| 139 | + |
| 140 | + public void startHotOnlyNode() { |
| 141 | + Settings nodeSettings = Settings.builder().putList("node.roles", Arrays.asList("master", "data_hot", "ingest")).build(); |
| 142 | + internalCluster().startNode(nodeSettings); |
| 143 | + } |
| 144 | + |
| 145 | + public void startWarmOnlyNode() { |
| 146 | + Settings nodeSettings = Settings.builder().putList("node.roles", Arrays.asList("master", "data_warm", "ingest")).build(); |
| 147 | + internalCluster().startNode(nodeSettings); |
| 148 | + } |
| 149 | +} |
0 commit comments