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

Add next_execution to SLM policy metadata #41221

Merged
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
Expand Up @@ -113,6 +113,11 @@ public Map<String, Object> getConfig() {
return this.configuration;
}

public long calculateNextExecution() {
final Cron schedule = new Cron(this.schedule);
return schedule.getNextValidTimeAfter(System.currentTimeMillis());
}

public ActionRequestValidationException validate() {
ActionRequestValidationException err = new ActionRequestValidationException();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,18 @@ public boolean equals(Object obj) {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(policy.getId());
builder.field("version", version);
builder.timeField("modified_date_millis", "modified_date", modifiedDate);
builder.field("policy", policy);
builder.field(SnapshotLifecyclePolicyMetadata.VERSION.getPreferredName(), version);
builder.timeField(SnapshotLifecyclePolicyMetadata.MODIFIED_DATE_MILLIS.getPreferredName(),
SnapshotLifecyclePolicyMetadata.MODIFIED_DATE.getPreferredName(), modifiedDate);
builder.field(SnapshotLifecyclePolicyMetadata.POLICY.getPreferredName(), policy);
if (lastSuccess != null) {
builder.field("last_success", lastSuccess);
builder.field(SnapshotLifecyclePolicyMetadata.LAST_SUCCESS.getPreferredName(), lastSuccess);
}
if (lastFailure != null) {
builder.field("last_failure", lastFailure);
builder.field(SnapshotLifecyclePolicyMetadata.LAST_FAILURE.getPreferredName(), lastFailure);
}
builder.timeField(SnapshotLifecyclePolicyMetadata.NEXT_EXECUTION_MILLIS.getPreferredName(),
SnapshotLifecyclePolicyMetadata.NEXT_EXECUTION.getPreferredName(), policy.calculateNextExecution());
builder.endObject();
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -38,10 +35,12 @@ public class SnapshotLifecyclePolicyMetadata extends AbstractDiffable<SnapshotLi
static final ParseField POLICY = new ParseField("policy");
static final ParseField HEADERS = new ParseField("headers");
static final ParseField VERSION = new ParseField("version");
static final ParseField MODIFIED_DATE_MILLIS = new ParseField("modified_date_millis");
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");
static final ParseField NEXT_EXECUTION_MILLIS = new ParseField("next_execution_millis");
static final ParseField NEXT_EXECUTION = new ParseField("next_execution");

private final SnapshotLifecyclePolicy policy;
private final Map<String, String> headers;
Expand All @@ -57,8 +56,8 @@ public class SnapshotLifecyclePolicyMetadata extends AbstractDiffable<SnapshotLi
new ConstructingObjectParser<>("snapshot_policy_metadata",
a -> {
SnapshotLifecyclePolicy policy = (SnapshotLifecyclePolicy) a[0];
SnapshotInvocationRecord lastSuccess = (SnapshotInvocationRecord) a[5];
SnapshotInvocationRecord lastFailure = (SnapshotInvocationRecord) a[6];
SnapshotInvocationRecord lastSuccess = (SnapshotInvocationRecord) a[4];
SnapshotInvocationRecord lastFailure = (SnapshotInvocationRecord) a[5];

return builder()
.setPolicy(policy)
Expand All @@ -74,8 +73,7 @@ public class SnapshotLifecyclePolicyMetadata extends AbstractDiffable<SnapshotLi
PARSER.declareObject(ConstructingObjectParser.constructorArg(), SnapshotLifecyclePolicy::parse, POLICY);
PARSER.declareField(ConstructingObjectParser.constructorArg(), XContentParser::mapStrings, HEADERS, ObjectParser.ValueType.OBJECT);
PARSER.declareLong(ConstructingObjectParser.constructorArg(), VERSION);
PARSER.declareLong(ConstructingObjectParser.constructorArg(), MODIFIED_DATE);
PARSER.declareString(ConstructingObjectParser.constructorArg(), MODIFIED_DATE_STRING);
PARSER.declareLong(ConstructingObjectParser.constructorArg(), MODIFIED_DATE_MILLIS);
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), SnapshotInvocationRecord::parse, LAST_SUCCESS);
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), SnapshotInvocationRecord::parse, LAST_FAILURE);
}
Expand Down Expand Up @@ -151,18 +149,6 @@ 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() {
return dateToDateString(modifiedDate);
}

public SnapshotInvocationRecord getLastSuccess() {
return lastSuccess;
}
Expand All @@ -177,8 +163,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.field(POLICY.getPreferredName(), policy);
builder.field(HEADERS.getPreferredName(), headers);
builder.field(VERSION.getPreferredName(), version);
builder.field(MODIFIED_DATE.getPreferredName(), modifiedDate);
builder.field(MODIFIED_DATE_STRING.getPreferredName(), getModifiedDateString());
builder.timeField(MODIFIED_DATE_MILLIS.getPreferredName(), MODIFIED_DATE.getPreferredName(), modifiedDate);
if (Objects.nonNull(lastSuccess)) {
builder.field(LAST_SUCCESS.getPreferredName(), lastSuccess);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected Writeable.Reader<SnapshotLifecyclePolicyMetadata> instanceReader() {

@Override
protected SnapshotLifecyclePolicyMetadata mutateInstance(SnapshotLifecyclePolicyMetadata instance) throws IOException {
switch (between(0, 4)) {
switch (between(0, 5)) {
case 0:
return SnapshotLifecyclePolicyMetadata.builder(instance)
.setPolicy(randomValueOtherThan(instance.getPolicy(), () -> createRandomPolicy(randomAlphaOfLength(10))))
Expand All @@ -58,14 +58,18 @@ protected SnapshotLifecyclePolicyMetadata mutateInstance(SnapshotLifecyclePolicy
.build();
case 2:
return SnapshotLifecyclePolicyMetadata.builder(instance)
.setHeaders(randomValueOtherThan(instance.getHeaders(), SnapshotLifecyclePolicyMetadataTests::randomHeaders))
.setModifiedDate(randomValueOtherThan(instance.getModifiedDate(), ESTestCase::randomNonNegativeLong))
.build();
case 3:
return SnapshotLifecyclePolicyMetadata.builder(instance)
.setHeaders(randomValueOtherThan(instance.getHeaders(), SnapshotLifecyclePolicyMetadataTests::randomHeaders))
.build();
case 4:
return SnapshotLifecyclePolicyMetadata.builder(instance)
.setLastSuccess(randomValueOtherThan(instance.getLastSuccess(),
SnapshotInvocationRecordTests::randomSnapshotInvocationRecord))
.build();
case 4:
case 5:
return SnapshotLifecyclePolicyMetadata.builder(instance)
.setLastFailure(randomValueOtherThan(instance.getLastFailure(),
SnapshotInvocationRecordTests::randomSnapshotInvocationRecord))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Map;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.startsWith;

Expand All @@ -43,6 +44,11 @@ public void testNameGeneration() {
assertThat(p.generateSnapshotName(context), startsWith("name-2019-03-15.21:09:00-"));
}

public void testNextExecutionTime() {
SnapshotLifecyclePolicy p = new SnapshotLifecyclePolicy("id", "name", "0 1 2 3 4 ? 2099", "repo", Collections.emptyMap());
assertThat(p.calculateNextExecution(), equalTo(4078864860000L));
}

public void testValidation() {
SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy("a,b", "<my, snapshot-{now/M}>",
"* * * * * L", " ", Collections.emptyMap());
Expand Down