Skip to content

Commit b7ab521

Browse files
authored
Throw AssertionError when no master (#38432)
Today we throw a fatal `RuntimeException` if an exception occurs in `getMasterName()`, and this includes the case where there is currently no master. However, sometimes we call this method inside an `assertBusy()` in order to allow for a cluster that is in the process of stabilising and electing a master. The trouble is that `assertBusy()` only retries on an `AssertionError` and not on a general `RuntimeException`, so the lack of a master is immediately fatal. This commit fixes the issue by asserting there is a master, triggering a retry if there is not. Fixes #38331
1 parent 12657fd commit b7ab521

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

server/src/test/java/org/elasticsearch/cluster/SpecificMasterNodesIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ public void testSimpleOnlyMasterNodeElection() throws IOException {
8484
.execute().actionGet().getState().nodes().getMasterNode().getName(), equalTo(nextMasterEligibleNodeName));
8585
}
8686

87-
@AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/38331")
8887
public void testElectOnlyBetweenMasterNodes() throws Exception {
8988
internalCluster().setBootstrapMasterNodeIndex(0);
9089
logger.info("--> start data node / non master node");

test/framework/src/main/java/org/elasticsearch/test/InternalTestCluster.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
import static org.hamcrest.Matchers.not;
168168
import static org.hamcrest.Matchers.nullValue;
169169
import static org.junit.Assert.assertFalse;
170+
import static org.junit.Assert.assertNotNull;
170171
import static org.junit.Assert.assertThat;
171172
import static org.junit.Assert.assertTrue;
172173
import static org.junit.Assert.fail;
@@ -1909,8 +1910,9 @@ public String getMasterName() {
19091910
public String getMasterName(@Nullable String viaNode) {
19101911
try {
19111912
Client client = viaNode != null ? client(viaNode) : client();
1912-
ClusterState state = client.admin().cluster().prepareState().execute().actionGet().getState();
1913-
return state.nodes().getMasterNode().getName();
1913+
final DiscoveryNode masterNode = client.admin().cluster().prepareState().get().getState().nodes().getMasterNode();
1914+
assertNotNull(masterNode);
1915+
return masterNode.getName();
19141916
} catch (Exception e) {
19151917
logger.warn("Can't fetch cluster state", e);
19161918
throw new RuntimeException("Can't get master node " + e.getMessage(), e);

0 commit comments

Comments
 (0)