diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java b/server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java index d4bc58c299435..d986e4c285ed7 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java @@ -1490,12 +1490,6 @@ public Builder templates(Map templates) { return this; } - @Deprecated(forRemoval = true) - public Builder put(String name, ComponentTemplate componentTemplate) { - getSingleProject().put(name, componentTemplate); - return this; - } - @Deprecated(forRemoval = true) public Builder componentTemplates(Map componentTemplates) { getSingleProject().componentTemplates(componentTemplates); @@ -1536,12 +1530,6 @@ public boolean put(String aliasName, String dataStream, Boolean isWriteDataStrea return getSingleProject().put(aliasName, dataStream, isWriteDataStream, filter); } - @Deprecated(forRemoval = true) - public Builder removeDataStream(String name) { - getSingleProject().removeDataStream(name); - return this; - } - public Builder putCustom(String type, ClusterCustom custom) { customs.put(type, Objects.requireNonNull(custom, type)); return this; diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingService.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingService.java index 809f2e6178044..43884812658e7 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingService.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingService.java @@ -11,6 +11,7 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.client.internal.Client; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.ComponentTemplate; import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; import org.elasticsearch.cluster.metadata.IndexMetadata; @@ -19,10 +20,10 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.metadata.Template; -import org.elasticsearch.cluster.project.ProjectResolver; import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.NotMultiProjectCapable; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.Tuple; import org.elasticsearch.license.XPackLicenseState; @@ -178,8 +179,7 @@ private MetadataMigrateToDataTiersRoutingService() {} * ILM routing allocations. It also returns a summary of the affected abstractions encapsulated in {@link MigratedEntities} */ public static Tuple migrateToDataTiersRouting( - ClusterState currentState, - ProjectResolver projectResolver, + ProjectState currentState, @Nullable String nodeAttrName, @Nullable String indexTemplateToDelete, NamedXContentRegistry xContentRegistry, @@ -187,7 +187,7 @@ public static Tuple migrateToDataTiersRouting( XPackLicenseState licenseState, boolean dryRun ) { - ProjectMetadata currentProjectMetadata = projectResolver.getProjectMetadata(currentState); + ProjectMetadata currentProjectMetadata = currentState.metadata(); if (dryRun == false) { IndexLifecycleMetadata currentMetadata = currentProjectMetadata.custom(IndexLifecycleMetadata.TYPE); if (currentMetadata != null && currentILMMode(currentProjectMetadata) != STOPPED) { @@ -197,7 +197,9 @@ public static Tuple migrateToDataTiersRouting( } } - Metadata.Builder mb = Metadata.builder(currentState.metadata()); + @NotMultiProjectCapable // We're doing something fishy here by updating the Metadata even though we're inside the scope of a single + // project. This is generally not correct, but since ILM is not properly project-aware, we're making an exception here. + Metadata.Builder mb = Metadata.builder(currentState.cluster().metadata()); ProjectMetadata.Builder newProjectMetadataBuilder = ProjectMetadata.builder(currentProjectMetadata); // remove ENFORCE_DEFAULT_TIER_PREFERENCE from the persistent settings @@ -245,7 +247,7 @@ public static Tuple migrateToDataTiersRouting( attribute ); return Tuple.tuple( - ClusterState.builder(currentState).metadata(mb).putProjectMetadata(newProjectMetadataBuilder).build(), + ClusterState.builder(currentState.cluster()).metadata(mb).putProjectMetadata(newProjectMetadataBuilder).build(), new MigratedEntities(removedIndexTemplateName, migratedIndices, migratedPolicies, migratedTemplates) ); } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportMigrateToDataTiersAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportMigrateToDataTiersAction.java index 7a9ae2c2526e8..2ef2260891f03 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportMigrateToDataTiersAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportMigrateToDataTiersAction.java @@ -90,10 +90,10 @@ protected void masterOperation( ClusterState state, ActionListener listener ) throws Exception { + final var projectId = projectResolver.getProjectId(); if (request.isDryRun()) { MigratedEntities entities = migrateToDataTiersRouting( - state, - projectResolver, + state.projectState(projectId), request.getNodeAttributeName(), request.getLegacyTemplateToDelete(), xContentRegistry, @@ -131,8 +131,7 @@ protected void masterOperation( @Override public ClusterState execute(ClusterState currentState) { Tuple migratedEntitiesTuple = migrateToDataTiersRouting( - currentState, - projectResolver, + currentState.projectState(projectId), request.getNodeAttributeName(), request.getLegacyTemplateToDelete(), xContentRegistry, diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingServiceTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingServiceTests.java index d3fcbfd8df4d3..128eec377a8fc 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingServiceTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingServiceTests.java @@ -10,6 +10,7 @@ import org.elasticsearch.client.internal.Client; import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.ComponentTemplate; import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; import org.elasticsearch.cluster.metadata.IndexMetadata; @@ -18,7 +19,6 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.metadata.Template; -import org.elasticsearch.cluster.project.DefaultProjectResolver; import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentHelper; @@ -114,26 +114,21 @@ public void testMigrateIlmPolicyForIndexWithoutILMMetadata() { coldAllocateAction ); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .putCustom( - IndexLifecycleMetadata.TYPE, - new IndexLifecycleMetadata(Map.of(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED) - ) - .put(IndexMetadata.builder(indexName).settings(getBaseIndexSettings())) - .build() + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata(Map.of(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED) ) + .put(IndexMetadata.builder(indexName).settings(getBaseIndexSettings())) .build(); - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedPolicies = migrateIlmPolicies(projectMetadataBuilder, projectMetadata, "data", REGISTRY, client, null); assertThat(migratedPolicies.size(), is(1)); assertThat(migratedPolicies.get(0), is(lifecycleName)); - ClusterState newState = ClusterState.builder(state).putProjectMetadata(projectMetadataBuilder).build(); - IndexLifecycleMetadata updatedLifecycleMetadata = newState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE); + ProjectMetadata newProject = projectMetadataBuilder.build(); + IndexLifecycleMetadata updatedLifecycleMetadata = newProject.custom(IndexLifecycleMetadata.TYPE); LifecyclePolicy lifecyclePolicy = updatedLifecycleMetadata.getPolicies().get(lifecycleName); Map warmActions = lifecyclePolicy.getPhases().get("warm").getActions(); assertThat( @@ -180,26 +175,21 @@ public void testMigrateIlmPolicyForPhaseWithDeactivatedMigrateAction() { randomNonNegativeLong() ); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .putCustom( - IndexLifecycleMetadata.TYPE, - new IndexLifecycleMetadata(Map.of(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED) - ) - .put(IndexMetadata.builder(indexName).settings(getBaseIndexSettings())) - .build() + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata(Map.of(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED) ) + .put(IndexMetadata.builder(indexName).settings(getBaseIndexSettings())) .build(); - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedPolicies = migrateIlmPolicies(projectMetadataBuilder, projectMetadata, "data", REGISTRY, client, null); assertThat(migratedPolicies.size(), is(1)); assertThat(migratedPolicies.get(0), is(lifecycleName)); - ClusterState newState = ClusterState.builder(state).putProjectMetadata(projectMetadataBuilder).build(); - IndexLifecycleMetadata updatedLifecycleMetadata = newState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE); + ProjectMetadata newProject = projectMetadataBuilder.build(); + IndexLifecycleMetadata updatedLifecycleMetadata = newProject.custom(IndexLifecycleMetadata.TYPE); LifecyclePolicy lifecyclePolicy = updatedLifecycleMetadata.getPolicies().get(lifecycleName); Map warmActions = lifecyclePolicy.getPhases().get("warm").getActions(); assertThat( @@ -237,25 +227,20 @@ public void testMigrateIlmPolicyRefreshesCachedPhase() { .settings(getBaseIndexSettings()) .putCustom(ILM_CUSTOM_METADATA_KEY, preMigrationExecutionState.asMap()); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .putCustom( - IndexLifecycleMetadata.TYPE, - new IndexLifecycleMetadata(Map.of(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED) - ) - .put(indexMetadata) - .build() + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata(Map.of(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED) ) + .put(indexMetadata) .build(); - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedPolicies = migrateIlmPolicies(projectMetadataBuilder, projectMetadata, "data", REGISTRY, client, null); assertThat(migratedPolicies.get(0), is(lifecycleName)); - ClusterState newState = ClusterState.builder(state).putProjectMetadata(projectMetadataBuilder).build(); - LifecycleExecutionState newLifecycleState = newState.metadata().getProject().index(indexName).getLifecycleExecutionState(); + ProjectMetadata newProject = projectMetadataBuilder.build(); + LifecycleExecutionState newLifecycleState = newProject.index(indexName).getLifecycleExecutionState(); Map migratedPhaseDefAsMap = getPhaseDefinitionAsMap(newLifecycleState); @@ -291,28 +276,23 @@ public void testMigrateIlmPolicyRefreshesCachedPhase() { .settings(getBaseIndexSettings()) .putCustom(ILM_CUSTOM_METADATA_KEY, preMigrationExecutionState.asMap()); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .putCustom( - IndexLifecycleMetadata.TYPE, - new IndexLifecycleMetadata( - Map.of(policyMetadataWithTotalShardsPerNode.getName(), policyMetadataWithTotalShardsPerNode), - OperationMode.STOPPED - ) - ) - .put(indexMetadata) - .build() + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata( + Map.of(policyMetadataWithTotalShardsPerNode.getName(), policyMetadataWithTotalShardsPerNode), + OperationMode.STOPPED + ) ) + .put(indexMetadata) .build(); - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedPolicies = migrateIlmPolicies(projectMetadataBuilder, projectMetadata, "data", REGISTRY, client, null); assertThat(migratedPolicies.get(0), is(lifecycleName)); - ClusterState newState = ClusterState.builder(state).putProjectMetadata(projectMetadataBuilder).build(); - LifecycleExecutionState newLifecycleState = newState.metadata().getProject().index(indexName).getLifecycleExecutionState(); + ProjectMetadata newProject = projectMetadataBuilder.build(); + LifecycleExecutionState newLifecycleState = newProject.index(indexName).getLifecycleExecutionState(); Map migratedPhaseDefAsMap = getPhaseDefinitionAsMap(newLifecycleState); @@ -340,25 +320,20 @@ public void testMigrateIlmPolicyRefreshesCachedPhase() { .settings(getBaseIndexSettings()) .putCustom(ILM_CUSTOM_METADATA_KEY, preMigrationExecutionState.asMap()); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .putCustom( - IndexLifecycleMetadata.TYPE, - new IndexLifecycleMetadata(Map.of(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED) - ) - .put(indexMetadata) - .build() + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata(Map.of(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED) ) + .put(indexMetadata) .build(); - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedPolicies = migrateIlmPolicies(projectMetadataBuilder, projectMetadata, "data", REGISTRY, client, null); assertThat(migratedPolicies.get(0), is(lifecycleName)); - ClusterState newState = ClusterState.builder(state).putProjectMetadata(projectMetadataBuilder).build(); - LifecycleExecutionState newLifecycleState = newState.metadata().getProject().index(indexName).getLifecycleExecutionState(); + ProjectMetadata newProject = projectMetadataBuilder.build(); + LifecycleExecutionState newLifecycleState = newProject.index(indexName).getLifecycleExecutionState(); Map migratedPhaseDefAsMap = getPhaseDefinitionAsMap(newLifecycleState); @@ -392,25 +367,20 @@ public void testMigrateIlmPolicyRefreshesCachedPhase() { .settings(getBaseIndexSettings()) .putCustom(ILM_CUSTOM_METADATA_KEY, preMigrationExecutionState.asMap()); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .putCustom( - IndexLifecycleMetadata.TYPE, - new IndexLifecycleMetadata(Map.of(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED) - ) - .put(indexMetadata) - .build() + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata(Map.of(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED) ) + .put(indexMetadata) .build(); - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedPolicies = migrateIlmPolicies(projectMetadataBuilder, projectMetadata, "data", REGISTRY, client, null); assertThat(migratedPolicies.get(0), is(lifecycleName)); - ClusterState newState = ClusterState.builder(state).putProjectMetadata(projectMetadataBuilder).build(); - LifecycleExecutionState newLifecycleState = newState.metadata().getProject().index(indexName).getLifecycleExecutionState(); + ProjectMetadata newProject = projectMetadataBuilder.build(); + LifecycleExecutionState newLifecycleState = newProject.index(indexName).getLifecycleExecutionState(); Map migratedPhaseDefAsMap = getPhaseDefinitionAsMap(newLifecycleState); // expecting the phase definition to be refreshed with the index being in the set_priority action @@ -440,25 +410,20 @@ public void testMigrateIlmPolicyRefreshesCachedPhase() { .settings(getBaseIndexSettings()) .putCustom(ILM_CUSTOM_METADATA_KEY, preMigrationExecutionState.asMap()); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .putCustom( - IndexLifecycleMetadata.TYPE, - new IndexLifecycleMetadata(Map.of(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED) - ) - .put(indexMetadata) - .build() + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata(Map.of(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED) ) + .put(indexMetadata) .build(); - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedPolicies = migrateIlmPolicies(projectMetadataBuilder, projectMetadata, "data", REGISTRY, client, null); assertThat(migratedPolicies.get(0), is(lifecycleName)); - ClusterState newState = ClusterState.builder(state).putProjectMetadata(projectMetadataBuilder).build(); - LifecycleExecutionState newLifecycleState = newState.metadata().getProject().index(indexName).getLifecycleExecutionState(); + ProjectMetadata newProject = projectMetadataBuilder.build(); + LifecycleExecutionState newLifecycleState = newProject.index(indexName).getLifecycleExecutionState(); Map migratedPhaseDefAsMap = getPhaseDefinitionAsMap(newLifecycleState); @@ -515,19 +480,15 @@ public void testMigrateIndices() { // index with `warm` data attribute is migrated to the equivalent _tier_preference routing IndexMetadata.Builder indexWithWarmDataAttribute = IndexMetadata.builder("indexWithWarmDataAttribute") .settings(getBaseIndexSettings().put(DATA_ROUTING_REQUIRE_SETTING, "warm")); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexWithWarmDataAttribute)) - .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexWithWarmDataAttribute).build(); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); assertThat(migratedIndices.get(0), is("indexWithWarmDataAttribute")); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexWithWarmDataAttribute"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexWithWarmDataAttribute"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_REQUIRE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_warm,data_hot")); } @@ -536,19 +497,15 @@ public void testMigrateIndices() { // test the migration of the `include.data` configuration to the equivalent _tier_preference routing IndexMetadata.Builder indexWithWarmDataAttribute = IndexMetadata.builder("indexWithWarmDataAttribute") .settings(getBaseIndexSettings().put(DATA_ROUTING_INCLUDE_SETTING, "warm")); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexWithWarmDataAttribute)) - .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexWithWarmDataAttribute).build(); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); assertThat(migratedIndices.get(0), is("indexWithWarmDataAttribute")); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexWithWarmDataAttribute"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexWithWarmDataAttribute"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_INCLUDE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_warm,data_hot")); } @@ -557,19 +514,15 @@ public void testMigrateIndices() { // test the migration of `include.data: frozen` configuration to the equivalent _tier_preference routing IndexMetadata.Builder indexWithFrozenDataAttribute = IndexMetadata.builder("indexWithFrozenDataAttribute") .settings(getBaseIndexSettings().put(DATA_ROUTING_INCLUDE_SETTING, "frozen")); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexWithFrozenDataAttribute)) - .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexWithFrozenDataAttribute).build(); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); assertThat(migratedIndices.get(0), is("indexWithFrozenDataAttribute")); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexWithFrozenDataAttribute"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexWithFrozenDataAttribute"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_INCLUDE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_frozen")); } @@ -585,19 +538,17 @@ public void testMigrateIndices() { .put(DATA_ROUTING_INCLUDE_SETTING, "hot") .put(TIER_PREFERENCE, "data_warm,data_hot") ); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexWithTierPreferenceAndDataAttribute)) + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(indexWithTierPreferenceAndDataAttribute) .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); assertThat(migratedIndices.get(0), is("indexWithTierPreferenceAndDataAttribute")); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexWithTierPreferenceAndDataAttribute"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexWithTierPreferenceAndDataAttribute"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_REQUIRE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_INCLUDE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_cold,data_warm,data_hot")); @@ -614,19 +565,17 @@ public void testMigrateIndices() { .put(DATA_ROUTING_INCLUDE_SETTING, "cold") .put(TIER_PREFERENCE, "data_warm,data_hot") ); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexWithTierPreferenceAndDataAttribute)) + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(indexWithTierPreferenceAndDataAttribute) .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); assertThat(migratedIndices.get(0), is("indexWithTierPreferenceAndDataAttribute")); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexWithTierPreferenceAndDataAttribute"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexWithTierPreferenceAndDataAttribute"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_REQUIRE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_INCLUDE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_cold,data_warm,data_hot")); @@ -639,19 +588,17 @@ public void testMigrateIndices() { // _tier_preference must be updated to reflect the coldest tier configured in the `include.data` attribute IndexMetadata.Builder indexWithTierPreferenceAndDataAttribute = IndexMetadata.builder("indexWithTierPreferenceAndDataAttribute") .settings(getBaseIndexSettings().put(DATA_ROUTING_INCLUDE_SETTING, "cold").put(TIER_PREFERENCE, "data_warm,data_hot")); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexWithTierPreferenceAndDataAttribute)) + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(indexWithTierPreferenceAndDataAttribute) .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); assertThat(migratedIndices.get(0), is("indexWithTierPreferenceAndDataAttribute")); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexWithTierPreferenceAndDataAttribute"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexWithTierPreferenceAndDataAttribute"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_INCLUDE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_cold,data_warm,data_hot")); } @@ -663,19 +610,17 @@ public void testMigrateIndices() { // the _tier_preference must be updated to reflect the coldest tier configured in the `include.data` attribute IndexMetadata.Builder indexWithTierPreferenceAndDataAttribute = IndexMetadata.builder("indexWithTierPreferenceAndDataAttribute") .settings(getBaseIndexSettings().put(DATA_ROUTING_INCLUDE_SETTING, "cold").put(TIER_PREFERENCE, "data_content")); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexWithTierPreferenceAndDataAttribute)) + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(indexWithTierPreferenceAndDataAttribute) .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); assertThat(migratedIndices.get(0), is("indexWithTierPreferenceAndDataAttribute")); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexWithTierPreferenceAndDataAttribute"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexWithTierPreferenceAndDataAttribute"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_INCLUDE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_cold,data_warm,data_hot")); } @@ -687,19 +632,17 @@ public void testMigrateIndices() { // configuration must NOT be changed IndexMetadata.Builder indexWithTierPreferenceAndDataAttribute = IndexMetadata.builder("indexWithTierPreferenceAndDataAttribute") .settings(getBaseIndexSettings().put(DATA_ROUTING_REQUIRE_SETTING, "hot").put(TIER_PREFERENCE, "data_content")); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexWithTierPreferenceAndDataAttribute)) + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(indexWithTierPreferenceAndDataAttribute) .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); assertThat(migratedIndices.get(0), is("indexWithTierPreferenceAndDataAttribute")); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexWithTierPreferenceAndDataAttribute"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexWithTierPreferenceAndDataAttribute"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_INCLUDE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_content")); } @@ -712,19 +655,17 @@ public void testMigrateIndices() { .put(DATA_ROUTING_INCLUDE_SETTING, "cold") .put(TIER_PREFERENCE, "data_warm,data_hot") ); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexWithTierPreferenceAndDataAttribute)) + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(indexWithTierPreferenceAndDataAttribute) .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); assertThat(migratedIndices.get(0), is("indexWithTierPreferenceAndDataAttribute")); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexWithTierPreferenceAndDataAttribute"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexWithTierPreferenceAndDataAttribute"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_REQUIRE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_INCLUDE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_cold,data_warm,data_hot")); @@ -734,19 +675,17 @@ public void testMigrateIndices() { // the include attribute routing is not colder than the existing _tier_preference IndexMetadata.Builder indexWithTierPreferenceAndDataAttribute = IndexMetadata.builder("indexWithTierPreferenceAndDataAttribute") .settings(getBaseIndexSettings().put(DATA_ROUTING_INCLUDE_SETTING, "hot").put(TIER_PREFERENCE, "data_warm,data_hot")); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexWithTierPreferenceAndDataAttribute)) + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(indexWithTierPreferenceAndDataAttribute) .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); assertThat(migratedIndices.get(0), is("indexWithTierPreferenceAndDataAttribute")); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexWithTierPreferenceAndDataAttribute"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexWithTierPreferenceAndDataAttribute"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_REQUIRE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_INCLUDE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_warm,data_hot")); @@ -756,18 +695,16 @@ public void testMigrateIndices() { // index with an unknown `data` attribute routing value should be migrated IndexMetadata.Builder indexWithUnknownDataAttribute = IndexMetadata.builder("indexWithUnknownDataAttribute") .settings(getBaseIndexSettings().put(DATA_ROUTING_REQUIRE_SETTING, "something_else")); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexWithUnknownDataAttribute)) + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(indexWithUnknownDataAttribute) .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexWithUnknownDataAttribute"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexWithUnknownDataAttribute"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_REQUIRE_SETTING), is("something_else")); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_content")); } @@ -777,19 +714,15 @@ public void testMigrateIndices() { // configured IndexMetadata.Builder indexDataAndBoxAttribute = IndexMetadata.builder("indexWithDataAndBoxAttribute") .settings(getBaseIndexSettings().put(DATA_ROUTING_REQUIRE_SETTING, "warm").put(BOX_ROUTING_REQUIRE_SETTING, "box1")); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexDataAndBoxAttribute)) - .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexDataAndBoxAttribute).build(); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); assertThat(migratedIndices.get(0), is("indexWithDataAndBoxAttribute")); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexWithDataAndBoxAttribute"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexWithDataAndBoxAttribute"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_REQUIRE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(BOX_ROUTING_REQUIRE_SETTING), is("box1")); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_warm,data_hot")); @@ -799,16 +732,14 @@ public void testMigrateIndices() { // index that doesn't have any data attribute routing but has another attribute should be migrated IndexMetadata.Builder indexBoxAttribute = IndexMetadata.builder("indexWithBoxAttribute") .settings(getBaseIndexSettings().put(BOX_ROUTING_REQUIRE_SETTING, "warm")); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexBoxAttribute)).build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexBoxAttribute).build(); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexWithBoxAttribute"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexWithBoxAttribute"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_REQUIRE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(BOX_ROUTING_REQUIRE_SETTING), is("warm")); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_content")); @@ -817,18 +748,14 @@ public void testMigrateIndices() { { IndexMetadata.Builder indexNoRoutingAttribute = IndexMetadata.builder("indexNoRoutingAttribute") .settings(getBaseIndexSettings()); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexNoRoutingAttribute)) - .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexNoRoutingAttribute).build(); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexNoRoutingAttribute"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexNoRoutingAttribute"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_REQUIRE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(BOX_ROUTING_REQUIRE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_content")); @@ -844,16 +771,14 @@ public void testMigrateMountedIndices() { .put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), SEARCHABLE_SNAPSHOT_STORE_TYPE) .put(SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING.getKey(), false) ); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(partiallyMountedIndex)).build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()).put(partiallyMountedIndex).build(); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("foo"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("foo"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_INCLUDE_SETTING), nullValue()); assertThat( migratedIndex.getSettings().get(TIER_PREFERENCE), @@ -869,16 +794,15 @@ public void testMigrateMountedIndices() { .put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), SEARCHABLE_SNAPSHOT_STORE_TYPE) .put(SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING.getKey(), true) ); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(partiallyMountedIndex)).build(); - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()).put(partiallyMountedIndex).build(); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); // no index to migrate as the IndexMetadata.Builder#build method adds a tier preference for this partial index assertThat(migratedIndices.size(), is(0)); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("foo"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("foo"); assertThat(migratedIndex.getSettings().get(BOX_ROUTING_REQUIRE_SETTING), is("cold")); // partially mounted index must remain in `data_frozen`, however we do not change the setting assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is(nullValue())); @@ -894,16 +818,14 @@ public void testMigrateMountedIndices() { .put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), SEARCHABLE_SNAPSHOT_STORE_TYPE) .put(SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING.getKey(), false) ); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(partiallyMountedIndex)).build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()).put(partiallyMountedIndex).build(); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("foo"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("foo"); assertThat(migratedIndex.getSettings().get(BOX_ROUTING_REQUIRE_SETTING), is("cold")); assertThat( @@ -920,16 +842,14 @@ public void testMigrateMountedIndices() { .put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), SEARCHABLE_SNAPSHOT_STORE_TYPE) .put(SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING.getKey(), true) ); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(partiallyMountedIndex)).build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()).put(partiallyMountedIndex).build(); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "box"); assertThat(migratedIndices.size(), is(1)); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("foo"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("foo"); assertThat(migratedIndex.getSettings().get(BOX_ROUTING_REQUIRE_SETTING), is(nullValue())); // partially mounted index must have _tier_preference and remain in `data_frozen` (despite the cold custom filtering // attributed) @@ -949,19 +869,15 @@ public void testColdestAttributeIsConvertedToTierPreference() { .put(DATA_ROUTING_INCLUDE_SETTING, "cold") .put(DATA_ROUTING_EXCLUDE_SETTING, "hot") ); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexWithAllRoutingSettings)) - .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexWithAllRoutingSettings).build(); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); assertThat(migratedIndices.get(0), is("indexWithAllRoutingSettings")); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexWithAllRoutingSettings"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexWithAllRoutingSettings"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_INCLUDE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_REQUIRE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_EXCLUDE_SETTING), nullValue()); @@ -976,19 +892,15 @@ public void testColdestAttributeIsConvertedToTierPreference() { .put(DATA_ROUTING_INCLUDE_SETTING, "warm") .put(DATA_ROUTING_EXCLUDE_SETTING, "hot") ); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexWithAllRoutingSettings)) - .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(state); + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexWithAllRoutingSettings).build(); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedIndices = migrateIndices(projectMetadataBuilder, projectMetadata, "data"); assertThat(migratedIndices.size(), is(1)); assertThat(migratedIndices.get(0), is("indexWithAllRoutingSettings")); - ClusterState migratedState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(projectMetadataBuilder).build(); - IndexMetadata migratedIndex = migratedState.metadata().getProject().index("indexWithAllRoutingSettings"); + ProjectMetadata migratedProject = projectMetadataBuilder.build(); + IndexMetadata migratedIndex = migratedProject.index("indexWithAllRoutingSettings"); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_INCLUDE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_REQUIRE_SETTING), nullValue()); assertThat(migratedIndex.getSettings().get(DATA_ROUTING_EXCLUDE_SETTING), nullValue()); @@ -1047,42 +959,39 @@ public void testMigrateToDataTiersRouting() { IndexMetadata.Builder indexWithWarmDataAttribute = IndexMetadata.builder("indexWithWarmDataAttribute") .settings(getBaseIndexSettings().put(DATA_ROUTING_REQUIRE_SETTING, "warm")); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .putCustom( - IndexLifecycleMetadata.TYPE, - new IndexLifecycleMetadata( - Map.of( - policyToMigrate.getName(), - policyWithDataAttribute, - shouldntBeMigratedPolicy.getName(), - policyWithOtherAttribute - ), - OperationMode.STOPPED - ) + ProjectState state = projectStateFromProject( + ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata( + Map.of( + policyToMigrate.getName(), + policyWithDataAttribute, + shouldntBeMigratedPolicy.getName(), + policyWithOtherAttribute + ), + OperationMode.STOPPED ) - .put( - IndexTemplateMetadata.builder("catch-all") - .patterns(List.of("*")) - .settings(Settings.builder().put(DATA_ROUTING_REQUIRE_SETTING, "hot")) - .build() - ) - .put( - IndexTemplateMetadata.builder("other-template") - .patterns(List.of("other-*")) - .settings(Settings.builder().put(DATA_ROUTING_REQUIRE_SETTING, "hot")) - .build() - ) - .put(indexWithUnknownDataAttribute) - .put(indexWithWarmDataAttribute) - ) - .build(); + ) + .put( + IndexTemplateMetadata.builder("catch-all") + .patterns(List.of("*")) + .settings(Settings.builder().put(DATA_ROUTING_REQUIRE_SETTING, "hot")) + .build() + ) + .put( + IndexTemplateMetadata.builder("other-template") + .patterns(List.of("other-*")) + .settings(Settings.builder().put(DATA_ROUTING_REQUIRE_SETTING, "hot")) + .build() + ) + .put(indexWithUnknownDataAttribute) + .put(indexWithWarmDataAttribute) + ); { Tuple migratedEntitiesTuple = migrateToDataTiersRouting( state, - DefaultProjectResolver.INSTANCE, "data", "catch-all", REGISTRY, @@ -1098,17 +1007,16 @@ public void testMigrateToDataTiersRouting() { assertThat(migratedEntities.migratedIndices().size(), is(2)); assertThat(migratedEntities.migratedIndices(), hasItems("indexWithWarmDataAttribute", "indexWithUnknownDataAttribute")); - ClusterState newState = migratedEntitiesTuple.v1(); - assertThat(newState.metadata().getProject().templates().size(), is(1)); - assertThat(newState.metadata().getProject().templates().get("catch-all"), nullValue()); - assertThat(newState.metadata().getProject().templates().get("other-template"), notNullValue()); + final var newProject = migratedEntitiesTuple.v1().metadata().getProject(state.projectId()); + assertThat(newProject.templates().size(), is(1)); + assertThat(newProject.templates().get("catch-all"), nullValue()); + assertThat(newProject.templates().get("other-template"), notNullValue()); } { // let's test a null template name to make sure nothing is removed Tuple migratedEntitiesTuple = migrateToDataTiersRouting( state, - DefaultProjectResolver.INSTANCE, "data", null, REGISTRY, @@ -1124,17 +1032,16 @@ public void testMigrateToDataTiersRouting() { assertThat(migratedEntities.migratedIndices().size(), is(2)); assertThat(migratedEntities.migratedIndices(), hasItems("indexWithWarmDataAttribute", "indexWithUnknownDataAttribute")); - ClusterState newState = migratedEntitiesTuple.v1(); - assertThat(newState.metadata().getProject().templates().size(), is(2)); - assertThat(newState.metadata().getProject().templates().get("catch-all"), notNullValue()); - assertThat(newState.metadata().getProject().templates().get("other-template"), notNullValue()); + final var newProject = migratedEntitiesTuple.v1().metadata().getProject(state.projectId()); + assertThat(newProject.templates().size(), is(2)); + assertThat(newProject.templates().get("catch-all"), notNullValue()); + assertThat(newProject.templates().get("other-template"), notNullValue()); } { // let's test a null node attribute parameter defaults to "data" Tuple migratedEntitiesTuple = migrateToDataTiersRouting( state, - DefaultProjectResolver.INSTANCE, null, null, REGISTRY, @@ -1150,67 +1057,47 @@ public void testMigrateToDataTiersRouting() { assertThat(migratedEntities.migratedIndices(), hasItems("indexWithWarmDataAttribute", "indexWithUnknownDataAttribute")); IndexMetadata migratedIndex; - migratedIndex = migratedEntitiesTuple.v1().metadata().getProject().index("indexWithWarmDataAttribute"); + final var newProject = migratedEntitiesTuple.v1().metadata().getProject(state.projectId()); + migratedIndex = newProject.index("indexWithWarmDataAttribute"); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_warm,data_hot")); - migratedIndex = migratedEntitiesTuple.v1().metadata().getProject().index("indexWithUnknownDataAttribute"); + migratedIndex = newProject.index("indexWithUnknownDataAttribute"); assertThat(migratedIndex.getSettings().get(TIER_PREFERENCE), is("data_content")); } } public void testMigrateToDataTiersRoutingRequiresILMStopped() { { - ClusterState ilmRunningState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder().putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Map.of(), OperationMode.RUNNING)) - ) - .build(); + ProjectState ilmRunningState = projectStateFromProject( + ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Map.of(), OperationMode.RUNNING)) + ); IllegalStateException illegalStateException = expectThrows( IllegalStateException.class, - () -> migrateToDataTiersRouting( - ilmRunningState, - DefaultProjectResolver.INSTANCE, - "data", - "catch-all", - REGISTRY, - client, - null, - false - ) + () -> migrateToDataTiersRouting(ilmRunningState, "data", "catch-all", REGISTRY, client, null, false) ); assertThat(illegalStateException.getMessage(), is("stop ILM before migrating to data tiers, current state is [RUNNING]")); } { - ClusterState ilmStoppingState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder().putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Map.of(), OperationMode.STOPPING)) - ) - .build(); + ProjectState ilmStoppingState = projectStateFromProject( + ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Map.of(), OperationMode.STOPPING)) + ); + IllegalStateException illegalStateException = expectThrows( IllegalStateException.class, - () -> migrateToDataTiersRouting( - ilmStoppingState, - DefaultProjectResolver.INSTANCE, - "data", - "catch" + "-all", - REGISTRY, - client, - null, - false - ) + () -> migrateToDataTiersRouting(ilmStoppingState, "data", "catch" + "-all", REGISTRY, client, null, false) ); assertThat(illegalStateException.getMessage(), is("stop ILM before migrating to data tiers, current state is [STOPPING]")); } { - ClusterState ilmStoppedState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder().putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Map.of(), OperationMode.STOPPED)) - ) - .build(); + ProjectState ilmStoppedState = projectStateFromProject( + ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Map.of(), OperationMode.STOPPED)) + ); Tuple migratedState = migrateToDataTiersRouting( ilmStoppedState, - DefaultProjectResolver.INSTANCE, "data", "catch-all", REGISTRY, @@ -1226,22 +1113,20 @@ public void testMigrateToDataTiersRoutingRequiresILMStopped() { public void testDryRunDoesntRequireILMStopped() { { - ClusterState ilmRunningState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder().putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Map.of(), OperationMode.RUNNING)) - ) - .build(); - migrateToDataTiersRouting(ilmRunningState, DefaultProjectResolver.INSTANCE, "data", "catch-all", REGISTRY, client, null, true); + ProjectState ilmRunningState = projectStateFromProject( + ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Map.of(), OperationMode.RUNNING)) + ); + migrateToDataTiersRouting(ilmRunningState, "data", "catch-all", REGISTRY, client, null, true); // no exceptions } { - ClusterState ilmStoppingState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder().putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Map.of(), OperationMode.STOPPING)) - ) - .build(); - migrateToDataTiersRouting(ilmStoppingState, DefaultProjectResolver.INSTANCE, "data", "catch-all", REGISTRY, client, null, true); + ProjectState ilmStoppingState = projectStateFromProject( + ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Map.of(), OperationMode.STOPPING)) + ); + migrateToDataTiersRouting(ilmStoppingState, "data", "catch-all", REGISTRY, client, null, true); // no exceptions } } @@ -1253,12 +1138,11 @@ public void testMigrationDoesNotRemoveComposableTemplates() { .build(); String composableTemplateName = "catch-all-composable-template"; - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(composableTemplateName, composableIndexTemplate).build()) - .build(); + ProjectState state = projectStateFromProject( + ProjectMetadata.builder(randomProjectIdOrDefault()).put(composableTemplateName, composableIndexTemplate) + ); Tuple migratedEntitiesTuple = migrateToDataTiersRouting( - clusterState, - DefaultProjectResolver.INSTANCE, + state, "data", composableTemplateName, REGISTRY, @@ -1268,45 +1152,29 @@ public void testMigrationDoesNotRemoveComposableTemplates() { ); assertThat(migratedEntitiesTuple.v2().removedIndexTemplateName(), nullValue()); // the composable template still exists, however it was migrated to not use the custom require.data routing setting - assertThat(migratedEntitiesTuple.v1().metadata().getProject().templatesV2().get(composableTemplateName), is(notNullValue())); + final var newProject = migratedEntitiesTuple.v1().metadata().getProject(state.projectId()); + assertThat(newProject.templatesV2().get(composableTemplateName), is(notNullValue())); } public void testMigrationSetsEnforceTierPreferenceToTrue() { - ClusterState clusterState; + ProjectState state; Tuple migratedEntitiesTuple; Metadata.Builder metadata; - // if the cluster state doesn't mention the setting, it ends up true - clusterState = ClusterState.builder(ClusterName.DEFAULT).build(); - migratedEntitiesTuple = migrateToDataTiersRouting( - clusterState, - DefaultProjectResolver.INSTANCE, - null, - null, - REGISTRY, - client, - null, - false - ); + // if the cluster project doesn't mention the setting, it ends up true + state = projectStateWithEmptyProject(); + migratedEntitiesTuple = migrateToDataTiersRouting(state, null, null, REGISTRY, client, null, false); assertTrue(DataTier.ENFORCE_DEFAULT_TIER_PREFERENCE_SETTING.get(migratedEntitiesTuple.v1().metadata().persistentSettings())); assertFalse(migratedEntitiesTuple.v1().metadata().transientSettings().keySet().contains(DataTier.ENFORCE_DEFAULT_TIER_PREFERENCE)); // regardless of the true/false combinations of persistent/transient settings for ENFORCE_DEFAULT_TIER_PREFERENCE, // it ends up set to true as a persistent setting (and if there was a transient setting, it was removed) - metadata = Metadata.builder(); - metadata.persistentSettings(Settings.builder().put(ENFORCE_DEFAULT_TIER_PREFERENCE, randomBoolean()).build()); - metadata.transientSettings(Settings.builder().put(ENFORCE_DEFAULT_TIER_PREFERENCE, randomBoolean()).build()); - clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).build(); - migratedEntitiesTuple = migrateToDataTiersRouting( - clusterState, - DefaultProjectResolver.INSTANCE, - null, - null, - REGISTRY, - client, - null, - false - ); + metadata = Metadata.builder() + .persistentSettings(Settings.builder().put(ENFORCE_DEFAULT_TIER_PREFERENCE, randomBoolean()).build()) + .transientSettings(Settings.builder().put(ENFORCE_DEFAULT_TIER_PREFERENCE, randomBoolean()).build()) + .put(ProjectMetadata.builder(state.projectId())); + state = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).build().projectState(state.projectId()); + migratedEntitiesTuple = migrateToDataTiersRouting(state, null, null, REGISTRY, client, null, false); assertTrue(DataTier.ENFORCE_DEFAULT_TIER_PREFERENCE_SETTING.get(migratedEntitiesTuple.v1().metadata().persistentSettings())); assertFalse(migratedEntitiesTuple.v1().metadata().transientSettings().keySet().contains(DataTier.ENFORCE_DEFAULT_TIER_PREFERENCE)); } @@ -1403,19 +1271,14 @@ public void testMigrateLegacyIndexTemplates() { Map.of() ); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .put(templateWithRequireRouting) - .put(templateWithIncludeRouting) - .put(templateWithRequireAndIncludeRoutings) - .put(templateWithExcludeRouting) - .put(templateWithoutCustomRoutings) - .build() - ) + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(templateWithRequireRouting) + .put(templateWithIncludeRouting) + .put(templateWithRequireAndIncludeRoutings) + .put(templateWithExcludeRouting) + .put(templateWithoutCustomRoutings) .build(); - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(clusterState); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migrateLegacyTemplates = MetadataMigrateToDataTiersRoutingService.migrateLegacyTemplates( @@ -1526,19 +1389,13 @@ public void testMigrateComposableIndexTemplates() { .version(randomLong()) .build(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .put("template-with-require-routing", templateWithRequireRouting) - .put("template-with-include-routing", templateWithIncludeRouting) - .put("template-with-exclude-routing", templateWithExcludeRouting) - .put("template-with-require-and-include-routing", templateWithRequireAndIncludeRoutings) - .put("template-without-custom-routing", templateWithoutCustomRoutings) - .build() - ) + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put("template-with-require-routing", templateWithRequireRouting) + .put("template-with-include-routing", templateWithIncludeRouting) + .put("template-with-exclude-routing", templateWithExcludeRouting) + .put("template-with-require-and-include-routing", templateWithRequireAndIncludeRoutings) + .put("template-without-custom-routing", templateWithoutCustomRoutings) .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(clusterState); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); List migratedComposableTemplates = MetadataMigrateToDataTiersRoutingService.migrateComposableTemplates( @@ -1629,20 +1486,15 @@ public void testMigrateComponentTemplates() { null ); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .put("template-with-require-routing", compTemplateWithRequireRouting) - .put("template-with-include-routing", compTemplateWithIncludeRouting) - .put("template-with-exclude-routing", compTemplateWithExcludeRouting) - .put("template-with-require-and-include-routing", compTemplateWithRequireAndIncludeRoutings) - .put("template-without-custom-routing", compTemplateWithoutCustomRoutings) - .build() - ) + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put("template-with-require-routing", compTemplateWithRequireRouting) + .put("template-with-include-routing", compTemplateWithIncludeRouting) + .put("template-with-exclude-routing", compTemplateWithExcludeRouting) + .put("template-with-require-and-include-routing", compTemplateWithRequireAndIncludeRoutings) + .put("template-without-custom-routing", compTemplateWithoutCustomRoutings) .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(clusterState); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); + List migratedComponentTemplates = MetadataMigrateToDataTiersRoutingService.migrateComponentTemplates( projectMetadataBuilder, projectMetadata, @@ -1719,19 +1571,14 @@ public void testMigrateIndexAndComponentTemplates() { null ); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .put(legacyTemplateWithRequireRouting) - .put("composable-template-with-require-routing", composableTemplateWithRequireRouting) - .put("component-with-require-and-include-routing", compTemplateWithRequireAndIncludeRoutings) - .put("component-template-without-custom-routing", compTemplateWithoutCustomRoutings) - .build() - ) + ProjectMetadata projectMetadata = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(legacyTemplateWithRequireRouting) + .put("composable-template-with-require-routing", composableTemplateWithRequireRouting) + .put("component-with-require-and-include-routing", compTemplateWithRequireAndIncludeRoutings) + .put("component-template-without-custom-routing", compTemplateWithoutCustomRoutings) .build(); - - ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(clusterState); ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata); + MetadataMigrateToDataTiersRoutingService.MigratedTemplates migratedTemplates = MetadataMigrateToDataTiersRoutingService .migrateIndexAndComponentTemplates(projectMetadataBuilder, projectMetadata, nodeAttrName); assertThat(migratedTemplates.migratedLegacyTemplates(), is(List.of("template-with-require-routing"))); diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PolicyStepsRegistryTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PolicyStepsRegistryTests.java index 5e3054e7c873d..78aa4117958c8 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PolicyStepsRegistryTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PolicyStepsRegistryTests.java @@ -7,27 +7,16 @@ package org.elasticsearch.xpack.ilm; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterName; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; -import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.cluster.node.DiscoveryNode; -import org.elasticsearch.cluster.node.DiscoveryNodeUtils; -import org.elasticsearch.cluster.node.DiscoveryNodes; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.transport.TransportAddress; -import org.elasticsearch.common.xcontent.ChunkedToXContent; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.NodeRoles; import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ilm.ErrorStep; import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.ilm.InitializePolicyContextStep; @@ -199,8 +188,7 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { IndexLifecycleMetadata lifecycleMetadata = new IndexLifecycleMetadata(policyMap, OperationMode.RUNNING); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setPhase("new"); - Metadata metadata = Metadata.builder() - .persistentSettings(settings(IndexVersion.current()).build()) + ProjectMetadata currentProject = ProjectMetadata.builder(randomProjectIdOrDefault()) .putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata) .put( IndexMetadata.builder("test") @@ -212,27 +200,12 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) ) .build(); - try (XContentBuilder builder = JsonXContent.contentBuilder()) { - builder.startObject(); - ChunkedToXContent.wrapAsToXContent(metadata).toXContent(builder, ToXContent.EMPTY_PARAMS); - builder.endObject(); - logger.info("--> metadata: {}", Strings.toString(builder)); - } - String nodeId = randomAlphaOfLength(10); - DiscoveryNode masterNode = DiscoveryNodeUtils.builder(nodeId) - .applySettings(NodeRoles.masterNode(settings(IndexVersion.current()).build())) - .address(new TransportAddress(TransportAddress.META_ADDRESS, 9300)) - .build(); - ClusterState currentState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(metadata) - .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) - .build(); // start with empty registry PolicyStepsRegistry registry = new PolicyStepsRegistry(NamedXContentRegistry.EMPTY, client, null); // add new policy - registry.update(currentState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE)); + registry.update(currentProject.custom(IndexLifecycleMetadata.TYPE)); assertThat(registry.getFirstStep(newPolicy.getName()), equalTo(policySteps.get(0))); assertThat(registry.getLifecyclePolicyMap().size(), equalTo(1)); @@ -245,36 +218,30 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { for (Step step : policySteps) { LifecycleExecutionState.Builder newIndexState = LifecycleExecutionState.builder(); newIndexState.setPhase(step.getKey().phase()); - currentState = ClusterState.builder(currentState) - .metadata( - Metadata.builder(currentState.metadata()) - .put( - IndexMetadata.builder(currentState.metadata().getProject().index("test")) - .settings(Settings.builder().put(currentState.metadata().getProject().index("test").getSettings())) - .putCustom(ILM_CUSTOM_METADATA_KEY, newIndexState.build().asMap()) - ) + currentProject = ProjectMetadata.builder(currentProject) + .put( + IndexMetadata.builder(currentProject.index("test")) + .settings(Settings.builder().put(currentProject.index("test").getSettings())) + .putCustom(ILM_CUSTOM_METADATA_KEY, newIndexState.build().asMap()) ) - .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) .build(); - registry.update(currentState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE)); + registry.update(currentProject.custom(IndexLifecycleMetadata.TYPE)); assertThat(registeredStepsForPolicy.get(step.getKey()), equalTo(step)); - assertThat(registry.getStep(metadata.getProject().index(index), step.getKey()), equalTo(step)); + assertThat(registry.getStep(currentProject.index(index), step.getKey()), equalTo(step)); } Map registryPolicyMap = registry.getLifecyclePolicyMap(); Map registryFirstStepMap = registry.getFirstStepMap(); Map> registryStepMap = registry.getStepMap(); - registry.update(currentState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE)); + registry.update(currentProject.custom(IndexLifecycleMetadata.TYPE)); assertThat(registry.getLifecyclePolicyMap(), equalTo(registryPolicyMap)); assertThat(registry.getFirstStepMap(), equalTo(registryFirstStepMap)); assertThat(registry.getStepMap(), equalTo(registryStepMap)); // remove policy lifecycleMetadata = new IndexLifecycleMetadata(Map.of(), OperationMode.RUNNING); - currentState = ClusterState.builder(currentState) - .metadata(Metadata.builder(metadata).putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata)) - .build(); - registry.update(currentState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE)); + currentProject = ProjectMetadata.builder(currentProject).putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata).build(); + registry.update(currentProject.custom(IndexLifecycleMetadata.TYPE)); assertTrue(registry.getLifecyclePolicyMap().isEmpty()); assertTrue(registry.getFirstStepMap().isEmpty()); assertTrue(registry.getStepMap().isEmpty()); @@ -295,22 +262,12 @@ public void testUpdateChangedPolicy() { new LifecyclePolicyMetadata(newPolicy, headers, randomNonNegativeLong(), randomNonNegativeLong()) ); IndexLifecycleMetadata lifecycleMetadata = new IndexLifecycleMetadata(policyMap, OperationMode.RUNNING); - Metadata metadata = Metadata.builder() - .persistentSettings(settings(IndexVersion.current()).build()) + ProjectMetadata currentProject = ProjectMetadata.builder(randomProjectIdOrDefault()) .putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata) .build(); - String nodeId = randomAlphaOfLength(10); - DiscoveryNode masterNode = DiscoveryNodeUtils.builder(nodeId) - .applySettings(NodeRoles.masterNode(settings(IndexVersion.current()).build())) - .address(new TransportAddress(TransportAddress.META_ADDRESS, 9300)) - .build(); - ClusterState currentState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(metadata) - .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) - .build(); PolicyStepsRegistry registry = new PolicyStepsRegistry(NamedXContentRegistry.EMPTY, client, null); // add new policy - registry.update(currentState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE)); + registry.update(currentProject.custom(IndexLifecycleMetadata.TYPE)); // swap out policy newPolicy = LifecyclePolicyTests.randomTestLifecyclePolicy(policyName); @@ -318,10 +275,8 @@ public void testUpdateChangedPolicy() { Map.of(policyName, new LifecyclePolicyMetadata(newPolicy, Map.of(), randomNonNegativeLong(), randomNonNegativeLong())), OperationMode.RUNNING ); - currentState = ClusterState.builder(currentState) - .metadata(Metadata.builder(metadata).putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata)) - .build(); - registry.update(currentState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE)); + currentProject = ProjectMetadata.builder(currentProject).putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata).build(); + registry.update(currentProject.custom(IndexLifecycleMetadata.TYPE)); // TODO(talevy): assert changes... right now we do not support updates to policies. will require internal cleanup } @@ -360,8 +315,7 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setPhase("warm"); lifecycleState.setPhaseDefinition(phaseJson); - Metadata metadata = Metadata.builder() - .persistentSettings(settings(IndexVersion.current()).build()) + ProjectMetadata currentProject = ProjectMetadata.builder(randomProjectIdOrDefault()) .putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata) .put( IndexMetadata.builder("test") @@ -373,27 +327,12 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) ) .build(); - try (XContentBuilder builder = JsonXContent.contentBuilder()) { - builder.startObject(); - ChunkedToXContent.wrapAsToXContent(metadata).toXContent(builder, ToXContent.EMPTY_PARAMS); - builder.endObject(); - logger.info("--> metadata: {}", Strings.toString(builder)); - } - String nodeId = randomAlphaOfLength(10); - DiscoveryNode masterNode = DiscoveryNodeUtils.builder(nodeId) - .applySettings(NodeRoles.masterNode(settings(IndexVersion.current()).build())) - .address(new TransportAddress(TransportAddress.META_ADDRESS, 9300)) - .build(); - ClusterState currentState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(metadata) - .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) - .build(); // start with empty registry PolicyStepsRegistry registry = new PolicyStepsRegistry(REGISTRY, client, null); // add new policy - registry.update(currentState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE)); + registry.update(currentProject.custom(IndexLifecycleMetadata.TYPE)); Map registeredStepsForPolicy = registry.getStepMap().get(newPolicy.getName()); Step shrinkStep = registeredStepsForPolicy.entrySet() @@ -402,7 +341,7 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except .findFirst() .get() .getValue(); - Step gotStep = registry.getStep(metadata.getProject().index(index), shrinkStep.getKey()); + Step gotStep = registry.getStep(currentProject.index(index), shrinkStep.getKey()); assertThat(((ShrinkStep) shrinkStep).getNumberOfShards(), equalTo(1)); assertThat(((ShrinkStep) gotStep).getNumberOfShards(), equalTo(1)); @@ -412,17 +351,10 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except new LifecyclePolicyMetadata(updatedPolicy, headers, randomNonNegativeLong(), randomNonNegativeLong()) ); lifecycleMetadata = new IndexLifecycleMetadata(policyMap, OperationMode.RUNNING); - metadata = Metadata.builder(metadata).putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata).build(); - try (XContentBuilder builder = JsonXContent.contentBuilder()) { - builder.startObject(); - ChunkedToXContent.wrapAsToXContent(metadata).toXContent(builder, ToXContent.EMPTY_PARAMS); - builder.endObject(); - logger.info("--> metadata: {}", Strings.toString(builder)); - } - currentState = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).build(); + currentProject = ProjectMetadata.builder(currentProject).putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata).build(); // Update the policies - registry.update(currentState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE)); + registry.update(currentProject.custom(IndexLifecycleMetadata.TYPE)); registeredStepsForPolicy = registry.getStepMap().get(newPolicy.getName()); shrinkStep = registeredStepsForPolicy.entrySet() @@ -431,7 +363,7 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except .findFirst() .get() .getValue(); - gotStep = registry.getStep(metadata.getProject().index(index), shrinkStep.getKey()); + gotStep = registry.getStep(currentProject.index(index), shrinkStep.getKey()); assertThat(((ShrinkStep) shrinkStep).getNumberOfShards(), equalTo(2)); assertThat(((ShrinkStep) gotStep).getNumberOfShards(), equalTo(1)); }