Skip to content

Commit a286963

Browse files
authored
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 elastic#28971
1 parent c39b6d3 commit a286963

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

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

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.List;
4141
import java.util.Map;
4242
import java.util.Objects;
43+
import java.util.stream.StreamSupport;
4344

4445
/**
4546
* This class holds all {@link DiscoveryNode} in the cluster and provides convenience methods to
@@ -232,10 +233,6 @@ public DiscoveryNode findByAddress(TransportAddress address) {
232233
return null;
233234
}
234235

235-
public boolean isAllNodes(String... nodesIds) {
236-
return nodesIds == null || nodesIds.length == 0 || (nodesIds.length == 1 && nodesIds[0].equals("_all"));
237-
}
238-
239236
/**
240237
* Returns the version of the node with the oldest version in the cluster that is not a client node
241238
*
@@ -304,13 +301,8 @@ public DiscoveryNode resolveNode(String node) {
304301
* or a generic node attribute name in which case value will be treated as a wildcard and matched against the node attribute values.
305302
*/
306303
public String[] resolveNodes(String... nodes) {
307-
if (isAllNodes(nodes)) {
308-
int index = 0;
309-
nodes = new String[this.nodes.size()];
310-
for (DiscoveryNode node : this) {
311-
nodes[index++] = node.getId();
312-
}
313-
return nodes;
304+
if (nodes == null || nodes.length == 0) {
305+
return StreamSupport.stream(this.spliterator(), false).map(DiscoveryNode::getId).toArray(String[]::new);
314306
} else {
315307
ObjectHashSet<String> resolvedNodesIds = new ObjectHashSet<>(nodes.length);
316308
for (String nodeId : nodes) {
@@ -327,16 +319,11 @@ public String[] resolveNodes(String... nodes) {
327319
} else if (nodeExists(nodeId)) {
328320
resolvedNodesIds.add(nodeId);
329321
} else {
330-
// not a node id, try and search by name
331-
for (DiscoveryNode node : this) {
332-
if (Regex.simpleMatch(nodeId, node.getName())) {
333-
resolvedNodesIds.add(node.getId());
334-
}
335-
}
336322
for (DiscoveryNode node : this) {
337-
if (Regex.simpleMatch(nodeId, node.getHostAddress())) {
338-
resolvedNodesIds.add(node.getId());
339-
} else if (Regex.simpleMatch(nodeId, node.getHostName())) {
323+
if ("_all".equals(nodeId)
324+
|| Regex.simpleMatch(nodeId, node.getName())
325+
|| Regex.simpleMatch(nodeId, node.getHostAddress())
326+
|| Regex.simpleMatch(nodeId, node.getHostName())) {
340327
resolvedNodesIds.add(node.getId());
341328
}
342329
}

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

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

3940
import static org.hamcrest.CoreMatchers.containsString;
4041
import static org.hamcrest.CoreMatchers.equalTo;
42+
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
4143
import static org.hamcrest.Matchers.containsInAnyOrder;
4244
import static org.hamcrest.Matchers.nullValue;
4345

@@ -70,6 +72,41 @@ public void testResolveNodeByAttribute() {
7072
}
7173
}
7274

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

0 commit comments

Comments
 (0)