|
| 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 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.core.ilm; |
| 8 | + |
| 9 | +import org.apache.lucene.util.SetOnce; |
| 10 | +import org.elasticsearch.client.Client; |
| 11 | +import org.elasticsearch.cluster.ClusterState; |
| 12 | +import org.elasticsearch.cluster.ClusterStateObserver; |
| 13 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 14 | + |
| 15 | +import java.util.Objects; |
| 16 | + |
| 17 | +/** |
| 18 | + * This step wraps an {@link AsyncActionStep} in order to be able to manipulate what the next step will be, depending on the result of the |
| 19 | + * wrapped {@link AsyncActionStep}. |
| 20 | + * <p> |
| 21 | + * If the action response is complete, the {@link AsyncActionBranchingStep}'s nextStepKey will be the nextStepKey of the wrapped action. If |
| 22 | + * the response is incomplete the nextStepKey will be the provided {@link AsyncActionBranchingStep#nextKeyOnIncompleteResponse}. |
| 23 | + * Failures encountered whilst executing the wrapped action will be propagated directly. |
| 24 | + */ |
| 25 | +public class AsyncActionBranchingStep extends AsyncActionStep { |
| 26 | + private final AsyncActionStep stepToExecute; |
| 27 | + |
| 28 | + private StepKey nextKeyOnIncompleteResponse; |
| 29 | + private SetOnce<Boolean> onResponseResult; |
| 30 | + |
| 31 | + public AsyncActionBranchingStep(AsyncActionStep stepToExecute, StepKey nextKeyOnIncompleteResponse, Client client) { |
| 32 | + // super.nextStepKey is set to null since it is not used by this step |
| 33 | + super(stepToExecute.getKey(), null, client); |
| 34 | + this.stepToExecute = stepToExecute; |
| 35 | + this.nextKeyOnIncompleteResponse = nextKeyOnIncompleteResponse; |
| 36 | + this.onResponseResult = new SetOnce<>(); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public boolean isRetryable() { |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void performAction(IndexMetadata indexMetadata, ClusterState currentClusterState, ClusterStateObserver observer, |
| 46 | + Listener listener) { |
| 47 | + stepToExecute.performAction(indexMetadata, currentClusterState, observer, new Listener() { |
| 48 | + @Override |
| 49 | + public void onResponse(boolean complete) { |
| 50 | + onResponseResult.set(complete); |
| 51 | + listener.onResponse(complete); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onFailure(Exception e) { |
| 56 | + listener.onFailure(e); |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public final StepKey getNextStepKey() { |
| 63 | + if (onResponseResult.get() == null) { |
| 64 | + throw new IllegalStateException("cannot call getNextStepKey before performAction"); |
| 65 | + } |
| 66 | + return onResponseResult.get() ? stepToExecute.getNextStepKey() : nextKeyOnIncompleteResponse; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Represents the {@link AsyncActionStep} that's wrapped by this branching step. |
| 71 | + */ |
| 72 | + AsyncActionStep getStepToExecute() { |
| 73 | + return stepToExecute; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * The step key to be reported as the {@link AsyncActionBranchingStep#getNextStepKey()} if the response of the wrapped |
| 78 | + * {@link AsyncActionBranchingStep#getStepToExecute()} is incomplete. |
| 79 | + */ |
| 80 | + StepKey getNextKeyOnIncompleteResponse() { |
| 81 | + return nextKeyOnIncompleteResponse; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean equals(Object o) { |
| 86 | + if (this == o) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + if (o == null || getClass() != o.getClass()) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + if (!super.equals(o)) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + AsyncActionBranchingStep that = (AsyncActionBranchingStep) o; |
| 96 | + return super.equals(o) |
| 97 | + && Objects.equals(stepToExecute, that.stepToExecute) |
| 98 | + && Objects.equals(nextKeyOnIncompleteResponse, that.nextKeyOnIncompleteResponse); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public int hashCode() { |
| 103 | + return Objects.hash(super.hashCode(), stepToExecute, nextKeyOnIncompleteResponse); |
| 104 | + } |
| 105 | +} |
0 commit comments