|
50 | 50 | import java.util.Collections;
|
51 | 51 | import java.util.SortedMap;
|
52 | 52 | import java.util.TreeMap;
|
| 53 | +import java.util.concurrent.CountDownLatch; |
53 | 54 | import java.util.concurrent.ExecutorService;
|
| 55 | +import java.util.concurrent.TimeUnit; |
54 | 56 |
|
55 | 57 | import static org.elasticsearch.node.Node.NODE_MASTER_SETTING;
|
56 | 58 | import static org.elasticsearch.xpack.core.ilm.AbstractStepTestCase.randomStepKey;
|
@@ -299,6 +301,111 @@ public void testRequestedStopOnSafeAction() {
|
299 | 301 | assertTrue(moveToMaintenance.get());
|
300 | 302 | }
|
301 | 303 |
|
| 304 | + public void testExceptionStillProcessesOtherIndices() { |
| 305 | + doTestExceptionStillProcessesOtherIndices(false); |
| 306 | + } |
| 307 | + |
| 308 | + public void testExceptionStillProcessesOtherIndicesOnMaster() { |
| 309 | + doTestExceptionStillProcessesOtherIndices(true); |
| 310 | + } |
| 311 | + |
| 312 | + @SuppressWarnings("unchecked") |
| 313 | + public void doTestExceptionStillProcessesOtherIndices(boolean useOnMaster) { |
| 314 | + String policy1 = randomAlphaOfLengthBetween(1, 20); |
| 315 | + Step.StepKey i1currentStepKey = randomStepKey(); |
| 316 | + final Step i1mockStep; |
| 317 | + if (useOnMaster) { |
| 318 | + i1mockStep = new IndexLifecycleRunnerTests.MockAsyncActionStep(i1currentStepKey, randomStepKey()); |
| 319 | + } else { |
| 320 | + i1mockStep = new IndexLifecycleRunnerTests.MockClusterStateActionStep(i1currentStepKey, randomStepKey()); |
| 321 | + } |
| 322 | + MockAction i1mockAction = new MockAction(Collections.singletonList(i1mockStep)); |
| 323 | + Phase i1phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", i1mockAction)); |
| 324 | + LifecyclePolicy i1policy = newTestLifecyclePolicy(policy1, Collections.singletonMap(i1phase.getName(), i1phase)); |
| 325 | + Index index1 = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); |
| 326 | + LifecycleExecutionState.Builder i1lifecycleState = LifecycleExecutionState.builder(); |
| 327 | + i1lifecycleState.setPhase(i1currentStepKey.getPhase()); |
| 328 | + i1lifecycleState.setAction(i1currentStepKey.getAction()); |
| 329 | + i1lifecycleState.setStep(i1currentStepKey.getName()); |
| 330 | + |
| 331 | + String policy2 = randomValueOtherThan(policy1, () -> randomAlphaOfLengthBetween(1, 20)); |
| 332 | + Step.StepKey i2currentStepKey = randomStepKey(); |
| 333 | + final Step i2mockStep; |
| 334 | + if (useOnMaster) { |
| 335 | + i2mockStep = new IndexLifecycleRunnerTests.MockAsyncActionStep(i2currentStepKey, randomStepKey()); |
| 336 | + } else { |
| 337 | + i2mockStep = new IndexLifecycleRunnerTests.MockClusterStateActionStep(i2currentStepKey, randomStepKey()); |
| 338 | + } |
| 339 | + MockAction mockAction = new MockAction(Collections.singletonList(i2mockStep)); |
| 340 | + Phase i2phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", mockAction)); |
| 341 | + LifecyclePolicy i2policy = newTestLifecyclePolicy(policy1, Collections.singletonMap(i2phase.getName(), i1phase)); |
| 342 | + Index index2 = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); |
| 343 | + LifecycleExecutionState.Builder i2lifecycleState = LifecycleExecutionState.builder(); |
| 344 | + i2lifecycleState.setPhase(i2currentStepKey.getPhase()); |
| 345 | + i2lifecycleState.setAction(i2currentStepKey.getAction()); |
| 346 | + i2lifecycleState.setStep(i2currentStepKey.getName()); |
| 347 | + |
| 348 | + CountDownLatch stepLatch = new CountDownLatch(2); |
| 349 | + boolean failStep1 = randomBoolean(); |
| 350 | + if (useOnMaster) { |
| 351 | + ((IndexLifecycleRunnerTests.MockAsyncActionStep) i1mockStep).setLatch(stepLatch); |
| 352 | + ((IndexLifecycleRunnerTests.MockAsyncActionStep) i1mockStep) |
| 353 | + .setException(failStep1 ? new IllegalArgumentException("forcing a failure for index 1") : null); |
| 354 | + ((IndexLifecycleRunnerTests.MockAsyncActionStep) i2mockStep).setLatch(stepLatch); |
| 355 | + ((IndexLifecycleRunnerTests.MockAsyncActionStep) i2mockStep) |
| 356 | + .setException(failStep1 ? null : new IllegalArgumentException("forcing a failure for index 2")); |
| 357 | + } else { |
| 358 | + ((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep).setLatch(stepLatch); |
| 359 | + ((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep) |
| 360 | + .setException(failStep1 ? new IllegalArgumentException("forcing a failure for index 1") : null); |
| 361 | + ((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep).setLatch(stepLatch); |
| 362 | + ((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep) |
| 363 | + .setException(failStep1 ? null : new IllegalArgumentException("forcing a failure for index 2")); |
| 364 | + } |
| 365 | + |
| 366 | + SortedMap<String, LifecyclePolicyMetadata> policyMap = new TreeMap<>(); |
| 367 | + policyMap.put(policy1, new LifecyclePolicyMetadata(i1policy, Collections.emptyMap(), |
| 368 | + randomNonNegativeLong(), randomNonNegativeLong())); |
| 369 | + policyMap.put(policy2, new LifecyclePolicyMetadata(i2policy, Collections.emptyMap(), |
| 370 | + randomNonNegativeLong(), randomNonNegativeLong())); |
| 371 | + |
| 372 | + IndexMetaData i1indexMetadata = IndexMetaData.builder(index1.getName()) |
| 373 | + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policy1)) |
| 374 | + .putCustom(ILM_CUSTOM_METADATA_KEY, i1lifecycleState.build().asMap()) |
| 375 | + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); |
| 376 | + IndexMetaData i2indexMetadata = IndexMetaData.builder(index2.getName()) |
| 377 | + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policy1)) |
| 378 | + .putCustom(ILM_CUSTOM_METADATA_KEY, i2lifecycleState.build().asMap()) |
| 379 | + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); |
| 380 | + ImmutableOpenMap.Builder<String, IndexMetaData> indices = ImmutableOpenMap.<String, IndexMetaData> builder() |
| 381 | + .fPut(index1.getName(), i1indexMetadata) |
| 382 | + .fPut(index2.getName(), i2indexMetadata); |
| 383 | + |
| 384 | + MetaData metaData = MetaData.builder() |
| 385 | + .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.RUNNING)) |
| 386 | + .indices(indices.build()) |
| 387 | + .persistentSettings(settings(Version.CURRENT).build()) |
| 388 | + .build(); |
| 389 | + |
| 390 | + ClusterState currentState = ClusterState.builder(ClusterName.DEFAULT) |
| 391 | + .metaData(metaData) |
| 392 | + .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) |
| 393 | + .build(); |
| 394 | + |
| 395 | + if (useOnMaster) { |
| 396 | + when(clusterService.state()).thenReturn(currentState); |
| 397 | + indexLifecycleService.onMaster(); |
| 398 | + } else { |
| 399 | + indexLifecycleService.triggerPolicies(currentState, randomBoolean()); |
| 400 | + } |
| 401 | + try { |
| 402 | + stepLatch.await(5, TimeUnit.SECONDS); |
| 403 | + } catch (InterruptedException e) { |
| 404 | + logger.error("failure while waiting for step execution", e); |
| 405 | + fail("both steps should have been executed, even with an exception"); |
| 406 | + } |
| 407 | + } |
| 408 | + |
302 | 409 | public void testTriggeredDifferentJob() {
|
303 | 410 | Mockito.reset(clusterService);
|
304 | 411 | SchedulerEngine.Event schedulerEvent = new SchedulerEngine.Event("foo", randomLong(), randomLong());
|
|
0 commit comments