Skip to content

Commit cce7dc2

Browse files
authored
Smaller aesthetic fixes to InternalTestCluster (#31831)
Allows cluster to auto-reconfigure faster by starting up nodes in parallel.
1 parent 450a450 commit cce7dc2

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

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

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,6 @@ public InternalTestCluster(long clusterSeed, Path baseDir,
263263
this.nodePrefix = nodePrefix;
264264

265265
assert nodePrefix != null;
266-
ArrayList<Class<? extends Plugin>> tmpMockPlugins = new ArrayList<>(mockPlugins);
267-
268266

269267
this.mockPlugins = mockPlugins;
270268

@@ -458,14 +456,9 @@ private synchronized NodeAndClient getRandomNodeAndClient() {
458456

459457
private synchronized NodeAndClient getRandomNodeAndClient(Predicate<NodeAndClient> predicate) {
460458
ensureOpen();
461-
Collection<NodeAndClient> values = nodes.values().stream().filter(predicate).collect(Collectors.toCollection(ArrayList::new));
462-
if (!values.isEmpty()) {
463-
int whichOne = random.nextInt(values.size());
464-
for (NodeAndClient nodeAndClient : values) {
465-
if (whichOne-- == 0) {
466-
return nodeAndClient;
467-
}
468-
}
459+
List<NodeAndClient> values = nodes.values().stream().filter(predicate).collect(Collectors.toList());
460+
if (values.isEmpty() == false) {
461+
return randomFrom(random, values);
469462
}
470463
return null;
471464
}
@@ -476,18 +469,14 @@ private synchronized NodeAndClient getRandomNodeAndClient(Predicate<NodeAndClien
476469
* stop any of the running nodes.
477470
*/
478471
public synchronized void ensureAtLeastNumDataNodes(int n) {
479-
boolean added = false;
480472
int size = numDataNodes();
481-
for (int i = size; i < n; i++) {
473+
if (size < n) {
482474
logger.info("increasing cluster size from {} to {}", size, n);
483-
added = true;
484475
if (numSharedDedicatedMasterNodes > 0) {
485-
startDataOnlyNode(Settings.EMPTY);
476+
startDataOnlyNodes(n - size);
486477
} else {
487-
startNode(Settings.EMPTY);
478+
startNodes(n - size);
488479
}
489-
}
490-
if (added) {
491480
validateClusterFormed();
492481
}
493482
}
@@ -1361,8 +1350,9 @@ private synchronized void startAndPublishNodesAndClients(List<NodeAndClient> nod
13611350
.filter(nac -> nodes.containsKey(nac.name) == false) // filter out old masters
13621351
.count();
13631352
final int currentMasters = getMasterNodesCount();
1364-
if (autoManageMinMasterNodes && currentMasters > 1 && newMasters > 0) {
1365-
// special case for 1 node master - we can't update the min master nodes before we add more nodes.
1353+
if (autoManageMinMasterNodes && currentMasters > 0 && newMasters > 0 &&
1354+
getMinMasterNodes(currentMasters + newMasters) <= currentMasters) {
1355+
// if we're adding too many master-eligible nodes at once, we can't update the min master setting before adding the nodes.
13661356
updateMinMasterNodes(currentMasters + newMasters);
13671357
}
13681358
List<Future<?>> futures = nodeAndClients.stream().map(node -> executor.submit(node::startNode)).collect(Collectors.toList());
@@ -1377,7 +1367,8 @@ private synchronized void startAndPublishNodesAndClients(List<NodeAndClient> nod
13771367
}
13781368
nodeAndClients.forEach(this::publishNode);
13791369

1380-
if (autoManageMinMasterNodes && currentMasters == 1 && newMasters > 0) {
1370+
if (autoManageMinMasterNodes && currentMasters > 0 && newMasters > 0 &&
1371+
getMinMasterNodes(currentMasters + newMasters) > currentMasters) {
13811372
// update once masters have joined
13821373
validateClusterFormed();
13831374
updateMinMasterNodes(currentMasters + newMasters);
@@ -1632,27 +1623,24 @@ public synchronized Set<String> nodesInclude(String index) {
16321623
}
16331624

16341625
/**
1635-
* Starts a node with default settings and returns it's name.
1626+
* Starts a node with default settings and returns its name.
16361627
*/
16371628
public synchronized String startNode() {
16381629
return startNode(Settings.EMPTY);
16391630
}
16401631

16411632
/**
1642-
* Starts a node with the given settings builder and returns it's name.
1633+
* Starts a node with the given settings builder and returns its name.
16431634
*/
16441635
public synchronized String startNode(Settings.Builder settings) {
16451636
return startNode(settings.build());
16461637
}
16471638

16481639
/**
1649-
* Starts a node with the given settings and returns it's name.
1640+
* Starts a node with the given settings and returns its name.
16501641
*/
16511642
public synchronized String startNode(Settings settings) {
1652-
final int defaultMinMasterNodes = getMinMasterNodes(getMasterNodesCount() + (Node.NODE_MASTER_SETTING.get(settings) ? 1 : 0));
1653-
NodeAndClient buildNode = buildNode(settings, defaultMinMasterNodes);
1654-
startAndPublishNodesAndClients(Collections.singletonList(buildNode));
1655-
return buildNode.name;
1643+
return startNodes(settings).get(0);
16561644
}
16571645

16581646
/**

0 commit comments

Comments
 (0)