|
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;
|
|
58 | 59 | import static org.hamcrest.Matchers.equalTo;
|
59 | 60 | import static org.mockito.Matchers.any;
|
60 | 61 | import static org.mockito.Matchers.anyString;
|
| 62 | +import static org.mockito.Matchers.eq; |
61 | 63 | import static org.mockito.Mockito.doAnswer;
|
62 | 64 | import static org.mockito.Mockito.mock;
|
63 | 65 | import static org.mockito.Mockito.when;
|
@@ -148,7 +150,7 @@ public void testStoppedModeSkip() {
|
148 | 150 | }
|
149 | 151 |
|
150 | 152 | public void testRequestedStopOnShrink() {
|
151 |
| - Step.StepKey mockShrinkStep = new Step.StepKey(randomAlphaOfLength(4), ShrinkAction.NAME, randomAlphaOfLength(5)); |
| 153 | + Step.StepKey mockShrinkStep = new Step.StepKey(randomAlphaOfLength(4), ShrinkAction.NAME, ShrinkStep.NAME); |
152 | 154 | String policyName = randomAlphaOfLengthBetween(1, 20);
|
153 | 155 | IndexLifecycleRunnerTests.MockClusterStateActionStep mockStep =
|
154 | 156 | new IndexLifecycleRunnerTests.MockClusterStateActionStep(mockShrinkStep, randomStepKey());
|
@@ -180,14 +182,67 @@ public void testRequestedStopOnShrink() {
|
180 | 182 | .build();
|
181 | 183 |
|
182 | 184 | ClusterChangedEvent event = new ClusterChangedEvent("_source", currentState, ClusterState.EMPTY_STATE);
|
183 |
| - SetOnce<Boolean> executedShrink = new SetOnce<>(); |
| 185 | + SetOnce<Boolean> changedOperationMode = new SetOnce<>(); |
184 | 186 | doAnswer(invocationOnMock -> {
|
185 |
| - executedShrink.set(true); |
| 187 | + changedOperationMode.set(true); |
186 | 188 | return null;
|
187 |
| - }).when(clusterService).submitStateUpdateTask(anyString(), any(ExecuteStepsUpdateTask.class)); |
| 189 | + }).when(clusterService).submitStateUpdateTask(eq("ilm_operation_mode_update"), any(OperationModeUpdateTask.class)); |
| 190 | + indexLifecycleService.applyClusterState(event); |
| 191 | + indexLifecycleService.triggerPolicies(currentState, true); |
| 192 | + assertNull(changedOperationMode.get()); |
| 193 | + } |
| 194 | + |
| 195 | + public void testRequestedStopInShrinkActionButNotShrinkStep() { |
| 196 | + // test all the shrink action steps that ILM can be stopped during (basically all of them minus the actual shrink) |
| 197 | + ShrinkAction action = new ShrinkAction(1); |
| 198 | + action.toSteps(mock(Client.class), "warm", randomStepKey()).stream() |
| 199 | + .map(sk -> sk.getKey().getName()) |
| 200 | + .filter(name -> name.equals(ShrinkStep.NAME) == false) |
| 201 | + .forEach(this::verifyCanStopWithStep); |
| 202 | + } |
| 203 | + |
| 204 | + // Check that ILM can stop when in the shrink action on the provided step |
| 205 | + private void verifyCanStopWithStep(String stoppableStep) { |
| 206 | + Step.StepKey mockShrinkStep = new Step.StepKey(randomAlphaOfLength(4), ShrinkAction.NAME, stoppableStep); |
| 207 | + String policyName = randomAlphaOfLengthBetween(1, 20); |
| 208 | + IndexLifecycleRunnerTests.MockClusterStateActionStep mockStep = |
| 209 | + new IndexLifecycleRunnerTests.MockClusterStateActionStep(mockShrinkStep, randomStepKey()); |
| 210 | + MockAction mockAction = new MockAction(Collections.singletonList(mockStep)); |
| 211 | + Phase phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", mockAction)); |
| 212 | + LifecyclePolicy policy = newTestLifecyclePolicy(policyName, Collections.singletonMap(phase.getName(), phase)); |
| 213 | + SortedMap<String, LifecyclePolicyMetadata> policyMap = new TreeMap<>(); |
| 214 | + policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap(), |
| 215 | + randomNonNegativeLong(), randomNonNegativeLong())); |
| 216 | + Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); |
| 217 | + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); |
| 218 | + lifecycleState.setPhase(mockShrinkStep.getPhase()); |
| 219 | + lifecycleState.setAction(mockShrinkStep.getAction()); |
| 220 | + lifecycleState.setStep(mockShrinkStep.getName()); |
| 221 | + IndexMetaData indexMetadata = IndexMetaData.builder(index.getName()) |
| 222 | + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName)) |
| 223 | + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) |
| 224 | + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); |
| 225 | + ImmutableOpenMap.Builder<String, IndexMetaData> indices = ImmutableOpenMap.<String, IndexMetaData> builder() |
| 226 | + .fPut(index.getName(), indexMetadata); |
| 227 | + MetaData metaData = MetaData.builder() |
| 228 | + .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.STOPPING)) |
| 229 | + .indices(indices.build()) |
| 230 | + .persistentSettings(settings(Version.CURRENT).build()) |
| 231 | + .build(); |
| 232 | + ClusterState currentState = ClusterState.builder(ClusterName.DEFAULT) |
| 233 | + .metaData(metaData) |
| 234 | + .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) |
| 235 | + .build(); |
| 236 | + |
| 237 | + ClusterChangedEvent event = new ClusterChangedEvent("_source", currentState, ClusterState.EMPTY_STATE); |
| 238 | + SetOnce<Boolean> changedOperationMode = new SetOnce<>(); |
| 239 | + doAnswer(invocationOnMock -> { |
| 240 | + changedOperationMode.set(true); |
| 241 | + return null; |
| 242 | + }).when(clusterService).submitStateUpdateTask(eq("ilm_operation_mode_update"), any(OperationModeUpdateTask.class)); |
188 | 243 | indexLifecycleService.applyClusterState(event);
|
189 | 244 | indexLifecycleService.triggerPolicies(currentState, true);
|
190 |
| - assertTrue(executedShrink.get()); |
| 245 | + assertTrue(changedOperationMode.get()); |
191 | 246 | }
|
192 | 247 |
|
193 | 248 | public void testRequestedStopOnSafeAction() {
|
@@ -236,7 +291,7 @@ public void testRequestedStopOnSafeAction() {
|
236 | 291 | assertThat(task.getOperationMode(), equalTo(OperationMode.STOPPED));
|
237 | 292 | moveToMaintenance.set(true);
|
238 | 293 | return null;
|
239 |
| - }).when(clusterService).submitStateUpdateTask(anyString(), any(OperationModeUpdateTask.class)); |
| 294 | + }).when(clusterService).submitStateUpdateTask(eq("ilm_operation_mode_update"), any(OperationModeUpdateTask.class)); |
240 | 295 |
|
241 | 296 | indexLifecycleService.applyClusterState(event);
|
242 | 297 | indexLifecycleService.triggerPolicies(currentState, randomBoolean());
|
|
0 commit comments