-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent Duplicate ILM Cluster State Updates from Being Created #78390
Merged
original-brownbear
merged 4 commits into
elastic:master
from
original-brownbear:dedup-ilm
Sep 29, 2021
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5985b35
Prevent Duplicate ILM Cluster State Updates from Being Created
original-brownbear c8d0f89
nice
original-brownbear e9736be
Merge remote-tracking branch 'elastic/master' into dedup-ilm
original-brownbear bdde571
CR: comments + simpler
original-brownbear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...n/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleClusterStateUpdateTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.ilm; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.ClusterStateUpdateTask; | ||
import org.elasticsearch.common.util.concurrent.ListenableFuture; | ||
|
||
/** | ||
* Base class for index lifecycle cluster state update tasks that requires implementing {@code equals} and {@code hashCode} to allow | ||
* for these tasks to be deduplicated by {@link IndexLifecycleRunner}. | ||
*/ | ||
public abstract class IndexLifecycleClusterStateUpdateTask extends ClusterStateUpdateTask { | ||
|
||
private final ListenableFuture<Void> listener = new ListenableFuture<>(); | ||
|
||
@Override | ||
public final void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { | ||
listener.onResponse(null); | ||
onClusterStateProcessed(source, oldState, newState); | ||
} | ||
|
||
@Override | ||
public final void onFailure(String source, Exception e) { | ||
listener.onFailure(e); | ||
handleFailure(source, e); | ||
} | ||
|
||
/** | ||
* Add a listener that is resolved once this update has been processed or failed and before either the | ||
* {@link #onClusterStateProcessed(String, ClusterState, ClusterState)} or the {@link #handleFailure(String, Exception)} hooks are | ||
* executed. | ||
*/ | ||
public final void addListener(ActionListener<Void> listener) { | ||
this.listener.addListener(listener); | ||
} | ||
|
||
/** | ||
* This method is functionally the same as {@link ClusterStateUpdateTask#clusterStateProcessed(String, ClusterState, ClusterState)} | ||
* and implementations can override it as they would override {@code ClusterStateUpdateTask#clusterStateProcessed}. | ||
*/ | ||
protected void onClusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { | ||
} | ||
|
||
@Override | ||
public abstract boolean equals(Object other); | ||
|
||
@Override | ||
public abstract int hashCode(); | ||
|
||
/** | ||
* This method is functionally the same as {@link ClusterStateUpdateTask#onFailure(String, Exception)} and implementations can override | ||
* it as they would override {@code ClusterStateUpdateTask#onFailure}. | ||
*/ | ||
protected abstract void handleFailure(String source, Exception e); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -12,6 +12,7 @@ | |||||||||||||||||||
import org.elasticsearch.action.ActionListener; | ||||||||||||||||||||
import org.elasticsearch.cluster.ClusterState; | ||||||||||||||||||||
import org.elasticsearch.cluster.ClusterStateObserver; | ||||||||||||||||||||
import org.elasticsearch.cluster.ClusterStateTaskConfig; | ||||||||||||||||||||
import org.elasticsearch.cluster.ClusterStateUpdateTask; | ||||||||||||||||||||
import org.elasticsearch.cluster.metadata.IndexMetadata; | ||||||||||||||||||||
import org.elasticsearch.cluster.metadata.Metadata; | ||||||||||||||||||||
|
@@ -35,7 +36,9 @@ | |||||||||||||||||||
import org.elasticsearch.xpack.ilm.history.ILMHistoryItem; | ||||||||||||||||||||
import org.elasticsearch.xpack.ilm.history.ILMHistoryStore; | ||||||||||||||||||||
|
||||||||||||||||||||
import java.util.HashSet; | ||||||||||||||||||||
import java.util.Locale; | ||||||||||||||||||||
import java.util.Set; | ||||||||||||||||||||
import java.util.function.LongSupplier; | ||||||||||||||||||||
|
||||||||||||||||||||
import static org.elasticsearch.xpack.core.ilm.LifecycleSettings.LIFECYCLE_ORIGINATION_DATE; | ||||||||||||||||||||
|
@@ -374,7 +377,7 @@ void runPolicyAfterStateChange(String policy, IndexMetadata indexMetadata) { | |||||||||||||||||||
} | ||||||||||||||||||||
} else if (currentStep instanceof ClusterStateActionStep || currentStep instanceof ClusterStateWaitStep) { | ||||||||||||||||||||
logger.debug("[{}] running policy with current-step [{}]", indexMetadata.getIndex().getName(), currentStep.getKey()); | ||||||||||||||||||||
clusterService.submitStateUpdateTask(String.format(Locale.ROOT, "ilm-execute-cluster-state-steps [%s]", currentStep), | ||||||||||||||||||||
submitUnlessAlreadyQueued(String.format(Locale.ROOT, "ilm-execute-cluster-state-steps [%s]", currentStep), | ||||||||||||||||||||
new ExecuteStepsUpdateTask(policy, indexMetadata.getIndex(), currentStep, stepRegistry, this, nowSupplier)); | ||||||||||||||||||||
} else { | ||||||||||||||||||||
logger.trace("[{}] ignoring step execution from cluster state change event [{}]", index, currentStep.getKey()); | ||||||||||||||||||||
|
@@ -387,7 +390,7 @@ void runPolicyAfterStateChange(String policy, IndexMetadata indexMetadata) { | |||||||||||||||||||
*/ | ||||||||||||||||||||
private void moveToStep(Index index, String policy, Step.StepKey currentStepKey, Step.StepKey newStepKey) { | ||||||||||||||||||||
logger.debug("[{}] moving to step [{}] {} -> {}", index.getName(), policy, currentStepKey, newStepKey); | ||||||||||||||||||||
clusterService.submitStateUpdateTask( | ||||||||||||||||||||
submitUnlessAlreadyQueued( | ||||||||||||||||||||
String.format(Locale.ROOT, "ilm-move-to-step {policy [%s], index [%s], currentStep [%s], nextStep [%s]}", policy, | ||||||||||||||||||||
index.getName(), currentStepKey, newStepKey), | ||||||||||||||||||||
new MoveToNextStepUpdateTask(index, policy, currentStepKey, newStepKey, nowSupplier, stepRegistry, clusterState -> | ||||||||||||||||||||
|
@@ -420,7 +423,7 @@ private void moveToErrorStep(Index index, String policy, Step.StepKey currentSte | |||||||||||||||||||
* changing other execution state. | ||||||||||||||||||||
*/ | ||||||||||||||||||||
private void setStepInfo(Index index, String policy, @Nullable Step.StepKey currentStepKey, ToXContentObject stepInfo) { | ||||||||||||||||||||
clusterService.submitStateUpdateTask( | ||||||||||||||||||||
submitUnlessAlreadyQueued( | ||||||||||||||||||||
String.format(Locale.ROOT, "ilm-set-step-info {policy [%s], index [%s], currentStep [%s]}", policy, index.getName(), | ||||||||||||||||||||
currentStepKey), | ||||||||||||||||||||
new SetStepInfoUpdateTask(index, policy, currentStepKey, stepInfo)); | ||||||||||||||||||||
|
@@ -504,4 +507,38 @@ void registerFailedOperation(IndexMetadata indexMetadata, Exception failure) { | |||||||||||||||||||
LifecycleExecutionState.fromIndexMetadata(indexMetadata), | ||||||||||||||||||||
failure)); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
private final Set<IndexLifecycleClusterStateUpdateTask> executingTasks = new HashSet<>(); | ||||||||||||||||||||
|
||||||||||||||||||||
/** | ||||||||||||||||||||
* Tracks already executing {@link IndexLifecycleClusterStateUpdateTask} tasks in {@link #executingTasks} to prevent queueing up | ||||||||||||||||||||
* duplicate cluster state updates. | ||||||||||||||||||||
* TODO: refactor ILM logic so that this is not required any longer. It is unreasonably expensive to only filter out duplicate tasks at | ||||||||||||||||||||
* this point given how these tasks are mostly set up on the cluster state applier thread. | ||||||||||||||||||||
* | ||||||||||||||||||||
* @param source source string as used in {@link ClusterService#submitStateUpdateTask(String, ClusterStateTaskConfig)} | ||||||||||||||||||||
* @param task task to submit unless already tracked in {@link #executingTasks}. | ||||||||||||||||||||
*/ | ||||||||||||||||||||
private void submitUnlessAlreadyQueued(String source, IndexLifecycleClusterStateUpdateTask task) { | ||||||||||||||||||||
if (registerTask(task)) { | ||||||||||||||||||||
task.addListener(ActionListener.wrap(() -> unregisterTask(task))); | ||||||||||||||||||||
clusterService.submitStateUpdateTask(source, task); | ||||||||||||||||||||
} else { | ||||||||||||||||||||
logger.trace("skipped redundant execution of [{}]", source); | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
private boolean registerTask(IndexLifecycleClusterStateUpdateTask task) { | ||||||||||||||||||||
synchronized (executingTasks) { | ||||||||||||||||||||
return executingTasks.add(task); | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
private void unregisterTask(IndexLifecycleClusterStateUpdateTask task) { | ||||||||||||||||||||
final boolean removed; | ||||||||||||||||||||
synchronized (executingTasks) { | ||||||||||||||||||||
removed = executingTasks.remove(task); | ||||||||||||||||||||
} | ||||||||||||||||||||
assert removed : "tried to unregister unknown task [" + task + "]"; | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
} | ||||||||||||||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason for explicit
synchronized
block and not usingCollections.synchronizedSet(new HashSet<>())
orConcurrentHashMap
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right ... not sure why I did it this used the sync set now and just inlined everything since it's all one liners now :)
Thanks!