|
161 | 161 | import static org.hamcrest.Matchers.equalTo;
|
162 | 162 | import static org.hamcrest.Matchers.greaterThan;
|
163 | 163 | import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
164 |
| -import static org.junit.Assert.assertEquals; |
165 | 164 | import static org.hamcrest.Matchers.not;
|
166 | 165 | import static org.hamcrest.Matchers.nullValue;
|
| 166 | +import static org.junit.Assert.assertEquals; |
167 | 167 | import static org.junit.Assert.assertFalse;
|
168 | 168 | import static org.junit.Assert.assertThat;
|
169 | 169 | import static org.junit.Assert.assertTrue;
|
@@ -1239,29 +1239,35 @@ private synchronized void reset(boolean wipeData) throws IOException {
|
1239 | 1239 |
|
1240 | 1240 | /** ensure a cluster is formed with all published nodes. */
|
1241 | 1241 | public synchronized void validateClusterFormed() {
|
1242 |
| - String name = randomFrom(random, getNodeNames()); |
1243 |
| - validateClusterFormed(name); |
1244 |
| - } |
1245 |
| - |
1246 |
| - /** ensure a cluster is formed with all published nodes, but do so by using the client of the specified node */ |
1247 |
| - private synchronized void validateClusterFormed(String viaNode) { |
1248 |
| - Set<DiscoveryNode> expectedNodes = new HashSet<>(); |
| 1242 | + final Set<DiscoveryNode> expectedNodes = new HashSet<>(); |
1249 | 1243 | for (NodeAndClient nodeAndClient : nodes.values()) {
|
1250 | 1244 | expectedNodes.add(getInstanceFromNode(ClusterService.class, nodeAndClient.node()).localNode());
|
1251 | 1245 | }
|
1252 |
| - logger.trace("validating cluster formed via [{}], expecting {}", viaNode, expectedNodes); |
1253 |
| - final Client client = client(viaNode); |
| 1246 | + logger.trace("validating cluster formed, expecting {}", expectedNodes); |
| 1247 | + |
1254 | 1248 | try {
|
1255 | 1249 | assertBusy(() -> {
|
1256 |
| - DiscoveryNodes discoveryNodes = client.admin().cluster().prepareState().get().getState().nodes(); |
1257 |
| - assertEquals(expectedNodes.size(), discoveryNodes.getSize()); |
1258 |
| - for (DiscoveryNode expectedNode : expectedNodes) { |
1259 |
| - assertTrue("Expected node to exist: " + expectedNode, discoveryNodes.nodeExists(expectedNode)); |
1260 |
| - } |
| 1250 | + final List<ClusterState> states = nodes.values().stream() |
| 1251 | + .map(node -> getInstanceFromNode(ClusterService.class, node.node())) |
| 1252 | + .map(ClusterService::state) |
| 1253 | + .collect(Collectors.toList()); |
| 1254 | + final String debugString = ", expected nodes: " + expectedNodes + " and actual cluster states " + states; |
| 1255 | + // all nodes have a master |
| 1256 | + assertTrue("Missing master" + debugString, states.stream().allMatch(cs -> cs.nodes().getMasterNodeId() != null)); |
| 1257 | + // all nodes have the same master (in same term) |
| 1258 | + assertEquals("Not all masters in same term" + debugString, 1, |
| 1259 | + states.stream().mapToLong(ClusterState::term).distinct().count()); |
| 1260 | + // all nodes know about all other nodes |
| 1261 | + states.forEach(cs -> { |
| 1262 | + DiscoveryNodes discoveryNodes = cs.nodes(); |
| 1263 | + assertEquals("Node size mismatch" + debugString, expectedNodes.size(), discoveryNodes.getSize()); |
| 1264 | + for (DiscoveryNode expectedNode : expectedNodes) { |
| 1265 | + assertTrue("Expected node to exist: " + expectedNode + debugString, discoveryNodes.nodeExists(expectedNode)); |
| 1266 | + } |
| 1267 | + }); |
1261 | 1268 | }, 30, TimeUnit.SECONDS);
|
1262 | 1269 | } catch (AssertionError ae) {
|
1263 |
| - throw new IllegalStateException("cluster failed to form with expected nodes " + expectedNodes + " and actual nodes " + |
1264 |
| - client.admin().cluster().prepareState().get().getState().nodes()); |
| 1270 | + throw new IllegalStateException("cluster failed to form", ae); |
1265 | 1271 | } catch (Exception e) {
|
1266 | 1272 | throw new IllegalStateException(e);
|
1267 | 1273 | }
|
@@ -1821,8 +1827,7 @@ private void restartNode(NodeAndClient nodeAndClient, RestartCallback callback)
|
1821 | 1827 |
|
1822 | 1828 | if (callback.validateClusterForming() || excludedNodeIds.isEmpty() == false) {
|
1823 | 1829 | // we have to validate cluster size to ensure that the restarted node has rejoined the cluster if it was master-eligible;
|
1824 |
| - // we have to do this via the node that was just restarted as it may be that the master didn't yet process the fact that it left |
1825 |
| - validateClusterFormed(nodeAndClient.name); |
| 1830 | + validateClusterFormed(); |
1826 | 1831 | }
|
1827 | 1832 |
|
1828 | 1833 | if (excludedNodeIds.isEmpty() == false) {
|
|
0 commit comments