|
| 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 | +package org.elasticsearch.xpack.core.ilm; |
| 7 | + |
| 8 | +import org.elasticsearch.client.Client; |
| 9 | +import org.elasticsearch.common.Nullable; |
| 10 | +import org.elasticsearch.common.ParseField; |
| 11 | +import org.elasticsearch.common.Strings; |
| 12 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 13 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 14 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 15 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 16 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 17 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 18 | +import org.elasticsearch.xpack.core.ilm.Step.StepKey; |
| 19 | +import org.elasticsearch.xpack.core.rollup.RollupActionConfig; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Objects; |
| 24 | + |
| 25 | +/** |
| 26 | + * A {@link LifecycleAction} which calls {@link org.elasticsearch.xpack.core.rollup.action.RollupAction} on an index |
| 27 | + */ |
| 28 | +public class RollupILMAction implements LifecycleAction { |
| 29 | + public static final String NAME = "rollup"; |
| 30 | + |
| 31 | + private static final ParseField CONFIG_FIELD = new ParseField("config"); |
| 32 | + private static final ParseField POLICY_FIELD = new ParseField("rollup_policy"); |
| 33 | + |
| 34 | + @SuppressWarnings("unchecked") |
| 35 | + private static final ConstructingObjectParser<RollupILMAction, Void> PARSER = new ConstructingObjectParser<>(NAME, |
| 36 | + a -> new RollupILMAction((RollupActionConfig) a[0], (String) a[1])); |
| 37 | + |
| 38 | + private final RollupActionConfig config; |
| 39 | + private final String rollupPolicy; |
| 40 | + |
| 41 | + static { |
| 42 | + PARSER.declareField(ConstructingObjectParser.constructorArg(), |
| 43 | + (p, c) -> RollupActionConfig.fromXContent(p), CONFIG_FIELD, ObjectParser.ValueType.OBJECT); |
| 44 | + PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), POLICY_FIELD); |
| 45 | + } |
| 46 | + |
| 47 | + public static RollupILMAction parse(XContentParser parser) { |
| 48 | + return PARSER.apply(parser, null); |
| 49 | + } |
| 50 | + |
| 51 | + public RollupILMAction(RollupActionConfig config, @Nullable String rollupPolicy) { |
| 52 | + this.config = config; |
| 53 | + this.rollupPolicy = rollupPolicy; |
| 54 | + } |
| 55 | + |
| 56 | + public RollupILMAction(StreamInput in) throws IOException { |
| 57 | + this(new RollupActionConfig(in), in.readOptionalString()); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String getWriteableName() { |
| 62 | + return NAME; |
| 63 | + } |
| 64 | + |
| 65 | + RollupActionConfig config() { |
| 66 | + return config; |
| 67 | + } |
| 68 | + |
| 69 | + String rollupPolicy() { |
| 70 | + return rollupPolicy; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 75 | + builder.startObject(); |
| 76 | + builder.field(CONFIG_FIELD.getPreferredName(), config); |
| 77 | + if (rollupPolicy != null) { |
| 78 | + builder.field(POLICY_FIELD.getPreferredName(), rollupPolicy); |
| 79 | + } |
| 80 | + builder.endObject(); |
| 81 | + return builder; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void writeTo(StreamOutput out) throws IOException { |
| 86 | + config.writeTo(out); |
| 87 | + out.writeOptionalString(rollupPolicy); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean isSafeAction() { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public List<Step> toSteps(Client client, String phase, StepKey nextStepKey) { |
| 97 | + StepKey checkNotWriteIndex = new StepKey(phase, NAME, CheckNotDataStreamWriteIndexStep.NAME); |
| 98 | + StepKey readOnlyKey = new StepKey(phase, NAME, ReadOnlyStep.NAME); |
| 99 | + StepKey rollupKey = new StepKey(phase, NAME, NAME); |
| 100 | + CheckNotDataStreamWriteIndexStep checkNotWriteIndexStep = new CheckNotDataStreamWriteIndexStep(checkNotWriteIndex, |
| 101 | + readOnlyKey); |
| 102 | + ReadOnlyStep readOnlyStep = new ReadOnlyStep(readOnlyKey, rollupKey, client); |
| 103 | + if (rollupPolicy == null) { |
| 104 | + Step rollupStep = new RollupStep(rollupKey, nextStepKey, client, config); |
| 105 | + return List.of(checkNotWriteIndexStep, readOnlyStep, rollupStep); |
| 106 | + } else { |
| 107 | + StepKey updateRollupIndexPolicyStepKey = new StepKey(phase, NAME, UpdateRollupIndexPolicyStep.NAME); |
| 108 | + Step rollupStep = new RollupStep(rollupKey, updateRollupIndexPolicyStepKey, client, config); |
| 109 | + Step updateRollupIndexPolicyStep = new UpdateRollupIndexPolicyStep(updateRollupIndexPolicyStepKey, nextStepKey, |
| 110 | + client, rollupPolicy); |
| 111 | + return List.of(checkNotWriteIndexStep, readOnlyStep, rollupStep, updateRollupIndexPolicyStep); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public boolean equals(Object o) { |
| 117 | + if (this == o) return true; |
| 118 | + if (o == null || getClass() != o.getClass()) return false; |
| 119 | + |
| 120 | + RollupILMAction that = (RollupILMAction) o; |
| 121 | + |
| 122 | + return Objects.equals(this.config, that.config) |
| 123 | + && Objects.equals(this.rollupPolicy, that.rollupPolicy); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public int hashCode() { |
| 128 | + return Objects.hash(config, rollupPolicy); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public String toString() { |
| 133 | + return Strings.toString(this); |
| 134 | + } |
| 135 | +} |
0 commit comments