Skip to content

Commit 9e4b5ea

Browse files
committed
Introduce abstract superclass, group cat module bindings, and short-circuit helps.
2 parents adef349 + 971b828 commit 9e4b5ea

13 files changed

+263
-106
lines changed

src/main/java/org/elasticsearch/rest/action/RestActionModule.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.common.collect.Lists;
2323
import org.elasticsearch.common.inject.AbstractModule;
24+
import org.elasticsearch.common.inject.multibindings.Multibinder;
2425
import org.elasticsearch.rest.BaseRestHandler;
2526
import org.elasticsearch.rest.action.admin.cluster.health.RestClusterHealthAction;
2627
import org.elasticsearch.rest.action.admin.cluster.node.hotthreads.RestNodesHotThreadsAction;
@@ -207,17 +208,20 @@ protected void configure() {
207208

208209
bind(RestExplainAction.class).asEagerSingleton();
209210

210-
bind(RestAllocationAction.class).asEagerSingleton();
211-
bind(RestShardsAction.class).asEagerSingleton();
212-
bind(RestMasterAction.class).asEagerSingleton();
213-
bind(RestNodesAction.class).asEagerSingleton();
214-
bind(RestIndicesAction.class).asEagerSingleton();
211+
// cat API
212+
Multibinder<AbstractCatAction> catActionMultibinder = Multibinder.newSetBinder(binder(), AbstractCatAction.class);
213+
catActionMultibinder.addBinding().to(RestAllocationAction.class).asEagerSingleton();
214+
catActionMultibinder.addBinding().to(RestShardsAction.class).asEagerSingleton();
215+
catActionMultibinder.addBinding().to(RestMasterAction.class).asEagerSingleton();
216+
catActionMultibinder.addBinding().to(RestNodesAction.class).asEagerSingleton();
217+
catActionMultibinder.addBinding().to(RestIndicesAction.class).asEagerSingleton();
215218
// Fully qualified to prevent interference with rest.action.count.RestCountAction
216-
bind(org.elasticsearch.rest.action.cat.RestCountAction.class).asEagerSingleton();
217-
bind(RestRecoveryAction.class).asEagerSingleton();
218-
bind(RestHealthAction.class).asEagerSingleton();
219+
catActionMultibinder.addBinding().to(org.elasticsearch.rest.action.cat.RestCountAction.class).asEagerSingleton();
220+
catActionMultibinder.addBinding().to(RestRecoveryAction.class).asEagerSingleton();
221+
catActionMultibinder.addBinding().to(RestHealthAction.class).asEagerSingleton();
222+
catActionMultibinder.addBinding().to(org.elasticsearch.rest.action.cat.RestPendingClusterTasksAction.class).asEagerSingleton();
223+
// no abstract cat action
219224
bind(RestCatAction.class).asEagerSingleton();
220225
bind(RestHelpAction.class).asEagerSingleton();
221-
bind(org.elasticsearch.rest.action.cat.RestPendingClusterTasksAction.class).asEagerSingleton();
222226
}
223227
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to ElasticSearch and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. ElasticSearch licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.rest.action.cat;
20+
21+
import org.elasticsearch.client.Client;
22+
import org.elasticsearch.common.Table;
23+
import org.elasticsearch.common.settings.Settings;
24+
import org.elasticsearch.rest.*;
25+
26+
import static org.elasticsearch.rest.action.support.RestTable.buildHelpWidths;
27+
import static org.elasticsearch.rest.action.support.RestTable.pad;
28+
29+
/**
30+
*
31+
*/
32+
public abstract class AbstractCatAction extends BaseRestHandler {
33+
34+
public AbstractCatAction(Settings settings, Client client) {
35+
super(settings, client);
36+
}
37+
38+
abstract void doRequest(final RestRequest request, final RestChannel channel);
39+
abstract void documentation(StringBuilder sb);
40+
abstract Table getTableWithHeader(final RestRequest request);
41+
42+
@Override
43+
public void handleRequest(final RestRequest request, final RestChannel channel) {
44+
boolean helpWanted = request.paramAsBoolean("h", false);
45+
if (helpWanted) {
46+
Table table = getTableWithHeader(request);
47+
int[] width = buildHelpWidths(table, request, false);
48+
StringBuilder out = new StringBuilder();
49+
for (Table.Cell cell : table.getHeaders()) {
50+
// need to do left-align always, so create new cells
51+
pad(new Table.Cell(cell.value), width[0], request, out);
52+
out.append(" ");
53+
pad(new Table.Cell(cell.attr.containsKey("desc") ? cell.attr.get("desc") : "not available"), width[1], request, out);
54+
out.append("\n");
55+
}
56+
channel.sendResponse(new StringRestResponse(RestStatus.OK, out.toString()));
57+
} else {
58+
doRequest(request, channel);
59+
}
60+
}
61+
62+
63+
}

