Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncTwoPhaseIndexerTests race condition fixed #37830

Merged
merged 4 commits into from
Jan 25, 2019
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.IOException;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -35,11 +36,14 @@ public class AsyncTwoPhaseIndexerTests extends ESTestCase {

private class MockIndexer extends AsyncTwoPhaseIndexer<Integer, MockJobStats> {

private final CountDownLatch latch;
// test the execution order
private int step;

protected MockIndexer(Executor executor, AtomicReference<IndexerState> initialState, Integer initialPosition) {
protected MockIndexer(Executor executor, AtomicReference<IndexerState> initialState, Integer initialPosition,
CountDownLatch latch) {
super(executor, initialState, initialPosition, new MockJobStats());
this.latch = latch;
}

@Override
Expand All @@ -49,11 +53,20 @@ protected String getJobId() {

@Override
protected IterationResult<Integer> doProcess(SearchResponse searchResponse) {
awaitForLatch();
assertThat(step, equalTo(3));
++step;
return new IterationResult<Integer>(Collections.emptyList(), 3, true);
}

private void awaitForLatch() {
try {
latch.await();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put a timeout (10s or whatever you think it's appropriate) to prevent this for hanging up.

} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

@Override
protected SearchRequest buildSearchRequest() {
assertThat(step, equalTo(1));
Expand Down Expand Up @@ -196,12 +209,14 @@ public void testStateMachine() throws InterruptedException {
final ExecutorService executor = Executors.newFixedThreadPool(1);
isFinished.set(false);
try {

MockIndexer indexer = new MockIndexer(executor, state, 2);
CountDownLatch countDownLatch = new CountDownLatch(1);
MockIndexer indexer = new MockIndexer(executor, state, 2, countDownLatch);
indexer.start();
assertThat(indexer.getState(), equalTo(IndexerState.STARTED));
assertTrue(indexer.maybeTriggerAsyncJob(System.currentTimeMillis()));
assertThat(indexer.getState(), equalTo(IndexerState.INDEXING));
countDownLatch.countDown();

assertThat(indexer.getPosition(), equalTo(2));
ESTestCase.awaitBusy(() -> isFinished.get());
assertThat(indexer.getStep(), equalTo(6));
Expand Down