Skip to content

Commit d269e47

Browse files
Ran ./gradlew spotlessApply precommit
1 parent dac102a commit d269e47

File tree

6 files changed

+127
-106
lines changed

6 files changed

+127
-106
lines changed

server/src/internalClusterTest/java/org/elasticsearch/cluster/coordination/MasterElectionTestCase.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
import java.util.concurrent.CountDownLatch;
2323
import java.util.concurrent.CyclicBarrier;
2424

25+
/**
26+
* An integration test base class to be used when a test requires a master re-election
27+
*/
2528
public abstract class MasterElectionTestCase extends ESIntegTestCase {
29+
2630
/**
2731
* Block the cluster state applier on a node. Returns only when applier is blocked.
2832
*
@@ -73,17 +77,21 @@ protected CountDownLatch configureElectionLatchForNewMaster(String newMaster, Li
7377
* Configure a latch that will be released when the existing master knows it has been re-elected
7478
*
7579
* @param masterNodeName The name of the current master node
76-
* @param originalTerm The term the current master node was elected
80+
* @param electedTerm The term the current master node was elected
7781
* @param cleanupTasks The list of cleanup tasks
7882
* @return A latch that will be released when the master acknowledges it's re-election
7983
*/
80-
protected CountDownLatch configureElectionLatchForReElectedMaster(String masterNodeName, long originalTerm, List<Releasable> cleanupTasks) {
84+
protected CountDownLatch configureElectionLatchForReElectedMaster(
85+
String masterNodeName,
86+
long electedTerm,
87+
List<Releasable> cleanupTasks
88+
) {
8189
final var masterKnowsItIsReElectedLatch = new CountDownLatch(1);
8290
ClusterStateApplier newMasterMonitor = event -> {
8391
DiscoveryNode masterNode = event.state().nodes().getMasterNode();
8492
long currentTerm = event.state().coordinationMetadata().term();
85-
if (masterNode != null && masterNode.getName().equals(masterNodeName) && currentTerm > originalTerm) {
86-
logger.info("Carrot: Master knows it's reelected. term:{}, version:{}", currentTerm, event.state().getVersion());
93+
if (masterNode != null && masterNode.getName().equals(masterNodeName) && currentTerm > electedTerm) {
94+
logger.info("Master knows it's re-elected");
8795
masterKnowsItIsReElectedLatch.countDown();
8896
}
8997
};

server/src/internalClusterTest/java/org/elasticsearch/cluster/coordination/NodeJoiningIT.java

Lines changed: 80 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ public class NodeJoiningIT extends MasterElectionTestCase {
4141

4242
@Override
4343
protected Collection<Class<? extends Plugin>> nodePlugins() {
44-
return CollectionUtils.appendToCopyNoNullElements(
45-
super.nodePlugins(),
46-
MockTransportService.TestPlugin.class
47-
);
44+
return CollectionUtils.appendToCopyNoNullElements(super.nodePlugins(), MockTransportService.TestPlugin.class);
4845
}
4946

5047
@Override
@@ -123,11 +120,10 @@ public void testNodeTriesToJoinClusterAndThenDifferentMasterIsElected() {
123120
if (mockTransportService.getLocalNode().getName().equals(newMasterNodeName) == false) {
124121
mockTransportService.addSendBehavior((connection, requestId, action, request, options) -> {
125122
if (
126-
// This disables pre-voting on all nodes except the new master, forcing it to win the election
127-
action.equals(StatefulPreVoteCollector.REQUEST_PRE_VOTE_ACTION_NAME)
123+
// This disables pre-voting on all nodes except the new master, forcing it to win the election
124+
action.equals(StatefulPreVoteCollector.REQUEST_PRE_VOTE_ACTION_NAME)
128125
// This forces the current master node to fail
129-
|| action.equals(PublicationTransportHandler.PUBLISH_STATE_ACTION_NAME)
130-
) {
126+
|| action.equals(PublicationTransportHandler.PUBLISH_STATE_ACTION_NAME)) {
131127
throw new ElasticsearchException("[{}] for [{}] denied", action, connection.getNode());
132128
} else {
133129
connection.sendRequest(requestId, action, request, options);
@@ -179,8 +175,12 @@ public void testNodeTriesToJoinClusterAndThenDifferentMasterIsElected() {
179175

180176
// Tests whether a WARN log is thrown when a node attempts to join a cluster, and then the same master node is re-elected (#126192)
181177
@TestLogging(
182-
reason = "test includes assertions about logging",
183-
value = "org.elasticsearch.cluster.coordination.NodeJoinExecutor:WARN,org.elasticsearch.cluster.coordination.NodeJoinExecutor:INFO,org.elasticsearch.cluster.coordination.MasterService:WARN,org.elasticsearch.cluster.coordination.MasterService:INFO,org.elasticsearch.cluster.coordination.ClusterApplierService:WARN"
178+
reason = "test includes assertions about logging",
179+
value = "org.elasticsearch.cluster.coordination.NodeJoinExecutor:WARN,"
180+
+ "org.elasticsearch.cluster.coordination.NodeJoinExecutor:INFO,"
181+
+ "org.elasticsearch.cluster.coordination.MasterService:WARN,"
182+
+ "org.elasticsearch.cluster.coordination.MasterService:INFO,"
183+
+ "org.elasticsearch.cluster.coordination.ClusterApplierService:WARN"
184184
)
185185
public void testNodeTriesToJoinClusterAndThenSameMasterIsElected_DoesNotIncludeWarnLog() {
186186
final var cleanupTasks = new ArrayList<Releasable>();
@@ -210,10 +210,18 @@ public void testNodeTriesToJoinClusterAndThenSameMasterIsElected_DoesNotIncludeW
210210
});
211211

212212
// Latch to remove publishing ban to allow re-election
213-
CountDownLatch publishingBanRemovedLatch = removeMockTransportServicePublishBanWhenMasterHasSteppedDown(masterNodeName, masterNodeTransportService, cleanupTasks);
213+
CountDownLatch publishingBanRemovedLatch = removeMockTransportServicePublishBanWhenMasterHasSteppedDown(
214+
masterNodeName,
215+
masterNodeTransportService,
216+
cleanupTasks
217+
);
214218

215219
// A CountDownLatch that only gets decremented when the first master node is re-elected
216-
final var masterKnowsItHasBeenReElectedLatch = configureElectionLatchForReElectedMaster(masterNodeName, originalTerm, cleanupTasks);
220+
final var masterKnowsItHasBeenReElectedLatch = configureElectionLatchForReElectedMaster(
221+
masterNodeName,
222+
originalTerm,
223+
cleanupTasks
224+
);
217225

218226
for (String nodeName : internalCluster().getNodeNames()) {
219227
final var mockTransportService = MockTransportService.getInstance(nodeName);
@@ -280,9 +288,9 @@ public void testNodeTriesToJoinClusterAndThenSameMasterIsElected_DoesNotIncludeW
280288
// Await for N to be in the cluster state of all nodes
281289
for (String nodeName : namesOfAllNodesInOriginalCluster) {
282290
ClusterServiceUtils.awaitClusterState(
283-
logger,
284-
clusterState -> clusterState.nodes().nodeExistsWithName(newNodeName),
285-
internalCluster().clusterService(nodeName)
291+
logger,
292+
clusterState -> clusterState.nodes().nodeExistsWithName(newNodeName),
293+
internalCluster().clusterService(nodeName)
286294
);
287295
}
288296
} catch (Exception e) {
@@ -309,10 +317,8 @@ public void testNodeTriesToJoinClusterAndThenSameMasterIsElected_DoesNotIncludeW
309317
}
310318
}
311319

312-
313-
private List<String> getListOfDataNodeNamesFromCluster(String nodeName){
314-
return internalCluster()
315-
.clusterService(nodeName)
320+
private List<String> getListOfDataNodeNamesFromCluster(String nodeName) {
321+
return internalCluster().clusterService(nodeName)
316322
.state()
317323
.getNodes()
318324
.getDataNodes()
@@ -336,12 +342,7 @@ public void match(LogEvent event) {
336342
return;
337343
}
338344

339-
Pattern pattern = Pattern.compile(
340-
"node-join: \\["
341-
+ expectedNewNodeAsString
342-
+ "] "
343-
+ "with reason \\[joining]"
344-
);
345+
Pattern pattern = Pattern.compile("node-join: \\[" + expectedNewNodeAsString + "] " + "with reason \\[joining]");
345346
Matcher matcher = pattern.matcher(event.getMessage().getFormattedMessage());
346347

347348
if (matcher.find()) {
@@ -370,11 +371,15 @@ public void match(LogEvent event) {
370371
}
371372

372373
Pattern pattern = Pattern.compile(
373-
"node-join\\["
374+
"node-join\\["
374375
+ expectedNewNodeAsString
375376
+ " joining],"
376-
+ " term: " + term + ","
377-
+ " version: " + version + ","
377+
+ " term: "
378+
+ term
379+
+ ","
380+
+ " version: "
381+
+ version
382+
+ ","
378383
+ " delta: added \\{"
379384
+ expectedNewNodeAsString
380385
+ "}"
@@ -407,9 +412,11 @@ public void match(LogEvent event) {
407412
}
408413

409414
Pattern pattern = Pattern.compile(
410-
"failing \\[node-join\\["
411-
+ expectedNewNodeAsString
412-
+ " joining]]: failed to commit cluster state version \\[" + version +"]"
415+
"failing \\[node-join\\["
416+
+ expectedNewNodeAsString
417+
+ " joining]]: failed to commit cluster state version \\["
418+
+ version
419+
+ "]"
413420
);
414421
Matcher matcher = pattern.matcher(event.getMessage().getFormattedMessage());
415422

@@ -440,13 +447,14 @@ public void match(LogEvent event) {
440447

441448
String regexToMatchAnyCharacterExceptClosingBrace = "([^}]+)";
442449
Pattern pattern = Pattern.compile(
443-
"node-join: \\["
444-
+ expectedNewNodeAsString
445-
+ "] "
446-
+ "with reason \\[joining, removed \\["
447-
+ regexToMatchAnyCharacterExceptClosingBrace
448-
+ "] ago with reason \\[disconnected]]; "
449-
+ "for troubleshooting guidance, see https://www.elastic.co/docs/troubleshoot/elasticsearch/troubleshooting-unstable-cluster\\?version=master"
450+
"node-join: \\["
451+
+ expectedNewNodeAsString
452+
+ "] "
453+
+ "with reason \\[joining, removed \\["
454+
+ regexToMatchAnyCharacterExceptClosingBrace
455+
+ "] ago with reason \\[disconnected]]; "
456+
+ "for troubleshooting guidance, see "
457+
+ "https://www.elastic.co/docs/troubleshoot/elasticsearch/troubleshooting-unstable-cluster\\?version=master"
450458
);
451459
Matcher matcher = pattern.matcher(event.getMessage().getFormattedMessage());
452460

@@ -462,7 +470,10 @@ public void assertMatched() {
462470
});
463471
}
464472

465-
private void addNodeJoinProcessedDuringNewElectionAndClusterStatePublicationExpectation(MockLog mockLog, String expectedNewNodeAsString) {
473+
private void addNodeJoinProcessedDuringNewElectionAndClusterStatePublicationExpectation(
474+
MockLog mockLog,
475+
String expectedNewNodeAsString
476+
) {
466477
mockLog.addExpectation(new MockLog.LoggingExpectation() {
467478
boolean matched = false;
468479

@@ -475,11 +486,7 @@ public void match(LogEvent event) {
475486
return;
476487
}
477488

478-
Pattern pattern = Pattern.compile(
479-
"added \\{"
480-
+ expectedNewNodeAsString
481-
+ "}"
482-
);
489+
Pattern pattern = Pattern.compile("added \\{" + expectedNewNodeAsString + "}");
483490
Matcher matcher = pattern.matcher(event.getMessage().getFormattedMessage());
484491

485492
if (matcher.find()) {
@@ -500,15 +507,31 @@ private String generateNodeDescriptionForNewDiscoveryNode(int numberOfNodesOrigi
500507
String newNodeName = "node_s" + numberOfNodesOriginallyInCluster;
501508
String regexToMatchAnyCharacterExceptClosingBrace = "([^}]+)";
502509

503-
return "\\{" + newNodeName + "}"
504-
+ "\\{" + regexToMatchAnyCharacterExceptClosingBrace + "}"
505-
+ "\\{" + regexToMatchAnyCharacterExceptClosingBrace + "}"
506-
+ "\\{" + newNodeName + "}"
507-
+ "\\{" + masterNode.getHostAddress() + "}"
508-
+ "\\{" + masterNode.getHostAddress() + ":\\d+}"
510+
return "\\{"
511+
+ newNodeName
512+
+ "}"
513+
+ "\\{"
514+
+ regexToMatchAnyCharacterExceptClosingBrace
515+
+ "}"
516+
+ "\\{"
517+
+ regexToMatchAnyCharacterExceptClosingBrace
518+
+ "}"
519+
+ "\\{"
520+
+ newNodeName
521+
+ "}"
522+
+ "\\{"
523+
+ masterNode.getHostAddress()
524+
+ "}"
525+
+ "\\{"
526+
+ masterNode.getHostAddress()
527+
+ ":\\d+}"
509528
+ "\\{d}"
510-
+ "\\{" + masterNode.getVersion() + "}"
511-
+ "\\{" + regexToMatchAnyCharacterExceptClosingBrace + "}";
529+
+ "\\{"
530+
+ masterNode.getVersion()
531+
+ "}"
532+
+ "\\{"
533+
+ regexToMatchAnyCharacterExceptClosingBrace
534+
+ "}";
512535
}
513536

514537
/**
@@ -518,7 +541,11 @@ private String generateNodeDescriptionForNewDiscoveryNode(int numberOfNodesOrigi
518541
* @param mockTransportService The transport service to remove the `addSendBehavior` from
519542
* @param cleanupTasks The list of cleanup tasks
520543
*/
521-
protected CountDownLatch removeMockTransportServicePublishBanWhenMasterHasSteppedDown(String masterNodeName, MockTransportService mockTransportService, List<Releasable> cleanupTasks) {
544+
protected CountDownLatch removeMockTransportServicePublishBanWhenMasterHasSteppedDown(
545+
String masterNodeName,
546+
MockTransportService mockTransportService,
547+
List<Releasable> cleanupTasks
548+
) {
522549
CountDownLatch latch = new CountDownLatch(1);
523550
ClusterStateApplier newMasterMonitor = event -> {
524551
DiscoveryNode masterNode = event.state().nodes().getMasterNode();

0 commit comments

Comments
 (0)