Skip to content

Commit d3af535

Browse files
committed
Remove special handling for _all in nodes info
Today when requesting _all we return all nodes regardless of what other node qualifiers are in the request. This is contrary to how the remainder of the API behaves which acts as additive and subtractive based on the qualifiers and their ordering. It is also contrary to how the wildcard * behaves. This commit removes the special handling for _all so that it behaves identical to the wildcard *. Relates #28971
1 parent 3861959 commit d3af535

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

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

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.Iterator;
4040
import java.util.List;
4141
import java.util.Map;
42+
import java.util.stream.StreamSupport;
4243

4344
/**
4445
* This class holds all {@link DiscoveryNode} in the cluster and provides convenience methods to
@@ -229,10 +230,6 @@ public DiscoveryNode findByAddress(TransportAddress address) {
229230
return null;
230231
}
231232

232-
public boolean isAllNodes(String... nodesIds) {
233-
return nodesIds == null || nodesIds.length == 0 || (nodesIds.length == 1 && nodesIds[0].equals("_all"));
234-
}
235-
236233
/**
237234
* Returns the version of the node with the oldest version in the cluster that is not a client node
238235
*
@@ -301,13 +298,8 @@ public DiscoveryNode resolveNode(String node) {
301298
* or a generic node attribute name in which case value will be treated as a wildcard and matched against the node attribute values.
302299
*/
303300
public String[] resolveNodes(String... nodes) {
304-
if (isAllNodes(nodes)) {
305-
int index = 0;
306-
nodes = new String[this.nodes.size()];
307-
for (DiscoveryNode node : this) {
308-
nodes[index++] = node.getId();
309-
}
310-
return nodes;
301+
if (nodes == null || nodes.length == 0) {
302+
return StreamSupport.stream(this.spliterator(), false).map(DiscoveryNode::getId).toArray(String[]::new);
311303
} else {
312304
ObjectHashSet<String> resolvedNodesIds = new ObjectHashSet<>(nodes.length);
313305
for (String nodeId : nodes) {
@@ -324,16 +316,11 @@ public String[] resolveNodes(String... nodes) {
324316
} else if (nodeExists(nodeId)) {
325317
resolvedNodesIds.add(nodeId);
326318
} else {
327-
// not a node id, try and search by name
328-
for (DiscoveryNode node : this) {
329-
if (Regex.simpleMatch(nodeId, node.getName())) {
330-
resolvedNodesIds.add(node.getId());
331-
}
332-
}
333319
for (DiscoveryNode node : this) {
334-
if (Regex.simpleMatch(nodeId, node.getHostAddress())) {
335-
resolvedNodesIds.add(node.getId());
336-
} else if (Regex.simpleMatch(nodeId, node.getHostName())) {
320+
if ("_all".equals(nodeId)
321+
|| Regex.simpleMatch(nodeId, node.getName())
322+
|| Regex.simpleMatch(nodeId, node.getHostAddress())
323+
|| Regex.simpleMatch(nodeId, node.getHostName())) {
337324
resolvedNodesIds.add(node.getId());
338325
}
339326
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@
3434
import java.util.Set;
3535
import java.util.concurrent.atomic.AtomicInteger;
3636
import java.util.stream.Collectors;
37+
import java.util.stream.StreamSupport;
3738

3839
import static org.hamcrest.CoreMatchers.containsString;
3940
import static org.hamcrest.CoreMatchers.equalTo;
41+
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
4042
import static org.hamcrest.Matchers.containsInAnyOrder;
4143
import static org.hamcrest.Matchers.nullValue;
4244

@@ -69,6 +71,41 @@ public void testResolveNodeByAttribute() {
6971
}
7072
}
7173

74+
public void testAll() {
75+
final DiscoveryNodes discoveryNodes = buildDiscoveryNodes();
76+
77+
final String[] allNodes =
78+
StreamSupport.stream(discoveryNodes.spliterator(), false).map(DiscoveryNode::getId).toArray(String[]::new);
79+
assertThat(discoveryNodes.resolveNodes(), arrayContainingInAnyOrder(allNodes));
80+
assertThat(discoveryNodes.resolveNodes(new String[0]), arrayContainingInAnyOrder(allNodes));
81+
assertThat(discoveryNodes.resolveNodes("_all"), arrayContainingInAnyOrder(allNodes));
82+
83+
final String[] nonMasterNodes =
84+
StreamSupport.stream(discoveryNodes.getNodes().values().spliterator(), false)
85+
.map(n -> n.value)
86+
.filter(n -> n.isMasterNode() == false)
87+
.map(DiscoveryNode::getId)
88+
.toArray(String[]::new);
89+
assertThat(discoveryNodes.resolveNodes("_all", "master:false"), arrayContainingInAnyOrder(nonMasterNodes));
90+
91+
assertThat(discoveryNodes.resolveNodes("master:false", "_all"), arrayContainingInAnyOrder(allNodes));
92+
}
93+
94+
public void testCoordinatorOnlyNodes() {
95+
final DiscoveryNodes discoveryNodes = buildDiscoveryNodes();
96+
97+
final String[] coordinatorOnlyNodes =
98+
StreamSupport.stream(discoveryNodes.getNodes().values().spliterator(), false)
99+
.map(n -> n.value)
100+
.filter(n -> n.isDataNode() == false && n.isIngestNode() == false && n.isMasterNode() == false)
101+
.map(DiscoveryNode::getId)
102+
.toArray(String[]::new);
103+
104+
assertThat(
105+
discoveryNodes.resolveNodes("_all", "data:false", "ingest:false", "master:false"),
106+
arrayContainingInAnyOrder(coordinatorOnlyNodes));
107+
}
108+
72109
public void testResolveNodesIds() {
73110
DiscoveryNodes discoveryNodes = buildDiscoveryNodes();
74111

0 commit comments

Comments
 (0)