|
| 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.allocation; |
| 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.ESAllocationTestCase; |
| 16 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 17 | +import org.elasticsearch.cluster.metadata.ProjectId; |
| 18 | +import org.elasticsearch.cluster.routing.RoutingChangesObserver; |
| 19 | +import org.elasticsearch.cluster.routing.RoutingNode; |
| 20 | +import org.elasticsearch.cluster.routing.RoutingNodes; |
| 21 | +import org.elasticsearch.cluster.routing.RoutingTable; |
| 22 | +import org.elasticsearch.cluster.routing.ShardRouting; |
| 23 | +import org.elasticsearch.cluster.routing.allocation.decider.Decision; |
| 24 | +import org.elasticsearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider; |
| 25 | +import org.elasticsearch.common.settings.ClusterSettings; |
| 26 | +import org.elasticsearch.common.settings.Settings; |
| 27 | +import org.elasticsearch.index.Index; |
| 28 | +import org.elasticsearch.index.shard.ShardId; |
| 29 | + |
| 30 | +import static org.hamcrest.Matchers.equalTo; |
| 31 | +import static org.junit.Assert.assertFalse; |
| 32 | +import static org.junit.Assert.assertTrue; |
| 33 | + |
| 34 | +public class ThrottlingAllocationDeciderTests extends ESAllocationTestCase { |
| 35 | + |
| 36 | + private record TestHarness( |
| 37 | + ClusterState clusterState, |
| 38 | + RoutingNodes mutableRoutingNodes, |
| 39 | + RoutingNode mutableRoutingNode1, |
| 40 | + RoutingNode mutableRoutingNode2, |
| 41 | + ShardRouting unassignedShardRouting1Primary, |
| 42 | + ShardRouting unassignedShardRouting1Replica, |
| 43 | + ShardRouting unassignedShardRouting2Primary, |
| 44 | + ShardRouting unassignedShardRouting2Replica |
| 45 | + ) {} |
| 46 | + |
| 47 | + private TestHarness setUpTwoNodesAndIndexWithTwoUnassignedPrimariesAndReplicas() { |
| 48 | + int numberOfShards = 2; |
| 49 | + ClusterState clusterState = ClusterStateCreationUtils.stateWithUnassignedPrimariesAndReplicas( |
| 50 | + new String[] { "test-index" }, |
| 51 | + numberOfShards, |
| 52 | + 1 |
| 53 | + ); |
| 54 | + // The number of data nodes the util method above creates is numberOfReplicas+1. |
| 55 | + assertEquals(2, clusterState.nodes().size()); |
| 56 | + assertEquals(1, clusterState.metadata().getTotalNumberOfIndices()); |
| 57 | + |
| 58 | + var indexIterator = clusterState.metadata().indicesAllProjects().iterator(); |
| 59 | + assertTrue(indexIterator.hasNext()); |
| 60 | + IndexMetadata testIndexMetadata = indexIterator.next(); |
| 61 | + assertFalse(indexIterator.hasNext()); |
| 62 | + Index testIndex = testIndexMetadata.getIndex(); |
| 63 | + assertEquals(numberOfShards, testIndexMetadata.getNumberOfShards()); |
| 64 | + ShardId testShardId1 = new ShardId(testIndex, 0); |
| 65 | + ShardId testShardId2 = new ShardId(testIndex, 1); |
| 66 | + |
| 67 | + var mutableRoutingNodes = clusterState.mutableRoutingNodes(); |
| 68 | + |
| 69 | + // The RoutingNode references must be to the RoutingAllocation's RoutingNodes, so that changes to one is reflected in the other. |
| 70 | + var routingNodesIterator = mutableRoutingNodes.iterator(); |
| 71 | + assertTrue(routingNodesIterator.hasNext()); |
| 72 | + var mutableRoutingNode1 = routingNodesIterator.next(); |
| 73 | + assertTrue(routingNodesIterator.hasNext()); |
| 74 | + var mutableRoutingNode2 = routingNodesIterator.next(); |
| 75 | + assertFalse(routingNodesIterator.hasNext()); |
| 76 | + |
| 77 | + RoutingTable routingTable = clusterState.routingTable(ProjectId.DEFAULT); |
| 78 | + |
| 79 | + assertThat(routingTable.shardRoutingTable(testShardId1).replicaShards().size(), equalTo(1)); |
| 80 | + assertThat(routingTable.shardRoutingTable(testShardId2).replicaShards().size(), equalTo(1)); |
| 81 | + |
| 82 | + ShardRouting unassignedShardRouting1Primary = routingTable.shardRoutingTable(testShardId1).primaryShard(); |
| 83 | + ShardRouting unassignedShardRouting1Replica = routingTable.shardRoutingTable(testShardId1).replicaShards().get(0); |
| 84 | + ShardRouting unassignedShardRouting2Primary = routingTable.shardRoutingTable(testShardId2).primaryShard(); |
| 85 | + ShardRouting unassignedShardRouting2Replica = routingTable.shardRoutingTable(testShardId2).replicaShards().get(0); |
| 86 | + |
| 87 | + assertFalse(unassignedShardRouting1Primary.assignedToNode()); |
| 88 | + assertFalse(unassignedShardRouting1Replica.assignedToNode()); |
| 89 | + assertFalse(unassignedShardRouting2Primary.assignedToNode()); |
| 90 | + assertFalse(unassignedShardRouting2Replica.assignedToNode()); |
| 91 | + |
| 92 | + return new TestHarness( |
| 93 | + clusterState, |
| 94 | + mutableRoutingNodes, |
| 95 | + mutableRoutingNode1, |
| 96 | + mutableRoutingNode2, |
| 97 | + unassignedShardRouting1Primary, |
| 98 | + unassignedShardRouting1Replica, |
| 99 | + unassignedShardRouting2Primary, |
| 100 | + unassignedShardRouting2Replica |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + public void testPrimaryAndReplicaThrottlingNotSimulation() { |
| 105 | + /* Create cluster state for multiple nodes and an index with _unassigned_ shards. */ |
| 106 | + TestHarness harness = setUpTwoNodesAndIndexWithTwoUnassignedPrimariesAndReplicas(); |
| 107 | + |
| 108 | + /* Decider Testing */ |
| 109 | + |
| 110 | + // Set up RoutingAllocation in non-simulation mode. |
| 111 | + var routingAllocation = new RoutingAllocation( |
| 112 | + null, |
| 113 | + harness.mutableRoutingNodes, |
| 114 | + harness.clusterState, |
| 115 | + ClusterInfo.builder().build(), |
| 116 | + null, |
| 117 | + System.nanoTime(), |
| 118 | + false // Turn off isSimulating |
| 119 | + ); |
| 120 | + |
| 121 | + final RoutingChangesObserver NOOP = new RoutingChangesObserver() { |
| 122 | + }; |
| 123 | + Settings settings = Settings.builder() |
| 124 | + .put("cluster.routing.allocation.unthrottle_replica_assignment_in_simulation", randomBoolean() ? true : false) |
| 125 | + .put("cluster.routing.allocation.node_concurrent_recoveries", 1) |
| 126 | + .put("cluster.routing.allocation.node_initial_primaries_recoveries", 1) |
| 127 | + .build(); |
| 128 | + assertFalse(routingAllocation.isSimulating()); |
| 129 | + ThrottlingAllocationDecider decider = new ThrottlingAllocationDecider(ClusterSettings.createBuiltInClusterSettings(settings)); |
| 130 | + |
| 131 | + // A single primary can be allocated. |
| 132 | + assertThat( |
| 133 | + decider.canAllocate(harness.unassignedShardRouting1Primary, harness.mutableRoutingNode1, routingAllocation), |
| 134 | + equalTo(Decision.YES) |
| 135 | + ); |
| 136 | + var shardRouting1PrimaryInitializing = harness.mutableRoutingNodes.initializeShard( |
| 137 | + harness.unassignedShardRouting1Primary, |
| 138 | + harness.mutableRoutingNode1.nodeId(), |
| 139 | + null, |
| 140 | + 0, |
| 141 | + NOOP |
| 142 | + ); |
| 143 | + |
| 144 | + // Leaving the first shard's primary in an INITIALIZING state should THROTTLE further allocation. |
| 145 | + // Only 1 concurrent allocation is allowed. |
| 146 | + assertThat( |
| 147 | + decider.canAllocate(harness.unassignedShardRouting2Primary, harness.mutableRoutingNode1, routingAllocation), |
| 148 | + equalTo(Decision.THROTTLE) |
| 149 | + ); |
| 150 | + |
| 151 | + // The first shard's replica should receive a simple NO because the corresponding primary is not active yet. |
| 152 | + assertThat( |
| 153 | + decider.canAllocate(harness.unassignedShardRouting1Replica, harness.mutableRoutingNode2, routingAllocation), |
| 154 | + equalTo(Decision.NO) |
| 155 | + ); |
| 156 | + |
| 157 | + // Start the first shard's primary, and initialize the second shard's primary to again reach the 1 concurrency limit. |
| 158 | + harness.mutableRoutingNodes.startShard(shardRouting1PrimaryInitializing, NOOP, 0); |
| 159 | + assertThat( |
| 160 | + decider.canAllocate(harness.unassignedShardRouting2Primary, harness.mutableRoutingNode2, routingAllocation), |
| 161 | + equalTo(Decision.YES) |
| 162 | + ); |
| 163 | + harness.mutableRoutingNodes.initializeShard( |
| 164 | + harness.unassignedShardRouting2Primary, |
| 165 | + harness.mutableRoutingNode2.nodeId(), |
| 166 | + null, |
| 167 | + 0, |
| 168 | + NOOP |
| 169 | + ); |
| 170 | + |
| 171 | + // The first shard's replica should receive THROTTLE now, since the primary is active. |
| 172 | + // There is still already 1 allocation in progress, which is the limit. |
| 173 | + assertThat( |
| 174 | + decider.canAllocate(harness.unassignedShardRouting1Replica, harness.mutableRoutingNode2, routingAllocation), |
| 175 | + equalTo(Decision.THROTTLE) |
| 176 | + ); |
| 177 | + } |
| 178 | + |
| 179 | + public void testPrimaryAndReplicaThrottlingInSimulation() { |
| 180 | + /* Create cluster state for multiple nodes and an index with _unassigned_ shards. */ |
| 181 | + TestHarness harness = setUpTwoNodesAndIndexWithTwoUnassignedPrimariesAndReplicas(); |
| 182 | + var mutableRoutingNodes = harness.clusterState.mutableRoutingNodes(); |
| 183 | + |
| 184 | + /* Decider Testing */ |
| 185 | + |
| 186 | + // Set up RoutingAllocation in simulation mode. |
| 187 | + var routingAllocation = new RoutingAllocation( |
| 188 | + null, |
| 189 | + mutableRoutingNodes, |
| 190 | + harness.clusterState, |
| 191 | + ClusterInfo.builder().build(), |
| 192 | + null, |
| 193 | + System.nanoTime(), |
| 194 | + true // Turn on isSimulating |
| 195 | + ); |
| 196 | + |
| 197 | + final RoutingChangesObserver NOOP = new RoutingChangesObserver() { |
| 198 | + }; |
| 199 | + Settings settings = Settings.builder() |
| 200 | + .put("cluster.routing.allocation.unthrottle_replica_assignment_in_simulation", true) |
| 201 | + .put("cluster.routing.allocation.node_concurrent_recoveries", 1) |
| 202 | + .put("cluster.routing.allocation.node_initial_primaries_recoveries", 1) |
| 203 | + .build(); |
| 204 | + assertTrue(routingAllocation.isSimulating()); |
| 205 | + ThrottlingAllocationDecider decider = new ThrottlingAllocationDecider(ClusterSettings.createBuiltInClusterSettings(settings)); |
| 206 | + |
| 207 | + // Primary path is unthrottled during simulation, regardless of the `node_initial_primaries_recoveries` setting |
| 208 | + assertThat( |
| 209 | + decider.canAllocate(harness.unassignedShardRouting1Primary, harness.mutableRoutingNode1, routingAllocation), |
| 210 | + equalTo(Decision.YES) |
| 211 | + ); |
| 212 | + mutableRoutingNodes.initializeShard(harness.unassignedShardRouting1Primary, harness.mutableRoutingNode1.nodeId(), null, 0, NOOP); |
| 213 | + assertThat( |
| 214 | + decider.canAllocate(harness.unassignedShardRouting2Primary, harness.mutableRoutingNode1, routingAllocation), |
| 215 | + equalTo(Decision.YES) |
| 216 | + ); |
| 217 | + mutableRoutingNodes.initializeShard(harness.unassignedShardRouting2Primary, harness.mutableRoutingNode1.nodeId(), null, 0, NOOP); |
| 218 | + |
| 219 | + // Replica path is unthrottled during simulation AND `unthrottle_replica_assignment_in_simulation` is set to true. |
| 220 | + assertThat( |
| 221 | + decider.canAllocate(harness.unassignedShardRouting1Replica, harness.mutableRoutingNode2, routingAllocation), |
| 222 | + equalTo(Decision.YES) |
| 223 | + ); |
| 224 | + mutableRoutingNodes.initializeShard(harness.unassignedShardRouting1Replica, harness.mutableRoutingNode2.nodeId(), null, 0, NOOP); |
| 225 | + assertThat( |
| 226 | + decider.canAllocate(harness.unassignedShardRouting2Replica, harness.mutableRoutingNode2, routingAllocation), |
| 227 | + equalTo(Decision.YES) |
| 228 | + ); |
| 229 | + mutableRoutingNodes.initializeShard(harness.unassignedShardRouting2Replica, harness.mutableRoutingNode2.nodeId(), null, 0, NOOP); |
| 230 | + |
| 231 | + // Note: INITIALIZING was chosen above, not STARTED, because the BalancedShardsAllocator only initializes. We want that path to be |
| 232 | + // unthrottled in simulation. |
| 233 | + } |
| 234 | +} |
0 commit comments