15
15
import org .elasticsearch .action .admin .indices .stats .CommonStats ;
16
16
import org .elasticsearch .action .admin .indices .stats .IndexShardStats ;
17
17
import org .elasticsearch .action .admin .indices .stats .ShardStats ;
18
+ import org .elasticsearch .cluster .metadata .Metadata ;
19
+ import org .elasticsearch .cluster .metadata .ProjectId ;
18
20
import org .elasticsearch .common .collect .Iterators ;
19
21
import org .elasticsearch .common .io .stream .StreamInput ;
20
22
import org .elasticsearch .common .io .stream .StreamOutput ;
@@ -66,6 +68,7 @@ public class NodeIndicesStats implements Writeable, ChunkedToXContent {
66
68
private final CommonStats stats ;
67
69
private final Map <Index , List <IndexShardStats >> statsByShard ;
68
70
private final Map <Index , CommonStats > statsByIndex ;
71
+ private final Map <Index , ProjectId > projectsByIndex ;
69
72
70
73
public NodeIndicesStats (StreamInput in ) throws IOException {
71
74
stats = new CommonStats (in );
@@ -87,12 +90,24 @@ public NodeIndicesStats(StreamInput in) throws IOException {
87
90
} else {
88
91
statsByIndex = new HashMap <>();
89
92
}
93
+
94
+ if (in .getTransportVersion ().onOrAfter (TransportVersions .NODES_STATS_SUPPORTS_MULTI_PROJECT )) {
95
+ boolean hasProjectsByIndex = in .readBoolean ();
96
+ projectsByIndex = hasProjectsByIndex ? in .readMap (Index ::new , ProjectId ::readFrom ) : null ;
97
+ } else {
98
+ projectsByIndex = null ;
99
+ }
90
100
}
91
101
102
+ /**
103
+ * Constructs an instance. If {@code projectsByIndex} argument is non-null, then the index-to-project map will be stored, and the
104
+ * project IDs will be prepended to the index names when converting this instance to XContent (except when it is the default project).
105
+ */
92
106
public NodeIndicesStats (
93
107
CommonStats oldStats ,
94
108
Map <Index , CommonStats > statsByIndex ,
95
109
Map <Index , List <IndexShardStats >> statsByShard ,
110
+ @ Nullable Map <Index , ProjectId > projectsByIndex ,
96
111
boolean includeShardsStats
97
112
) {
98
113
if (includeShardsStats ) {
@@ -114,6 +129,7 @@ public NodeIndicesStats(
114
129
for (CommonStats indexStats : statsByIndex .values ()) {
115
130
stats .add (indexStats );
116
131
}
132
+ this .projectsByIndex = projectsByIndex ;
117
133
}
118
134
119
135
@ Nullable
@@ -228,19 +244,28 @@ public void writeTo(StreamOutput out) throws IOException {
228
244
if (out .getTransportVersion ().onOrAfter (VERSION_SUPPORTING_STATS_BY_INDEX )) {
229
245
out .writeMap (statsByIndex );
230
246
}
247
+ if (out .getTransportVersion ().onOrAfter (TransportVersions .NODES_STATS_SUPPORTS_MULTI_PROJECT )) {
248
+ out .writeBoolean (projectsByIndex != null );
249
+ if (projectsByIndex != null ) {
250
+ out .writeMap (projectsByIndex );
251
+ }
252
+ }
231
253
}
232
254
233
255
@ Override
234
256
public boolean equals (Object o ) {
235
257
if (this == o ) return true ;
236
258
if (o == null || getClass () != o .getClass ()) return false ;
237
259
NodeIndicesStats that = (NodeIndicesStats ) o ;
238
- return stats .equals (that .stats ) && statsByShard .equals (that .statsByShard ) && statsByIndex .equals (that .statsByIndex );
260
+ return stats .equals (that .stats )
261
+ && statsByShard .equals (that .statsByShard )
262
+ && statsByIndex .equals (that .statsByIndex )
263
+ && Objects .equals (projectsByIndex , that .projectsByIndex );
239
264
}
240
265
241
266
@ Override
242
267
public int hashCode () {
243
- return Objects .hash (stats , statsByShard , statsByIndex );
268
+ return Objects .hash (stats , statsByShard , statsByIndex , projectsByIndex );
244
269
}
245
270
246
271
@ Override
@@ -260,7 +285,7 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params outerP
260
285
case INDICES -> ChunkedToXContentHelper .object (
261
286
Fields .INDICES ,
262
287
Iterators .map (createCommonStatsByIndex ().entrySet ().iterator (), entry -> (builder , params ) -> {
263
- builder .startObject (entry .getKey (). getName ( ));
288
+ builder .startObject (xContentKey ( entry .getKey ()));
264
289
entry .getValue ().toXContent (builder , outerParams );
265
290
return builder .endObject ();
266
291
})
@@ -271,7 +296,7 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params outerP
271
296
Iterators .flatMap (
272
297
statsByShard .entrySet ().iterator (),
273
298
entry -> ChunkedToXContentHelper .array (
274
- entry .getKey (). getName ( ),
299
+ xContentKey ( entry .getKey ()),
275
300
Iterators .flatMap (
276
301
entry .getValue ().iterator (),
277
302
indexShardStats -> Iterators .concat (
@@ -291,6 +316,17 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params outerP
291
316
);
292
317
}
293
318
319
+ private String xContentKey (Index index ) {
320
+ if (projectsByIndex == null ) {
321
+ return index .getName ();
322
+ }
323
+ ProjectId projectId = projectsByIndex .get (index );
324
+ if (Objects .equals (projectId , Metadata .DEFAULT_PROJECT_ID )) {
325
+ return index .getName ();
326
+ }
327
+ return projectId + "/" + index .getName ();
328
+ }
329
+
294
330
private Map <Index , CommonStats > createCommonStatsByIndex () {
295
331
Map <Index , CommonStats > statsMap = new HashMap <>();
296
332
0 commit comments