32
32
33
33
package org .opensearch .indices ;
34
34
35
+ import org .opensearch .Version ;
35
36
import org .opensearch .action .admin .indices .stats .CommonStats ;
36
37
import org .opensearch .action .admin .indices .stats .IndexShardStats ;
37
38
import org .opensearch .action .admin .indices .stats .ShardStats ;
63
64
64
65
import java .io .IOException ;
65
66
import java .util .ArrayList ;
67
+ import java .util .Arrays ;
66
68
import java .util .HashMap ;
67
69
import java .util .List ;
68
70
import java .util .Map ;
75
77
@ PublicApi (since = "1.0.0" )
76
78
public class NodeIndicesStats implements Writeable , ToXContentFragment {
77
79
private CommonStats stats ;
80
+ private Map <Index , CommonStats > statsByIndex ;
78
81
private Map <Index , List <IndexShardStats >> statsByShard ;
79
82
80
83
public NodeIndicesStats (StreamInput in ) throws IOException {
81
84
stats = new CommonStats (in );
85
+ if (in .getVersion ().onOrAfter (Version .V_2_13_0 )) {
86
+ // contains statsByIndex
87
+ if (in .readBoolean ()) {
88
+ statsByIndex = new HashMap <>();
89
+ readStatsByIndex (in );
90
+ }
91
+ }
82
92
if (in .readBoolean ()) {
83
- int entries = in .readVInt ();
84
93
statsByShard = new HashMap <>();
85
- for (int i = 0 ; i < entries ; i ++) {
86
- Index index = new Index (in );
87
- int indexShardListSize = in .readVInt ();
88
- List <IndexShardStats > indexShardStats = new ArrayList <>(indexShardListSize );
89
- for (int j = 0 ; j < indexShardListSize ; j ++) {
90
- indexShardStats .add (new IndexShardStats (in ));
91
- }
92
- statsByShard .put (index , indexShardStats );
93
- }
94
+ readStatsByShards (in );
94
95
}
95
96
}
96
97
@@ -112,6 +113,57 @@ public NodeIndicesStats(CommonStats oldStats, Map<Index, List<IndexShardStats>>
112
113
}
113
114
}
114
115
116
+ public NodeIndicesStats (
117
+ CommonStats oldStats ,
118
+ Map <Index , List <IndexShardStats >> statsByShard ,
119
+ SearchRequestStats searchRequestStats ,
120
+ String [] levels
121
+ ) {
122
+ // make a total common stats from old ones and current ones
123
+ this .stats = oldStats ;
124
+ for (List <IndexShardStats > shardStatsList : statsByShard .values ()) {
125
+ for (IndexShardStats indexShardStats : shardStatsList ) {
126
+ for (ShardStats shardStats : indexShardStats .getShards ()) {
127
+ stats .add (shardStats .getStats ());
128
+ }
129
+ }
130
+ }
131
+
132
+ if (this .stats .search != null ) {
133
+ this .stats .search .setSearchRequestStats (searchRequestStats );
134
+ }
135
+
136
+ if (levels != null ) {
137
+ if (Arrays .stream (levels ).anyMatch (NodeIndicesStats .levels .indices ::equals )) {
138
+ this .statsByIndex = createStatsByIndex (statsByShard );
139
+ } else if (Arrays .stream (levels ).anyMatch (NodeIndicesStats .levels .shards ::equals )) {
140
+ this .statsByShard = statsByShard ;
141
+ }
142
+ }
143
+ }
144
+
145
+ private void readStatsByIndex (StreamInput in ) throws IOException {
146
+ int indexEntries = in .readVInt ();
147
+ for (int i = 0 ; i < indexEntries ; i ++) {
148
+ Index index = new Index (in );
149
+ CommonStats commonStats = new CommonStats (in );
150
+ statsByIndex .put (index , commonStats );
151
+ }
152
+ }
153
+
154
+ private void readStatsByShards (StreamInput in ) throws IOException {
155
+ int entries = in .readVInt ();
156
+ for (int i = 0 ; i < entries ; i ++) {
157
+ Index index = new Index (in );
158
+ int indexShardListSize = in .readVInt ();
159
+ List <IndexShardStats > indexShardStats = new ArrayList <>(indexShardListSize );
160
+ for (int j = 0 ; j < indexShardListSize ; j ++) {
161
+ indexShardStats .add (new IndexShardStats (in ));
162
+ }
163
+ statsByShard .put (index , indexShardStats );
164
+ }
165
+ }
166
+
115
167
@ Nullable
116
168
public StoreStats getStore () {
117
169
return stats .getStore ();
@@ -195,7 +247,31 @@ public RecoveryStats getRecoveryStats() {
195
247
@ Override
196
248
public void writeTo (StreamOutput out ) throws IOException {
197
249
stats .writeTo (out );
250
+
251
+ if (out .getVersion ().onOrAfter (Version .V_2_13_0 )) {
252
+ out .writeBoolean (statsByIndex != null );
253
+ if (statsByIndex != null ) {
254
+ writeStatsByIndex (out );
255
+ }
256
+ }
257
+
198
258
out .writeBoolean (statsByShard != null );
259
+ if (statsByShard != null ) {
260
+ writeStatsByShards (out );
261
+ }
262
+ }
263
+
264
+ private void writeStatsByIndex (StreamOutput out ) throws IOException {
265
+ if (statsByIndex != null ) {
266
+ out .writeVInt (statsByIndex .size ());
267
+ for (Map .Entry <Index , CommonStats > entry : statsByIndex .entrySet ()) {
268
+ entry .getKey ().writeTo (out );
269
+ entry .getValue ().writeTo (out );
270
+ }
271
+ }
272
+ }
273
+
274
+ private void writeStatsByShards (StreamOutput out ) throws IOException {
199
275
if (statsByShard != null ) {
200
276
out .writeVInt (statsByShard .size ());
201
277
for (Map .Entry <Index , List <IndexShardStats >> entry : statsByShard .entrySet ()) {
@@ -222,16 +298,15 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
222
298
builder .startObject (Fields .INDICES );
223
299
stats .toXContent (builder , params );
224
300
225
- if ("indices" .equals (level )) {
226
- Map <Index , CommonStats > indexStats = createStatsByIndex ();
301
+ if (levels .indices .equals (level )) {
227
302
builder .startObject (Fields .INDICES );
228
- for (Map .Entry <Index , CommonStats > entry : indexStats .entrySet ()) {
303
+ for (Map .Entry <Index , CommonStats > entry : statsByIndex .entrySet ()) {
229
304
builder .startObject (entry .getKey ().getName ());
230
305
entry .getValue ().toXContent (builder , params );
231
306
builder .endObject ();
232
307
}
233
308
builder .endObject ();
234
- } else if (" shards" .equals (level )) {
309
+ } else if (levels . shards .equals (level )) {
235
310
builder .startObject ("shards" );
236
311
for (Map .Entry <Index , List <IndexShardStats >> entry : statsByShard .entrySet ()) {
237
312
builder .startArray (entry .getKey ().getName ());
@@ -251,7 +326,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
251
326
return builder ;
252
327
}
253
328
254
- private Map <Index , CommonStats > createStatsByIndex () {
329
+ private Map <Index , CommonStats > createStatsByIndex (Map < Index , List < IndexShardStats >> statsByShard ) {
255
330
Map <Index , CommonStats > statsMap = new HashMap <>();
256
331
for (Map .Entry <Index , List <IndexShardStats >> entry : statsByShard .entrySet ()) {
257
332
if (!statsMap .containsKey (entry .getKey ())) {
@@ -276,6 +351,14 @@ public List<IndexShardStats> getShardStats(Index index) {
276
351
}
277
352
}
278
353
354
+ public CommonStats getIndexStats (Index index ) {
355
+ if (statsByIndex == null ) {
356
+ return null ;
357
+ } else {
358
+ return statsByIndex .get (index );
359
+ }
360
+ }
361
+
279
362
/**
280
363
* Fields used for parsing and toXContent
281
364
*
@@ -284,4 +367,28 @@ public List<IndexShardStats> getShardStats(Index index) {
284
367
static final class Fields {
285
368
static final String INDICES = "indices" ;
286
369
}
370
+
371
+ /**
372
+ * Levels for the NodeIndicesStats
373
+ */
374
+ public enum levels {
375
+ nodes ("nodes" ),
376
+ indices ("indices" ),
377
+ shards ("shards" );
378
+
379
+ private final String name ;
380
+
381
+ levels (String name ) {
382
+ this .name = name ;
383
+ }
384
+
385
+ @ Override
386
+ public String toString () {
387
+ return name ;
388
+ }
389
+
390
+ public boolean equals (String value ) {
391
+ return this .name .equals (value );
392
+ }
393
+ }
287
394
}
0 commit comments