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