Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record most recent snapshot policy success/failure #40619

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

package org.elasticsearch.xpack.core.snapshotlifecycle;

import org.elasticsearch.cluster.AbstractDiffable;
import org.elasticsearch.cluster.Diffable;
import org.elasticsearch.common.ParseField;
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.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;

/**
* Holds information about Snapshots kicked off by Snapshot Lifecycle Management in the cluster state, so that this information can be
* presented to the user. This class is used for both successes and failures as the structure of the data is very similar.
*/
public class SnapshotInvocationRecord extends AbstractDiffable<SnapshotInvocationRecord>
gwbrown marked this conversation as resolved.
Show resolved Hide resolved
implements Writeable, ToXContentObject, Diffable<SnapshotInvocationRecord> {

static final ParseField SNAPSHOT_NAME = new ParseField("snapshot_name");
static final ParseField TIMESTAMP = new ParseField("time");
static final ParseField DETAILS = new ParseField("details");

private String snapshotName;
private long timestamp;
private String details;

public static final ConstructingObjectParser<SnapshotInvocationRecord, String> PARSER =
new ConstructingObjectParser<>("snapshot_policy_invocation_record", true,
a -> new SnapshotInvocationRecord((String) a[0], (long) a[1], (String) a[2]));

static {
PARSER.declareString(ConstructingObjectParser.constructorArg(), SNAPSHOT_NAME);
PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIMESTAMP);
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), DETAILS);
}

public static SnapshotInvocationRecord parse(XContentParser parser, String name) {
return PARSER.apply(parser, name);
}

public SnapshotInvocationRecord(String snapshotName, long timestamp, String details) {
this.snapshotName = Objects.requireNonNull(snapshotName, "snapshot name must be provided");
this.timestamp = timestamp;
this.details = details;
}

public SnapshotInvocationRecord(StreamInput in) throws IOException {
this.snapshotName = in.readString();
this.timestamp = in.readVLong();
this.details = in.readOptionalString();
}

public String getSnapshotName() {
return snapshotName;
}

public long getTimestamp() {
return timestamp;
}

public String getDetails() {
return details;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(snapshotName);
out.writeVLong(timestamp);
out.writeOptionalString(details);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
{
builder.field(SNAPSHOT_NAME.getPreferredName(), snapshotName);
builder.timeField(TIMESTAMP.getPreferredName(), "time_string", timestamp);
if (Objects.nonNull(details)) {
builder.field(DETAILS.getPreferredName(), details);
}
}
builder.endObject();
return builder;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SnapshotInvocationRecord that = (SnapshotInvocationRecord) o;
return getTimestamp() == that.getTimestamp() &&
Objects.equals(getSnapshotName(), that.getSnapshotName()) &&
Objects.equals(getDetails(), that.getDetails());
}

@Override
public int hashCode() {
return Objects.hash(getSnapshotName(), getTimestamp(), getDetails());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class SnapshotLifecyclePolicy extends AbstractDiffable<SnapshotLifecycleP

public SnapshotLifecyclePolicy(final String id, final String name, final String schedule,
final String repository, Map<String, Object> configuration) {
this.id = id;
this.id = Objects.requireNonNull(id);
this.name = name;
this.schedule = schedule;
this.repository = repository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.elasticsearch.cluster.AbstractDiffable;
import org.elasticsearch.cluster.Diffable;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
Expand All @@ -21,8 +22,10 @@
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
* {@code SnapshotLifecyclePolicyMetadata} encapsulates a {@link SnapshotLifecyclePolicy} as well as
Expand All @@ -37,18 +40,34 @@ public class SnapshotLifecyclePolicyMetadata extends AbstractDiffable<SnapshotLi
static final ParseField VERSION = new ParseField("version");
static final ParseField MODIFIED_DATE = new ParseField("modified_date");
static final ParseField MODIFIED_DATE_STRING = new ParseField("modified_date_string");
static final ParseField LAST_SUCCESS = new ParseField("last_success");
static final ParseField LAST_FAILURE = new ParseField("last_failure");

private final SnapshotLifecyclePolicy policy;
private final Map<String, String> headers;
private final long version;
private final long modifiedDate;
@Nullable
private final SnapshotInvocationRecord lastSuccess;
@Nullable
private final SnapshotInvocationRecord lastFailure;

@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<SnapshotLifecyclePolicyMetadata, String> PARSER =
new ConstructingObjectParser<>("snapshot_policy_metadata",
a -> {
SnapshotLifecyclePolicy policy = (SnapshotLifecyclePolicy) a[0];
return new SnapshotLifecyclePolicyMetadata(policy, (Map<String, String>) a[1], (long) a[2], (long) a[3]);
SnapshotInvocationRecord lastSuccess = (SnapshotInvocationRecord) a[5];
SnapshotInvocationRecord lastFailure = (SnapshotInvocationRecord) a[6];

return builder()
.setPolicy(policy)
.setHeaders((Map<String, String>) a[1])
.setVersion((long) a[2])
.setModifiedDate((long) a[3])
.setLastSuccess(lastSuccess)
.setLastFailure(lastFailure)
.build();
});

static {
Expand All @@ -57,17 +76,22 @@ public class SnapshotLifecyclePolicyMetadata extends AbstractDiffable<SnapshotLi
PARSER.declareLong(ConstructingObjectParser.constructorArg(), VERSION);
PARSER.declareLong(ConstructingObjectParser.constructorArg(), MODIFIED_DATE);
PARSER.declareString(ConstructingObjectParser.constructorArg(), MODIFIED_DATE_STRING);
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), SnapshotInvocationRecord::parse, LAST_SUCCESS);
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), SnapshotInvocationRecord::parse, LAST_FAILURE);
}

