|
| 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 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.core; |
| 8 | + |
| 9 | +import org.elasticsearch.action.ActionListener; |
| 10 | +import org.elasticsearch.action.admin.cluster.node.stats.NodeStats; |
| 11 | +import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse; |
| 12 | +import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags; |
| 13 | +import org.elasticsearch.client.Client; |
| 14 | +import org.elasticsearch.cluster.ClusterState; |
| 15 | +import org.elasticsearch.cluster.node.DiscoveryNodeRole; |
| 16 | +import org.elasticsearch.cluster.routing.RoutingNode; |
| 17 | +import org.elasticsearch.cluster.routing.RoutingNodes; |
| 18 | +import org.elasticsearch.cluster.routing.ShardRouting; |
| 19 | +import org.elasticsearch.cluster.routing.ShardRoutingState; |
| 20 | +import org.elasticsearch.cluster.service.ClusterService; |
| 21 | +import org.elasticsearch.common.inject.Inject; |
| 22 | +import org.elasticsearch.index.Index; |
| 23 | +import org.elasticsearch.index.store.StoreStats; |
| 24 | +import org.elasticsearch.search.aggregations.metrics.TDigestState; |
| 25 | + |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Set; |
| 30 | +import java.util.concurrent.atomic.AtomicInteger; |
| 31 | +import java.util.concurrent.atomic.AtomicLong; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +public class DataTiersFeatureSet implements XPackFeatureSet { |
| 35 | + |
| 36 | + private final Client client; |
| 37 | + private final ClusterService clusterService; |
| 38 | + |
| 39 | + @Inject |
| 40 | + public DataTiersFeatureSet(Client client, ClusterService clusterService) { |
| 41 | + this.client = client; |
| 42 | + this.clusterService = clusterService; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String name() { |
| 47 | + return XPackField.DATA_TIERS; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public boolean available() { |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean enabled() { |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Map<String, Object> nativeCodeInfo() { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void usage(ActionListener<Usage> listener) { |
| 67 | + final ClusterState state = clusterService.state(); |
| 68 | + client.admin().cluster().prepareNodesStats() |
| 69 | + .all() |
| 70 | + .setIndices(CommonStatsFlags.ALL) |
| 71 | + .execute(ActionListener.wrap(nodesStatsResponse -> { |
| 72 | + final RoutingNodes routingNodes = state.getRoutingNodes(); |
| 73 | + |
| 74 | + // First separate the nodes into separate tiers, note that nodes *may* be duplicated |
| 75 | + Map<String, List<NodeStats>> tierSpecificNodeStats = separateTiers(nodesStatsResponse); |
| 76 | + |
| 77 | + // Generate tier specific stats for the nodes |
| 78 | + Map<String, DataTiersFeatureSetUsage.TierSpecificStats> tierSpecificStats = tierSpecificNodeStats.entrySet() |
| 79 | + .stream().collect(Collectors.toMap(Map.Entry::getKey, ns -> calculateStats(ns.getValue(), routingNodes))); |
| 80 | + |
| 81 | + listener.onResponse(new DataTiersFeatureSetUsage(tierSpecificStats)); |
| 82 | + }, listener::onFailure)); |
| 83 | + } |
| 84 | + |
| 85 | + // Visible for testing |
| 86 | + static Map<String, List<NodeStats>> separateTiers(NodesStatsResponse nodesStatsResponse) { |
| 87 | + Map<String, List<NodeStats>> responses = new HashMap<>(); |
| 88 | + DataTier.ALL_DATA_TIERS.forEach(tier -> |
| 89 | + responses.put(tier, nodesStatsResponse.getNodes().stream() |
| 90 | + .filter(stats -> stats.getNode().getRoles().stream() |
| 91 | + .map(DiscoveryNodeRole::roleName) |
| 92 | + .anyMatch(rn -> rn.equals(tier))) |
| 93 | + .collect(Collectors.toList()))); |
| 94 | + return responses; |
| 95 | + } |
| 96 | + |
| 97 | + private DataTiersFeatureSetUsage.TierSpecificStats calculateStats(List<NodeStats> nodesStats, RoutingNodes routingNodes) { |
| 98 | + int nodeCount = 0; |
| 99 | + int indexCount = 0; |
| 100 | + int totalShardCount = 0; |
| 101 | + long totalByteCount = 0; |
| 102 | + long docCount = 0; |
| 103 | + final AtomicInteger primaryShardCount = new AtomicInteger(0); |
| 104 | + final AtomicLong primaryByteCount = new AtomicLong(0); |
| 105 | + final TDigestState valueSketch = new TDigestState(1000); |
| 106 | + for (NodeStats nodeStats : nodesStats) { |
| 107 | + nodeCount++; |
| 108 | + totalByteCount += nodeStats.getIndices().getStore().getSizeInBytes(); |
| 109 | + docCount += nodeStats.getIndices().getDocs().getCount(); |
| 110 | + String nodeId = nodeStats.getNode().getId(); |
| 111 | + final RoutingNode node = routingNodes.node(nodeId); |
| 112 | + if (node != null) { |
| 113 | + totalShardCount += node.shardsWithState(ShardRoutingState.STARTED).size(); |
| 114 | + Set<Index> indicesOnNode = node.shardsWithState(ShardRoutingState.STARTED).stream() |
| 115 | + .map(ShardRouting::index) |
| 116 | + .collect(Collectors.toSet()); |
| 117 | + indexCount += indicesOnNode.size(); |
| 118 | + indicesOnNode.forEach(index -> { |
| 119 | + nodeStats.getIndices().getShardStats(index).stream() |
| 120 | + .filter(shardStats -> shardStats.getPrimary().getStore() != null) |
| 121 | + .forEach(shardStats -> { |
| 122 | + StoreStats primaryStoreStats = shardStats.getPrimary().getStore(); |
| 123 | + // If storeStats is null, it means this is not a replica |
| 124 | + primaryShardCount.incrementAndGet(); |
| 125 | + long primarySize = primaryStoreStats.getSizeInBytes(); |
| 126 | + primaryByteCount.addAndGet(primarySize); |
| 127 | + valueSketch.add(primarySize); |
| 128 | + }); |
| 129 | + }); |
| 130 | + } |
| 131 | + } |
| 132 | + long primaryShardSizeMedian = (long) valueSketch.quantile(0.5); |
| 133 | + long primaryShardSizeMAD = computeMedianAbsoluteDeviation(valueSketch); |
| 134 | + return new DataTiersFeatureSetUsage.TierSpecificStats(nodeCount, indexCount, totalShardCount, primaryShardCount.get(), docCount, |
| 135 | + totalByteCount, primaryByteCount.get(), primaryShardSizeMedian, primaryShardSizeMAD); |
| 136 | + } |
| 137 | + |
| 138 | + // Visible for testing |
| 139 | + static long computeMedianAbsoluteDeviation(TDigestState valuesSketch) { |
| 140 | + if (valuesSketch.size() == 0) { |
| 141 | + return 0; |
| 142 | + } else { |
| 143 | + final double approximateMedian = valuesSketch.quantile(0.5); |
| 144 | + final TDigestState approximatedDeviationsSketch = new TDigestState(valuesSketch.compression()); |
| 145 | + valuesSketch.centroids().forEach(centroid -> { |
| 146 | + final double deviation = Math.abs(approximateMedian - centroid.mean()); |
| 147 | + approximatedDeviationsSketch.add(deviation, centroid.count()); |
| 148 | + }); |
| 149 | + |
| 150 | + return (long) approximatedDeviationsSketch.quantile(0.5); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments