|
| 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.cluster.routing; |
| 11 | + |
| 12 | +import org.elasticsearch.action.support.replication.ClusterStateCreationUtils; |
| 13 | +import org.elasticsearch.cluster.ClusterInfo; |
| 14 | +import org.elasticsearch.cluster.ClusterState; |
| 15 | +import org.elasticsearch.cluster.NodeUsageStatsForThreadPools; |
| 16 | +import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; |
| 17 | +import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders; |
| 18 | +import org.elasticsearch.snapshots.SnapshotShardSizeInfo; |
| 19 | +import org.elasticsearch.test.ESTestCase; |
| 20 | +import org.hamcrest.Matchers; |
| 21 | + |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.stream.StreamSupport; |
| 26 | + |
| 27 | +import static org.hamcrest.Matchers.equalTo; |
| 28 | +import static org.hamcrest.Matchers.greaterThan; |
| 29 | +import static org.hamcrest.Matchers.lessThan; |
| 30 | +import static org.hamcrest.Matchers.sameInstance; |
| 31 | + |
| 32 | +public class WriteLoadPerShardSimulatorTests extends ESTestCase { |
| 33 | + |
| 34 | + private static final RoutingChangesObserver NOOP = new RoutingChangesObserver() { |
| 35 | + }; |
| 36 | + |
| 37 | + /** |
| 38 | + * We should not adjust the values if there's no movement |
| 39 | + */ |
| 40 | + public void testNoShardMovement() { |
| 41 | + final var originalNode0WriteLoadStats = new NodeUsageStatsForThreadPools.ThreadPoolUsageStats( |
| 42 | + randomIntBetween(4, 32), |
| 43 | + randomFloatBetween(0f, 1f, true), |
| 44 | + randomLongBetween(0L, 7_000L) |
| 45 | + ); |
| 46 | + final var originalNode1WriteLoadStats = new NodeUsageStatsForThreadPools.ThreadPoolUsageStats( |
| 47 | + randomIntBetween(4, 32), |
| 48 | + randomFloatBetween(0f, 1f, true), |
| 49 | + randomLongBetween(0L, 7_000L) |
| 50 | + ); |
| 51 | + final var allocation = createRoutingAllocation(originalNode0WriteLoadStats, originalNode1WriteLoadStats); |
| 52 | + |
| 53 | + final var writeLoadPerShardSimulator = new WriteLoadPerShardSimulator(allocation); |
| 54 | + final var calculatedNodeUsageStates = writeLoadPerShardSimulator.nodeUsageStatsForThreadPools(); |
| 55 | + assertThat(calculatedNodeUsageStates, Matchers.aMapWithSize(2)); |
| 56 | + assertThat( |
| 57 | + calculatedNodeUsageStates.get("node_0").threadPoolUsageStatsMap().get("write"), |
| 58 | + sameInstance(originalNode0WriteLoadStats) |
| 59 | + ); |
| 60 | + assertThat( |
| 61 | + calculatedNodeUsageStates.get("node_1").threadPoolUsageStatsMap().get("write"), |
| 62 | + sameInstance(originalNode1WriteLoadStats) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public void testMovementOfAShardWillReduceThreadPoolUtilisation() { |
| 67 | + final var originalNode0WriteLoadStats = new NodeUsageStatsForThreadPools.ThreadPoolUsageStats( |
| 68 | + randomIntBetween(4, 16), |
| 69 | + randomFloatBetween(0.2f, 1.0f, true), |
| 70 | + 0 |
| 71 | + ); |
| 72 | + final var originalNode1WriteLoadStats = new NodeUsageStatsForThreadPools.ThreadPoolUsageStats( |
| 73 | + randomIntBetween(4, 16), |
| 74 | + randomFloatBetween(0.1f, 0.5f, true), |
| 75 | + 0 |
| 76 | + ); |
| 77 | + final var allocation = createRoutingAllocation(originalNode0WriteLoadStats, originalNode1WriteLoadStats); |
| 78 | + |
| 79 | + final var writeLoadPerShardSimulator = new WriteLoadPerShardSimulator(allocation); |
| 80 | + |
| 81 | + // Relocate a random shard from node_0 to node_1 |
| 82 | + final var randomShard = randomFrom(StreamSupport.stream(allocation.routingNodes().node("node_0").spliterator(), false).toList()); |
| 83 | + final var moveShardTuple = allocation.routingNodes().relocateShard(randomShard, "node_1", randomNonNegativeLong(), "testing", NOOP); |
| 84 | + writeLoadPerShardSimulator.simulateShardStarted(moveShardTuple.v2()); |
| 85 | + |
| 86 | + final var calculatedNodeUsageStates = writeLoadPerShardSimulator.nodeUsageStatsForThreadPools(); |
| 87 | + assertThat(calculatedNodeUsageStates, Matchers.aMapWithSize(2)); |
| 88 | + |
| 89 | + // Some node_0 utilization should have been moved to node_1 |
| 90 | + assertThat( |
| 91 | + getAverageWritePoolUtilization(writeLoadPerShardSimulator, "node_0"), |
| 92 | + lessThan(originalNode0WriteLoadStats.averageThreadPoolUtilization()) |
| 93 | + ); |
| 94 | + assertThat( |
| 95 | + getAverageWritePoolUtilization(writeLoadPerShardSimulator, "node_1"), |
| 96 | + greaterThan(originalNode1WriteLoadStats.averageThreadPoolUtilization()) |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + public void testMovementFollowedByMovementBackWillNotChangeAnything() { |
| 101 | + final var originalNode0WriteLoadStats = randomUsageStats(); |
| 102 | + final var originalNode1WriteLoadStats = randomUsageStats(); |
| 103 | + final var allocation = createRoutingAllocation(originalNode0WriteLoadStats, originalNode1WriteLoadStats); |
| 104 | + final var writeLoadPerShardSimulator = new WriteLoadPerShardSimulator(allocation); |
| 105 | + |
| 106 | + // Relocate a random shard from node_0 to node_1 |
| 107 | + final long expectedShardSize = randomNonNegativeLong(); |
| 108 | + final var randomShard = randomFrom(StreamSupport.stream(allocation.routingNodes().node("node_0").spliterator(), false).toList()); |
| 109 | + final var moveShardTuple = allocation.routingNodes().relocateShard(randomShard, "node_1", expectedShardSize, "testing", NOOP); |
| 110 | + writeLoadPerShardSimulator.simulateShardStarted(moveShardTuple.v2()); |
| 111 | + final ShardRouting movedAndStartedShard = allocation.routingNodes().startShard(moveShardTuple.v2(), NOOP, expectedShardSize); |
| 112 | + |
| 113 | + // Some node_0 utilization should have been moved to node_1 |
| 114 | + assertThat( |
| 115 | + getAverageWritePoolUtilization(writeLoadPerShardSimulator, "node_0"), |
| 116 | + lessThan(originalNode0WriteLoadStats.averageThreadPoolUtilization()) |
| 117 | + ); |
| 118 | + assertThat( |
| 119 | + getAverageWritePoolUtilization(writeLoadPerShardSimulator, "node_1"), |
| 120 | + greaterThan(originalNode1WriteLoadStats.averageThreadPoolUtilization()) |
| 121 | + ); |
| 122 | + |
| 123 | + // Then move it back |
| 124 | + final var moveBackTuple = allocation.routingNodes() |
| 125 | + .relocateShard(movedAndStartedShard, "node_0", expectedShardSize, "testing", NOOP); |
| 126 | + writeLoadPerShardSimulator.simulateShardStarted(moveBackTuple.v2()); |
| 127 | + |
| 128 | + // The utilization numbers should be back to their original values |
| 129 | + assertThat( |
| 130 | + getAverageWritePoolUtilization(writeLoadPerShardSimulator, "node_0"), |
| 131 | + equalTo(originalNode0WriteLoadStats.averageThreadPoolUtilization()) |
| 132 | + ); |
| 133 | + assertThat( |
| 134 | + getAverageWritePoolUtilization(writeLoadPerShardSimulator, "node_1"), |
| 135 | + equalTo(originalNode1WriteLoadStats.averageThreadPoolUtilization()) |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + public void testMovementBetweenNodesWithNoThreadPoolStats() { |
| 140 | + final var originalNode0WriteLoadStats = randomBoolean() ? randomUsageStats() : null; |
| 141 | + final var originalNode1WriteLoadStats = randomBoolean() ? randomUsageStats() : null; |
| 142 | + final var allocation = createRoutingAllocation(originalNode0WriteLoadStats, originalNode1WriteLoadStats); |
| 143 | + final var writeLoadPerShardSimulator = new WriteLoadPerShardSimulator(allocation); |
| 144 | + |
| 145 | + // Relocate a random shard from node_0 to node_1 |
| 146 | + final long expectedShardSize = randomNonNegativeLong(); |
| 147 | + final var randomShard = randomFrom(StreamSupport.stream(allocation.routingNodes().node("node_0").spliterator(), false).toList()); |
| 148 | + final var moveShardTuple = allocation.routingNodes().relocateShard(randomShard, "node_1", expectedShardSize, "testing", NOOP); |
| 149 | + writeLoadPerShardSimulator.simulateShardStarted(moveShardTuple.v2()); |
| 150 | + allocation.routingNodes().startShard(moveShardTuple.v2(), NOOP, expectedShardSize); |
| 151 | + |
| 152 | + final var generated = writeLoadPerShardSimulator.nodeUsageStatsForThreadPools(); |
| 153 | + assertThat(generated.containsKey("node_0"), equalTo(originalNode0WriteLoadStats != null)); |
| 154 | + assertThat(generated.containsKey("node_1"), equalTo(originalNode1WriteLoadStats != null)); |
| 155 | + } |
| 156 | + |
| 157 | + private float getAverageWritePoolUtilization(WriteLoadPerShardSimulator writeLoadPerShardSimulator, String nodeId) { |
| 158 | + final var generatedNodeUsageStates = writeLoadPerShardSimulator.nodeUsageStatsForThreadPools(); |
| 159 | + final var node0WritePoolStats = generatedNodeUsageStates.get(nodeId).threadPoolUsageStatsMap().get("write"); |
| 160 | + return node0WritePoolStats.averageThreadPoolUtilization(); |
| 161 | + } |
| 162 | + |
| 163 | + private NodeUsageStatsForThreadPools.ThreadPoolUsageStats randomUsageStats() { |
| 164 | + return new NodeUsageStatsForThreadPools.ThreadPoolUsageStats(randomIntBetween(4, 16), randomFloatBetween(0.0f, 1.0f, false), 0); |
| 165 | + } |
| 166 | + |
| 167 | + private RoutingAllocation createRoutingAllocation( |
| 168 | + NodeUsageStatsForThreadPools.ThreadPoolUsageStats node0WriteLoadStats, |
| 169 | + NodeUsageStatsForThreadPools.ThreadPoolUsageStats node1WriteLoadStats |
| 170 | + ) { |
| 171 | + final Map<String, NodeUsageStatsForThreadPools> nodeUsageStats = new HashMap<>(); |
| 172 | + if (node0WriteLoadStats != null) { |
| 173 | + nodeUsageStats.put("node_0", new NodeUsageStatsForThreadPools("node_0", Map.of("write", node0WriteLoadStats))); |
| 174 | + } |
| 175 | + if (node1WriteLoadStats != null) { |
| 176 | + nodeUsageStats.put("node_1", new NodeUsageStatsForThreadPools("node_1", Map.of("write", node1WriteLoadStats))); |
| 177 | + } |
| 178 | + |
| 179 | + return new RoutingAllocation( |
| 180 | + new AllocationDeciders(List.of()), |
| 181 | + createClusterState(), |
| 182 | + ClusterInfo.builder().nodeUsageStatsForThreadPools(nodeUsageStats).build(), |
| 183 | + SnapshotShardSizeInfo.EMPTY, |
| 184 | + System.nanoTime() |
| 185 | + ).mutableCloneForSimulation(); |
| 186 | + } |
| 187 | + |
| 188 | + private ClusterState createClusterState() { |
| 189 | + return ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(new String[] { "indexOne", "indexTwo", "indexThree" }, 3, 0); |
| 190 | + } |
| 191 | +} |
0 commit comments