Skip to content

Commit 9429022

Browse files
authored
AsyncTwoPhaseIndexerTests race condition fixed #38195 Backport#37830
The unlucky timing can cause this test to fail when the indexing is triggered from maybeTriggerAsyncJob. As this is asynchronous, in can finish quicker then the test stepping over to next assertion The introduced barrier solves the problem closes #37695 Backport #37830
1 parent 3e2da3c commit 9429022

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexerTests.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020

2121
import java.io.IOException;
2222
import java.util.Collections;
23+
import java.util.concurrent.CountDownLatch;
2324
import java.util.concurrent.Executor;
2425
import java.util.concurrent.ExecutorService;
2526
import java.util.concurrent.Executors;
27+
import java.util.concurrent.TimeUnit;
2628
import java.util.concurrent.atomic.AtomicBoolean;
2729
import java.util.concurrent.atomic.AtomicReference;
2830

@@ -34,11 +36,14 @@ public class AsyncTwoPhaseIndexerTests extends ESTestCase {
3436

3537
private class MockIndexer extends AsyncTwoPhaseIndexer<Integer, MockJobStats> {
3638

39+
private final CountDownLatch latch;
3740
// test the execution order
3841
private int step;
3942

40-
protected MockIndexer(Executor executor, AtomicReference<IndexerState> initialState, Integer initialPosition) {
43+
protected MockIndexer(Executor executor, AtomicReference<IndexerState> initialState, Integer initialPosition,
44+
CountDownLatch latch) {
4145
super(executor, initialState, initialPosition, new MockJobStats());
46+
this.latch = latch;
4247
}
4348

4449
@Override
@@ -48,11 +53,20 @@ protected String getJobId() {
4853

4954
@Override
5055
protected IterationResult<Integer> doProcess(SearchResponse searchResponse) {
56+
awaitForLatch();
5157
assertThat(step, equalTo(3));
5258
++step;
5359
return new IterationResult<Integer>(Collections.emptyList(), 3, true);
5460
}
5561

62+
private void awaitForLatch() {
63+
try {
64+
latch.await(10, TimeUnit.SECONDS);
65+
} catch (InterruptedException e) {
66+
throw new RuntimeException(e);
67+
}
68+
}
69+
5670
@Override
5771
protected SearchRequest buildSearchRequest() {
5872
assertThat(step, equalTo(1));
@@ -195,12 +209,14 @@ public void testStateMachine() throws InterruptedException {
195209
final ExecutorService executor = Executors.newFixedThreadPool(1);
196210
isFinished.set(false);
197211
try {
198-
199-
MockIndexer indexer = new MockIndexer(executor, state, 2);
212+
CountDownLatch countDownLatch = new CountDownLatch(1);
213+
MockIndexer indexer = new MockIndexer(executor, state, 2, countDownLatch);
200214
indexer.start();
201215
assertThat(indexer.getState(), equalTo(IndexerState.STARTED));
202216
assertTrue(indexer.maybeTriggerAsyncJob(System.currentTimeMillis()));
203217
assertThat(indexer.getState(), equalTo(IndexerState.INDEXING));
218+
countDownLatch.countDown();
219+
204220
assertThat(indexer.getPosition(), equalTo(2));
205221
ESTestCase.awaitBusy(() -> isFinished.get());
206222
assertThat(indexer.getStep(), equalTo(6));

0 commit comments

Comments
 (0)