|
| 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; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.core; |
| 9 | + |
| 10 | +import org.elasticsearch.action.ActionListener; |
| 11 | +import org.elasticsearch.action.admin.cluster.node.stats.NodeStats; |
| 12 | +import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags; |
| 13 | +import org.elasticsearch.action.admin.indices.stats.IndexShardStats; |
| 14 | +import org.elasticsearch.action.support.ActionFilters; |
| 15 | +import org.elasticsearch.client.internal.Client; |
| 16 | +import org.elasticsearch.client.internal.ParentTaskAssigningClient; |
| 17 | +import org.elasticsearch.cluster.ClusterState; |
| 18 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 19 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
| 20 | +import org.elasticsearch.cluster.node.DiscoveryNodeRole; |
| 21 | +import org.elasticsearch.cluster.routing.RoutingNode; |
| 22 | +import org.elasticsearch.cluster.routing.RoutingNodes; |
| 23 | +import org.elasticsearch.cluster.routing.ShardRouting; |
| 24 | +import org.elasticsearch.cluster.routing.ShardRoutingState; |
| 25 | +import org.elasticsearch.cluster.routing.allocation.DataTier; |
| 26 | +import org.elasticsearch.cluster.service.ClusterService; |
| 27 | +import org.elasticsearch.common.Strings; |
| 28 | +import org.elasticsearch.common.inject.Inject; |
| 29 | +import org.elasticsearch.index.Index; |
| 30 | +import org.elasticsearch.index.store.StoreStats; |
| 31 | +import org.elasticsearch.protocol.xpack.XPackUsageRequest; |
| 32 | +import org.elasticsearch.search.aggregations.metrics.TDigestState; |
| 33 | +import org.elasticsearch.tasks.Task; |
| 34 | +import org.elasticsearch.threadpool.ThreadPool; |
| 35 | +import org.elasticsearch.transport.TransportService; |
| 36 | +import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction; |
| 37 | +import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse; |
| 38 | +import org.elasticsearch.xpack.core.action.XPackUsageFeatureTransportAction; |
| 39 | + |
| 40 | +import java.util.HashMap; |
| 41 | +import java.util.HashSet; |
| 42 | +import java.util.List; |
| 43 | +import java.util.Map; |
| 44 | +import java.util.Set; |
| 45 | +import java.util.stream.StreamSupport; |
| 46 | + |
| 47 | +public class DataTiersUsageTransportAction extends XPackUsageFeatureTransportAction { |
| 48 | + |
| 49 | + private final Client client; |
| 50 | + |
| 51 | + @Inject |
| 52 | + public DataTiersUsageTransportAction( |
| 53 | + TransportService transportService, |
| 54 | + ClusterService clusterService, |
| 55 | + ThreadPool threadPool, |
| 56 | + ActionFilters actionFilters, |
| 57 | + IndexNameExpressionResolver indexNameExpressionResolver, |
| 58 | + Client client |
| 59 | + ) { |
| 60 | + super( |
| 61 | + XPackUsageFeatureAction.DATA_TIERS.name(), |
| 62 | + transportService, |
| 63 | + clusterService, |
| 64 | + threadPool, |
| 65 | + actionFilters, |
| 66 | + indexNameExpressionResolver |
| 67 | + ); |
| 68 | + this.client = client; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected void masterOperation( |
| 73 | + Task task, |
| 74 | + XPackUsageRequest request, |
| 75 | + ClusterState state, |
| 76 | + ActionListener<XPackUsageFeatureResponse> listener |
| 77 | + ) { |
| 78 | + new ParentTaskAssigningClient(client, clusterService.localNode(), task).admin() |
| 79 | + .cluster() |
| 80 | + .prepareNodesStats() |
| 81 | + .all() |
| 82 | + .setIndices(CommonStatsFlags.ALL) |
| 83 | + .execute(listener.delegateFailureAndWrap((delegate, nodesStatsResponse) -> { |
| 84 | + final RoutingNodes routingNodes = state.getRoutingNodes(); |
| 85 | + final Map<String, IndexMetadata> indices = state.getMetadata().getIndices(); |
| 86 | + |
| 87 | + // Determine which tiers each index would prefer to be within |
| 88 | + Map<String, String> indicesToTiers = tierIndices(indices); |
| 89 | + |
| 90 | + // Generate tier specific stats for the nodes and indices |
| 91 | + Map<String, DataTiersFeatureSetUsage.TierSpecificStats> tierSpecificStats = calculateStats( |
| 92 | + nodesStatsResponse.getNodes(), |
| 93 | + indicesToTiers, |
| 94 | + routingNodes |
| 95 | + ); |
| 96 | + |
| 97 | + delegate.onResponse(new XPackUsageFeatureResponse(new DataTiersFeatureSetUsage(tierSpecificStats))); |
| 98 | + })); |
| 99 | + } |
| 100 | + |
| 101 | + // Visible for testing |
| 102 | + // Takes a registry of indices and returns a mapping of index name to which tier it most prefers. Always 1 to 1, some may filter out. |
| 103 | + static Map<String, String> tierIndices(Map<String, IndexMetadata> indices) { |
| 104 | + Map<String, String> indexByTier = new HashMap<>(); |
| 105 | + indices.entrySet().forEach(entry -> { |
| 106 | + String tierPref = entry.getValue().getSettings().get(DataTier.TIER_PREFERENCE); |
| 107 | + if (Strings.hasText(tierPref)) { |
| 108 | + String[] tiers = tierPref.split(","); |
| 109 | + if (tiers.length > 0) { |
| 110 | + indexByTier.put(entry.getKey(), tiers[0]); |
| 111 | + } |
| 112 | + } |
| 113 | + }); |
| 114 | + return indexByTier; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Accumulator to hold intermediate data tier stats before final calculation. |
| 119 | + */ |
| 120 | + private static class TierStatsAccumulator { |
| 121 | + int nodeCount = 0; |
| 122 | + Set<String> indexNames = new HashSet<>(); |
| 123 | + int totalShardCount = 0; |
| 124 | + long totalByteCount = 0; |
| 125 | + long docCount = 0; |
| 126 | + int primaryShardCount = 0; |
| 127 | + long primaryByteCount = 0L; |
| 128 | + final TDigestState valueSketch = TDigestState.create(1000); |
| 129 | + } |
| 130 | + |
| 131 | + // Visible for testing |
| 132 | + static Map<String, DataTiersFeatureSetUsage.TierSpecificStats> calculateStats( |
| 133 | + List<NodeStats> nodesStats, |
| 134 | + Map<String, String> indexByTier, |
| 135 | + RoutingNodes routingNodes |
| 136 | + ) { |
| 137 | + Map<String, TierStatsAccumulator> statsAccumulators = new HashMap<>(); |
| 138 | + for (NodeStats nodeStats : nodesStats) { |
| 139 | + aggregateDataTierNodeCounts(nodeStats, statsAccumulators); |
| 140 | + aggregateDataTierIndexStats(nodeStats, routingNodes, indexByTier, statsAccumulators); |
| 141 | + } |
| 142 | + Map<String, DataTiersFeatureSetUsage.TierSpecificStats> results = new HashMap<>(); |
| 143 | + for (Map.Entry<String, TierStatsAccumulator> entry : statsAccumulators.entrySet()) { |
| 144 | + results.put(entry.getKey(), calculateFinalTierStats(entry.getValue())); |
| 145 | + } |
| 146 | + return results; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Determine which data tiers this node belongs to (if any), and increment the node counts for those tiers. |
| 151 | + */ |
| 152 | + private static void aggregateDataTierNodeCounts(NodeStats nodeStats, Map<String, TierStatsAccumulator> tiersStats) { |
| 153 | + nodeStats.getNode() |
| 154 | + .getRoles() |
| 155 | + .stream() |
| 156 | + .map(DiscoveryNodeRole::roleName) |
| 157 | + .filter(DataTier::validTierName) |
| 158 | + .forEach(tier -> tiersStats.computeIfAbsent(tier, k -> new TierStatsAccumulator()).nodeCount++); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Locate which indices are hosted on the node specified by the NodeStats, then group and aggregate the available index stats by tier. |
| 163 | + */ |
| 164 | + private static void aggregateDataTierIndexStats( |
| 165 | + NodeStats nodeStats, |
| 166 | + RoutingNodes routingNodes, |
| 167 | + Map<String, String> indexByTier, |
| 168 | + Map<String, TierStatsAccumulator> accumulators |
| 169 | + ) { |
| 170 | + final RoutingNode node = routingNodes.node(nodeStats.getNode().getId()); |
| 171 | + if (node != null) { |
| 172 | + StreamSupport.stream(node.spliterator(), false) |
| 173 | + .map(ShardRouting::index) |
| 174 | + .distinct() |
| 175 | + .forEach(index -> classifyIndexAndCollectStats(index, nodeStats, indexByTier, node, accumulators)); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Determine which tier an index belongs in, then accumulate its stats into that tier's stats. |
| 181 | + */ |
| 182 | + private static void classifyIndexAndCollectStats( |
| 183 | + Index index, |
| 184 | + NodeStats nodeStats, |
| 185 | + Map<String, String> indexByTier, |
| 186 | + RoutingNode node, |
| 187 | + Map<String, TierStatsAccumulator> accumulators |
| 188 | + ) { |
| 189 | + // Look up which tier this index belongs to (its most preferred) |
| 190 | + String indexTier = indexByTier.get(index.getName()); |
| 191 | + if (indexTier != null) { |
| 192 | + final TierStatsAccumulator accumulator = accumulators.computeIfAbsent(indexTier, k -> new TierStatsAccumulator()); |
| 193 | + accumulator.indexNames.add(index.getName()); |
| 194 | + aggregateDataTierShardStats(nodeStats, index, node, accumulator); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Collect shard-level data tier stats from shard stats contained in the node stats response. |
| 200 | + */ |
| 201 | + private static void aggregateDataTierShardStats(NodeStats nodeStats, Index index, RoutingNode node, TierStatsAccumulator accumulator) { |
| 202 | + // Shard based stats |
| 203 | + final List<IndexShardStats> allShardStats = nodeStats.getIndices().getShardStats(index); |
| 204 | + if (allShardStats != null) { |
| 205 | + for (IndexShardStats shardStat : allShardStats) { |
| 206 | + accumulator.totalByteCount += shardStat.getTotal().getStore().totalDataSetSizeInBytes(); |
| 207 | + accumulator.docCount += shardStat.getTotal().getDocs().getCount(); |
| 208 | + |
| 209 | + // Accumulate stats about started shards |
| 210 | + ShardRouting shardRouting = node.getByShardId(shardStat.getShardId()); |
| 211 | + if (shardRouting != null && shardRouting.state() == ShardRoutingState.STARTED) { |
| 212 | + accumulator.totalShardCount += 1; |
| 213 | + |
| 214 | + // Accumulate stats about started primary shards |
| 215 | + StoreStats primaryStoreStats = shardStat.getPrimary().getStore(); |
| 216 | + if (primaryStoreStats != null) { |
| 217 | + // if primaryStoreStats is null, it means there is no primary on the node in question |
| 218 | + accumulator.primaryShardCount++; |
| 219 | + long primarySize = primaryStoreStats.totalDataSetSizeInBytes(); |
| 220 | + accumulator.primaryByteCount += primarySize; |
| 221 | + accumulator.valueSketch.add(primarySize); |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + private static DataTiersFeatureSetUsage.TierSpecificStats calculateFinalTierStats(TierStatsAccumulator accumulator) { |
| 229 | + long primaryShardSizeMedian = (long) accumulator.valueSketch.quantile(0.5); |
| 230 | + long primaryShardSizeMAD = computeMedianAbsoluteDeviation(accumulator.valueSketch); |
| 231 | + return new DataTiersFeatureSetUsage.TierSpecificStats( |
| 232 | + accumulator.nodeCount, |
| 233 | + accumulator.indexNames.size(), |
| 234 | + accumulator.totalShardCount, |
| 235 | + accumulator.primaryShardCount, |
| 236 | + accumulator.docCount, |
| 237 | + accumulator.totalByteCount, |
| 238 | + accumulator.primaryByteCount, |
| 239 | + primaryShardSizeMedian, |
| 240 | + primaryShardSizeMAD |
| 241 | + ); |
| 242 | + } |
| 243 | + |
| 244 | + // Visible for testing |
| 245 | + static long computeMedianAbsoluteDeviation(TDigestState valuesSketch) { |
| 246 | + if (valuesSketch.size() == 0) { |
| 247 | + return 0; |
| 248 | + } else { |
| 249 | + final double approximateMedian = valuesSketch.quantile(0.5); |
| 250 | + final TDigestState approximatedDeviationsSketch = TDigestState.createUsingParamsFrom(valuesSketch); |
| 251 | + valuesSketch.centroids().forEach(centroid -> { |
| 252 | + final double deviation = Math.abs(approximateMedian - centroid.mean()); |
| 253 | + approximatedDeviationsSketch.add(deviation, centroid.count()); |
| 254 | + }); |
| 255 | + |
| 256 | + return (long) approximatedDeviationsSketch.quantile(0.5); |
| 257 | + } |
| 258 | + } |
| 259 | +} |
0 commit comments