|
@@ -42,6 +42,7 @@ import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.Comparator;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
/**
|
|
@@ -51,6 +52,7 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
|
|
|
public static final String CONTEXT_MODE_PARAM = "context_mode";
|
|
|
public static final String CONTEXT_MODE_SNAPSHOT = "SNAPSHOT";
|
|
|
+ public static final Version METADATA_FIELD_INTRODUCED = Version.V_8_0_0; // TODO Set this to the earliest version this is backported to
|
|
|
private static final DateFormatter DATE_TIME_FORMATTER = DateFormatter.forPattern("strictDateOptionalTime");
|
|
|
private static final String SNAPSHOT = "snapshot";
|
|
|
private static final String UUID = "uuid";
|
|
@@ -74,6 +76,7 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
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 Comparator<SnapshotInfo> COMPARATOR =
|
|
|
Comparator.comparing(SnapshotInfo::startTime).thenComparing(SnapshotInfo::snapshotId);
|
|
@@ -88,6 +91,7 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
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;
|
|
|
|
|
@@ -127,6 +131,10 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
this.includeGlobalState = includeGlobalState;
|
|
|
}
|
|
|
|
|
|
+ private void setUserMetadata(Map<String, Object> userMetadata) {
|
|
|
+ this.userMetadata = userMetadata;
|
|
|
+ }
|
|
|
+
|
|
|
private void setVersion(int version) {
|
|
|
this.version = version;
|
|
|
}
|
|
@@ -153,7 +161,7 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
}
|
|
|
|
|
|
return new SnapshotInfo(snapshotId, indices, snapshotState, reason, version, startTime, endTime,
|
|
|
- totalShards, successfulShards, shardFailures, includeGlobalState);
|
|
|
+ totalShards, successfulShards, shardFailures, includeGlobalState, userMetadata);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -194,6 +202,7 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
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));
|
|
@@ -223,6 +232,9 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
@Nullable
|
|
|
private final Boolean includeGlobalState;
|
|
|
|
|
|
+ @Nullable
|
|
|
+ private final Map<String, Object> userMetadata;
|
|
|
+
|
|
|
@Nullable
|
|
|
private final Version version;
|
|
|
|
|
@@ -230,28 +242,30 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
|
|
|
public SnapshotInfo(SnapshotId snapshotId, List<String> indices, SnapshotState state) {
|
|
|
this(snapshotId, indices, state, null, null, 0L, 0L, 0, 0,
|
|
|
- Collections.emptyList(), null);
|
|
|
+ Collections.emptyList(), null, null);
|
|
|
}
|
|
|
|
|
|
public SnapshotInfo(SnapshotId snapshotId, List<String> indices, SnapshotState state, Version version) {
|
|
|
this(snapshotId, indices, state, null, version, 0L, 0L, 0, 0,
|
|
|
- Collections.emptyList(), null);
|
|
|
+ Collections.emptyList(), null, null);
|
|
|
}
|
|
|
|
|
|
- public SnapshotInfo(SnapshotId snapshotId, List<String> indices, long startTime, Boolean includeGlobalState) {
|
|
|
+ public SnapshotInfo(SnapshotId snapshotId, List<String> indices, long startTime, Boolean includeGlobalState,
|
|
|
+ Map<String, Object> userMetadata) {
|
|
|
this(snapshotId, indices, SnapshotState.IN_PROGRESS, null, Version.CURRENT, startTime, 0L,
|
|
|
- 0, 0, Collections.emptyList(), includeGlobalState);
|
|
|
+ 0, 0, Collections.emptyList(), includeGlobalState, userMetadata);
|
|
|
}
|
|
|
|
|
|
public SnapshotInfo(SnapshotId snapshotId, List<String> indices, long startTime, String reason, long endTime,
|
|
|
- int totalShards, List<SnapshotShardFailure> shardFailures, Boolean includeGlobalState) {
|
|
|
+ int totalShards, List<SnapshotShardFailure> shardFailures, Boolean includeGlobalState,
|
|
|
+ Map<String, Object> userMetadata) {
|
|
|
this(snapshotId, indices, snapshotState(reason, shardFailures), reason, Version.CURRENT,
|
|
|
- startTime, endTime, totalShards, totalShards - shardFailures.size(), shardFailures, includeGlobalState);
|
|
|
+ startTime, endTime, totalShards, totalShards - shardFailures.size(), shardFailures, includeGlobalState, userMetadata);
|
|
|
}
|
|
|
|
|
|
private SnapshotInfo(SnapshotId snapshotId, List<String> indices, SnapshotState state, String reason, Version version,
|
|
|
long startTime, long endTime, int totalShards, int successfulShards, List<SnapshotShardFailure> shardFailures,
|
|
|
- Boolean includeGlobalState) {
|
|
|
+ Boolean includeGlobalState, Map<String, Object> userMetadata) {
|
|
|
this.snapshotId = Objects.requireNonNull(snapshotId);
|
|
|
this.indices = Collections.unmodifiableList(Objects.requireNonNull(indices));
|
|
|
this.state = state;
|
|
@@ -263,6 +277,7 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
this.successfulShards = successfulShards;
|
|
|
this.shardFailures = Objects.requireNonNull(shardFailures);
|
|
|
this.includeGlobalState = includeGlobalState;
|
|
|
+ this.userMetadata = userMetadata;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -294,6 +309,11 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
}
|
|
|
version = in.readBoolean() ? Version.readVersion(in) : null;
|
|
|
includeGlobalState = in.readOptionalBoolean();
|
|
|
+ if (in.getVersion().onOrAfter(METADATA_FIELD_INTRODUCED)) {
|
|
|
+ userMetadata = in.readMap();
|
|
|
+ } else {
|
|
|
+ userMetadata = null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -304,7 +324,7 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
return new SnapshotInfo(snapshotId, Collections.emptyList(), SnapshotState.INCOMPATIBLE,
|
|
|
"the snapshot is incompatible with the current version of Elasticsearch and its exact version is unknown",
|
|
|
null, 0L, 0L, 0, 0,
|
|
|
- Collections.emptyList(), null);
|
|
|
+ Collections.emptyList(), null, null);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -428,6 +448,15 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
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;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Compares two snapshots by their start time; if the start times are the same, then
|
|
|
* compares the two snapshots by their snapshot ids.
|
|
@@ -492,6 +521,9 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
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);
|
|
|
}
|
|
@@ -543,6 +575,7 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
if (includeGlobalState != null) {
|
|
|
builder.field(INCLUDE_GLOBAL_STATE, includeGlobalState);
|
|
|
}
|
|
|
+ builder.field(USER_METADATA, userMetadata);
|
|
|
builder.field(START_TIME, startTime);
|
|
|
builder.field(END_TIME, endTime);
|
|
|
builder.field(TOTAL_SHARDS, totalShards);
|
|
@@ -573,6 +606,7 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
int totalShards = 0;
|
|
|
int successfulShards = 0;
|
|
|
Boolean includeGlobalState = null;
|
|
|
+ Map<String, Object> userMetadata = null;
|
|
|
List<SnapshotShardFailure> shardFailures = Collections.emptyList();
|
|
|
if (parser.currentToken() == null) { // fresh parser? move to the first token
|
|
|
parser.nextToken();
|
|
@@ -628,8 +662,12 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
parser.skipChildren();
|
|
|
}
|
|
|
} else if (token == XContentParser.Token.START_OBJECT) {
|
|
|
- // It was probably created by newer version - ignoring
|
|
|
- parser.skipChildren();
|
|
|
+ if (USER_METADATA.equals(currentFieldName)) {
|
|
|
+ userMetadata = parser.map();
|
|
|
+ } else {
|
|
|
+ // It was probably created by newer version - ignoring
|
|
|
+ parser.skipChildren();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -651,7 +689,8 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
totalShards,
|
|
|
successfulShards,
|
|
|
shardFailures,
|
|
|
- includeGlobalState);
|
|
|
+ includeGlobalState,
|
|
|
+ userMetadata);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -683,6 +722,9 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
out.writeBoolean(false);
|
|
|
}
|
|
|
out.writeOptionalBoolean(includeGlobalState);
|
|
|
+ if (out.getVersion().onOrAfter(METADATA_FIELD_INTRODUCED)) {
|
|
|
+ out.writeMap(userMetadata);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private static SnapshotState snapshotState(final String reason, final List<SnapshotShardFailure> shardFailures) {
|
|
@@ -712,13 +754,14 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
|
|
|
Objects.equals(indices, that.indices) &&
|
|
|
Objects.equals(includeGlobalState, that.includeGlobalState) &&
|
|
|
Objects.equals(version, that.version) &&
|
|
|
- Objects.equals(shardFailures, that.shardFailures);
|
|
|
+ Objects.equals(shardFailures, that.shardFailures) &&
|
|
|
+ Objects.equals(userMetadata, that.userMetadata);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int hashCode() {
|
|
|
|
|
|
return Objects.hash(snapshotId, state, reason, indices, startTime, endTime,
|
|
|
- totalShards, successfulShards, includeGlobalState, version, shardFailures);
|
|
|
+ totalShards, successfulShards, includeGlobalState, version, shardFailures, userMetadata);
|
|
|
}
|
|
|
}
|