|
37 | 37 | import org.elasticsearch.xpack.core.indexlifecycle.OperationMode;
|
38 | 38 | import org.elasticsearch.xpack.core.indexlifecycle.Phase;
|
39 | 39 | import org.elasticsearch.xpack.core.indexlifecycle.ShrinkAction;
|
| 40 | +import org.elasticsearch.xpack.core.indexlifecycle.ShrinkStep; |
40 | 41 | import org.elasticsearch.xpack.core.indexlifecycle.Step;
|
41 | 42 | import org.elasticsearch.xpack.core.scheduler.SchedulerEngine;
|
42 | 43 | import org.junit.After;
|
|
47 | 48 | import java.time.Instant;
|
48 | 49 | import java.time.ZoneId;
|
49 | 50 | import java.util.Collections;
|
| 51 | +import java.util.List; |
50 | 52 | import java.util.SortedMap;
|
51 | 53 | import java.util.TreeMap;
|
52 | 54 | import java.util.concurrent.ExecutorService;
|
| 55 | +import java.util.stream.Collectors; |
53 | 56 |
|
54 | 57 | import static org.elasticsearch.node.Node.NODE_MASTER_SETTING;
|
55 | 58 | import static org.elasticsearch.xpack.core.indexlifecycle.AbstractStepTestCase.randomStepKey;
|
|
58 | 61 | import static org.hamcrest.Matchers.equalTo;
|
59 | 62 | import static org.mockito.Matchers.any;
|
60 | 63 | import static org.mockito.Matchers.anyString;
|
| 64 | +import static org.mockito.Matchers.eq; |
61 | 65 | import static org.mockito.Mockito.doAnswer;
|
62 | 66 | import static org.mockito.Mockito.mock;
|
63 | 67 | import static org.mockito.Mockito.when;
|
@@ -148,7 +152,7 @@ public void testStoppedModeSkip() {
|
148 | 152 | }
|
149 | 153 |
|
150 | 154 | public void testRequestedStopOnShrink() {
|
151 |
| - Step.StepKey mockShrinkStep = new Step.StepKey(randomAlphaOfLength(4), ShrinkAction.NAME, randomAlphaOfLength(5)); |
| 155 | + Step.StepKey mockShrinkStep = new Step.StepKey(randomAlphaOfLength(4), ShrinkAction.NAME, ShrinkStep.NAME); |
152 | 156 | String policyName = randomAlphaOfLengthBetween(1, 20);
|
153 | 157 | IndexLifecycleRunnerTests.MockClusterStateActionStep mockStep =
|
154 | 158 | new IndexLifecycleRunnerTests.MockClusterStateActionStep(mockShrinkStep, randomStepKey());
|
@@ -180,14 +184,64 @@ public void testRequestedStopOnShrink() {
|
180 | 184 | .build();
|
181 | 185 |
|
182 | 186 | ClusterChangedEvent event = new ClusterChangedEvent("_source", currentState, ClusterState.EMPTY_STATE);
|
183 |
| - SetOnce<Boolean> executedShrink = new SetOnce<>(); |
| 187 | + SetOnce<Boolean> changedOperationMode = new SetOnce<>(); |
184 | 188 | doAnswer(invocationOnMock -> {
|
185 |
| - executedShrink.set(true); |
| 189 | + changedOperationMode.set(true); |
186 | 190 | return null;
|
187 |
| - }).when(clusterService).submitStateUpdateTask(anyString(), any(ExecuteStepsUpdateTask.class)); |
| 191 | + }).when(clusterService).submitStateUpdateTask(eq("ilm_operation_mode_update"), any(OperationModeUpdateTask.class)); |
| 192 | + indexLifecycleService.applyClusterState(event); |
| 193 | + indexLifecycleService.triggerPolicies(currentState, true); |
| 194 | + assertNull(changedOperationMode.get()); |
| 195 | + } |
| 196 | + |
| 197 | + public void testRequestedStopInShrinkActionButNotShrinkStep() { |
| 198 | + // Create a list of all the shrink action steps that can be stopped during (basically all of them minus the actual shrink) |
| 199 | + ShrinkAction action = new ShrinkAction(1); |
| 200 | + List<String> stoppableSteps = action.toSteps(mock(Client.class), "warm", randomStepKey()).stream() |
| 201 | + .map(sk -> sk.getKey().getName()) |
| 202 | + .filter(name -> name.equals(ShrinkStep.NAME) == false) |
| 203 | + .collect(Collectors.toList()); |
| 204 | + |
| 205 | + Step.StepKey mockShrinkStep = new Step.StepKey(randomAlphaOfLength(4), ShrinkAction.NAME, randomFrom(stoppableSteps)); |
| 206 | + String policyName = randomAlphaOfLengthBetween(1, 20); |
| 207 | + IndexLifecycleRunnerTests.MockClusterStateActionStep mockStep = |
| 208 | + new IndexLifecycleRunnerTests.MockClusterStateActionStep(mockShrinkStep, randomStepKey()); |
| 209 | + MockAction mockAction = new MockAction(Collections.singletonList(mockStep)); |
| 210 | + Phase phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", mockAction)); |
| 211 | + LifecyclePolicy policy = newTestLifecyclePolicy(policyName, Collections.singletonMap(phase.getName(), phase)); |
| 212 | + SortedMap<String, LifecyclePolicyMetadata> policyMap = new TreeMap<>(); |
| 213 | + policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap(), |
| 214 | + randomNonNegativeLong(), randomNonNegativeLong())); |
| 215 | + Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); |
| 216 | + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); |
| 217 | + lifecycleState.setPhase(mockShrinkStep.getPhase()); |
| 218 | + lifecycleState.setAction(mockShrinkStep.getAction()); |
| 219 | + lifecycleState.setStep(mockShrinkStep.getName()); |
| 220 | + IndexMetaData indexMetadata = IndexMetaData.builder(index.getName()) |
| 221 | + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName)) |
| 222 | + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) |
| 223 | + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); |
| 224 | + ImmutableOpenMap.Builder<String, IndexMetaData> indices = ImmutableOpenMap.<String, IndexMetaData> builder() |
| 225 | + .fPut(index.getName(), indexMetadata); |
| 226 | + MetaData metaData = MetaData.builder() |
| 227 | + .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.STOPPING)) |
| 228 | + .indices(indices.build()) |
| 229 | + .persistentSettings(settings(Version.CURRENT).build()) |
| 230 | + .build(); |
| 231 | + ClusterState currentState = ClusterState.builder(ClusterName.DEFAULT) |
| 232 | + .metaData(metaData) |
| 233 | + .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) |
| 234 | + .build(); |
| 235 | + |
| 236 | + ClusterChangedEvent event = new ClusterChangedEvent("_source", currentState, ClusterState.EMPTY_STATE); |
| 237 | + SetOnce<Boolean> changedOperationMode = new SetOnce<>(); |
| 238 | + doAnswer(invocationOnMock -> { |
| 239 | + changedOperationMode.set(true); |
| 240 | + return null; |
| 241 | + }).when(clusterService).submitStateUpdateTask(eq("ilm_operation_mode_update"), any(OperationModeUpdateTask.class)); |
188 | 242 | indexLifecycleService.applyClusterState(event);
|
189 | 243 | indexLifecycleService.triggerPolicies(currentState, true);
|
190 |
| - assertTrue(executedShrink.get()); |
| 244 | + assertTrue(changedOperationMode.get()); |
191 | 245 | }
|
192 | 246 |
|
193 | 247 | public void testRequestedStopOnSafeAction() {
|
@@ -236,7 +290,7 @@ public void testRequestedStopOnSafeAction() {
|
236 | 290 | assertThat(task.getOperationMode(), equalTo(OperationMode.STOPPED));
|
237 | 291 | moveToMaintenance.set(true);
|
238 | 292 | return null;
|
239 |
| - }).when(clusterService).submitStateUpdateTask(anyString(), any(OperationModeUpdateTask.class)); |
| 293 | + }).when(clusterService).submitStateUpdateTask(eq("ilm_operation_mode_update"), any(OperationModeUpdateTask.class)); |
240 | 294 |
|
241 | 295 | indexLifecycleService.applyClusterState(event);
|
242 | 296 | indexLifecycleService.triggerPolicies(currentState, randomBoolean());
|
|
0 commit comments