|
| 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