Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 21 additions & 0 deletions server/src/main/java/org/elasticsearch/cluster/ProjectState.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.routing.RoutingTable;
import org.elasticsearch.common.Strings;

import java.util.Objects;
import java.util.function.Consumer;
Expand Down Expand Up @@ -74,4 +75,24 @@ public ClusterState updatedState(Consumer<ProjectMetadata.Builder> projectBuilde
projectBuilderConsumer.accept(projectBuilder);
return ClusterState.builder(cluster).putProjectMetadata(projectBuilder).build();
}

/**
* Build a new {@link ProjectState} with the updated {@code project}.
*/
public ProjectState withProject(ProjectMetadata updatedProject) {
// No need to build a new object if the project is unchanged.
if (metadata() == updatedProject) {
return this;
}
if (project.equals(updatedProject.id()) == false) {
throw new IllegalArgumentException(
Strings.format(
"Unable to update project state with project ID [%s] because updated project has ID [%s]",
project,
updatedProject.id()
)
);
}
return new ProjectState(ClusterState.builder(cluster).putProjectMetadata(updatedProject).build(), project);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@
import org.elasticsearch.client.internal.ElasticsearchClient;
import org.elasticsearch.client.internal.Requests;
import org.elasticsearch.cluster.ClusterModule;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ProjectState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.common.CheckedSupplier;
import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.breaker.CircuitBreaker;
Expand Down Expand Up @@ -2818,4 +2822,11 @@ public static IndexSearcher newSearcher(IndexReader r, boolean maybeWrap, boolea
}
return newSearcher(r, maybeWrap, wrapWithAssertions, Concurrency.NONE);
}

