Skip to content

Commit 8b73ceb

Browse files
committed
Smaller aesthetic fixes to InternalTestCluster (#31831)
Allows cluster to auto-reconfigure faster by starting up nodes in parallel.
1 parent f21977b commit 8b73ceb

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
@@ -265,8 +265,6 @@ public InternalTestCluster(long clusterSeed, Path baseDir,
265265
this.nodePrefix = nodePrefix;
266266

267267
assert nodePrefix != null;
268-
ArrayList<Class<? extends Plugin>> tmpMockPlugins = new ArrayList<>(mockPlugins);
269-
270268

271269
this.mockPlugins = mockPlugins;
272270

@@ -461,14 +459,9 @@ private synchronized NodeAndClient getRandomNodeAndClient() {
461459

462460
private synchronized NodeAndClient getRandomNodeAndClient(Predicate<NodeAndClient> predicate) {
463461
ensureOpen();
464-
Collection<NodeAndClient> values = nodes.values().stream().filter(predicate).collect(Collectors.toCollection(ArrayList::new));
465-
if (!values.isEmpty()) {
466-
int whichOne = random.nextInt(values.size());
467-
for (NodeAndClient nodeAndClient : values) {
468-
if (whichOne-- == 0) {
469-
return nodeAndClient;
470-
}
471-
}
462+
List<NodeAndClient> values = nodes.values().stream().filter(predicate).collect(Collectors.toList());
463+
if (values.isEmpty() == false) {
464+
return randomFrom(random, values);
472465
}
473466
return null;
474467
}
@@ -479,18 +472,14 @@ private synchronized NodeAndClient getRandomNodeAndClient(Predicate<NodeAndClien
479472
* stop any of the running nodes.
480473
*/
481474
public synchronized void ensureAtLeastNumDataNodes(int n) {
482-
boolean added = false;
483475
int size = numDataNodes();
484-
for (int i = size; i < n; i++) {
476+
if (size < n) {
485477
logger.info("increasing cluster size from {} to {}", size, n);
486-
added = true;
487478
if (numSharedDedicatedMasterNodes > 0) {
488-
startDataOnlyNode(Settings.EMPTY);
479+
startDataOnlyNodes(n - size);
489480
} else {
490-
startNode(Settings.EMPTY);
481+
startNodes(n - size);
491482
}
492-
}
493-
if (added) {
494483
validateClusterFormed();
495484
}
496485
}
@@ -1364,8 +1353,9 @@ private synchronized void startAndPublishNodesAndClients(List<NodeAndClient> nod
13641353
.filter(nac -> nodes.containsKey(nac.name) == false) // filter out old masters
13651354
.count();
13661355
final int currentMasters = getMasterNodesCount();
1367-
if (autoManageMinMasterNodes && currentMasters > 1 && newMasters > 0) {
1368-
// special case for 1 node master - we can't update the min master nodes before we add more nodes.
1356+
if (autoManageMinMasterNodes && currentMasters > 0 && newMasters > 0 &&
1357+
getMinMasterNodes(currentMasters + newMasters) <= currentMasters) {
1358+
// if we're adding too many master-eligible nodes at once, we can't update the min master setting before adding the nodes.
13691359
updateMinMasterNodes(currentMasters + newMasters);
13701360
}
13711361
List<Future<?>> futures = nodeAndClients.stream().map(node -> executor.submit(node::startNode)).collect(Collectors.toList());
@@ -1380,7 +1370,8 @@ private synchronized void startAndPublishNodesAndClients(List<NodeAndClient> nod
13801370
}
13811371
nodeAndClients.forEach(this::publishNode);
13821372

1383-
if (autoManageMinMasterNodes && currentMasters == 1 && newMasters > 0) {
1373+
if (autoManageMinMasterNodes && currentMasters > 0 && newMasters > 0 &&
1374+
getMinMasterNodes(currentMasters + newMasters) > currentMasters) {
13841375
// update once masters have joined
13851376
validateClusterFormed();
13861377
updateMinMasterNodes(currentMasters + newMasters);
@@ -1635,27 +1626,24 @@ public synchronized Set<String> nodesInclude(String index) {
16351626
}
16361627

16371628
/**
1638-
* Starts a node with default settings and returns it's name.
1629+
* Starts a node with default settings and returns its name.
16391630
*/
16401631
public synchronized String startNode() {
16411632
return startNode(Settings.EMPTY);
16421633
}
16431634

16441635
/**
1645-
* Starts a node with the given settings builder and returns it's name.
1636+
* Starts a node with the given settings builder and returns its name.
16461637
*/
16471638
public synchronized String startNode(Settings.Builder settings) {
16481639
return startNode(settings.build());
16491640
}
16501641

16511642
/**
1652-
* Starts a node with the given settings and returns it's name.
1643+
* Starts a node with the given settings and returns its name.
16531644
*/
16541645
public synchronized String startNode(Settings settings) {
1655-
final int defaultMinMasterNodes = getMinMasterNodes(getMasterNodesCount() + (Node.NODE_MASTER_SETTING.get(settings) ? 1 : 0));
1656-
NodeAndClient buildNode = buildNode(settings, defaultMinMasterNodes);
1657-
startAndPublishNodesAndClients(Collections.singletonList(buildNode));
1658-
return buildNode.name;
1646+
return startNodes(settings).get(0);
16591647
}
16601648

16611649
/**

0 commit comments

Comments
 (0)