Skip to content

Commit

Permalink
Include node roles in cluster state JSON response
Browse files Browse the repository at this point in the history
Today the response to `GET _cluster/state` does not include the roles of
the nodes in the cluster. In the past this made sense, roles were
relatively unchanging things that could be determined from elsewhere.
These days we have an increasingly rich collection of roles, with
nontrivial BWC implications, so it is important for debugging to be able
to see the specific roles as viewed by the master. This commit adds the
role names to the cluster state API output.

Relates elastic#71385
  • Loading branch information
DaveCTurner committed Apr 7, 2021
1 parent 6a081e1 commit 086852f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Expand Up @@ -422,6 +422,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}
builder.endObject();

builder.startArray("roles");
for (DiscoveryNodeRole role : roles) {
builder.value(role.roleName());
}
builder.endArray();

builder.endObject();
return builder;
}
Expand Down
Expand Up @@ -9,26 +9,33 @@
package org.elasticsearch.cluster.node;

import org.elasticsearch.Version;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.ESTestCase;

import java.net.InetAddress;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static org.elasticsearch.test.NodeRoles.nonRemoteClusterClientNode;
import static org.elasticsearch.test.NodeRoles.remoteClusterClientNode;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;
Expand Down Expand Up @@ -157,4 +164,41 @@ public void testDiscoveryNodeDescriptionWithoutAttributes() {
assertThat(descriptionWithoutAttributes, not(containsString("test-attr")));
}

public void testDiscoveryNodeToXContent() {
final TransportAddress transportAddress = buildNewFakeTransportAddress();
final DiscoveryNode node = new DiscoveryNode(
"test-name",
"test-id",
"test-ephemeral-id",
"test-hostname",
"test-hostaddr",
transportAddress,
Collections.singletonMap("test-attr", "val"),
DiscoveryNodeRole.BUILT_IN_ROLES,
Version.CURRENT);

final String jsonString = Strings.toString(node, randomBoolean(), randomBoolean());
final Map<String, Object> topLevelMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), jsonString, randomBoolean());

assertThat(topLevelMap.toString(), topLevelMap.size(), equalTo(1));
assertTrue(topLevelMap.toString(), topLevelMap.containsKey("test-id"));

@SuppressWarnings("unchecked")
final Map<String, Object> detailsMap = (Map<String, Object>) topLevelMap.get("test-id");

assertThat(topLevelMap.toString(), detailsMap.remove("name"), equalTo("test-name"));
assertThat(topLevelMap.toString(), detailsMap.remove("ephemeral_id"), equalTo("test-ephemeral-id"));
assertThat(topLevelMap.toString(), detailsMap.remove("transport_address"), equalTo(transportAddress.toString()));

@SuppressWarnings("unchecked")
final Map<String, Object> attributes = (Map<String, Object>) detailsMap.remove("attributes");
assertThat(topLevelMap.toString(), attributes.get("test-attr"), equalTo("val"));

@SuppressWarnings("unchecked")
final List<String> roles = (List<String>) detailsMap.remove("roles");
assertThat(roles, hasItems("master", "data_content", "remote_cluster_client"));

assertThat(detailsMap.toString(), detailsMap, anEmptyMap());
}

}

0 comments on commit 086852f

Please sign in to comment.