|
| 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.monitor.metrics; |
| 11 | + |
| 12 | +import org.elasticsearch.action.ActionType; |
| 13 | +import org.elasticsearch.action.FailedNodeException; |
| 14 | +import org.elasticsearch.action.support.ActionFilters; |
| 15 | +import org.elasticsearch.action.support.nodes.BaseNodeResponse; |
| 16 | +import org.elasticsearch.action.support.nodes.BaseNodesRequest; |
| 17 | +import org.elasticsearch.action.support.nodes.BaseNodesResponse; |
| 18 | +import org.elasticsearch.action.support.nodes.TransportNodesAction; |
| 19 | +import org.elasticsearch.cluster.ClusterName; |
| 20 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 21 | +import org.elasticsearch.cluster.service.ClusterService; |
| 22 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 23 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 24 | +import org.elasticsearch.index.IndexMode; |
| 25 | +import org.elasticsearch.indices.IndicesService; |
| 26 | +import org.elasticsearch.injection.guice.Inject; |
| 27 | +import org.elasticsearch.tasks.Task; |
| 28 | +import org.elasticsearch.threadpool.ThreadPool; |
| 29 | +import org.elasticsearch.transport.TransportRequest; |
| 30 | +import org.elasticsearch.transport.TransportService; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.EnumMap; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | + |
| 37 | +public final class IndexModeStatsActionType extends ActionType<IndexModeStatsActionType.StatsResponse> { |
| 38 | + public static final IndexModeStatsActionType TYPE = new IndexModeStatsActionType(); |
| 39 | + |
| 40 | + private IndexModeStatsActionType() { |
| 41 | + super("cluster:monitor/nodes/index_mode_stats"); |
| 42 | + } |
| 43 | + |
| 44 | + public static final class StatsRequest extends BaseNodesRequest { |
| 45 | + public StatsRequest(String[] nodesIds) { |
| 46 | + super(nodesIds); |
| 47 | + } |
| 48 | + |
| 49 | + public StatsRequest(DiscoveryNode... concreteNodes) { |
| 50 | + super(concreteNodes); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static final class StatsResponse extends BaseNodesResponse<NodeResponse> { |
| 55 | + StatsResponse(ClusterName clusterName, List<NodeResponse> nodes, List<FailedNodeException> failures) { |
| 56 | + super(clusterName, nodes, failures); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void writeTo(StreamOutput out) throws IOException { |
| 61 | + assert false : "must be local"; |
| 62 | + throw new UnsupportedOperationException("must be local"); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected List<NodeResponse> readNodesFrom(StreamInput in) throws IOException { |
| 67 | + assert false : "must be local"; |
| 68 | + throw new UnsupportedOperationException("must be local"); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected void writeNodesTo(StreamOutput out, List<NodeResponse> nodes) throws IOException { |
| 73 | + assert false : "must be local"; |
| 74 | + throw new UnsupportedOperationException("must be local"); |
| 75 | + } |
| 76 | + |
| 77 | + public Map<IndexMode, IndexStats> stats() { |
| 78 | + final Map<IndexMode, IndexStats> stats = new EnumMap<>(IndexMode.class); |
| 79 | + for (IndexMode mode : IndexMode.values()) { |
| 80 | + stats.put(mode, new IndexStats()); |
| 81 | + } |
| 82 | + for (NodeResponse node : getNodes()) { |
| 83 | + for (Map.Entry<IndexMode, IndexStats> e : node.stats.entrySet()) { |
| 84 | + stats.get(e.getKey()).add(e.getValue()); |
| 85 | + } |
| 86 | + } |
| 87 | + return stats; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public static final class NodeRequest extends TransportRequest { |
| 92 | + NodeRequest() { |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + NodeRequest(StreamInput in) throws IOException { |
| 97 | + super(in); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public static class NodeResponse extends BaseNodeResponse { |
| 102 | + private final Map<IndexMode, IndexStats> stats; |
| 103 | + |
| 104 | + NodeResponse(DiscoveryNode node, Map<IndexMode, IndexStats> stats) { |
| 105 | + super(node); |
| 106 | + this.stats = stats; |
| 107 | + } |
| 108 | + |
| 109 | + NodeResponse(StreamInput in, DiscoveryNode node) throws IOException { |
| 110 | + super(in, node); |
| 111 | + stats = in.readMap(IndexMode::readFrom, IndexStats::new); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void writeTo(StreamOutput out) throws IOException { |
| 116 | + super.writeTo(out); |
| 117 | + out.writeMap(stats, (o, m) -> IndexMode.writeTo(m, o), (o, s) -> s.writeTo(o)); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public static class TransportAction extends TransportNodesAction<StatsRequest, StatsResponse, NodeRequest, NodeResponse, Void> { |
| 122 | + private final IndicesService indicesService; |
| 123 | + |
| 124 | + @Inject |
| 125 | + public TransportAction( |
| 126 | + ClusterService clusterService, |
| 127 | + TransportService transportService, |
| 128 | + ActionFilters actionFilters, |
| 129 | + IndicesService indicesService |
| 130 | + ) { |
| 131 | + super( |
| 132 | + TYPE.name(), |
| 133 | + clusterService, |
| 134 | + transportService, |
| 135 | + actionFilters, |
| 136 | + NodeRequest::new, |
| 137 | + transportService.getThreadPool().executor(ThreadPool.Names.MANAGEMENT) |
| 138 | + ); |
| 139 | + this.indicesService = indicesService; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + protected StatsResponse newResponse(StatsRequest request, List<NodeResponse> nodeResponses, List<FailedNodeException> failures) { |
| 144 | + return new StatsResponse(ClusterName.DEFAULT, nodeResponses, failures); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + protected NodeRequest newNodeRequest(StatsRequest request) { |
| 149 | + return new NodeRequest(); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + protected NodeResponse newNodeResponse(StreamInput in, DiscoveryNode node) throws IOException { |
| 154 | + return new NodeResponse(in, node); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + protected NodeResponse nodeOperation(NodeRequest request, Task task) { |
| 159 | + return new NodeResponse(clusterService.localNode(), IndicesMetrics.getStatsWithoutCache(indicesService)); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments