Skip to content

Commit

Permalink
Enable node roles to be pluggable (elastic#43175)
Browse files Browse the repository at this point in the history
This commit introduces the possibility for a plugin to introduce
additional node roles.
  • Loading branch information
jasontedor authored Jun 13, 2019
1 parent a3f2f40 commit aa12af8
Show file tree
Hide file tree
Showing 72 changed files with 783 additions and 317 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.cluster.ClusterModule;
import org.elasticsearch.cluster.EmptyClusterInfoService;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.routing.allocation.AllocationService;
import org.elasticsearch.cluster.routing.allocation.FailedShard;
Expand Down Expand Up @@ -92,7 +93,7 @@ public static AllocationDeciders defaultAllocationDeciders(Settings settings, Cl

public static DiscoveryNode newNode(String nodeId, Map<String, String> attributes) {
return new DiscoveryNode("", nodeId, new TransportAddress(TransportAddress.META_ADDRESS,
portGenerator.incrementAndGet()), attributes, Sets.newHashSet(DiscoveryNode.Role.MASTER,
DiscoveryNode.Role.DATA), Version.CURRENT);
portGenerator.incrementAndGet()), attributes, Sets.newHashSet(DiscoveryNodeRole.MASTER_ROLE,
DiscoveryNodeRole.DATA_ROLE), Version.CURRENT);
}
}
2 changes: 1 addition & 1 deletion docs/reference/cat/nodes.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Might look like:
[source,txt]
--------------------------------------------------
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1 65 99 42 3.07 mdi * mJw06l1
127.0.0.1 65 99 42 3.07 dim * mJw06l1
--------------------------------------------------
// TESTRESPONSE[s/3.07/(\\d+\\.\\d+( \\d+\\.\\d+ (\\d+\\.\\d+)?)?)?/]
// TESTRESPONSE[s/65 99 42/\\d+ \\d+ \\d+/]
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/getting-started.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ And the response:
[source,txt]
--------------------------------------------------
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1 10 5 5 4.46 mdi * PB2SGZY
127.0.0.1 10 5 5 4.46 dim * PB2SGZY
--------------------------------------------------
// TESTRESPONSE[s/10 5 5 4.46/\\d+ \\d+ \\d+ (\\d+\\.\\d+)? (\\d+\\.\\d+)? (\\d+\.\\d+)?/]
// TESTRESPONSE[s/[*]/[*]/ s/PB2SGZY/.+/ non_json]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.elasticsearch.action.FailedNodeException;
import org.elasticsearch.action.support.nodes.BaseNodesResponse;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
Expand Down Expand Up @@ -74,8 +74,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}

builder.startArray("roles");
for (DiscoveryNode.Role role : nodeInfo.getNode().getRoles()) {
builder.value(role.getRoleName());
for (DiscoveryNodeRole role : nodeInfo.getNode().getRoles()) {
builder.value(role.roleName());
}
builder.endArray();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.elasticsearch.action.support.nodes.BaseNodeResponse;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
Expand Down Expand Up @@ -269,8 +270,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.field("ip", getNode().getAddress());

builder.startArray("roles");
for (DiscoveryNode.Role role : getNode().getRoles()) {
builder.value(role.getRoleName());
for (DiscoveryNodeRole role : getNode().getRoles()) {
builder.value(role.roleName());
}
builder.endArray();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.action.TaskOperationFailure;
import org.elasticsearch.action.support.tasks.BaseTasksResponse;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -175,8 +176,8 @@ public XContentBuilder toXContentGroupedByNode(XContentBuilder builder, Params p
builder.field("ip", node.getAddress());

builder.startArray("roles");
for (DiscoveryNode.Role role : node.getRoles()) {
builder.value(role.getRoleName());
for (DiscoveryNodeRole role : node.getRoles()) {
builder.value(role.roleName());
}
builder.endArray();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

import com.carrotsearch.hppc.ObjectIntHashMap;
import com.carrotsearch.hppc.cursors.ObjectIntCursor;

import org.elasticsearch.Version;
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.network.NetworkModule;
Expand All @@ -49,6 +49,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;

public class ClusterStatsNodes implements ToXContentFragment {
Expand Down Expand Up @@ -186,27 +187,27 @@ public static class Counts implements ToXContentFragment {
private final int total;
private final Map<String, Integer> roles;

private Counts(List<NodeInfo> nodeInfos) {
this.roles = new HashMap<>();
for (DiscoveryNode.Role role : DiscoveryNode.Role.values()) {
this.roles.put(role.getRoleName(), 0);
private Counts(final List<NodeInfo> nodeInfos) {
// TODO: do we need to report zeros?
final Map<String, Integer> roles = new HashMap<>(DiscoveryNode.getPossibleRoleNames().size());
roles.put(COORDINATING_ONLY, 0);
for (final String possibleRoleName : DiscoveryNode.getPossibleRoleNames()) {
roles.put(possibleRoleName, 0);
}
this.roles.put(COORDINATING_ONLY, 0);

int total = 0;
for (NodeInfo nodeInfo : nodeInfos) {
for (final NodeInfo nodeInfo : nodeInfos) {
total++;
if (nodeInfo.getNode().getRoles().isEmpty()) {
Integer count = roles.get(COORDINATING_ONLY);
roles.put(COORDINATING_ONLY, ++count);
roles.merge(COORDINATING_ONLY, 1, Integer::sum);
} else {
for (DiscoveryNode.Role role : nodeInfo.getNode().getRoles()) {
Integer count = roles.get(role.getRoleName());
roles.put(role.getRoleName(), ++count);
for (DiscoveryNodeRole role : nodeInfo.getNode().getRoles()) {
roles.merge(role.roleName(), 1, Integer::sum);
}
}
}
this.total = total;
this.roles = Map.copyOf(roles);
}

public int getTotal() {
Expand All @@ -225,7 +226,7 @@ static final class Fields {
public XContentBuilder toXContent(XContentBuilder builder, Params params)
throws IOException {
builder.field(Fields.TOTAL, total);
for (Map.Entry<String, Integer> entry : roles.entrySet()) {
for (Map.Entry<String, Integer> entry : new TreeMap<>(roles).entrySet()) {
builder.field(entry.getKey(), entry.getValue());
}
return builder;
Expand Down
Loading

0 comments on commit aa12af8

Please sign in to comment.