Skip to content

Commit 659f8c6

Browse files
authored
Allow getting the service instance by a node role (#96926)
This changes makes it possible to retreive a reference to the service from the random node that has a provided role
1 parent dbe98c1 commit 659f8c6

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,13 @@ public final class InternalTestCluster extends TestCluster {
180180
nodeAndClient.node.settings()
181181
);
182182

183-
private static final Predicate<NodeAndClient> NO_DATA_NO_MASTER_PREDICATE = nodeAndClient -> DiscoveryNode.isMasterNode(
184-
nodeAndClient.node.settings()
185-
) == false && DiscoveryNode.canContainData(nodeAndClient.node.settings()) == false;
186-
187183
private static final Predicate<NodeAndClient> MASTER_NODE_PREDICATE = nodeAndClient -> DiscoveryNode.isMasterNode(
188184
nodeAndClient.node.settings()
189185
);
190186

187+
private static final Predicate<NodeAndClient> NO_DATA_NO_MASTER_PREDICATE = DATA_NODE_PREDICATE.negate()
188+
.and(MASTER_NODE_PREDICATE.negate());
189+
191190
public static final int DEFAULT_LOW_NUM_MASTER_NODES = 1;
192191
public static final int DEFAULT_HIGH_NUM_MASTER_NODES = 3;
193192

@@ -1585,10 +1584,17 @@ private <T> Iterable<T> getInstances(Class<T> clazz, Predicate<NodeAndClient> pr
15851584
}
15861585

15871586
/**
1588-
* Returns a reference to the given nodes instances of the given class &gt;T&lt;
1587+
* @return the instance of the given class from the node with provided {@code nodeName}
1588+
*/
1589+
public <T> T getInstance(Class<T> clazz, final String nodeName) {
1590+
return getInstance(clazz, nc -> nodeName == null || nodeName.equals(nc.name));
1591+
}
1592+
1593+
/**
1594+
* @return the instance of the given class from a random node with provided {@code role}
15891595
*/
1590-
public <T> T getInstance(Class<T> clazz, final String node) {
1591-
return getInstance(clazz, nc -> node == null || node.equals(nc.name));
1596+
public <T> T getInstance(Class<T> clazz, DiscoveryNodeRole role) {
1597+
return getInstance(clazz, nc -> DiscoveryNode.getRolesFromSettings(nc.node.settings()).contains(role));
15921598
}
15931599

15941600
public <T> T getDataNodeInstance(Class<T> clazz) {

0 commit comments

Comments
 (0)