|
9 | 9 |
|
10 | 10 | package org.elasticsearch.cluster.routing.allocation.allocator; |
11 | 11 |
|
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | + |
12 | 16 | /** |
13 | 17 | * Summarizes the impact to the cluster as a result of a rebalancing round. |
14 | 18 | * |
15 | | - * @param numberOfShardsToMove The number of shard moves required to move from the previous desired balance to the new one. |
| 19 | + * @param nodeNameToWeightChanges The shard balance weight changes for each node (by name), comparing a previous DesiredBalance shard |
| 20 | + * allocation to a new DesiredBalance allocation. |
| 21 | + * @param numberOfShardsToMove The number of shard moves required to move from the previous desired balance to the new one. Does not include |
| 22 | + * new (index creation) or removed (index deletion) shard assignements. |
16 | 23 | */ |
17 | | -public record BalancingRoundSummary(long numberOfShardsToMove) { |
| 24 | +public record BalancingRoundSummary(Map<String, NodesWeightsChanges> nodeNameToWeightChanges, long numberOfShardsToMove) { |
| 25 | + |
| 26 | + /** |
| 27 | + * Represents the change in weights for a node going from an old DesiredBalance to a new DesiredBalance |
| 28 | + * Saves the node weights of an old DesiredBalance, along with a diff against a newer DesiredBalance. |
| 29 | + * |
| 30 | + * @param weights The starting {@link DesiredBalanceMetrics.NodeWeightStats} of a previous DesiredBalance. |
| 31 | + * @param nextWeightsDiff The difference between a previous DesiredBalance and a new DesiredBalance. |
| 32 | + */ |
| 33 | + record NodesWeightsChanges(DesiredBalanceMetrics.NodeWeightStats weights, NodeWeightsDiff nextWeightsDiff) {} |
| 34 | + |
| 35 | + /** |
| 36 | + * Represents the change of shard balance weights for a node, comparing an older DesiredBalance with the latest DesiredBalance. |
| 37 | + * |
| 38 | + * @param shardCountDiff How many more, or less, shards are assigned to the node in the latest DesiredBalance. |
| 39 | + * @param diskUsageInBytesDiff How much more, or less, disk is used by shards assigned to the node in the latest DesiredBalance. |
| 40 | + * @param writeLoadDiff How much more, or less, write load is estimated for shards assigned to the node in the latest DesiredBalance. |
| 41 | + * @param totalWeightDiff How much more, or less, the total weight is of shards assigned to the node in the latest DesiredBalance. |
| 42 | + */ |
| 43 | + record NodeWeightsDiff(long shardCountDiff, double diskUsageInBytesDiff, double writeLoadDiff, double totalWeightDiff) { |
| 44 | + public static NodeWeightsDiff create(DesiredBalanceMetrics.NodeWeightStats base, DesiredBalanceMetrics.NodeWeightStats next) { |
| 45 | + return new NodeWeightsDiff( |
| 46 | + next.shardCount() - base.shardCount(), |
| 47 | + next.diskUsageInBytes() - base.diskUsageInBytes(), |
| 48 | + next.writeLoad() - base.writeLoad(), |
| 49 | + next.nodeWeight() - base.nodeWeight() |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
18 | 53 |
|
19 | 54 | @Override |
20 | 55 | public String toString() { |
21 | | - return "BalancingRoundSummary{" + "numberOfShardsToMove=" + numberOfShardsToMove + '}'; |
| 56 | + return "BalancingRoundSummary{" |
| 57 | + + "nodeNameToWeightChanges" |
| 58 | + + nodeNameToWeightChanges |
| 59 | + + ", numberOfShardsToMove=" |
| 60 | + + numberOfShardsToMove |
| 61 | + + '}'; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Holds combined {@link BalancingRoundSummary} results. Essentially holds a list of the balancing events and the summed up changes |
| 66 | + * across all those events: what allocation work was done across some period of time. |
| 67 | + * TODO: WIP ES-10341 |
| 68 | + * |
| 69 | + * Note that each balancing round summary is the difference between, at the time, latest desired balance and the previous desired balance. |
| 70 | + * Each summary represents a step towards the next desired balance, which is based on presuming the previous desired balance is reached. So |
| 71 | + * combining them is roughly the difference between the first summary's previous desired balance and the last summary's latest desired |
| 72 | + * balance. |
| 73 | + * |
| 74 | + * @param numberOfBalancingRounds How many balancing round summaries are combined in this report. |
| 75 | + * @param nodeNameToWeightChanges |
| 76 | + * @param numberOfShardMoves The sum of shard moves for each balancing round being combined into a single summary. |
| 77 | + */ |
| 78 | + public record CombinedBalancingRoundSummary( |
| 79 | + int numberOfBalancingRounds, |
| 80 | + Map<String, NodesWeightsChanges> nodeNameToWeightChanges, |
| 81 | + long numberOfShardMoves |
| 82 | + ) { |
| 83 | + |
| 84 | + public static final CombinedBalancingRoundSummary EMPTY_RESULTS = new CombinedBalancingRoundSummary(0, new HashMap<>(), 0); |
| 85 | + |
| 86 | + public static CombinedBalancingRoundSummary combine(List<BalancingRoundSummary> summaries) { |
| 87 | + if (summaries.isEmpty()) { |
| 88 | + return EMPTY_RESULTS; |
| 89 | + } |
| 90 | + |
| 91 | + // Initialize the combined weight changes with the oldest changes. We can then build the combined changes by adding the diffs of |
| 92 | + // newer weight changes. If a new node gets added in a later summary, then we will initialize its weights starting there. |
| 93 | + // Similarly, a node may be removed in a later summary: in this case we will keep that nodes work, up until it was removed. |
| 94 | + var iterator = summaries.iterator(); |
| 95 | + assert iterator.hasNext(); |
| 96 | + var firstSummary = iterator.next(); |
| 97 | + Map<String, NodesWeightsChanges> combinedNodeNameToWeightChanges = new HashMap<>(firstSummary.nodeNameToWeightChanges); |
| 98 | + |
| 99 | + // Number of shards moves are simply summed across summaries. Each new balancing round is built upon the last one, so it is |
| 100 | + // possible that a shard is reassigned back to a node before it even moves away, and that will still be counted as 2 moves here. |
| 101 | + long numberOfShardMoves = firstSummary.numberOfShardsToMove; |
| 102 | + |
| 103 | + // Initialize with 1 because we've already begun to iterate the summaries. |
| 104 | + int numSummaries = 1; |
| 105 | + |
| 106 | + // Iterate any remaining summaries (after the first one). |
| 107 | + while (iterator.hasNext()) { |
| 108 | + var summary = iterator.next(); |
| 109 | + for (var nodeNameAndWeights : summary.nodeNameToWeightChanges.entrySet()) { |
| 110 | + var combined = combinedNodeNameToWeightChanges.get(nodeNameAndWeights.getKey()); |
| 111 | + if (combined == null) { |
| 112 | + // Encountered a new node in a later summary. Add the new node initializing it with the base weights from that |
| 113 | + // summary. |
| 114 | + combinedNodeNameToWeightChanges.put(nodeNameAndWeights.getKey(), nodeNameAndWeights.getValue()); |
| 115 | + } else { |
| 116 | + var newCombinedDiff = new NodeWeightsDiff( |
| 117 | + combined.nextWeightsDiff.shardCountDiff + nodeNameAndWeights.getValue().nextWeightsDiff.shardCountDiff, |
| 118 | + combined.nextWeightsDiff.diskUsageInBytesDiff + nodeNameAndWeights |
| 119 | + .getValue().nextWeightsDiff.diskUsageInBytesDiff, |
| 120 | + combined.nextWeightsDiff.writeLoadDiff + nodeNameAndWeights.getValue().nextWeightsDiff.writeLoadDiff, |
| 121 | + combined.nextWeightsDiff.totalWeightDiff + nodeNameAndWeights.getValue().nextWeightsDiff.totalWeightDiff |
| 122 | + ); |
| 123 | + var newCombinedChanges = new NodesWeightsChanges(combined.weights, newCombinedDiff); |
| 124 | + combinedNodeNameToWeightChanges.compute(nodeNameAndWeights.getKey(), (k, weightChanges) -> newCombinedChanges); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + ++numSummaries; |
| 129 | + numberOfShardMoves += summary.numberOfShardsToMove(); |
| 130 | + } |
| 131 | + |
| 132 | + return new CombinedBalancingRoundSummary(numSummaries, combinedNodeNameToWeightChanges, numberOfShardMoves); |
| 133 | + } |
| 134 | + |
22 | 135 | } |
23 | 136 |
|
24 | 137 | } |
0 commit comments