|
|
@@ -19,22 +19,42 @@
|
|
|
package org.elasticsearch.cluster.health;
|
|
|
|
|
|
import org.elasticsearch.Version;
|
|
|
+import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
|
|
|
+import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
|
|
|
+import org.elasticsearch.action.admin.cluster.health.TransportClusterHealthAction;
|
|
|
+import org.elasticsearch.action.support.ActionFilters;
|
|
|
import org.elasticsearch.action.support.IndicesOptions;
|
|
|
+import org.elasticsearch.action.support.PlainActionFuture;
|
|
|
import org.elasticsearch.cluster.ClusterName;
|
|
|
import org.elasticsearch.cluster.ClusterState;
|
|
|
+import org.elasticsearch.cluster.ClusterStateUpdateTask;
|
|
|
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
|
|
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
|
|
|
import org.elasticsearch.cluster.metadata.MetaData;
|
|
|
import org.elasticsearch.cluster.routing.IndexRoutingTable;
|
|
|
import org.elasticsearch.cluster.routing.RoutingTable;
|
|
|
+import org.elasticsearch.cluster.service.ClusterService;
|
|
|
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
+import org.elasticsearch.test.gateway.NoopGatewayAllocator;
|
|
|
+import org.elasticsearch.test.transport.CapturingTransport;
|
|
|
+import org.elasticsearch.threadpool.ThreadPool;
|
|
|
+import org.elasticsearch.transport.TransportService;
|
|
|
import org.hamcrest.Matchers;
|
|
|
+import org.junit.After;
|
|
|
+import org.junit.AfterClass;
|
|
|
+import org.junit.Before;
|
|
|
+import org.junit.BeforeClass;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.concurrent.CountDownLatch;
|
|
|
+import java.util.concurrent.ExecutionException;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
+import static org.elasticsearch.cluster.service.ClusterServiceUtils.createClusterService;
|
|
|
import static org.hamcrest.CoreMatchers.allOf;
|
|
|
import static org.hamcrest.CoreMatchers.equalTo;
|
|
|
import static org.hamcrest.Matchers.empty;
|
|
|
@@ -45,6 +65,83 @@ import static org.hamcrest.Matchers.lessThanOrEqualTo;
|
|
|
public class ClusterStateHealthTests extends ESTestCase {
|
|
|
private final IndexNameExpressionResolver indexNameExpressionResolver = new IndexNameExpressionResolver(Settings.EMPTY);
|
|
|
|
|
|
+
|
|
|
+ private static ThreadPool threadPool;
|
|
|
+
|
|
|
+ private ClusterService clusterService;
|
|
|
+ private TransportService transportService;
|
|
|
+ private CapturingTransport transport;
|
|
|
+
|
|
|
+ @BeforeClass
|
|
|
+ public static void beforeClass() {
|
|
|
+ threadPool = new ThreadPool("ClusterStateHealthTests");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Before
|
|
|
+ public void setUp() throws Exception {
|
|
|
+ super.setUp();
|
|
|
+ transport = new CapturingTransport();
|
|
|
+ clusterService = createClusterService(threadPool);
|
|
|
+ transportService = new TransportService(transport, threadPool);
|
|
|
+ transportService.start();
|
|
|
+ transportService.acceptIncomingRequests();
|
|
|
+ }
|
|
|
+
|
|
|
+ @After
|
|
|
+ public void tearDown() throws Exception {
|
|
|
+ super.tearDown();
|
|
|
+ clusterService.close();
|
|
|
+ transportService.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @AfterClass
|
|
|
+ public static void afterClass() {
|
|
|
+ ThreadPool.terminate(threadPool, 30, TimeUnit.SECONDS);
|
|
|
+ threadPool = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testClusterHealthWaitsForClusterStateApplication() throws InterruptedException, ExecutionException {
|
|
|
+ final CountDownLatch applyLatch = new CountDownLatch(1);
|
|
|
+ final CountDownLatch listenerCalled = new CountDownLatch(1);
|
|
|
+ clusterService.add(event -> {
|
|
|
+ listenerCalled.countDown();
|
|
|
+ try {
|
|
|
+ applyLatch.await();
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ logger.debug("interrupted", e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ clusterService.submitStateUpdateTask("test", new ClusterStateUpdateTask() {
|
|
|
+ @Override
|
|
|
+ public ClusterState execute(ClusterState currentState) throws Exception {
|
|
|
+ return ClusterState.builder(currentState).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(String source, Throwable t) {
|
|
|
+ logger.warn("unexpected failure", t);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ logger.info("--> waiting for listener to be called and cluster state being blocked");
|
|
|
+ listenerCalled.await();
|
|
|
+
|
|
|
+ TransportClusterHealthAction action = new TransportClusterHealthAction(Settings.EMPTY, transportService,
|
|
|
+ clusterService, threadPool, clusterService.state().getClusterName(), new ActionFilters(new HashSet<>()),
|
|
|
+ indexNameExpressionResolver, NoopGatewayAllocator.INSTANCE);
|
|
|
+ PlainActionFuture<ClusterHealthResponse> listener = new PlainActionFuture<>();
|
|
|
+
|
|
|
+ action.execute(new ClusterHealthRequest(), listener);
|
|
|
+
|
|
|
+ assertFalse(listener.isDone());
|
|
|
+
|
|
|
+ applyLatch.countDown();
|
|
|
+ listener.get();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
public void testClusterHealth() throws IOException {
|
|
|
RoutingTableGenerator routingTableGenerator = new RoutingTableGenerator();
|
|
|
RoutingTableGenerator.ShardCounter counter = new RoutingTableGenerator.ShardCounter();
|