|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.core.ilm; |
| 9 | + |
| 10 | +import org.apache.logging.log4j.LogManager; |
| 11 | +import org.apache.logging.log4j.Logger; |
| 12 | +import org.elasticsearch.cluster.ClusterState; |
| 13 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 14 | +import org.elasticsearch.common.Nullable; |
| 15 | +import org.elasticsearch.common.unit.TimeValue; |
| 16 | +import org.elasticsearch.index.Index; |
| 17 | +import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo; |
| 18 | + |
| 19 | +import java.time.Clock; |
| 20 | +import java.util.Locale; |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 23 | + |
| 24 | +import static org.elasticsearch.xpack.core.ilm.LifecycleExecutionState.fromIndexMetadata; |
| 25 | + |
| 26 | +/** |
| 27 | + * This step wraps an {@link ClusterStateWaitStep} in order to be able to manipulate what the next step will be, depending on the result of |
| 28 | + * the wrapped {@link ClusterStateWaitStep}. |
| 29 | + * <p> |
| 30 | + * If the action response is complete, the {@link ClusterStateWaitUntilThresholdStep}'s nextStepKey will be the nextStepKey of the |
| 31 | + * wrapped action. When the threshold level is surpassed, if the underlying step's condition was not met, the nextStepKey will be changed to |
| 32 | + * the provided {@link #nextKeyOnThresholdBreach} and this step will stop waiting. |
| 33 | + * |
| 34 | + * Failures encountered whilst executing the wrapped action will be propagated directly. |
| 35 | + */ |
| 36 | +public class ClusterStateWaitUntilThresholdStep extends ClusterStateWaitStep { |
| 37 | + |
| 38 | + private static final Logger logger = LogManager.getLogger(ClusterStateWaitUntilThresholdStep.class); |
| 39 | + |
| 40 | + private final ClusterStateWaitStep stepToExecute; |
| 41 | + private final StepKey nextKeyOnThresholdBreach; |
| 42 | + private final AtomicBoolean thresholdPassed = new AtomicBoolean(false); |
| 43 | + |
| 44 | + public ClusterStateWaitUntilThresholdStep(ClusterStateWaitStep stepToExecute, StepKey nextKeyOnThresholdBreach) { |
| 45 | + super(stepToExecute.getKey(), stepToExecute.getNextStepKey()); |
| 46 | + this.stepToExecute = stepToExecute; |
| 47 | + this.nextKeyOnThresholdBreach = nextKeyOnThresholdBreach; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public boolean isRetryable() { |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Result isConditionMet(Index index, ClusterState clusterState) { |
| 57 | + IndexMetadata idxMeta = clusterState.metadata().index(index); |
| 58 | + if (idxMeta == null) { |
| 59 | + // Index must have been since deleted, ignore it |
| 60 | + logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", |
| 61 | + getKey().getAction(), index.getName()); |
| 62 | + return new Result(false, null); |
| 63 | + } |
| 64 | + |
| 65 | + Result stepResult = stepToExecute.isConditionMet(index, clusterState); |
| 66 | + |
| 67 | + if (stepResult.isComplete() == false) { |
| 68 | + // checking the threshold after we execute the step to make sure we execute the wrapped step at least once (because time is a |
| 69 | + // wonderful thing) |
| 70 | + TimeValue retryThreshold = LifecycleSettings.LIFECYCLE_STEP_WAIT_TIME_THRESHOLD_SETTING.get(idxMeta.getSettings()); |
| 71 | + LifecycleExecutionState lifecycleState = fromIndexMetadata(idxMeta); |
| 72 | + if (waitedMoreThanThresholdLevel(retryThreshold, lifecycleState, Clock.systemUTC())) { |
| 73 | + // we retried this step enough, next step will be the configured to {@code nextKeyOnThresholdBreach} |
| 74 | + thresholdPassed.set(true); |
| 75 | + |
| 76 | + String message = String.format(Locale.ROOT, "[%s] lifecycle step, as part of [%s] action, for index [%s] executed for" + |
| 77 | + " more than [%s]. Abandoning execution and moving to the next fallback step [%s]", |
| 78 | + getKey().getName(), getKey().getAction(), idxMeta.getIndex().getName(), retryThreshold, |
| 79 | + nextKeyOnThresholdBreach); |
| 80 | + logger.debug(message); |
| 81 | + |
| 82 | + return new Result(true, new SingleMessageFieldInfo(message)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return stepResult; |
| 87 | + } |
| 88 | + |
| 89 | + static boolean waitedMoreThanThresholdLevel(@Nullable TimeValue retryThreshold, LifecycleExecutionState lifecycleState, Clock clock) { |
| 90 | + assert lifecycleState.getStepTime() != null : "lifecycle state [" + lifecycleState + "] does not have the step time set"; |
| 91 | + if (retryThreshold != null) { |
| 92 | + // return true if the threshold was surpassed and false otherwise |
| 93 | + return (lifecycleState.getStepTime() + retryThreshold.millis()) < clock.millis(); |
| 94 | + } |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public StepKey getNextStepKey() { |
| 100 | + if (thresholdPassed.get()) { |
| 101 | + return nextKeyOnThresholdBreach; |
| 102 | + } else { |
| 103 | + return super.getNextStepKey(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Represents the {@link ClusterStateWaitStep} that's wrapped by this branching step. |
| 109 | + */ |
| 110 | + ClusterStateWaitStep getStepToExecute() { |
| 111 | + return stepToExecute; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * The step key to be reported as the {@link ClusterStateWaitUntilThresholdStep#getNextStepKey()} if the index configured a max wait |
| 116 | + * time using {@link LifecycleSettings#LIFECYCLE_STEP_WAIT_TIME_THRESHOLD_SETTING} and the threshold was passed. |
| 117 | + */ |
| 118 | + StepKey getNextKeyOnThreshold() { |
| 119 | + return nextKeyOnThresholdBreach; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public boolean equals(Object o) { |
| 124 | + if (this == o) { |
| 125 | + return true; |
| 126 | + } |
| 127 | + if (o == null || getClass() != o.getClass()) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + if (super.equals(o) == false) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + ClusterStateWaitUntilThresholdStep that = (ClusterStateWaitUntilThresholdStep) o; |
| 134 | + return super.equals(o) |
| 135 | + && Objects.equals(stepToExecute, that.stepToExecute) |
| 136 | + && Objects.equals(nextKeyOnThresholdBreach, that.nextKeyOnThresholdBreach); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public int hashCode() { |
| 141 | + return Objects.hash(super.hashCode(), stepToExecute, nextKeyOnThresholdBreach); |
| 142 | + } |
| 143 | +} |
0 commit comments