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