21
21
22
22
import java .io .IOException ;
23
23
import java .util .Collections ;
24
+ import java .util .Comparator ;
24
25
import java .util .HashMap ;
25
26
import java .util .List ;
26
27
import java .util .Map ;
@@ -71,7 +72,7 @@ public class SnapshotLifecycleStats implements Writeable, ToXContentObject {
71
72
PARSER .declareLong (ConstructingObjectParser .constructorArg (), RETENTION_FAILED );
72
73
PARSER .declareLong (ConstructingObjectParser .constructorArg (), RETENTION_TIMED_OUT );
73
74
PARSER .declareLong (ConstructingObjectParser .constructorArg (), RETENTION_TIME_MILLIS );
74
- PARSER .declareNamedObjects (ConstructingObjectParser .constructorArg (), ( p , c , n ) -> SnapshotPolicyStats .parse ( p , n ) , POLICY_STATS );
75
+ PARSER .declareObjectArray (ConstructingObjectParser .constructorArg (), SnapshotPolicyStats .PARSER , POLICY_STATS );
75
76
}
76
77
77
78
public SnapshotLifecycleStats () {
@@ -213,23 +214,25 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
213
214
builder .field (RETENTION_TIME .getPreferredName (), retentionTime );
214
215
builder .field (RETENTION_TIME_MILLIS .getPreferredName (), retentionTime .millis ());
215
216
216
- Map <String , SnapshotPolicyStats > metrics = getMetrics ();
217
- long totalTaken = metrics .values ().stream ().mapToLong (s -> s .snapshotsTaken .count ()).sum ();
218
- long totalFailed = metrics .values ().stream ().mapToLong (s -> s .snapshotsFailed .count ()).sum ();
219
- long totalDeleted = metrics .values ().stream ().mapToLong (s -> s .snapshotsDeleted .count ()).sum ();
220
- long totalDeleteFailures = metrics .values ().stream ().mapToLong (s -> s .snapshotDeleteFailures .count ()).sum ();
217
+ List <SnapshotPolicyStats > metrics = getMetrics ().values ().stream ()
218
+ .sorted (Comparator .comparing (SnapshotPolicyStats ::getPolicyId )) // maintain a consistent order when serializing
219
+ .collect (Collectors .toList ());
220
+ long totalTaken = metrics .stream ().mapToLong (s -> s .snapshotsTaken .count ()).sum ();
221
+ long totalFailed = metrics .stream ().mapToLong (s -> s .snapshotsFailed .count ()).sum ();
222
+ long totalDeleted = metrics .stream ().mapToLong (s -> s .snapshotsDeleted .count ()).sum ();
223
+ long totalDeleteFailures = metrics .stream ().mapToLong (s -> s .snapshotDeleteFailures .count ()).sum ();
221
224
builder .field (TOTAL_TAKEN .getPreferredName (), totalTaken );
222
225
builder .field (TOTAL_FAILED .getPreferredName (), totalFailed );
223
226
builder .field (TOTAL_DELETIONS .getPreferredName (), totalDeleted );
224
227
builder .field (TOTAL_DELETION_FAILURES .getPreferredName (), totalDeleteFailures );
225
- builder . startObject ( POLICY_STATS . getPreferredName ());
226
- for ( Map . Entry < String , SnapshotPolicyStats > policy : metrics . entrySet ()) {
227
- SnapshotPolicyStats perPolicyMetrics = policy . getValue ();
228
- builder .startObject (perPolicyMetrics . policyId );
229
- perPolicyMetrics .toXContent (builder , params );
228
+
229
+ builder . startArray ( POLICY_STATS . getPreferredName ());
230
+ for ( SnapshotPolicyStats stats : metrics ) {
231
+ builder .startObject ();
232
+ stats .toXContent (builder , params );
230
233
builder .endObject ();
231
234
}
232
- builder .endObject ();
235
+ builder .endArray ();
233
236
builder .endObject ();
234
237
return builder ;
235
238
}
@@ -268,22 +271,25 @@ public static class SnapshotPolicyStats implements Writeable, ToXContentFragment
268
271
private final CounterMetric snapshotsDeleted = new CounterMetric ();
269
272
private final CounterMetric snapshotDeleteFailures = new CounterMetric ();
270
273
274
+ public static final ParseField POLICY_ID = new ParseField ("policy" );
271
275
public static final ParseField SNAPSHOTS_TAKEN = new ParseField ("snapshots_taken" );
272
276
public static final ParseField SNAPSHOTS_FAILED = new ParseField ("snapshots_failed" );
273
277
public static final ParseField SNAPSHOTS_DELETED = new ParseField ("snapshots_deleted" );
274
278
public static final ParseField SNAPSHOT_DELETION_FAILURES = new ParseField ("snapshot_deletion_failures" );
275
279
276
- private static final ConstructingObjectParser <SnapshotPolicyStats , String > PARSER =
280
+ static final ConstructingObjectParser <SnapshotPolicyStats , Void > PARSER =
277
281
new ConstructingObjectParser <>("snapshot_policy_stats" , true ,
278
- (a , id ) -> {
279
- long taken = (long ) a [0 ];
280
- long failed = (long ) a [1 ];
281
- long deleted = (long ) a [2 ];
282
- long deleteFailed = (long ) a [3 ];
282
+ a -> {
283
+ String id = (String ) a [0 ];
284
+ long taken = (long ) a [1 ];
285
+ long failed = (long ) a [2 ];
286
+ long deleted = (long ) a [3 ];
287
+ long deleteFailed = (long ) a [4 ];
283
288
return new SnapshotPolicyStats (id , taken , failed , deleted , deleteFailed );
284
289
});
285
290
286
291
static {
292
+ PARSER .declareString (ConstructingObjectParser .constructorArg (), POLICY_ID );
287
293
PARSER .declareLong (ConstructingObjectParser .constructorArg (), SNAPSHOTS_TAKEN );
288
294
PARSER .declareLong (ConstructingObjectParser .constructorArg (), SNAPSHOTS_FAILED );
289
295
PARSER .declareLong (ConstructingObjectParser .constructorArg (), SNAPSHOTS_DELETED );
@@ -310,8 +316,8 @@ public SnapshotPolicyStats(StreamInput in) throws IOException {
310
316
this .snapshotDeleteFailures .inc (in .readVLong ());
311
317
}
312
318
313
- public static SnapshotPolicyStats parse (XContentParser parser , String policyId ) {
314
- return PARSER .apply (parser , policyId );
319
+ public static SnapshotPolicyStats parse (XContentParser parser ) {
320
+ return PARSER .apply (parser , null );
315
321
}
316
322
317
323
public SnapshotPolicyStats merge (SnapshotPolicyStats other ) {
@@ -339,6 +345,10 @@ void snapshotDeleteFailure() {
339
345
snapshotDeleteFailures .inc ();
340
346
}
341
347
348
+ public String getPolicyId () {
349
+ return policyId ;
350
+ }
351
+
342
352
@ Override
343
353
public void writeTo (StreamOutput out ) throws IOException {
344
354
out .writeString (policyId );
@@ -372,6 +382,7 @@ public boolean equals(Object obj) {
372
382
373
383
@ Override
374
384
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
385
+ builder .field (SnapshotPolicyStats .POLICY_ID .getPreferredName (), policyId );
375
386
builder .field (SnapshotPolicyStats .SNAPSHOTS_TAKEN .getPreferredName (), snapshotsTaken .count ());
376
387
builder .field (SnapshotPolicyStats .SNAPSHOTS_FAILED .getPreferredName (), snapshotsFailed .count ());
377
388
builder .field (SnapshotPolicyStats .SNAPSHOTS_DELETED .getPreferredName (), snapshotsDeleted .count ());
0 commit comments