-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
SnapshotInfo.java
1195 lines (1074 loc) · 42.2 KB
/
SnapshotInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.snapshots;
import org.elasticsearch.Version;
import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest;
import org.elasticsearch.cluster.SnapshotsInProgress;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.xcontent.XContentParserUtils;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.repositories.RepositoryShardId;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.ToXContentFragment;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;
import java.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Information about a snapshot
*/
public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContentFragment, Writeable {
public static final String INDEX_DETAILS_XCONTENT_PARAM = "index_details";
public static final String INDEX_NAMES_XCONTENT_PARAM = "index_names";
public static final String INCLUDE_REPOSITORY_XCONTENT_PARAM = "include_repository";
private static final DateFormatter DATE_TIME_FORMATTER = DateFormatter.forPattern("strict_date_optional_time");
private static final String SNAPSHOT = "snapshot";
private static final String UUID = "uuid";
private static final String REPOSITORY = "repository";
private static final String INDICES = "indices";
private static final String DATA_STREAMS = "data_streams";
private static final String STATE = "state";
private static final String REASON = "reason";
private static final String START_TIME = "start_time";
private static final String START_TIME_IN_MILLIS = "start_time_in_millis";
private static final String END_TIME = "end_time";
private static final String END_TIME_IN_MILLIS = "end_time_in_millis";
private static final String DURATION = "duration";
private static final String DURATION_IN_MILLIS = "duration_in_millis";
private static final String FAILURES = "failures";
private static final String SHARDS = "shards";
private static final String TOTAL = "total";
private static final String FAILED = "failed";
private static final String SUCCESSFUL = "successful";
private static final String VERSION_ID = "version_id";
private static final String VERSION = "version";
private static final String NAME = "name";
private static final String TOTAL_SHARDS = "total_shards";
private static final String SUCCESSFUL_SHARDS = "successful_shards";
private static final String INCLUDE_GLOBAL_STATE = "include_global_state";
private static final String USER_METADATA = "metadata";
private static final String FEATURE_STATES = "feature_states";
private static final String INDEX_DETAILS = "index_details";
private static final String UNKNOWN_REPO_NAME = "_na_";
private static final Comparator<SnapshotInfo> COMPARATOR = Comparator.comparing(SnapshotInfo::startTime)
.thenComparing(SnapshotInfo::snapshotId);
public static final class SnapshotInfoBuilder {
private String snapshotName = null;
private String snapshotUUID = null;
private String repository = UNKNOWN_REPO_NAME;
private String state = null;
private String reason = null;
private List<String> indices = null;
private List<String> dataStreams = null;
private List<SnapshotFeatureInfo> featureStates = null;
private Map<String, IndexSnapshotDetails> indexSnapshotDetails = null;
private long startTime = 0L;
private long endTime = 0L;
private ShardStatsBuilder shardStatsBuilder = null;
private Boolean includeGlobalState = null;
private Map<String, Object> userMetadata = null;
private int version = -1;
private List<SnapshotShardFailure> shardFailures = null;
private void setSnapshotName(String snapshotName) {
this.snapshotName = snapshotName;
}
private void setSnapshotUUID(String snapshotUUID) {
this.snapshotUUID = snapshotUUID;
}
private void setRepository(String repository) {
this.repository = repository;
}
private void setState(String state) {
this.state = state;
}
private void setReason(String reason) {
this.reason = reason;
}
private void setIndices(List<String> indices) {
this.indices = indices;
}
private void setDataStreams(List<String> dataStreams) {
this.dataStreams = dataStreams;
}
private void setFeatureStates(List<SnapshotFeatureInfo> featureStates) {
this.featureStates = featureStates;
}
private void setIndexSnapshotDetails(Map<String, IndexSnapshotDetails> indexSnapshotDetails) {
this.indexSnapshotDetails = indexSnapshotDetails;
}
private void setStartTime(long startTime) {
this.startTime = startTime;
}
private void setEndTime(long endTime) {
this.endTime = endTime;
}
private void setShardStatsBuilder(ShardStatsBuilder shardStatsBuilder) {
this.shardStatsBuilder = shardStatsBuilder;
}
private void setIncludeGlobalState(Boolean includeGlobalState) {
this.includeGlobalState = includeGlobalState;
}
private void setUserMetadata(Map<String, Object> userMetadata) {
this.userMetadata = userMetadata;
}
private void setVersion(int version) {
this.version = version;
}
private void setShardFailures(List<SnapshotShardFailure> shardFailures) {
this.shardFailures = shardFailures;
}
public SnapshotInfo build() {
final Snapshot snapshot = new Snapshot(repository, new SnapshotId(snapshotName, snapshotUUID));
if (indices == null) {
indices = Collections.emptyList();
}
if (dataStreams == null) {
dataStreams = Collections.emptyList();
}
if (featureStates == null) {
featureStates = Collections.emptyList();
}
if (indexSnapshotDetails == null) {
indexSnapshotDetails = Collections.emptyMap();
}
SnapshotState snapshotState = state == null ? null : SnapshotState.valueOf(state);
Version version = this.version == -1 ? Version.CURRENT : Version.fromId(this.version);
int totalShards = shardStatsBuilder == null ? 0 : shardStatsBuilder.getTotalShards();
int successfulShards = shardStatsBuilder == null ? 0 : shardStatsBuilder.getSuccessfulShards();
if (shardFailures == null) {
shardFailures = new ArrayList<>();
}
return new SnapshotInfo(
snapshot,
indices,
dataStreams,
featureStates,
reason,
version,
startTime,
endTime,
totalShards,
successfulShards,
shardFailures,
includeGlobalState,
userMetadata,
snapshotState,
indexSnapshotDetails
);
}
}
private static final class ShardStatsBuilder {
private int totalShards;
private int successfulShards;
private void setTotalShards(int totalShards) {
this.totalShards = totalShards;
}
int getTotalShards() {
return totalShards;
}
private void setSuccessfulShards(int successfulShards) {
this.successfulShards = successfulShards;
}
int getSuccessfulShards() {
return successfulShards;
}
}
public static final ObjectParser<SnapshotInfoBuilder, Void> SNAPSHOT_INFO_PARSER = new ObjectParser<>(
SnapshotInfoBuilder.class.getName(),
true,
SnapshotInfoBuilder::new
);
private static final ObjectParser<ShardStatsBuilder, Void> SHARD_STATS_PARSER = new ObjectParser<>(
ShardStatsBuilder.class.getName(),
true,
ShardStatsBuilder::new
);
static {
SNAPSHOT_INFO_PARSER.declareString(SnapshotInfoBuilder::setSnapshotName, new ParseField(SNAPSHOT));
SNAPSHOT_INFO_PARSER.declareString(SnapshotInfoBuilder::setSnapshotUUID, new ParseField(UUID));
SNAPSHOT_INFO_PARSER.declareString(SnapshotInfoBuilder::setRepository, new ParseField(REPOSITORY));
SNAPSHOT_INFO_PARSER.declareString(SnapshotInfoBuilder::setState, new ParseField(STATE));
SNAPSHOT_INFO_PARSER.declareString(SnapshotInfoBuilder::setReason, new ParseField(REASON));
SNAPSHOT_INFO_PARSER.declareStringArray(SnapshotInfoBuilder::setIndices, new ParseField(INDICES));
SNAPSHOT_INFO_PARSER.declareStringArray(SnapshotInfoBuilder::setDataStreams, new ParseField(DATA_STREAMS));
SNAPSHOT_INFO_PARSER.declareObjectArray(
SnapshotInfoBuilder::setFeatureStates,
SnapshotFeatureInfo.SNAPSHOT_FEATURE_INFO_PARSER,
new ParseField(FEATURE_STATES)
);
SNAPSHOT_INFO_PARSER.declareObject(
SnapshotInfoBuilder::setIndexSnapshotDetails,
(p, c) -> p.map(HashMap::new, p2 -> IndexSnapshotDetails.PARSER.parse(p2, c)),
new ParseField(INDEX_DETAILS)
);
SNAPSHOT_INFO_PARSER.declareLong(SnapshotInfoBuilder::setStartTime, new ParseField(START_TIME_IN_MILLIS));
SNAPSHOT_INFO_PARSER.declareLong(SnapshotInfoBuilder::setEndTime, new ParseField(END_TIME_IN_MILLIS));
SNAPSHOT_INFO_PARSER.declareObject(SnapshotInfoBuilder::setShardStatsBuilder, SHARD_STATS_PARSER, new ParseField(SHARDS));
SNAPSHOT_INFO_PARSER.declareBoolean(SnapshotInfoBuilder::setIncludeGlobalState, new ParseField(INCLUDE_GLOBAL_STATE));
SNAPSHOT_INFO_PARSER.declareObject(SnapshotInfoBuilder::setUserMetadata, (p, c) -> p.map(), new ParseField(USER_METADATA));
SNAPSHOT_INFO_PARSER.declareInt(SnapshotInfoBuilder::setVersion, new ParseField(VERSION_ID));
SNAPSHOT_INFO_PARSER.declareObjectArray(
SnapshotInfoBuilder::setShardFailures,
SnapshotShardFailure.SNAPSHOT_SHARD_FAILURE_PARSER,
new ParseField(FAILURES)
);
SHARD_STATS_PARSER.declareInt(ShardStatsBuilder::setTotalShards, new ParseField(TOTAL));
SHARD_STATS_PARSER.declareInt(ShardStatsBuilder::setSuccessfulShards, new ParseField(SUCCESSFUL));
}
private final Snapshot snapshot;
@Nullable
private final SnapshotState state;
@Nullable
private final String reason;
private final List<String> indices;
private final List<String> dataStreams;
private final List<SnapshotFeatureInfo> featureStates;
private final long startTime;
private final long endTime;
private final int totalShards;
private final int successfulShards;
@Nullable
private final Boolean includeGlobalState;
@Nullable
private final Map<String, Object> userMetadata;
@Nullable
private final Version version;
private final List<SnapshotShardFailure> shardFailures;
private final Map<String, IndexSnapshotDetails> indexSnapshotDetails;
public SnapshotInfo(
Snapshot snapshot,
List<String> indices,
List<String> dataStreams,
List<SnapshotFeatureInfo> featureStates,
SnapshotState state
) {
this(
snapshot,
indices,
dataStreams,
featureStates,
null,
null,
0L,
0L,
0,
0,
Collections.emptyList(),
null,
null,
state,
Collections.emptyMap()
);
}
public SnapshotInfo(
Snapshot snapshot,
List<String> indices,
List<String> dataStreams,
List<SnapshotFeatureInfo> featureStates,
Version version,
SnapshotState state
) {
this(
snapshot,
indices,
dataStreams,
featureStates,
null,
version,
0L,
0L,
0,
0,
Collections.emptyList(),
null,
null,
state,
Collections.emptyMap()
);
}
public static SnapshotInfo inProgress(SnapshotsInProgress.Entry entry) {
int successfulShards = 0;
List<SnapshotShardFailure> shardFailures = new ArrayList<>();
for (Map.Entry<RepositoryShardId, SnapshotsInProgress.ShardSnapshotStatus> c : entry.shardsByRepoShardId().entrySet()) {
if (c.getValue().state() == SnapshotsInProgress.ShardState.SUCCESS) {
successfulShards++;
} else if (c.getValue().state().failed() && c.getValue().state().completed()) {
shardFailures.add(new SnapshotShardFailure(c.getValue().nodeId(), entry.shardId(c.getKey()), c.getValue().reason()));
}
}
int totalShards = entry.shardsByRepoShardId().size();
return new SnapshotInfo(
entry.snapshot(),
List.copyOf(entry.indices().keySet()),
entry.dataStreams(),
entry.featureStates(),
null,
Version.CURRENT,
entry.startTime(),
0L,
totalShards,
successfulShards,
shardFailures,
entry.includeGlobalState(),
entry.userMetadata(),
SnapshotState.IN_PROGRESS,
Collections.emptyMap()
);
}
public SnapshotInfo(
Snapshot snapshot,
List<String> indices,
List<String> dataStreams,
List<SnapshotFeatureInfo> featureStates,
String reason,
long endTime,
int totalShards,
List<SnapshotShardFailure> shardFailures,
Boolean includeGlobalState,
Map<String, Object> userMetadata,
long startTime,
Map<String, IndexSnapshotDetails> indexSnapshotDetails
) {
this(
snapshot,
indices,
dataStreams,
featureStates,
reason,
Version.CURRENT,
startTime,
endTime,
totalShards,
totalShards - shardFailures.size(),
shardFailures,
includeGlobalState,
userMetadata,
snapshotState(reason, shardFailures),
indexSnapshotDetails
);
}
public SnapshotInfo(
Snapshot snapshot,
List<String> indices,
List<String> dataStreams,
List<SnapshotFeatureInfo> featureStates,
String reason,
Version version,
long startTime,
long endTime,
int totalShards,
int successfulShards,
List<SnapshotShardFailure> shardFailures,
Boolean includeGlobalState,
Map<String, Object> userMetadata,
SnapshotState state,
Map<String, IndexSnapshotDetails> indexSnapshotDetails
) {
this.snapshot = Objects.requireNonNull(snapshot);
this.indices = List.copyOf(indices);
this.dataStreams = List.copyOf(dataStreams);
this.featureStates = List.copyOf(featureStates);
this.state = state;
this.reason = reason;
this.version = version;
this.startTime = startTime;
this.endTime = endTime;
this.totalShards = totalShards;
this.successfulShards = successfulShards;
this.shardFailures = List.copyOf(shardFailures);
this.includeGlobalState = includeGlobalState;
this.userMetadata = userMetadata == null ? null : Map.copyOf(userMetadata);
this.indexSnapshotDetails = Map.copyOf(indexSnapshotDetails);
}
public SnapshotInfo withoutIndices() {
if (indices.isEmpty()) {
return this;
}
return new SnapshotInfo(
snapshot,
List.of(),
dataStreams,
featureStates,
reason,
version,
startTime,
endTime,
totalShards,
successfulShards,
shardFailures,
includeGlobalState,
userMetadata,
state,
indexSnapshotDetails
);
}
/**
* Constructs snapshot information from stream input
*/
public static SnapshotInfo readFrom(final StreamInput in) throws IOException {
final Snapshot snapshot;
if (in.getVersion().onOrAfter(GetSnapshotsRequest.PAGINATED_GET_SNAPSHOTS_VERSION)) {
snapshot = new Snapshot(in);
} else {
snapshot = new Snapshot(UNKNOWN_REPO_NAME, new SnapshotId(in));
}
final List<String> indices = in.readStringList();
final SnapshotState state = in.readBoolean() ? SnapshotState.fromValue(in.readByte()) : null;
final String reason = in.readOptionalString();
final long startTime = in.readVLong();
final long endTime = in.readVLong();
final int totalShards = in.readVInt();
final int successfulShards = in.readVInt();
final List<SnapshotShardFailure> shardFailures = in.readList(SnapshotShardFailure::new);
final Version version = in.readBoolean() ? Version.readVersion(in) : null;
final Boolean includeGlobalState = in.readOptionalBoolean();
final Map<String, Object> userMetadata = in.readMap();
final List<String> dataStreams = in.readStringList();
final List<SnapshotFeatureInfo> featureStates = in.readList(SnapshotFeatureInfo::new);
final Map<String, IndexSnapshotDetails> indexSnapshotDetails = in.readMap(StreamInput::readString, IndexSnapshotDetails::new);
return new SnapshotInfo(
snapshot,
indices,
dataStreams,
featureStates,
reason,
version,
startTime,
endTime,
totalShards,
successfulShards,
shardFailures,
includeGlobalState,
userMetadata,
state,
indexSnapshotDetails
);
}
/**
* Gets a new {@link SnapshotInfo} instance from the given {@link SnapshotInfo} with
* all information stripped out except the snapshot id, state, and indices.
*/
public SnapshotInfo basic() {
return new SnapshotInfo(snapshot, indices, Collections.emptyList(), featureStates, state);
}
public Snapshot snapshot() {
return snapshot;
}
/**
* Returns snapshot id
*
* @return snapshot id
*/
public SnapshotId snapshotId() {
return snapshot.getSnapshotId();
}
public String repository() {
return snapshot.getRepository();
}
/**
* Returns snapshot state; {@code null} if the state is unknown.
*
* @return snapshot state
*/
@Nullable
public SnapshotState state() {
return state;
}
/**
* Returns snapshot failure reason; {@code null} if the snapshot succeeded.
*
* @return snapshot failure reason
*/
@Nullable
public String reason() {
return reason;
}
/**
* Returns indices that were included in this snapshot.
*
* @return list of indices
*/
public List<String> indices() {
return indices;
}
/**
* @return list of data streams that were included in this snapshot.
*/
public List<String> dataStreams() {
return dataStreams;
}
/**
* Returns time when snapshot started; a value of {@code 0L} will be returned if
* {@link #state()} returns {@code null}.
*
* @return snapshot start time
*/
public long startTime() {
return startTime;
}
/**
* Returns time when snapshot ended; a value of {@code 0L} will be returned if the
* snapshot is still running or if {@link #state()} returns {@code null}.
*
* @return snapshot end time
*/
public long endTime() {
return endTime;
}
/**
* Returns total number of shards that were snapshotted; a value of {@code 0} will
* be returned if {@link #state()} returns {@code null}.
*
* @return number of shards
*/
public int totalShards() {
return totalShards;
}
/**
* Number of failed shards.
*
* @return number of failed shards
*/
public int failedShards() {
return shardFailures.size();
}
/**
* Returns total number of shards that were successfully snapshotted; a value of
* {@code 0} will be returned if {@link #state()} returns {@code null}.
*
* @return number of successful shards
*/
public int successfulShards() {
return successfulShards;
}
public Boolean includeGlobalState() {
return includeGlobalState;
}
/**
* Returns shard failures; an empty list will be returned if there were no shard
* failures, or if {@link #state()} returns {@code null}.
*
* @return shard failures
*/
public List<SnapshotShardFailure> shardFailures() {
return shardFailures;
}
/**
* Returns the version of elasticsearch that the snapshot was created with. Will only
* return {@code null} if {@link #state()} returns {@code null} or {@link SnapshotState#INCOMPATIBLE}.
*
* @return version of elasticsearch that the snapshot was created with
*/
@Nullable
public Version version() {
return version;
}
/**
* Returns the custom metadata that was attached to this snapshot at creation time.
* @return custom metadata
*/
@Nullable
public Map<String, Object> userMetadata() {
return userMetadata;
}
public List<SnapshotFeatureInfo> featureStates() {
return featureStates;
}
/**
* @return details of each index in the snapshot, if available, or an empty map otherwise.
*/
public Map<String, IndexSnapshotDetails> indexSnapshotDetails() {
return indexSnapshotDetails;
}
/**
* Compares two snapshots by their start time; if the start times are the same, then
* compares the two snapshots by their snapshot ids.
*/
@Override
public int compareTo(final SnapshotInfo o) {
return COMPARATOR.compare(this, o);
}
@Override
public String toString() {
return "SnapshotInfo{"
+ "snapshot="
+ snapshot
+ ", state="
+ state
+ ", reason='"
+ reason
+ '\''
+ ", indices="
+ indices
+ ", startTime="
+ startTime
+ ", endTime="
+ endTime
+ ", totalShards="
+ totalShards
+ ", successfulShards="
+ successfulShards
+ ", includeGlobalState="
+ includeGlobalState
+ ", version="
+ version
+ ", shardFailures="
+ shardFailures
+ ", featureStates="
+ featureStates
+ ", indexSnapshotDetails="
+ indexSnapshotDetails
+ '}';
}
/**
* Returns snapshot REST status
*/
public RestStatus status() {
if (state == SnapshotState.FAILED) {
return RestStatus.INTERNAL_SERVER_ERROR;
}
if (shardFailures.size() == 0) {
return RestStatus.OK;
}
return RestStatus.status(
successfulShards,
totalShards,
shardFailures.toArray(new ShardOperationFailedException[shardFailures.size()])
);
}
/**
* Serialize this {@link SnapshotInfo} for external consumption, i.e. REST responses, from which we don't need to be able to read it
* back again. This method builds a well-formed object, not a fragment like {@link #toXContent} does.
*/
public XContentBuilder toXContentExternal(final XContentBuilder builder, final ToXContent.Params params) throws IOException {
assert Metadata.CONTEXT_MODE_SNAPSHOT.equals(params.param(Metadata.CONTEXT_MODE_PARAM)) == false
: "use toXContent() in SNAPSHOT context";
final boolean verbose = params.paramAsBoolean("verbose", GetSnapshotsRequest.DEFAULT_VERBOSE_MODE);
// write snapshot info for the API and any other situations
builder.startObject();
final SnapshotId snapshotId = snapshot.getSnapshotId();
builder.field(SNAPSHOT, snapshotId.getName());
builder.field(UUID, snapshotId.getUUID());
if (params.paramAsBoolean(INCLUDE_REPOSITORY_XCONTENT_PARAM, true) && UNKNOWN_REPO_NAME.equals(snapshot.getRepository()) == false) {
builder.field(REPOSITORY, snapshot.getRepository());
}
if (version != null) {
builder.field(VERSION_ID, version.id);
builder.field(VERSION, version.toString());
}
if (params.paramAsBoolean(INDEX_NAMES_XCONTENT_PARAM, true)) {
builder.stringListField(INDICES, indices);
}
if (params.paramAsBoolean(INDEX_DETAILS_XCONTENT_PARAM, false) && indexSnapshotDetails.isEmpty() == false) {
builder.startObject(INDEX_DETAILS);
for (Map.Entry<String, IndexSnapshotDetails> entry : indexSnapshotDetails.entrySet()) {
builder.field(entry.getKey());
entry.getValue().toXContent(builder, params);
}
builder.endObject();
}
builder.startArray(DATA_STREAMS);
for (String dataStream : dataStreams) {
builder.value(dataStream);
}
builder.endArray();
if (includeGlobalState != null) {
builder.field(INCLUDE_GLOBAL_STATE, includeGlobalState);
}
if (userMetadata != null) {
builder.field(USER_METADATA, userMetadata);
}
if (verbose || state != null) {
builder.field(STATE, state);
}
if (reason != null) {
builder.field(REASON, reason);
}
if (verbose || startTime != 0) {
builder.field(START_TIME, DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(startTime).atZone(ZoneOffset.UTC)));
builder.field(START_TIME_IN_MILLIS, startTime);
}
if (verbose || endTime != 0) {
builder.field(END_TIME, DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(endTime).atZone(ZoneOffset.UTC)));
builder.field(END_TIME_IN_MILLIS, endTime);
builder.humanReadableField(DURATION_IN_MILLIS, DURATION, new TimeValue(Math.max(0L, endTime - startTime)));
}
if (verbose || shardFailures.isEmpty() == false) {
builder.startArray(FAILURES);
for (SnapshotShardFailure shardFailure : shardFailures) {
shardFailure.toXContent(builder, params);
}
builder.endArray();
}
if (verbose || totalShards != 0) {
builder.startObject(SHARDS);
builder.field(TOTAL, totalShards);
builder.field(FAILED, failedShards());
builder.field(SUCCESSFUL, successfulShards);
builder.endObject();
}
if (verbose || featureStates.isEmpty() == false) {
builder.startArray(FEATURE_STATES);
for (SnapshotFeatureInfo snapshotFeatureInfo : featureStates) {
builder.value(snapshotFeatureInfo);
}
builder.endArray();
}
builder.endObject();
return builder;
}
@Override
public XContentBuilder toXContent(final XContentBuilder builder, final ToXContent.Params params) throws IOException {
assert Metadata.CONTEXT_MODE_SNAPSHOT.equals(params.param(Metadata.CONTEXT_MODE_PARAM))
: "use toXContentExternal() in external context";
builder.startObject(SNAPSHOT);
final SnapshotId snapshotId = snapshot.getSnapshotId();
builder.field(NAME, snapshotId.getName());
builder.field(UUID, snapshotId.getUUID());
assert version != null : "version must always be known when writing a snapshot metadata blob";
builder.field(VERSION_ID, version.id);
builder.startArray(INDICES);
for (String index : indices) {
builder.value(index);
}
builder.endArray();
builder.startArray(DATA_STREAMS);
for (String dataStream : dataStreams) {
builder.value(dataStream);
}
builder.endArray();
builder.field(STATE, state);
if (reason != null) {
builder.field(REASON, reason);
}
if (includeGlobalState != null) {
builder.field(INCLUDE_GLOBAL_STATE, includeGlobalState);
}
if (userMetadata != null) {
builder.field(USER_METADATA, userMetadata);
}
builder.field(START_TIME, startTime);
builder.field(END_TIME, endTime);
builder.field(TOTAL_SHARDS, totalShards);
builder.field(SUCCESSFUL_SHARDS, successfulShards);
builder.startArray(FAILURES);
for (SnapshotShardFailure shardFailure : shardFailures) {
shardFailure.toXContent(builder, params);
}
builder.endArray();
builder.startArray(FEATURE_STATES);
for (SnapshotFeatureInfo snapshotFeatureInfo : featureStates) {
builder.value(snapshotFeatureInfo);
}
builder.endArray();
builder.startObject(INDEX_DETAILS);
for (Map.Entry<String, IndexSnapshotDetails> entry : indexSnapshotDetails.entrySet()) {
builder.field(entry.getKey());
entry.getValue().toXContent(builder, params);
}
builder.endObject();
builder.endObject();
return builder;
}
/**
* This method creates a SnapshotInfo from internal x-content. It does not
* handle x-content written with the external version as external x-content
* is only for display purposes and does not need to be parsed.
*/
public static SnapshotInfo fromXContentInternal(final String repoName, final XContentParser parser) throws IOException {
String name = null;
String uuid = null;
Version version = Version.CURRENT;
SnapshotState state = SnapshotState.IN_PROGRESS;
String reason = null;
List<String> indices = Collections.emptyList();
List<String> dataStreams = Collections.emptyList();
long startTime = 0;
long endTime = 0;
int totalShards = 0;
int successfulShards = 0;
Boolean includeGlobalState = null;
Map<String, Object> userMetadata = null;
List<SnapshotShardFailure> shardFailures = Collections.emptyList();
List<SnapshotFeatureInfo> featureStates = Collections.emptyList();
Map<String, IndexSnapshotDetails> indexSnapshotDetails = null;
if (parser.currentToken() == null) { // fresh parser? move to the first token
parser.nextToken();
}
if (parser.currentToken() == XContentParser.Token.START_OBJECT) { // on a start object move to next token
parser.nextToken();
}
XContentParserUtils.ensureFieldName(parser, parser.currentToken(), SNAPSHOT);
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
while (parser.nextToken() == XContentParser.Token.FIELD_NAME) {
final String currentFieldName = parser.currentName();
final XContentParser.Token token = parser.nextToken();
switch (currentFieldName) {
case NAME:
name = parser.text();
break;
case UUID:
uuid = parser.text();
break;
case STATE:
state = SnapshotState.valueOf(parser.text());
break;
case REASON:
reason = parser.text();
break;
case START_TIME:
startTime = parser.longValue();
break;
case END_TIME:
endTime = parser.longValue();
break;
case TOTAL_SHARDS:
totalShards = parser.intValue();
break;
case SUCCESSFUL_SHARDS:
successfulShards = parser.intValue();
break;
case VERSION_ID:
version = Version.fromId(parser.intValue());
break;
case INCLUDE_GLOBAL_STATE:
includeGlobalState = parser.booleanValue();
break;
case DATA_STREAMS:
dataStreams = XContentParserUtils.parseList(parser, XContentParser::text);
break;
case INDICES:
indices = XContentParserUtils.parseList(parser, XContentParser::text);
break;
case FAILURES:
shardFailures = XContentParserUtils.parseList(parser, SnapshotShardFailure::fromXContent);
break;
case FEATURE_STATES:
featureStates = XContentParserUtils.parseList(parser, SnapshotFeatureInfo::fromXContent);
break;
case USER_METADATA:
if (token != XContentParser.Token.VALUE_NULL) {
// some older versions a redundant null value for this field
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
userMetadata = parser.map();
}
break;
case INDEX_DETAILS:
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
indexSnapshotDetails = parser.map(HashMap::new, p -> IndexSnapshotDetails.PARSER.parse(p, null));
break;
default:
// It was probably created by newer version - ignoring
parser.skipChildren();
break;
}
}
// closing bracket of the object containing the "snapshot" field should be there
XContentParserUtils.ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser);
if (uuid == null) {
// the old format where there wasn't a UUID
uuid = name;
}
return new SnapshotInfo(
new Snapshot(repoName, new SnapshotId(name, uuid)),
indices,
dataStreams,