|
| 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.cluster.node.usage; |
| 11 | + |
| 12 | +import org.elasticsearch.action.ActionType; |
| 13 | +import org.elasticsearch.action.FailedNodeException; |
| 14 | +import org.elasticsearch.action.support.nodes.BaseNodeResponse; |
| 15 | +import org.elasticsearch.action.support.nodes.BaseNodesRequest; |
| 16 | +import org.elasticsearch.action.support.nodes.BaseNodesResponse; |
| 17 | +import org.elasticsearch.cluster.ClusterName; |
| 18 | +import org.elasticsearch.cluster.NodeUsageStatsForThreadPools; |
| 19 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 20 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 21 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 22 | +import org.elasticsearch.transport.AbstractTransportRequest; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Objects; |
| 29 | + |
| 30 | +/** |
| 31 | + * |
| 32 | + */ |
| 33 | +public class NodeUsageStatsForThreadPoolsAction extends ActionType<NodeUsageStatsForThreadPoolsAction.Response> { |
| 34 | + public static final NodeUsageStatsForThreadPoolsAction INSTANCE = new NodeUsageStatsForThreadPoolsAction(); |
| 35 | + public static final String NAME = "internal:monitor/thread_pool/stats"; |
| 36 | + |
| 37 | + public NodeUsageStatsForThreadPoolsAction() { |
| 38 | + super(NAME); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * The request specifying to which data nodes individual {@link NodeRequest} requests should be sent. |
| 43 | + */ |
| 44 | + public static class Request extends BaseNodesRequest { |
| 45 | + public Request() { |
| 46 | + super((String[]) null); // send all nodes a request by specifying `null` |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean equals(Object o) { |
| 51 | + if (this == o) return true; |
| 52 | + if (o == null || getClass() != o.getClass()) return false; |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public int hashCode() { |
| 58 | + // The class doesn't have any members at the moment so return the same hash code |
| 59 | + return Objects.hash(NAME); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Request sent to the data nodes. No additional parameters to send in the node-specific request. |
| 65 | + */ |
| 66 | + public static class NodeRequest extends AbstractTransportRequest { |
| 67 | + public NodeRequest(StreamInput in) throws IOException { |
| 68 | + super(in); |
| 69 | + } |
| 70 | + |
| 71 | + public NodeRequest() {} |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * The collection of {@link NodeUsageStatsForThreadPools} responses from all the data nodes. |
| 76 | + */ |
| 77 | + public static class Response extends BaseNodesResponse<NodeResponse> { |
| 78 | + |
| 79 | + protected Response(StreamInput in) throws IOException { |
| 80 | + super(in); |
| 81 | + } |
| 82 | + |
| 83 | + public Response( |
| 84 | + ClusterName clusterName, |
| 85 | + List<NodeUsageStatsForThreadPoolsAction.NodeResponse> nodeResponses, |
| 86 | + List<FailedNodeException> nodeFailures |
| 87 | + ) { |
| 88 | + super(clusterName, nodeResponses, nodeFailures); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Combines the responses from each node that was called into a single map (by node ID) for the final {@link Response}. |
| 93 | + */ |
| 94 | + public Map<String, NodeUsageStatsForThreadPools> getAllNodeUsageStatsForThreadPools() { |
| 95 | + Map<String, NodeUsageStatsForThreadPools> allNodeUsageStatsForThreadPools = new HashMap<>(); |
| 96 | + for (NodeUsageStatsForThreadPoolsAction.NodeResponse nodeResponse : getNodes()) { |
| 97 | + // NOMERGE: Is the nodeID in NodeUsageStatsForThreadPools redundant? What is it useful for? If not, remove? |
| 98 | + allNodeUsageStatsForThreadPools.put( |
| 99 | + nodeResponse.getNodeUsageStatsForThreadPools().nodeId(), |
| 100 | + nodeResponse.getNodeUsageStatsForThreadPools() |
| 101 | + ); |
| 102 | + } |
| 103 | + return allNodeUsageStatsForThreadPools; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + protected void writeNodesTo(StreamOutput out, List<NodeResponse> nodeResponses) throws IOException { |
| 108 | + out.writeCollection(nodeResponses); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + protected List<NodeResponse> readNodesFrom(StreamInput in) throws IOException { |
| 113 | + return in.readCollectionAsList(NodeUsageStatsForThreadPoolsAction.NodeResponse::new); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public String toString() { |
| 118 | + return "NodeUsageStatsForThreadPoolsAction.Response{" + "NodeUsageStatsForThreadPoolsAction.NodeResponse=" + getNodes() + "}"; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * The {@link NodeUsageStatsForThreadPools} response from a single data node. |
| 124 | + */ |
| 125 | + public static class NodeResponse extends BaseNodeResponse { |
| 126 | + private final NodeUsageStatsForThreadPools nodeUsageStatsForThreadPools; |
| 127 | + |
| 128 | + protected NodeResponse(StreamInput in, DiscoveryNode node) throws IOException { |
| 129 | + super(in, node); |
| 130 | + this.nodeUsageStatsForThreadPools = new NodeUsageStatsForThreadPools(in); |
| 131 | + } |
| 132 | + |
| 133 | + public NodeResponse(DiscoveryNode node, NodeUsageStatsForThreadPools nodeUsageStatsForThreadPools) { |
| 134 | + super(node); |
| 135 | + this.nodeUsageStatsForThreadPools = nodeUsageStatsForThreadPools; |
| 136 | + } |
| 137 | + |
| 138 | + public NodeResponse(StreamInput in) throws IOException { |
| 139 | + super(in); |
| 140 | + this.nodeUsageStatsForThreadPools = new NodeUsageStatsForThreadPools(in); |
| 141 | + } |
| 142 | + |
| 143 | + public NodeUsageStatsForThreadPools getNodeUsageStatsForThreadPools() { |
| 144 | + return nodeUsageStatsForThreadPools; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void writeTo(StreamOutput out) throws IOException { |
| 149 | + super.writeTo(out); |
| 150 | + nodeUsageStatsForThreadPools.writeTo(out); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public boolean equals(Object o) { |
| 155 | + if (this == o) return true; |
| 156 | + if (o == null || getClass() != o.getClass()) return false; |
| 157 | + NodeUsageStatsForThreadPoolsAction.NodeResponse that = (NodeUsageStatsForThreadPoolsAction.NodeResponse) o; |
| 158 | + return Objects.equals(this.getNode(), that.getNode()) |
| 159 | + && Objects.equals(this.nodeUsageStatsForThreadPools, that.nodeUsageStatsForThreadPools); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public int hashCode() { |
| 164 | + return Objects.hash(nodeUsageStatsForThreadPools, getNode()); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public String toString() { |
| 169 | + return "NodeUsageStatsForThreadPoolsAction.NodeResponse{" |
| 170 | + + "nodeId=" |
| 171 | + + getNode().getId() |
| 172 | + + ", nodeUsageStatsForThreadPools=" |
| 173 | + + nodeUsageStatsForThreadPools |
| 174 | + + "}"; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments