From b021acbd3232def7016cb8f714d41c62e82ad1e3 Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Wed, 5 Sep 2018 17:03:29 -0700 Subject: [PATCH 1/3] add notion of version and creation_date to LifecyclePolicyMetadata It is useful to keep track of which version of a policy is currently being executed by a specific index. For management purposes, it would also be useful to know at which time the latest version was inserted so that an audit trail is left for reconciling changes happening in ILM. --- .../LifecyclePolicyMetadata.java | 48 ++++++++++++++++--- .../action/GetLifecycleAction.java | 32 ++++++++----- .../LifecyclePolicyMetadataTests.java | 15 ++++-- .../action/GetLifecycleResponseTests.java | 25 +++++----- .../action/TransportGetLifecycleAction.java | 25 ++++++---- .../action/TransportPutLifecycleAction.java | 8 +++- .../ExecuteStepsUpdateTaskTests.java | 9 ++-- .../IndexLifecycleInitialisationIT.java | 22 +++++++++ .../IndexLifecycleMetadataTests.java | 9 ++-- .../IndexLifecycleRunnerTests.java | 22 +++++---- .../IndexLifecycleServiceTests.java | 9 ++-- .../MoveToErrorStepUpdateTaskTests.java | 3 +- .../MoveToNextStepUpdateTaskTests.java | 3 +- .../PolicyStepsRegistryTests.java | 12 +++-- .../rest-api-spec/test/ilm/10_basic.yml | 22 +++++---- 15 files changed, 190 insertions(+), 74 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadata.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadata.java index ca2c5f1a53571..18733aac3840e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadata.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadata.java @@ -18,23 +18,33 @@ 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.Map; import java.util.Objects; public class LifecyclePolicyMetadata extends AbstractDiffable implements ToXContentObject, Diffable { - public static final ParseField POLICY = new ParseField("policy"); - public static final ParseField HEADERS = new ParseField("headers"); + static final ParseField POLICY = new ParseField("policy"); + static final ParseField HEADERS = new ParseField("headers"); + static final ParseField VERSION = new ParseField("version"); + static final ParseField CREATION_DATE = new ParseField("creation_date"); + static final ParseField CREATION_DATE_STRING = new ParseField("creation_date_string"); + @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("policy_metadata", a -> { LifecyclePolicy policy = (LifecyclePolicy) a[0]; - return new LifecyclePolicyMetadata(policy, (Map) a[1]); + return new LifecyclePolicyMetadata(policy, (Map) a[1], (long) a[2], (long) a[3]); }); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), LifecyclePolicy::parse, POLICY); PARSER.declareField(ConstructingObjectParser.constructorArg(), XContentParser::mapStrings, HEADERS, ValueType.OBJECT); + PARSER.declareLong(ConstructingObjectParser.constructorArg(), VERSION); + PARSER.declareLong(ConstructingObjectParser.constructorArg(), CREATION_DATE); + PARSER.declareString(ConstructingObjectParser.constructorArg(), CREATION_DATE_STRING); } public static LifecyclePolicyMetadata parse(XContentParser parser, String name) { @@ -43,16 +53,22 @@ public static LifecyclePolicyMetadata parse(XContentParser parser, String name) private final LifecyclePolicy policy; private final Map headers; + private final long version; + private final long creationDate; - public LifecyclePolicyMetadata(LifecyclePolicy policy, Map headers) { + public LifecyclePolicyMetadata(LifecyclePolicy policy, Map headers, long version, long creationDate) { this.policy = policy; this.headers = headers; + this.version = version; + this.creationDate = creationDate; } @SuppressWarnings("unchecked") public LifecyclePolicyMetadata(StreamInput in) throws IOException { this.policy = new LifecyclePolicy(in); this.headers = (Map) in.readGenericValue(); + this.version = in.readVLong(); + this.creationDate = in.readVLong(); } public Map getHeaders() { @@ -67,11 +83,27 @@ public String getName() { return policy.getName(); } + public long getVersion() { + return version; + } + + public long getCreationDate() { + return creationDate; + } + + public String getCreationDateString() { + ZonedDateTime creationDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(creationDate), ZoneOffset.UTC); + return creationDateTime.toString(); + } + @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); builder.field(POLICY.getPreferredName(), policy); builder.field(HEADERS.getPreferredName(), headers); + builder.field(VERSION.getPreferredName(), version); + builder.field(CREATION_DATE.getPreferredName(), creationDate); + builder.field(CREATION_DATE_STRING.getPreferredName(), getCreationDateString()); builder.endObject(); return builder; } @@ -80,11 +112,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws public void writeTo(StreamOutput out) throws IOException { policy.writeTo(out); out.writeGenericValue(headers); + out.writeVLong(version); + out.writeVLong(creationDate); } @Override public int hashCode() { - return Objects.hash(policy, headers); + return Objects.hash(policy, headers, version, creationDate); } @Override @@ -97,7 +131,9 @@ public boolean equals(Object obj) { } LifecyclePolicyMetadata other = (LifecyclePolicyMetadata) obj; return Objects.equals(policy, other.policy) && - Objects.equals(headers, other.headers); + Objects.equals(headers, other.headers) && + Objects.equals(version, other.version) && + Objects.equals(creationDate, other.creationDate); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleAction.java index de3002994581e..2cb247342bfd9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleAction.java @@ -11,6 +11,7 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ToXContentObject; @@ -19,7 +20,7 @@ import java.io.IOException; import java.util.Arrays; -import java.util.List; +import java.util.Map; import java.util.Objects; public class GetLifecycleAction extends Action { @@ -37,24 +38,28 @@ public Response newResponse() { public static class Response extends ActionResponse implements ToXContentObject { - private List policies; + private Map> policiesToMetadata; public Response() { } - public Response(List policies) { - this.policies = policies; + public Response(Map> policiesToMetadata) { + this.policiesToMetadata = policiesToMetadata; } - public List getPolicies() { - return policies; + public Map> getPoliciesToMetadata() { + return policiesToMetadata; } @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); - for (LifecyclePolicy policy : policies) { - builder.field(policy.getName(), policy); + for (Map.Entry> entry : policiesToMetadata.entrySet()) { + builder.startObject(entry.getKey().getName()); + builder.field("version", entry.getValue().v1()); + builder.field("creation_date", entry.getValue().v2()); + builder.field("policy", entry.getKey()); + builder.endObject(); } builder.endObject(); return builder; @@ -62,17 +67,20 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public void readFrom(StreamInput in) throws IOException { - policies = in.readList(LifecyclePolicy::new); + policiesToMetadata = in.readMap(LifecyclePolicy::new, (inn) -> new Tuple<>(inn.readVLong(), inn.readString())); } @Override public void writeTo(StreamOutput out) throws IOException { - out.writeList(policies); + out.writeMap(policiesToMetadata, (o, policy) -> policy.writeTo(o), (o, tuple) -> { + o.writeVLong(tuple.v1()); + o.writeString(tuple.v2()); + }); } @Override public int hashCode() { - return Objects.hash(policies); + return Objects.hash(policiesToMetadata); } @Override @@ -84,7 +92,7 @@ public boolean equals(Object obj) { return false; } Response other = (Response) obj; - return Objects.equals(policies, other.policies); + return Objects.equals(policiesToMetadata, other.policiesToMetadata); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadataTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadataTests.java index 71118c625498a..809e998bedced 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadataTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadataTests.java @@ -75,7 +75,8 @@ protected LifecyclePolicyMetadata createTestInstance() { for (int i = 0; i < numberHeaders; i++) { headers.put(randomAlphaOfLength(10), randomAlphaOfLength(10)); } - return new LifecyclePolicyMetadata(LifecyclePolicyTests.randomTimeseriesLifecyclePolicy(lifecycleName), headers); + return new LifecyclePolicyMetadata(LifecyclePolicyTests.randomTimeseriesLifecyclePolicy(lifecycleName), headers, + randomNonNegativeLong(), randomNonNegativeLong()); } @Override @@ -87,7 +88,9 @@ protected Reader instanceReader() { protected LifecyclePolicyMetadata mutateInstance(LifecyclePolicyMetadata instance) throws IOException { LifecyclePolicy policy = instance.getPolicy(); Map headers = instance.getHeaders(); - switch (between(0, 1)) { + long version = instance.getVersion(); + long creationDate = instance.getCreationDate(); + switch (between(0, 3)) { case 0: policy = new LifecyclePolicy(TimeseriesLifecycleType.INSTANCE, policy.getName() + randomAlphaOfLengthBetween(1, 5), policy.getPhases()); @@ -96,10 +99,16 @@ protected LifecyclePolicyMetadata mutateInstance(LifecyclePolicyMetadata instanc headers = new HashMap<>(headers); headers.put(randomAlphaOfLength(11), randomAlphaOfLength(11)); break; + case 2: + version++; + break; + case 3: + creationDate++; + break; default: throw new AssertionError("Illegal randomisation branch"); } - return new LifecyclePolicyMetadata(policy, headers); + return new LifecyclePolicyMetadata(policy, headers, version, creationDate); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleResponseTests.java index 63d963d59da21..a06bd4d836194 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleResponseTests.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.xpack.core.indexlifecycle.action; +import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.test.AbstractStreamableTestCase; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleAction; @@ -14,9 +15,9 @@ import org.elasticsearch.xpack.core.indexlifecycle.TestLifecycleType; import org.elasticsearch.xpack.core.indexlifecycle.action.GetLifecycleAction.Response; -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; +import java.util.HashMap; +import java.util.Map; import static org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyTests.randomTestLifecyclePolicy; @@ -25,11 +26,12 @@ public class GetLifecycleResponseTests extends AbstractStreamableTestCase policies = new ArrayList<>(); + Map> policyMap = new HashMap<>(); for (int i = 0; i < randomIntBetween(0, 2); i++) { - policies.add(randomTestLifecyclePolicy(randomPrefix + i)); + policyMap.put(randomTestLifecyclePolicy(randomPrefix + i), + new Tuple<>(randomNonNegativeLong(), randomAlphaOfLength(8))); } - return new Response(policies); + return new Response(policyMap); } @Override @@ -45,16 +47,17 @@ protected NamedWriteableRegistry getNamedWriteableRegistry() { @Override protected Response mutateInstance(Response response) { - List policies = new ArrayList<>(response.getPolicies()); - if (policies.size() > 0) { + Map> policyMap = new HashMap<>(response.getPoliciesToMetadata()); + if (policyMap.size() > 0) { if (randomBoolean()) { - policies.add(randomTestLifecyclePolicy(randomAlphaOfLength(5))); + policyMap.put(randomTestLifecyclePolicy(randomAlphaOfLength(5)), + new Tuple<>(randomNonNegativeLong(), randomAlphaOfLength(4))); } else { - policies.remove(policies.size() - 1); + policyMap.remove(randomFrom(response.getPoliciesToMetadata().keySet())); } } else { - policies.add(randomTestLifecyclePolicy(randomAlphaOfLength(2))); + policyMap.put(randomTestLifecyclePolicy(randomAlphaOfLength(2)), new Tuple<>(randomNonNegativeLong(), randomAlphaOfLength(4))); } - return new Response(policies); + return new Response(policyMap); } } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java index 27cc82d0c0e82..a387b164b04e5 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java @@ -15,19 +15,23 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; +import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyMetadata; import org.elasticsearch.xpack.core.indexlifecycle.action.GetLifecycleAction; import org.elasticsearch.xpack.core.indexlifecycle.action.GetLifecycleAction.Request; import org.elasticsearch.xpack.core.indexlifecycle.action.GetLifecycleAction.Response; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class TransportGetLifecycleAction extends TransportMasterNodeAction { @@ -49,27 +53,32 @@ protected Response newResponse() { } @Override - protected void masterOperation(Request request, ClusterState state, ActionListener listener) throws Exception { + protected void masterOperation(Request request, ClusterState state, ActionListener listener) { IndexLifecycleMetadata metadata = clusterService.state().metaData().custom(IndexLifecycleMetadata.TYPE); if (metadata == null) { listener.onFailure(new ResourceNotFoundException("Lifecycle policy not found: {}", Arrays.toString(request.getPolicyNames()))); } else { - List requestedPolicies; + List requestedPolicies; // if no policies explicitly provided, behave as if `*` was specified if (request.getPolicyNames().length == 0) { - requestedPolicies = new ArrayList<>(metadata.getPolicies().values()); + requestedPolicies = new ArrayList<>(metadata.getPolicyMetadatas().values()); } else { requestedPolicies = new ArrayList<>(request.getPolicyNames().length); for (String name : request.getPolicyNames()) { - LifecyclePolicy policy = metadata.getPolicies().get(name); - if (policy == null) { + LifecyclePolicyMetadata policyMetadata = metadata.getPolicyMetadatas().get(name); + if (policyMetadata == null) { listener.onFailure(new ResourceNotFoundException("Lifecycle policy not found: {}", name)); return; } - requestedPolicies.add(policy); + requestedPolicies.add(policyMetadata); } } - listener.onResponse(new Response(requestedPolicies)); + Map> policyMap = new HashMap<>(requestedPolicies.size()); + for (LifecyclePolicyMetadata policyMetadata : requestedPolicies) { + policyMap.put(policyMetadata.getPolicy(), + new Tuple<>(policyMetadata.getVersion(), policyMetadata.getCreationDateString())); + } + listener.onResponse(new Response(policyMap)); } } @@ -77,4 +86,4 @@ protected void masterOperation(Request request, ClusterState state, ActionListen protected ClusterBlockException checkBlock(Request request, ClusterState state) { return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_WRITE); } -} \ No newline at end of file +} diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportPutLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportPutLifecycleAction.java index 61640fdac21f6..2a56f179f39a5 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportPutLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportPutLifecycleAction.java @@ -28,6 +28,7 @@ import org.elasticsearch.xpack.core.indexlifecycle.action.PutLifecycleAction.Request; import org.elasticsearch.xpack.core.indexlifecycle.action.PutLifecycleAction.Response; +import java.time.Instant; import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; @@ -79,9 +80,12 @@ public ClusterState execute(ClusterState currentState) throws Exception { if (currentMetadata == null) { // first time using index-lifecycle feature, bootstrap metadata currentMetadata = IndexLifecycleMetadata.EMPTY; } - // NORELEASE Check if current step exists in new policy and if not move to next available step + LifecyclePolicyMetadata existingPolicyMetadata = currentMetadata.getPolicyMetadatas() + .get(request.getPolicy().getName()); + long nextVersion = (existingPolicyMetadata == null) ? 1L : existingPolicyMetadata.getVersion() + 1L; SortedMap newPolicies = new TreeMap<>(currentMetadata.getPolicyMetadatas()); - LifecyclePolicyMetadata lifecyclePolicyMetadata = new LifecyclePolicyMetadata(request.getPolicy(), filteredHeaders); + LifecyclePolicyMetadata lifecyclePolicyMetadata = new LifecyclePolicyMetadata(request.getPolicy(), filteredHeaders, + nextVersion, Instant.now().toEpochMilli()); newPolicies.put(lifecyclePolicyMetadata.getName(), lifecyclePolicyMetadata); IndexLifecycleMetadata newMetadata = new IndexLifecycleMetadata(newPolicies, OperationMode.RUNNING); newState.metaData(MetaData.builder(currentState.getMetaData()) diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/ExecuteStepsUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/ExecuteStepsUpdateTaskTests.java index 2e3e39d8f8345..35a907e06bd2c 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/ExecuteStepsUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/ExecuteStepsUpdateTaskTests.java @@ -94,9 +94,12 @@ public void prepareState() throws IOException { LifecyclePolicy invalidPolicy = newTestLifecyclePolicy(invalidPolicyName, Collections.singletonMap(invalidPhase.getName(), invalidPhase)); Map policyMap = new HashMap<>(); - policyMap.put(mixedPolicyName, new LifecyclePolicyMetadata(mixedPolicy, Collections.emptyMap())); - policyMap.put(allClusterPolicyName, new LifecyclePolicyMetadata(allClusterPolicy, Collections.emptyMap())); - policyMap.put(invalidPolicyName, new LifecyclePolicyMetadata(invalidPolicy, Collections.emptyMap())); + policyMap.put(mixedPolicyName, new LifecyclePolicyMetadata(mixedPolicy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())); + policyMap.put(allClusterPolicyName, new LifecyclePolicyMetadata(allClusterPolicy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())); + policyMap.put(invalidPolicyName, new LifecyclePolicyMetadata(invalidPolicy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())); policyStepsRegistry = new PolicyStepsRegistry(NamedXContentRegistry.EMPTY); indexName = randomAlphaOfLength(5); diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java index 77d0bd84ce8f4..d28dd04c5c506 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java @@ -12,6 +12,7 @@ import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.cluster.routing.RoutingNode; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.NamedWriteable; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; @@ -36,10 +37,12 @@ import org.elasticsearch.xpack.core.indexlifecycle.Phase; import org.elasticsearch.xpack.core.indexlifecycle.Step; import org.elasticsearch.xpack.core.indexlifecycle.TerminalPolicyStep; +import org.elasticsearch.xpack.core.indexlifecycle.action.GetLifecycleAction; import org.elasticsearch.xpack.core.indexlifecycle.action.PutLifecycleAction; import org.junit.Before; import java.io.IOException; +import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -57,6 +60,10 @@ import static org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyTestsUtils.newLockableLifecyclePolicy; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.lessThanOrEqualTo; +import static org.hamcrest.core.CombinableMatcher.both; +import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsNull.nullValue; @ESIntegTestCase.ClusterScope(scope = Scope.TEST, numDataNodes = 0) @@ -133,8 +140,23 @@ public void testSingleNodeCluster() throws Exception { final String node1 = getLocalNodeId(server_1); logger.info("Creating lifecycle [test_lifecycle]"); PutLifecycleAction.Request putLifecycleRequest = new PutLifecycleAction.Request(lifecyclePolicy); + long lowerBoundCreationDate = Instant.now().toEpochMilli(); PutLifecycleAction.Response putLifecycleResponse = client().execute(PutLifecycleAction.INSTANCE, putLifecycleRequest).get(); assertAcked(putLifecycleResponse); + long upperBoundCreationDate = Instant.now().toEpochMilli(); + + // assert version and creation_date + GetLifecycleAction.Response getLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE, + new GetLifecycleAction.Request()).get(); + assertThat(getLifecycleResponse.getPoliciesToMetadata().size(), equalTo(1)); + Map.Entry> getPolicyEntry = getLifecycleResponse.getPoliciesToMetadata().entrySet() + .iterator().next(); + assertThat(getPolicyEntry.getKey(), equalTo(lifecyclePolicy)); + assertThat(getPolicyEntry.getValue().v1(), equalTo(1L)); + long actualCreationDate = Instant.parse(getPolicyEntry.getValue().v2()).toEpochMilli(); + assertThat(actualCreationDate, + is(both(greaterThanOrEqualTo(lowerBoundCreationDate)).and(lessThanOrEqualTo(upperBoundCreationDate)))); + logger.info("Creating index [test]"); CreateIndexResponse createIndexResponse = client().admin().indices().create(createIndexRequest("test").settings(settings)) .actionGet(); diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadataTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadataTests.java index 60d4c3a829f11..97f78c89dc9ea 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadataTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadataTests.java @@ -54,7 +54,8 @@ protected IndexLifecycleMetadata createTestInstance() { Map policies = new HashMap<>(numPolicies); for (int i = 0; i < numPolicies; i++) { LifecyclePolicy policy = randomTimeseriesLifecyclePolicy(randomAlphaOfLength(4) + i); - policies.put(policy.getName(), new LifecyclePolicyMetadata(policy, Collections.emptyMap())); + policies.put(policy.getName(), new LifecyclePolicyMetadata(policy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())); } return new IndexLifecycleMetadata(policies, randomFrom(OperationMode.values())); } @@ -108,7 +109,8 @@ protected MetaData.Custom mutateInstance(MetaData.Custom instance) { OperationMode mode = metadata.getOperationMode(); if (randomBoolean()) { String policyName = randomAlphaOfLength(10); - policies.put(policyName, new LifecyclePolicyMetadata(randomTimeseriesLifecyclePolicy(policyName), Collections.emptyMap())); + policies.put(policyName, new LifecyclePolicyMetadata(randomTimeseriesLifecyclePolicy(policyName), Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())); } else { mode = randomValueOtherThan(metadata.getOperationMode(), () -> randomFrom(OperationMode.values())); } @@ -148,7 +150,8 @@ public static IndexLifecycleMetadata createTestInstance(int numPolicies, Operati phases.put(phaseName, new Phase(phaseName, after, actions)); } String policyName = randomAlphaOfLength(10); - policies.put(policyName, new LifecyclePolicyMetadata(newTestLifecyclePolicy(policyName, phases), Collections.emptyMap())); + policies.put(policyName, new LifecyclePolicyMetadata(newTestLifecyclePolicy(policyName, phases), Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())); } return new IndexLifecycleMetadata(policies, mode); } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java index 432a1229170cc..0e6fe1e0dca55 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java @@ -560,7 +560,7 @@ public void testMoveClusterStateToNextStep() { () -> LifecyclePolicyTests.randomTestLifecyclePolicy("policy")); Phase nextPhase = policy.getPhases().values().stream().findFirst().get(); List policyMetadatas = Collections.singletonList( - new LifecyclePolicyMetadata(policy, Collections.emptyMap())); + new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong())); StepKey currentStep = new StepKey("current_phase", "current_action", "current_step"); StepKey nextStep = new StepKey(nextPhase.getName(), "next_action", "next_step"); long now = randomNonNegativeLong(); @@ -645,7 +645,7 @@ public void testSuccessfulValidatedMoveClusterStateToNextStep() { () -> LifecyclePolicyTests.randomTestLifecyclePolicy(policyName)); Phase nextPhase = policy.getPhases().values().stream().findFirst().get(); List policyMetadatas = Collections.singletonList( - new LifecyclePolicyMetadata(policy, Collections.emptyMap())); + new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong())); StepKey currentStepKey = new StepKey("current_phase", "current_action", "current_step"); StepKey nextStepKey = new StepKey(nextPhase.getName(), "next_action", "next_step"); long now = randomNonNegativeLong(); @@ -894,8 +894,10 @@ public void testSetPolicyForIndex() { .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); - policyMetadatas.add(new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap())); + policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())); + policyMetadatas.add(new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); Index index = clusterState.metaData().index(indexName).getIndex(); Index[] indices = new Index[] { index }; @@ -940,7 +942,8 @@ public void testSetPolicyForIndexIndexDoesntExist() { .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); + policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); Index index = new Index("doesnt_exist", "im_not_here"); Index[] indices = new Index[] { index }; @@ -989,7 +992,8 @@ public void testRemovePolicyForIndex() { .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); + policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); Index index = clusterState.metaData().index(indexName).getIndex(); Index[] indices = new Index[] { index }; @@ -1025,7 +1029,8 @@ public void testRemovePolicyForIndexIndexDoesntExist() { .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); + policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); Index index = new Index("doesnt_exist", "im_not_here"); Index[] indices = new Index[] { index }; @@ -1048,7 +1053,8 @@ public void testRemovePolicyForIndexIndexInUnsafe() { .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); + policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); Index index = clusterState.metaData().index(indexName).getIndex(); Index[] indices = new Index[] { index }; diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleServiceTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleServiceTests.java index 0444e14079192..d7296ffc81622 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleServiceTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleServiceTests.java @@ -113,7 +113,8 @@ public void testStoppedModeSkip() { Phase phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", mockAction)); LifecyclePolicy policy = newTestLifecyclePolicy(policyName, Collections.singletonMap(phase.getName(), phase)); SortedMap policyMap = new TreeMap<>(); - policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap())); + policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())); Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); IndexMetaData indexMetadata = IndexMetaData.builder(index.getName()) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName)) @@ -144,7 +145,8 @@ public void testRequestedStopOnShrink() { Phase phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", mockAction)); LifecyclePolicy policy = newTestLifecyclePolicy(policyName, Collections.singletonMap(phase.getName(), phase)); SortedMap policyMap = new TreeMap<>(); - policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap())); + policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())); Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); IndexMetaData indexMetadata = IndexMetaData.builder(index.getName()) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName) @@ -184,7 +186,8 @@ public void testRequestedStopOnSafeAction() { Phase phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", mockAction)); LifecyclePolicy policy = newTestLifecyclePolicy(policyName, Collections.singletonMap(phase.getName(), phase)); SortedMap policyMap = new TreeMap<>(); - policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap())); + policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())); Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); IndexMetaData indexMetadata = IndexMetaData.builder(index.getName()) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName) diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToErrorStepUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToErrorStepUpdateTaskTests.java index bbc31eb9aeeda..40844d0f93d19 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToErrorStepUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToErrorStepUpdateTaskTests.java @@ -50,7 +50,8 @@ public void setupClusterState() { .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); index = indexMetadata.getIndex(); IndexLifecycleMetadata ilmMeta = new IndexLifecycleMetadata( - Collections.singletonMap(policy, new LifecyclePolicyMetadata(lifecyclePolicy, Collections.emptyMap())), + Collections.singletonMap(policy, new LifecyclePolicyMetadata(lifecyclePolicy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())), OperationMode.RUNNING); MetaData metaData = MetaData.builder() .persistentSettings(settings(Version.CURRENT).build()) diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToNextStepUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToNextStepUpdateTaskTests.java index 1c4ea47cb6186..cee1148d1bff5 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToNextStepUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToNextStepUpdateTaskTests.java @@ -47,7 +47,8 @@ public void setupClusterState() { index = indexMetadata.getIndex(); lifecyclePolicy = LifecyclePolicyTests.randomTestLifecyclePolicy(policy); IndexLifecycleMetadata ilmMeta = new IndexLifecycleMetadata( - Collections.singletonMap(policy, new LifecyclePolicyMetadata(lifecyclePolicy, Collections.emptyMap())), + Collections.singletonMap(policy, new LifecyclePolicyMetadata(lifecyclePolicy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())), OperationMode.RUNNING); MetaData metaData = MetaData.builder() .persistentSettings(settings(Version.CURRENT).build()) diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/PolicyStepsRegistryTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/PolicyStepsRegistryTests.java index 92914ed6ecbaa..1ea81001ef147 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/PolicyStepsRegistryTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/PolicyStepsRegistryTests.java @@ -116,7 +116,7 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { headers.put(randomAlphaOfLength(10), randomAlphaOfLength(10)); } Map policyMap = Collections.singletonMap(newPolicy.getName(), - new LifecyclePolicyMetadata(newPolicy, headers)); + new LifecyclePolicyMetadata(newPolicy, headers, randomNonNegativeLong(), randomNonNegativeLong())); IndexLifecycleMetadata lifecycleMetadata = new IndexLifecycleMetadata(policyMap, OperationMode.RUNNING); MetaData metaData = MetaData.builder() .persistentSettings(settings(Version.CURRENT).build()) @@ -203,7 +203,7 @@ public void testUpdateChangedPolicy() throws Exception { headers.put(randomAlphaOfLength(10), randomAlphaOfLength(10)); } Map policyMap = Collections.singletonMap(newPolicy.getName(), - new LifecyclePolicyMetadata(newPolicy, headers)); + new LifecyclePolicyMetadata(newPolicy, headers, randomNonNegativeLong(), randomNonNegativeLong())); IndexLifecycleMetadata lifecycleMetadata = new IndexLifecycleMetadata(policyMap, OperationMode.RUNNING); MetaData metaData = MetaData.builder() .persistentSettings(settings(Version.CURRENT).build()) @@ -224,7 +224,8 @@ public void testUpdateChangedPolicy() throws Exception { // swap out policy newPolicy = LifecyclePolicyTests.randomTestLifecyclePolicy(policyName); lifecycleMetadata = new IndexLifecycleMetadata(Collections.singletonMap(policyName, - new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap())), OperationMode.RUNNING); + new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap(), + randomNonNegativeLong(), randomNonNegativeLong())), OperationMode.RUNNING); currentState = ClusterState.builder(currentState) .metaData(MetaData.builder(metaData).putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata)).build(); registry.update(currentState, client); @@ -256,7 +257,7 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except headers.put(randomAlphaOfLength(10), randomAlphaOfLength(10)); } Map policyMap = Collections.singletonMap(newPolicy.getName(), - new LifecyclePolicyMetadata(newPolicy, headers)); + new LifecyclePolicyMetadata(newPolicy, headers, randomNonNegativeLong(), randomNonNegativeLong())); IndexLifecycleMetadata lifecycleMetadata = new IndexLifecycleMetadata(policyMap, OperationMode.RUNNING); MetaData metaData = MetaData.builder() .persistentSettings(settings(Version.CURRENT).build()) @@ -300,7 +301,8 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except assertThat(((ShrinkStep) gotStep).getNumberOfShards(), equalTo(1)); // Update the policy with the new policy, but keep the phase the same - policyMap = Collections.singletonMap(updatedPolicy.getName(), new LifecyclePolicyMetadata(updatedPolicy, headers)); + policyMap = Collections.singletonMap(updatedPolicy.getName(), new LifecyclePolicyMetadata(updatedPolicy, headers, + randomNonNegativeLong(), randomNonNegativeLong())); lifecycleMetadata = new IndexLifecycleMetadata(policyMap, OperationMode.RUNNING); metaData = MetaData.builder(metaData) .putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata) diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/10_basic.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/10_basic.yml index 85f44549d80c2..c2e042930be87 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/10_basic.yml +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/10_basic.yml @@ -46,8 +46,10 @@ setup: acknowledge: true ilm.get_lifecycle: lifecycle: "my_timeseries_lifecycle" - - match: { my_timeseries_lifecycle.phases.warm.after: "10s" } - - match: { my_timeseries_lifecycle.phases.delete.after: "30s" } + - match: { my_timeseries_lifecycle.version: 1 } + - is_true: my_timeseries_lifecycle.creation_date + - match: { my_timeseries_lifecycle.policy.phases.warm.after: "10s" } + - match: { my_timeseries_lifecycle.policy.phases.delete.after: "30s" } - do: acknowledge: true @@ -91,8 +93,10 @@ setup: acknowledge: true ilm.get_lifecycle: lifecycle: "my_timeseries_lifecycle" - - match: { my_timeseries_lifecycle.phases.warm.after: "10s" } - - match: { my_timeseries_lifecycle.phases.delete.after: "30s" } + - match: { my_timeseries_lifecycle.version: 1 } + - is_true: my_timeseries_lifecycle.creation_date + - match: { my_timeseries_lifecycle.policy.phases.warm.after: "10s" } + - match: { my_timeseries_lifecycle.policy.phases.delete.after: "30s" } - do: @@ -139,8 +143,10 @@ setup: acknowledge: true ilm.get_lifecycle: lifecycle: "my_timeseries_lifecycle" - - match: { my_timeseries_lifecycle.phases.warm.after: "300s" } - - match: { my_timeseries_lifecycle.phases.delete.after: "600s" } + - match: { my_timeseries_lifecycle.version: 2 } + - is_true: my_timeseries_lifecycle.creation_date + - match: { my_timeseries_lifecycle.policy.phases.warm.after: "300s" } + - match: { my_timeseries_lifecycle.policy.phases.delete.after: "600s" } - do: acknowledge: true @@ -193,8 +199,8 @@ setup: acknowledge: true ilm.get_lifecycle: lifecycle: "my_timeseries_lifecycle" - - match: { my_timeseries_lifecycle.phases.warm.after: "10s" } - - match: { my_timeseries_lifecycle.phases.delete.after: "30s" } + - match: { my_timeseries_lifecycle.policy.phases.warm.after: "10s" } + - match: { my_timeseries_lifecycle.policy.phases.delete.after: "30s" } - do: indices.create: From b74a56d914502fb00cc64ab1ace0c6ed15c5de7f Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Thu, 6 Sep 2018 09:59:12 -0700 Subject: [PATCH 2/3] make proper class --- .../action/GetLifecycleAction.java | 92 +++++++++++++++---- .../action/GetLifecycleResponseTests.java | 30 +++--- .../action/TransportGetLifecycleAction.java | 13 +-- .../IndexLifecycleInitialisationIT.java | 12 +-- .../IndexLifecycleRunnerTests.java | 3 +- 5 files changed, 100 insertions(+), 50 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleAction.java index 2cb247342bfd9..e6e0bb5ce403d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleAction.java @@ -11,16 +11,16 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.collect.Tuple; 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.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; import java.io.IOException; import java.util.Arrays; -import java.util.Map; +import java.util.List; import java.util.Objects; public class GetLifecycleAction extends Action { @@ -38,27 +38,27 @@ public Response newResponse() { public static class Response extends ActionResponse implements ToXContentObject { - private Map> policiesToMetadata; + private List policies; public Response() { } - public Response(Map> policiesToMetadata) { - this.policiesToMetadata = policiesToMetadata; + public Response(List policies) { + this.policies = policies; } - public Map> getPoliciesToMetadata() { - return policiesToMetadata; + public List getPolicies() { + return policies; } @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); - for (Map.Entry> entry : policiesToMetadata.entrySet()) { - builder.startObject(entry.getKey().getName()); - builder.field("version", entry.getValue().v1()); - builder.field("creation_date", entry.getValue().v2()); - builder.field("policy", entry.getKey()); + for (LifecyclePolicyResponseItem item : policies) { + builder.startObject(item.getLifecyclePolicy().getName()); + builder.field("version", item.getVersion()); + builder.field("creation_date", item.getCreationDate()); + builder.field("policy", item.getLifecyclePolicy()); builder.endObject(); } builder.endObject(); @@ -67,20 +67,17 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public void readFrom(StreamInput in) throws IOException { - policiesToMetadata = in.readMap(LifecyclePolicy::new, (inn) -> new Tuple<>(inn.readVLong(), inn.readString())); + this.policies = in.readList(LifecyclePolicyResponseItem::new); } @Override public void writeTo(StreamOutput out) throws IOException { - out.writeMap(policiesToMetadata, (o, policy) -> policy.writeTo(o), (o, tuple) -> { - o.writeVLong(tuple.v1()); - o.writeString(tuple.v2()); - }); + out.writeList(policies); } @Override public int hashCode() { - return Objects.hash(policiesToMetadata); + return Objects.hash(policies); } @Override @@ -92,7 +89,7 @@ public boolean equals(Object obj) { return false; } Response other = (Response) obj; - return Objects.equals(policiesToMetadata, other.policiesToMetadata); + return Objects.equals(policies, other.policies); } @Override @@ -156,4 +153,61 @@ public boolean equals(Object obj) { } + public static class LifecyclePolicyResponseItem implements Writeable { + private final LifecyclePolicy lifecyclePolicy; + private final long version; + private final String creationDate; + + public LifecyclePolicyResponseItem(LifecyclePolicy lifecyclePolicy, long version, String creationDate) { + this.lifecyclePolicy = lifecyclePolicy; + this.version = version; + this.creationDate = creationDate; + } + + LifecyclePolicyResponseItem(StreamInput in) throws IOException { + this.lifecyclePolicy = new LifecyclePolicy(in); + this.version = in.readVLong(); + this.creationDate = in.readString(); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + lifecyclePolicy.writeTo(out); + out.writeVLong(version); + out.writeString(creationDate); + } + + public LifecyclePolicy getLifecyclePolicy() { + return lifecyclePolicy; + } + + public long getVersion() { + return version; + } + + public String getCreationDate() { + return creationDate; + } + + @Override + public int hashCode() { + return Objects.hash(lifecyclePolicy, version, creationDate); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (obj.getClass() != getClass()) { + return false; + } + LifecyclePolicyResponseItem other = (LifecyclePolicyResponseItem) obj; + return Objects.equals(lifecyclePolicy, other.lifecyclePolicy) && + Objects.equals(version, other.version) && + Objects.equals(creationDate, other.creationDate); + } + + } + } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleResponseTests.java index a06bd4d836194..08688407b3db6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleResponseTests.java @@ -5,19 +5,18 @@ */ package org.elasticsearch.xpack.core.indexlifecycle.action; -import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.test.AbstractStreamableTestCase; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleAction; -import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleType; import org.elasticsearch.xpack.core.indexlifecycle.MockAction; import org.elasticsearch.xpack.core.indexlifecycle.TestLifecycleType; +import org.elasticsearch.xpack.core.indexlifecycle.action.GetLifecycleAction.LifecyclePolicyResponseItem; import org.elasticsearch.xpack.core.indexlifecycle.action.GetLifecycleAction.Response; +import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; +import java.util.List; import static org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyTests.randomTestLifecyclePolicy; @@ -26,12 +25,12 @@ public class GetLifecycleResponseTests extends AbstractStreamableTestCase> policyMap = new HashMap<>(); + List responseItems = new ArrayList<>(); for (int i = 0; i < randomIntBetween(0, 2); i++) { - policyMap.put(randomTestLifecyclePolicy(randomPrefix + i), - new Tuple<>(randomNonNegativeLong(), randomAlphaOfLength(8))); + responseItems.add(new LifecyclePolicyResponseItem(randomTestLifecyclePolicy(randomPrefix + i), + randomNonNegativeLong(), randomAlphaOfLength(8))); } - return new Response(policyMap); + return new Response(responseItems); } @Override @@ -47,17 +46,18 @@ protected NamedWriteableRegistry getNamedWriteableRegistry() { @Override protected Response mutateInstance(Response response) { - Map> policyMap = new HashMap<>(response.getPoliciesToMetadata()); - if (policyMap.size() > 0) { + List responseItems = new ArrayList<>(response.getPolicies()); + if (responseItems.size() > 0) { if (randomBoolean()) { - policyMap.put(randomTestLifecyclePolicy(randomAlphaOfLength(5)), - new Tuple<>(randomNonNegativeLong(), randomAlphaOfLength(4))); + responseItems.add(new LifecyclePolicyResponseItem(randomTestLifecyclePolicy(randomAlphaOfLength(5)), + randomNonNegativeLong(), randomAlphaOfLength(4))); } else { - policyMap.remove(randomFrom(response.getPoliciesToMetadata().keySet())); + responseItems.remove(0); } } else { - policyMap.put(randomTestLifecyclePolicy(randomAlphaOfLength(2)), new Tuple<>(randomNonNegativeLong(), randomAlphaOfLength(4))); + responseItems.add(new LifecyclePolicyResponseItem(randomTestLifecyclePolicy(randomAlphaOfLength(2)), + randomNonNegativeLong(), randomAlphaOfLength(4))); } - return new Response(policyMap); + return new Response(responseItems); } } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java index a387b164b04e5..daf2f7ec4c1d9 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java @@ -15,23 +15,20 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata; -import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyMetadata; import org.elasticsearch.xpack.core.indexlifecycle.action.GetLifecycleAction; +import org.elasticsearch.xpack.core.indexlifecycle.action.GetLifecycleAction.LifecyclePolicyResponseItem; import org.elasticsearch.xpack.core.indexlifecycle.action.GetLifecycleAction.Request; import org.elasticsearch.xpack.core.indexlifecycle.action.GetLifecycleAction.Response; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; public class TransportGetLifecycleAction extends TransportMasterNodeAction { @@ -73,12 +70,12 @@ protected void masterOperation(Request request, ClusterState state, ActionListen requestedPolicies.add(policyMetadata); } } - Map> policyMap = new HashMap<>(requestedPolicies.size()); + List responseItems = new ArrayList<>(requestedPolicies.size()); for (LifecyclePolicyMetadata policyMetadata : requestedPolicies) { - policyMap.put(policyMetadata.getPolicy(), - new Tuple<>(policyMetadata.getVersion(), policyMetadata.getCreationDateString())); + responseItems.add(new LifecyclePolicyResponseItem(policyMetadata.getPolicy(), + policyMetadata.getVersion(), policyMetadata.getCreationDateString())); } - listener.onResponse(new Response(policyMap)); + listener.onResponse(new Response(responseItems)); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java index d28dd04c5c506..b81a036404c43 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java @@ -12,7 +12,6 @@ import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.cluster.routing.RoutingNode; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.NamedWriteable; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; @@ -148,12 +147,11 @@ public void testSingleNodeCluster() throws Exception { // assert version and creation_date GetLifecycleAction.Response getLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE, new GetLifecycleAction.Request()).get(); - assertThat(getLifecycleResponse.getPoliciesToMetadata().size(), equalTo(1)); - Map.Entry> getPolicyEntry = getLifecycleResponse.getPoliciesToMetadata().entrySet() - .iterator().next(); - assertThat(getPolicyEntry.getKey(), equalTo(lifecyclePolicy)); - assertThat(getPolicyEntry.getValue().v1(), equalTo(1L)); - long actualCreationDate = Instant.parse(getPolicyEntry.getValue().v2()).toEpochMilli(); + assertThat(getLifecycleResponse.getPolicies().size(), equalTo(1)); + GetLifecycleAction.LifecyclePolicyResponseItem responseItem = getLifecycleResponse.getPolicies().get(0); + assertThat(responseItem.getLifecyclePolicy(), equalTo(lifecyclePolicy)); + assertThat(responseItem.getVersion(), equalTo(1L)); + long actualCreationDate = Instant.parse(responseItem.getCreationDate()).toEpochMilli(); assertThat(actualCreationDate, is(both(greaterThanOrEqualTo(lowerBoundCreationDate)).and(lessThanOrEqualTo(upperBoundCreationDate)))); diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java index 0e6fe1e0dca55..ff846b2553c36 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java @@ -1072,7 +1072,8 @@ public void testIsReadyToTransition() { MockAsyncActionStep step = new MockAsyncActionStep(stepKey, null); step.setWillComplete(true); SortedMap lifecyclePolicyMap = new TreeMap<>(Collections.singletonMap(policyName, - new LifecyclePolicyMetadata(createPolicy(policyName, null, step.getKey()), new HashMap<>()))); + new LifecyclePolicyMetadata(createPolicy(policyName, null, step.getKey()), new HashMap<>(), + randomNonNegativeLong(), randomNonNegativeLong()))); Index index = new Index("my_index", "uuid"); Map firstStepMap = Collections.singletonMap(policyName, step); Map policySteps = Collections.singletonMap(step.getKey(), step); From 6af208317bead0b666f00bab1e78fa26e2b8c3ec Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Thu, 6 Sep 2018 11:16:11 -0700 Subject: [PATCH 3/3] rename to modifiedDate and cleanup lists --- .../LifecyclePolicyMetadata.java | 36 +++++++++---------- .../action/GetLifecycleAction.java | 20 +++++------ .../LifecyclePolicyMetadataTests.java | 2 +- .../action/TransportGetLifecycleAction.java | 18 +++++----- .../IndexLifecycleInitialisationIT.java | 12 +++---- .../rest-api-spec/test/ilm/10_basic.yml | 6 ++-- 6 files changed, 47 insertions(+), 47 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadata.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadata.java index 18733aac3840e..bfb2bee1edaac 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadata.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadata.java @@ -30,8 +30,8 @@ public class LifecyclePolicyMetadata extends AbstractDiffable PARSER = new ConstructingObjectParser<>("policy_metadata", @@ -43,8 +43,8 @@ public class LifecyclePolicyMetadata extends AbstractDiffable headers; private final long version; - private final long creationDate; + private final long modifiedDate; - public LifecyclePolicyMetadata(LifecyclePolicy policy, Map headers, long version, long creationDate) { + public LifecyclePolicyMetadata(LifecyclePolicy policy, Map headers, long version, long modifiedDate) { this.policy = policy; this.headers = headers; this.version = version; - this.creationDate = creationDate; + this.modifiedDate = modifiedDate; } @SuppressWarnings("unchecked") @@ -68,7 +68,7 @@ public LifecyclePolicyMetadata(StreamInput in) throws IOException { this.policy = new LifecyclePolicy(in); this.headers = (Map) in.readGenericValue(); this.version = in.readVLong(); - this.creationDate = in.readVLong(); + this.modifiedDate = in.readVLong(); } public Map getHeaders() { @@ -87,13 +87,13 @@ public long getVersion() { return version; } - public long getCreationDate() { - return creationDate; + public long getModifiedDate() { + return modifiedDate; } - public String getCreationDateString() { - ZonedDateTime creationDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(creationDate), ZoneOffset.UTC); - return creationDateTime.toString(); + public String getModifiedDateString() { + ZonedDateTime modifiedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(modifiedDate), ZoneOffset.UTC); + return modifiedDateTime.toString(); } @Override @@ -102,8 +102,8 @@ 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(CREATION_DATE.getPreferredName(), creationDate); - builder.field(CREATION_DATE_STRING.getPreferredName(), getCreationDateString()); + builder.field(MODIFIED_DATE.getPreferredName(), modifiedDate); + builder.field(MODIFIED_DATE_STRING.getPreferredName(), getModifiedDateString()); builder.endObject(); return builder; } @@ -113,12 +113,12 @@ public void writeTo(StreamOutput out) throws IOException { policy.writeTo(out); out.writeGenericValue(headers); out.writeVLong(version); - out.writeVLong(creationDate); + out.writeVLong(modifiedDate); } @Override public int hashCode() { - return Objects.hash(policy, headers, version, creationDate); + return Objects.hash(policy, headers, version, modifiedDate); } @Override @@ -133,7 +133,7 @@ public boolean equals(Object obj) { return Objects.equals(policy, other.policy) && Objects.equals(headers, other.headers) && Objects.equals(version, other.version) && - Objects.equals(creationDate, other.creationDate); + Objects.equals(modifiedDate, other.modifiedDate); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleAction.java index e6e0bb5ce403d..aaa295354a850 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/action/GetLifecycleAction.java @@ -57,7 +57,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws for (LifecyclePolicyResponseItem item : policies) { builder.startObject(item.getLifecyclePolicy().getName()); builder.field("version", item.getVersion()); - builder.field("creation_date", item.getCreationDate()); + builder.field("modified_date", item.getModifiedDate()); builder.field("policy", item.getLifecyclePolicy()); builder.endObject(); } @@ -156,25 +156,25 @@ public boolean equals(Object obj) { public static class LifecyclePolicyResponseItem implements Writeable { private final LifecyclePolicy lifecyclePolicy; private final long version; - private final String creationDate; + private final String modifiedDate; - public LifecyclePolicyResponseItem(LifecyclePolicy lifecyclePolicy, long version, String creationDate) { + public LifecyclePolicyResponseItem(LifecyclePolicy lifecyclePolicy, long version, String modifiedDate) { this.lifecyclePolicy = lifecyclePolicy; this.version = version; - this.creationDate = creationDate; + this.modifiedDate = modifiedDate; } LifecyclePolicyResponseItem(StreamInput in) throws IOException { this.lifecyclePolicy = new LifecyclePolicy(in); this.version = in.readVLong(); - this.creationDate = in.readString(); + this.modifiedDate = in.readString(); } @Override public void writeTo(StreamOutput out) throws IOException { lifecyclePolicy.writeTo(out); out.writeVLong(version); - out.writeString(creationDate); + out.writeString(modifiedDate); } public LifecyclePolicy getLifecyclePolicy() { @@ -185,13 +185,13 @@ public long getVersion() { return version; } - public String getCreationDate() { - return creationDate; + public String getModifiedDate() { + return modifiedDate; } @Override public int hashCode() { - return Objects.hash(lifecyclePolicy, version, creationDate); + return Objects.hash(lifecyclePolicy, version, modifiedDate); } @Override @@ -205,7 +205,7 @@ public boolean equals(Object obj) { LifecyclePolicyResponseItem other = (LifecyclePolicyResponseItem) obj; return Objects.equals(lifecyclePolicy, other.lifecyclePolicy) && Objects.equals(version, other.version) && - Objects.equals(creationDate, other.creationDate); + Objects.equals(modifiedDate, other.modifiedDate); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadataTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadataTests.java index 809e998bedced..5cb75e132ce92 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadataTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyMetadataTests.java @@ -89,7 +89,7 @@ protected LifecyclePolicyMetadata mutateInstance(LifecyclePolicyMetadata instanc LifecyclePolicy policy = instance.getPolicy(); Map headers = instance.getHeaders(); long version = instance.getVersion(); - long creationDate = instance.getCreationDate(); + long creationDate = instance.getModifiedDate(); switch (between(0, 3)) { case 0: policy = new LifecyclePolicy(TimeseriesLifecycleType.INSTANCE, policy.getName() + randomAlphaOfLengthBetween(1, 5), diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java index daf2f7ec4c1d9..638bc82400047 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java @@ -55,10 +55,14 @@ protected void masterOperation(Request request, ClusterState state, ActionListen if (metadata == null) { listener.onFailure(new ResourceNotFoundException("Lifecycle policy not found: {}", Arrays.toString(request.getPolicyNames()))); } else { - List requestedPolicies; + List requestedPolicies; // if no policies explicitly provided, behave as if `*` was specified if (request.getPolicyNames().length == 0) { - requestedPolicies = new ArrayList<>(metadata.getPolicyMetadatas().values()); + requestedPolicies = new ArrayList<>(metadata.getPolicyMetadatas().size()); + for (LifecyclePolicyMetadata policyMetadata : metadata.getPolicyMetadatas().values()) { + requestedPolicies.add(new LifecyclePolicyResponseItem(policyMetadata.getPolicy(), + policyMetadata.getVersion(), policyMetadata.getModifiedDateString())); + } } else { requestedPolicies = new ArrayList<>(request.getPolicyNames().length); for (String name : request.getPolicyNames()) { @@ -67,15 +71,11 @@ protected void masterOperation(Request request, ClusterState state, ActionListen listener.onFailure(new ResourceNotFoundException("Lifecycle policy not found: {}", name)); return; } - requestedPolicies.add(policyMetadata); + requestedPolicies.add(new LifecyclePolicyResponseItem(policyMetadata.getPolicy(), + policyMetadata.getVersion(), policyMetadata.getModifiedDateString())); } } - List responseItems = new ArrayList<>(requestedPolicies.size()); - for (LifecyclePolicyMetadata policyMetadata : requestedPolicies) { - responseItems.add(new LifecyclePolicyResponseItem(policyMetadata.getPolicy(), - policyMetadata.getVersion(), policyMetadata.getCreationDateString())); - } - listener.onResponse(new Response(responseItems)); + listener.onResponse(new Response(requestedPolicies)); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java index b81a036404c43..376e3d2175042 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java @@ -139,21 +139,21 @@ public void testSingleNodeCluster() throws Exception { final String node1 = getLocalNodeId(server_1); logger.info("Creating lifecycle [test_lifecycle]"); PutLifecycleAction.Request putLifecycleRequest = new PutLifecycleAction.Request(lifecyclePolicy); - long lowerBoundCreationDate = Instant.now().toEpochMilli(); + long lowerBoundModifiedDate = Instant.now().toEpochMilli(); PutLifecycleAction.Response putLifecycleResponse = client().execute(PutLifecycleAction.INSTANCE, putLifecycleRequest).get(); assertAcked(putLifecycleResponse); - long upperBoundCreationDate = Instant.now().toEpochMilli(); + long upperBoundModifiedDate = Instant.now().toEpochMilli(); - // assert version and creation_date + // assert version and modified_date GetLifecycleAction.Response getLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE, new GetLifecycleAction.Request()).get(); assertThat(getLifecycleResponse.getPolicies().size(), equalTo(1)); GetLifecycleAction.LifecyclePolicyResponseItem responseItem = getLifecycleResponse.getPolicies().get(0); assertThat(responseItem.getLifecyclePolicy(), equalTo(lifecyclePolicy)); assertThat(responseItem.getVersion(), equalTo(1L)); - long actualCreationDate = Instant.parse(responseItem.getCreationDate()).toEpochMilli(); - assertThat(actualCreationDate, - is(both(greaterThanOrEqualTo(lowerBoundCreationDate)).and(lessThanOrEqualTo(upperBoundCreationDate)))); + long actualModifiedDate = Instant.parse(responseItem.getModifiedDate()).toEpochMilli(); + assertThat(actualModifiedDate, + is(both(greaterThanOrEqualTo(lowerBoundModifiedDate)).and(lessThanOrEqualTo(upperBoundModifiedDate)))); logger.info("Creating index [test]"); CreateIndexResponse createIndexResponse = client().admin().indices().create(createIndexRequest("test").settings(settings)) diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/10_basic.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/10_basic.yml index c2e042930be87..eb004d2afa0ed 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/10_basic.yml +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/10_basic.yml @@ -47,7 +47,7 @@ setup: ilm.get_lifecycle: lifecycle: "my_timeseries_lifecycle" - match: { my_timeseries_lifecycle.version: 1 } - - is_true: my_timeseries_lifecycle.creation_date + - is_true: my_timeseries_lifecycle.modified_date - match: { my_timeseries_lifecycle.policy.phases.warm.after: "10s" } - match: { my_timeseries_lifecycle.policy.phases.delete.after: "30s" } @@ -94,7 +94,7 @@ setup: ilm.get_lifecycle: lifecycle: "my_timeseries_lifecycle" - match: { my_timeseries_lifecycle.version: 1 } - - is_true: my_timeseries_lifecycle.creation_date + - is_true: my_timeseries_lifecycle.modified_date - match: { my_timeseries_lifecycle.policy.phases.warm.after: "10s" } - match: { my_timeseries_lifecycle.policy.phases.delete.after: "30s" } @@ -144,7 +144,7 @@ setup: ilm.get_lifecycle: lifecycle: "my_timeseries_lifecycle" - match: { my_timeseries_lifecycle.version: 2 } - - is_true: my_timeseries_lifecycle.creation_date + - is_true: my_timeseries_lifecycle.modified_date - match: { my_timeseries_lifecycle.policy.phases.warm.after: "300s" } - match: { my_timeseries_lifecycle.policy.phases.delete.after: "600s" }