|
| 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 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.action.admin.indices.sampling; |
| 11 | + |
| 12 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 13 | +import org.elasticsearch.action.ActionType; |
| 14 | +import org.elasticsearch.action.FailedNodeException; |
| 15 | +import org.elasticsearch.action.IndicesRequest; |
| 16 | +import org.elasticsearch.action.support.IndicesOptions; |
| 17 | +import org.elasticsearch.action.support.nodes.BaseNodeResponse; |
| 18 | +import org.elasticsearch.action.support.nodes.BaseNodesRequest; |
| 19 | +import org.elasticsearch.action.support.nodes.BaseNodesResponse; |
| 20 | +import org.elasticsearch.cluster.ClusterName; |
| 21 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 22 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 23 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 24 | +import org.elasticsearch.common.io.stream.Writeable; |
| 25 | +import org.elasticsearch.common.xcontent.ChunkedToXContent; |
| 26 | +import org.elasticsearch.ingest.SamplingService; |
| 27 | +import org.elasticsearch.tasks.CancellableTask; |
| 28 | +import org.elasticsearch.tasks.Task; |
| 29 | +import org.elasticsearch.tasks.TaskId; |
| 30 | +import org.elasticsearch.transport.AbstractTransportRequest; |
| 31 | +import org.elasticsearch.xcontent.ToXContent; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.Iterator; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.Objects; |
| 38 | + |
| 39 | +import static org.elasticsearch.common.xcontent.ChunkedToXContentHelper.chunk; |
| 40 | + |
| 41 | +public class GetSampleStatsAction extends ActionType<GetSampleStatsAction.Response> { |
| 42 | + |
| 43 | + public static final GetSampleStatsAction INSTANCE = new GetSampleStatsAction(); |
| 44 | + public static final String NAME = "indices:admin/sample/stats"; |
| 45 | + |
| 46 | + private GetSampleStatsAction() { |
| 47 | + super(NAME); |
| 48 | + } |
| 49 | + |
| 50 | + public static class Request extends BaseNodesRequest implements IndicesRequest.Replaceable { |
| 51 | + private String indexName; |
| 52 | + |
| 53 | + public Request(String indexName) { |
| 54 | + super((String[]) null); |
| 55 | + this.indexName = indexName; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) { |
| 60 | + return new CancellableTask(id, type, action, "get sample stats", parentTaskId, headers); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public ActionRequestValidationException validate() { |
| 65 | + if (this.indexName.contains("*")) { |
| 66 | + return (ActionRequestValidationException) new ActionRequestValidationException().addValidationError( |
| 67 | + "Wildcards are not supported, but found [" + indexName + "]" |
| 68 | + ); |
| 69 | + } |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public IndicesRequest indices(String... indices) { |
| 75 | + assert indices.length == 1 : "GetSampleStatsAction only supports a single index name"; |
| 76 | + this.indexName = indices[0]; |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public String[] indices() { |
| 82 | + return new String[] { indexName }; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public IndicesOptions indicesOptions() { |
| 87 | + return IndicesOptions.STRICT_SINGLE_INDEX_NO_EXPAND_FORBID_CLOSED_ALLOW_SELECTORS; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public static class NodeRequest extends AbstractTransportRequest implements IndicesRequest { |
| 92 | + private final String indexName; |
| 93 | + |
| 94 | + public NodeRequest(String indexName) { |
| 95 | + this.indexName = indexName; |
| 96 | + } |
| 97 | + |
| 98 | + public NodeRequest(StreamInput in) throws IOException { |
| 99 | + super(in); |
| 100 | + this.indexName = in.readString(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void writeTo(StreamOutput out) throws IOException { |
| 105 | + super.writeTo(out); |
| 106 | + out.writeString(indexName); |
| 107 | + } |
| 108 | + |
| 109 | + public String getIndexName() { |
| 110 | + return indexName; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String[] indices() { |
| 115 | + return new String[] { indexName }; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public IndicesOptions indicesOptions() { |
| 120 | + return IndicesOptions.STRICT_SINGLE_INDEX_NO_EXPAND_FORBID_CLOSED_ALLOW_SELECTORS; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public boolean equals(Object o) { |
| 125 | + if (this == o) return true; |
| 126 | + if (o == null || getClass() != o.getClass()) return false; |
| 127 | + NodeRequest other = (NodeRequest) o; |
| 128 | + return Objects.equals(indexName, other.indexName); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public int hashCode() { |
| 133 | + return Objects.hash(indexName); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public static class Response extends BaseNodesResponse<GetSampleStatsAction.NodeResponse> implements Writeable, ChunkedToXContent { |
| 138 | + final int maxSize; |
| 139 | + |
| 140 | + public Response(StreamInput in) throws IOException { |
| 141 | + super(in); |
| 142 | + maxSize = in.readInt(); |
| 143 | + } |
| 144 | + |
| 145 | + public Response( |
| 146 | + ClusterName clusterName, |
| 147 | + List<GetSampleStatsAction.NodeResponse> nodes, |
| 148 | + List<FailedNodeException> failures, |
| 149 | + int maxSize |
| 150 | + ) { |
| 151 | + super(clusterName, nodes, failures); |
| 152 | + this.maxSize = maxSize; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void writeTo(StreamOutput out) throws IOException { |
| 157 | + super.writeTo(out); |
| 158 | + out.writeInt(maxSize); |
| 159 | + } |
| 160 | + |
| 161 | + public SamplingService.SampleStats getSampleStats() { |
| 162 | + SamplingService.SampleStats rawStats = getRawSampleStats(); |
| 163 | + return rawStats.adjustForMaxSize(maxSize); |
| 164 | + } |
| 165 | + |
| 166 | + private SamplingService.SampleStats getRawSampleStats() { |
| 167 | + return getNodes().stream() |
| 168 | + .map(NodeResponse::getSampleStats) |
| 169 | + .filter(Objects::nonNull) |
| 170 | + .reduce(SamplingService.SampleStats::combine) |
| 171 | + .orElse(new SamplingService.SampleStats()); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + protected List<GetSampleStatsAction.NodeResponse> readNodesFrom(StreamInput in) throws IOException { |
| 176 | + return in.readCollectionAsList(GetSampleStatsAction.NodeResponse::new); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + protected void writeNodesTo(StreamOutput out, List<GetSampleStatsAction.NodeResponse> nodes) throws IOException { |
| 181 | + out.writeCollection(nodes); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public boolean equals(Object o) { |
| 186 | + if (this == o) return true; |
| 187 | + if (o == null || getClass() != o.getClass()) return false; |
| 188 | + Response other = (Response) o; |
| 189 | + return Objects.equals(getNodes(), other.getNodes()) && maxSize == other.maxSize; |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public int hashCode() { |
| 194 | + return Objects.hash(getNodes(), maxSize); |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) { |
| 199 | + return chunk(getSampleStats()); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + public static class NodeResponse extends BaseNodeResponse { |
| 204 | + private final SamplingService.SampleStats sampleStats; |
| 205 | + |
| 206 | + protected NodeResponse(StreamInput in) throws IOException { |
| 207 | + super(in); |
| 208 | + sampleStats = new SamplingService.SampleStats(in); |
| 209 | + } |
| 210 | + |
| 211 | + protected NodeResponse(DiscoveryNode node, SamplingService.SampleStats sampleStats) { |
| 212 | + super(node); |
| 213 | + this.sampleStats = sampleStats; |
| 214 | + } |
| 215 | + |
| 216 | + public SamplingService.SampleStats getSampleStats() { |
| 217 | + return sampleStats; |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public void writeTo(StreamOutput out) throws IOException { |
| 222 | + super.writeTo(out); |
| 223 | + sampleStats.writeTo(out); |
| 224 | + } |
| 225 | + |
| 226 | + @Override |
| 227 | + public boolean equals(Object o) { |
| 228 | + if (this == o) return true; |
| 229 | + if (o == null || getClass() != o.getClass()) return false; |
| 230 | + GetSampleStatsAction.NodeResponse other = (GetSampleStatsAction.NodeResponse) o; |
| 231 | + return getNode().equals(other.getNode()) && sampleStats.equals(other.sampleStats); |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + public int hashCode() { |
| 236 | + return Objects.hash(getNode(), sampleStats); |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments