2121
2222import java .io .IOException ;
2323import java .util .Collections ;
24+ import java .util .Comparator ;
2425import java .util .HashMap ;
2526import java .util .List ;
2627import java .util .Map ;
@@ -71,7 +72,7 @@ public class SnapshotLifecycleStats implements Writeable, ToXContentObject {
7172 PARSER .declareLong (ConstructingObjectParser .constructorArg (), RETENTION_FAILED );
7273 PARSER .declareLong (ConstructingObjectParser .constructorArg (), RETENTION_TIMED_OUT );
7374 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 );
7576 }
7677
7778 public SnapshotLifecycleStats () {
@@ -213,23 +214,25 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
213214 builder .field (RETENTION_TIME .getPreferredName (), retentionTime );
214215 builder .field (RETENTION_TIME_MILLIS .getPreferredName (), retentionTime .millis ());
215216
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 ();
221224 builder .field (TOTAL_TAKEN .getPreferredName (), totalTaken );
222225 builder .field (TOTAL_FAILED .getPreferredName (), totalFailed );
223226 builder .field (TOTAL_DELETIONS .getPreferredName (), totalDeleted );
224227 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 );
230233 builder .endObject ();
231234 }
232- builder .endObject ();
235+ builder .endArray ();
233236 builder .endObject ();
234237 return builder ;
235238 }
@@ -268,22 +271,25 @@ public static class SnapshotPolicyStats implements Writeable, ToXContentFragment
268271 private final CounterMetric snapshotsDeleted = new CounterMetric ();
269272 private final CounterMetric snapshotDeleteFailures = new CounterMetric ();
270273
274+ public static final ParseField POLICY_ID = new ParseField ("policy" );
271275 public static final ParseField SNAPSHOTS_TAKEN = new ParseField ("snapshots_taken" );
272276 public static final ParseField SNAPSHOTS_FAILED = new ParseField ("snapshots_failed" );
273277 public static final ParseField SNAPSHOTS_DELETED = new ParseField ("snapshots_deleted" );
274278 public static final ParseField SNAPSHOT_DELETION_FAILURES = new ParseField ("snapshot_deletion_failures" );
275279
276- private static final ConstructingObjectParser <SnapshotPolicyStats , String > PARSER =
280+ static final ConstructingObjectParser <SnapshotPolicyStats , Void > PARSER =
277281 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 ];
283288 return new SnapshotPolicyStats (id , taken , failed , deleted , deleteFailed );
284289 });
285290
286291 static {
292+ PARSER .declareString (ConstructingObjectParser .constructorArg (), POLICY_ID );
287293 PARSER .declareLong (ConstructingObjectParser .constructorArg (), SNAPSHOTS_TAKEN );
288294 PARSER .declareLong (ConstructingObjectParser .constructorArg (), SNAPSHOTS_FAILED );
289295 PARSER .declareLong (ConstructingObjectParser .constructorArg (), SNAPSHOTS_DELETED );
@@ -310,8 +316,8 @@ public SnapshotPolicyStats(StreamInput in) throws IOException {
310316 this .snapshotDeleteFailures .inc (in .readVLong ());
311317 }
312318
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 );
315321 }
316322
317323 public SnapshotPolicyStats merge (SnapshotPolicyStats other ) {
@@ -339,6 +345,10 @@ void snapshotDeleteFailure() {
339345 snapshotDeleteFailures .inc ();
340346 }
341347
348+ public String getPolicyId () {
349+ return policyId ;
350+ }
351+
342352 @ Override
343353 public void writeTo (StreamOutput out ) throws IOException {
344354 out .writeString (policyId );
@@ -372,6 +382,7 @@ public boolean equals(Object obj) {
372382
373383 @ Override
374384 public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
385+ builder .field (SnapshotPolicyStats .POLICY_ID .getPreferredName (), policyId );
375386 builder .field (SnapshotPolicyStats .SNAPSHOTS_TAKEN .getPreferredName (), snapshotsTaken .count ());
376387 builder .field (SnapshotPolicyStats .SNAPSHOTS_FAILED .getPreferredName (), snapshotsFailed .count ());
377388 builder .field (SnapshotPolicyStats .SNAPSHOTS_DELETED .getPreferredName (), snapshotsDeleted .count ());
0 commit comments