From 971b828e76a5881b87518ff0d135186f79cc398d Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Sat, 30 Nov 2013 16:20:15 +0100 Subject: [PATCH] Cat API: Improve help and execute help before sending server requests * Force the cat API classes to have simple help available by extending from a AbstractCatAction * Use a set binder on guice creation to create the help on node start up * Make sure that the help/field info is returned without querying any data --- .../rest/action/RestActionModule.java | 22 ++++--- .../rest/action/cat/AbstractCatAction.java | 63 +++++++++++++++++++ .../rest/action/cat/RestAllocationAction.java | 38 +++++++---- .../rest/action/cat/RestCountAction.java | 20 ++++-- .../rest/action/cat/RestHealthAction.java | 48 ++++++++------ .../rest/action/cat/RestHelpAction.java | 31 ++++----- .../rest/action/cat/RestIndicesAction.java | 21 +++++-- .../rest/action/cat/RestMasterAction.java | 18 ++++-- .../rest/action/cat/RestNodesAction.java | 24 ++++--- .../cat/RestPendingClusterTasksAction.java | 20 ++++-- .../rest/action/cat/RestRecoveryAction.java | 38 +++++++---- .../rest/action/cat/RestShardsAction.java | 22 +++++-- .../rest/action/support/RestTable.java | 4 +- 13 files changed, 263 insertions(+), 106 deletions(-) create mode 100644 src/main/java/org/elasticsearch/rest/action/cat/AbstractCatAction.java diff --git a/src/main/java/org/elasticsearch/rest/action/RestActionModule.java b/src/main/java/org/elasticsearch/rest/action/RestActionModule.java index 7df13d5ec2b..67ec61b2168 100644 --- a/src/main/java/org/elasticsearch/rest/action/RestActionModule.java +++ b/src/main/java/org/elasticsearch/rest/action/RestActionModule.java @@ -21,6 +21,7 @@ package org.elasticsearch.rest.action; import com.google.common.collect.Lists; import org.elasticsearch.common.inject.AbstractModule; +import org.elasticsearch.common.inject.multibindings.Multibinder; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.action.admin.cluster.health.RestClusterHealthAction; import org.elasticsearch.rest.action.admin.cluster.node.hotthreads.RestNodesHotThreadsAction; @@ -207,17 +208,20 @@ public class RestActionModule extends AbstractModule { bind(RestExplainAction.class).asEagerSingleton(); - bind(RestAllocationAction.class).asEagerSingleton(); - bind(RestShardsAction.class).asEagerSingleton(); - bind(RestMasterAction.class).asEagerSingleton(); - bind(RestNodesAction.class).asEagerSingleton(); - bind(RestIndicesAction.class).asEagerSingleton(); + // cat API + Multibinder catActionMultibinder = Multibinder.newSetBinder(binder(), AbstractCatAction.class); + catActionMultibinder.addBinding().to(RestAllocationAction.class).asEagerSingleton(); + catActionMultibinder.addBinding().to(RestShardsAction.class).asEagerSingleton(); + catActionMultibinder.addBinding().to(RestMasterAction.class).asEagerSingleton(); + catActionMultibinder.addBinding().to(RestNodesAction.class).asEagerSingleton(); + catActionMultibinder.addBinding().to(RestIndicesAction.class).asEagerSingleton(); // Fully qualified to prevent interference with rest.action.count.RestCountAction - bind(org.elasticsearch.rest.action.cat.RestCountAction.class).asEagerSingleton(); - bind(RestRecoveryAction.class).asEagerSingleton(); - bind(RestHealthAction.class).asEagerSingleton(); + catActionMultibinder.addBinding().to(org.elasticsearch.rest.action.cat.RestCountAction.class).asEagerSingleton(); + catActionMultibinder.addBinding().to(RestRecoveryAction.class).asEagerSingleton(); + catActionMultibinder.addBinding().to(RestHealthAction.class).asEagerSingleton(); + catActionMultibinder.addBinding().to(org.elasticsearch.rest.action.cat.RestPendingClusterTasksAction.class).asEagerSingleton(); + // no abstract cat action bind(RestCatAction.class).asEagerSingleton(); bind(RestHelpAction.class).asEagerSingleton(); - bind(org.elasticsearch.rest.action.cat.RestPendingClusterTasksAction.class).asEagerSingleton(); } } diff --git a/src/main/java/org/elasticsearch/rest/action/cat/AbstractCatAction.java b/src/main/java/org/elasticsearch/rest/action/cat/AbstractCatAction.java new file mode 100644 index 00000000000..33da1e1aec7 --- /dev/null +++ b/src/main/java/org/elasticsearch/rest/action/cat/AbstractCatAction.java @@ -0,0 +1,63 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.rest.action.cat; + +import org.elasticsearch.client.Client; +import org.elasticsearch.common.Table; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.rest.*; + +import static org.elasticsearch.rest.action.support.RestTable.buildHelpWidths; +import static org.elasticsearch.rest.action.support.RestTable.pad; + +/** + * + */ +public abstract class AbstractCatAction extends BaseRestHandler { + + public AbstractCatAction(Settings settings, Client client) { + super(settings, client); + } + + abstract void doRequest(final RestRequest request, final RestChannel channel); + abstract void documentation(StringBuilder sb); + abstract Table getTableWithHeader(final RestRequest request); + + @Override + public void handleRequest(final RestRequest request, final RestChannel channel) { + boolean helpWanted = request.paramAsBoolean("h", false); + if (helpWanted) { + Table table = getTableWithHeader(request); + int[] width = buildHelpWidths(table, request, false); + StringBuilder out = new StringBuilder(); + for (Table.Cell cell : table.getHeaders()) { + // need to do left-align always, so create new cells + pad(new Table.Cell(cell.value), width[0], request, out); + out.append(" "); + pad(new Table.Cell(cell.attr.containsKey("desc") ? cell.attr.get("desc") : "not available"), width[1], request, out); + out.append("\n"); + } + channel.sendResponse(new StringRestResponse(RestStatus.OK, out.toString())); + } else { + doRequest(request, channel); + } + } + + +} diff --git a/src/main/java/org/elasticsearch/rest/action/cat/RestAllocationAction.java b/src/main/java/org/elasticsearch/rest/action/cat/RestAllocationAction.java index dd66457e62e..747454f6284 100644 --- a/src/main/java/org/elasticsearch/rest/action/cat/RestAllocationAction.java +++ b/src/main/java/org/elasticsearch/rest/action/cat/RestAllocationAction.java @@ -46,7 +46,8 @@ import java.util.Locale; import static org.elasticsearch.rest.RestRequest.Method.GET; -public class RestAllocationAction extends BaseRestHandler{ +public class RestAllocationAction extends AbstractCatAction { + @Inject public RestAllocationAction(Settings settings, Client client, RestController controller) { super(settings, client); @@ -55,7 +56,12 @@ public class RestAllocationAction extends BaseRestHandler{ } @Override - public void handleRequest(final RestRequest request, final RestChannel channel) { + void documentation(StringBuilder sb) { + sb.append("/_cat/allocation\n"); + } + + @Override + public void doRequest(final RestRequest request, final RestChannel channel) { final String[] nodes = Strings.splitStringByCommaToArray(request.param("nodes")); final ClusterStateRequest clusterStateRequest = new ClusterStateRequest(); clusterStateRequest.filterMetaData(true); @@ -72,7 +78,7 @@ public class RestAllocationAction extends BaseRestHandler{ @Override public void onResponse(NodesStatsResponse stats) { try { - Table tab = buildTable(state, stats); + Table tab = buildTable(request, state, stats); channel.sendResponse(RestTable.buildResponse(tab, request, channel)); } catch (Throwable e) { onFailure(e); @@ -102,7 +108,21 @@ public class RestAllocationAction extends BaseRestHandler{ } - private Table buildTable(final ClusterStateResponse state, final NodesStatsResponse stats) { + @Override + Table getTableWithHeader(final RestRequest request) { + final Table table = new Table(); + table.startHeaders(); + table.addCell("shards", "text-align:right;"); + table.addCell("diskUsed", "text-align:right;"); + table.addCell("diskAvail", "text-align:right;"); + table.addCell("diskRatio", "text-align:right;"); + table.addCell("ip"); + table.addCell("node"); + table.endHeaders(); + return table; + } + + private Table buildTable(RestRequest request, final ClusterStateResponse state, final NodesStatsResponse stats) { final ObjectIntOpenHashMap allocs = new ObjectIntOpenHashMap(); for (ShardRouting shard : state.getState().routingTable().allShards()) { @@ -115,15 +135,7 @@ public class RestAllocationAction extends BaseRestHandler{ allocs.addTo(nodeId, 1); } - final Table table = new Table(); - table.startHeaders(); - table.addCell("shards", "text-align:right;"); - table.addCell("diskUsed", "text-align:right;"); - table.addCell("diskAvail", "text-align:right;"); - table.addCell("diskRatio", "text-align:right;"); - table.addCell("ip"); - table.addCell("node"); - table.endHeaders(); + Table table = getTableWithHeader(request); for (NodeStats nodeStats : stats.getNodes()) { DiscoveryNode node = nodeStats.getNode(); diff --git a/src/main/java/org/elasticsearch/rest/action/cat/RestCountAction.java b/src/main/java/org/elasticsearch/rest/action/cat/RestCountAction.java index b7186efccbe..d2ee462690d 100644 --- a/src/main/java/org/elasticsearch/rest/action/cat/RestCountAction.java +++ b/src/main/java/org/elasticsearch/rest/action/cat/RestCountAction.java @@ -38,7 +38,7 @@ import java.io.IOException; import static org.elasticsearch.rest.RestRequest.Method.GET; -public class RestCountAction extends BaseRestHandler { +public class RestCountAction extends AbstractCatAction { @Inject protected RestCountAction(Settings settings, Client client, RestController restController) { @@ -48,7 +48,13 @@ public class RestCountAction extends BaseRestHandler { } @Override - public void handleRequest(final RestRequest request, final RestChannel channel) { + void documentation(StringBuilder sb) { + sb.append("/_cat/count\n"); + sb.append("/_cat/count/{index}\n"); + } + + @Override + public void doRequest(final RestRequest request, final RestChannel channel) { String[] indices = Strings.splitStringByCommaToArray(request.param("index")); CountRequest countRequest = new CountRequest(indices); countRequest.operationThreading(BroadcastOperationThreading.SINGLE_THREAD); @@ -67,7 +73,7 @@ public class RestCountAction extends BaseRestHandler { @Override public void onResponse(CountResponse countResponse) { try { - channel.sendResponse(RestTable.buildResponse(buildTable(countResponse), request, channel)); + channel.sendResponse(RestTable.buildResponse(buildTable(request, countResponse), request, channel)); } catch (Throwable t) { onFailure(t); } @@ -84,13 +90,17 @@ public class RestCountAction extends BaseRestHandler { }); } - private Table buildTable(CountResponse response) { - + @Override + Table getTableWithHeader(final RestRequest request) { Table table = new TimestampedTable(); table.startHeaders(); table.addCell("count", "desc:the document count"); table.endHeaders(); + return table; + } + private Table buildTable(RestRequest request, CountResponse response) { + Table table = getTableWithHeader(request); table.startRow(); table.addCell(response.getCount()); table.endRow(); diff --git a/src/main/java/org/elasticsearch/rest/action/cat/RestHealthAction.java b/src/main/java/org/elasticsearch/rest/action/cat/RestHealthAction.java index 2cdb974e83d..84ded9237c9 100644 --- a/src/main/java/org/elasticsearch/rest/action/cat/RestHealthAction.java +++ b/src/main/java/org/elasticsearch/rest/action/cat/RestHealthAction.java @@ -35,7 +35,7 @@ import java.util.Locale; import static org.elasticsearch.rest.RestRequest.Method.GET; -public class RestHealthAction extends BaseRestHandler { +public class RestHealthAction extends AbstractCatAction { @Inject public RestHealthAction(Settings settings, Client client, RestController controller) { @@ -44,15 +44,19 @@ public class RestHealthAction extends BaseRestHandler { } @Override - public void handleRequest(final RestRequest request, final RestChannel channel) { + void documentation(StringBuilder sb) { + sb.append("/_cat/health\n"); + } + + @Override + public void doRequest(final RestRequest request, final RestChannel channel) { ClusterHealthRequest clusterHealthRequest = new ClusterHealthRequest(); - final boolean timeStamp = request.paramAsBoolean("ts", true); client.admin().cluster().health(clusterHealthRequest, new ActionListener() { @Override public void onResponse(final ClusterHealthResponse health) { try { - channel.sendResponse(RestTable.buildResponse(buildTable(health, timeStamp), request, channel)); + channel.sendResponse(RestTable.buildResponse(buildTable(health, request), request, channel)); } catch (Throwable t) { onFailure(t); } @@ -69,28 +73,33 @@ public class RestHealthAction extends BaseRestHandler { }); } - private Table buildTable (final ClusterHealthResponse health, boolean timeStamp) { + @Override + Table getTableWithHeader(final RestRequest request) { + final boolean timeStamp = request.paramAsBoolean("ts", true); Table t; - if (timeStamp) { t = new TimestampedTable(); } else { t = new Table(); } + t.startHeaders(); + t.addCell("cluster"); + t.addCell("status"); + t.addCell("nodeTotal", "text-align:right;"); + t.addCell("nodeData", "text-align:right;"); + t.addCell("shards", "text-align:right;"); + t.addCell("pri", "text-align:right;"); + t.addCell("relo", "text-align:right;"); + t.addCell("init", "text-align:right;"); + t.addCell("unassign", "text-align:right;"); + t.endHeaders(); + return t; + } + + private Table buildTable (final ClusterHealthResponse health, final RestRequest request) { if (null != health) { - t.startHeaders(); - t.addCell("cluster"); - t.addCell("status"); - t.addCell("nodeTotal", "text-align:right;"); - t.addCell("nodeData", "text-align:right;"); - t.addCell("shards", "text-align:right;"); - t.addCell("pri", "text-align:right;"); - t.addCell("relo", "text-align:right;"); - t.addCell("init", "text-align:right;"); - t.addCell("unassign", "text-align:right;"); - t.endHeaders(); - + Table t = getTableWithHeader(request); t.startRow(); t.addCell(health.getClusterName()); t.addCell(health.getStatus().name().toLowerCase(Locale.ROOT)); @@ -102,7 +111,8 @@ public class RestHealthAction extends BaseRestHandler { t.addCell(health.getInitializingShards()); t.addCell(health.getUnassignedShards()); t.endRow(); + return t; } - return t; + return null; } } diff --git a/src/main/java/org/elasticsearch/rest/action/cat/RestHelpAction.java b/src/main/java/org/elasticsearch/rest/action/cat/RestHelpAction.java index f74a773ef58..d055ae34651 100644 --- a/src/main/java/org/elasticsearch/rest/action/cat/RestHelpAction.java +++ b/src/main/java/org/elasticsearch/rest/action/cat/RestHelpAction.java @@ -20,42 +20,33 @@ package org.elasticsearch.rest.action.cat; import org.elasticsearch.client.Client; -import org.elasticsearch.common.inject.Guice; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.inject.Injector; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.*; +import java.util.Set; + import static org.elasticsearch.rest.RestRequest.Method.GET; public class RestHelpAction extends BaseRestHandler { + StringBuilder sb = new StringBuilder(); + @Inject - public RestHelpAction(Settings settings, Client client, RestController controller) { + public RestHelpAction(Settings settings, Client client, RestController controller, Set catActions) { super(settings, client); controller.registerHandler(GET, "/_cat/help", this); controller.registerHandler(GET, "/_cat/halp", this); + sb.append("Try:\n\n"); + sb.append("/_cat/help\n"); + for (AbstractCatAction catAction : catActions) { + catAction.documentation(sb); + } } @Override public void handleRequest(final RestRequest request, final RestChannel channel) { - // Maybe build this list from a classloader or the RestActionModule injector - - StringBuilder s = new StringBuilder(); - s.append("Try:\n\n"); - s.append("/_cat/allocation\n"); - s.append("/_cat/count\n"); - s.append("/_cat/count/{index}\n"); - s.append("/_cat/health\n"); - s.append("/_cat/indices\n"); - s.append("/_cat/indices/{index}\n"); - s.append("/_cat/master\n"); - s.append("/_cat/nodes\n"); - s.append("/_cat/pending_tasks\n"); - s.append("/_cat/recovery\n"); - s.append("/_cat/shards\n"); - s.append("/_cat/shards/{index}\n"); - channel.sendResponse(new StringRestResponse(RestStatus.OK, s.toString())); + channel.sendResponse(new StringRestResponse(RestStatus.OK, sb.toString())); } } diff --git a/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java b/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java index 40fc4ee4ee0..79862a9a825 100644 --- a/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java +++ b/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java @@ -39,11 +39,10 @@ import org.elasticsearch.rest.action.support.RestTable; import java.io.IOException; import java.util.Locale; -import java.util.Map; import static org.elasticsearch.rest.RestRequest.Method.GET; -public class RestIndicesAction extends BaseRestHandler { +public class RestIndicesAction extends AbstractCatAction { @Inject public RestIndicesAction(Settings settings, Client client, RestController controller) { @@ -53,7 +52,13 @@ public class RestIndicesAction extends BaseRestHandler { } @Override - public void handleRequest(final RestRequest request, final RestChannel channel) { + void documentation(StringBuilder sb) { + sb.append("/_cat/indices\n"); + sb.append("/_cat/indices/{index}\n"); + } + + @Override + public void doRequest(final RestRequest request, final RestChannel channel) { final String[] indices = Strings.splitStringByCommaToArray(request.param("index")); final ClusterStateRequest clusterStateRequest = new ClusterStateRequest(); clusterStateRequest.filteredIndices(indices); @@ -75,7 +80,7 @@ public class RestIndicesAction extends BaseRestHandler { @Override public void onResponse(IndicesStatsResponse indicesStatsResponse) { try { - Table tab = buildTable(concreteIndices, clusterHealthResponse, indicesStatsResponse); + Table tab = buildTable(request, concreteIndices, clusterHealthResponse, indicesStatsResponse); channel.sendResponse(RestTable.buildResponse(tab, request, channel)); } catch (Throwable e) { onFailure(e); @@ -116,7 +121,8 @@ public class RestIndicesAction extends BaseRestHandler { }); } - private Table buildTable(String[] indices, ClusterHealthResponse health, IndicesStatsResponse stats) { + @Override + Table getTableWithHeader(final RestRequest request) { Table table = new Table(); table.startHeaders(); table.addCell("health"); @@ -128,6 +134,11 @@ public class RestIndicesAction extends BaseRestHandler { table.addCell("size/pri", "text-align:right;"); table.addCell("size/total", "text-align:right;"); table.endHeaders(); + return table; + } + + private Table buildTable(RestRequest request, String[] indices, ClusterHealthResponse health, IndicesStatsResponse stats) { + Table table = getTableWithHeader(request); for (String index : indices) { ClusterIndexHealth indexHealth = health.getIndices().get(index); diff --git a/src/main/java/org/elasticsearch/rest/action/cat/RestMasterAction.java b/src/main/java/org/elasticsearch/rest/action/cat/RestMasterAction.java index 66df40a019d..cec117d5f2e 100644 --- a/src/main/java/org/elasticsearch/rest/action/cat/RestMasterAction.java +++ b/src/main/java/org/elasticsearch/rest/action/cat/RestMasterAction.java @@ -34,7 +34,7 @@ import java.io.IOException; import static org.elasticsearch.rest.RestRequest.Method.GET; -public class RestMasterAction extends BaseRestHandler { +public class RestMasterAction extends AbstractCatAction { @Inject public RestMasterAction(Settings settings, Client client, RestController controller) { @@ -43,7 +43,12 @@ public class RestMasterAction extends BaseRestHandler { } @Override - public void handleRequest(final RestRequest request, final RestChannel channel) { + void documentation(StringBuilder sb) { + sb.append("/_cat/master\n"); + } + + @Override + public void doRequest(final RestRequest request, final RestChannel channel) { final ClusterStateRequest clusterStateRequest = new ClusterStateRequest(); clusterStateRequest.filterMetaData(true); clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local())); @@ -53,7 +58,7 @@ public class RestMasterAction extends BaseRestHandler { @Override public void onResponse(final ClusterStateResponse clusterStateResponse) { try { - channel.sendResponse(RestTable.buildResponse(buildTable(clusterStateResponse), request, channel)); + channel.sendResponse(RestTable.buildResponse(buildTable(request, clusterStateResponse), request, channel)); } catch (Throwable e) { onFailure(e); } @@ -70,14 +75,19 @@ public class RestMasterAction extends BaseRestHandler { }); } - private Table buildTable(ClusterStateResponse state) { + @Override + Table getTableWithHeader(final RestRequest request) { Table table = new Table(); table.startHeaders() .addCell("id") .addCell("ip") .addCell("node") .endHeaders(); + return table; + } + private Table buildTable(RestRequest request, ClusterStateResponse state) { + Table table = getTableWithHeader(request); String masterId = state.getState().nodes().masterNodeId(); String masterIp = ((InetSocketTransportAddress) state.getState().nodes().get(masterId).address()).address().getAddress().getHostAddress(); String masterName = state.getState().nodes().masterNode().name(); diff --git a/src/main/java/org/elasticsearch/rest/action/cat/RestNodesAction.java b/src/main/java/org/elasticsearch/rest/action/cat/RestNodesAction.java index 9e76bdb1b28..03c4125ecd7 100644 --- a/src/main/java/org/elasticsearch/rest/action/cat/RestNodesAction.java +++ b/src/main/java/org/elasticsearch/rest/action/cat/RestNodesAction.java @@ -45,7 +45,7 @@ import java.util.Locale; import static org.elasticsearch.rest.RestRequest.Method.GET; -public class RestNodesAction extends BaseRestHandler { +public class RestNodesAction extends AbstractCatAction { @Inject public RestNodesAction(Settings settings, Client client, RestController controller) { @@ -54,7 +54,12 @@ public class RestNodesAction extends BaseRestHandler { } @Override - public void handleRequest(final RestRequest request, final RestChannel channel) { + void documentation(StringBuilder sb) { + sb.append("/_cat/nodes\n"); + } + + @Override + public void doRequest(final RestRequest request, final RestChannel channel) { final ClusterStateRequest clusterStateRequest = new ClusterStateRequest(); clusterStateRequest.filterMetaData(true); clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local())); @@ -112,11 +117,8 @@ public class RestNodesAction extends BaseRestHandler { }); } - private Table buildTable(RestRequest req, ClusterStateResponse state, NodesInfoResponse nodesInfo, NodesStatsResponse nodesStats) { - boolean fullId = req.paramAsBoolean("full_id", false); - - String masterId = state.getState().nodes().masterNodeId(); - + @Override + Table getTableWithHeader(final RestRequest request) { Table table = new Table(); table.startHeaders(); table.addCell("nodeId"); @@ -138,6 +140,14 @@ public class RestNodesAction extends BaseRestHandler { table.addCell("master"); table.addCell("name"); table.endHeaders(); + return table; + } + + private Table buildTable(RestRequest req, ClusterStateResponse state, NodesInfoResponse nodesInfo, NodesStatsResponse nodesStats) { + boolean fullId = req.paramAsBoolean("full_id", false); + + String masterId = state.getState().nodes().masterNodeId(); + Table table = getTableWithHeader(req); for (DiscoveryNode node : state.getState().nodes()) { NodeInfo info = nodesInfo.getNodesMap().get(node.id()); diff --git a/src/main/java/org/elasticsearch/rest/action/cat/RestPendingClusterTasksAction.java b/src/main/java/org/elasticsearch/rest/action/cat/RestPendingClusterTasksAction.java index ec1cb8df1c8..e391731abaf 100644 --- a/src/main/java/org/elasticsearch/rest/action/cat/RestPendingClusterTasksAction.java +++ b/src/main/java/org/elasticsearch/rest/action/cat/RestPendingClusterTasksAction.java @@ -34,7 +34,7 @@ import java.io.IOException; import static org.elasticsearch.rest.RestRequest.Method.GET; -public class RestPendingClusterTasksAction extends BaseRestHandler { +public class RestPendingClusterTasksAction extends AbstractCatAction { @Inject public RestPendingClusterTasksAction(Settings settings, Client client, RestController controller) { super(settings, client); @@ -42,13 +42,18 @@ public class RestPendingClusterTasksAction extends BaseRestHandler { } @Override - public void handleRequest(final RestRequest request, final RestChannel channel) { + void documentation(StringBuilder sb) { + sb.append("/_cat/pending_tasks\n"); + } + + @Override + public void doRequest(final RestRequest request, final RestChannel channel) { PendingClusterTasksRequest pendingClusterTasksRequest = new PendingClusterTasksRequest(); client.admin().cluster().pendingClusterTasks(pendingClusterTasksRequest, new ActionListener() { @Override public void onResponse(PendingClusterTasksResponse pendingClusterTasks) { try { - Table tab = buildTable(pendingClusterTasks); + Table tab = buildTable(request, pendingClusterTasks); channel.sendResponse(RestTable.buildResponse(tab, request, channel)); } catch (Throwable e) { onFailure(e); @@ -66,15 +71,20 @@ public class RestPendingClusterTasksAction extends BaseRestHandler { }); } - private Table buildTable(PendingClusterTasksResponse tasks) { + @Override + Table getTableWithHeader(final RestRequest request) { Table t = new Table(); - t.startHeaders(); t.addCell("insertOrder", "text-align:right;desc:Task insertion order"); t.addCell("timeInQueue", "text-align:right;desc:How long task has been in queue"); t.addCell("priority", "desc:Task priority"); t.addCell("source", "desc:Task source"); t.endHeaders(); + return t; + } + + private Table buildTable(RestRequest request, PendingClusterTasksResponse tasks) { + Table t = getTableWithHeader(request); for (PendingClusterTask task : tasks) { t.startRow(); diff --git a/src/main/java/org/elasticsearch/rest/action/cat/RestRecoveryAction.java b/src/main/java/org/elasticsearch/rest/action/cat/RestRecoveryAction.java index 0f5c3979618..a8338b42c47 100644 --- a/src/main/java/org/elasticsearch/rest/action/cat/RestRecoveryAction.java +++ b/src/main/java/org/elasticsearch/rest/action/cat/RestRecoveryAction.java @@ -47,7 +47,7 @@ import static org.elasticsearch.rest.RestRequest.Method.GET; * in a string format, designed to be used at the command line. An Index can * be specified to limit output to a particular index or indices. */ -public class RestRecoveryAction extends BaseRestHandler { +public class RestRecoveryAction extends AbstractCatAction { @Inject protected RestRecoveryAction(Settings settings, Client client, RestController restController) { @@ -57,7 +57,13 @@ public class RestRecoveryAction extends BaseRestHandler { } @Override - public void handleRequest(final RestRequest request, final RestChannel channel) { + void documentation(StringBuilder sb) { + sb.append("/_cat/recovery\n"); + sb.append("/_cat/recovery/{index}\n"); + } + + @Override + public void doRequest(final RestRequest request, final RestChannel channel) { final String[] indices = Strings.splitStringByCommaToArray(request.param("index")); final ClusterStateRequest clusterStateRequest = new ClusterStateRequest(); clusterStateRequest.filterMetaData(true); @@ -90,7 +96,7 @@ public class RestRecoveryAction extends BaseRestHandler { } try { - channel.sendResponse(RestTable.buildResponse(buildRecoveryTable(clusterStateResponse, primarySizes, replicas), request, channel)); + channel.sendResponse(RestTable.buildResponse(buildRecoveryTable(request, clusterStateResponse, primarySizes, replicas), request, channel)); } catch (Throwable e) { try { channel.sendResponse(new XContentThrowableRestResponse(request, e)); @@ -123,15 +129,8 @@ public class RestRecoveryAction extends BaseRestHandler { } - /** - * buildRecoveryTable will build a table of recovery information suitable - * for displaying at the command line. - * @param state Current cluster state. - * @param primarySizes A Map of {@code index + shardId} strings to store size for all primary shards. - * @param recoveringReplicas A Set of {@link ShardStatus} objects for each recovering replica to be displayed. - * @return A table containing index, shardId, node, target size, recovered size and percentage for each recovering replica - */ - public static Table buildRecoveryTable(ClusterStateResponse state, Map primarySizes, Set recoveringReplicas) { + @Override + Table getTableWithHeader(RestRequest request) { Table t = new Table(); t.startHeaders().addCell("index") .addCell("shard") @@ -141,6 +140,21 @@ public class RestRecoveryAction extends BaseRestHandler { .addCell("ip") .addCell("node") .endHeaders(); + return t; + } + + /** + * buildRecoveryTable will build a table of recovery information suitable + * for displaying at the command line. + * + * @param request + * @param state Current cluster state. + * @param primarySizes A Map of {@code index + shardId} strings to store size for all primary shards. + * @param recoveringReplicas A Set of {@link org.elasticsearch.action.admin.indices.status.ShardStatus} objects for each recovering replica to be displayed. + * @return A table containing index, shardId, node, target size, recovered size and percentage for each recovering replica + */ + public Table buildRecoveryTable(RestRequest request, ClusterStateResponse state, Map primarySizes, Set recoveringReplicas) { + Table t = getTableWithHeader(request); for (ShardStatus status : recoveringReplicas) { DiscoveryNode node = state.getState().nodes().get(status.getShardRouting().currentNodeId()); diff --git a/src/main/java/org/elasticsearch/rest/action/cat/RestShardsAction.java b/src/main/java/org/elasticsearch/rest/action/cat/RestShardsAction.java index b0e3924821f..c50190380eb 100644 --- a/src/main/java/org/elasticsearch/rest/action/cat/RestShardsAction.java +++ b/src/main/java/org/elasticsearch/rest/action/cat/RestShardsAction.java @@ -41,7 +41,7 @@ import java.util.Set; import static org.elasticsearch.rest.RestRequest.Method.GET; -public class RestShardsAction extends BaseRestHandler { +public class RestShardsAction extends AbstractCatAction { @Inject public RestShardsAction(Settings settings, Client client, RestController controller) { @@ -51,7 +51,13 @@ public class RestShardsAction extends BaseRestHandler { } @Override - public void handleRequest(final RestRequest request, final RestChannel channel) { + void documentation(StringBuilder sb) { + sb.append("/_cat/shards\n"); + sb.append("/_cat/shards/{index}\n"); + } + + @Override + public void doRequest(final RestRequest request, final RestChannel channel) { final String[] indices = Strings.splitStringByCommaToArray(request.param("index")); final ClusterStateRequest clusterStateRequest = new ClusterStateRequest(); clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local())); @@ -67,7 +73,7 @@ public class RestShardsAction extends BaseRestHandler { @Override public void onResponse(IndicesStatsResponse indicesStatsResponse) { try { - channel.sendResponse(RestTable.buildResponse(buildTable(concreteIndices, clusterStateResponse, indicesStatsResponse), request, channel)); + channel.sendResponse(RestTable.buildResponse(buildTable(request, concreteIndices, clusterStateResponse, indicesStatsResponse), request, channel)); } catch (Throwable e) { onFailure(e); } @@ -95,8 +101,8 @@ public class RestShardsAction extends BaseRestHandler { }); } - private Table buildTable(String[] concreteIndices, ClusterStateResponse state, IndicesStatsResponse stats) { - Set indices = Sets.newHashSet(concreteIndices); + @Override + Table getTableWithHeader(final RestRequest request) { Table table = new Table(); table.startHeaders() .addCell("index", "default:true;") @@ -108,7 +114,13 @@ public class RestShardsAction extends BaseRestHandler { .addCell("ip", "default:true;") .addCell("node", "default:true;") .endHeaders(); + return table; + } + private Table buildTable(RestRequest request, String[] concreteIndices, ClusterStateResponse state, IndicesStatsResponse stats) { + Table table = getTableWithHeader(request); + + Set indices = Sets.newHashSet(concreteIndices); for (ShardRouting shard : state.getState().routingTable().allShards()) { if (!indices.contains(shard.index())) { continue; diff --git a/src/main/java/org/elasticsearch/rest/action/support/RestTable.java b/src/main/java/org/elasticsearch/rest/action/support/RestTable.java index aa50e7194dd..6c45ed126be 100644 --- a/src/main/java/org/elasticsearch/rest/action/support/RestTable.java +++ b/src/main/java/org/elasticsearch/rest/action/support/RestTable.java @@ -127,7 +127,7 @@ public class RestTable { return display; } - private static int[] buildHelpWidths(Table table, RestRequest request, boolean verbose) { + public static int[] buildHelpWidths(Table table, RestRequest request, boolean verbose) { int[] width = new int[2]; for (Table.Cell cell : table.getHeaders()) { String v = renderValue(request, cell.value); @@ -170,7 +170,7 @@ public class RestTable { return width; } - private static void pad(Table.Cell cell, int width, RestRequest request, StringBuilder out) { + public static void pad(Table.Cell cell, int width, RestRequest request, StringBuilder out) { String sValue = renderValue(request, cell.value); int length = sValue == null ? 0 : sValue.length(); byte leftOver = (byte) (width - length);