19
19
20
20
package org .elasticsearch .action .admin .indices .stats ;
21
21
22
+ import org .elasticsearch .cluster .routing .ShardRouting ;
23
+ import org .elasticsearch .cluster .routing .ShardRoutingState ;
24
+ import org .elasticsearch .cluster .routing .TestShardRouting ;
25
+ import org .elasticsearch .common .UUIDs ;
22
26
import org .elasticsearch .common .xcontent .ToXContent ;
23
27
import org .elasticsearch .common .xcontent .json .JsonXContent ;
28
+ import org .elasticsearch .index .Index ;
29
+ import org .elasticsearch .index .shard .ShardId ;
30
+ import org .elasticsearch .index .shard .ShardPath ;
24
31
import org .elasticsearch .test .ESTestCase ;
25
32
33
+ import java .nio .file .Path ;
34
+ import java .util .ArrayList ;
26
35
import java .util .Collections ;
36
+ import java .util .HashMap ;
37
+ import java .util .List ;
38
+ import java .util .Map ;
39
+ import java .util .concurrent .atomic .AtomicLong ;
27
40
28
41
import static org .hamcrest .CoreMatchers .containsString ;
42
+ import static org .hamcrest .Matchers .containsInAnyOrder ;
43
+ import static org .hamcrest .Matchers .is ;
29
44
import static org .hamcrest .object .HasToString .hasToString ;
30
45
31
-
32
46
public class IndicesStatsResponseTests extends ESTestCase {
33
47
34
48
public void testInvalidLevel () {
@@ -42,4 +56,59 @@ public void testInvalidLevel() {
42
56
hasToString (containsString ("level parameter must be one of [cluster] or [indices] or [shards] but was [" + level + "]" )));
43
57
}
44
58
59
+ public void testGetIndices () {
60
+ List <ShardStats > shards = new ArrayList <>();
61
+ int noOfIndexes = randomIntBetween (2 , 5 );
62
+ List <String > expectedIndexes = new ArrayList <>();
63
+ Map <String , AtomicLong > expectedIndexToPrimaryShardsCount = new HashMap <>();
64
+ Map <String , AtomicLong > expectedIndexToTotalShardsCount = new HashMap <>();
65
+
66
+ for (int indCnt = 0 ; indCnt < noOfIndexes ; indCnt ++) {
67
+ Index index = createIndex (randomAlphaOfLength (9 ));
68
+ expectedIndexes .add (index .getName ());
69
+ int numShards = randomIntBetween (1 , 5 );
70
+ for (int shardId = 0 ; shardId < numShards ; shardId ++) {
71
+ ShardId shId = new ShardId (index , shardId );
72
+ Path path = createTempDir ().resolve ("indices" ).resolve (index .getUUID ()).resolve (String .valueOf (shardId ));
73
+ ShardPath shardPath = new ShardPath (false , path , path , shId );
74
+ ShardRouting routing = createShardRouting (index , shId , (shardId == 0 ));
75
+ shards .add (new ShardStats (routing , shardPath , null , null , null , null ));
76
+ AtomicLong primaryShardsCounter = expectedIndexToPrimaryShardsCount .computeIfAbsent (index .getName (),
77
+ k -> new AtomicLong (0L ));
78
+ if (routing .primary ()) {
79
+ primaryShardsCounter .incrementAndGet ();
80
+ }
81
+ AtomicLong shardsCounter = expectedIndexToTotalShardsCount .computeIfAbsent (index .getName (), k -> new AtomicLong (0L ));
82
+ shardsCounter .incrementAndGet ();
83
+ }
84
+ }
85
+ final IndicesStatsResponse indicesStatsResponse = new IndicesStatsResponse (shards .toArray (new ShardStats [shards .size ()]), 0 , 0 , 0 ,
86
+ null );
87
+ Map <String , IndexStats > indexStats = indicesStatsResponse .getIndices ();
88
+
89
+ assertThat (indexStats .size (), is (noOfIndexes ));
90
+ assertThat (indexStats .keySet (), containsInAnyOrder (expectedIndexes .toArray (new String [0 ])));
91
+
92
+ for (String index : indexStats .keySet ()) {
93
+ IndexStats stat = indexStats .get (index );
94
+ ShardStats [] shardStats = stat .getShards ();
95
+ long primaryCount = 0L ;
96
+ long totalCount = shardStats .length ;
97
+ for (ShardStats shardStat : shardStats ) {
98
+ if (shardStat .getShardRouting ().primary ()) {
99
+ primaryCount ++;
100
+ }
101
+ }
102
+ assertThat (primaryCount , is (expectedIndexToPrimaryShardsCount .get (index ).get ()));
103
+ assertThat (totalCount , is (expectedIndexToTotalShardsCount .get (index ).get ()));
104
+ }
105
+ }
106
+
107
+ private ShardRouting createShardRouting (Index index , ShardId shardId , boolean isPrimary ) {
108
+ return TestShardRouting .newShardRouting (shardId , randomAlphaOfLength (4 ), isPrimary , ShardRoutingState .STARTED );
109
+ }
110
+
111
+ private Index createIndex (String indexName ) {
112
+ return new Index (indexName , UUIDs .base64UUID ());
113
+ }
45
114
}
0 commit comments