public static SnapshotLifecyclePolicyMetadata parse(XContentParser parser, String name) {
return PARSER.apply(parser, name);
}

public SnapshotLifecyclePolicyMetadata(SnapshotLifecyclePolicy policy, Map<String, String> headers, long version, long modifiedDate) {
SnapshotLifecyclePolicyMetadata(SnapshotLifecyclePolicy policy, Map<String, String> headers, long version, long modifiedDate,
SnapshotInvocationRecord lastSuccess, SnapshotInvocationRecord lastFailure) {
this.policy = policy;
this.headers = headers;
this.version = version;
this.modifiedDate = modifiedDate;
this.lastSuccess = lastSuccess;
this.lastFailure = lastFailure;
}

@SuppressWarnings("unchecked")
Expand All @@ -76,6 +100,8 @@ public SnapshotLifecyclePolicyMetadata(SnapshotLifecyclePolicy policy, Map<Strin
this.headers = (Map<String, String>) in.readGenericValue();
this.version = in.readVLong();
this.modifiedDate = in.readVLong();
this.lastSuccess = in.readOptionalWriteable(SnapshotInvocationRecord::new);
this.lastFailure = in.readOptionalWriteable(SnapshotInvocationRecord::new);
}

@Override
Expand All @@ -84,6 +110,25 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeGenericValue(this.headers);
out.writeVLong(this.version);
out.writeVLong(this.modifiedDate);
out.writeOptionalWriteable(this.lastSuccess);
out.writeOptionalWriteable(this.lastFailure);
}

public static Builder builder() {
return new Builder();
}

public static Builder builder(SnapshotLifecyclePolicyMetadata metadata) {
if (metadata == null) {
return builder();
}
return new Builder()
.setHeaders(metadata.getHeaders())
.setPolicy(metadata.getPolicy())
.setVersion(metadata.getVersion())
.setModifiedDate(metadata.getModifiedDate())
.setLastSuccess(metadata.getLastSuccess())
.setLastFailure(metadata.getLastFailure());
}

public Map<String, String> getHeaders() {
Expand All @@ -106,9 +151,24 @@ public long getModifiedDate() {
return modifiedDate;
}

private String dateToDateString(Long date) {
if (Objects.isNull(date)) {
return null;
}
ZonedDateTime dateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneOffset.UTC);
return dateTime.toString();
}

public String getModifiedDateString() {
ZonedDateTime modifiedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(modifiedDate), ZoneOffset.UTC);
return modifiedDateTime.toString();
return dateToDateString(modifiedDate);
}

public SnapshotInvocationRecord getLastSuccess() {
return lastSuccess;
}

public SnapshotInvocationRecord getLastFailure() {
return lastFailure;
}

@Override
Expand All @@ -119,13 +179,19 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.field(VERSION.getPreferredName(), version);
builder.field(MODIFIED_DATE.getPreferredName(), modifiedDate);
builder.field(MODIFIED_DATE_STRING.getPreferredName(), getModifiedDateString());
if (Objects.nonNull(lastSuccess)) {
builder.field(LAST_SUCCESS.getPreferredName(), lastSuccess);
}
if (Objects.nonNull(lastFailure)) {
builder.field(LAST_FAILURE.getPreferredName(), lastFailure);
}
builder.endObject();
return builder;
}

@Override
public int hashCode() {
return Objects.hash(policy, headers, version, modifiedDate);
return Objects.hash(policy, headers, version, modifiedDate, lastSuccess, lastFailure);
}

@Override
Expand All @@ -140,7 +206,9 @@ public boolean equals(Object obj) {
return Objects.equals(policy, other.policy) &&
gwbrown marked this conversation as resolved.
Show resolved Hide resolved
Objects.equals(headers, other.headers) &&
Objects.equals(version, other.version) &&
Objects.equals(modifiedDate, other.modifiedDate);
Objects.equals(modifiedDate, other.modifiedDate) &&
Objects.equals(lastSuccess, other.lastSuccess) &&
Objects.equals(lastFailure, other.lastFailure);
}

@Override
Expand All @@ -150,4 +218,58 @@ public String toString() {
// should not emit them in case it accidentally gets logged.
return super.toString();
}

public static class Builder {

private Builder() {
}

private SnapshotLifecyclePolicy policy;
private Map<String, String> headers;
private long version = 1L;
private Long modifiedDate;
private SnapshotInvocationRecord lastSuccessDate;
private SnapshotInvocationRecord lastFailureDate;

public Builder setPolicy(SnapshotLifecyclePolicy policy) {
this.policy = policy;
return this;
}

public Builder setHeaders(Map<String, String> headers) {
this.headers = headers;
return this;
}

public Builder setVersion(long version) {
this.version = version;
return this;
}

public Builder setModifiedDate(long modifiedDate) {
this.modifiedDate = modifiedDate;
return this;
}

public Builder setLastSuccess(SnapshotInvocationRecord lastSuccessDate) {
this.lastSuccessDate = lastSuccessDate;
return this;
}

public Builder setLastFailure(SnapshotInvocationRecord lastFailureDate) {
this.lastFailureDate = lastFailureDate;
return this;
}

public SnapshotLifecyclePolicyMetadata build() {
return new SnapshotLifecyclePolicyMetadata(
Objects.requireNonNull(policy),
Optional.ofNullable(headers).orElse(new HashMap<>()),
version,
Objects.requireNonNull(modifiedDate, "modifiedDate must be set"),
lastSuccessDate,
lastFailureDate);
}
}

}
Loading