|
150 | 150 | import static org.hamcrest.Matchers.equalTo;
|
151 | 151 | import static org.hamcrest.Matchers.greaterThan;
|
152 | 152 | import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
153 |
| -import static org.junit.Assert.assertEquals; |
154 | 153 | import static org.hamcrest.Matchers.not;
|
155 | 154 | import static org.hamcrest.Matchers.nullValue;
|
| 155 | +import static org.junit.Assert.assertEquals; |
156 | 156 | import static org.junit.Assert.assertFalse;
|
157 | 157 | import static org.junit.Assert.assertThat;
|
158 | 158 | import static org.junit.Assert.assertTrue;
|
@@ -1092,29 +1092,35 @@ private synchronized void reset(boolean wipeData) throws IOException {
|
1092 | 1092 |
|
1093 | 1093 | /** ensure a cluster is formed with all published nodes. */
|
1094 | 1094 | public synchronized void validateClusterFormed() {
|
1095 |
| - String name = randomFrom(random, getNodeNames()); |
1096 |
| - validateClusterFormed(name); |
1097 |
| - } |
1098 |
| - |
1099 |
| - /** ensure a cluster is formed with all published nodes, but do so by using the client of the specified node */ |
1100 |
| - private synchronized void validateClusterFormed(String viaNode) { |
1101 |
| - Set<DiscoveryNode> expectedNodes = new HashSet<>(); |
| 1095 | + final Set<DiscoveryNode> expectedNodes = new HashSet<>(); |
1102 | 1096 | for (NodeAndClient nodeAndClient : nodes.values()) {
|
1103 | 1097 | expectedNodes.add(getInstanceFromNode(ClusterService.class, nodeAndClient.node()).localNode());
|
1104 | 1098 | }
|
1105 |
| - logger.trace("validating cluster formed via [{}], expecting {}", viaNode, expectedNodes); |
1106 |
| - final Client client = client(viaNode); |
| 1099 | + logger.trace("validating cluster formed, expecting {}", expectedNodes); |
| 1100 | + |
1107 | 1101 | try {
|
1108 | 1102 | assertBusy(() -> {
|
1109 |
| - DiscoveryNodes discoveryNodes = client.admin().cluster().prepareState().get().getState().nodes(); |
1110 |
| - assertEquals(expectedNodes.size(), discoveryNodes.getSize()); |
1111 |
| - for (DiscoveryNode expectedNode : expectedNodes) { |
1112 |
| - assertTrue("Expected node to exist: " + expectedNode, discoveryNodes.nodeExists(expectedNode)); |
1113 |
| - } |
| 1103 | + final List<ClusterState> states = nodes.values().stream() |
| 1104 | + .map(node -> getInstanceFromNode(ClusterService.class, node.node())) |
| 1105 | + .map(ClusterService::state) |
| 1106 | + .collect(Collectors.toList()); |
| 1107 | + final String debugString = ", expected nodes: " + expectedNodes + " and actual cluster states " + states; |
| 1108 | + // all nodes have a master |
| 1109 | + assertTrue("Missing master" + debugString, states.stream().allMatch(cs -> cs.nodes().getMasterNodeId() != null)); |
| 1110 | + // all nodes have the same master (in same term) |
| 1111 | + assertEquals("Not all masters in same term" + debugString, 1, |
| 1112 | + states.stream().mapToLong(ClusterState::term).distinct().count()); |
| 1113 | + // all nodes know about all other nodes |
| 1114 | + states.forEach(cs -> { |
| 1115 | + DiscoveryNodes discoveryNodes = cs.nodes(); |
| 1116 | + assertEquals("Node size mismatch" + debugString, expectedNodes.size(), discoveryNodes.getSize()); |
| 1117 | + for (DiscoveryNode expectedNode : expectedNodes) { |
| 1118 | + assertTrue("Expected node to exist: " + expectedNode + debugString, discoveryNodes.nodeExists(expectedNode)); |
| 1119 | + } |
| 1120 | + }); |
1114 | 1121 | }, 30, TimeUnit.SECONDS);
|
1115 | 1122 | } catch (AssertionError ae) {
|
1116 |
| - throw new IllegalStateException("cluster failed to form with expected nodes " + expectedNodes + " and actual nodes " + |
1117 |
| - client.admin().cluster().prepareState().get().getState().nodes()); |
| 1123 | + throw new IllegalStateException("cluster failed to form", ae); |
1118 | 1124 | } catch (Exception e) {
|
1119 | 1125 | throw new IllegalStateException(e);
|
1120 | 1126 | }
|
@@ -1663,8 +1669,7 @@ private void restartNode(NodeAndClient nodeAndClient, RestartCallback callback)
|
1663 | 1669 |
|
1664 | 1670 | if (callback.validateClusterForming() || excludedNodeIds.isEmpty() == false) {
|
1665 | 1671 | // we have to validate cluster size to ensure that the restarted node has rejoined the cluster if it was master-eligible;
|
1666 |
| - // 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 |
1667 |
| - validateClusterFormed(nodeAndClient.name); |
| 1672 | + validateClusterFormed(); |
1668 | 1673 | }
|
1669 | 1674 | }
|
1670 | 1675 |
|
|
0 commit comments