Skip to content

Commit e069316

Browse files
committed
Remove canSetPolicy, canUpdatePolicy, and canRemovePolicy (#33037)
* Remove canSetPolicy, canUpdatePolicy and canRemovePolicy Since we now store a pre-compiled list of steps for an index's phase in the `PolicyStepsRegistry`, we no longer need to worry about updating policies as any updates won't affect the current phase, and will only be picked up on phase transitions. This also removes the tests that test these methods Relates to #29823
1 parent 38b3b03 commit e069316

File tree

3 files changed

+29
-477
lines changed

3 files changed

+29
-477
lines changed

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunner.java

Lines changed: 27 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
package org.elasticsearch.xpack.indexlifecycle;
77

8-
import com.carrotsearch.hppc.cursors.ObjectCursor;
98
import org.apache.logging.log4j.LogManager;
109
import org.apache.logging.log4j.Logger;
1110
import org.elasticsearch.ElasticsearchException;
@@ -27,19 +26,15 @@
2726
import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateActionStep;
2827
import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateWaitStep;
2928
import org.elasticsearch.xpack.core.indexlifecycle.ErrorStep;
30-
import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata;
31-
import org.elasticsearch.xpack.core.indexlifecycle.LifecycleAction;
3229
import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy;
3330
import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings;
34-
import org.elasticsearch.xpack.core.indexlifecycle.Phase;
3531
import org.elasticsearch.xpack.core.indexlifecycle.RolloverAction;
3632
import org.elasticsearch.xpack.core.indexlifecycle.Step;
3733
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
3834
import org.elasticsearch.xpack.core.indexlifecycle.TerminalPolicyStep;
3935

4036
import java.io.IOException;
4137
import java.util.List;
42-
import java.util.Map;
4338
import java.util.function.LongSupplier;
4439

4540
public class IndexLifecycleRunner {
@@ -325,8 +320,6 @@ private void setStepInfo(Index index, String policy, StepKey currentStepKey, ToX
325320

326321
public static ClusterState setPolicyForIndexes(final String newPolicyName, final Index[] indices, ClusterState currentState,
327322
LifecyclePolicy newPolicy, List<String> failedIndexes, LongSupplier nowSupplier) {
328-
Map<String, LifecyclePolicy> policiesMap = ((IndexLifecycleMetadata) currentState.metaData().custom(IndexLifecycleMetadata.TYPE))
329-
.getPolicies();
330323
MetaData.Builder newMetadata = MetaData.builder(currentState.getMetaData());
331324
boolean clusterStateChanged = false;
332325
for (Index index : indices) {
@@ -335,8 +328,8 @@ public static ClusterState setPolicyForIndexes(final String newPolicyName, final
335328
// Index doesn't exist so fail it
336329
failedIndexes.add(index.getName());
337330
} else {
338-
IndexMetaData.Builder newIdxMetadata = IndexLifecycleRunner.setPolicyForIndex(newPolicyName, newPolicy, policiesMap,
339-
failedIndexes, index, indexMetadata, nowSupplier);
331+
IndexMetaData.Builder newIdxMetadata = IndexLifecycleRunner.setPolicyForIndex(newPolicyName,
332+
newPolicy, indexMetadata, nowSupplier);
340333
if (newIdxMetadata != null) {
341334
newMetadata.put(newIdxMetadata);
342335
clusterStateChanged = true;
@@ -353,106 +346,24 @@ public static ClusterState setPolicyForIndexes(final String newPolicyName, final
353346
}
354347

355348
private static IndexMetaData.Builder setPolicyForIndex(final String newPolicyName, LifecyclePolicy newPolicy,
356-
Map<String, LifecyclePolicy> policiesMap, List<String> failedIndexes, Index index, IndexMetaData indexMetadata,
357-
LongSupplier nowSupplier) {
349+
IndexMetaData indexMetadata, LongSupplier nowSupplier) {
358350
Settings idxSettings = indexMetadata.getSettings();
359-
String currentPolicyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxSettings);
360351
StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(idxSettings);
361-
LifecyclePolicy currentPolicy = null;
362-
if (Strings.hasLength(currentPolicyName)) {
363-
currentPolicy = policiesMap.get(currentPolicyName);
364-
}
365-
366-
if (canSetPolicy(currentStepKey, currentPolicy, newPolicy)) {
367-
Settings.Builder newSettings = Settings.builder().put(idxSettings);
368-
if (currentStepKey != null) {
369-
// Check if current step exists in new policy and if not move to
370-
// next available step
371-
StepKey nextValidStepKey = newPolicy.getNextValidStep(currentStepKey);
372-
if (nextValidStepKey.equals(currentStepKey) == false) {
373-
newSettings = moveIndexSettingsToNextStep(idxSettings, currentStepKey, nextValidStepKey, nowSupplier);
374-
}
375-
}
376-
newSettings.put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), newPolicyName);
377-
return IndexMetaData.builder(indexMetadata).settings(newSettings);
378-
} else {
379-
failedIndexes.add(index.getName());
380-
return null;
381-
}
382-
}
383-
384-
private static boolean canSetPolicy(StepKey currentStepKey, LifecyclePolicy currentPolicy, LifecyclePolicy newPolicy) {
385-
if (currentPolicy != null) {
386-
if (currentPolicy.isActionSafe(currentStepKey)) {
387-
return true;
388-
} else {
389-
// Index is in an unsafe action so fail it if the action has changed between oldPolicy and newPolicy
390-
return isActionChanged(currentStepKey, currentPolicy, newPolicy) == false;
391-
}
392-
} else {
393-
// Index not previously managed by ILM so safe to change policy
394-
return true;
395-
}
396-
}
397-
398-
private static boolean isActionChanged(StepKey stepKey, LifecyclePolicy currentPolicy, LifecyclePolicy newPolicy) {
399-
LifecycleAction currentAction = getActionFromPolicy(currentPolicy, stepKey.getPhase(), stepKey.getAction());
400-
LifecycleAction newAction = getActionFromPolicy(newPolicy, stepKey.getPhase(), stepKey.getAction());
401-
if (newAction == null) {
402-
return true;
403-
} else {
404-
return currentAction.equals(newAction) == false;
405-
}
406-
}
407-
408-
private static LifecycleAction getActionFromPolicy(LifecyclePolicy policy, String phaseName, String actionName) {
409-
Phase phase = policy.getPhases().get(phaseName);
410-
if (phase != null) {
411-
return phase.getActions().get(actionName);
412-
} else {
413-
return null;
414-
}
415-
}
416352

417-
/**
418-
* Returns <code>true</code> if the provided policy is allowed to be updated
419-
* given the current {@link ClusterState}. In practice this method checks
420-
* that all the indexes using the provided <code>policyName</code> is in a
421-
* state where it is able to deal with the policy being updated to
422-
* <code>newPolicy</code>. If any of these indexes is not in a state wheree
423-
* it can deal with the update the method will return <code>false</code>.
424-
*
425-
* @param policyName
426-
* the name of the policy being updated
427-
* @param newPolicy
428-
* the new version of the {@link LifecyclePolicy}
429-
* @param currentState
430-
* the current {@link ClusterState}
431-
*/
432-
public static boolean canUpdatePolicy(String policyName, LifecyclePolicy newPolicy, ClusterState currentState) {
433-
Map<String, LifecyclePolicy> policiesMap = ((IndexLifecycleMetadata) currentState.metaData().custom(IndexLifecycleMetadata.TYPE))
434-
.getPolicies();
435-
for (ObjectCursor<IndexMetaData> cursor : currentState.getMetaData().indices().values()) {
436-
IndexMetaData idxMetadata = cursor.value;
437-
Settings idxSettings = idxMetadata.getSettings();
438-
String currentPolicyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxSettings);
439-
LifecyclePolicy currentPolicy = null;
440-
if (Strings.hasLength(currentPolicyName)) {
441-
currentPolicy = policiesMap.get(currentPolicyName);
442-
}
443-
if (policyName.equals(currentPolicyName)) {
444-
StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(idxSettings);
445-
if (canSetPolicy(currentStepKey, currentPolicy, newPolicy) == false) {
446-
return false;
447-
}
353+
Settings.Builder newSettings = Settings.builder().put(idxSettings);
354+
if (currentStepKey != null) {
355+
// Check if current step exists in new policy and if not move to
356+
// next available step
357+
StepKey nextValidStepKey = newPolicy.getNextValidStep(currentStepKey);
358+
if (nextValidStepKey.equals(currentStepKey) == false) {
359+
newSettings = moveIndexSettingsToNextStep(idxSettings, currentStepKey, nextValidStepKey, nowSupplier);
448360
}
449361
}
450-
return true;
362+
newSettings.put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), newPolicyName);
363+
return IndexMetaData.builder(indexMetadata).settings(newSettings);
451364
}
452365

453366
public static ClusterState removePolicyForIndexes(final Index[] indices, ClusterState currentState, List<String> failedIndexes) {
454-
Map<String, LifecyclePolicy> policiesMap = ((IndexLifecycleMetadata) currentState.metaData().custom(IndexLifecycleMetadata.TYPE))
455-
.getPolicies();
456367
MetaData.Builder newMetadata = MetaData.builder(currentState.getMetaData());
457368
boolean clusterStateChanged = false;
458369
for (Index index : indices) {
@@ -461,8 +372,7 @@ public static ClusterState removePolicyForIndexes(final Index[] indices, Cluster
461372
// Index doesn't exist so fail it
462373
failedIndexes.add(index.getName());
463374
} else {
464-
IndexMetaData.Builder newIdxMetadata = IndexLifecycleRunner.removePolicyForIndex(index, indexMetadata, policiesMap,
465-
failedIndexes);
375+
IndexMetaData.Builder newIdxMetadata = IndexLifecycleRunner.removePolicyForIndex(indexMetadata);
466376
if (newIdxMetadata != null) {
467377
newMetadata.put(newIdxMetadata);
468378
clusterStateChanged = true;
@@ -478,44 +388,22 @@ public static ClusterState removePolicyForIndexes(final Index[] indices, Cluster
478388
}
479389
}
480390

481-
private static IndexMetaData.Builder removePolicyForIndex(Index index, IndexMetaData indexMetadata,
482-
Map<String, LifecyclePolicy> policiesMap, List<String> failedIndexes) {
391+
private static IndexMetaData.Builder removePolicyForIndex(IndexMetaData indexMetadata) {
483392
Settings idxSettings = indexMetadata.getSettings();
484393
Settings.Builder newSettings = Settings.builder().put(idxSettings);
485-
String currentPolicyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxSettings);
486-
StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(idxSettings);
487-
LifecyclePolicy currentPolicy = null;
488-
if (Strings.hasLength(currentPolicyName)) {
489-
currentPolicy = policiesMap.get(currentPolicyName);
490-
}
491-
492-
if (canRemovePolicy(currentStepKey, currentPolicy)) {
493-
newSettings.remove(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey());
494-
newSettings.remove(LifecycleSettings.LIFECYCLE_PHASE_SETTING.getKey());
495-
newSettings.remove(LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.getKey());
496-
newSettings.remove(LifecycleSettings.LIFECYCLE_ACTION_SETTING.getKey());
497-
newSettings.remove(LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.getKey());
498-
newSettings.remove(LifecycleSettings.LIFECYCLE_STEP_SETTING.getKey());
499-
newSettings.remove(LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING.getKey());
500-
newSettings.remove(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.getKey());
501-
newSettings.remove(LifecycleSettings.LIFECYCLE_FAILED_STEP_SETTING.getKey());
502-
newSettings.remove(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE_SETTING.getKey());
503-
newSettings.remove(LifecycleSettings.LIFECYCLE_SKIP_SETTING.getKey());
504-
newSettings.remove(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING.getKey());
505-
return IndexMetaData.builder(indexMetadata).settings(newSettings);
506-
} else {
507-
failedIndexes.add(index.getName());
508-
return null;
509-
}
510-
}
511394

512-
private static boolean canRemovePolicy(StepKey currentStepKey, LifecyclePolicy currentPolicy) {
513-
if (currentPolicy != null) {
514-
// Can't remove policy if the index is currently in an unsafe action
515-
return currentPolicy.isActionSafe(currentStepKey);
516-
} else {
517-
// Index not previously managed by ILM
518-
return true;
519-
}
395+
newSettings.remove(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey());
396+
newSettings.remove(LifecycleSettings.LIFECYCLE_PHASE_SETTING.getKey());
397+
newSettings.remove(LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.getKey());
398+
newSettings.remove(LifecycleSettings.LIFECYCLE_ACTION_SETTING.getKey());
399+
newSettings.remove(LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.getKey());
400+
newSettings.remove(LifecycleSettings.LIFECYCLE_STEP_SETTING.getKey());
401+
newSettings.remove(LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING.getKey());
402+
newSettings.remove(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.getKey());
403+
newSettings.remove(LifecycleSettings.LIFECYCLE_FAILED_STEP_SETTING.getKey());
404+
newSettings.remove(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE_SETTING.getKey());
405+
newSettings.remove(LifecycleSettings.LIFECYCLE_SKIP_SETTING.getKey());
406+
newSettings.remove(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING.getKey());
407+
return IndexMetaData.builder(indexMetadata).settings(newSettings);
520408
}
521409
}

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportPutLifecycleAction.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
package org.elasticsearch.xpack.indexlifecycle.action;
88

9-
import org.elasticsearch.ResourceAlreadyExistsException;
109
import org.elasticsearch.action.ActionListener;
1110
import org.elasticsearch.action.support.ActionFilters;
1211
import org.elasticsearch.action.support.master.TransportMasterNodeAction;
@@ -28,7 +27,6 @@
2827
import org.elasticsearch.xpack.core.indexlifecycle.action.PutLifecycleAction;
2928
import org.elasticsearch.xpack.core.indexlifecycle.action.PutLifecycleAction.Request;
3029
import org.elasticsearch.xpack.core.indexlifecycle.action.PutLifecycleAction.Response;
31-
import org.elasticsearch.xpack.indexlifecycle.IndexLifecycleRunner;
3230

3331
import java.util.Map;
3432
import java.util.SortedMap;
@@ -81,11 +79,6 @@ public ClusterState execute(ClusterState currentState) throws Exception {
8179
if (currentMetadata == null) { // first time using index-lifecycle feature, bootstrap metadata
8280
currentMetadata = IndexLifecycleMetadata.EMPTY;
8381
}
84-
if (currentMetadata.getPolicyMetadatas().containsKey(request.getPolicy().getName()) && IndexLifecycleRunner
85-
.canUpdatePolicy(request.getPolicy().getName(), request.getPolicy(), currentState) == false) {
86-
throw new ResourceAlreadyExistsException("Lifecycle policy already exists: {}",
87-
request.getPolicy().getName());
88-
}
8982
// NORELEASE Check if current step exists in new policy and if not move to next available step
9083
SortedMap<String, LifecyclePolicyMetadata> newPolicies = new TreeMap<>(currentMetadata.getPolicyMetadatas());
9184
LifecyclePolicyMetadata lifecyclePolicyMetadata = new LifecyclePolicyMetadata(request.getPolicy(), filteredHeaders);

0 commit comments

Comments
 (0)