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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.cluster.metadata.ComponentTemplate;
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
import org.elasticsearch.cluster.node.DiscoveryNodes;
Expand Down Expand Up @@ -553,16 +554,15 @@ private ClusterState createClusterState(
}
IngestMetadata ingestMetadata = new IngestMetadata(ingestPipelineConfigurations);

final var project = ProjectMetadata.builder(randomProjectIdOrDefault())
.componentTemplates(componentTemplates)
.indexTemplates(composableTemplates)
.putCustom(IndexLifecycleMetadata.TYPE, ilmMeta)
.putCustom(IngestMetadata.TYPE, ingestMetadata)
.build();
return ClusterState.builder(new ClusterName("test"))
.metadata(
Metadata.builder()
.componentTemplates(componentTemplates)
.indexTemplates(composableTemplates)
.transientSettings(nodeSettings)
.putCustom(IndexLifecycleMetadata.TYPE, ilmMeta)
.putCustom(IngestMetadata.TYPE, ingestMetadata)
.build()
)
// We need to ensure only one project is present in the cluster state to simplify the assertions in these tests.
.metadata(Metadata.builder().projectMetadata(Map.of(project.id(), project)).build())
.blocks(new ClusterBlocks.Builder().build())
.nodes(nodes)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -881,17 +881,6 @@ private ClusterState createClusterState(
// ------------- functionality unit test --------

public void testFindRolloverTargetDataStreams() {
ClusterState state = ClusterState.EMPTY_STATE;
state = ClusterState.builder(state)
.metadata(
Metadata.builder(state.metadata())
.put(DataStreamTestHelper.newInstance("ds1", Collections.singletonList(new Index(".ds-ds1-000001", "ds1i"))))
.put(DataStreamTestHelper.newInstance("ds2", Collections.singletonList(new Index(".ds-ds2-000001", "ds2i"))))
.put(DataStreamTestHelper.newInstance("ds3", Collections.singletonList(new Index(".ds-ds3-000001", "ds3i"))))
.put(DataStreamTestHelper.newInstance("ds4", Collections.singletonList(new Index(".ds-ds4-000001", "ds4i"))))
)
.build();

ComposableIndexTemplate it1 = ComposableIndexTemplate.builder()
.indexPatterns(List.of("ds1*", "ds2*", "ds3*"))
.priority(100L)
Expand All @@ -910,16 +899,19 @@ public void testFindRolloverTargetDataStreams() {
.dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate())
.build();

state = ClusterState.builder(state)
.metadata(Metadata.builder(state.metadata()).put("it1", it1).put("it2", it2).put("it5", it5))
ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault())
.put(DataStreamTestHelper.newInstance("ds1", Collections.singletonList(new Index(".ds-ds1-000001", "ds1i"))))
.put(DataStreamTestHelper.newInstance("ds2", Collections.singletonList(new Index(".ds-ds2-000001", "ds2i"))))
.put(DataStreamTestHelper.newInstance("ds3", Collections.singletonList(new Index(".ds-ds3-000001", "ds3i"))))
.put(DataStreamTestHelper.newInstance("ds4", Collections.singletonList(new Index(".ds-ds4-000001", "ds4i"))))
.put("it1", it1)
.put("it2", it2)
.put("it5", it5)
.build();

assertThat(
IndexTemplateRegistry.findRolloverTargetDataStreams(state.metadata().getProject(), "it1", it1),
containsInAnyOrder("ds1", "ds3")
);
assertThat(IndexTemplateRegistry.findRolloverTargetDataStreams(state.metadata().getProject(), "it2", it2), contains("ds2"));
assertThat(IndexTemplateRegistry.findRolloverTargetDataStreams(state.metadata().getProject(), "it5", it5), empty());
assertThat(IndexTemplateRegistry.findRolloverTargetDataStreams(project, "it1", it1), containsInAnyOrder("ds1", "ds3"));
assertThat(IndexTemplateRegistry.findRolloverTargetDataStreams(project, "it2", it2), contains("ds2"));
assertThat(IndexTemplateRegistry.findRolloverTargetDataStreams(project, "it5", it5), empty());
}

