Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2829,4 +2829,11 @@ public static IndexSearcher newSearcher(IndexReader r, boolean maybeWrap, boolea
public static ProjectState projectStateFromProject(ProjectMetadata.Builder project) {
return ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(project).build().projectState(project.getId());
}

/**
* Constructs an empty {@link ProjectState} with one (empty) project.
*/
public static ProjectState emptyProjectState() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think something like projectStateWithEmptyProject() would be less confusing? I don't think this would be immediately obvious to me that it meant this, rather than a project state with no projects. (The latter is actually impossible, I think, but those less familiar with the code — e.g. me — might not realize that straight away.)

return projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()));
}
}
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.support.ActiveShardCount;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ProjectState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.routing.IndexRoutingTable;
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
Expand Down Expand Up @@ -45,18 +45,14 @@ public boolean isRetryable() {
}

@Override
public Result isConditionMet(Index index, ClusterState clusterState) {
IndexMetadata idxMeta = clusterState.metadata().getProject().index(index);
public Result isConditionMet(Index index, ProjectState currentState) {
IndexMetadata idxMeta = currentState.metadata().index(index);
if (idxMeta == 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 new Result(false, null);
}
if (ActiveShardCount.ALL.enoughShardsActive(
clusterState.metadata().getProject(),
clusterState.routingTable(),
index.getName()
) == false) {
if (ActiveShardCount.ALL.enoughShardsActive(currentState.metadata(), currentState.routingTable(), index.getName()) == false) {
logger.debug(
"[{}] lifecycle action for index [{}] cannot make progress because not all shards are active",
getKey().action(),
Expand All @@ -68,12 +64,12 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
AllocationDeciders allocationDeciders = new AllocationDeciders(
List.of(
new FilterAllocationDecider(
clusterState.getMetadata().settings(),
currentState.cluster().metadata().settings(),
new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)
)
)
);
int allocationPendingAllShards = getPendingAllocations(index, allocationDeciders, clusterState);
int allocationPendingAllShards = getPendingAllocations(index, allocationDeciders, currentState);

if (allocationPendingAllShards > 0) {
logger.debug(
Expand All @@ -89,22 +85,22 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
}
}

static int getPendingAllocations(Index index, AllocationDeciders allocationDeciders, ClusterState clusterState) {
static int getPendingAllocations(Index index, AllocationDeciders allocationDeciders, ProjectState currentState) {
// All the allocation attributes are already set so just need to check
// if the allocation has happened
RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, clusterState, null, null, System.nanoTime());
RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, currentState.cluster(), null, null, System.nanoTime());

int allocationPendingAllShards = 0;

final IndexRoutingTable indexRoutingTable = clusterState.getRoutingTable().index(index);
final IndexRoutingTable indexRoutingTable = currentState.routingTable().index(index);
for (int shardId = 0; shardId < indexRoutingTable.size(); shardId++) {
final IndexShardRoutingTable indexShardRoutingTable = indexRoutingTable.shard(shardId);
for (int copy = 0; copy < indexShardRoutingTable.size(); copy++) {
ShardRouting shardRouting = indexShardRoutingTable.shard(copy);
String currentNodeId = shardRouting.currentNodeId();
boolean canRemainOnCurrentNode = allocationDeciders.canRemain(
shardRouting,
clusterState.getRoutingNodes().node(currentNodeId),
currentState.cluster().getRoutingNodes().node(currentNodeId),
allocation
).type() == Decision.Type.YES;
if (canRemainOnCurrentNode == false || shardRouting.started() == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

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.DataStream;
import org.elasticsearch.cluster.metadata.IndexAbstraction;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.common.Strings;
import org.elasticsearch.index.Index;
import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo;
Expand All @@ -39,9 +38,8 @@ public boolean isRetryable() {
}

@Override
public Result isConditionMet(Index index, ClusterState clusterState) {
Metadata metadata = clusterState.metadata();
IndexMetadata indexMetadata = metadata.getProject().index(index);
public Result isConditionMet(Index index, ProjectState currentState) {
IndexMetadata indexMetadata = currentState.metadata().index(index);
String indexName = index.getName();

if (indexMetadata == null) {
Expand All @@ -56,7 +54,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
}

String policyName = indexMetadata.getLifecyclePolicyName();
IndexAbstraction indexAbstraction = clusterState.metadata().getProject().getIndicesLookup().get(indexName);
IndexAbstraction indexAbstraction = currentState.metadata().getIndicesLookup().get(indexName);
assert indexAbstraction != null : "invalid cluster metadata. index [" + indexName + "] was not found";
DataStream dataStream = indexAbstraction.getParentDataStream();
if (dataStream != null) {
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.SingleNodeShutdownMetadata;
import org.elasticsearch.cluster.routing.IndexRoutingTable;
Expand Down Expand Up @@ -51,8 +51,8 @@ public boolean isCompletable() {
}

@Override
public Result isConditionMet(Index index, ClusterState clusterState) {
IndexMetadata idxMeta = clusterState.metadata().getProject().index(index);
public Result isConditionMet(Index index, ProjectState currentState) {
IndexMetadata idxMeta = currentState.metadata().index(index);

if (idxMeta == null) {
// Index must have been since deleted, ignore it
Expand All @@ -69,10 +69,10 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
throw new IllegalStateException("Cannot check shrink allocation as there are no allocation rules by _id");
}

var shutdown = clusterState.metadata().nodeShutdowns().get(idShardsShouldBeOn);
var shutdown = currentState.cluster().metadata().nodeShutdowns().get(idShardsShouldBeOn);
boolean nodeBeingRemoved = shutdown != null && shutdown.getType() != SingleNodeShutdownMetadata.Type.RESTART;

final IndexRoutingTable routingTable = clusterState.getRoutingTable().index(index);
final IndexRoutingTable routingTable = currentState.routingTable().index(index);
int foundShards = 0;
for (ShardRouting shard : routingTable.shardsWithState(ShardRoutingState.STARTED)) {
final String currentNodeId = shard.currentNodeId();
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.common.Strings;
import org.elasticsearch.index.Index;
Expand Down Expand Up @@ -40,8 +40,8 @@ public Integer getNumberOfShards() {
}

@Override
public Result isConditionMet(Index index, ClusterState clusterState) {
IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index);
public Result isConditionMet(Index index, ProjectState currentState) {
IndexMetadata indexMetadata = currentState.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());
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.index.Index;
import org.elasticsearch.xcontent.ToXContentObject;

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

public abstract Result isConditionMet(Index index, ClusterState clusterState);
public abstract Result isConditionMet(Index index, ProjectState currentState);

/**
* Whether the step can be completed at all. This only affects the
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.common.Strings;
Expand Down Expand Up @@ -52,15 +52,15 @@ public boolean isRetryable() {
}

@Override
public Result isConditionMet(Index index, ClusterState clusterState) {
IndexMetadata idxMeta = clusterState.metadata().getProject().index(index);
public Result isConditionMet(Index index, ProjectState currentState) {
IndexMetadata idxMeta = currentState.metadata().index(index);
if (idxMeta == 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 new Result(false, null);
}

Result stepResult = stepToExecute.isConditionMet(index, clusterState);
Result stepResult = stepToExecute.isConditionMet(index, currentState);

if (stepResult.complete() == false) {
// checking the threshold after we execute the step to make sure we execute the wrapped step at least once (because time is a
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.support.ActiveShardCount;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ProjectState;
import org.elasticsearch.cluster.metadata.DesiredNodes;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders;
Expand Down Expand Up @@ -45,8 +45,8 @@ public boolean isRetryable() {
}

@Override
public Result isConditionMet(Index index, ClusterState clusterState) {
IndexMetadata idxMeta = clusterState.metadata().getProject().index(index);
public Result isConditionMet(Index index, ProjectState currentState) {
IndexMetadata idxMeta = currentState.metadata().index(index);
if (idxMeta == 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());
Expand All @@ -55,16 +55,12 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
List<String> preferredTierConfiguration = idxMeta.getTierPreference();
Optional<String> availableDestinationTier = DataTierAllocationDecider.preferredAvailableTier(
preferredTierConfiguration,
clusterState.getNodes(),
DesiredNodes.latestFromClusterState(clusterState),
clusterState.metadata().nodeShutdowns()
currentState.cluster().getNodes(),
DesiredNodes.latestFromClusterState(currentState.cluster()),
currentState.cluster().metadata().nodeShutdowns()
);

if (ActiveShardCount.ALL.enoughShardsActive(
clusterState.metadata().getProject(),
clusterState.routingTable(),
index.getName()
) == false) {
if (ActiveShardCount.ALL.enoughShardsActive(currentState.metadata(), currentState.routingTable(), index.getName()) == false) {
if (preferredTierConfiguration.isEmpty()) {
logger.debug(
"[{}] lifecycle action for index [{}] cannot make progress because not all shards are active",
Expand Down Expand Up @@ -103,7 +99,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
return new Result(true, null);
}

int allocationPendingAllShards = getPendingAllocations(index, DECIDERS, clusterState);
int allocationPendingAllShards = getPendingAllocations(index, DECIDERS, currentState);

if (allocationPendingAllShards > 0) {
String statusMessage = availableDestinationTier.map(
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.index.Index;

/**
Expand All @@ -27,7 +27,7 @@ public boolean isRetryable() {
}

@Override
public Result isConditionMet(Index index, ClusterState clusterState) {
public Result isConditionMet(Index index, ProjectState currentState) {
// We always want to move forward with this step so this should always be true
return new Result(true, null);
}
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.support.ActiveShardCount;
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.common.Strings;
Expand Down Expand Up @@ -42,8 +42,8 @@ public boolean isRetryable() {
}

@Override
public Result isConditionMet(Index index, ClusterState clusterState) {
IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index);
public Result isConditionMet(Index index, ProjectState currentState) {
IndexMetadata indexMetadata = currentState.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());
Expand All @@ -55,16 +55,16 @@ public Result isConditionMet(Index index, ClusterState clusterState) {

// We only want to make progress if all shards of the shrunk index are
// active
boolean indexExists = clusterState.metadata().getProject().index(shrunkenIndexName) != null;
boolean indexExists = currentState.metadata().index(shrunkenIndexName) != null;
if (indexExists == false) {
return new Result(false, new Info(false, -1, false));
}
boolean allShardsActive = ActiveShardCount.ALL.enoughShardsActive(
clusterState.metadata().getProject(),
clusterState.routingTable(),
currentState.metadata(),
currentState.routingTable(),
shrunkenIndexName
);
int numShrunkIndexShards = clusterState.metadata().getProject().index(shrunkenIndexName).getNumberOfShards();
int numShrunkIndexShards = currentState.metadata().index(shrunkenIndexName).getNumberOfShards();
if (allShardsActive) {
return new Result(true, null);
} else {
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.common.Strings;
Expand Down Expand Up @@ -41,24 +41,21 @@ public boolean isRetryable() {
}

@Override
public Result isConditionMet(Index index, ClusterState clusterState) {
IndexMetadata idxMeta = clusterState.getMetadata().getProject().index(index);
public Result isConditionMet(Index index, ProjectState currentState) {
IndexMetadata idxMeta = currentState.metadata().index(index);
if (idxMeta == 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 new Result(false, null);
}
String shrunkenIndexSource = IndexMetadata.INDEX_RESIZE_SOURCE_NAME.get(
clusterState.metadata().getProject().index(index).getSettings()
);
String shrunkenIndexSource = IndexMetadata.INDEX_RESIZE_SOURCE_NAME.get(currentState.metadata().index(index).getSettings());
if (Strings.isNullOrEmpty(shrunkenIndexSource)) {
throw new IllegalStateException("step[" + NAME + "] is checking an un-shrunken index[" + index.getName() + "]");
}

LifecycleExecutionState lifecycleState = idxMeta.getLifecycleExecutionState();
String targetIndexName = getShrinkIndexName(shrunkenIndexSource, lifecycleState);
boolean isConditionMet = index.getName().equals(targetIndexName)
&& clusterState.metadata().getProject().index(shrunkenIndexSource) == null;
boolean isConditionMet = index.getName().equals(targetIndexName) && currentState.metadata().index(shrunkenIndexSource) == null;
if (isConditionMet) {
return new Result(true, null);
} else {
Expand Down
Loading