|
| 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.scaleToZero; |
| 10 | + |
| 11 | +import org.opensearch.core.action.ActionResponse; |
| 12 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 13 | +import org.opensearch.core.common.io.stream.StreamOutput; |
| 14 | +import org.opensearch.core.xcontent.ToXContent; |
| 15 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collection; |
| 20 | + |
| 21 | +public class PreScaleSyncResponse extends ActionResponse implements ToXContent { |
| 22 | + private final Collection<NodePreScaleSyncResponse> nodeResponses; |
| 23 | + private final String failureReason; |
| 24 | + private final boolean hasFailures; |
| 25 | + |
| 26 | + public PreScaleSyncResponse(Collection<NodePreScaleSyncResponse> responses) { |
| 27 | + this.nodeResponses = responses; |
| 28 | + this.hasFailures = responses.stream() |
| 29 | + .anyMatch(r -> r.getShardResponses().stream().anyMatch(s -> s.hasUncommittedOperations() || s.needsSync())); |
| 30 | + this.failureReason = buildFailureReason(); |
| 31 | + } |
| 32 | + |
| 33 | + public PreScaleSyncResponse(StreamInput in) throws IOException { |
| 34 | + this.nodeResponses = in.readList(NodePreScaleSyncResponse::new); |
| 35 | + this.hasFailures = in.readBoolean(); |
| 36 | + this.failureReason = in.readOptionalString(); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void writeTo(StreamOutput out) throws IOException { |
| 41 | + out.writeList(new ArrayList<>(nodeResponses)); // Convert Collection to List |
| 42 | + out.writeBoolean(hasFailures); |
| 43 | + out.writeOptionalString(failureReason); |
| 44 | + } |
| 45 | + |
| 46 | + public boolean hasFailures() { |
| 47 | + return hasFailures; |
| 48 | + } |
| 49 | + |
| 50 | + public String getFailureReason() { |
| 51 | + return failureReason; |
| 52 | + } |
| 53 | + |
| 54 | + private String buildFailureReason() { |
| 55 | + if (!hasFailures) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + StringBuilder reason = new StringBuilder(); |
| 59 | + for (NodePreScaleSyncResponse nodeResponse : nodeResponses) { |
| 60 | + for (ShardPreScaleSyncResponse shardResponse : nodeResponse.getShardResponses()) { |
| 61 | + if (shardResponse.hasUncommittedOperations() || shardResponse.needsSync()) { |
| 62 | + reason.append("Shard ") |
| 63 | + .append(shardResponse.getShardId()) |
| 64 | + .append(" on node ") |
| 65 | + .append(nodeResponse.getNode()) |
| 66 | + .append(": "); |
| 67 | + if (shardResponse.hasUncommittedOperations()) { |
| 68 | + reason.append("has uncommitted operations "); |
| 69 | + } |
| 70 | + if (shardResponse.needsSync()) { |
| 71 | + reason.append("needs sync "); |
| 72 | + } |
| 73 | + reason.append("; "); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + return reason.toString(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { |
| 82 | + builder.startObject(); |
| 83 | + builder.field("has_failures", hasFailures); |
| 84 | + if (failureReason != null) { |
| 85 | + builder.field("failure_reason", failureReason); |
| 86 | + } |
| 87 | + builder.endObject(); |
| 88 | + return builder; |
| 89 | + } |
| 90 | +} |
0 commit comments