Skip to content

Commit dc7a48d

Browse files
committed
Add waiting for master
1 parent 343837c commit dc7a48d

File tree

14 files changed

+227
-28
lines changed

14 files changed

+227
-28
lines changed

server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/state/TransportClusterStateActionDisruptionIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public void testNonLocalRequestAlwaysFindsMaster() throws Exception {
5252
runRepeatedlyWhileChangingMaster(() -> {
5353
final ClusterStateRequestBuilder clusterStateRequestBuilder = clusterAdmin().prepareState(TimeValue.timeValueMillis(100))
5454
.clear()
55+
.setWaitForMaster(true)
5556
.setNodes(true)
5657
.setBlocks(true);
5758
final ClusterStateResponse clusterStateResponse;
@@ -98,6 +99,7 @@ public void testNonLocalRequestAlwaysFindsMasterAndWaitsForMetadata() throws Exc
9899
.cluster()
99100
.prepareState(TimeValue.timeValueMillis(100))
100101
.clear()
102+
.setWaitForMaster(true)
101103
.setNodes(true)
102104
.setMetadata(true)
103105
.setBlocks(true)

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ public void testSimpleOnlyMasterNodeElection() throws IOException {
3737
logger.info("--> start data node / non master node");
3838
internalCluster().startNode(Settings.builder().put(dataOnlyNode()).put("discovery.initial_state_timeout", "1s"));
3939
try {
40-
assertThat(clusterAdmin().prepareState(TimeValue.timeValueMillis(100)).get().getState().nodes().getMasterNodeId(), nullValue());
40+
assertThat(
41+
clusterAdmin().prepareState(TimeValue.timeValueMillis(100))
42+
.setWaitForMaster(true)
43+
.get()
44+
.getState()
45+
.nodes()
46+
.getMasterNodeId(),
47+
nullValue()
48+
);
4149
fail("should not be able to find master");
4250
} catch (MasterNotDiscoveredException e) {
4351
// all is well, no master elected
@@ -49,6 +57,7 @@ public void testSimpleOnlyMasterNodeElection() throws IOException {
4957
.admin()
5058
.cluster()
5159
.prepareState(TEST_REQUEST_TIMEOUT)
60+
.setWaitForMaster(true)
5261
.get()
5362
.getState()
5463
.nodes()
@@ -74,7 +83,15 @@ public void testSimpleOnlyMasterNodeElection() throws IOException {
7483
internalCluster().stopCurrentMasterNode();
7584

7685
try {
77-
assertThat(clusterAdmin().prepareState(TimeValue.timeValueMillis(100)).get().getState().nodes().getMasterNodeId(), nullValue());
86+
assertThat(
87+
clusterAdmin().prepareState(TimeValue.timeValueMillis(100))
88+
.setWaitForMaster(true)
89+
.get()
90+
.getState()
91+
.nodes()
92+
.getMasterNodeId(),
93+
nullValue()
94+
);
7895
fail("should not be able to find master");
7996
} catch (MasterNotDiscoveredException e) {
8097
// all is well, no master elected
@@ -89,6 +106,7 @@ public void testSimpleOnlyMasterNodeElection() throws IOException {
89106
.admin()
90107
.cluster()
91108
.prepareState(TEST_REQUEST_TIMEOUT)
109+
.setWaitForMaster(true)
92110
.get()
93111
.getState()
94112
.nodes()
@@ -115,7 +133,15 @@ public void testElectOnlyBetweenMasterNodes() throws Exception {
115133
logger.info("--> start data node / non master node");
116134
internalCluster().startNode(Settings.builder().put(dataOnlyNode()).put("discovery.initial_state_timeout", "1s"));
117135
try {
118-
assertThat(clusterAdmin().prepareState(TimeValue.timeValueMillis(100)).get().getState().nodes().getMasterNodeId(), nullValue());
136+
assertThat(
137+
clusterAdmin().prepareState(TimeValue.timeValueMillis(100))
138+
.setWaitForMaster(true)
139+
.get()
140+
.getState()
141+
.nodes()
142+
.getMasterNodeId(),
143+
nullValue()
144+
);
119145
fail("should not be able to find master");
120146
} catch (MasterNotDiscoveredException e) {
121147
// all is well, no master elected
@@ -127,6 +153,7 @@ public void testElectOnlyBetweenMasterNodes() throws Exception {
127153
.admin()
128154
.cluster()
129155
.prepareState(TEST_REQUEST_TIMEOUT)
156+
.setWaitForMaster(true)
130157
.get()
131158
.getState()
132159
.nodes()
@@ -139,6 +166,7 @@ public void testElectOnlyBetweenMasterNodes() throws Exception {
139166
.admin()
140167
.cluster()
141168
.prepareState(TEST_REQUEST_TIMEOUT)
169+
.setWaitForMaster(true)
142170
.get()
143171
.getState()
144172
.nodes()
@@ -154,6 +182,7 @@ public void testElectOnlyBetweenMasterNodes() throws Exception {
154182
.admin()
155183
.cluster()
156184
.prepareState(TEST_REQUEST_TIMEOUT)
185+
.setWaitForMaster(true)
157186
.get()
158187
.getState()
159188
.nodes()
@@ -166,6 +195,7 @@ public void testElectOnlyBetweenMasterNodes() throws Exception {
166195
.admin()
167196
.cluster()
168197
.prepareState(TEST_REQUEST_TIMEOUT)
198+
.setWaitForMaster(true)
169199
.get()
170200
.getState()
171201
.nodes()
@@ -186,6 +216,7 @@ public void testElectOnlyBetweenMasterNodes() throws Exception {
186216
.admin()
187217
.cluster()
188218
.prepareState(TEST_REQUEST_TIMEOUT)
219+
.setWaitForMaster(true)
189220
.get()
190221
.getState()
191222
.nodes()
@@ -198,6 +229,7 @@ public void testElectOnlyBetweenMasterNodes() throws Exception {
198229
.admin()
199230
.cluster()
200231
.prepareState(TEST_REQUEST_TIMEOUT)
232+
.setWaitForMaster(true)
201233
.get()
202234
.getState()
203235
.nodes()
@@ -212,6 +244,7 @@ public void testElectOnlyBetweenMasterNodes() throws Exception {
212244
.admin()
213245
.cluster()
214246
.prepareState(TEST_REQUEST_TIMEOUT)
247+
.setWaitForMaster(true)
215248
.get()
216249
.getState()
217250
.nodes()
@@ -224,6 +257,7 @@ public void testElectOnlyBetweenMasterNodes() throws Exception {
224257
.admin()
225258
.cluster()
226259
.prepareState(TEST_REQUEST_TIMEOUT)
260+
.setWaitForMaster(true)
227261
.get()
228262
.getState()
229263
.nodes()

server/src/internalClusterTest/java/org/elasticsearch/readiness/ReadinessClusterIT.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,24 @@ protected Collection<Class<? extends Plugin>> getMockPlugins() {
112112

113113
private void assertMasterNode(Client client, String node) {
114114
assertThat(
115-
client.admin().cluster().prepareState(TEST_REQUEST_TIMEOUT).get().getState().nodes().getMasterNode().getName(),
115+
client.admin()
116+
.cluster()
117+
.prepareState(TEST_REQUEST_TIMEOUT)
118+
.setWaitForMaster(true)
119+
.get()
120+
.getState()
121+
.nodes()
122+
.getMasterNode()
123+
.getName(),
116124
equalTo(node)
117125
);
118126
}
119127

120128
private void expectMasterNotFound() {
121-
expectThrows(MasterNotDiscoveredException.class, clusterAdmin().prepareState(TimeValue.timeValueMillis(100)));
129+
expectThrows(
130+
MasterNotDiscoveredException.class,
131+
clusterAdmin().prepareState(TimeValue.timeValueMillis(100)).setWaitForMaster(true)
132+
);
122133
}
123134

124135
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/108613")
@@ -193,8 +204,6 @@ public Settings onNodeStopped(String nodeName) throws Exception {
193204
internalCluster().restartNode(masterNode, new InternalTestCluster.RestartCallback() {
194205
@Override
195206
public Settings onNodeStopped(String nodeName) throws Exception {
196-
expectMasterNotFound();
197-
198207
logger.info("--> master node [{}] stopped", nodeName);
199208

200209
for (String dataNode : dataNodes) {

server/src/internalClusterTest/java/org/elasticsearch/reservedstate/service/ComponentTemplatesFileSettingsIT.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,16 @@ public class ComponentTemplatesFileSettingsIT extends ESIntegTestCase {
357357

358358
private void assertMasterNode(Client client, String node) throws ExecutionException, InterruptedException {
359359
assertThat(
360-
client.admin().cluster().prepareState(TEST_REQUEST_TIMEOUT).execute().get().getState().nodes().getMasterNode().getName(),
360+
client.admin()
361+
.cluster()
362+
.prepareState(TEST_REQUEST_TIMEOUT)
363+
.setWaitForMaster(true)
364+
.execute()
365+
.get()
366+
.getState()
367+
.nodes()
368+
.getMasterNode()
369+
.getName(),
361370
equalTo(node)
362371
);
363372
}

server/src/internalClusterTest/java/org/elasticsearch/reservedstate/service/FileSettingsServiceIT.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,15 @@ public void resetVersionCounter() {
127127

128128
private void assertMasterNode(Client client, String node) {
129129
assertThat(
130-
client.admin().cluster().prepareState(TEST_REQUEST_TIMEOUT).get().getState().nodes().getMasterNode().getName(),
130+
client.admin()
131+
.cluster()
132+
.prepareState(TEST_REQUEST_TIMEOUT)
133+
.setWaitForMaster(true)
134+
.get()
135+
.getState()
136+
.nodes()
137+
.getMasterNode()
138+
.getName(),
131139
equalTo(node)
132140
);
133141
}

server/src/internalClusterTest/java/org/elasticsearch/reservedstate/service/RepositoriesFileSettingsIT.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,16 @@ public class RepositoriesFileSettingsIT extends ESIntegTestCase {
9494

9595
private void assertMasterNode(Client client, String node) throws ExecutionException, InterruptedException {
9696
assertThat(
97-
client.admin().cluster().prepareState(TEST_REQUEST_TIMEOUT).execute().get().getState().nodes().getMasterNode().getName(),
97+
client.admin()
98+
.cluster()
99+
.prepareState(TEST_REQUEST_TIMEOUT)
100+
.setWaitForMaster(true)
101+
.execute()
102+
.get()
103+
.getState()
104+
.nodes()
105+
.getMasterNode()
106+
.getName(),
98107
equalTo(node)
99108
);
100109
}

server/src/main/java/org/elasticsearch/action/admin/cluster/state/ClusterStateRequestBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,9 @@ public ClusterStateRequestBuilder setWaitForTimeOut(TimeValue waitForTimeout) {
108108
request.waitForTimeout(waitForTimeout);
109109
return this;
110110
}
111+
112+
public ClusterStateRequestBuilder setWaitForMaster(boolean waitForMaster) {
113+
request.waitForMaster(waitForMaster);
114+
return this;
115+
}
111116
}

server/src/main/java/org/elasticsearch/action/support/local/LocalClusterStateRequest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public abstract class LocalClusterStateRequest extends ActionRequest {
3232
*/
3333
private final TimeValue masterTimeout;
3434

35+
private boolean waitForMaster = false;
36+
3537
protected LocalClusterStateRequest(TimeValue masterTimeout) {
3638
this.masterTimeout = Objects.requireNonNull(masterTimeout);
3739
}
@@ -74,4 +76,13 @@ public ActionRequestValidationException validate() {
7476
public TimeValue masterTimeout() {
7577
return masterTimeout;
7678
}
79+
80+
public LocalClusterStateRequest waitForMaster(boolean waitForMaster) {
81+
this.waitForMaster = waitForMaster;
82+
return this;
83+
}
84+
85+
public boolean waitForMaster() {
86+
return waitForMaster;
87+
}
7788
}

0 commit comments

Comments
 (0)