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

Allow background cluster state update in tests #61455

Merged
Show file tree
Hide file tree
Changes from all 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 @@ -1367,7 +1367,7 @@ public void testClusterRecoversAfterExceptionDuringSerialization() {
assertThat(e.getCause().getMessage(), equalTo(BrokenCustom.EXCEPTION_MESSAGE));
failed.set(true);
});
cluster.runFor(DEFAULT_DELAY_VARIABILITY + 1, "processing broken task");
cluster.runFor(2 * DEFAULT_DELAY_VARIABILITY + 1, "processing broken task");
assertTrue(failed.get());

cluster.stabilise();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected void publish(ClusterChangedEvent clusterChangedEvent, TaskOutputs task
assert waitForPublish == false;
waitForPublish = true;
final AckListener ackListener = taskOutputs.createAckListener(threadPool, clusterChangedEvent.state());
clusterStatePublisher.publish(clusterChangedEvent, new ActionListener<Void>() {
final ActionListener<Void> publishListener = new ActionListener<>() {

private boolean listenerCalled = false;

Expand Down Expand Up @@ -157,7 +157,20 @@ public void onFailure(Exception e) {
scheduleNextTaskIfNecessary();
}
}
}, wrapAckListener(ackListener));
};
threadPool.generic().execute(threadPool.getThreadContext().preserveContext(new Runnable() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

NB not really the generic threadpool, we're using the DeterministicTaskQueue to run on a single thread here.

@Override
public void run() {
clusterStatePublisher.publish(clusterChangedEvent, publishListener, wrapAckListener(ackListener));
}

@Override
public String toString() {
return "publish change of cluster state from version [" + clusterChangedEvent.previousState().version() + "] in term [" +
clusterChangedEvent.previousState().term() + "] to version [" + clusterChangedEvent.state().version()
+ "] in term [" + clusterChangedEvent.state().term() + "]";
}
}));
}

protected AckListener wrapAckListener(AckListener ackListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasToString;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -56,6 +61,11 @@ public void testFakeMasterService() {
final ThreadContext context = new ThreadContext(Settings.EMPTY);
final ThreadPool mockThreadPool = mock(ThreadPool.class);
when(mockThreadPool.getThreadContext()).thenReturn(context);

final ExecutorService executorService = mock(ExecutorService.class);
doAnswer(invocationOnMock -> runnableTasks.add((Runnable) invocationOnMock.getArguments()[0])).when(executorService).execute(any());
when(mockThreadPool.generic()).thenReturn(executorService);

FakeThreadPoolMasterService masterService = new FakeThreadPoolMasterService("test_node","test", mockThreadPool, runnableTasks::add);
masterService.setClusterStateSupplier(lastClusterStateRef::get);
masterService.setClusterStatePublisher((event, publishListener, ackListener) -> {
Expand Down Expand Up @@ -89,7 +99,14 @@ public void onFailure(String source, Exception e) {
assertNull(publishingCallback.get());
assertFalse(firstTaskCompleted.get());

runnableTasks.remove(0).run();
final Runnable scheduleTask = runnableTasks.remove(0);
assertThat(scheduleTask, hasToString("master service scheduling next task"));
scheduleTask.run();

final Runnable publishTask = runnableTasks.remove(0);
assertThat(publishTask, hasToString(containsString("publish change of cluster state")));
publishTask.run();

assertThat(lastClusterStateRef.get().metadata().indices().size(), equalTo(1));
assertThat(lastClusterStateRef.get().version(), equalTo(firstClusterStateVersion + 1));
assertNotNull(publishingCallback.get());
Expand Down Expand Up @@ -121,7 +138,8 @@ public void onFailure(String source, Exception e) {
assertTrue(firstTaskCompleted.get());
assertThat(runnableTasks.size(), equalTo(1)); // check that new task gets queued

runnableTasks.remove(0).run();
runnableTasks.remove(0).run(); // schedule again
runnableTasks.remove(0).run(); // publish again
assertThat(lastClusterStateRef.get().metadata().indices().size(), equalTo(2));
assertThat(lastClusterStateRef.get().version(), equalTo(firstClusterStateVersion + 2));
assertNotNull(publishingCallback.get());
Expand Down