|
10 | 10 |
|
11 | 11 | import org.elasticsearch.action.support.replication.ClusterStateCreationUtils; |
12 | 12 | import org.elasticsearch.cluster.ClusterState; |
| 13 | +import org.elasticsearch.cluster.routing.IndexRoutingTable; |
| 14 | +import org.elasticsearch.cluster.routing.IndexShardRoutingTable; |
13 | 15 | import org.elasticsearch.cluster.routing.RoutingNode; |
| 16 | +import org.elasticsearch.cluster.routing.RoutingTable; |
14 | 17 | import org.elasticsearch.cluster.routing.ShardRouting; |
15 | 18 | import org.elasticsearch.cluster.routing.ShardRoutingState; |
16 | 19 | import org.elasticsearch.cluster.routing.UnassignedInfo; |
@@ -114,6 +117,33 @@ public void testFindAnyUnassignedShardToExplain() { |
114 | 117 | shard = findShardToExplain(request, routingAllocation(clusterState)); |
115 | 118 | assertEquals(clusterState.getRoutingTable().index("idx").shard(0).replicaShards().get(0), shard); |
116 | 119 |
|
| 120 | + // prefer unassigned primary to replica |
| 121 | + clusterState = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(new String[]{"idx1", "idx2"}, 1, 1); |
| 122 | + final String redIndex = randomBoolean() ? "idx1" : "idx2"; |
| 123 | + final RoutingTable.Builder routingTableBuilder = RoutingTable.builder(clusterState.routingTable()); |
| 124 | + for (final IndexRoutingTable indexRoutingTable : clusterState.routingTable()) { |
| 125 | + final IndexRoutingTable.Builder indexBuilder = new IndexRoutingTable.Builder(indexRoutingTable.getIndex()); |
| 126 | + for (final IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) { |
| 127 | + final IndexShardRoutingTable.Builder shardBuilder = new IndexShardRoutingTable.Builder(indexShardRoutingTable.shardId()); |
| 128 | + for (final ShardRouting shardRouting : indexShardRoutingTable) { |
| 129 | + if (shardRouting.primary() == false || indexRoutingTable.getIndex().getName().equals(redIndex)) { |
| 130 | + // move all replicas and one primary to unassigned |
| 131 | + shardBuilder.addShard(shardRouting.moveToUnassigned(new UnassignedInfo( |
| 132 | + UnassignedInfo.Reason.ALLOCATION_FAILED, |
| 133 | + "test"))); |
| 134 | + } else { |
| 135 | + shardBuilder.addShard(shardRouting); |
| 136 | + } |
| 137 | + } |
| 138 | + indexBuilder.addIndexShard(shardBuilder.build()); |
| 139 | + } |
| 140 | + routingTableBuilder.add(indexBuilder); |
| 141 | + } |
| 142 | + clusterState = ClusterState.builder(clusterState).routingTable(routingTableBuilder.build()).build(); |
| 143 | + request = new ClusterAllocationExplainRequest(); |
| 144 | + shard = findShardToExplain(request, routingAllocation(clusterState)); |
| 145 | + assertEquals(clusterState.getRoutingTable().index(redIndex).shard(0).primaryShard(), shard); |
| 146 | + |
117 | 147 | // no unassigned shard to explain |
118 | 148 | final ClusterState allStartedClusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), |
119 | 149 | ShardRoutingState.STARTED, ShardRoutingState.STARTED); |
|
0 commit comments