|
| 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 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.cluster.routing.allocation.decider; |
| 10 | + |
| 11 | +import org.elasticsearch.Version; |
| 12 | +import org.elasticsearch.cluster.ClusterName; |
| 13 | +import org.elasticsearch.cluster.ClusterState; |
| 14 | +import org.elasticsearch.cluster.ESAllocationTestCase; |
| 15 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 16 | +import org.elasticsearch.cluster.metadata.Metadata; |
| 17 | +import org.elasticsearch.cluster.node.DiscoveryNodes; |
| 18 | +import org.elasticsearch.cluster.routing.IndexRoutingTable; |
| 19 | +import org.elasticsearch.cluster.routing.RoutingTable; |
| 20 | +import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; |
| 21 | +import org.elasticsearch.index.Index; |
| 22 | +import org.elasticsearch.index.shard.ShardId; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import static org.elasticsearch.cluster.routing.ShardRoutingState.INITIALIZING; |
| 27 | +import static org.elasticsearch.cluster.routing.ShardRoutingState.RELOCATING; |
| 28 | +import static org.elasticsearch.cluster.routing.ShardRoutingState.STARTED; |
| 29 | +import static org.elasticsearch.cluster.routing.ShardRoutingState.UNASSIGNED; |
| 30 | +import static org.elasticsearch.cluster.routing.TestShardRouting.newShardRouting; |
| 31 | +import static org.elasticsearch.cluster.routing.allocation.decider.RebalanceOnlyWhenActiveAllocationDecider.NO_SOME_REPLICAS_INACTIVE; |
| 32 | +import static org.elasticsearch.cluster.routing.allocation.decider.RebalanceOnlyWhenActiveAllocationDecider.YES_ALL_REPLICAS_ACTIVE; |
| 33 | +import static org.hamcrest.Matchers.equalTo; |
| 34 | + |
| 35 | +public class RebalanceOnlyWhenActiveAllocationDeciderTests extends ESAllocationTestCase { |
| 36 | + |
| 37 | + public void testAllowRebalanceWhenAllShardsActive() { |
| 38 | + |
| 39 | + var index = new Index("test", "_na_"); |
| 40 | + var primary = newShardRouting(new ShardId(index, 0), "node-1", true, STARTED); |
| 41 | + var replica = newShardRouting(new ShardId(index, 0), "node-2", false, STARTED); |
| 42 | + |
| 43 | + var state = ClusterState.builder(ClusterName.DEFAULT) |
| 44 | + .metadata(Metadata.builder().put(IndexMetadata.builder(index.getName()).settings(indexSettings(Version.CURRENT, 1, 1)))) |
| 45 | + .nodes(DiscoveryNodes.builder().add(newNode("node-1")).add(newNode("node-2")).add(newNode("node-3"))) |
| 46 | + .routingTable(RoutingTable.builder().add(IndexRoutingTable.builder(index).addShard(primary).addShard(replica))) |
| 47 | + .build(); |
| 48 | + |
| 49 | + var allocation = createRoutingAllocation(state); |
| 50 | + var decider = new RebalanceOnlyWhenActiveAllocationDecider(); |
| 51 | + |
| 52 | + assertThat(decider.canRebalance(primary, allocation), equalTo(YES_ALL_REPLICAS_ACTIVE)); |
| 53 | + assertThat(decider.canRebalance(replica, allocation), equalTo(YES_ALL_REPLICAS_ACTIVE)); |
| 54 | + } |
| 55 | + |
| 56 | + public void testDoNotAllowRebalanceWhenSomeShardsAreNotActive() { |
| 57 | + |
| 58 | + var index = new Index("test", "_na_"); |
| 59 | + var primary = newShardRouting(new ShardId(index, 0), "node-1", true, STARTED); |
| 60 | + var replica = randomBoolean() |
| 61 | + ? newShardRouting(new ShardId(index, 0), null, false, UNASSIGNED) |
| 62 | + : newShardRouting(new ShardId(index, 0), "node-2", false, INITIALIZING); |
| 63 | + |
| 64 | + var state = ClusterState.builder(ClusterName.DEFAULT) |
| 65 | + .metadata(Metadata.builder().put(IndexMetadata.builder(index.getName()).settings(indexSettings(Version.CURRENT, 1, 1)))) |
| 66 | + .nodes(DiscoveryNodes.builder().add(newNode("node-1")).add(newNode("node-2")).add(newNode("node-3"))) |
| 67 | + .routingTable(RoutingTable.builder().add(IndexRoutingTable.builder(index).addShard(primary).addShard(replica))) |
| 68 | + .build(); |
| 69 | + |
| 70 | + var allocation = createRoutingAllocation(state); |
| 71 | + var decider = new RebalanceOnlyWhenActiveAllocationDecider(); |
| 72 | + |
| 73 | + assertThat(decider.canRebalance(primary, allocation), equalTo(NO_SOME_REPLICAS_INACTIVE)); |
| 74 | + } |
| 75 | + |
| 76 | + public void testDoNotAllowRebalanceWhenSomeShardsAreNotActiveAndRebalancing() { |
| 77 | + |
| 78 | + var index = new Index("test", "_na_"); |
| 79 | + var primary = newShardRouting(new ShardId(index, 0), "node-1", true, STARTED); |
| 80 | + var replica1 = newShardRouting(new ShardId(index, 0), "node-2", "node-3", false, RELOCATING); |
| 81 | + var replica2 = newShardRouting(new ShardId(index, 0), null, false, UNASSIGNED); |
| 82 | + |
| 83 | + var state = ClusterState.builder(ClusterName.DEFAULT) |
| 84 | + .metadata(Metadata.builder().put(IndexMetadata.builder(index.getName()).settings(indexSettings(Version.CURRENT, 1, 2)))) |
| 85 | + .nodes(DiscoveryNodes.builder().add(newNode("node-1")).add(newNode("node-2")).add(newNode("node-3"))) |
| 86 | + .routingTable( |
| 87 | + RoutingTable.builder().add(IndexRoutingTable.builder(index).addShard(primary).addShard(replica1).addShard(replica2)) |
| 88 | + ) |
| 89 | + .build(); |
| 90 | + |
| 91 | + var allocation = createRoutingAllocation(state); |
| 92 | + var decider = new RebalanceOnlyWhenActiveAllocationDecider(); |
| 93 | + |
| 94 | + assertThat(decider.canRebalance(primary, allocation), equalTo(NO_SOME_REPLICAS_INACTIVE)); |
| 95 | + } |
| 96 | + |
| 97 | + public void testAllowConcurrentRebalance() { |
| 98 | + |
| 99 | + var index = new Index("test", "_na_"); |
| 100 | + var primary = newShardRouting(new ShardId(index, 0), "node-1", true, STARTED); |
| 101 | + var replica = newShardRouting(new ShardId(index, 0), "node-2", "node-3", false, RELOCATING); |
| 102 | + |
| 103 | + var state = ClusterState.builder(ClusterName.DEFAULT) |
| 104 | + .metadata(Metadata.builder().put(IndexMetadata.builder(index.getName()).settings(indexSettings(Version.CURRENT, 1, 1)))) |
| 105 | + .nodes(DiscoveryNodes.builder().add(newNode("node-1")).add(newNode("node-2")).add(newNode("node-3"))) |
| 106 | + .routingTable(RoutingTable.builder().add(IndexRoutingTable.builder(index).addShard(primary).addShard(replica))) |
| 107 | + .build(); |
| 108 | + |
| 109 | + var allocation = createRoutingAllocation(state); |
| 110 | + var decider = new RebalanceOnlyWhenActiveAllocationDecider(); |
| 111 | + |
| 112 | + assertThat(decider.canRebalance(primary, allocation), equalTo(YES_ALL_REPLICAS_ACTIVE)); |
| 113 | + } |
| 114 | + |
| 115 | + private static RoutingAllocation createRoutingAllocation(ClusterState state) { |
| 116 | + return new RoutingAllocation(new AllocationDeciders(List.of()), state, null, null, 0L); |
| 117 | + } |
| 118 | +} |
0 commit comments