// -------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import org.elasticsearch.cluster.metadata.ComponentTemplate;
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.cluster.project.TestProjectResolvers;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.TriFunction;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.Strings;
import org.elasticsearch.ingest.IngestMetadata;
import org.elasticsearch.ingest.PipelineConfiguration;
Expand Down Expand Up @@ -452,16 +452,15 @@ private ClusterState createClusterState(
.collect(Collectors.toMap(Map.Entry::getKey, e -> new LifecyclePolicyMetadata(e.getValue(), Collections.emptyMap(), 1, 1)));
IndexLifecycleMetadata ilmMeta = new IndexLifecycleMetadata(existingILMMeta, OperationMode.RUNNING);

final var project = ProjectMetadata.builder(randomProjectIdOrDefault())
.indexTemplates(composableTemplates)
.componentTemplates(componentTemplates)
.putCustom(IngestMetadata.TYPE, ingestMetadata)
.putCustom(IndexLifecycleMetadata.TYPE, ilmMeta)
.build();
return ClusterState.builder(new ClusterName("test"))
.metadata(
Metadata.builder()
.indexTemplates(composableTemplates)
.componentTemplates(componentTemplates)
.transientSettings(Settings.EMPTY)
.putCustom(IngestMetadata.TYPE, ingestMetadata)
.putCustom(IndexLifecycleMetadata.TYPE, ilmMeta)
.build()
)
// We need to ensure only one project is present in the cluster state to simplify the assertions in these tests.
.metadata(Metadata.builder().projectMetadata(Map.of(project.id(), project)).build())
.blocks(new ClusterBlocks.Builder().build())
.nodes(nodes)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import org.elasticsearch.cluster.metadata.ComponentTemplate;
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.cluster.project.TestProjectResolvers;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.TriFunction;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.Strings;
import org.elasticsearch.ingest.IngestMetadata;
import org.elasticsearch.ingest.PipelineConfiguration;
Expand Down Expand Up @@ -478,16 +478,15 @@ private ClusterState createClusterState(
.collect(Collectors.toMap(Map.Entry::getKey, e -> new LifecyclePolicyMetadata(e.getValue(), Collections.emptyMap(), 1, 1)));
IndexLifecycleMetadata ilmMeta = new IndexLifecycleMetadata(existingILMMeta, OperationMode.RUNNING);

final var project = ProjectMetadata.builder(randomProjectIdOrDefault())
.indexTemplates(composableTemplates)
.componentTemplates(componentTemplates)
.putCustom(IngestMetadata.TYPE, ingestMetadata)
.putCustom(IndexLifecycleMetadata.TYPE, ilmMeta)
.build();
return ClusterState.builder(new ClusterName("test"))
.metadata(
Metadata.builder()
.indexTemplates(composableTemplates)
.componentTemplates(componentTemplates)
.transientSettings(Settings.EMPTY)
.putCustom(IngestMetadata.TYPE, ingestMetadata)
.putCustom(IndexLifecycleMetadata.TYPE, ilmMeta)
.build()
)
// We need to ensure only one project is present in the cluster state to simplify the assertions in these tests.
.metadata(Metadata.builder().projectMetadata(Map.of(project.id(), project)).build())
.blocks(new ClusterBlocks.Builder().build())
.nodes(nodes)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.cluster.block.ClusterBlocks;
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
import org.elasticsearch.cluster.node.DiscoveryNodes;
Expand Down Expand Up @@ -383,7 +384,7 @@ private ClusterChangedEvent createClusterChangedEvent(
Map<String, LifecyclePolicy> existingPolicies,
DiscoveryNodes nodes
) {
ClusterState cs = createClusterState(Settings.EMPTY, existingTemplates, existingPolicies, nodes);
ClusterState cs = createClusterState(existingTemplates, existingPolicies, nodes);
ClusterChangedEvent realEvent = new ClusterChangedEvent(
"created-from-test",
cs,
Expand All @@ -396,7 +397,6 @@ private ClusterChangedEvent createClusterChangedEvent(
}

private ClusterState createClusterState(
Settings nodeSettings,
Map<String, Integer> existingComposableTemplates,
Map<String, LifecyclePolicy> existingPolicies,
DiscoveryNodes nodes
Expand All @@ -413,14 +413,13 @@ private ClusterState createClusterState(
.collect(Collectors.toMap(Map.Entry::getKey, e -> new LifecyclePolicyMetadata(e.getValue(), Collections.emptyMap(), 1, 1)));
IndexLifecycleMetadata ilmMeta = new IndexLifecycleMetadata(existingILMMeta, OperationMode.RUNNING);

final var project = ProjectMetadata.builder(randomProjectIdOrDefault())
.indexTemplates(composableTemplates)
.putCustom(IndexLifecycleMetadata.TYPE, ilmMeta)
.build();
return ClusterState.builder(new ClusterName("test"))
.metadata(
Metadata.builder()
.indexTemplates(composableTemplates)
.transientSettings(nodeSettings)
.putCustom(IndexLifecycleMetadata.TYPE, ilmMeta)
.build()
)
// We need to ensure only one project is present in the cluster state to simplify the assertions in these tests.
.metadata(Metadata.builder().projectMetadata(Map.of(project.id(), project)).build())
.blocks(new ClusterBlocks.Builder().build())
.nodes(nodes)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
package org.elasticsearch.xpack.slm.history;

import org.elasticsearch.client.internal.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.xcontent.NamedXContentRegistry;
import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata;
import org.elasticsearch.xpack.core.ilm.LifecyclePolicy;
import org.elasticsearch.xpack.core.template.IndexTemplateConfig;
import org.elasticsearch.xpack.core.template.IndexTemplateRegistry;
Expand All @@ -23,9 +21,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static org.elasticsearch.xpack.core.ClientHelper.INDEX_LIFECYCLE_ORIGIN;
import static org.elasticsearch.xpack.core.ilm.LifecycleSettings.SLM_HISTORY_INDEX_ENABLED_SETTING;
Expand Down Expand Up @@ -101,18 +96,4 @@ protected List<LifecyclePolicyConfig> getLifecycleConfigs() {
protected String getOrigin() {
return INDEX_LIFECYCLE_ORIGIN; // TODO use separate SLM origin?
}

public boolean validate(ClusterState state) {
boolean allTemplatesPresent = getComposableTemplateConfigs().keySet()
.stream()
.allMatch(name -> state.metadata().getProject().templatesV2().containsKey(name));

Optional<Map<String, LifecyclePolicy>> maybePolicies = Optional.<IndexLifecycleMetadata>ofNullable(
state.metadata().getProject().custom(IndexLifecycleMetadata.TYPE)
).map(IndexLifecycleMetadata::getPolicies);
Set<String> policyNames = getLifecyclePolicies().stream().map(LifecyclePolicy::getName).collect(Collectors.toSet());

boolean allPoliciesPresent = maybePolicies.map(policies -> policies.keySet().containsAll(policyNames)).orElse(false);
return allTemplatesPresent && allPoliciesPresent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.elasticsearch.cluster.block.ClusterBlocks;
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
import org.elasticsearch.cluster.node.DiscoveryNodes;
Expand Down Expand Up @@ -303,23 +304,6 @@ public void testThatMissingMasterNodeDoesNothing() {
registry.clusterChanged(event);
}

public void testValidate() {
assertFalse(registry.validate(createClusterState(Settings.EMPTY, Collections.emptyMap(), Collections.emptyMap(), null)));
assertFalse(
registry.validate(
createClusterState(Settings.EMPTY, Collections.singletonMap(SLM_TEMPLATE_NAME, null), Collections.emptyMap(), null)
)
);

Map<String, LifecyclePolicy> policyMap = new HashMap<>();
policyMap.put(SLM_POLICY_NAME, new LifecyclePolicy(SLM_POLICY_NAME, new HashMap<>()));
assertFalse(registry.validate(createClusterState(Settings.EMPTY, Collections.emptyMap(), policyMap, null)));

assertTrue(
registry.validate(createClusterState(Settings.EMPTY, Collections.singletonMap(SLM_TEMPLATE_NAME, null), policyMap, null))
);
}

public void testTemplateNameIsVersioned() {
assertThat(SLM_TEMPLATE_NAME, endsWith("-" + INDEX_TEMPLATE_VERSION));
}
Expand Down Expand Up @@ -394,7 +378,7 @@ private ClusterChangedEvent createClusterChangedEvent(
Map<String, LifecyclePolicy> existingPolicies,
DiscoveryNodes nodes
) {
ClusterState cs = createClusterState(Settings.EMPTY, existingTemplates, existingPolicies, nodes);
ClusterState cs = createClusterState(existingTemplates, existingPolicies, nodes);
ClusterChangedEvent realEvent = new ClusterChangedEvent(
"created-from-test",
cs,
Expand All @@ -407,7 +391,6 @@ private ClusterChangedEvent createClusterChangedEvent(
}

private ClusterState createClusterState(
Settings nodeSettings,
Map<String, Integer> existingTemplates,
Map<String, LifecyclePolicy> existingPolicies,
DiscoveryNodes nodes
Expand All @@ -426,14 +409,13 @@ private ClusterState createClusterState(
.collect(Collectors.toMap(Map.Entry::getKey, e -> new LifecyclePolicyMetadata(e.getValue(), Collections.emptyMap(), 1, 1)));
IndexLifecycleMetadata ilmMeta = new IndexLifecycleMetadata(existingILMMeta, OperationMode.RUNNING);

final var project = ProjectMetadata.builder(randomProjectIdOrDefault())
.indexTemplates(indexTemplates)
.putCustom(IndexLifecycleMetadata.TYPE, ilmMeta)
.build();
return ClusterState.builder(new ClusterName("test"))
.metadata(
Metadata.builder()
.indexTemplates(indexTemplates)
.transientSettings(nodeSettings)
.putCustom(IndexLifecycleMetadata.TYPE, ilmMeta)
.build()
)
// We need to ensure only one project is present in the cluster state to simplify the assertions in these tests.
.metadata(Metadata.builder().projectMetadata(Map.of(project.id(), project)).build())
.blocks(new ClusterBlocks.Builder().build())
.nodes(nodes)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.cluster.metadata.ComponentTemplate;
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
import org.elasticsearch.cluster.node.DiscoveryNodes;
Expand Down Expand Up @@ -607,7 +608,7 @@ private ClusterChangedEvent createClusterChangedEvent(
DiscoveryNodes nodes,
boolean addRegistryPipelines
) {
ClusterState cs = createClusterState(Settings.EMPTY, existingTemplates, existingPolicies, nodes, addRegistryPipelines);
ClusterState cs = createClusterState(existingTemplates, existingPolicies, nodes, addRegistryPipelines);
ClusterChangedEvent realEvent = new ClusterChangedEvent(
"created-from-test",
cs,
Expand All @@ -620,7 +621,6 @@ private ClusterChangedEvent createClusterChangedEvent(
}

private ClusterState createClusterState(
Settings nodeSettings,
Map<String, Integer> existingComponentTemplates,
Map<String, LifecyclePolicy> existingPolicies,
DiscoveryNodes nodes,
Expand Down Expand Up @@ -651,15 +651,14 @@ private ClusterState createClusterState(
}
IngestMetadata ingestMetadata = new IngestMetadata(ingestPipelines);

final var project = ProjectMetadata.builder(randomProjectIdOrDefault())
.componentTemplates(componentTemplates)
.putCustom(IndexLifecycleMetadata.TYPE, ilmMeta)
.putCustom(IngestMetadata.TYPE, ingestMetadata)
.build();
return ClusterState.builder(new ClusterName("test"))
.metadata(
Metadata.builder()
.componentTemplates(componentTemplates)
.transientSettings(nodeSettings)
.putCustom(IndexLifecycleMetadata.TYPE, ilmMeta)
.putCustom(IngestMetadata.TYPE, ingestMetadata)
.build()
)
// We need to ensure only one project is present in the cluster state to simplify the assertions in these tests.
.metadata(Metadata.builder().projectMetadata(Map.of(project.id(), project)).build())
.blocks(new ClusterBlocks.Builder().build())
.nodes(nodes)
.build();
Expand Down
Loading