diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AbstractUnfollowIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AbstractUnfollowIndexStep.java index 17e0a78d8a8f3..05b8b2bd6461a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AbstractUnfollowIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AbstractUnfollowIndexStep.java @@ -8,8 +8,8 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import java.util.Map; @@ -25,7 +25,7 @@ abstract class AbstractUnfollowIndexStep extends AsyncActionStep { @Override public final void performAction( IndexMetadata indexMetadata, - ClusterState currentClusterState, + ProjectState currentState, ClusterStateObserver observer, ActionListener listener ) { @@ -36,8 +36,8 @@ public final void performAction( return; } - innerPerformAction(followerIndex, currentClusterState, listener); + innerPerformAction(followerIndex, currentState, listener); } - abstract void innerPerformAction(String followerIndex, ClusterState currentClusterState, ActionListener listener); + abstract void innerPerformAction(String followerIndex, ProjectState currentState, ActionListener listener); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java index aa2e3caab0291..21dc4ab74d80c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java @@ -8,9 +8,11 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.cluster.metadata.ProjectId; +import org.elasticsearch.core.Nullable; /** * Performs an action which must be performed asynchronously because it may take time to complete. @@ -24,17 +26,23 @@ public AsyncActionStep(StepKey key, StepKey nextStepKey, Client client) { this.client = client; } - protected Client getClient() { + // For testing only + @Nullable + Client getClientWithoutProject() { return client; } + protected Client getClient(ProjectId projectId) { + return client.projectClient(projectId); + } + public boolean indexSurvives() { return true; } public abstract void performAction( IndexMetadata indexMetadata, - ClusterState currentClusterState, + ProjectState currentState, ClusterStateObserver observer, ActionListener listener ); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncBranchingStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncBranchingStep.java index 152a740312d4f..93f4eac7ceec6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncBranchingStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncBranchingStep.java @@ -10,12 +10,13 @@ import org.apache.lucene.util.SetOnce; import org.elasticsearch.action.ActionListener; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.cluster.metadata.ProjectId; +import org.elasticsearch.common.TriConsumer; import java.util.Objects; -import java.util.function.BiConsumer; /** * This step changes its {@link #getNextStepKey()} depending on the @@ -26,14 +27,14 @@ public class AsyncBranchingStep extends AsyncActionStep { private final StepKey nextStepKeyOnFalse; private final StepKey nextStepKeyOnTrue; - private final BiConsumer> asyncPredicate; + private final TriConsumer> asyncPredicate; private final SetOnce predicateValue; public AsyncBranchingStep( StepKey key, StepKey nextStepKeyOnFalse, StepKey nextStepKeyOnTrue, - BiConsumer> asyncPredicate, + TriConsumer> asyncPredicate, Client client ) { // super.nextStepKey is set to null since it is not used by this step @@ -52,11 +53,11 @@ public boolean isRetryable() { @Override public void performAction( IndexMetadata indexMetadata, - ClusterState currentClusterState, + ProjectState currentState, ClusterStateObserver observer, ActionListener listener ) { - asyncPredicate.accept(indexMetadata, listener.safeMap(value -> { + asyncPredicate.apply(currentState.projectId(), indexMetadata, listener.safeMap(value -> { predicateValue.set(value); return null; })); @@ -87,7 +88,7 @@ final StepKey getNextStepKeyOnTrue() { /** * @return the next step if {@code predicate} is true */ - final BiConsumer> getAsyncPredicate() { + final TriConsumer> getAsyncPredicate() { return asyncPredicate; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncRetryDuringSnapshotActionStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncRetryDuringSnapshotActionStep.java index c7b4e626c1781..94e44e7f9118f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncRetryDuringSnapshotActionStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncRetryDuringSnapshotActionStep.java @@ -14,8 +14,11 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; import org.elasticsearch.cluster.NotMasterException; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.SnapshotsInProgress; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.cluster.metadata.ProjectId; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; @@ -39,44 +42,48 @@ public AsyncRetryDuringSnapshotActionStep(StepKey key, StepKey nextStepKey, Clie @Override public final void performAction( IndexMetadata indexMetadata, - ClusterState currentClusterState, + ProjectState currentState, ClusterStateObserver observer, ActionListener listener ) { // Wrap the original listener to handle exceptions caused by ongoing snapshots SnapshotExceptionListener snapshotExceptionListener = new SnapshotExceptionListener( + currentState.projectId(), indexMetadata.getIndex(), listener, observer, - currentClusterState.nodes().getLocalNode() + currentState.cluster().nodes().getLocalNode() ); - performDuringNoSnapshot(indexMetadata, currentClusterState, snapshotExceptionListener); + performDuringNoSnapshot(indexMetadata, currentState.metadata(), snapshotExceptionListener); } /** * Method to be performed during which no snapshots for the index are already underway. */ - abstract void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentClusterState, ActionListener listener); + abstract void performDuringNoSnapshot(IndexMetadata indexMetadata, ProjectMetadata currentProject, ActionListener listener); /** * SnapshotExceptionListener is an injected listener wrapper that checks to see if a particular * action failed due to a {@code SnapshotInProgressException}. If it did, then it registers a * ClusterStateObserver listener waiting for the next time the snapshot is not running, - * re-running the step's {@link #performAction(IndexMetadata, ClusterState, ClusterStateObserver, ActionListener)} + * re-running the step's {@link #performAction(IndexMetadata, ProjectState, ClusterStateObserver, ActionListener)} * method when the snapshot is no longer running. */ class SnapshotExceptionListener implements ActionListener { + private final ProjectId projectId; private final Index index; private final ActionListener originalListener; private final ClusterStateObserver observer; private final DiscoveryNode localNode; SnapshotExceptionListener( + ProjectId projectId, Index index, ActionListener originalListener, ClusterStateObserver observer, DiscoveryNode localNode ) { + this.projectId = projectId; this.index = index; this.originalListener = originalListener; this.observer = observer; @@ -106,13 +113,14 @@ public void onNewClusterState(ClusterState state) { } try { logger.debug("[{}] retrying ILM step after snapshot has completed", indexName); - IndexMetadata idxMeta = state.metadata().getProject().index(index); + final var projectState = state.projectState(projectId); + IndexMetadata idxMeta = projectState.metadata().index(index); if (idxMeta == null) { // The index has since been deleted, mission accomplished! originalListener.onResponse(null); } else { // Re-invoke the performAction method with the new state - performAction(idxMeta, state, observer, originalListener); + performAction(idxMeta, projectState, observer, originalListener); } } catch (Exception e) { originalListener.onFailure(e); @@ -133,7 +141,7 @@ public void onTimeout(TimeValue timeout) { // ILM actions should only run on master, lets bail on failover return true; } - if (state.metadata().getProject().index(index) == null) { + if (state.metadata().getProject(projectId).index(index) == null) { // The index has since been deleted, mission accomplished! return true; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStep.java index ce9df691cc428..c93dab7cdc2fa 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStep.java @@ -12,9 +12,9 @@ import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.IndexNotFoundException; @@ -36,11 +36,11 @@ public boolean isRetryable() { } @Override - void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentClusterState, ActionListener listener) { + void performDuringNoSnapshot(IndexMetadata indexMetadata, ProjectMetadata currentProject, ActionListener listener) { final String shrunkenIndexSource = IndexMetadata.INDEX_RESIZE_SOURCE_NAME.get(indexMetadata.getSettings()); if (Strings.isNullOrEmpty(shrunkenIndexSource) == false) { // the current managed index is a shrunk index - if (currentClusterState.metadata().getProject().index(shrunkenIndexSource) == null) { + if (currentProject.index(shrunkenIndexSource) == null) { // if the source index does not exist, we'll skip deleting the // (managed) shrunk index as that will cause data loss String policyName = indexMetadata.getLifecyclePolicyName(); @@ -64,7 +64,7 @@ void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentCl listener.onResponse(null); return; } - getClient().admin() + getClient(currentProject.id()).admin() .indices() .delete(new DeleteIndexRequest(shrinkIndexName).masterNodeTimeout(TimeValue.MAX_VALUE), new ActionListener<>() { @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStep.java index 3be40e5a1550d..c6e16aed2447b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStep.java @@ -9,9 +9,9 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.repositories.RepositoryMissingException; @@ -33,7 +33,7 @@ public boolean isRetryable() { } @Override - void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentClusterState, ActionListener listener) { + void performDuringNoSnapshot(IndexMetadata indexMetadata, ProjectMetadata currentProject, ActionListener listener) { final String indexName = indexMetadata.getIndex().getName(); LifecycleExecutionState lifecycleState = indexMetadata.getLifecycleExecutionState(); @@ -48,7 +48,7 @@ void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentCl listener.onResponse(null); return; } - getClient().admin() + getClient(currentProject.id()).admin() .cluster() .prepareDeleteSnapshot(TimeValue.MAX_VALUE, repositoryName, snapshotName) .execute(new ActionListener<>() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java index 19630b590a365..a72541452044c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java @@ -10,8 +10,8 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.core.TimeValue; import java.util.Map; @@ -32,7 +32,7 @@ public boolean isRetryable() { } @Override - void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentClusterState, ActionListener listener) { + void performDuringNoSnapshot(IndexMetadata indexMetadata, ProjectMetadata currentProject, ActionListener listener) { String followerIndex = indexMetadata.getIndex().getName(); Map customIndexMetadata = indexMetadata.getCustomData(CCR_METADATA_KEY); if (customIndexMetadata == null) { @@ -42,7 +42,7 @@ void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentCl if (indexMetadata.getState() == IndexMetadata.State.OPEN) { CloseIndexRequest closeIndexRequest = new CloseIndexRequest(followerIndex).masterNodeTimeout(TimeValue.MAX_VALUE); - getClient().admin().indices().close(closeIndexRequest, listener.delegateFailureAndWrap((l, r) -> { + getClient(currentProject.id()).admin().indices().close(closeIndexRequest, listener.delegateFailureAndWrap((l, r) -> { if (r.isAcknowledged() == false) { throw new ElasticsearchException("close index request failed to be acknowledged"); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseIndexStep.java index ffb6885b2172a..c27818c71394e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseIndexStep.java @@ -11,8 +11,8 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.core.TimeValue; @@ -30,18 +30,20 @@ public class CloseIndexStep extends AsyncActionStep { @Override public void performAction( IndexMetadata indexMetadata, - ClusterState currentClusterState, + ProjectState currentState, ClusterStateObserver observer, ActionListener listener ) { if (indexMetadata.getState() == IndexMetadata.State.OPEN) { CloseIndexRequest request = new CloseIndexRequest(indexMetadata.getIndex().getName()).masterNodeTimeout(TimeValue.MAX_VALUE); - getClient().admin().indices().close(request, listener.delegateFailureAndWrap((l, closeIndexResponse) -> { - if (closeIndexResponse.isAcknowledged() == false) { - throw new ElasticsearchException("close index request failed to be acknowledged"); - } - l.onResponse(null); - })); + getClient(currentState.projectId()).admin() + .indices() + .close(request, listener.delegateFailureAndWrap((l, closeIndexResponse) -> { + if (closeIndexResponse.isAcknowledged() == false) { + throw new ElasticsearchException("close index request failed to be acknowledged"); + } + l.onResponse(null); + })); } else { listener.onResponse(null); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStep.java index 070bc804f3279..7a85ead2d1472 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStep.java @@ -12,9 +12,10 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; +import org.elasticsearch.cluster.metadata.ProjectId; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.snapshots.SnapshotInfo; @@ -49,8 +50,8 @@ public boolean isRetryable() { } @Override - void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentClusterState, ActionListener listener) { - createSnapshot(indexMetadata, new ActionListener<>() { + void performDuringNoSnapshot(IndexMetadata indexMetadata, ProjectMetadata currentProject, ActionListener listener) { + createSnapshot(currentProject.id(), indexMetadata, new ActionListener<>() { @Override public void onResponse(Boolean complete) { // based on the result of action we'll decide what the next step will be @@ -77,7 +78,7 @@ public void onFailure(Exception e) { }); } - void createSnapshot(IndexMetadata indexMetadata, ActionListener listener) { + void createSnapshot(ProjectId projectId, IndexMetadata indexMetadata, ActionListener listener) { final String indexName = indexMetadata.getIndex().getName(); final LifecycleExecutionState lifecycleState = indexMetadata.getLifecycleExecutionState(); @@ -107,7 +108,7 @@ void createSnapshot(IndexMetadata indexMetadata, ActionListener listene request.waitForCompletion(true); request.includeGlobalState(false); - getClient().admin().cluster().createSnapshot(request, listener.map(response -> { + getClient(projectId).admin().cluster().createSnapshot(request, listener.map(response -> { logger.debug( "create snapshot response for policy [{}] and index [{}] is: {}", policyName, diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java index c9c83a63ec9db..a6cedfceb1fbe 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java @@ -13,10 +13,10 @@ import org.elasticsearch.action.datastreams.DeleteDataStreamAction; import org.elasticsearch.action.support.master.MasterNodeRequest; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.DataStream; import org.elasticsearch.cluster.metadata.IndexAbstraction; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; @@ -33,10 +33,10 @@ public DeleteStep(StepKey key, StepKey nextStepKey, Client client) { } @Override - public void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentState, ActionListener listener) { + public void performDuringNoSnapshot(IndexMetadata indexMetadata, ProjectMetadata currentProject, ActionListener listener) { String policyName = indexMetadata.getLifecyclePolicyName(); String indexName = indexMetadata.getIndex().getName(); - IndexAbstraction indexAbstraction = currentState.metadata().getProject().getIndicesLookup().get(indexName); + IndexAbstraction indexAbstraction = currentProject.getIndicesLookup().get(indexName); assert indexAbstraction != null : "invalid cluster metadata. index [" + indexName + "] was not found"; DataStream dataStream = indexAbstraction.getParentDataStream(); @@ -56,7 +56,7 @@ public void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState cu MasterNodeRequest.INFINITE_MASTER_NODE_TIMEOUT, dataStream.getName() ); - getClient().execute( + getClient(currentProject.id()).execute( DeleteDataStreamAction.INSTANCE, deleteReq, listener.delegateFailureAndWrap((l, response) -> l.onResponse(null)) @@ -78,7 +78,7 @@ public void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState cu } } - getClient().admin() + getClient(currentProject.id()).admin() .indices() .delete( new DeleteIndexRequest(indexName).masterNodeTimeout(TimeValue.MAX_VALUE), diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DownsampleStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DownsampleStep.java index 2056c41c0152f..78921a4e045df 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DownsampleStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DownsampleStep.java @@ -12,10 +12,11 @@ import org.elasticsearch.action.downsample.DownsampleAction; import org.elasticsearch.action.downsample.DownsampleConfig; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; +import org.elasticsearch.cluster.metadata.ProjectId; import org.elasticsearch.core.TimeValue; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; @@ -54,7 +55,7 @@ public boolean isRetryable() { @Override public void performAction( IndexMetadata indexMetadata, - ClusterState currentState, + ProjectState currentState, ClusterStateObserver observer, ActionListener listener ) { @@ -66,7 +67,7 @@ public void performAction( final String policyName = indexMetadata.getLifecyclePolicyName(); final String indexName = indexMetadata.getIndex().getName(); final String downsampleIndexName = lifecycleState.downsampleIndexName(); - IndexMetadata downsampleIndexMetadata = currentState.metadata().getProject().index(downsampleIndexName); + IndexMetadata downsampleIndexMetadata = currentState.metadata().index(downsampleIndexName); if (downsampleIndexMetadata != null) { IndexMetadata.DownsampleTaskStatus downsampleIndexStatus = IndexMetadata.INDEX_DOWNSAMPLE_STATUS.get( downsampleIndexMetadata.getSettings() @@ -85,10 +86,15 @@ public void performAction( return; } } - performDownsampleIndex(indexName, downsampleIndexName, listener.delegateFailureAndWrap((l, r) -> l.onResponse(r))); + performDownsampleIndex( + currentState.projectId(), + indexName, + downsampleIndexName, + listener.delegateFailureAndWrap((l, r) -> l.onResponse(r)) + ); } - void performDownsampleIndex(String indexName, String downsampleIndexName, ActionListener listener) { + void performDownsampleIndex(ProjectId projectId, String indexName, String downsampleIndexName, ActionListener listener) { DownsampleConfig config = new DownsampleConfig(fixedInterval); DownsampleAction.Request request = new DownsampleAction.Request( TimeValue.MAX_VALUE, @@ -98,7 +104,11 @@ void performDownsampleIndex(String indexName, String downsampleIndexName, Action config ); // Currently, DownsampleAction always acknowledges action was complete when no exceptions are thrown. - getClient().execute(DownsampleAction.INSTANCE, request, listener.delegateFailureAndWrap((l, response) -> l.onResponse(null))); + getClient(projectId).execute( + DownsampleAction.INSTANCE, + request, + listener.delegateFailureAndWrap((l, response) -> l.onResponse(null)) + ); } public DateHistogramInterval getFixedInterval() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ForceMergeStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ForceMergeStep.java index 741fff63f61f5..0fc0ac1f91f6a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ForceMergeStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ForceMergeStep.java @@ -13,8 +13,8 @@ import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest; import org.elasticsearch.action.support.DefaultShardOperationFailedException; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.Strings; @@ -47,14 +47,14 @@ public int getMaxNumSegments() { @Override public void performAction( IndexMetadata indexMetadata, - ClusterState currentState, + ProjectState currentState, ClusterStateObserver observer, ActionListener listener ) { String indexName = indexMetadata.getIndex().getName(); ForceMergeRequest request = new ForceMergeRequest(indexName); request.maxNumSegments(maxNumSegments); - getClient().admin().indices().forceMerge(request, listener.delegateFailureAndWrap((l, response) -> { + getClient(currentState.projectId()).admin().indices().forceMerge(request, listener.delegateFailureAndWrap((l, response) -> { if (response.getFailedShards() == 0) { l.onResponse(null); } else { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStep.java index 25c006e483c87..58ac74a420b9f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStep.java @@ -11,9 +11,9 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider; import org.elasticsearch.common.Strings; @@ -88,7 +88,7 @@ public int getReplicas() { } @Override - void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentClusterState, ActionListener listener) { + void performDuringNoSnapshot(IndexMetadata indexMetadata, ProjectMetadata currentProject, ActionListener listener) { String indexName = indexMetadata.getIndex().getName(); LifecycleExecutionState lifecycleState = indexMetadata.getLifecycleExecutionState(); @@ -121,7 +121,7 @@ void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentCl } String mountedIndexName = restoredIndexPrefix + indexName; - if (currentClusterState.metadata().getProject().index(mountedIndexName) != null) { + if (currentProject.index(mountedIndexName) != null) { logger.debug( "mounted index [{}] for policy [{}] and index [{}] already exists. will not attempt to mount the index again", mountedIndexName, @@ -183,7 +183,7 @@ void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentCl false, storageType ); - getClient().execute( + getClient(currentProject.id()).execute( MountSearchableSnapshotAction.INSTANCE, mountSearchableSnapshotRequest, listener.delegateFailureAndWrap((l, response) -> { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenIndexStep.java index 6b033c8cebd58..02061f3bfc376 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenIndexStep.java @@ -11,8 +11,8 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.core.TimeValue; @@ -31,13 +31,13 @@ final class OpenIndexStep extends AsyncActionStep { @Override public void performAction( IndexMetadata indexMetadata, - ClusterState currentClusterState, + ProjectState currentState, ClusterStateObserver observer, ActionListener listener ) { if (indexMetadata.getState() == IndexMetadata.State.CLOSE) { OpenIndexRequest request = new OpenIndexRequest(indexMetadata.getIndex().getName()).masterNodeTimeout(TimeValue.MAX_VALUE); - getClient().admin().indices().open(request, listener.delegateFailureAndWrap((l, openIndexResponse) -> { + getClient(currentState.projectId()).admin().indices().open(request, listener.delegateFailureAndWrap((l, openIndexResponse) -> { if (openIndexResponse.isAcknowledged() == false) { throw new ElasticsearchException("open index request failed to be acknowledged"); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStep.java index 172e595be53d4..9d40fe3f6d9ab 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStep.java @@ -9,7 +9,7 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.core.TimeValue; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.xpack.core.ccr.action.PauseFollowAction; @@ -31,10 +31,8 @@ public boolean isRetryable() { } @Override - void innerPerformAction(String followerIndex, ClusterState currentClusterState, ActionListener listener) { - PersistentTasksCustomMetadata persistentTasksMetadata = currentClusterState.metadata() - .getProject() - .custom(PersistentTasksCustomMetadata.TYPE); + void innerPerformAction(String followerIndex, ProjectState currentState, ActionListener listener) { + PersistentTasksCustomMetadata persistentTasksMetadata = currentState.metadata().custom(PersistentTasksCustomMetadata.TYPE); if (persistentTasksMetadata == null) { listener.onResponse(null); return; @@ -55,7 +53,7 @@ void innerPerformAction(String followerIndex, ClusterState currentClusterState, } PauseFollowAction.Request request = new PauseFollowAction.Request(TimeValue.MAX_VALUE, followerIndex); - getClient().execute(PauseFollowAction.INSTANCE, request, listener.delegateFailureAndWrap((l, r) -> { + getClient(currentState.projectId()).execute(PauseFollowAction.INSTANCE, request, listener.delegateFailureAndWrap((l, r) -> { if (r.isAcknowledged() == false) { throw new ElasticsearchException("pause follow request failed to be acknowledged"); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReadOnlyStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReadOnlyStep.java index 2f142d832fc3e..916472ee53c4c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReadOnlyStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReadOnlyStep.java @@ -11,8 +11,8 @@ import org.elasticsearch.action.admin.indices.readonly.AddIndexBlockRequest; import org.elasticsearch.action.admin.indices.readonly.TransportAddIndexBlockAction; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.core.TimeValue; @@ -37,11 +37,11 @@ public ReadOnlyStep(StepKey key, StepKey nextStepKey, Client client, boolean mar @Override public void performAction( IndexMetadata indexMetadata, - ClusterState currentState, + ProjectState currentState, ClusterStateObserver observer, ActionListener listener ) { - getClient().admin() + getClient(currentState.projectId()).admin() .indices() .execute( TransportAddIndexBlockAction.TYPE, diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverStep.java index 1aa0fe54be7d8..2546202960c2b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverStep.java @@ -13,8 +13,8 @@ import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.action.support.IndexComponentSelector; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.DataStream; import org.elasticsearch.cluster.metadata.IndexAbstraction; import org.elasticsearch.cluster.metadata.IndexMetadata; @@ -44,7 +44,7 @@ public boolean isRetryable() { @Override public void performAction( IndexMetadata indexMetadata, - ClusterState currentClusterState, + ProjectState currentState, ClusterStateObserver observer, ActionListener listener ) { @@ -55,7 +55,7 @@ public void performAction( listener.onResponse(null); return; } - IndexAbstraction indexAbstraction = currentClusterState.metadata().getProject().getIndicesLookup().get(indexName); + IndexAbstraction indexAbstraction = currentState.metadata().getIndicesLookup().get(indexName); assert indexAbstraction != null : "expected the index " + indexName + " to exist in the lookup but it didn't"; final String rolloverTarget; final boolean targetFailureStore; @@ -128,14 +128,16 @@ public void performAction( // We don't wait for active shards when we perform the rollover because the // {@link org.elasticsearch.xpack.core.ilm.WaitForActiveShardsStep} step will do so rolloverRequest.setWaitForActiveShards(ActiveShardCount.NONE); - getClient().admin().indices().rolloverIndex(rolloverRequest, listener.delegateFailureAndWrap((l, response) -> { - assert response.isRolledOver() : "the only way this rollover call should fail is with an exception"; - if (response.isRolledOver()) { - l.onResponse(null); - } else { - l.onFailure(new IllegalStateException("unexepected exception on unconditional rollover")); - } - })); + getClient(currentState.projectId()).admin() + .indices() + .rolloverIndex(rolloverRequest, listener.delegateFailureAndWrap((l, response) -> { + assert response.isRolledOver() : "the only way this rollover call should fail is with an exception"; + if (response.isRolledOver()) { + l.onResponse(null); + } else { + l.onFailure(new IllegalStateException("unexepected exception on unconditional rollover")); + } + })); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java index 379888493c894..68f271180e480 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java @@ -12,8 +12,8 @@ import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; import org.elasticsearch.client.internal.Client; import org.elasticsearch.client.internal.transport.NoNodeAvailableException; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.routing.RoutingNode; import org.elasticsearch.cluster.routing.ShardRouting; @@ -60,7 +60,7 @@ public boolean isRetryable() { @Override public void performAction( IndexMetadata indexMetadata, - ClusterState clusterState, + ProjectState currentState, ClusterStateObserver observer, ActionListener listener ) { @@ -71,7 +71,7 @@ public void performAction( AllocationDeciders allocationDeciders = new AllocationDeciders( List.of( new FilterAllocationDecider( - clusterState.getMetadata().settings(), + currentState.cluster().getMetadata().settings(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS) ), DataTierAllocationDecider.INSTANCE, @@ -81,10 +81,10 @@ public void performAction( new NodeReplacementAllocationDecider() ) ); - RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, clusterState, null, null, System.nanoTime()); + RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, currentState.cluster(), null, null, System.nanoTime()); List validNodeIds = new ArrayList<>(); String indexName = indexMetadata.getIndex().getName(); - final Map> routingsByShardId = clusterState.getRoutingTable() + final Map> routingsByShardId = currentState.routingTable() .allShards(indexName) .stream() .collect(Collectors.groupingBy(ShardRouting::shardId)); @@ -113,7 +113,7 @@ public void performAction( .build(); UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indexName).masterNodeTimeout(TimeValue.MAX_VALUE) .settings(settings); - getClient().admin() + getClient(currentState.projectId()).admin() .indices() .updateSettings(updateSettingsRequest, listener.delegateFailureAndWrap((l, response) -> l.onResponse(null))); } else { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkAction.java index 4988ac33b60d4..e991d46570766 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkAction.java @@ -187,7 +187,7 @@ public List toSteps(Client client, String phase, Step.StepKey nextStepKey) preShrinkBranchingKey, checkNotWriteIndex, lastOrNextStep, - (indexMetadata, listener) -> { + (projectId, indexMetadata, listener) -> { if (indexMetadata.getSettings().get(LifecycleSettings.SNAPSHOT_INDEX_NAME) != null) { logger.warn( "[{}] action is configured for index [{}] in policy [{}] which is mounted as searchable snapshot. " @@ -200,7 +200,8 @@ public List toSteps(Client client, String phase, Step.StepKey nextStepKey) return; } String indexName = indexMetadata.getIndex().getName(); - client.admin() + client.projectClient(projectId) + .admin() .indices() .prepareStats(indexName) .clear() diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStep.java index ecccd72e1f4d1..e82d2e258c1fc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStep.java @@ -8,9 +8,9 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import static org.elasticsearch.xpack.core.ilm.ShrinkIndexNameSupplier.getShrinkIndexName; import static org.elasticsearch.xpack.core.ilm.SwapAliasesAndDeleteSourceIndexStep.deleteSourceIndexAndTransferAliases; @@ -32,13 +32,13 @@ public boolean isRetryable() { } @Override - public void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentState, ActionListener listener) { + public void performDuringNoSnapshot(IndexMetadata indexMetadata, ProjectMetadata currentProject, ActionListener listener) { // get source index String indexName = indexMetadata.getIndex().getName(); // get target shrink index LifecycleExecutionState lifecycleState = indexMetadata.getLifecycleExecutionState(); String targetIndexName = getShrinkIndexName(indexName, lifecycleState); - deleteSourceIndexAndTransferAliases(getClient(), indexMetadata, targetIndexName, listener, true); + deleteSourceIndexAndTransferAliases(getClient(currentProject.id()), indexMetadata, targetIndexName, listener, true); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java index 94893d5e12576..d8bb60d5ee5e5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java @@ -11,8 +11,8 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; import org.elasticsearch.common.settings.Settings; @@ -55,7 +55,7 @@ public ByteSizeValue getMaxPrimaryShardSize() { @Override public void performAction( IndexMetadata indexMetadata, - ClusterState currentState, + ProjectState currentState, ClusterStateObserver observer, ActionListener listener ) { @@ -65,7 +65,7 @@ public void performAction( } String shrunkenIndexName = getShrinkIndexName(indexMetadata.getIndex().getName(), lifecycleState); - if (currentState.metadata().getProject().index(shrunkenIndexName) != null) { + if (currentState.metadata().index(shrunkenIndexName) != null) { logger.warn( "skipping [{}] step for index [{}] as part of policy [{}] as the shrunk index [{}] already exists", ShrinkStep.NAME, @@ -101,7 +101,9 @@ public void performAction( // Hard coding this to true as the resize request was executed and the corresponding cluster change was committed, so the // eventual retry will not be able to succeed anymore (shrunk index was created already) // The next step in the ShrinkAction will wait for the shrunk index to be created and for the shards to be allocated. - getClient().admin().indices().resizeIndex(resizeRequest, listener.delegateFailureAndWrap((l, response) -> l.onResponse(null))); + getClient(currentState.projectId()).admin() + .indices() + .resizeIndex(resizeRequest, listener.delegateFailureAndWrap((l, response) -> l.onResponse(null))); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStep.java index eb04af9ebfd79..ff0e6ac4932ab 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStep.java @@ -11,8 +11,8 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; import org.elasticsearch.common.Strings; @@ -73,13 +73,13 @@ boolean getCreateSourceIndexAlias() { @Override public void performAction( IndexMetadata indexMetadata, - ClusterState currentClusterState, + ProjectState currentState, ClusterStateObserver observer, ActionListener listener ) { String originalIndex = indexMetadata.getIndex().getName(); final String targetIndexName = targetIndexNameSupplier.apply(originalIndex, indexMetadata.getLifecycleExecutionState()); - IndexMetadata targetIndexMetadata = currentClusterState.metadata().getProject().index(targetIndexName); + IndexMetadata targetIndexMetadata = currentState.metadata().index(targetIndexName); if (targetIndexMetadata == null) { String policyName = indexMetadata.getLifecyclePolicyName(); @@ -94,7 +94,13 @@ public void performAction( return; } - deleteSourceIndexAndTransferAliases(getClient(), indexMetadata, targetIndexName, listener, createSourceIndexAlias); + deleteSourceIndexAndTransferAliases( + getClient(currentState.projectId()), + indexMetadata, + targetIndexName, + listener, + createSourceIndexAlias + ); } /** diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStep.java index b896196185b37..c623c64586178 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStep.java @@ -11,7 +11,7 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.ccr.action.UnfollowAction; @@ -32,9 +32,9 @@ public boolean isRetryable() { } @Override - void innerPerformAction(String followerIndex, ClusterState currentClusterState, ActionListener listener) { + void innerPerformAction(String followerIndex, ProjectState currentState, ActionListener listener) { final var request = new UnfollowAction.Request(TimeValue.MAX_VALUE, TimeValue.MAX_VALUE, followerIndex); - getClient().execute(UnfollowAction.INSTANCE, request, ActionListener.wrap(r -> { + getClient(currentState.projectId()).execute(UnfollowAction.INSTANCE, request, ActionListener.wrap(r -> { if (r.isAcknowledged() == false) { throw new ElasticsearchException("unfollow request failed to be acknowledged"); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStep.java index 5d5b04cf78815..cde9c83465cca 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStep.java @@ -9,8 +9,8 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; @@ -38,14 +38,16 @@ public boolean isRetryable() { @Override public void performAction( IndexMetadata indexMetadata, - ClusterState currentState, + ProjectState currentState, ClusterStateObserver observer, ActionListener listener ) { UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indexMetadata.getIndex().getName()).masterNodeTimeout( TimeValue.MAX_VALUE ).settings(settings); - getClient().admin().indices().updateSettings(updateSettingsRequest, listener.delegateFailureAndWrap((l, r) -> l.onResponse(null))); + getClient(currentState.projectId()).admin() + .indices() + .updateSettings(updateSettingsRequest, listener.delegateFailureAndWrap((l, r) -> l.onResponse(null))); } public Settings getSettings() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepTestCase.java index 74e9202eae873..971751b0716fd 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepTestCase.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepTestCase.java @@ -10,9 +10,8 @@ import org.elasticsearch.client.internal.AdminClient; import org.elasticsearch.client.internal.Client; import org.elasticsearch.client.internal.IndicesAdminClient; -import org.elasticsearch.cluster.ClusterName; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.ESTestCase; @@ -30,10 +29,6 @@ public abstract class AbstractStepTestCase extends ESTestCase { protected AdminClient adminClient; protected IndicesAdminClient indicesClient; - public static ClusterState emptyClusterState() { - return ClusterState.builder(ClusterName.DEFAULT).build(); - } - @Before public void setupClient() { client = Mockito.mock(Client.class); @@ -78,11 +73,11 @@ public void testStepNameNotError() { protected void performActionAndWait( AsyncActionStep step, IndexMetadata indexMetadata, - ClusterState currentClusterState, + ProjectState currentState, ClusterStateObserver observer ) throws Exception { final var future = new PlainActionFuture(); - step.performAction(indexMetadata, currentClusterState, observer, future); + step.performAction(indexMetadata, currentState, observer, future); try { future.get(SAFE_AWAIT_TIMEOUT.millis(), TimeUnit.MILLISECONDS); } catch (ExecutionException e) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AsyncBranchingStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AsyncBranchingStepTests.java index 1cca9fdfde3c2..b100d76bba65e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AsyncBranchingStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AsyncBranchingStepTests.java @@ -7,16 +7,16 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.action.ActionListener; -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.TriConsumer; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import java.util.function.BiConsumer; import static org.hamcrest.Matchers.equalTo; @@ -24,28 +24,28 @@ public class AsyncBranchingStepTests extends AbstractStepTestCase l.onResponse(true), client); + AsyncBranchingStep step = new AsyncBranchingStep(stepKey, nextStepKey, nextSkipKey, (p, i, l) -> l.onResponse(true), client); expectThrows(IllegalStateException.class, step::getNextStepKey); CountDownLatch latch = new CountDownLatch(1); - step.performAction(state.metadata().getProject().index(indexName), state, null, new Listener(latch)); + step.performAction(indexMetadata, state, null, new Listener(latch)); assertTrue(latch.await(5, TimeUnit.SECONDS)); assertThat(step.getNextStepKey(), equalTo(step.getNextStepKeyOnTrue())); } { - AsyncBranchingStep step = new AsyncBranchingStep(stepKey, nextStepKey, nextSkipKey, (i, l) -> l.onResponse(false), client); + AsyncBranchingStep step = new AsyncBranchingStep(stepKey, nextStepKey, nextSkipKey, (p, i, l) -> l.onResponse(false), client); expectThrows(IllegalStateException.class, step::getNextStepKey); CountDownLatch latch = new CountDownLatch(1); - step.performAction(state.metadata().getProject().index(indexName), state, null, new Listener(latch)); + step.performAction(indexMetadata, state, null, new Listener(latch)); assertTrue(latch.await(5, TimeUnit.SECONDS)); assertThat(step.getNextStepKey(), equalTo(step.getNextStepKeyOnFalse())); } @@ -56,7 +56,7 @@ public AsyncBranchingStep createRandomInstance() { StepKey stepKey = new StepKey(randomAlphaOfLength(5), randomAlphaOfLength(5), BranchingStep.NAME); StepKey nextStepKey = new StepKey(randomAlphaOfLength(6), randomAlphaOfLength(6), BranchingStep.NAME); StepKey nextSkipKey = new StepKey(randomAlphaOfLength(7), randomAlphaOfLength(7), BranchingStep.NAME); - return new AsyncBranchingStep(stepKey, nextStepKey, nextSkipKey, (i, l) -> l.onResponse(randomBoolean()), client); + return new AsyncBranchingStep(stepKey, nextStepKey, nextSkipKey, (p, i, l) -> l.onResponse(randomBoolean()), client); } @Override @@ -64,7 +64,7 @@ public AsyncBranchingStep mutateInstance(AsyncBranchingStep instance) { StepKey key = instance.getKey(); StepKey nextStepKey = instance.getNextStepKeyOnFalse(); StepKey nextSkipStepKey = instance.getNextStepKeyOnTrue(); - BiConsumer> asyncPredicate = instance.getAsyncPredicate(); + TriConsumer> asyncPredicate = instance.getAsyncPredicate(); switch (between(0, 2)) { case 0 -> key = new StepKey(key.phase(), key.action(), key.name() + randomAlphaOfLength(5)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStepTests.java index be6338a566669..fa175baaf30b1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStepTests.java @@ -12,10 +12,11 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.TransportDeleteIndexAction; -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.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; +import org.elasticsearch.cluster.project.TestProjectResolvers; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.test.client.NoOpClient; import org.elasticsearch.threadpool.ThreadPool; @@ -38,7 +39,7 @@ public CleanupShrinkIndexStep createRandomInstance() { @Override protected CleanupShrinkIndexStep copyInstance(CleanupShrinkIndexStep instance) { - return new CleanupShrinkIndexStep(instance.getKey(), instance.getNextStepKey(), instance.getClient()); + return new CleanupShrinkIndexStep(instance.getKey(), instance.getNextStepKey(), instance.getClientWithoutProject()); } @Override @@ -50,7 +51,7 @@ public CleanupShrinkIndexStep mutateInstance(CleanupShrinkIndexStep instance) { case 1 -> nextKey = new StepKey(nextKey.phase(), nextKey.action(), nextKey.name() + randomAlphaOfLength(5)); default -> throw new AssertionError("Illegal randomisation branch"); } - return new CleanupShrinkIndexStep(key, nextKey, instance.getClient()); + return new CleanupShrinkIndexStep(key, nextKey, instance.getClientWithoutProject()); } public void testPerformActionDoesntFailIfShrinkingIndexNameIsMissing() { @@ -64,12 +65,10 @@ public void testPerformActionDoesntFailIfShrinkingIndexNameIsMissing() { IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); CleanupShrinkIndexStep cleanupShrinkIndexStep = createRandomInstance(); - cleanupShrinkIndexStep.performAction(indexMetadata, clusterState, null, new ActionListener<>() { + cleanupShrinkIndexStep.performAction(indexMetadata, state, null, new ActionListener<>() { @Override public void onResponse(Void unused) {} @@ -97,14 +96,12 @@ public void testPerformAction() { .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); try (var threadPool = createThreadPool()) { final var client = getDeleteIndexRequestAssertingClient(threadPool, shrinkIndexName); CleanupShrinkIndexStep step = new CleanupShrinkIndexStep(randomStepKey(), randomStepKey(), client); - step.performAction(indexMetadata, clusterState, null, ActionListener.noop()); + step.performAction(indexMetadata, state, null, ActionListener.noop()); } } @@ -124,19 +121,17 @@ public void testDeleteSkippedIfManagedIndexIsShrunkAndSourceDoesntExist() { .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata shrunkIndexMetadata = shrunkIndexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(shrunkIndexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(shrunkIndexMetadata, true)); try (var threadPool = createThreadPool()) { final var client = getFailingIfCalledClient(threadPool); CleanupShrinkIndexStep step = new CleanupShrinkIndexStep(randomStepKey(), randomStepKey(), client); - step.performAction(shrunkIndexMetadata, clusterState, null, ActionListener.noop()); + step.performAction(shrunkIndexMetadata, state, null, ActionListener.noop()); } } private NoOpClient getDeleteIndexRequestAssertingClient(ThreadPool threadPool, String shrinkIndexName) { - return new NoOpClient(threadPool) { + return new NoOpClient(threadPool, TestProjectResolvers.usingRequestHeader(threadPool.getThreadContext())) { @Override protected void doExecute( ActionType action, @@ -151,7 +146,7 @@ protected void } private NoOpClient getFailingIfCalledClient(ThreadPool threadPool) { - return new NoOpClient(threadPool) { + return new NoOpClient(threadPool, TestProjectResolvers.usingRequestHeader(threadPool.getThreadContext())) { @Override protected void doExecute( ActionType action, diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStepTests.java index 922826032a224..9b2d5dfb22b7f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStepTests.java @@ -12,10 +12,10 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.admin.cluster.snapshots.delete.DeleteSnapshotRequest; import org.elasticsearch.action.admin.cluster.snapshots.delete.TransportDeleteSnapshotAction; -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.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.test.client.NoOpClient; import org.elasticsearch.threadpool.ThreadPool; @@ -37,7 +37,7 @@ public CleanupSnapshotStep createRandomInstance() { @Override protected CleanupSnapshotStep copyInstance(CleanupSnapshotStep instance) { - return new CleanupSnapshotStep(instance.getKey(), instance.getNextStepKey(), instance.getClient()); + return new CleanupSnapshotStep(instance.getKey(), instance.getNextStepKey(), instance.getClientWithoutProject()); } @Override @@ -49,7 +49,7 @@ public CleanupSnapshotStep mutateInstance(CleanupSnapshotStep instance) { case 1 -> nextKey = new StepKey(nextKey.phase(), nextKey.action(), nextKey.name() + randomAlphaOfLength(5)); default -> throw new AssertionError("Illegal randomisation branch"); } - return new CleanupSnapshotStep(key, nextKey, instance.getClient()); + return new CleanupSnapshotStep(key, nextKey, instance.getClientWithoutProject()); } public void testPerformActionDoesntFailIfSnapshotInfoIsMissing() throws Exception { @@ -62,13 +62,9 @@ public void testPerformActionDoesntFailIfSnapshotInfoIsMissing() throws Exceptio .numberOfShards(randomIntBetween(1, 5)) .numberOfReplicas(randomIntBetween(0, 5)) .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); - performActionAndWait( - createRandomInstance(), - indexMetadata, - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(), - null - ); + performActionAndWait(createRandomInstance(), indexMetadata, state, null); } { @@ -80,13 +76,9 @@ public void testPerformActionDoesntFailIfSnapshotInfoIsMissing() throws Exceptio indexMetadataBuilder.putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom); IndexMetadata indexMetadata = indexMetadataBuilder.build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); - performActionAndWait( - createRandomInstance(), - indexMetadata, - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(), - null - ); + performActionAndWait(createRandomInstance(), indexMetadata, state, null); } } @@ -103,14 +95,12 @@ public void testPerformAction() { .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); try (var threadPool = createThreadPool()) { final var client = getDeleteSnapshotRequestAssertingClient(threadPool, snapshotName); CleanupSnapshotStep step = new CleanupSnapshotStep(randomStepKey(), randomStepKey(), client); - step.performAction(indexMetadata, clusterState, null, ActionListener.noop()); + step.performAction(indexMetadata, state, null, ActionListener.noop()); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java index ef7325be0a496..820c01266ad3a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java @@ -44,7 +44,7 @@ public void testCloseFollowingIndex() throws Exception { }).when(indicesClient).close(Mockito.any(), Mockito.any()); CloseFollowerIndexStep step = new CloseFollowerIndexStep(randomStepKey(), randomStepKey(), client); - performActionAndWait(step, indexMetadata, emptyClusterState(), null); + performActionAndWait(step, indexMetadata, projectStateWithEmptyProject(), null); } public void testRequestNotAcknowledged() { @@ -60,7 +60,7 @@ public void testRequestNotAcknowledged() { }).when(indicesClient).close(Mockito.any(), Mockito.any()); CloseFollowerIndexStep step = new CloseFollowerIndexStep(randomStepKey(), randomStepKey(), client); - Exception e = expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, emptyClusterState(), null)); + Exception e = expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, projectStateWithEmptyProject(), null)); assertThat(e.getMessage(), is("close index request failed to be acknowledged")); } @@ -78,7 +78,10 @@ public void testCloseFollowingIndexFailed() { }).when(indicesClient).close(Mockito.any(), Mockito.any()); CloseFollowerIndexStep step = new CloseFollowerIndexStep(randomStepKey(), randomStepKey(), client); - assertSame(error, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, emptyClusterState(), null))); + assertSame( + error, + expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, projectStateWithEmptyProject(), null)) + ); Mockito.verify(indicesClient).close(Mockito.any(), Mockito.any()); Mockito.verifyNoMoreInteractions(indicesClient); } @@ -92,7 +95,7 @@ public void testCloseFollowerIndexIsNoopForAlreadyClosedIndex() throws Exception .numberOfReplicas(0) .build(); CloseFollowerIndexStep step = new CloseFollowerIndexStep(randomStepKey(), randomStepKey(), client); - performActionAndWait(step, indexMetadata, emptyClusterState(), null); + performActionAndWait(step, indexMetadata, projectStateWithEmptyProject(), null); Mockito.verifyNoMoreInteractions(client); } @@ -114,11 +117,11 @@ protected CloseFollowerIndexStep mutateInstance(CloseFollowerIndexStep instance) nextKey = new Step.StepKey(nextKey.phase(), nextKey.action(), nextKey.name() + randomAlphaOfLength(5)); } - return new CloseFollowerIndexStep(key, nextKey, instance.getClient()); + return new CloseFollowerIndexStep(key, nextKey, instance.getClientWithoutProject()); } @Override protected CloseFollowerIndexStep copyInstance(CloseFollowerIndexStep instance) { - return new CloseFollowerIndexStep(instance.getKey(), instance.getNextStepKey(), instance.getClient()); + return new CloseFollowerIndexStep(instance.getKey(), instance.getNextStepKey(), instance.getClientWithoutProject()); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseIndexStepTests.java index b546aeaa20687..1cc979c73a03f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseIndexStepTests.java @@ -11,12 +11,8 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; import org.elasticsearch.action.admin.indices.close.CloseIndexResponse; -import org.elasticsearch.client.internal.AdminClient; -import org.elasticsearch.client.internal.Client; -import org.elasticsearch.client.internal.IndicesAdminClient; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.index.IndexVersion; -import org.junit.Before; import org.mockito.Mockito; import org.mockito.stubbing.Answer; @@ -26,13 +22,6 @@ public class CloseIndexStepTests extends AbstractStepTestCase { - private Client client; - - @Before - public void setup() { - client = Mockito.mock(Client.class); - } - @Override protected CloseIndexStep createRandomInstance() { return new CloseIndexStep(randomStepKey(), randomStepKey(), client); @@ -54,7 +43,7 @@ protected CloseIndexStep mutateInstance(CloseIndexStep instance) { @Override protected CloseIndexStep copyInstance(CloseIndexStep instance) { - return new CloseIndexStep(instance.getKey(), instance.getNextStepKey(), instance.getClient()); + return new CloseIndexStep(instance.getKey(), instance.getNextStepKey(), instance.getClientWithoutProject()); } public void testPerformAction() { @@ -63,15 +52,10 @@ public void testPerformAction() { .numberOfShards(randomIntBetween(1, 5)) .numberOfReplicas(randomIntBetween(0, 5)) .build(); + var state = projectStateWithEmptyProject(); CloseIndexStep step = createRandomInstance(); - AdminClient adminClient = Mockito.mock(AdminClient.class); - IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); - - Mockito.when(client.admin()).thenReturn(adminClient); - Mockito.when(adminClient.indices()).thenReturn(indicesClient); - Mockito.doAnswer((Answer) invocation -> { CloseIndexRequest request = (CloseIndexRequest) invocation.getArguments()[0]; @SuppressWarnings("unchecked") @@ -83,7 +67,7 @@ public void testPerformAction() { SetOnce actionCompleted = new SetOnce<>(); - step.performAction(indexMetadata, null, null, new ActionListener<>() { + step.performAction(indexMetadata, state, null, new ActionListener<>() { @Override public void onResponse(Void complete) { @@ -97,7 +81,9 @@ public void onFailure(Exception e) { }); assertEquals(true, actionCompleted.get()); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).close(Mockito.any(), Mockito.any()); } @@ -108,14 +94,10 @@ public void testPerformActionFailure() { .numberOfShards(randomIntBetween(1, 5)) .numberOfReplicas(randomIntBetween(0, 5)) .build(); + var state = projectStateWithEmptyProject(); CloseIndexStep step = createRandomInstance(); Exception exception = new RuntimeException(); - AdminClient adminClient = Mockito.mock(AdminClient.class); - IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); - - Mockito.when(client.admin()).thenReturn(adminClient); - Mockito.when(adminClient.indices()).thenReturn(indicesClient); Mockito.doAnswer((Answer) invocation -> { CloseIndexRequest request = (CloseIndexRequest) invocation.getArguments()[0]; @@ -126,8 +108,10 @@ public void testPerformActionFailure() { return null; }).when(indicesClient).close(Mockito.any(), Mockito.any()); - assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, null, null))); - Mockito.verify(client, Mockito.only()).admin(); + assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, state, null))); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).close(Mockito.any(), Mockito.any()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStepTests.java index de0bfa0440179..a466308e75324 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStepTests.java @@ -12,10 +12,12 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest; import org.elasticsearch.action.admin.cluster.snapshots.create.TransportCreateSnapshotAction; -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.Metadata; +import org.elasticsearch.cluster.metadata.ProjectId; +import org.elasticsearch.cluster.metadata.ProjectMetadata; +import org.elasticsearch.cluster.project.TestProjectResolvers; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.snapshots.SnapshotNameAlreadyInUseException; import org.elasticsearch.test.client.NoOpClient; @@ -43,7 +45,7 @@ protected CreateSnapshotStep copyInstance(CreateSnapshotStep instance) { instance.getKey(), instance.getNextKeyOnComplete(), instance.getNextKeyOnIncomplete(), - instance.getClient() + instance.getClientWithoutProject() ); } @@ -58,7 +60,7 @@ public CreateSnapshotStep mutateInstance(CreateSnapshotStep instance) { case 2 -> nextKeyOnIncompleteResponse = randomStepKey(); default -> throw new AssertionError("Illegal randomisation branch"); } - return new CreateSnapshotStep(key, nextKeyOnCompleteResponse, nextKeyOnIncompleteResponse, instance.getClient()); + return new CreateSnapshotStep(key, nextKeyOnCompleteResponse, nextKeyOnIncompleteResponse, instance.getClientWithoutProject()); } public void testPerformActionFailure() { @@ -77,14 +79,12 @@ public void testPerformActionFailure() { IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); CreateSnapshotStep createSnapshotStep = createRandomInstance(); Exception e = expectThrows( IllegalStateException.class, - () -> performActionAndWait(createSnapshotStep, indexMetadata, clusterState, null) + () -> performActionAndWait(createSnapshotStep, indexMetadata, state, null) ); assertThat(e.getMessage(), is("snapshot name was not generated for policy [" + policyName + "] and index [" + indexName + "]")); } @@ -96,14 +96,12 @@ public void testPerformActionFailure() { .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); CreateSnapshotStep createSnapshotStep = createRandomInstance(); Exception e = expectThrows( IllegalStateException.class, - () -> performActionAndWait(createSnapshotStep, indexMetadata, clusterState, null) + () -> performActionAndWait(createSnapshotStep, indexMetadata, state, null) ); assertThat( e.getMessage(), @@ -128,14 +126,12 @@ public void testPerformAction() { .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); try (var threadPool = createThreadPool()) { final var client = getCreateSnapshotRequestAssertingClient(threadPool, repository, snapshotName, indexName); CreateSnapshotStep step = new CreateSnapshotStep(randomStepKey(), randomStepKey(), randomStepKey(), client); - step.performAction(indexMetadata, clusterState, null, ActionListener.noop()); + step.performAction(indexMetadata, state, null, ActionListener.noop()); } } @@ -155,9 +151,7 @@ public void testNextStepKey() { .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); { try (var threadPool = createThreadPool()) { final var client = new NoOpClient(threadPool); @@ -165,11 +159,11 @@ public void testNextStepKey() { StepKey nextKeyOnIncomplete = randomStepKey(); CreateSnapshotStep completeStep = new CreateSnapshotStep(randomStepKey(), nextKeyOnComplete, nextKeyOnIncomplete, client) { @Override - void createSnapshot(IndexMetadata indexMetadata, ActionListener listener) { + void createSnapshot(ProjectId projectId, IndexMetadata indexMetadata, ActionListener listener) { listener.onResponse(true); } }; - completeStep.performAction(indexMetadata, clusterState, null, ActionListener.noop()); + completeStep.performAction(indexMetadata, state, null, ActionListener.noop()); assertThat(completeStep.getNextStepKey(), is(nextKeyOnComplete)); } } @@ -186,11 +180,11 @@ void createSnapshot(IndexMetadata indexMetadata, ActionListener listene client ) { @Override - void createSnapshot(IndexMetadata indexMetadata, ActionListener listener) { + void createSnapshot(ProjectId projectId, IndexMetadata indexMetadata, ActionListener listener) { listener.onResponse(false); } }; - incompleteStep.performAction(indexMetadata, clusterState, null, ActionListener.noop()); + incompleteStep.performAction(indexMetadata, state, null, ActionListener.noop()); assertThat(incompleteStep.getNextStepKey(), is(nextKeyOnIncomplete)); } } @@ -207,11 +201,11 @@ void createSnapshot(IndexMetadata indexMetadata, ActionListener listene client ) { @Override - void createSnapshot(IndexMetadata indexMetadata, ActionListener listener) { + void createSnapshot(ProjectId projectId, IndexMetadata indexMetadata, ActionListener listener) { listener.onFailure(new SnapshotNameAlreadyInUseException(repository, snapshotName, "simulated")); } }; - doubleInvocationStep.performAction(indexMetadata, clusterState, null, ActionListener.noop()); + doubleInvocationStep.performAction(indexMetadata, state, null, ActionListener.noop()); assertThat(doubleInvocationStep.getNextStepKey(), is(nextKeyOnIncomplete)); } } @@ -223,7 +217,7 @@ private NoOpClient getCreateSnapshotRequestAssertingClient( String expectedSnapshotName, String indexName ) { - return new NoOpClient(threadPool) { + return new NoOpClient(threadPool, TestProjectResolvers.usingRequestHeader(threadPool.getThreadContext())) { @Override protected void doExecute( ActionType action, diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java index 7e9db3a4f1645..f969e7767dd96 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.datastreams.DeleteDataStreamAction; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.DataStream; import org.elasticsearch.cluster.metadata.DataStreamTestHelper; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import org.mockito.Mockito; @@ -48,12 +48,12 @@ public DeleteStep mutateInstance(DeleteStep instance) { default -> throw new AssertionError("Illegal randomisation branch"); } - return new DeleteStep(key, nextKey, instance.getClient()); + return new DeleteStep(key, nextKey, instance.getClientWithoutProject()); } @Override public DeleteStep copyInstance(DeleteStep instance) { - return new DeleteStep(instance.getKey(), instance.getNextStepKey(), instance.getClient()); + return new DeleteStep(instance.getKey(), instance.getNextStepKey(), instance.getClientWithoutProject()); } private static IndexMetadata getIndexMetadata() { @@ -83,12 +83,12 @@ public void testDeleted() throws Exception { }).when(indicesClient).delete(any(), any()); DeleteStep step = createRandomInstance(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); - performActionAndWait(step, indexMetadata, clusterState, null); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); + performActionAndWait(step, indexMetadata, state, null); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).delete(any(), any()); } @@ -109,10 +109,8 @@ public void testExceptionThrown() { }).when(indicesClient).delete(any(), any()); DeleteStep step = createRandomInstance(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); - assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, clusterState, null))); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); + assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, state, null))); } public void testPerformActionCallsFailureListenerIfIndexIsTheDataStreamWriteIndex() { @@ -168,22 +166,18 @@ public void testPerformActionCallsFailureListenerIfIndexIsTheDataStreamWriteInde List.of(index1.getIndex(), sourceIndexMetadata.getIndex()), List.of(failureIndex1.getIndex(), failureSourceIndexMetadata.getIndex()) ); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata( - Metadata.builder() - .put(index1, false) - .put(sourceIndexMetadata, false) - .put(failureIndex1, false) - .put(failureSourceIndexMetadata, false) - .put(dataStream) - .build() - ) + ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(index1, false) + .put(sourceIndexMetadata, false) + .put(failureIndex1, false) + .put(failureSourceIndexMetadata, false) + .put(dataStream) .build(); AtomicBoolean listenerCalled = new AtomicBoolean(false); final boolean useFailureStore = randomBoolean(); final IndexMetadata indexToOperateOn = useFailureStore ? failureSourceIndexMetadata : sourceIndexMetadata; - createRandomInstance().performDuringNoSnapshot(indexToOperateOn, clusterState, new ActionListener<>() { + createRandomInstance().performDuringNoSnapshot(indexToOperateOn, project, new ActionListener<>() { @Override public void onResponse(Void complete) { listenerCalled.set(true); @@ -231,9 +225,7 @@ public void testDeleteWorksIfWriteIndexIsTheOnlyIndexInDataStream() throws Excep DataStream dataStream = DataStreamTestHelper.newInstance(dataStreamName, List.of(index1.getIndex()), List.of()); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(index1, false).put(dataStream).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(index1, true).put(dataStream)); Mockito.doAnswer(invocation -> { DeleteDataStreamAction.Request request = (DeleteDataStreamAction.Request) invocation.getArguments()[1]; @@ -248,9 +240,11 @@ public void testDeleteWorksIfWriteIndexIsTheOnlyIndexInDataStream() throws Excep // Try on the normal data stream - It should delete the data stream DeleteStep step = createRandomInstance(); - performActionAndWait(step, index1, clusterState, null); + performActionAndWait(step, index1, state, null); - Mockito.verify(client, Mockito.only()).execute(any(), any(), any()); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).execute(any(), any(), any()); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.never()).indices(); Mockito.verify(indicesClient, Mockito.never()).delete(any(), any()); } @@ -297,16 +291,13 @@ public void testDeleteWorksIfWriteIndexIsTheOnlyIndexInDataStreamWithFailureStor List.of(failureIndex1.getIndex(), failureSourceIndexMetadata.getIndex()) ); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata( - Metadata.builder() - .put(index1, false) - .put(failureIndex1, false) - .put(failureSourceIndexMetadata, false) - .put(dataStreamWithFailureIndices) - .build() - ) - .build(); + ProjectState state = projectStateFromProject( + ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(index1, false) + .put(failureIndex1, false) + .put(failureSourceIndexMetadata, false) + .put(dataStreamWithFailureIndices) + ); Mockito.doAnswer(invocation -> { DeleteDataStreamAction.Request request = (DeleteDataStreamAction.Request) invocation.getArguments()[1]; @@ -321,9 +312,11 @@ public void testDeleteWorksIfWriteIndexIsTheOnlyIndexInDataStreamWithFailureStor // Again, the deletion should work since the data stream would be fully deleted anyway if the failure store were disabled. DeleteStep step = createRandomInstance(); - performActionAndWait(step, index1, clusterState, null); + performActionAndWait(step, index1, state, null); - Mockito.verify(client, Mockito.only()).execute(any(), any(), any()); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).execute(any(), any(), any()); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.never()).indices(); Mockito.verify(indicesClient, Mockito.never()).delete(any(), any()); } @@ -376,19 +369,15 @@ public void testDeletingFailureStoreWriteIndexOnDataStreamWithSingleBackingIndex List.of(failureIndex1.getIndex(), failureSourceIndexMetadata.getIndex()) ); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata( - Metadata.builder() - .put(index1, false) - .put(failureIndex1, false) - .put(failureSourceIndexMetadata, false) - .put(dataStreamWithFailureIndices) - .build() - ) + ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(index1, false) + .put(failureIndex1, false) + .put(failureSourceIndexMetadata, false) + .put(dataStreamWithFailureIndices) .build(); AtomicBoolean listenerCalled = new AtomicBoolean(false); - createRandomInstance().performDuringNoSnapshot(failureSourceIndexMetadata, clusterState, new ActionListener<>() { + createRandomInstance().performDuringNoSnapshot(failureSourceIndexMetadata, project, new ActionListener<>() { @Override public void onResponse(Void complete) { listenerCalled.set(true); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DownsampleStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DownsampleStepTests.java index 877d33b3cfda3..6dc4b3f391202 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DownsampleStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DownsampleStepTests.java @@ -9,12 +9,12 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.downsample.DownsampleAction; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.cluster.ClusterName; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.DataStream; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; -import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectId; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; @@ -71,7 +71,7 @@ public DownsampleStep mutateInstance(DownsampleStep instance) { default -> throw new AssertionError("Illegal randomisation branch"); } - return new DownsampleStep(key, nextKey, instance.getClient(), fixedInterval, timeout); + return new DownsampleStep(key, nextKey, instance.getClientWithoutProject(), fixedInterval, timeout); } @Override @@ -79,7 +79,7 @@ public DownsampleStep copyInstance(DownsampleStep instance) { return new DownsampleStep( instance.getKey(), instance.getNextStepKey(), - instance.getClient(), + instance.getClientWithoutProject(), instance.getFixedInterval(), instance.getWaitTimeout() ); @@ -118,8 +118,8 @@ public void testPerformAction() throws Exception { IndexMetadata indexMetadata = getIndexMetadata(index, lifecycleName, step); mockClientDownsampleCall(index); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, true)).build(); - performActionAndWait(step, indexMetadata, clusterState, null); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); + performActionAndWait(step, indexMetadata, state, null); } public void testPerformActionFailureInvalidExecutionState() { @@ -141,7 +141,7 @@ public void testPerformActionFailureInvalidExecutionState() { String policyName = indexMetadata.getLifecyclePolicyName(); String indexName = indexMetadata.getIndex().getName(); - step.performAction(indexMetadata, emptyClusterState(), null, new ActionListener<>() { + step.performAction(indexMetadata, projectStateWithEmptyProject(), null, new ActionListener<>() { @Override public void onResponse(Void unused) { fail("expecting a failure as the index doesn't have any downsample index name in its ILM execution state"); @@ -167,10 +167,12 @@ public void testPerformActionOnDataStream() throws Exception { mockClientDownsampleCall(backingIndexName); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(newInstance(dataStreamName, List.of(indexMetadata.getIndex()))).put(indexMetadata, true)) - .build(); - performActionAndWait(step, indexMetadata, clusterState, null); + ProjectState state = projectStateFromProject( + ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(newInstance(dataStreamName, List.of(indexMetadata.getIndex()))) + .put(indexMetadata, true) + ); + performActionAndWait(step, indexMetadata, state, null); } /** @@ -210,11 +212,11 @@ public void testPerformActionDownsampleInProgressIndexExists() { .numberOfReplicas(0) .build(); Map indices = Map.of(downsampleIndex, indexMetadata); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().indices(indices)).build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).indices(indices)); mockClientDownsampleCall(sourceIndexName); final AtomicBoolean listenerIsCalled = new AtomicBoolean(false); - step.performAction(sourceIndexMetadata, clusterState, null, new ActionListener<>() { + step.performAction(sourceIndexMetadata, state, null, new ActionListener<>() { @Override public void onResponse(Void unused) { listenerIsCalled.set(true); @@ -247,9 +249,7 @@ public void testNextStepKey() { .numberOfReplicas(randomIntBetween(0, 5)) .build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(sourceIndexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(sourceIndexMetadata, true)); { try (var threadPool = createThreadPool()) { final var client = new NoOpClient(threadPool); @@ -257,11 +257,17 @@ public void testNextStepKey() { DateHistogramInterval fixedInterval = ConfigTestHelpers.randomInterval(); TimeValue timeout = DownsampleAction.DEFAULT_WAIT_TIMEOUT; DownsampleStep completeStep = new DownsampleStep(randomStepKey(), nextKey, client, fixedInterval, timeout) { - void performDownsampleIndex(String indexName, String downsampleIndexName, ActionListener listener) { + @Override + void performDownsampleIndex( + ProjectId projectId, + String indexName, + String downsampleIndexName, + ActionListener listener + ) { listener.onResponse(null); } }; - completeStep.performAction(sourceIndexMetadata, clusterState, null, ActionListener.noop()); + completeStep.performAction(sourceIndexMetadata, state, null, ActionListener.noop()); assertThat(completeStep.getNextStepKey(), is(nextKey)); } } @@ -272,7 +278,13 @@ void performDownsampleIndex(String indexName, String downsampleIndexName, Action DateHistogramInterval fixedInterval = ConfigTestHelpers.randomInterval(); TimeValue timeout = DownsampleAction.DEFAULT_WAIT_TIMEOUT; DownsampleStep doubleInvocationStep = new DownsampleStep(randomStepKey(), nextKey, client, fixedInterval, timeout) { - void performDownsampleIndex(String indexName, String downsampleIndexName, ActionListener listener) { + @Override + void performDownsampleIndex( + ProjectId projectId, + String indexName, + String downsampleIndexName, + ActionListener listener + ) { listener.onFailure( new IllegalStateException( "failing [" @@ -290,7 +302,7 @@ void performDownsampleIndex(String indexName, String downsampleIndexName, Action ); } }; - doubleInvocationStep.performAction(sourceIndexMetadata, clusterState, null, ActionListener.noop()); + doubleInvocationStep.performAction(sourceIndexMetadata, state, null, ActionListener.noop()); assertThat(doubleInvocationStep.getNextStepKey(), is(nextKey)); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java index dd82a648f0436..458f91aa3ce75 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java @@ -12,10 +12,9 @@ import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest; import org.elasticsearch.action.support.DefaultShardOperationFailedException; import org.elasticsearch.action.support.broadcast.BroadcastResponse; -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.ProjectMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexVersion; @@ -52,12 +51,17 @@ public ForceMergeStep mutateInstance(ForceMergeStep instance) { default -> throw new AssertionError("Illegal randomisation branch"); } - return new ForceMergeStep(key, nextKey, instance.getClient(), maxNumSegments); + return new ForceMergeStep(key, nextKey, instance.getClientWithoutProject(), maxNumSegments); } @Override public ForceMergeStep copyInstance(ForceMergeStep instance) { - return new ForceMergeStep(instance.getKey(), instance.getNextStepKey(), instance.getClient(), instance.getMaxNumSegments()); + return new ForceMergeStep( + instance.getKey(), + instance.getNextStepKey(), + instance.getClientWithoutProject(), + instance.getMaxNumSegments() + ); } public void testPerformActionComplete() throws Exception { @@ -81,7 +85,8 @@ public void testPerformActionComplete() throws Exception { }).when(indicesClient).forceMerge(any(), any()); ForceMergeStep step = new ForceMergeStep(stepKey, nextStepKey, client, maxNumSegments); - performActionAndWait(step, indexMetadata, null, null); + var state = projectStateWithEmptyProject(); + performActionAndWait(step, indexMetadata, state, null); } public void testPerformActionThrowsException() { @@ -108,7 +113,8 @@ public void testPerformActionThrowsException() { }).when(indicesClient).forceMerge(any(), any()); ForceMergeStep step = new ForceMergeStep(stepKey, nextStepKey, client, maxNumSegments); - assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, null, null))); + var state = projectStateWithEmptyProject(); + assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, state, null))); } public void testForcemergeFailsOnSomeShards() { @@ -143,9 +149,7 @@ public void testForcemergeFailsOnSomeShards() { SetOnce failedStep = new SetOnce<>(); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); ForceMergeStep step = new ForceMergeStep(stepKey, nextStepKey, client, 1); step.performAction(indexMetadata, state, null, new ActionListener<>() { @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStepTests.java index d7a7dcabf9fb7..1dbd7213239fe 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStepTests.java @@ -11,10 +11,11 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionType; import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; -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.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; +import org.elasticsearch.cluster.project.TestProjectResolvers; import org.elasticsearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider; import org.elasticsearch.core.Nullable; import org.elasticsearch.index.IndexVersion; @@ -59,7 +60,7 @@ protected MountSnapshotStep copyInstance(MountSnapshotStep instance) { return new MountSnapshotStep( instance.getKey(), instance.getNextStepKey(), - instance.getClient(), + instance.getClientWithoutProject(), instance.getRestoredIndexPrefix(), instance.getStorage(), instance.getTotalShardsPerNode(), @@ -103,7 +104,15 @@ public MountSnapshotStep mutateInstance(MountSnapshotStep instance) { default: throw new AssertionError("Illegal randomisation branch"); } - return new MountSnapshotStep(key, nextKey, instance.getClient(), restoredIndexPrefix, storage, totalShardsPerNode, replicas); + return new MountSnapshotStep( + key, + nextKey, + instance.getClientWithoutProject(), + restoredIndexPrefix, + storage, + totalShardsPerNode, + replicas + ); } public void testCreateWithInvalidTotalShardsPerNode() throws Exception { @@ -135,14 +144,12 @@ public void testPerformActionFailure() { .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); MountSnapshotStep mountSnapshotStep = createRandomInstance(); Exception e = expectThrows( IllegalStateException.class, - () -> performActionAndWait(mountSnapshotStep, indexMetadata, clusterState, null) + () -> performActionAndWait(mountSnapshotStep, indexMetadata, state, null) ); assertThat( e.getMessage(), @@ -161,14 +168,12 @@ public void testPerformActionFailure() { indexMetadataBuilder.putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); MountSnapshotStep mountSnapshotStep = createRandomInstance(); Exception e = expectThrows( IllegalStateException.class, - () -> performActionAndWait(mountSnapshotStep, indexMetadata, clusterState, null) + () -> performActionAndWait(mountSnapshotStep, indexMetadata, state, null) ); assertThat(e.getMessage(), is("snapshot name was not generated for policy [" + policyName + "] and index [" + indexName + "]")); } @@ -190,9 +195,7 @@ public void testPerformAction() throws Exception { .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); try (var threadPool = createThreadPool()) { final var client = getRestoreSnapshotRequestAssertingClient( @@ -215,7 +218,7 @@ public void testPerformAction() throws Exception { null, 0 ); - performActionAndWait(step, indexMetadata, clusterState, null); + performActionAndWait(step, indexMetadata, state, null); } } @@ -235,9 +238,7 @@ public void testResponseStatusHandling() throws Exception { .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); { RestoreSnapshotResponse responseWithOKStatus = new RestoreSnapshotResponse(new RestoreInfo("test", List.of(), 1, 1)); @@ -252,7 +253,7 @@ public void testResponseStatusHandling() throws Exception { null, 0 ); - performActionAndWait(step, indexMetadata, clusterState, null); + performActionAndWait(step, indexMetadata, state, null); } } @@ -269,7 +270,7 @@ public void testResponseStatusHandling() throws Exception { null, 0 ); - performActionAndWait(step, indexMetadata, clusterState, null); + performActionAndWait(step, indexMetadata, state, null); } } } @@ -321,9 +322,7 @@ private void doTestMountWithoutSnapshotIndexNameInState(String prefix) throws Ex .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); try (var threadPool = createThreadPool()) { final var client = getRestoreSnapshotRequestAssertingClient( @@ -346,7 +345,7 @@ private void doTestMountWithoutSnapshotIndexNameInState(String prefix) throws Ex null, 0 ); - performActionAndWait(step, indexMetadata, clusterState, null); + performActionAndWait(step, indexMetadata, state, null); } } @@ -366,9 +365,7 @@ public void testIgnoreTotalShardsPerNodeInFrozenPhase() throws Exception { .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); try (var threadPool = createThreadPool()) { final var client = getRestoreSnapshotRequestAssertingClient( @@ -393,7 +390,7 @@ public void testIgnoreTotalShardsPerNodeInFrozenPhase() throws Exception { null, 0 ); - performActionAndWait(step, indexMetadata, clusterState, null); + performActionAndWait(step, indexMetadata, state, null); } } @@ -413,9 +410,7 @@ public void testDoNotIgnorePropagatedTotalShardsPerNodeInColdPhase() throws Exce .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); try (var threadPool = createThreadPool()) { final var client = getRestoreSnapshotRequestAssertingClient( @@ -438,7 +433,7 @@ public void testDoNotIgnorePropagatedTotalShardsPerNodeInColdPhase() throws Exce null, 0 ); - performActionAndWait(step, indexMetadata, clusterState, null); + performActionAndWait(step, indexMetadata, state, null); } } @@ -458,9 +453,7 @@ public void testDoNotIgnoreTotalShardsPerNodeAndReplicasIfSetInFrozenPhase() thr .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); final Integer totalShardsPerNode = randomTotalShardsPerNode(false); final int replicas = randomIntBetween(1, 5); @@ -486,7 +479,7 @@ public void testDoNotIgnoreTotalShardsPerNodeAndReplicasIfSetInFrozenPhase() thr totalShardsPerNode, replicas ); - performActionAndWait(step, indexMetadata, clusterState, null); + performActionAndWait(step, indexMetadata, state, null); } } @@ -506,9 +499,7 @@ public void testDoNotIgnoreTotalShardsPerNodeAndReplicasIfSetInCold() throws Exc .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); final Integer totalShardsPerNode = randomTotalShardsPerNode(false); final int replicas = randomIntBetween(1, 5); @@ -534,13 +525,13 @@ public void testDoNotIgnoreTotalShardsPerNodeAndReplicasIfSetInCold() throws Exc totalShardsPerNode, replicas ); - performActionAndWait(step, indexMetadata, clusterState, null); + performActionAndWait(step, indexMetadata, state, null); } } @SuppressWarnings("unchecked") private NoOpClient getClientTriggeringResponse(ThreadPool threadPool, RestoreSnapshotResponse response) { - return new NoOpClient(threadPool) { + return new NoOpClient(threadPool, TestProjectResolvers.usingRequestHeader(threadPool.getThreadContext())) { @Override protected void doExecute( ActionType action, @@ -564,7 +555,7 @@ private NoOpClient getRestoreSnapshotRequestAssertingClient( @Nullable Integer totalShardsPerNode, int replicas ) { - return new NoOpClient(threadPool) { + return new NoOpClient(threadPool, TestProjectResolvers.usingRequestHeader(threadPool.getThreadContext())) { @Override protected void doExecute( ActionType action, diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenIndexStepTests.java index 7f3d408ee3d49..08f6abf9bd667 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenIndexStepTests.java @@ -10,12 +10,8 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; -import org.elasticsearch.client.internal.AdminClient; -import org.elasticsearch.client.internal.Client; -import org.elasticsearch.client.internal.IndicesAdminClient; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.index.IndexVersion; -import org.junit.Before; import org.mockito.Mockito; import org.mockito.stubbing.Answer; @@ -23,13 +19,6 @@ public class OpenIndexStepTests extends AbstractStepTestCase { - private Client client; - - @Before - public void setup() { - client = Mockito.mock(Client.class); - } - @Override protected OpenIndexStep createRandomInstance() { return new OpenIndexStep(randomStepKey(), randomStepKey(), client); @@ -51,7 +40,7 @@ protected OpenIndexStep mutateInstance(OpenIndexStep instance) { @Override protected OpenIndexStep copyInstance(OpenIndexStep instance) { - return new OpenIndexStep(instance.getKey(), instance.getNextStepKey(), instance.getClient()); + return new OpenIndexStep(instance.getKey(), instance.getNextStepKey(), instance.getClientWithoutProject()); } public void testPerformAction() throws Exception { @@ -64,12 +53,6 @@ public void testPerformAction() throws Exception { OpenIndexStep step = createRandomInstance(); - AdminClient adminClient = Mockito.mock(AdminClient.class); - IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); - - Mockito.when(client.admin()).thenReturn(adminClient); - Mockito.when(adminClient.indices()).thenReturn(indicesClient); - Mockito.doAnswer((Answer) invocation -> { OpenIndexRequest request = (OpenIndexRequest) invocation.getArguments()[0]; @SuppressWarnings("unchecked") @@ -79,9 +62,12 @@ public void testPerformAction() throws Exception { return null; }).when(indicesClient).open(Mockito.any(), Mockito.any()); - performActionAndWait(step, indexMetadata, null, null); + var state = projectStateWithEmptyProject(); + performActionAndWait(step, indexMetadata, state, null); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).open(Mockito.any(), Mockito.any()); } @@ -96,11 +82,6 @@ public void testPerformActionFailure() { OpenIndexStep step = createRandomInstance(); Exception exception = new RuntimeException(); - AdminClient adminClient = Mockito.mock(AdminClient.class); - IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); - - Mockito.when(client.admin()).thenReturn(adminClient); - Mockito.when(adminClient.indices()).thenReturn(indicesClient); Mockito.doAnswer((Answer) invocation -> { OpenIndexRequest request = (OpenIndexRequest) invocation.getArguments()[0]; @@ -111,9 +92,12 @@ public void testPerformActionFailure() { return null; }).when(indicesClient).open(Mockito.any(), Mockito.any()); - assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, null, null))); + var state = projectStateWithEmptyProject(); + assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, state, null))); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).open(Mockito.any(), Mockito.any()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStepTests.java index efdd01b44d2fa..a00535786466c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStepTests.java @@ -8,10 +8,9 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.master.AcknowledgedResponse; -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.ProjectMetadata; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; @@ -42,7 +41,7 @@ public void testPauseFollowingIndex() throws Exception { .numberOfShards(1) .numberOfReplicas(0) .build(); - ClusterState clusterState = setupClusterStateWithFollowingIndex(indexMetadata); + ProjectState state = setupClusterStateWithFollowingIndex(indexMetadata); Mockito.doAnswer(invocation -> { PauseFollowAction.Request request = (PauseFollowAction.Request) invocation.getArguments()[1]; @@ -54,7 +53,7 @@ public void testPauseFollowingIndex() throws Exception { }).when(client).execute(Mockito.same(PauseFollowAction.INSTANCE), Mockito.any(), Mockito.any()); PauseFollowerIndexStep step = new PauseFollowerIndexStep(randomStepKey(), randomStepKey(), client); - performActionAndWait(step, indexMetadata, clusterState, null); + performActionAndWait(step, indexMetadata, state, null); } public void testRequestNotAcknowledged() { @@ -64,7 +63,7 @@ public void testRequestNotAcknowledged() { .numberOfShards(1) .numberOfReplicas(0) .build(); - ClusterState clusterState = setupClusterStateWithFollowingIndex(indexMetadata); + ProjectState state = setupClusterStateWithFollowingIndex(indexMetadata); Mockito.doAnswer(invocation -> { @SuppressWarnings("unchecked") @@ -74,7 +73,7 @@ public void testRequestNotAcknowledged() { }).when(client).execute(Mockito.same(PauseFollowAction.INSTANCE), Mockito.any(), Mockito.any()); PauseFollowerIndexStep step = new PauseFollowerIndexStep(randomStepKey(), randomStepKey(), client); - Exception e = expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, clusterState, null)); + Exception e = expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, state, null)); assertThat(e.getMessage(), is("pause follow request failed to be acknowledged")); } @@ -85,7 +84,7 @@ public void testPauseFollowingIndexFailed() { .numberOfShards(1) .numberOfReplicas(0) .build(); - ClusterState clusterState = setupClusterStateWithFollowingIndex(indexMetadata); + ProjectState state = setupClusterStateWithFollowingIndex(indexMetadata); // Mock pause follow api call: Exception error = new RuntimeException(); @@ -98,9 +97,10 @@ public void testPauseFollowingIndexFailed() { }).when(client).execute(Mockito.same(PauseFollowAction.INSTANCE), Mockito.any(), Mockito.any()); PauseFollowerIndexStep step = new PauseFollowerIndexStep(randomStepKey(), randomStepKey(), client); - assertSame(error, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, clusterState, null))); + assertSame(error, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, state, null))); Mockito.verify(client).execute(Mockito.same(PauseFollowAction.INSTANCE), Mockito.any(), Mockito.any()); + Mockito.verify(client).projectClient(state.projectId()); Mockito.verifyNoMoreInteractions(client); } @@ -113,18 +113,15 @@ public final void testNoShardFollowPersistentTasks() throws Exception { .build(); PersistentTasksCustomMetadata.Builder emptyPersistentTasks = PersistentTasksCustomMetadata.builder(); - ClusterState clusterState = ClusterState.builder(new ClusterName("_cluster")) - .metadata( - Metadata.builder() - .putCustom(PersistentTasksCustomMetadata.TYPE, emptyPersistentTasks.build()) - .put(indexMetadata, false) - .build() - ) - .build(); + ProjectState state = projectStateFromProject( + ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom(PersistentTasksCustomMetadata.TYPE, emptyPersistentTasks.build()) + .put(indexMetadata, false) + ); PauseFollowerIndexStep step = newInstance(randomStepKey(), randomStepKey()); - performActionAndWait(step, indexMetadata, clusterState, null); + performActionAndWait(step, indexMetadata, state, null); Mockito.verifyNoMoreInteractions(client); } @@ -142,17 +139,17 @@ public final void testNoShardFollowTasksForManagedIndex() throws Exception { .numberOfShards(1) .numberOfReplicas(0) .build(); - final ClusterState clusterState = ClusterState.builder(setupClusterStateWithFollowingIndex(followerIndex)) - .metadata(Metadata.builder().put(managedIndex, false).build()) - .build(); + final var initialState = setupClusterStateWithFollowingIndex(followerIndex); + final ProjectState state = initialState.updatedState(builder -> builder.put(managedIndex, false)) + .projectState(initialState.projectId()); PauseFollowerIndexStep step = newInstance(randomStepKey(), randomStepKey()); - performActionAndWait(step, managedIndex, clusterState, null); + performActionAndWait(step, managedIndex, state, null); Mockito.verifyNoMoreInteractions(client); } - private static ClusterState setupClusterStateWithFollowingIndex(IndexMetadata followerIndex) { + private static ProjectState setupClusterStateWithFollowingIndex(IndexMetadata followerIndex) { PersistentTasksCustomMetadata.Builder persistentTasks = PersistentTasksCustomMetadata.builder() .addTask( "1", @@ -176,11 +173,11 @@ private static ClusterState setupClusterStateWithFollowingIndex(IndexMetadata fo null ); - return ClusterState.builder(new ClusterName("_cluster")) - .metadata( - Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, persistentTasks.build()).put(followerIndex, false).build() - ) - .build(); + return projectStateFromProject( + ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom(PersistentTasksCustomMetadata.TYPE, persistentTasks.build()) + .put(followerIndex, false) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java index d839d6102b18a..a4e560a7a8d2d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java @@ -12,13 +12,12 @@ import org.elasticsearch.action.admin.indices.rollover.RolloverRequest; import org.elasticsearch.action.admin.indices.rollover.RolloverResponse; import org.elasticsearch.action.support.IndexComponentSelector; -import org.elasticsearch.cluster.ClusterName; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.AliasMetadata; import org.elasticsearch.cluster.metadata.DataStream; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; -import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.xpack.core.ilm.Step.StepKey; @@ -53,12 +52,12 @@ public RolloverStep mutateInstance(RolloverStep instance) { default -> throw new AssertionError("Illegal randomisation branch"); } - return new RolloverStep(key, nextKey, instance.getClient()); + return new RolloverStep(key, nextKey, instance.getClientWithoutProject()); } @Override public RolloverStep copyInstance(RolloverStep instance) { - return new RolloverStep(instance.getKey(), instance.getNextStepKey(), instance.getClient()); + return new RolloverStep(instance.getKey(), instance.getNextStepKey(), instance.getClientWithoutProject()); } private IndexMetadata getIndexMetadata(String alias) { @@ -91,10 +90,12 @@ public void testPerformAction() throws Exception { mockClientRolloverCall(alias, false); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, true)).build(); - performActionAndWait(step, indexMetadata, clusterState, null); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); + performActionAndWait(step, indexMetadata, state, null); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).rolloverIndex(Mockito.any(), Mockito.any()); } @@ -115,20 +116,20 @@ public void testPerformActionOnDataStream() throws Exception { RolloverStep step = createRandomInstance(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .put(newInstance(dataStreamName, List.of(indexMetadata.getIndex()), List.of(failureIndexMetadata.getIndex()))) - .put(indexMetadata, true) - .put(failureIndexMetadata, true) - ) - .build(); + ProjectState state = projectStateFromProject( + ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(newInstance(dataStreamName, List.of(indexMetadata.getIndex()), List.of(failureIndexMetadata.getIndex()))) + .put(indexMetadata, true) + .put(failureIndexMetadata, true) + ); boolean useFailureStore = randomBoolean(); IndexMetadata indexToOperateOn = useFailureStore ? failureIndexMetadata : indexMetadata; mockClientRolloverCall(dataStreamName, useFailureStore); - performActionAndWait(step, indexToOperateOn, clusterState, null); + performActionAndWait(step, indexToOperateOn, state, null); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).rolloverIndex(Mockito.any(), Mockito.any()); } @@ -159,25 +160,23 @@ public void testSkipRolloverIfDataStreamIsAlreadyRolledOver() throws Exception { .build(); RolloverStep step = createRandomInstance(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .put(firstGenerationIndex, true) - .put(writeIndex, true) - .put(failureFirstGenerationIndex, true) - .put(failureWriteIndex, true) - .put( - newInstance( - dataStreamName, - List.of(firstGenerationIndex.getIndex(), writeIndex.getIndex()), - List.of(failureFirstGenerationIndex.getIndex(), failureWriteIndex.getIndex()) - ) + ProjectState state = projectStateFromProject( + ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(firstGenerationIndex, true) + .put(writeIndex, true) + .put(failureFirstGenerationIndex, true) + .put(failureWriteIndex, true) + .put( + newInstance( + dataStreamName, + List.of(firstGenerationIndex.getIndex(), writeIndex.getIndex()), + List.of(failureFirstGenerationIndex.getIndex(), failureWriteIndex.getIndex()) ) - ) - .build(); + ) + ); boolean useFailureStore = randomBoolean(); IndexMetadata indexToOperateOn = useFailureStore ? failureFirstGenerationIndex : firstGenerationIndex; - performActionAndWait(step, indexToOperateOn, clusterState, null); + performActionAndWait(step, indexToOperateOn, state, null); verifyNoMoreInteractions(client); verifyNoMoreInteractions(adminClient); @@ -209,8 +208,8 @@ public void testPerformActionWithIndexingComplete() throws Exception { RolloverStep step = createRandomInstance(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, true)).build(); - performActionAndWait(step, indexMetadata, clusterState, null); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); + performActionAndWait(step, indexMetadata, state, null); } public void testPerformActionSkipsRolloverForAlreadyRolledIndex() throws Exception { @@ -226,8 +225,8 @@ public void testPerformActionSkipsRolloverForAlreadyRolledIndex() throws Excepti .build(); RolloverStep step = createRandomInstance(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, true)).build(); - performActionAndWait(step, indexMetadata, clusterState, null); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); + performActionAndWait(step, indexMetadata, state, null); Mockito.verify(indicesClient, Mockito.never()).rolloverIndex(Mockito.any(), Mockito.any()); } @@ -247,10 +246,12 @@ public void testPerformActionFailure() { return null; }).when(indicesClient).rolloverIndex(Mockito.any(), Mockito.any()); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, true)).build(); - assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, clusterState, null))); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); + assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, state, null))); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).rolloverIndex(Mockito.any(), Mockito.any()); } @@ -264,8 +265,8 @@ public void testPerformActionInvalidNullOrEmptyAlias() { .build(); RolloverStep step = createRandomInstance(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, true)).build(); - Exception e = expectThrows(IllegalArgumentException.class, () -> performActionAndWait(step, indexMetadata, clusterState, null)); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); + Exception e = expectThrows(IllegalArgumentException.class, () -> performActionAndWait(step, indexMetadata, state, null)); assertThat( e.getMessage(), Matchers.is( @@ -289,8 +290,8 @@ public void testPerformActionAliasDoesNotPointToIndex() { .build(); RolloverStep step = createRandomInstance(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, true)).build(); - Exception e = expectThrows(IllegalArgumentException.class, () -> performActionAndWait(step, indexMetadata, clusterState, null)); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true)); + Exception e = expectThrows(IllegalArgumentException.class, () -> performActionAndWait(step, indexMetadata, state, null)); assertThat( e.getMessage(), Matchers.is( diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStepTests.java index f988a6fd5769c..fbf02a15cafe3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStepTests.java @@ -10,9 +10,12 @@ import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.internal.transport.NoNodeAvailableException; +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.ProjectMetadata; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeRole; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; @@ -69,7 +72,7 @@ protected SetSingleNodeAllocateStep mutateInstance(SetSingleNodeAllocateStep ins default -> throw new AssertionError("Illegal randomisation branch"); } - return new SetSingleNodeAllocateStep(key, nextKey, instance.getClient()); + return new SetSingleNodeAllocateStep(key, nextKey, instance.getClientWithoutProject()); } @Override @@ -221,15 +224,17 @@ public void testPerformActionWithClusterExcludeFilters() throws IOException { Map indices = Map.of(index.getName(), indexMetadata); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node_id_0", true, ShardRoutingState.STARTED)); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder().indices(indices).transientSettings(clusterSettings)) + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()).indices(indices).build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata(Metadata.builder().put(project).transientSettings(clusterSettings)) .nodes(nodes) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); SetSingleNodeAllocateStep step = createRandomInstance(); - expectThrows(NoNodeAvailableException.class, () -> performActionAndWait(step, indexMetadata, clusterState, null)); + expectThrows(NoNodeAvailableException.class, () -> performActionAndWait(step, indexMetadata, state, null)); Mockito.verifyNoMoreInteractions(client); } @@ -302,11 +307,13 @@ public void testPerformActionAttrsRequestFails() { Map indices = Map.of(index.getName(), indexMetadata); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node_id_0", true, ShardRoutingState.STARTED)); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder().indices(indices)) + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()).indices(indices).build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) .nodes(nodes) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); SetSingleNodeAllocateStep step = createRandomInstance(); Exception exception = new RuntimeException(); @@ -326,9 +333,11 @@ public void testPerformActionAttrsRequestFails() { return null; }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); - assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, clusterState, null))); + assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, state, null))); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any()); } @@ -368,18 +377,17 @@ public void testPerformActionAttrsNoShard() { Map indices = Map.of(index.getName(), indexMetadata); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder().indices(indices)) + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()).indices(indices).build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) .nodes(nodes) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); SetSingleNodeAllocateStep step = createRandomInstance(); - IndexNotFoundException e = expectThrows( - IndexNotFoundException.class, - () -> performActionAndWait(step, indexMetadata, clusterState, null) - ); + IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () -> performActionAndWait(step, indexMetadata, state, null)); assertEquals(indexMetadata.getIndex(), e.getIndex()); Mockito.verifyNoMoreInteractions(client); @@ -630,11 +638,13 @@ private void assertNodeSelected( IndexRoutingTable indexRoutingTable ) throws Exception { Map indices = Map.of(index.getName(), indexMetadata); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder().indices(indices)) + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()).indices(indices).build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) .nodes(nodes) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); SetSingleNodeAllocateStep step = createRandomInstance(); @@ -653,9 +663,11 @@ private void assertNodeSelected( return null; }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); - performActionAndWait(step, indexMetadata, clusterState, null); + performActionAndWait(step, indexMetadata, state, null); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any()); } @@ -670,15 +682,17 @@ private void assertNoValidNode(IndexMetadata indexMetadata, Index index, Discove private void assertNoValidNode(IndexMetadata indexMetadata, Index index, DiscoveryNodes nodes, IndexRoutingTable indexRoutingTable) { Map indices = Map.of(index.getName(), indexMetadata); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder().indices(indices)) + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()).indices(indices).build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) .nodes(nodes) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); SetSingleNodeAllocateStep step = createRandomInstance(); - expectThrows(NoNodeAvailableException.class, () -> performActionAndWait(step, indexMetadata, clusterState, null)); + expectThrows(NoNodeAvailableException.class, () -> performActionAndWait(step, indexMetadata, state, null)); Mockito.verifyNoMoreInteractions(client); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkActionTests.java index 1bfe96bf0445c..83c7d51cbfa61 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkActionTests.java @@ -13,11 +13,10 @@ import org.elasticsearch.client.internal.AdminClient; import org.elasticsearch.client.internal.Client; import org.elasticsearch.client.internal.IndicesAdminClient; -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.LifecycleExecutionState; -import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; @@ -50,7 +49,9 @@ public void setUpClient() throws Exception { AdminClient adminClient = Mockito.mock(AdminClient.class); indicesClient = Mockito.mock(IndicesAdminClient.class); - Mockito.when(client.admin()).thenReturn(adminClient); + Client projectClient = Mockito.mock(Client.class); + Mockito.when(client.projectClient(Mockito.any())).thenReturn(projectClient); + Mockito.when(projectClient.admin()).thenReturn(adminClient); Mockito.when(adminClient.indices()).thenReturn(indicesClient); } @@ -211,33 +212,31 @@ public void assertPerformAction( randomNonNegativeLong(), randomNonNegativeLong() ); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .putCustom( - IndexLifecycleMetadata.TYPE, - new IndexLifecycleMetadata(Map.of(policyMetadata.getName(), policyMetadata), OperationMode.RUNNING) + ProjectState state = projectStateFromProject( + ProjectMetadata.builder(randomProjectIdOrDefault()) + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata(Map.of(policyMetadata.getName(), policyMetadata), OperationMode.RUNNING) + ) + .put( + indexMetadataBuilder.putCustom( + LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, + LifecycleExecutionState.builder() + .setPhase(branchStep.getKey().phase()) + .setPhaseTime(0L) + .setAction(branchStep.getKey().action()) + .setActionTime(0L) + .setStep(branchStep.getKey().name()) + .setStepTime(0L) + .build() + .asMap() ) - .put( - indexMetadataBuilder.putCustom( - LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, - LifecycleExecutionState.builder() - .setPhase(branchStep.getKey().phase()) - .setPhaseTime(0L) - .setAction(branchStep.getKey().action()) - .setActionTime(0L) - .setStep(branchStep.getKey().name()) - .setStepTime(0L) - .build() - .asMap() - ) - ) - ) - .build(); + ) + ); setUpIndicesStatsRequestMock(indexName, withError); CountDownLatch countDownLatch = new CountDownLatch(1); AtomicBoolean failurePropagated = new AtomicBoolean(false); - branchStep.performAction(state.metadata().getProject().index(indexName), state, null, new ActionListener<>() { + branchStep.performAction(state.metadata().index(indexName), state, null, new ActionListener<>() { @Override public void onResponse(Void unused) { countDownLatch.countDown(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java index c8efce288260f..bae3e8cdb8acb 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java @@ -40,12 +40,12 @@ public ShrinkSetAliasStep mutateInstance(ShrinkSetAliasStep instance) { case 1 -> nextKey = new StepKey(nextKey.phase(), nextKey.action(), nextKey.name() + randomAlphaOfLength(5)); default -> throw new AssertionError("Illegal randomisation branch"); } - return new ShrinkSetAliasStep(key, nextKey, instance.getClient()); + return new ShrinkSetAliasStep(key, nextKey, instance.getClientWithoutProject()); } @Override public ShrinkSetAliasStep copyInstance(ShrinkSetAliasStep instance) { - return new ShrinkSetAliasStep(instance.getKey(), instance.getNextStepKey(), instance.getClient()); + return new ShrinkSetAliasStep(instance.getKey(), instance.getNextStepKey(), instance.getClientWithoutProject()); } public void testPerformAction() throws Exception { @@ -93,9 +93,12 @@ public void testPerformAction() throws Exception { return null; }).when(indicesClient).aliases(Mockito.any(), Mockito.any()); - performActionAndWait(step, indexMetadata, emptyClusterState(), null); + final var state = projectStateWithEmptyProject(); + performActionAndWait(step, indexMetadata, state, null); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).aliases(Mockito.any(), Mockito.any()); } @@ -116,9 +119,12 @@ public void testPerformActionFailure() { return null; }).when(indicesClient).aliases(Mockito.any(), Mockito.any()); - assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, emptyClusterState(), null))); + final var state = projectStateWithEmptyProject(); + assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, state, null))); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).aliases(Mockito.any(), Mockito.any()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java index 36e56c4747ef7..469fd74349e03 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.rollover.RolloverResponse; import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.AliasMetadata; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; -import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.index.IndexVersion; @@ -66,7 +66,7 @@ public ShrinkStep mutateInstance(ShrinkStep instance) { default -> throw new AssertionError("Illegal randomisation branch"); } - return new ShrinkStep(key, nextKey, instance.getClient(), numberOfShards, maxPrimaryShardSize); + return new ShrinkStep(key, nextKey, instance.getClientWithoutProject(), numberOfShards, maxPrimaryShardSize); } @Override @@ -74,7 +74,7 @@ public ShrinkStep copyInstance(ShrinkStep instance) { return new ShrinkStep( instance.getKey(), instance.getNextStepKey(), - instance.getClient(), + instance.getClientWithoutProject(), instance.getNumberOfShards(), instance.getMaxPrimaryShardSize() ); @@ -123,9 +123,12 @@ public void testPerformAction() throws Exception { return null; }).when(indicesClient).resizeIndex(Mockito.any(), Mockito.any()); - performActionAndWait(step, sourceIndexMetadata, emptyClusterState(), null); + final var state = projectStateWithEmptyProject(); + performActionAndWait(step, sourceIndexMetadata, state, null); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).resizeIndex(Mockito.any(), Mockito.any()); } @@ -155,9 +158,9 @@ public void testPerformActionShrunkenIndexExists() throws Exception { .numberOfReplicas(0) .build(); Map indices = Map.of(generatedShrunkenIndexName, indexMetadata); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().indices(indices)).build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).indices(indices)); - step.performAction(sourceIndexMetadata, clusterState, null, new ActionListener<>() { + step.performAction(sourceIndexMetadata, state, null, new ActionListener<>() { @Override public void onResponse(Void unused) {} @@ -186,9 +189,12 @@ public void testPerformActionIsCompleteForUnAckedRequests() throws Exception { return null; }).when(indicesClient).resizeIndex(Mockito.any(), Mockito.any()); - performActionAndWait(step, indexMetadata, emptyClusterState(), null); + final var state = projectStateWithEmptyProject(); + performActionAndWait(step, indexMetadata, state, null); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).resizeIndex(Mockito.any(), Mockito.any()); } @@ -212,9 +218,12 @@ public void testPerformActionFailure() throws Exception { return null; }).when(indicesClient).resizeIndex(Mockito.any(), Mockito.any()); - assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, emptyClusterState(), null))); + final var state = projectStateWithEmptyProject(); + assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, state, null))); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).resizeIndex(Mockito.any(), Mockito.any()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStepTests.java index 1a99043b86ad7..00cb3169285c1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStepTests.java @@ -13,11 +13,12 @@ import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions; import org.elasticsearch.action.admin.indices.alias.TransportIndicesAliasesAction; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.AliasMetadata; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; -import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; +import org.elasticsearch.cluster.project.TestProjectResolvers; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.test.client.NoOpClient; import org.elasticsearch.threadpool.ThreadPool; @@ -44,7 +45,7 @@ protected SwapAliasesAndDeleteSourceIndexStep copyInstance(SwapAliasesAndDeleteS return new SwapAliasesAndDeleteSourceIndexStep( instance.getKey(), instance.getNextStepKey(), - instance.getClient(), + instance.getClientWithoutProject(), instance.getTargetIndexNameSupplier(), instance.getCreateSourceIndexAlias() ); @@ -63,7 +64,13 @@ public SwapAliasesAndDeleteSourceIndexStep mutateInstance(SwapAliasesAndDeleteSo case 3 -> createSourceIndexAlias = createSourceIndexAlias == false; default -> throw new AssertionError("Illegal randomisation branch"); } - return new SwapAliasesAndDeleteSourceIndexStep(key, nextKey, instance.getClient(), indexNameSupplier, createSourceIndexAlias); + return new SwapAliasesAndDeleteSourceIndexStep( + key, + nextKey, + instance.getClientWithoutProject(), + indexNameSupplier, + createSourceIndexAlias + ); } public void testPerformAction() { @@ -117,16 +124,16 @@ public void testPerformAction() { .numberOfShards(randomIntBetween(1, 5)) .numberOfReplicas(randomIntBetween(0, 5)); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(sourceIndexMetadata, true).put(targetIndexMetadataBuilder).build()) - .build(); + ProjectState state = projectStateFromProject( + ProjectMetadata.builder(randomProjectIdOrDefault()).put(sourceIndexMetadata, true).put(targetIndexMetadataBuilder) + ); - step.performAction(sourceIndexMetadata, clusterState, null, ActionListener.noop()); + step.performAction(sourceIndexMetadata, state, null, ActionListener.noop()); } } private NoOpClient getIndicesAliasAssertingClient(ThreadPool threadPool, List expectedAliasActions) { - return new NoOpClient(threadPool) { + return new NoOpClient(threadPool, TestProjectResolvers.usingRequestHeader(threadPool.getThreadContext())) { @Override protected void doExecute( ActionType action, diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStepTests.java index 8e40d3af86d81..ef94954275c5e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStepTests.java @@ -45,7 +45,8 @@ public void testUnFollow() throws Exception { }).when(client).execute(Mockito.same(UnfollowAction.INSTANCE), Mockito.any(), Mockito.any()); UnfollowFollowerIndexStep step = new UnfollowFollowerIndexStep(randomStepKey(), randomStepKey(), client); - performActionAndWait(step, indexMetadata, null, null); + var state = projectStateWithEmptyProject(); + performActionAndWait(step, indexMetadata, state, null); } public void testRequestNotAcknowledged() { @@ -64,7 +65,8 @@ public void testRequestNotAcknowledged() { }).when(client).execute(Mockito.same(UnfollowAction.INSTANCE), Mockito.any(), Mockito.any()); UnfollowFollowerIndexStep step = new UnfollowFollowerIndexStep(randomStepKey(), randomStepKey(), client); - Exception e = expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, null, null)); + var state = projectStateWithEmptyProject(); + Exception e = expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, state, null)); assertThat(e.getMessage(), is("unfollow request failed to be acknowledged")); } @@ -87,7 +89,8 @@ public void testUnFollowUnfollowFailed() { }).when(client).execute(Mockito.same(UnfollowAction.INSTANCE), Mockito.any(), Mockito.any()); UnfollowFollowerIndexStep step = new UnfollowFollowerIndexStep(randomStepKey(), randomStepKey(), client); - assertSame(error, expectThrows(RuntimeException.class, () -> performActionAndWait(step, indexMetadata, null, null))); + var state = projectStateWithEmptyProject(); + assertSame(error, expectThrows(RuntimeException.class, () -> performActionAndWait(step, indexMetadata, state, null))); } public void testFailureToReleaseRetentionLeases() throws Exception { @@ -110,6 +113,7 @@ public void testFailureToReleaseRetentionLeases() throws Exception { }).when(client).execute(Mockito.same(UnfollowAction.INSTANCE), Mockito.any(), Mockito.any()); UnfollowFollowerIndexStep step = new UnfollowFollowerIndexStep(randomStepKey(), randomStepKey(), client); - performActionAndWait(step, indexMetadata, null, null); + var state = projectStateWithEmptyProject(); + performActionAndWait(step, indexMetadata, state, null); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java index 750ab67a9e1a9..077104906098f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java @@ -46,7 +46,12 @@ public UpdateSettingsStep mutateInstance(UpdateSettingsStep instance) { @Override public UpdateSettingsStep copyInstance(UpdateSettingsStep instance) { - return new UpdateSettingsStep(instance.getKey(), instance.getNextStepKey(), instance.getClient(), instance.getSettings()); + return new UpdateSettingsStep( + instance.getKey(), + instance.getNextStepKey(), + instance.getClientWithoutProject(), + instance.getSettings() + ); } private static IndexMetadata getIndexMetadata() { @@ -72,9 +77,12 @@ public void testPerformAction() throws Exception { return null; }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); - performActionAndWait(step, indexMetadata, emptyClusterState(), null); + final var state = projectStateWithEmptyProject(); + performActionAndWait(step, indexMetadata, state, null); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any()); } @@ -94,9 +102,12 @@ public void testPerformActionFailure() { return null; }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); - assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, emptyClusterState(), null))); + final var state = projectStateWithEmptyProject(); + assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, state, null))); - Mockito.verify(client, Mockito.only()).admin(); + Mockito.verify(client).projectClient(state.projectId()); + Mockito.verify(client).admin(); + Mockito.verifyNoMoreInteractions(client); Mockito.verify(adminClient, Mockito.only()).indices(); Mockito.verify(indicesClient, Mockito.only()).updateSettings(Mockito.any(), Mockito.any()); } diff --git a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/UpdateSettingsStepTests.java b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/UpdateSettingsStepTests.java index bad0f732020f8..be7256f9d27e5 100644 --- a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/UpdateSettingsStepTests.java +++ b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/UpdateSettingsStepTests.java @@ -10,6 +10,7 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.cluster.metadata.ProjectId; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; @@ -107,7 +108,8 @@ public void testUpdateSettingsStepRetriesOnError() throws InterruptedException { ClusterService clusterService = getInstanceFromNode(ClusterService.class); ClusterState state = clusterService.state(); - IndexMetadata indexMetadata = state.metadata().getProject().index("test"); + final var projectId = ProjectId.DEFAULT; + IndexMetadata indexMetadata = state.metadata().getProject(projectId).index("test"); ThreadPool threadPool = getInstanceFromNode(ThreadPool.class); ClusterStateObserver observer = new ClusterStateObserver(clusterService, null, logger, threadPool.getThreadContext()); @@ -122,7 +124,7 @@ public void testUpdateSettingsStepRetriesOnError() throws InterruptedException { invalidValueSetting ); - step.performAction(indexMetadata, state, observer, new ActionListener<>() { + step.performAction(indexMetadata, state.projectState(projectId), observer, new ActionListener<>() { @Override public void onResponse(Void complete) { latch.countDown(); @@ -142,7 +144,7 @@ public void onFailure(Exception e) { validIndexSetting ); - step.performAction(indexMetadata, state, observer, new ActionListener<>() { + step.performAction(indexMetadata, state.projectState(projectId), observer, new ActionListener<>() { @Override public void onResponse(Void complete) { latch.countDown(); diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java index 3d31537f0a76c..48854972f5ed7 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java @@ -358,7 +358,7 @@ void maybeRunAsyncAction(ProjectState state, IndexMetadata indexMetadata, String logger.debug("[{}] running policy with async action step [{}]", index, currentStep.getKey()); ((AsyncActionStep) currentStep).performAction( indexMetadata, - state.cluster(), + state, new ClusterStateObserver(clusterService, null, logger, threadPool.getThreadContext()), new ActionListener<>() { diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java index 155149de1c133..2db166a91cbbe 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java @@ -994,7 +994,7 @@ public void setLatch(CountDownLatch latch) { @Override public void performAction( IndexMetadata indexMetadata, - ClusterState currentState, + ProjectState currentState, ClusterStateObserver observer, ActionListener listener ) {