|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.cluster.routing.allocation; |
| 10 | + |
| 11 | +import org.opensearch.Version; |
| 12 | +import org.opensearch.action.support.replication.ClusterStateCreationUtils; |
| 13 | +import org.opensearch.cluster.ClusterState; |
| 14 | +import org.opensearch.cluster.OpenSearchAllocationTestCase; |
| 15 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 16 | +import org.opensearch.cluster.metadata.Metadata; |
| 17 | +import org.opensearch.cluster.node.DiscoveryNode; |
| 18 | +import org.opensearch.cluster.node.DiscoveryNodes; |
| 19 | +import org.opensearch.cluster.routing.AllocationId; |
| 20 | +import org.opensearch.cluster.routing.IndexRoutingTable; |
| 21 | +import org.opensearch.cluster.routing.IndexShardRoutingTable; |
| 22 | +import org.opensearch.cluster.routing.RoutingNode; |
| 23 | +import org.opensearch.cluster.routing.RoutingTable; |
| 24 | +import org.opensearch.cluster.routing.ShardRouting; |
| 25 | +import org.opensearch.cluster.routing.ShardRoutingState; |
| 26 | +import org.opensearch.cluster.routing.TestShardRouting; |
| 27 | +import org.opensearch.cluster.routing.UnassignedInfo; |
| 28 | +import org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator; |
| 29 | +import org.opensearch.cluster.routing.allocation.decider.AllocationDecider; |
| 30 | +import org.opensearch.cluster.routing.allocation.decider.AllocationDeciders; |
| 31 | +import org.opensearch.cluster.routing.allocation.decider.Decision; |
| 32 | +import org.opensearch.common.settings.Settings; |
| 33 | +import org.opensearch.core.index.shard.ShardId; |
| 34 | + |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.HashSet; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Set; |
| 39 | +import java.util.stream.Collectors; |
| 40 | + |
| 41 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_CREATION_DATE; |
| 42 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS; |
| 43 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS; |
| 44 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_VERSION_CREATED; |
| 45 | +import static org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator.IGNORE_THROTTLE_FOR_REMOTE_RESTORE; |
| 46 | + |
| 47 | +public class DecideAllocateUnassignedTests extends OpenSearchAllocationTestCase { |
| 48 | + public void testAllocateUnassignedRemoteRestore_IgnoreThrottle() { |
| 49 | + final String[] indices = { "idx1" }; |
| 50 | + // Create a cluster state with 1 indices, each with 1 started primary shard, and only |
| 51 | + // one node initially so that all primary shards get allocated to the same node. |
| 52 | + // |
| 53 | + // When we add 1 more 1 index with 1 started primary shard and 1 more node , if the new node throttles the recovery |
| 54 | + // shard should get assigned on the older node if IgnoreThrottle is set to true |
| 55 | + ClusterState clusterState = ClusterStateCreationUtils.state(1, indices, 1); |
| 56 | + clusterState = addNodesToClusterState(clusterState, 1); |
| 57 | + clusterState = addRestoringIndexToClusterState(clusterState, "idx2"); |
| 58 | + List<AllocationDecider> allocationDeciders = getAllocationDecidersThrottleOnNode1(); |
| 59 | + RoutingAllocation routingAllocation = newRoutingAllocation(new AllocationDeciders(allocationDeciders), clusterState); |
| 60 | + // allocate and get the node that is now relocating |
| 61 | + Settings build = Settings.builder().put(IGNORE_THROTTLE_FOR_REMOTE_RESTORE.getKey(), true).build(); |
| 62 | + BalancedShardsAllocator allocator = new BalancedShardsAllocator(build); |
| 63 | + allocator.allocate(routingAllocation); |
| 64 | + assertEquals(routingAllocation.routingNodes().shardsWithState(ShardRoutingState.INITIALIZING).get(0).currentNodeId(), "node_0"); |
| 65 | + assertEquals(routingAllocation.routingNodes().shardsWithState(ShardRoutingState.INITIALIZING).get(0).getIndexName(), "idx2"); |
| 66 | + assertFalse(routingAllocation.routingNodes().hasUnassignedPrimaries()); |
| 67 | + } |
| 68 | + |
| 69 | + public void testAllocateUnassignedRemoteRestore() { |
| 70 | + final String[] indices = { "idx1" }; |
| 71 | + // Create a cluster state with 1 indices, each with 1 started primary shard, and only |
| 72 | + // one node initially so that all primary shards get allocated to the same node. |
| 73 | + // |
| 74 | + // When we add 1 more 1 index with 1 started primary shard and 1 more node , if the new node throttles the recovery |
| 75 | + // shard should remain unassigned if IgnoreThrottle is set to false |
| 76 | + ClusterState clusterState = ClusterStateCreationUtils.state(1, indices, 1); |
| 77 | + clusterState = addNodesToClusterState(clusterState, 1); |
| 78 | + clusterState = addRestoringIndexToClusterState(clusterState, "idx2"); |
| 79 | + List<AllocationDecider> allocationDeciders = getAllocationDecidersThrottleOnNode1(); |
| 80 | + RoutingAllocation routingAllocation = newRoutingAllocation(new AllocationDeciders(allocationDeciders), clusterState); |
| 81 | + // allocate and get the node that is now relocating |
| 82 | + Settings build = Settings.builder().put(IGNORE_THROTTLE_FOR_REMOTE_RESTORE.getKey(), false).build(); |
| 83 | + BalancedShardsAllocator allocator = new BalancedShardsAllocator(build); |
| 84 | + allocator.allocate(routingAllocation); |
| 85 | + assertEquals(routingAllocation.routingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size(), 0); |
| 86 | + assertTrue(routingAllocation.routingNodes().hasUnassignedPrimaries()); |
| 87 | + } |
| 88 | + |
| 89 | + private static List<AllocationDecider> getAllocationDecidersThrottleOnNode1() { |
| 90 | + // Allocation Deciders to throttle on `node_1` |
| 91 | + final Set<String> throttleNodes = new HashSet<>(); |
| 92 | + throttleNodes.add("node_1"); |
| 93 | + AllocationDecider allocationDecider = new AllocationDecider() { |
| 94 | + @Override |
| 95 | + public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { |
| 96 | + if (throttleNodes.contains(node.nodeId())) { |
| 97 | + return Decision.THROTTLE; |
| 98 | + } |
| 99 | + return Decision.YES; |
| 100 | + } |
| 101 | + }; |
| 102 | + List<AllocationDecider> allocationDeciders = Arrays.asList(allocationDecider); |
| 103 | + return allocationDeciders; |
| 104 | + } |
| 105 | + |
| 106 | + private ClusterState addNodesToClusterState(ClusterState clusterState, int nodeId) { |
| 107 | + DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder(clusterState.nodes()); |
| 108 | + DiscoveryNode discoveryNode = newNode("node_" + nodeId); |
| 109 | + nodesBuilder.add(discoveryNode); |
| 110 | + return ClusterState.builder(clusterState).nodes(nodesBuilder).build(); |
| 111 | + } |
| 112 | + |
| 113 | + private ClusterState addRestoringIndexToClusterState(ClusterState clusterState, String index) { |
| 114 | + final int primaryTerm = 1 + randomInt(200); |
| 115 | + final ShardId shardId = new ShardId(index, "_na_", 0); |
| 116 | + |
| 117 | + IndexMetadata indexMetadata = IndexMetadata.builder(index) |
| 118 | + .settings( |
| 119 | + Settings.builder() |
| 120 | + .put(SETTING_VERSION_CREATED, Version.CURRENT) |
| 121 | + .put(SETTING_NUMBER_OF_SHARDS, 1) |
| 122 | + .put(SETTING_NUMBER_OF_REPLICAS, 0) |
| 123 | + .put(SETTING_CREATION_DATE, System.currentTimeMillis()) |
| 124 | + ) |
| 125 | + .primaryTerm(0, primaryTerm) |
| 126 | + .build(); |
| 127 | + |
| 128 | + IndexShardRoutingTable.Builder indexShardRoutingBuilder = new IndexShardRoutingTable.Builder(shardId); |
| 129 | + UnassignedInfo unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.EXISTING_INDEX_RESTORED, null); |
| 130 | + indexShardRoutingBuilder.addShard( |
| 131 | + TestShardRouting.newShardRoutingRemoteRestore(index, shardId, null, null, true, ShardRoutingState.UNASSIGNED, unassignedInfo) |
| 132 | + ); |
| 133 | + final IndexShardRoutingTable indexShardRoutingTable = indexShardRoutingBuilder.build(); |
| 134 | + |
| 135 | + IndexMetadata.Builder indexMetadataBuilder = new IndexMetadata.Builder(indexMetadata); |
| 136 | + indexMetadataBuilder.putInSyncAllocationIds( |
| 137 | + 0, |
| 138 | + indexShardRoutingTable.activeShards() |
| 139 | + .stream() |
| 140 | + .map(ShardRouting::allocationId) |
| 141 | + .map(AllocationId::getId) |
| 142 | + .collect(Collectors.toSet()) |
| 143 | + ); |
| 144 | + ClusterState.Builder state = ClusterState.builder(clusterState); |
| 145 | + state.metadata(Metadata.builder(clusterState.metadata()).put(indexMetadataBuilder.build(), false).generateClusterUuidIfNeeded()); |
| 146 | + state.routingTable( |
| 147 | + RoutingTable.builder(clusterState.routingTable()) |
| 148 | + .add(IndexRoutingTable.builder(indexMetadata.getIndex()).addIndexShard(indexShardRoutingTable)) |
| 149 | + .build() |
| 150 | + ); |
| 151 | + return state.build(); |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments