|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.action.tiering; |
| 10 | + |
| 11 | +import org.opensearch.action.ActionRequestValidationException; |
| 12 | +import org.opensearch.action.IndicesRequest; |
| 13 | +import org.opensearch.action.support.IndicesOptions; |
| 14 | +import org.opensearch.action.support.master.AcknowledgedRequest; |
| 15 | +import org.opensearch.common.annotation.ExperimentalApi; |
| 16 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 17 | +import org.opensearch.core.common.io.stream.StreamOutput; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Locale; |
| 22 | +import java.util.Objects; |
| 23 | + |
| 24 | +import static org.opensearch.action.ValidateActions.addValidationError; |
| 25 | + |
| 26 | +/** |
| 27 | + * Represents the tiering request for indices |
| 28 | + * to move to a different tier |
| 29 | + * |
| 30 | + * @opensearch.experimental |
| 31 | + */ |
| 32 | +@ExperimentalApi |
| 33 | +public class TieringIndexRequest extends AcknowledgedRequest<TieringIndexRequest> implements IndicesRequest.Replaceable { |
| 34 | + |
| 35 | + private String[] indices; |
| 36 | + private TieringType tier; |
| 37 | + private IndicesOptions indicesOptions = IndicesOptions.fromOptions(false, false, true, true); |
| 38 | + private boolean waitForCompletion; |
| 39 | + public TieringIndexRequest() { |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public ActionRequestValidationException validate() { |
| 44 | + ActionRequestValidationException validationException = null; |
| 45 | + if (indices == null || indices.length == 0) { |
| 46 | + validationException = addValidationError("Mandatory parameter - indices is missing from the request", validationException); |
| 47 | + } |
| 48 | + if (tier == null) { |
| 49 | + validationException = addValidationError("Mandatory parameter - tier is missing from the request", validationException); |
| 50 | + } |
| 51 | + if (TieringType.HOT.equals(tier)) { |
| 52 | + validationException = addValidationError("The specified tiering to hot is not supported yet", validationException); |
| 53 | + } |
| 54 | + return validationException; |
| 55 | + } |
| 56 | + |
| 57 | + public TieringIndexRequest(String tier, String... indices) { |
| 58 | + this.tier = TieringType.fromString(tier); |
| 59 | + this.indices = indices; |
| 60 | + } |
| 61 | + |
| 62 | + public TieringIndexRequest(StreamInput in) throws IOException { |
| 63 | + super(in); |
| 64 | + indices = in.readStringArray(); |
| 65 | + tier = TieringType.fromString(in.readString()); |
| 66 | + indicesOptions = IndicesOptions.readIndicesOptions(in); |
| 67 | + waitForCompletion = in.readBoolean(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void writeTo(StreamOutput out) throws IOException { |
| 72 | + super.writeTo(out); |
| 73 | + out.writeStringArray(indices); |
| 74 | + out.writeString(tier.value()); |
| 75 | + indicesOptions.writeIndicesOptions(out); |
| 76 | + out.writeBoolean(waitForCompletion); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public String[] indices() { |
| 81 | + return indices; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public IndicesOptions indicesOptions() { |
| 86 | + return indicesOptions; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public TieringIndexRequest indices(String... indices) { |
| 91 | + this.indices = indices; |
| 92 | + return this; |
| 93 | + } |
| 94 | + |
| 95 | + public TieringIndexRequest indicesOptions(IndicesOptions indicesOptions) { |
| 96 | + this.indicesOptions = indicesOptions; |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * If this parameter is set to true the operation will wait for completion of tiering process before returning. |
| 102 | + * |
| 103 | + * @param waitForCompletion if true the operation will wait for completion |
| 104 | + * @return this request |
| 105 | + */ |
| 106 | + public TieringIndexRequest waitForCompletion(boolean waitForCompletion) { |
| 107 | + this.waitForCompletion = waitForCompletion; |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns wait for completion setting |
| 113 | + * |
| 114 | + * @return true if the operation will wait for completion |
| 115 | + */ |
| 116 | + public boolean waitForCompletion() { |
| 117 | + return waitForCompletion; |
| 118 | + } |
| 119 | + |
| 120 | + public TieringType tier() { |
| 121 | + return tier; |
| 122 | + } |
| 123 | + |
| 124 | + public TieringIndexRequest tier(TieringType tier) { |
| 125 | + this.tier = tier; |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public boolean equals(Object o) { |
| 131 | + if (this == o) { |
| 132 | + return true; |
| 133 | + } |
| 134 | + if (o == null || getClass() != o.getClass()) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + TieringIndexRequest that = (TieringIndexRequest) o; |
| 138 | + return clusterManagerNodeTimeout.equals(that.clusterManagerNodeTimeout) |
| 139 | + && timeout.equals(that.timeout) |
| 140 | + && Objects.equals(indicesOptions, that.indicesOptions) |
| 141 | + && Arrays.equals(indices, that.indices) |
| 142 | + && tier.equals(that.tier) |
| 143 | + && waitForCompletion == that.waitForCompletion; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public int hashCode() { |
| 148 | + return Objects.hash(clusterManagerNodeTimeout, timeout, indicesOptions, waitForCompletion, Arrays.hashCode(indices)); |
| 149 | + } |
| 150 | + |
| 151 | + @ExperimentalApi |
| 152 | + public enum TieringType { |
| 153 | + HOT, |
| 154 | + WARM; |
| 155 | + |
| 156 | + public static TieringType fromString(String name) { |
| 157 | + String upperCase = name.trim().toUpperCase(Locale.ROOT); |
| 158 | + if (HOT.name().equals(upperCase)) { |
| 159 | + return HOT; |
| 160 | + } |
| 161 | + if (WARM.name().equals(upperCase)) { |
| 162 | + return WARM; |
| 163 | + } |
| 164 | + throw new IllegalArgumentException("Tiering type [" + name + "] is not supported. Supported types are " + HOT + " and " + WARM); |
| 165 | + } |
| 166 | + |
| 167 | + public String value() { |
| 168 | + return name().toLowerCase(Locale.ROOT); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments