Skip to content

Commit 8bb2c05

Browse files
committed
[ILM] Fix race condition in test (#35143)
Previously, testRunStateChangePolicyWithNextStep asserted that the ClusterState before and after running the steps were equal. The test only passed due to a race condition: The latch would be triggered by the step execution, but the cluster state update thread would continue running before committing the change to the cluster state. This allowed the test to read the old cluster state and pass the equality check about 99.99% of the time. The test now waits for the new cluster state to be committed before checking that it is _not_ equal to the old cluster state.
1 parent 4748741 commit 8bb2c05

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,18 +255,26 @@ public void testRunStateChangePolicyWithNextStep() throws Exception {
255255
.localNodeId(node.getId()))
256256
.build();
257257
ClusterServiceUtils.setState(clusterService, state);
258-
IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L);
258+
long stepTime = randomLong();
259+
IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> stepTime);
259260

260261
ClusterState before = clusterService.state();
261262
CountDownLatch latch = new CountDownLatch(1);
262-
step.setLatch(latch);
263+
nextStep.setLatch(latch);
263264
runner.runPolicyAfterStateChange(policyName, indexMetaData);
264265

265-
latch.await(5, TimeUnit.SECONDS);
266-
ClusterState after = clusterService.state();
266+
assertTrue(latch.await(5, TimeUnit.SECONDS));
267267

268-
assertEquals(before, after);
268+
// The cluster state can take a few extra milliseconds to update after the steps are executed
269+
assertBusy(() -> assertNotEquals(before, clusterService.state()));
270+
LifecycleExecutionState newExecutionState = LifecycleExecutionState
271+
.fromIndexMetadata(clusterService.state().metaData().index(indexMetaData.getIndex()));
272+
assertThat(newExecutionState.getPhase(), equalTo("phase"));
273+
assertThat(newExecutionState.getAction(), equalTo("action"));
274+
assertThat(newExecutionState.getStep(), equalTo("next_cluster_state_action_step"));
275+
assertThat(newExecutionState.getStepTime(), equalTo(stepTime));
269276
assertThat(step.getExecuteCount(), equalTo(1L));
277+
assertThat(nextStep.getExecuteCount(), equalTo(1L));
270278
clusterService.close();
271279
threadPool.shutdownNow();
272280
}

0 commit comments

Comments
 (0)