fix REST api for cluster state and the ability to filter parts of the data returned

This commit is contained in:
kimchy 2010-07-14 16:05:47 +03:00
parent 95ba62f83d
commit f6fa056f1c
1 changed files with 98 additions and 83 deletions

View File

@ -62,13 +62,13 @@ public class RestClusterStateAction extends BaseRestHandler {
} }
@Override public void handleRequest(final RestRequest request, final RestChannel channel) { @Override public void handleRequest(final RestRequest request, final RestChannel channel) {
ClusterStateRequest clusterStateRequest = Requests.clusterState(); final ClusterStateRequest clusterStateRequest = Requests.clusterState();
clusterStateRequest.filterNodes(request.paramAsBoolean("filter_nodes", clusterStateRequest.filterNodes())); clusterStateRequest.filterNodes(request.paramAsBoolean("filter_nodes", clusterStateRequest.filterNodes()));
clusterStateRequest.filterRoutingTable(request.paramAsBoolean("filter_routing_table", clusterStateRequest.filterRoutingTable())); clusterStateRequest.filterRoutingTable(request.paramAsBoolean("filter_routing_table", clusterStateRequest.filterRoutingTable()));
clusterStateRequest.filterMetaData(request.paramAsBoolean("filter_metadata", clusterStateRequest.filterMetaData())); clusterStateRequest.filterMetaData(request.paramAsBoolean("filter_metadata", clusterStateRequest.filterMetaData()));
clusterStateRequest.filterBlocks(request.paramAsBoolean("filter_blocks", clusterStateRequest.filterBlocks())); clusterStateRequest.filterBlocks(request.paramAsBoolean("filter_blocks", clusterStateRequest.filterBlocks()));
clusterStateRequest.filteredIndices(RestActions.splitIndices(request.param("filter_indices", null))); clusterStateRequest.filteredIndices(RestActions.splitIndices(request.param("filter_indices", null)));
client.admin().cluster().state(new ClusterStateRequest(), new ActionListener<ClusterStateResponse>() { client.admin().cluster().state(clusterStateRequest, new ActionListener<ClusterStateResponse>() {
@Override public void onResponse(ClusterStateResponse response) { @Override public void onResponse(ClusterStateResponse response) {
try { try {
ClusterState state = response.state(); ClusterState state = response.state();
@ -77,117 +77,132 @@ public class RestClusterStateAction extends BaseRestHandler {
builder.field("cluster_name", response.clusterName().value()); builder.field("cluster_name", response.clusterName().value());
builder.field("master_node", state.nodes().masterNodeId()); if (!clusterStateRequest.filterNodes()) {
builder.field("master_node", state.nodes().masterNodeId());
}
// blocks // blocks
builder.startObject("blocks"); if (!clusterStateRequest.filterBlocks()) {
builder.startObject("blocks");
if (!state.blocks().global().isEmpty()) { if (!state.blocks().global().isEmpty()) {
builder.startObject("global"); builder.startObject("global");
for (ClusterBlock block : state.blocks().global()) { for (ClusterBlock block : state.blocks().global()) {
block.toXContent(builder, request); block.toXContent(builder, request);
}
builder.endObject();
} }
if (!state.blocks().indices().isEmpty()) {
builder.startObject("indices");
for (Map.Entry<String, ImmutableSet<ClusterBlock>> entry : state.blocks().indices().entrySet()) {
builder.startObject(entry.getKey());
for (ClusterBlock block : entry.getValue()) {
block.toXContent(builder, request);
}
builder.endObject();
}
builder.endObject();
}
builder.endObject(); builder.endObject();
} }
if (!state.blocks().indices().isEmpty()) { // nodes
builder.startObject("indices"); if (!clusterStateRequest.filterNodes()) {
for (Map.Entry<String, ImmutableSet<ClusterBlock>> entry : state.blocks().indices().entrySet()) { builder.startObject("nodes");
builder.startObject(entry.getKey()); for (DiscoveryNode node : state.nodes()) {
for (ClusterBlock block : entry.getValue()) { builder.startObject(node.id());
block.toXContent(builder, request); builder.field("name", node.name());
} builder.field("transport_address", node.address().toString());
builder.endObject(); builder.endObject();
} }
builder.endObject(); builder.endObject();
} }
builder.endObject(); // meta data
if (!clusterStateRequest.filterMetaData()) {
builder.startObject("metadata");
builder.startObject("indices");
for (IndexMetaData indexMetaData : state.metaData()) {
builder.startObject(indexMetaData.index());
builder.startObject("settings");
Settings settings = settingsFilter.filterSettings(indexMetaData.settings());
for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) {
builder.field(entry.getKey(), entry.getValue());
}
builder.endObject();
builder.startObject("mappings");
for (Map.Entry<String, String> entry : indexMetaData.mappings().entrySet()) {
XContentParser parser = XContentFactory.xContent(entry.getValue()).createParser(entry.getValue());
Map<String, Object> mapping = parser.map();
if (mapping.size() == 1 && mapping.containsKey(entry.getKey())) {
// the type name is the root value, reduce it
mapping = (Map<String, Object>) mapping.get(entry.getKey());
}
builder.field(entry.getKey());
builder.map(mapping);
}
builder.endObject();
builder.startArray("aliases");
for (String alias : indexMetaData.aliases()) {
builder.value(alias);
}
builder.endArray();
builder.endObject();
}
builder.endObject();
// nodes
builder.startObject("nodes");
for (DiscoveryNode node : state.nodes()) {
builder.startObject(node.id());
builder.field("name", node.name());
builder.field("transport_address", node.address().toString());
builder.endObject(); builder.endObject();
} }
builder.endObject();
// meta data // routing table
builder.startObject("metadata"); if (!clusterStateRequest.filterRoutingTable()) {
builder.startObject("indices"); builder.startObject("routing_table");
for (IndexMetaData indexMetaData : state.metaData()) { builder.startObject("indices");
builder.startObject(indexMetaData.index()); for (IndexRoutingTable indexRoutingTable : state.routingTable()) {
builder.startObject(indexRoutingTable.index());
builder.startObject("settings"); builder.startObject("shards");
Settings settings = settingsFilter.filterSettings(indexMetaData.settings()); for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) { builder.startArray(Integer.toString(indexShardRoutingTable.shardId().id()));
builder.field(entry.getKey(), entry.getValue()); for (ShardRouting shardRouting : indexShardRoutingTable) {
} jsonShardRouting(builder, shardRouting);
builder.endObject(); }
builder.endArray();
builder.startObject("mappings");
for (Map.Entry<String, String> entry : indexMetaData.mappings().entrySet()) {
XContentParser parser = XContentFactory.xContent(entry.getValue()).createParser(entry.getValue());
Map<String, Object> mapping = parser.map();
if (mapping.size() == 1 && mapping.containsKey(entry.getKey())) {
// the type name is the root value, reduce it
mapping = (Map<String, Object>) mapping.get(entry.getKey());
} }
builder.field(entry.getKey()); builder.endObject();
builder.map(mapping); builder.endObject();
} }
builder.endObject(); builder.endObject();
builder.endObject();
}
builder.startArray("aliases"); // routing nodes
for (String alias : indexMetaData.aliases()) { if (!clusterStateRequest.filterRoutingTable()) {
builder.value(alias); builder.startObject("routing_nodes");
builder.startArray("unassigned");
for (ShardRouting shardRouting : state.readOnlyRoutingNodes().unassigned()) {
jsonShardRouting(builder, shardRouting);
} }
builder.endArray(); builder.endArray();
builder.endObject(); builder.startObject("nodes");
} for (RoutingNode routingNode : state.readOnlyRoutingNodes()) {
builder.endObject(); builder.startArray(routingNode.nodeId());
builder.endObject(); for (ShardRouting shardRouting : routingNode) {
// routing table
builder.startObject("routing_table");
builder.startObject("indices");
for (IndexRoutingTable indexRoutingTable : state.routingTable()) {
builder.startObject(indexRoutingTable.index());
builder.startObject("shards");
for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
builder.startArray(Integer.toString(indexShardRoutingTable.shardId().id()));
for (ShardRouting shardRouting : indexShardRoutingTable) {
jsonShardRouting(builder, shardRouting); jsonShardRouting(builder, shardRouting);
} }
builder.endArray(); builder.endArray();
} }
builder.endObject(); builder.endObject();
builder.endObject(); builder.endObject();
} }
builder.endObject();
builder.endObject();
// routing nodes
builder.startObject("routing_nodes");
builder.startArray("unassigned");
for (ShardRouting shardRouting : state.readOnlyRoutingNodes().unassigned()) {
jsonShardRouting(builder, shardRouting);
}
builder.endArray();
builder.startObject("nodes");
for (RoutingNode routingNode : state.readOnlyRoutingNodes()) {
builder.startArray(routingNode.nodeId());
for (ShardRouting shardRouting : routingNode) {
jsonShardRouting(builder, shardRouting);
}
builder.endArray();
}
builder.endObject();
builder.endObject();
builder.endObject(); builder.endObject();
channel.sendResponse(new XContentRestResponse(request, RestResponse.Status.OK, builder)); channel.sendResponse(new XContentRestResponse(request, RestResponse.Status.OK, builder));