src/main/java/org/elasticsearch/rest/action/cat/RestAllocationAction.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
import static org.elasticsearch.rest.RestRequest.Method.GET;
4747

4848

49-
public class RestAllocationAction extends BaseRestHandler{
49+
public class RestAllocationAction extends AbstractCatAction {
50+
5051
@Inject
5152
public RestAllocationAction(Settings settings, Client client, RestController controller) {
5253
super(settings, client);
@@ -55,7 +56,12 @@ public RestAllocationAction(Settings settings, Client client, RestController con
5556
}
5657

5758
@Override
58-
public void handleRequest(final RestRequest request, final RestChannel channel) {
59+
void documentation(StringBuilder sb) {
60+
sb.append("/_cat/allocation\n");
61+
}
62+
63+
@Override
64+
public void doRequest(final RestRequest request, final RestChannel channel) {
5965
final String[] nodes = Strings.splitStringByCommaToArray(request.param("nodes"));
6066
final ClusterStateRequest clusterStateRequest = new ClusterStateRequest();
6167
clusterStateRequest.filterMetaData(true);
@@ -72,7 +78,7 @@ public void onResponse(final ClusterStateResponse state) {
7278
@Override
7379
public void onResponse(NodesStatsResponse stats) {
7480
try {
75-
Table tab = buildTable(state, stats);
81+
Table tab = buildTable(request, state, stats);
7682
channel.sendResponse(RestTable.buildResponse(tab, request, channel));
7783
} catch (Throwable e) {
7884
onFailure(e);
@@ -102,7 +108,21 @@ public void onFailure(Throwable e) {
102108

103109
}
104110

105-
private Table buildTable(final ClusterStateResponse state, final NodesStatsResponse stats) {
111+
@Override
112+
Table getTableWithHeader(final RestRequest request) {
113+
final Table table = new Table();
114+
table.startHeaders();
115+
table.addCell("shards", "text-align:right;");
116+
table.addCell("diskUsed", "text-align:right;");
117+
table.addCell("diskAvail", "text-align:right;");
118+
table.addCell("diskRatio", "text-align:right;");
119+
table.addCell("ip");
120+
table.addCell("node");
121+
table.endHeaders();
122+
return table;
123+
}
124+
125+
private Table buildTable(RestRequest request, final ClusterStateResponse state, final NodesStatsResponse stats) {
106126
final ObjectIntOpenHashMap<String> allocs = new ObjectIntOpenHashMap<String>();
107127

108128
for (ShardRouting shard : state.getState().routingTable().allShards()) {
@@ -115,15 +135,7 @@ private Table buildTable(final ClusterStateResponse state, final NodesStatsRespo
115135
allocs.addTo(nodeId, 1);
116136
}
117137

118-
final Table table = new Table();
119-
table.startHeaders();
120-
table.addCell("shards", "text-align:right;");
121-
table.addCell("diskUsed", "text-align:right;");
122-
table.addCell("diskAvail", "text-align:right;");
123-
table.addCell("diskRatio", "text-align:right;");
124-
table.addCell("ip");
125-
table.addCell("node");
126-
table.endHeaders();
138+
Table table = getTableWithHeader(request);
127139

128140
for (NodeStats nodeStats : stats.getNodes()) {
129141
DiscoveryNode node = nodeStats.getNode();

src/main/java/org/elasticsearch/rest/action/cat/RestCountAction.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
import static org.elasticsearch.rest.RestRequest.Method.GET;
4040

41-
public class RestCountAction extends BaseRestHandler {
41+
public class RestCountAction extends AbstractCatAction {
4242

4343
@Inject
4444
protected RestCountAction(Settings settings, Client client, RestController restController) {
@@ -48,7 +48,13 @@ protected RestCountAction(Settings settings, Client client, RestController restC
4848
}
4949

5050
@Override
51-
public void handleRequest(final RestRequest request, final RestChannel channel) {
51+
void documentation(StringBuilder sb) {
52+
sb.append("/_cat/count\n");
53+
sb.append("/_cat/count/{index}\n");
54+
}
55+
56+
@Override
57+
public void doRequest(final RestRequest request, final RestChannel channel) {
5258
String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
5359
CountRequest countRequest = new CountRequest(indices);
5460
countRequest.operationThreading(BroadcastOperationThreading.SINGLE_THREAD);
@@ -67,7 +73,7 @@ public void handleRequest(final RestRequest request, final RestChannel channel)
6773
@Override
6874
public void onResponse(CountResponse countResponse) {
6975
try {
70-
channel.sendResponse(RestTable.buildResponse(buildTable(countResponse), request, channel));
76+
channel.sendResponse(RestTable.buildResponse(buildTable(request, countResponse), request, channel));
7177
} catch (Throwable t) {
7278
onFailure(t);
7379
}
@@ -84,13 +90,17 @@ public void onFailure(Throwable t) {
8490
});
8591
}
8692

87-
private Table buildTable(CountResponse response) {
88-
93+
@Override
94+
Table getTableWithHeader(final RestRequest request) {
8995
Table table = new TimestampedTable();
9096
table.startHeaders();
9197
table.addCell("count", "desc:the document count");
9298
table.endHeaders();
99+
return table;
100+
}
93101

102+
private Table buildTable(RestRequest request, CountResponse response) {
103+
Table table = getTableWithHeader(request);
94104
table.startRow();
95105
table.addCell(response.getCount());
96106
table.endRow();

src/main/java/org/elasticsearch/rest/action/cat/RestHealthAction.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
import static org.elasticsearch.rest.RestRequest.Method.GET;
3737

38-
public class RestHealthAction extends BaseRestHandler {
38+
public class RestHealthAction extends AbstractCatAction {
3939

4040
@Inject
4141
public RestHealthAction(Settings settings, Client client, RestController controller) {
@@ -44,15 +44,19 @@ public RestHealthAction(Settings settings, Client client, RestController control
4444
}
4545

4646
@Override
47-
public void handleRequest(final RestRequest request, final RestChannel channel) {
47+
void documentation(StringBuilder sb) {
48+
sb.append("/_cat/health\n");
49+
}
50+
51+
@Override
52+
public void doRequest(final RestRequest request, final RestChannel channel) {
4853
ClusterHealthRequest clusterHealthRequest = new ClusterHealthRequest();
49-
final boolean timeStamp = request.paramAsBoolean("ts", true);
5054

5155
client.admin().cluster().health(clusterHealthRequest, new ActionListener<ClusterHealthResponse>() {
5256
@Override
5357
public void onResponse(final ClusterHealthResponse health) {
5458
try {
55-
channel.sendResponse(RestTable.buildResponse(buildTable(health, timeStamp), request, channel));
59+
channel.sendResponse(RestTable.buildResponse(buildTable(health, request), request, channel));
5660
} catch (Throwable t) {
5761
onFailure(t);
5862
}
@@ -69,28 +73,33 @@ public void onFailure(Throwable e) {
6973
});
7074
}
7175

72-
private Table buildTable (final ClusterHealthResponse health, boolean timeStamp) {
76+
@Override
77+
Table getTableWithHeader(final RestRequest request) {
78+
final boolean timeStamp = request.paramAsBoolean("ts", true);
7379
Table t;
74-
7580
if (timeStamp) {
7681
t = new TimestampedTable();
7782
} else {
7883
t = new Table();
7984
}
85+
t.startHeaders();
86+
t.addCell("cluster");
87+
t.addCell("status");
88+
t.addCell("nodeTotal", "text-align:right;");
89+
t.addCell("nodeData", "text-align:right;");
90+
t.addCell("shards", "text-align:right;");
91+
t.addCell("pri", "text-align:right;");
92+
t.addCell("relo", "text-align:right;");
93+
t.addCell("init", "text-align:right;");
94+
t.addCell("unassign", "text-align:right;");
95+
t.endHeaders();
8096

81-
if (null != health) {
82-
t.startHeaders();
83-
t.addCell("cluster");
84-
t.addCell("status");
85-
t.addCell("nodeTotal", "text-align:right;");
86-
t.addCell("nodeData", "text-align:right;");
87-
t.addCell("shards", "text-align:right;");
88-
t.addCell("pri", "text-align:right;");
89-
t.addCell("relo", "text-align:right;");
90-
t.addCell("init", "text-align:right;");
91-
t.addCell("unassign", "text-align:right;");
92-
t.endHeaders();
97+
return t;
98+
}
9399

100+
private Table buildTable (final ClusterHealthResponse health, final RestRequest request) {
101+
if (null != health) {
102+
Table t = getTableWithHeader(request);
94103
t.startRow();
95104
t.addCell(health.getClusterName());
96105
t.addCell(health.getStatus().name().toLowerCase(Locale.ROOT));
@@ -102,7 +111,8 @@ private Table buildTable (final ClusterHealthResponse health, boolean timeStamp)
102111
t.addCell(health.getInitializingShards());
103112
t.addCell(health.getUnassignedShards());
104113
t.endRow();
114+
return t;
105115
}
106-
return t;
116+
return null;
107117
}
108118
}

src/main/java/org/elasticsearch/rest/action/cat/RestHelpAction.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,33 @@
2020
package org.elasticsearch.rest.action.cat;
2121

2222
import org.elasticsearch.client.Client;
23-
import org.elasticsearch.common.inject.Guice;
2423
import org.elasticsearch.common.inject.Inject;
25-
import org.elasticsearch.common.inject.Injector;
2624
import org.elasticsearch.common.settings.Settings;
2725
import org.elasticsearch.rest.*;
2826

27+
import java.util.Set;
28+
2929
import static org.elasticsearch.rest.RestRequest.Method.GET;
3030

3131

3232
public class RestHelpAction extends BaseRestHandler {
3333

34+
StringBuilder sb = new StringBuilder();
35+
3436
@Inject
35-
public RestHelpAction(Settings settings, Client client, RestController controller) {
37+
public RestHelpAction(Settings settings, Client client, RestController controller, Set<AbstractCatAction> catActions) {
3638
super(settings, client);
3739
controller.registerHandler(GET, "/_cat/help", this);
3840
controller.registerHandler(GET, "/_cat/halp", this);
41+
sb.append("Try:\n\n");
42+
sb.append("/_cat/help\n");
43+
for (AbstractCatAction catAction : catActions) {
44+
catAction.documentation(sb);
45+
}
3946
}
4047

4148
@Override
4249
public void handleRequest(final RestRequest request, final RestChannel channel) {
43-
// Maybe build this list from a classloader or the RestActionModule injector
44-
45-
StringBuilder s = new StringBuilder();
46-
s.append("Try:\n\n");
47-
s.append("/_cat/allocation\n");
48-
s.append("/_cat/count\n");
49-
s.append("/_cat/count/{index}\n");
50-
s.append("/_cat/health\n");
51-
s.append("/_cat/indices\n");
52-
s.append("/_cat/indices/{index}\n");
53-
s.append("/_cat/master\n");
54-
s.append("/_cat/nodes\n");
55-
s.append("/_cat/pending_tasks\n");
56-
s.append("/_cat/recovery\n");
57-
s.append("/_cat/shards\n");
58-
s.append("/_cat/shards/{index}\n");
59-
channel.sendResponse(new StringRestResponse(RestStatus.OK, s.toString()));
50+
channel.sendResponse(new StringRestResponse(RestStatus.OK, sb.toString()));
6051
}
6152
}

0 commit comments

Comments
 (0)