Skip to content

Commit

Permalink
Add next_execution to SLM policy metadata (#41221)
Browse files Browse the repository at this point in the history
* Add next_execution to SLM policy metadata

This adds the next time a snapshot lifecycle policy will be executed when
retriving a policy's metadata, for example:

```json
GET /_ilm/snapshot?human
{
  "production" : {
    "version" : 1,
    "modified_date" : "2019-04-15T21:16:21.865Z",
    "modified_date_millis" : 1555362981865,
    "policy" : {
      "name" : "<production-snap-{now/d}>",
      "schedule" : "*/30 * * * * ?",
      "repository" : "repo",
      "config" : {
        "indices" : [
          "foo-*",
          "important"
        ],
        "ignore_unavailable" : true,
        "include_global_state" : false
      }
    },
    "next_execution" : "2019-04-15T21:16:30.000Z",
    "next_execution_millis" : 1555362990000
  },
  "other" : {
    "version" : 1,
    "modified_date" : "2019-04-15T21:12:19.959Z",
    "modified_date_millis" : 1555362739959,
    "policy" : {
      "name" : "<other-snap-{now/d}>",
      "schedule" : "0 30 2 * * ?",
      "repository" : "repo",
      "config" : {
        "indices" : [
          "other"
        ],
        "ignore_unavailable" : false,
        "include_global_state" : true
      }
    },
    "next_execution" : "2019-04-16T02:30:00.000Z",
    "next_execution_millis" : 1555381800000
  }
}
```

Relates to #38461

* Fix and enhance tests

* Figured out how to Cron
  • Loading branch information
dakrone authored Apr 16, 2019
1 parent ead7146 commit 7ea37fe
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 30 deletions.
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

0 comments on commit 7ea37fe

Please sign in to comment.