|
19 | 19 | package org.elasticsearch.cluster.health; |
20 | 20 |
|
21 | 21 | import org.elasticsearch.Version; |
| 22 | +import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; |
| 23 | +import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; |
| 24 | +import org.elasticsearch.action.admin.cluster.health.TransportClusterHealthAction; |
| 25 | +import org.elasticsearch.action.support.ActionFilters; |
22 | 26 | import org.elasticsearch.action.support.IndicesOptions; |
| 27 | +import org.elasticsearch.action.support.PlainActionFuture; |
23 | 28 | import org.elasticsearch.cluster.ClusterName; |
24 | 29 | import org.elasticsearch.cluster.ClusterState; |
| 30 | +import org.elasticsearch.cluster.ClusterStateUpdateTask; |
25 | 31 | import org.elasticsearch.cluster.metadata.IndexMetaData; |
26 | 32 | import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
27 | 33 | import org.elasticsearch.cluster.metadata.MetaData; |
28 | 34 | import org.elasticsearch.cluster.routing.IndexRoutingTable; |
29 | 35 | import org.elasticsearch.cluster.routing.RoutingTable; |
| 36 | +import org.elasticsearch.cluster.service.ClusterService; |
30 | 37 | import org.elasticsearch.common.io.stream.BytesStreamOutput; |
31 | 38 | import org.elasticsearch.common.io.stream.StreamInput; |
32 | 39 | import org.elasticsearch.common.settings.Settings; |
33 | 40 | import org.elasticsearch.test.ESTestCase; |
| 41 | +import org.elasticsearch.test.gateway.NoopGatewayAllocator; |
| 42 | +import org.elasticsearch.test.transport.CapturingTransport; |
| 43 | +import org.elasticsearch.threadpool.ThreadPool; |
| 44 | +import org.elasticsearch.transport.TransportService; |
34 | 45 | import org.hamcrest.Matchers; |
| 46 | +import org.junit.After; |
| 47 | +import org.junit.AfterClass; |
| 48 | +import org.junit.Before; |
| 49 | +import org.junit.BeforeClass; |
35 | 50 |
|
36 | 51 | import java.io.IOException; |
| 52 | +import java.util.HashSet; |
| 53 | +import java.util.concurrent.CountDownLatch; |
| 54 | +import java.util.concurrent.ExecutionException; |
| 55 | +import java.util.concurrent.TimeUnit; |
37 | 56 |
|
| 57 | +import static org.elasticsearch.cluster.service.ClusterServiceUtils.createClusterService; |
38 | 58 | import static org.hamcrest.CoreMatchers.allOf; |
39 | 59 | import static org.hamcrest.CoreMatchers.equalTo; |
40 | 60 | import static org.hamcrest.Matchers.empty; |
|
45 | 65 | public class ClusterStateHealthTests extends ESTestCase { |
46 | 66 | private final IndexNameExpressionResolver indexNameExpressionResolver = new IndexNameExpressionResolver(Settings.EMPTY); |
47 | 67 |
|
| 68 | + |
| 69 | + private static ThreadPool threadPool; |
| 70 | + |
| 71 | + private ClusterService clusterService; |
| 72 | + private TransportService transportService; |
| 73 | + private CapturingTransport transport; |
| 74 | + |
| 75 | + @BeforeClass |
| 76 | + public static void beforeClass() { |
| 77 | + threadPool = new ThreadPool("ClusterStateHealthTests"); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + @Before |
| 82 | + public void setUp() throws Exception { |
| 83 | + super.setUp(); |
| 84 | + transport = new CapturingTransport(); |
| 85 | + clusterService = createClusterService(threadPool); |
| 86 | + transportService = new TransportService(transport, threadPool); |
| 87 | + transportService.start(); |
| 88 | + transportService.acceptIncomingRequests(); |
| 89 | + } |
| 90 | + |
| 91 | + @After |
| 92 | + public void tearDown() throws Exception { |
| 93 | + super.tearDown(); |
| 94 | + clusterService.close(); |
| 95 | + transportService.close(); |
| 96 | + } |
| 97 | + |
| 98 | + @AfterClass |
| 99 | + public static void afterClass() { |
| 100 | + ThreadPool.terminate(threadPool, 30, TimeUnit.SECONDS); |
| 101 | + threadPool = null; |
| 102 | + } |
| 103 | + |
| 104 | + public void testClusterHealthWaitsForClusterStateApplication() throws InterruptedException, ExecutionException { |
| 105 | + final CountDownLatch applyLatch = new CountDownLatch(1); |
| 106 | + final CountDownLatch listenerCalled = new CountDownLatch(1); |
| 107 | + clusterService.add(event -> { |
| 108 | + listenerCalled.countDown(); |
| 109 | + try { |
| 110 | + applyLatch.await(); |
| 111 | + } catch (InterruptedException e) { |
| 112 | + logger.debug("interrupted", e); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + clusterService.submitStateUpdateTask("test", new ClusterStateUpdateTask() { |
| 117 | + @Override |
| 118 | + public ClusterState execute(ClusterState currentState) throws Exception { |
| 119 | + return ClusterState.builder(currentState).build(); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void onFailure(String source, Throwable t) { |
| 124 | + logger.warn("unexpected failure", t); |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + logger.info("--> waiting for listener to be called and cluster state being blocked"); |
| 129 | + listenerCalled.await(); |
| 130 | + |
| 131 | + TransportClusterHealthAction action = new TransportClusterHealthAction(Settings.EMPTY, transportService, |
| 132 | + clusterService, threadPool, clusterService.state().getClusterName(), new ActionFilters(new HashSet<>()), |
| 133 | + indexNameExpressionResolver, NoopGatewayAllocator.INSTANCE); |
| 134 | + PlainActionFuture<ClusterHealthResponse> listener = new PlainActionFuture<>(); |
| 135 | + |
| 136 | + action.execute(new ClusterHealthRequest(), listener); |
| 137 | + |
| 138 | + assertFalse(listener.isDone()); |
| 139 | + |
| 140 | + applyLatch.countDown(); |
| 141 | + listener.get(); |
| 142 | + } |
| 143 | + |
| 144 | + |
48 | 145 | public void testClusterHealth() throws IOException { |
49 | 146 | RoutingTableGenerator routingTableGenerator = new RoutingTableGenerator(); |
50 | 147 | RoutingTableGenerator.ShardCounter counter = new RoutingTableGenerator.ShardCounter(); |
|
0 commit comments