|
@@ -39,14 +39,21 @@ import org.elasticsearch.test.client.NoOpClient;
|
|
|
import org.elasticsearch.threadpool.TestThreadPool;
|
|
|
import org.elasticsearch.threadpool.ThreadPool;
|
|
|
import org.elasticsearch.xcontent.NamedXContentRegistry;
|
|
|
+import org.elasticsearch.xcontent.ParseField;
|
|
|
+import org.elasticsearch.xcontent.XContentParser;
|
|
|
+import org.elasticsearch.xcontent.XContentParserConfiguration;
|
|
|
import org.elasticsearch.xcontent.XContentType;
|
|
|
+import org.elasticsearch.xpack.core.ilm.DeleteAction;
|
|
|
import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata;
|
|
|
+import org.elasticsearch.xpack.core.ilm.LifecycleAction;
|
|
|
import org.elasticsearch.xpack.core.ilm.LifecyclePolicy;
|
|
|
import org.elasticsearch.xpack.core.ilm.LifecyclePolicyMetadata;
|
|
|
import org.elasticsearch.xpack.core.ilm.OperationMode;
|
|
|
+import org.elasticsearch.xpack.core.ilm.action.PutLifecycleAction;
|
|
|
import org.junit.After;
|
|
|
import org.junit.Before;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
@@ -92,6 +99,9 @@ public class IndexTemplateRegistryTests extends ESTestCase {
|
|
|
if (action instanceof PutPipelineAction) {
|
|
|
assertPutPipelineAction(calledTimes, action, request, listener, "custom-plugin-final_pipeline");
|
|
|
return AcknowledgedResponse.TRUE;
|
|
|
+ } else if (action instanceof PutLifecycleAction) {
|
|
|
+ // ignore lifecycle policies in this case
|
|
|
+ return AcknowledgedResponse.TRUE;
|
|
|
} else {
|
|
|
// the composable template is not expected to be added, as it's dependency is not available in the cluster state
|
|
|
// custom-plugin-settings.json is not expected to be added as it contains a dependency on the default_pipeline
|
|
@@ -114,6 +124,9 @@ public class IndexTemplateRegistryTests extends ESTestCase {
|
|
|
if (action instanceof PutPipelineAction) {
|
|
|
assertPutPipelineAction(calledTimes, action, request, listener, "custom-plugin-default_pipeline");
|
|
|
return AcknowledgedResponse.TRUE;
|
|
|
+ } else if (action instanceof PutLifecycleAction) {
|
|
|
+ // ignore lifecycle policies in this case
|
|
|
+ return AcknowledgedResponse.TRUE;
|
|
|
} else {
|
|
|
// the composable template is not expected to be added, as it's dependency is not available in the cluster state
|
|
|
// custom-plugin-settings.json is not expected to be added as it contains a dependency on the default_pipeline
|
|
@@ -141,6 +154,9 @@ public class IndexTemplateRegistryTests extends ESTestCase {
|
|
|
if (action instanceof PutComponentTemplateAction) {
|
|
|
assertPutComponentTemplate(calledTimes, action, request, listener);
|
|
|
return AcknowledgedResponse.TRUE;
|
|
|
+ } else if (action instanceof PutLifecycleAction) {
|
|
|
+ // ignore lifecycle policies in this case
|
|
|
+ return AcknowledgedResponse.TRUE;
|
|
|
} else {
|
|
|
// the composable template is not expected to be added, as it's dependency is not available in the cluster state
|
|
|
fail("client called with unexpected request: " + request.toString());
|
|
@@ -167,6 +183,9 @@ public class IndexTemplateRegistryTests extends ESTestCase {
|
|
|
if (action instanceof PutPipelineAction) {
|
|
|
assertPutPipelineAction(calledTimes, action, request, listener, "custom-plugin-default_pipeline");
|
|
|
return AcknowledgedResponse.TRUE;
|
|
|
+ } else if (action instanceof PutLifecycleAction) {
|
|
|
+ // ignore lifecycle policies in this case
|
|
|
+ return AcknowledgedResponse.TRUE;
|
|
|
} else {
|
|
|
// the template is not expected to be added, as the final pipeline is missing
|
|
|
fail("client called with unexpected request: " + request.toString());
|
|
@@ -193,6 +212,9 @@ public class IndexTemplateRegistryTests extends ESTestCase {
|
|
|
if (action instanceof PutComposableIndexTemplateAction) {
|
|
|
assertPutComposableIndexTemplateAction(calledTimes, action, request, listener);
|
|
|
return AcknowledgedResponse.TRUE;
|
|
|
+ } else if (action instanceof PutLifecycleAction) {
|
|
|
+ // ignore lifecycle policies in this case
|
|
|
+ return AcknowledgedResponse.TRUE;
|
|
|
} else if (action instanceof PutPipelineAction) {
|
|
|
// ignore pipelines in this case
|
|
|
return AcknowledgedResponse.TRUE;
|
|
@@ -224,6 +246,9 @@ public class IndexTemplateRegistryTests extends ESTestCase {
|
|
|
"custom-plugin-final_pipeline"
|
|
|
);
|
|
|
return AcknowledgedResponse.TRUE;
|
|
|
+ } else if (action instanceof PutLifecycleAction) {
|
|
|
+ // ignore lifecycle policies in this case
|
|
|
+ return AcknowledgedResponse.TRUE;
|
|
|
} else if (action instanceof PutComponentTemplateAction) {
|
|
|
assertPutComponentTemplate(calledTimes, action, request, listener);
|
|
|
return AcknowledgedResponse.TRUE;
|
|
@@ -255,6 +280,9 @@ public class IndexTemplateRegistryTests extends ESTestCase {
|
|
|
if (action instanceof PutComposableIndexTemplateAction) {
|
|
|
// ignore this
|
|
|
return AcknowledgedResponse.TRUE;
|
|
|
+ } else if (action instanceof PutLifecycleAction) {
|
|
|
+ // ignore lifecycle policies in this case
|
|
|
+ return AcknowledgedResponse.TRUE;
|
|
|
} else {
|
|
|
fail("client called with unexpected request: " + request.toString());
|
|
|
return null;
|
|
@@ -271,6 +299,173 @@ public class IndexTemplateRegistryTests extends ESTestCase {
|
|
|
assertBusy(() -> assertThat(calledTimes.get(), equalTo(0)));
|
|
|
}
|
|
|
|
|
|
+ public void testThatNonExistingPoliciesAreAddedImmediately() throws Exception {
|
|
|
+ DiscoveryNode node = DiscoveryNodeUtils.create("node");
|
|
|
+ DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build();
|
|
|
+
|
|
|
+ AtomicInteger calledTimes = new AtomicInteger(0);
|
|
|
+ client.setVerifier((action, request, listener) -> {
|
|
|
+ if (action instanceof PutComposableIndexTemplateAction) {
|
|
|
+ // ignore this
|
|
|
+ return AcknowledgedResponse.TRUE;
|
|
|
+ } else if (action instanceof PutLifecycleAction) {
|
|
|
+ assertPutLifecycleAction(calledTimes, action, request, listener);
|
|
|
+ return AcknowledgedResponse.TRUE;
|
|
|
+ } else {
|
|
|
+ fail("client called with unexpected request: " + request.toString());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ ClusterChangedEvent event = createClusterChangedEvent(
|
|
|
+ Map.of("custom-plugin-settings", 3),
|
|
|
+ Map.of(),
|
|
|
+ Map.of("custom-plugin-default_pipeline", 3, "custom-plugin-final_pipeline", 3),
|
|
|
+ nodes
|
|
|
+ );
|
|
|
+ registry.clusterChanged(event);
|
|
|
+ assertBusy(() -> assertThat(calledTimes.get(), equalTo(registry.getPolicyConfigs().size())));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testPolicyAlreadyExists() {
|
|
|
+ DiscoveryNode node = DiscoveryNodeUtils.create("node");
|
|
|
+ DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build();
|
|
|
+
|
|
|
+ Map<String, LifecyclePolicy> policyMap = new HashMap<>();
|
|
|
+ List<LifecyclePolicy> policies = registry.getPolicyConfigs();
|
|
|
+ assertThat(policies, hasSize(1));
|
|
|
+ policies.forEach(p -> policyMap.put(p.getName(), p));
|
|
|
+
|
|
|
+ client.setVerifier((action, request, listener) -> {
|
|
|
+ if (action instanceof PutComposableIndexTemplateAction) {
|
|
|
+ // ignore this
|
|
|
+ return AcknowledgedResponse.TRUE;
|
|
|
+ } else if (action instanceof PutLifecycleAction) {
|
|
|
+ fail("if the policy already exists it should not be re-put");
|
|
|
+ } else {
|
|
|
+ fail("client called with unexpected request: " + request.toString());
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+
|
|
|
+ ClusterChangedEvent event = createClusterChangedEvent(
|
|
|
+ Map.of("custom-plugin-settings", 3),
|
|
|
+ policyMap,
|
|
|
+ Map.of("custom-plugin-default_pipeline", 3, "custom-plugin-final_pipeline", 3),
|
|
|
+ nodes
|
|
|
+ );
|
|
|
+
|
|
|
+ registry.clusterChanged(event);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testPolicyAlreadyExistsButDiffers() throws IOException {
|
|
|
+ DiscoveryNode node = DiscoveryNodeUtils.create("node");
|
|
|
+ DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build();
|
|
|
+
|
|
|
+ Map<String, LifecyclePolicy> policyMap = new HashMap<>();
|
|
|
+ String policyStr = "{\"phases\":{\"delete\":{\"min_age\":\"1m\",\"actions\":{\"delete\":{}}}}}";
|
|
|
+ List<LifecyclePolicy> policies = registry.getPolicyConfigs();
|
|
|
+ assertThat(policies, hasSize(1));
|
|
|
+ policies.forEach(p -> policyMap.put(p.getName(), p));
|
|
|
+
|
|
|
+ client.setVerifier((action, request, listener) -> {
|
|
|
+ if (action instanceof PutComposableIndexTemplateAction) {
|
|
|
+ // ignore this
|
|
|
+ return AcknowledgedResponse.TRUE;
|
|
|
+ } else if (action instanceof PutLifecycleAction) {
|
|
|
+ fail("if the policy already exists it should not be re-put");
|
|
|
+ } else {
|
|
|
+ fail("client called with unexpected request: " + request.toString());
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+
|
|
|
+ try (
|
|
|
+ XContentParser parser = XContentType.JSON.xContent()
|
|
|
+ .createParser(
|
|
|
+ XContentParserConfiguration.EMPTY.withRegistry(
|
|
|
+ new NamedXContentRegistry(
|
|
|
+ List.of(
|
|
|
+ new NamedXContentRegistry.Entry(
|
|
|
+ LifecycleAction.class,
|
|
|
+ new ParseField(DeleteAction.NAME),
|
|
|
+ DeleteAction::parse
|
|
|
+ )
|
|
|
+ )
|
|
|
+ )
|
|
|
+ ),
|
|
|
+ policyStr
|
|
|
+ )
|
|
|
+ ) {
|
|
|
+ LifecyclePolicy different = LifecyclePolicy.parse(parser, policies.get(0).getName());
|
|
|
+ policyMap.put(policies.get(0).getName(), different);
|
|
|
+ ClusterChangedEvent event = createClusterChangedEvent(
|
|
|
+ Map.of("custom-plugin-settings", 3),
|
|
|
+ policyMap,
|
|
|
+ Map.of("custom-plugin-default_pipeline", 3, "custom-plugin-final_pipeline", 3),
|
|
|
+ nodes
|
|
|
+ );
|
|
|
+ registry.clusterChanged(event);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testPolicyUpgraded() throws Exception {
|
|
|
+ registry.setPolicyUpgradeRequired(true);
|
|
|
+ DiscoveryNode node = DiscoveryNodeUtils.create("node");
|
|
|
+ DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build();
|
|
|
+
|
|
|
+ Map<String, LifecyclePolicy> policyMap = new HashMap<>();
|
|
|
+ String priorPolicyStr = "{\"phases\":{\"delete\":{\"min_age\":\"1m\",\"actions\":{\"delete\":{}}}}}";
|
|
|
+ List<LifecyclePolicy> policies = registry.getPolicyConfigs();
|
|
|
+ assertThat(policies, hasSize(1));
|
|
|
+ policies.forEach(p -> policyMap.put(p.getName(), p));
|
|
|
+
|
|
|
+ AtomicInteger calledTimes = new AtomicInteger(0);
|
|
|
+ client.setVerifier((action, request, listener) -> {
|
|
|
+ if (action instanceof PutComposableIndexTemplateAction) {
|
|
|
+ // ignore this
|
|
|
+ return AcknowledgedResponse.TRUE;
|
|
|
+ } else if (action instanceof PutLifecycleAction) {
|
|
|
+ assertPutLifecycleAction(calledTimes, action, request, listener);
|
|
|
+ return AcknowledgedResponse.TRUE;
|
|
|
+
|
|
|
+ } else {
|
|
|
+ fail("client called with unexpected request: " + request.toString());
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+
|
|
|
+ try (
|
|
|
+ XContentParser parser = XContentType.JSON.xContent()
|
|
|
+ .createParser(
|
|
|
+ XContentParserConfiguration.EMPTY.withRegistry(
|
|
|
+ new NamedXContentRegistry(
|
|
|
+ List.of(
|
|
|
+ new NamedXContentRegistry.Entry(
|
|
|
+ LifecycleAction.class,
|
|
|
+ new ParseField(DeleteAction.NAME),
|
|
|
+ DeleteAction::parse
|
|
|
+ )
|
|
|
+ )
|
|
|
+ )
|
|
|
+ ),
|
|
|
+ priorPolicyStr
|
|
|
+ )
|
|
|
+ ) {
|
|
|
+ LifecyclePolicy priorPolicy = LifecyclePolicy.parse(parser, policies.get(0).getName());
|
|
|
+ policyMap.put(policies.get(0).getName(), priorPolicy);
|
|
|
+ ClusterChangedEvent event = createClusterChangedEvent(
|
|
|
+ Map.of("custom-plugin-settings", 3),
|
|
|
+ policyMap,
|
|
|
+ Map.of("custom-plugin-default_pipeline", 3, "custom-plugin-final_pipeline", 3),
|
|
|
+ nodes
|
|
|
+ );
|
|
|
+ registry.clusterChanged(event);
|
|
|
+ // we've changed one policy that should be upgraded
|
|
|
+ assertBusy(() -> assertThat(calledTimes.get(), equalTo(1)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private static void assertPutComponentTemplate(
|
|
|
AtomicInteger calledTimes,
|
|
|
ActionType<?> action,
|
|
@@ -332,6 +527,20 @@ public class IndexTemplateRegistryTests extends ESTestCase {
|
|
|
calledTimes.incrementAndGet();
|
|
|
}
|
|
|
|
|
|
+ private static void assertPutLifecycleAction(
|
|
|
+ AtomicInteger calledTimes,
|
|
|
+ ActionType<?> action,
|
|
|
+ ActionRequest request,
|
|
|
+ ActionListener<?> listener
|
|
|
+ ) {
|
|
|
+ assertThat(action, instanceOf(PutLifecycleAction.class));
|
|
|
+ assertThat(request, instanceOf(PutLifecycleAction.Request.class));
|
|
|
+ final PutLifecycleAction.Request putRequest = (PutLifecycleAction.Request) request;
|
|
|
+ assertThat(putRequest.getPolicy().getName(), equalTo("custom-plugin-policy"));
|
|
|
+ assertNotNull(listener);
|
|
|
+ calledTimes.incrementAndGet();
|
|
|
+ }
|
|
|
+
|
|
|
private ClusterChangedEvent createClusterChangedEvent(Map<String, Integer> existingTemplates, DiscoveryNodes nodes) {
|
|
|
return createClusterChangedEvent(existingTemplates, Collections.emptyMap(), Collections.emptyMap(), nodes);
|
|
|
}
|