|
6 | 6 |
|
7 | 7 | package org.elasticsearch.xpack.ilm;
|
8 | 8 |
|
| 9 | +import org.elasticsearch.action.admin.cluster.allocation.ClusterAllocationExplainRequest; |
| 10 | +import org.elasticsearch.action.admin.cluster.allocation.ClusterAllocationExplainResponse; |
9 | 11 | import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
|
| 12 | +import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; |
| 13 | +import org.elasticsearch.cluster.routing.ShardRoutingState; |
10 | 14 | import org.elasticsearch.common.settings.Settings;
|
11 | 15 | import org.elasticsearch.common.unit.TimeValue;
|
12 | 16 | import org.elasticsearch.plugins.Plugin;
|
13 | 17 | import org.elasticsearch.test.ESIntegTestCase;
|
| 18 | +import org.elasticsearch.xpack.cluster.routing.allocation.DataTierAllocationDecider; |
14 | 19 | import org.elasticsearch.xpack.core.DataTier;
|
15 | 20 | import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin;
|
16 | 21 | import org.elasticsearch.xpack.core.XPackSettings;
|
|
30 | 35 | import java.util.Collections;
|
31 | 36 | import java.util.Locale;
|
32 | 37 | import java.util.Map;
|
| 38 | +import java.util.concurrent.TimeUnit; |
33 | 39 |
|
34 | 40 | import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS;
|
35 | 41 | import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS;
|
@@ -138,4 +144,89 @@ public void testIndexDataTierMigration() throws Exception {
|
138 | 144 | assertThat(indexLifecycleExplainResponse.getStep(), is("complete"));
|
139 | 145 | });
|
140 | 146 | }
|
| 147 | + |
| 148 | + public void testUserOptsOutOfTierMigration() throws Exception { |
| 149 | + internalCluster().startMasterOnlyNodes(1, Settings.EMPTY); |
| 150 | + logger.info("starting hot data node"); |
| 151 | + internalCluster().startNode(hotNode(Settings.EMPTY)); |
| 152 | + |
| 153 | + Phase hotPhase = new Phase("hot", TimeValue.ZERO, Collections.emptyMap()); |
| 154 | + Phase warmPhase = new Phase("warm", TimeValue.ZERO, Collections.emptyMap()); |
| 155 | + Phase coldPhase = new Phase("cold", TimeValue.ZERO, Collections.emptyMap()); |
| 156 | + LifecyclePolicy lifecyclePolicy = new LifecyclePolicy(policy, Map.of("hot", hotPhase, "warm", warmPhase, "cold", coldPhase)); |
| 157 | + PutLifecycleAction.Request putLifecycleRequest = new PutLifecycleAction.Request(lifecyclePolicy); |
| 158 | + PutLifecycleAction.Response putLifecycleResponse = client().execute(PutLifecycleAction.INSTANCE, putLifecycleRequest).get(); |
| 159 | + assertAcked(putLifecycleResponse); |
| 160 | + |
| 161 | + Settings settings = Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, 1) |
| 162 | + .put(SETTING_NUMBER_OF_REPLICAS, 1).put(LifecycleSettings.LIFECYCLE_NAME, policy).build(); |
| 163 | + CreateIndexResponse res = client().admin().indices().prepareCreate(managedIndex).setSettings(settings).get(); |
| 164 | + assertTrue(res.isAcknowledged()); |
| 165 | + |
| 166 | + assertBusy(() -> { |
| 167 | + ExplainLifecycleRequest explainRequest = new ExplainLifecycleRequest().indices(managedIndex); |
| 168 | + ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, |
| 169 | + explainRequest).get(); |
| 170 | + |
| 171 | + IndexLifecycleExplainResponse indexLifecycleExplainResponse = explainResponse.getIndexResponses().get(managedIndex); |
| 172 | + assertThat(indexLifecycleExplainResponse.getPhase(), is("warm")); |
| 173 | + assertThat(indexLifecycleExplainResponse.getStep(), is(DataTierMigrationRoutedStep.NAME)); |
| 174 | + }); |
| 175 | + |
| 176 | + Settings removeTierRoutingSetting = Settings.builder().putNull(DataTierAllocationDecider.INDEX_ROUTING_PREFER).build(); |
| 177 | + UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(managedIndex).settings(removeTierRoutingSetting); |
| 178 | + assertAcked(client().admin().indices().updateSettings(updateSettingsRequest).actionGet()); |
| 179 | + |
| 180 | + assertBusy(() -> { |
| 181 | + ExplainLifecycleRequest explainRequest = new ExplainLifecycleRequest().indices(managedIndex); |
| 182 | + ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, |
| 183 | + explainRequest).get(); |
| 184 | + |
| 185 | + IndexLifecycleExplainResponse indexLifecycleExplainResponse = explainResponse.getIndexResponses().get(managedIndex); |
| 186 | + assertThat(indexLifecycleExplainResponse.getPhase(), is("warm")); |
| 187 | + assertThat(indexLifecycleExplainResponse.getStep(), is(DataTierMigrationRoutedStep.NAME)); |
| 188 | + assertReplicaIsUnassigned(); |
| 189 | + }, 30, TimeUnit.SECONDS); |
| 190 | + |
| 191 | + internalCluster().startNode(coldNode(Settings.EMPTY)); |
| 192 | + |
| 193 | + // the index should successfully allocate |
| 194 | + ensureGreen(managedIndex); |
| 195 | + |
| 196 | + // the index is successfully allocated but the migrate action from the cold phase re-configured the tier migration setting to the |
| 197 | + // cold tier so ILM is stuck in `check-migration` in the cold phase this time |
| 198 | + // we have 2 options to resume the ILM execution: |
| 199 | + // 1. start another cold node so both the primary and replica can relocate to the cold nodes |
| 200 | + // 2. remove the tier routing setting from the index again (we're doing this below) |
| 201 | + assertBusy(() -> { |
| 202 | + ExplainLifecycleRequest explainRequest = new ExplainLifecycleRequest().indices(managedIndex); |
| 203 | + ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, |
| 204 | + explainRequest).get(); |
| 205 | + |
| 206 | + IndexLifecycleExplainResponse indexLifecycleExplainResponse = explainResponse.getIndexResponses().get(managedIndex); |
| 207 | + assertThat(indexLifecycleExplainResponse.getPhase(), is("cold")); |
| 208 | + assertThat(indexLifecycleExplainResponse.getStep(), is(DataTierMigrationRoutedStep.NAME)); |
| 209 | + }); |
| 210 | + |
| 211 | + // remove the tier routing setting again |
| 212 | + assertAcked(client().admin().indices().updateSettings(updateSettingsRequest).actionGet()); |
| 213 | + |
| 214 | + // wait for lifecycle to complete in the cold phase |
| 215 | + assertBusy(() -> { |
| 216 | + ExplainLifecycleRequest explainRequest = new ExplainLifecycleRequest().indices(managedIndex); |
| 217 | + ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, |
| 218 | + explainRequest).get(); |
| 219 | + |
| 220 | + IndexLifecycleExplainResponse indexLifecycleExplainResponse = explainResponse.getIndexResponses().get(managedIndex); |
| 221 | + assertThat(indexLifecycleExplainResponse.getPhase(), is("cold")); |
| 222 | + assertThat(indexLifecycleExplainResponse.getStep(), is("complete")); |
| 223 | + }, 30, TimeUnit.SECONDS); |
| 224 | + } |
| 225 | + |
| 226 | + private void assertReplicaIsUnassigned() { |
| 227 | + ClusterAllocationExplainRequest explainReplicaShard = |
| 228 | + new ClusterAllocationExplainRequest().setIndex(managedIndex).setPrimary(false).setShard(0); |
| 229 | + ClusterAllocationExplainResponse response = client().admin().cluster().allocationExplain(explainReplicaShard).actionGet(); |
| 230 | + assertThat(response.getExplanation().getShardState(), is(ShardRoutingState.UNASSIGNED)); |
| 231 | + } |
141 | 232 | }
|
0 commit comments