Skip to content

Commit 5594d57

Browse files
probakowskiweizijunelasticmachine
authored
/_cat/shards support path stats (#53461) (#54119)
* _cat/shards support path stats * fix some style case * fix some style case * fix rest-api-spec cat.shards error * fix rest-api-spec cat.shards bwc error Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: weizijun <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
1 parent 68f74cf commit 5594d57

File tree

3 files changed

+133
-3
lines changed

3 files changed

+133
-3
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/cat.shards/10_basic.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
"Help":
33
- skip:
4-
version: " - 7.1.99"
5-
reason: external refresh stats were added in 7.2.0
4+
version: " - 7.99.99"
5+
reason: shard path stats were added in 8.0.0
66
- do:
77
cat.shards:
88
help: true
@@ -78,6 +78,8 @@
7878
warmer.current .+ \n
7979
warmer.total .+ \n
8080
warmer.total_time .+ \n
81+
path.data .+ \n
82+
path.state .+ \n
8183
$/
8284
---
8385
"Test cat shards output":

server/src/main/java/org/elasticsearch/rest/action/cat/RestShardsAction.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ protected Table getTableWithHeader(final RestRequest request) {
202202
table.addCell("warmer.total", "alias:wto,warmerTotal;default:false;text-align:right;desc:total warmer ops");
203203
table.addCell("warmer.total_time", "alias:wtt,warmerTotalTime;default:false;text-align:right;desc:time spent in warmers");
204204

205+
table.addCell("path.data", "alias:pd,dataPath;default:false;text-align:right;desc:shard data path");
206+
table.addCell("path.state", "alias:ps,statsPath;default:false;text-align:right;desc:shard state path");
207+
205208
table.endHeaders();
206209
return table;
207210
}
@@ -216,7 +219,8 @@ private static <S, T> Object getOrNull(S stats, Function<S, T> accessor, Functio
216219
return null;
217220
}
218221

219-
private Table buildTable(RestRequest request, ClusterStateResponse state, IndicesStatsResponse stats) {
222+
// package private for testing
223+
Table buildTable(RestRequest request, ClusterStateResponse state, IndicesStatsResponse stats) {
220224
Table table = getTableWithHeader(request);
221225

222226
for (ShardRouting shard : state.getState().routingTable().allShards()) {
@@ -353,6 +357,9 @@ private Table buildTable(RestRequest request, ClusterStateResponse state, Indice
353357
table.addCell(getOrNull(commonStats, CommonStats::getWarmer, WarmerStats::total));
354358
table.addCell(getOrNull(commonStats, CommonStats::getWarmer, WarmerStats::totalTime));
355359

360+
table.addCell(getOrNull(shardStats, ShardStats::getDataPath, s -> s));
361+
table.addCell(getOrNull(shardStats, ShardStats::getStatePath, s -> s));
362+
356363
table.endRow();
357364
}
358365

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* 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+
20+
package org.elasticsearch.rest.action.cat;
21+
22+
import org.elasticsearch.Version;
23+
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
24+
import org.elasticsearch.action.admin.indices.stats.CommonStats;
25+
import org.elasticsearch.action.admin.indices.stats.IndexStats;
26+
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
27+
import org.elasticsearch.action.admin.indices.stats.ShardStats;
28+
import org.elasticsearch.cluster.ClusterState;
29+
import org.elasticsearch.cluster.node.DiscoveryNode;
30+
import org.elasticsearch.cluster.node.DiscoveryNodes;
31+
import org.elasticsearch.cluster.routing.RoutingTable;
32+
import org.elasticsearch.cluster.routing.ShardRouting;
33+
import org.elasticsearch.cluster.routing.ShardRoutingState;
34+
import org.elasticsearch.cluster.routing.TestShardRouting;
35+
import org.elasticsearch.common.Table;
36+
import org.elasticsearch.index.shard.ShardPath;
37+
import org.elasticsearch.test.ESTestCase;
38+
import org.elasticsearch.test.rest.FakeRestRequest;
39+
40+
import java.nio.file.Path;
41+
import java.util.ArrayList;
42+
import java.util.HashMap;
43+
import java.util.Iterator;
44+
import java.util.List;
45+
import java.util.Map;
46+
47+
import static org.hamcrest.Matchers.equalTo;
48+
import static org.mockito.Mockito.mock;
49+
import static org.mockito.Mockito.when;
50+
51+
public class RestShardsActionTests extends ESTestCase {
52+
53+
public void testBuildTable() {
54+
final int numShards = randomIntBetween(1, 5);
55+
DiscoveryNode localNode = new DiscoveryNode("local", buildNewFakeTransportAddress(), Version.CURRENT);
56+
57+
List<ShardRouting> shardRoutings = new ArrayList<>(numShards);
58+
Map<ShardRouting, ShardStats> shardStatsMap = new HashMap<>();
59+
String index = "index";
60+
for (int i = 0; i < numShards; i++) {
61+
ShardRoutingState shardRoutingState = ShardRoutingState.fromValue((byte) randomIntBetween(2, 3));
62+
ShardRouting shardRouting = TestShardRouting.newShardRouting(index, i, localNode.getId(), randomBoolean(), shardRoutingState);
63+
Path path = createTempDir().resolve("indices").resolve(shardRouting.shardId().getIndex().getUUID())
64+
.resolve(String.valueOf(shardRouting.shardId().id()));
65+
ShardStats shardStats = new ShardStats(shardRouting, new ShardPath(false, path, path, shardRouting.shardId()),
66+
null, null, null, null);
67+
shardStatsMap.put(shardRouting, shardStats);
68+
shardRoutings.add(shardRouting);
69+
}
70+
71+
IndexStats indexStats = mock(IndexStats.class);
72+
when(indexStats.getPrimaries()).thenReturn(new CommonStats());
73+
when(indexStats.getTotal()).thenReturn(new CommonStats());
74+
75+
IndicesStatsResponse stats = mock(IndicesStatsResponse.class);
76+
when(stats.asMap()).thenReturn(shardStatsMap);
77+
78+
DiscoveryNodes discoveryNodes = mock(DiscoveryNodes.class);
79+
when(discoveryNodes.get(localNode.getId())).thenReturn(localNode);
80+
81+
ClusterStateResponse state = mock(ClusterStateResponse.class);
82+
RoutingTable routingTable = mock(RoutingTable.class);
83+
when(routingTable.allShards()).thenReturn(shardRoutings);
84+
ClusterState clusterState = mock(ClusterState.class);
85+
when(clusterState.routingTable()).thenReturn(routingTable);
86+
when(clusterState.nodes()).thenReturn(discoveryNodes);
87+
when(state.getState()).thenReturn(clusterState);
88+
89+
final RestShardsAction action = new RestShardsAction();
90+
final Table table = action.buildTable(new FakeRestRequest(), state, stats);
91+
92+
// now, verify the table is correct
93+
List<Table.Cell> headers = table.getHeaders();
94+
assertThat(headers.get(0).value, equalTo("index"));
95+
assertThat(headers.get(1).value, equalTo("shard"));
96+
assertThat(headers.get(2).value, equalTo("prirep"));
97+
assertThat(headers.get(3).value, equalTo("state"));
98+
assertThat(headers.get(4).value, equalTo("docs"));
99+
assertThat(headers.get(5).value, equalTo("store"));
100+
assertThat(headers.get(6).value, equalTo("ip"));
101+
assertThat(headers.get(7).value, equalTo("id"));
102+
assertThat(headers.get(8).value, equalTo("node"));
103+
104+
final List<List<Table.Cell>> rows = table.getRows();
105+
assertThat(rows.size(), equalTo(numShards));
106+
107+
Iterator<ShardRouting> shardRoutingsIt = shardRoutings.iterator();
108+
for (final List<Table.Cell> row : rows) {
109+
ShardRouting shardRouting = shardRoutingsIt.next();
110+
ShardStats shardStats = shardStatsMap.get(shardRouting);
111+
assertThat(row.get(0).value, equalTo(shardRouting.getIndexName()));
112+
assertThat(row.get(1).value, equalTo(shardRouting.getId()));
113+
assertThat(row.get(2).value, equalTo(shardRouting.primary() ? "p" : "r"));
114+
assertThat(row.get(3).value, equalTo(shardRouting.state()));
115+
assertThat(row.get(6).value, equalTo(localNode.getHostAddress()));
116+
assertThat(row.get(7).value, equalTo(localNode.getId()));
117+
assertThat(row.get(69).value, equalTo(shardStats.getDataPath()));
118+
assertThat(row.get(70).value, equalTo(shardStats.getStatePath()));
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)