Skip to content

Commit 106bed9

Browse files
PnPieDaveCTurner
authored andcommitted
Add coordinating_only node selector (#30313)
Today we can execute cluster API actions on only master, data or ingest nodes using the `master:true`, `data:true` and `ingest:true` filters, but it is not so easy to select coordinating-only nodes (i.e. those nodes that are neither master nor data nor ingest nodes). This change fixes this by adding support for a `coordinating_only` filter such that `coordinating_only:true` adds all coordinating-only nodes to the set of selected nodes, and `coordinating_only:false` deletes them. Resolves #28831.
1 parent 92a0b0a commit 106bed9

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

docs/CHANGELOG.asciidoc

+3
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ started or stopped. ({pull}30118[#30118])
176176

177177
Added put index template API to the high level rest client ({pull}30400[#30400])
178178

179+
Add ability to filter coordinating-only nodes when interacting with cluster
180+
APIs. ({pull}30313[#30313])
181+
179182
[float]
180183
=== Bug Fixes
181184

server/src/main/java/org/elasticsearch/cluster/node/DiscoveryNode.java

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
*/
4545
public class DiscoveryNode implements Writeable, ToXContentFragment {
4646

47+
static final String COORDINATING_ONLY = "coordinating_only";
48+
4749
public static boolean nodeRequiresLocalStorage(Settings settings) {
4850
boolean localStorageEnable = Node.NODE_LOCAL_STORAGE_SETTING.get(settings);
4951
if (localStorageEnable == false &&

server/src/main/java/org/elasticsearch/cluster/node/DiscoveryNodes.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,19 @@ public ImmutableOpenMap<String, DiscoveryNode> getMasterAndDataNodes() {
148148
return nodes.build();
149149
}
150150

151+
/**
152+
* Get a {@link Map} of the coordinating only nodes (nodes which are neither master, nor data, nor ingest nodes) arranged by their ids
153+
*
154+
* @return {@link Map} of the coordinating only nodes arranged by their ids
155+
*/
156+
public ImmutableOpenMap<String, DiscoveryNode> getCoordinatingOnlyNodes() {
157+
ImmutableOpenMap.Builder<String, DiscoveryNode> nodes = ImmutableOpenMap.builder(this.nodes);
158+
nodes.removeAll(masterNodes.keys());
159+
nodes.removeAll(dataNodes.keys());
160+
nodes.removeAll(ingestNodes.keys());
161+
return nodes.build();
162+
}
163+
151164
/**
152165
* Get a node by its id
153166
*
@@ -297,7 +310,7 @@ public DiscoveryNode resolveNode(String node) {
297310
* - "_local" or "_master" for the relevant nodes
298311
* - a node id
299312
* - a wild card pattern that will be matched against node names
300-
* - a "attr:value" pattern, where attr can be a node role (master, data, ingest etc.) in which case the value can be true of false
313+
* - a "attr:value" pattern, where attr can be a node role (master, data, ingest etc.) in which case the value can be true or false,
301314
* or a generic node attribute name in which case value will be treated as a wildcard and matched against the node attribute values.
302315
*/
303316
public String[] resolveNodes(String... nodes) {
@@ -349,6 +362,12 @@ public String[] resolveNodes(String... nodes) {
349362
} else {
350363
resolvedNodesIds.removeAll(ingestNodes.keys());
351364
}
365+
} else if (DiscoveryNode.COORDINATING_ONLY.equals(matchAttrName)) {
366+
if (Booleans.parseBoolean(matchAttrValue, true)) {
367+
resolvedNodesIds.addAll(getCoordinatingOnlyNodes().keys());
368+
} else {
369+
resolvedNodesIds.removeAll(getCoordinatingOnlyNodes().keys());
370+
}
352371
} else {
353372
for (DiscoveryNode node : this) {
354373
for (Map.Entry<String, String> entry : node.getAttributes().entrySet()) {

server/src/test/java/org/elasticsearch/cluster/node/DiscoveryNodesTests.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,17 @@ public void testCoordinatorOnlyNodes() {
102102
.map(DiscoveryNode::getId)
103103
.toArray(String[]::new);
104104

105-
assertThat(
106-
discoveryNodes.resolveNodes("_all", "data:false", "ingest:false", "master:false"),
107-
arrayContainingInAnyOrder(coordinatorOnlyNodes));
105+
final String[] nonCoordinatorOnlyNodes =
106+
StreamSupport.stream(discoveryNodes.getNodes().values().spliterator(), false)
107+
.map(n -> n.value)
108+
.filter(n -> n.isMasterNode() || n.isDataNode() || n.isIngestNode())
109+
.map(DiscoveryNode::getId)
110+
.toArray(String[]::new);
111+
112+
assertThat(discoveryNodes.resolveNodes("coordinating_only:true"), arrayContainingInAnyOrder(coordinatorOnlyNodes));
113+
assertThat(discoveryNodes.resolveNodes("_all", "data:false", "ingest:false", "master:false"),
114+
arrayContainingInAnyOrder(coordinatorOnlyNodes));
115+
assertThat(discoveryNodes.resolveNodes("_all", "coordinating_only:false"), arrayContainingInAnyOrder(nonCoordinatorOnlyNodes));
108116
}
109117

110118
public void testResolveNodesIds() {
@@ -275,6 +283,13 @@ Set<String> matchingNodeIds(DiscoveryNodes nodes) {
275283
nodes.getIngestNodes().keysIt().forEachRemaining(ids::add);
276284
return ids;
277285
}
286+
}, COORDINATING_ONLY(DiscoveryNode.COORDINATING_ONLY + ":true") {
287+
@Override
288+
Set<String> matchingNodeIds(DiscoveryNodes nodes) {
289+
Set<String> ids = new HashSet<>();
290+
nodes.getCoordinatingOnlyNodes().keysIt().forEachRemaining(ids::add);
291+
return ids;
292+
}
278293
}, CUSTOM_ATTRIBUTE("attr:value") {
279294
@Override
280295
Set<String> matchingNodeIds(DiscoveryNodes nodes) {

0 commit comments

Comments
 (0)