/**
* Constructs a {@link ProjectState} for the given {@link ProjectMetadata.Builder}.
*/
public static ProjectState projectStateFromProject(ProjectMetadata.Builder project) {
return ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(project).build().projectState(project.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.util.SetOnce;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ProjectState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.index.Index;
Expand Down Expand Up @@ -62,15 +62,15 @@ public boolean isRetryable() {
}

@Override
public ClusterState performAction(Index index, ClusterState clusterState) {
IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index);
public ProjectState performAction(Index index, ProjectState projectState) {
IndexMetadata indexMetadata = projectState.metadata().index(index);
if (indexMetadata == null) {
// Index must have been since deleted, ignore it
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName());
return clusterState;
return projectState;
}
predicateValue.set(predicate.test(index, clusterState.metadata().getProject()));
return clusterState;
predicateValue.set(predicate.test(index, projectState.metadata()));
return projectState;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
package org.elasticsearch.xpack.core.ilm;

import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ProjectState;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.index.Index;

Expand All @@ -19,7 +19,7 @@ public ClusterStateActionStep(StepKey key, StepKey nextStepKey) {
super(key, nextStepKey);
}

public abstract ClusterState performAction(Index index, ClusterState clusterState);
public abstract ProjectState performAction(Index index, ProjectState projectState);

/**
* Returns a tuple of index name to step key for an index *other* than the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.util.SetOnce;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ProjectState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
import org.elasticsearch.core.Tuple;
Expand Down Expand Up @@ -66,18 +66,18 @@ public Tuple<String, StepKey> indexForAsyncInvocation() {
}

@Override
public ClusterState performAction(Index index, ClusterState clusterState) {
IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index);
public ProjectState performAction(Index index, ProjectState projectState) {
IndexMetadata indexMetadata = projectState.metadata().index(index);
if (indexMetadata == null) {
// Index must have been since deleted, ignore it
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName());
return clusterState;
return projectState;
}
// get target index
LifecycleExecutionState lifecycleState = indexMetadata.getLifecycleExecutionState();
String targetIndexName = targetIndexNameSupplier.apply(index.getName(), lifecycleState);
calculatedTargetIndexName.set(targetIndexName);
IndexMetadata targetIndexMetadata = clusterState.metadata().getProject().index(targetIndexName);
IndexMetadata targetIndexMetadata = projectState.metadata().index(targetIndexName);

if (targetIndexMetadata == null) {
logger.warn(
Expand All @@ -101,10 +101,8 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
newLifecycleState.setAction(action);
newLifecycleState.setStep(step);

return LifecycleExecutionStateUtils.newClusterStateWithLifecycleState(
clusterState,
targetIndexMetadata.getIndex(),
newLifecycleState.build()
return projectState.withProject(
projectState.metadata().withLifecycleState(targetIndexMetadata.getIndex(), newLifecycleState.build())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ProjectState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
Expand Down Expand Up @@ -62,21 +62,21 @@ BiFunction<String, LifecycleExecutionState, String> getTargetIndexNameSupplier()
}

@Override
public ClusterState performAction(Index index, ClusterState clusterState) {
public ProjectState performAction(Index index, ProjectState projectState) {
String sourceIndexName = index.getName();
IndexMetadata sourceIndexMetadata = clusterState.metadata().getProject().index(sourceIndexName);
IndexMetadata sourceIndexMetadata = projectState.metadata().index(sourceIndexName);
if (sourceIndexMetadata == null) {
// Index must have been since deleted, ignore it
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), sourceIndexName);
return clusterState;
return projectState;
}

if (settingsKeys == null || settingsKeys.length == 0) {
return clusterState;
return projectState;
}

String targetIndexName = targetIndexNameSupplier.apply(sourceIndexName, sourceIndexMetadata.getLifecycleExecutionState());
IndexMetadata targetIndexMetadata = clusterState.metadata().getProject().index(targetIndexName);
IndexMetadata targetIndexMetadata = projectState.metadata().index(targetIndexName);
if (targetIndexMetadata == null) {
String errorMessage = Strings.format(
"index [%s] is being referenced by ILM action [%s] on step [%s] but it doesn't exist",
Expand All @@ -94,11 +94,10 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
settings.put(key, value);
}

ProjectMetadata.Builder newProject = ProjectMetadata.builder(clusterState.getMetadata().getProject())
.put(
IndexMetadata.builder(targetIndexMetadata).settingsVersion(targetIndexMetadata.getSettingsVersion() + 1).settings(settings)
);
return ClusterState.builder(clusterState).putProjectMetadata(newProject).build();
IndexMetadata.Builder updatedIndex = IndexMetadata.builder(targetIndexMetadata)
.settingsVersion(targetIndexMetadata.getSettingsVersion() + 1)
.settings(settings);
return projectState.withProject(ProjectMetadata.builder(projectState.metadata()).put(updatedIndex).build());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ProjectState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
import org.elasticsearch.index.Index;
Expand All @@ -35,12 +35,12 @@ public DownsamplePrepareLifeCycleStateStep(StepKey key, StepKey nextStepKey, Dat
}

@Override
public ClusterState performAction(Index index, ClusterState clusterState) {
IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index);
public ProjectState performAction(Index index, ProjectState projectState) {
IndexMetadata indexMetadata = projectState.metadata().index(index);
if (indexMetadata == null) {
// Index must have been since deleted, ignore it
LOGGER.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName());
return clusterState;
return projectState;
}

LifecycleExecutionState lifecycleState = indexMetadata.getLifecycleExecutionState();
Expand All @@ -49,11 +49,7 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
final String downsampleIndexName = generateDownsampleIndexName(DOWNSAMPLED_INDEX_PREFIX, indexMetadata, fixedInterval);
newLifecycleState.setDownsampleIndexName(downsampleIndexName);

return LifecycleExecutionStateUtils.newClusterStateWithLifecycleState(
clusterState,
indexMetadata.getIndex(),
newLifecycleState.build()
);
return projectState.withProject(projectState.metadata().withLifecycleState(indexMetadata.getIndex(), newLifecycleState.build()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ProjectState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
Expand Down Expand Up @@ -46,12 +46,12 @@ public String getSnapshotRepository() {
}

@Override
public ClusterState performAction(Index index, ClusterState clusterState) {
IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index);
public ProjectState performAction(Index index, ProjectState projectState) {
IndexMetadata indexMetadata = projectState.metadata().index(index);
if (indexMetadata == null) {
// Index must have been since deleted, ignore it
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName());
return clusterState;
return projectState;
}

String policyName = indexMetadata.getLifecyclePolicyName();
Expand All @@ -60,7 +60,7 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
// validate that the snapshot repository exists -- because policies are refreshed on later retries, and because
// this fails prior to the snapshot repository being recorded in the ilm metadata, the policy can just be corrected
// and everything will pass on the subsequent retry
if (RepositoriesMetadata.get(clusterState).repository(snapshotRepository) == null) {
if (RepositoriesMetadata.get(projectState.metadata()).repository(snapshotRepository) == null) {
throw new IllegalStateException(
"repository ["
+ snapshotRepository
Expand Down Expand Up @@ -95,11 +95,7 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
newLifecycleState.setSnapshotName(snapshotName);
}

return LifecycleExecutionStateUtils.newClusterStateWithLifecycleState(
clusterState,
indexMetadata.getIndex(),
newLifecycleState.build()
);
return projectState.withProject(projectState.metadata().withLifecycleState(indexMetadata.getIndex(), newLifecycleState.build()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ProjectState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
import org.elasticsearch.cluster.metadata.LifecycleExecutionState.Builder;
Expand Down Expand Up @@ -61,20 +61,20 @@ BiFunction<String, Builder, Builder> lifecycleStateSetter() {
}

@Override
public ClusterState performAction(Index index, ClusterState clusterState) {
IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index);
public ProjectState performAction(Index index, ProjectState projectState) {
IndexMetadata indexMetadata = projectState.metadata().index(index);
if (indexMetadata == null) {
// Index must have been since deleted, ignore it
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName());
return clusterState;
return projectState;
}

LifecycleExecutionState lifecycleState = indexMetadata.getLifecycleExecutionState();

Builder newLifecycleState = LifecycleExecutionState.builder(lifecycleState);
String policyName = indexMetadata.getLifecyclePolicyName();
String generatedIndexName = generateIndexName(prefix, index.getName());
ActionRequestValidationException validationException = validateGeneratedIndexName(generatedIndexName, clusterState.projectState());
ActionRequestValidationException validationException = validateGeneratedIndexName(generatedIndexName, projectState);
if (validationException != null) {
logger.warn(
"unable to generate a valid index name as part of policy [{}] for index [{}] due to [{}]",
Expand All @@ -86,11 +86,7 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
}
lifecycleStateSetter.apply(generatedIndexName, newLifecycleState);

return LifecycleExecutionStateUtils.newClusterStateWithLifecycleState(
clusterState,
indexMetadata.getIndex(),
newLifecycleState.build()
);
return projectState.withProject(projectState.metadata().withLifecycleState(indexMetadata.getIndex(), newLifecycleState.build()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ProjectState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
Expand All @@ -33,17 +33,17 @@ public final class InitializePolicyContextStep extends ClusterStateActionStep {
}

@Override
public ClusterState performAction(Index index, ClusterState clusterState) {
IndexMetadata indexMetadata = clusterState.getMetadata().getProject().index(index);
public ProjectState performAction(Index index, ProjectState projectState) {
IndexMetadata indexMetadata = projectState.metadata().index(index);
if (indexMetadata == null) {
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName());
// Index must have been since deleted, ignore it
return clusterState;
return projectState;
}

LifecycleExecutionState lifecycleState = indexMetadata.getLifecycleExecutionState();
if (lifecycleState.lifecycleDate() != null) {
return clusterState;
return projectState;
}

LifecycleExecutionState newLifecycleState = LifecycleExecutionState.builder(lifecycleState)
Expand All @@ -63,11 +63,7 @@ public ClusterState performAction(Index index, ClusterState clusterState) {

if (parsedOriginationDate == null) {
// we don't need to update the LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, so we can use the fast path
return LifecycleExecutionStateUtils.newClusterStateWithLifecycleState(
clusterState,
indexMetadata.getIndex(),
newLifecycleState
);
return projectState.withProject(projectState.metadata().withLifecycleState(indexMetadata.getIndex(), newLifecycleState));
} else {
// we do need to update the LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, so we can't use the fast path
IndexMetadata.Builder builder = IndexMetadata.builder(indexMetadata);
Expand All @@ -79,9 +75,7 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
.build()
);
builder.putCustom(ILM_CUSTOM_METADATA_KEY, newLifecycleState.asMap());
return ClusterState.builder(clusterState)
.putProjectMetadata(ProjectMetadata.builder(clusterState.metadata().getProject()).put(builder).build())
.build();
return projectState.withProject(ProjectMetadata.builder(projectState.metadata()).put(builder).build());
}
}

Expand Down
Loading