-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Adds a check to only fail policy update if unsafe action is changed #32002
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -938,6 +938,67 @@ public void testSetPolicyForIndexIndexInUnsafe() { | |
assertSame(clusterState, newClusterState); | ||
} | ||
|
||
public void testSetPolicyForIndexIndexInUnsafeActionUnchanged() { | ||
long now = randomNonNegativeLong(); | ||
String indexName = randomAlphaOfLength(10); | ||
String oldPolicyName = "old_policy"; | ||
String newPolicyName = "new_policy"; | ||
StepKey currentStep = new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)); | ||
LifecyclePolicy oldPolicy = createPolicy(oldPolicyName, null, currentStep); | ||
LifecyclePolicy newPolicy = createPolicy(newPolicyName, null, currentStep); | ||
Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, oldPolicyName) | ||
.put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) | ||
.put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) | ||
.put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); | ||
List<LifecyclePolicyMetadata> policyMetadatas = new ArrayList<>(); | ||
policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); | ||
ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); | ||
Index index = clusterState.metaData().index(indexName).getIndex(); | ||
Index[] indices = new Index[] { index }; | ||
List<String> failedIndexes = new ArrayList<>(); | ||
|
||
ClusterState newClusterState = IndexLifecycleRunner.setPolicyForIndexes(newPolicyName, indices, clusterState, newPolicy, | ||
failedIndexes); | ||
|
||
assertTrue(failedIndexes.isEmpty()); | ||
assertClusterStateOnPolicy(clusterState, index, newPolicyName, currentStep, currentStep, newClusterState, now); | ||
} | ||
|
||
public void testSetPolicyForIndexIndexInUnsafeActionChanged() { | ||
String indexName = randomAlphaOfLength(10); | ||
String oldPolicyName = "old_policy"; | ||
String newPolicyName = "new_policy"; | ||
StepKey currentStep = new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)); | ||
LifecyclePolicy oldPolicy = createPolicy(oldPolicyName, null, currentStep); | ||
|
||
// change the current action so its not equal to the old one by adding a step | ||
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. should we also add a change here to remove the action from the policy? 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. you mean as a separate test? The existing Unsafe tests actually already do this as the new policy we create is empty 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. oh, I missed this. thanks |
||
Map<String, Phase> phases = new HashMap<>(); | ||
Map<String, LifecycleAction> actions = new HashMap<>(); | ||
MockAction unsafeAction = new MockAction(Collections.singletonList(new MockStep(currentStep, null)), false); | ||
actions.put(unsafeAction.getWriteableName(), unsafeAction); | ||
Phase phase = new Phase(currentStep.getPhase(), TimeValue.timeValueMillis(0), actions); | ||
phases.put(phase.getName(), phase); | ||
LifecyclePolicy newPolicy = new LifecyclePolicy(TestLifecycleType.INSTANCE, newPolicyName, phases); | ||
|
||
Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, oldPolicyName) | ||
.put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) | ||
.put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) | ||
.put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); | ||
List<LifecyclePolicyMetadata> policyMetadatas = new ArrayList<>(); | ||
policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); | ||
ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); | ||
Index index = clusterState.metaData().index(indexName).getIndex(); | ||
Index[] indices = new Index[] { index }; | ||
List<String> failedIndexes = new ArrayList<>(); | ||
|
||
ClusterState newClusterState = IndexLifecycleRunner.setPolicyForIndexes(newPolicyName, indices, clusterState, newPolicy, | ||
failedIndexes); | ||
|
||
assertEquals(1, failedIndexes.size()); | ||
assertEquals(index.getName(), failedIndexes.get(0)); | ||
assertSame(clusterState, newClusterState); | ||
} | ||
|
||
private static LifecyclePolicy createPolicy(String policyName, StepKey safeStep, StepKey unsafeStep) { | ||
Map<String, Phase> phases = new HashMap<>(); | ||
if (safeStep != null) { | ||
|
@@ -1002,6 +1063,56 @@ public void testCanUpdatePolicyIndexInUnsafe() { | |
assertFalse(canUpdatePolicy); | ||
} | ||
|
||
public void testCanUpdatePolicyIndexInUnsafeActionUnchanged() { | ||
String indexName = randomAlphaOfLength(10); | ||
String oldPolicyName = "old_policy"; | ||
String newPolicyName = "new_policy"; | ||
StepKey currentStep = new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)); | ||
LifecyclePolicy oldPolicy = createPolicy(oldPolicyName, null, currentStep); | ||
LifecyclePolicy newPolicy = createPolicy(newPolicyName, | ||
new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)), currentStep); | ||
Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, oldPolicyName) | ||
.put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) | ||
.put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) | ||
.put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); | ||
List<LifecyclePolicyMetadata> policyMetadatas = new ArrayList<>(); | ||
policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); | ||
ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); | ||
|
||
boolean canUpdatePolicy = IndexLifecycleRunner.canUpdatePolicy(oldPolicyName, newPolicy, clusterState); | ||
|
||
assertTrue(canUpdatePolicy); | ||
} | ||
|
||
public void testCanUpdatePolicyIndexInUnsafeActionChanged() { | ||
String indexName = randomAlphaOfLength(10); | ||
String oldPolicyName = "old_policy"; | ||
String newPolicyName = "new_policy"; | ||
StepKey currentStep = new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)); | ||
LifecyclePolicy oldPolicy = createPolicy(oldPolicyName, null, currentStep); | ||
|
||
// change the current action so its not equal to the old one by adding a step | ||
Map<String, Phase> phases = new HashMap<>(); | ||
Map<String, LifecycleAction> actions = new HashMap<>(); | ||
MockAction unsafeAction = new MockAction(Collections.singletonList(new MockStep(currentStep, null)), false); | ||
actions.put(unsafeAction.getWriteableName(), unsafeAction); | ||
Phase phase = new Phase(currentStep.getPhase(), TimeValue.timeValueMillis(0), actions); | ||
phases.put(phase.getName(), phase); | ||
LifecyclePolicy newPolicy = new LifecyclePolicy(TestLifecycleType.INSTANCE, newPolicyName, phases); | ||
|
||
Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, oldPolicyName) | ||
.put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) | ||
.put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) | ||
.put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); | ||
List<LifecyclePolicyMetadata> policyMetadatas = new ArrayList<>(); | ||
policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); | ||
ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); | ||
|
||
boolean canUpdatePolicy = IndexLifecycleRunner.canUpdatePolicy(oldPolicyName, newPolicy, clusterState); | ||
|
||
assertFalse(canUpdatePolicy); | ||
} | ||
|
||
public void testCanUpdatePolicyIndexNotManaged() { | ||
String indexName = randomAlphaOfLength(10); | ||
String oldPolicyName = "old_policy"; | ||
|
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.
since
getActionFromPolicy
can, theoretically, returnnull
, would it be safer to switch these around toThere 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 but if
currentAction
isnull
then we have a bug because we should never be in a situation where the current step doesn't exist in the current policy. If this does happen I would rather not silently fail returning a value. If this does not seem very friendly to you I can add a null check forcurrentAction
which throws andIllegalStateException
instead?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.
I suppose that would make things super clear, but you're right, it would be a bug... so tossing a stacktrace vs. an illegalstateexception is almost the same thing 😄. all good