|
| 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.admin.indices.tiering; |
| 10 | + |
| 11 | +import org.opensearch.action.support.master.AcknowledgedResponse; |
| 12 | +import org.opensearch.common.annotation.ExperimentalApi; |
| 13 | +import org.opensearch.core.common.Strings; |
| 14 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 15 | +import org.opensearch.core.common.io.stream.StreamOutput; |
| 16 | +import org.opensearch.core.common.io.stream.Writeable; |
| 17 | +import org.opensearch.core.xcontent.MediaTypeRegistry; |
| 18 | +import org.opensearch.core.xcontent.ToXContentFragment; |
| 19 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.Comparator; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Objects; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +/** |
| 29 | + * Response object for an {@link TieringIndexRequest} which is sent to client after the initial verification of the request |
| 30 | + * by the backend service. The format of the response object will be as below: |
| 31 | + * |
| 32 | + * { |
| 33 | + * "acknowledged": true/false, |
| 34 | + * "failed_indices": [ |
| 35 | + * { |
| 36 | + * "index": "index1", |
| 37 | + * "error": "Low disk threshold watermark breached" |
| 38 | + * }, |
| 39 | + * { |
| 40 | + * "index": "index2", |
| 41 | + * "error": "Index is not a remote store backed index" |
| 42 | + * } |
| 43 | + * ] |
| 44 | + * } |
| 45 | + * |
| 46 | + * @opensearch.experimental |
| 47 | + */ |
| 48 | +@ExperimentalApi |
| 49 | +public class HotToWarmTieringResponse extends AcknowledgedResponse { |
| 50 | + |
| 51 | + private final List<IndexResult> failedIndices; |
| 52 | + |
| 53 | + public HotToWarmTieringResponse(boolean acknowledged) { |
| 54 | + super(acknowledged); |
| 55 | + this.failedIndices = Collections.emptyList(); |
| 56 | + } |
| 57 | + |
| 58 | + public HotToWarmTieringResponse(boolean acknowledged, List<IndexResult> indicesResults) { |
| 59 | + super(acknowledged); |
| 60 | + this.failedIndices = (indicesResults == null) |
| 61 | + ? Collections.emptyList() |
| 62 | + : indicesResults.stream().sorted(Comparator.comparing(IndexResult::getIndex)).collect(Collectors.toList()); |
| 63 | + } |
| 64 | + |
| 65 | + public HotToWarmTieringResponse(StreamInput in) throws IOException { |
| 66 | + super(in); |
| 67 | + failedIndices = Collections.unmodifiableList(in.readList(IndexResult::new)); |
| 68 | + } |
| 69 | + |
| 70 | + public List<IndexResult> getFailedIndices() { |
| 71 | + return this.failedIndices; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void writeTo(StreamOutput out) throws IOException { |
| 76 | + super.writeTo(out); |
| 77 | + out.writeList(this.failedIndices); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected void addCustomFields(XContentBuilder builder, Params params) throws IOException { |
| 82 | + super.addCustomFields(builder, params); |
| 83 | + builder.startArray("failed_indices"); |
| 84 | + |
| 85 | + for (IndexResult failedIndex : failedIndices) { |
| 86 | + failedIndex.toXContent(builder, params); |
| 87 | + } |
| 88 | + builder.endArray(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public String toString() { |
| 93 | + return Strings.toString(MediaTypeRegistry.JSON, this); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Inner class to represent the result of a failed index for tiering. |
| 98 | + * @opensearch.experimental |
| 99 | + */ |
| 100 | + @ExperimentalApi |
| 101 | + public static class IndexResult implements Writeable, ToXContentFragment { |
| 102 | + private final String index; |
| 103 | + private final String failureReason; |
| 104 | + |
| 105 | + public IndexResult(String index, String failureReason) { |
| 106 | + this.index = index; |
| 107 | + this.failureReason = failureReason; |
| 108 | + } |
| 109 | + |
| 110 | + IndexResult(StreamInput in) throws IOException { |
| 111 | + this.index = in.readString(); |
| 112 | + this.failureReason = in.readString(); |
| 113 | + } |
| 114 | + |
| 115 | + public String getIndex() { |
| 116 | + return index; |
| 117 | + } |
| 118 | + |
| 119 | + public String getFailureReason() { |
| 120 | + return failureReason; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void writeTo(StreamOutput out) throws IOException { |
| 125 | + out.writeString(index); |
| 126 | + out.writeString(failureReason); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 131 | + builder.startObject(); |
| 132 | + builder.field("index", index); |
| 133 | + builder.field("error", failureReason); |
| 134 | + return builder.endObject(); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public boolean equals(Object o) { |
| 139 | + if (this == o) return true; |
| 140 | + if (o == null || getClass() != o.getClass()) return false; |
| 141 | + IndexResult that = (IndexResult) o; |
| 142 | + return Objects.equals(index, that.index) && Objects.equals(failureReason, that.failureReason); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public int hashCode() { |
| 147 | + int result = Objects.hashCode(index); |
| 148 | + result = 31 * result + Objects.hashCode(failureReason); |
| 149 | + return result; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public String toString() { |
| 154 | + return Strings.toString(MediaTypeRegistry.JSON, this); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments