|
27 | 27 | import org.elasticsearch.cluster.metadata.MetaData;
|
28 | 28 | import org.elasticsearch.cluster.node.DiscoveryNode;
|
29 | 29 | import org.elasticsearch.cluster.node.DiscoveryNodes;
|
| 30 | +import org.elasticsearch.cluster.routing.AllocationId; |
30 | 31 | import org.elasticsearch.cluster.routing.IndexRoutingTable;
|
31 | 32 | import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
|
32 | 33 | import org.elasticsearch.cluster.routing.RoutingTable;
|
33 | 34 | import org.elasticsearch.cluster.routing.RoutingTable.Builder;
|
| 35 | +import org.elasticsearch.cluster.routing.ShardRouting; |
34 | 36 | import org.elasticsearch.cluster.routing.ShardRoutingState;
|
35 | 37 | import org.elasticsearch.cluster.routing.TestShardRouting;
|
36 | 38 | import org.elasticsearch.cluster.routing.UnassignedInfo;
|
|
44 | 46 | import java.util.HashSet;
|
45 | 47 | import java.util.List;
|
46 | 48 | import java.util.Set;
|
| 49 | +import java.util.stream.Collectors; |
47 | 50 |
|
48 | 51 | import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_CREATION_DATE;
|
49 | 52 | import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
|
@@ -93,7 +96,8 @@ public static ClusterState state(String index, boolean activePrimaryLocal, Shard
|
93 | 96 | IndexMetaData indexMetaData = IndexMetaData.builder(index).settings(Settings.builder()
|
94 | 97 | .put(SETTING_VERSION_CREATED, Version.CURRENT)
|
95 | 98 | .put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, numberOfReplicas)
|
96 |
| - .put(SETTING_CREATION_DATE, System.currentTimeMillis())).primaryTerm(0, primaryTerm).build(); |
| 99 | + .put(SETTING_CREATION_DATE, System.currentTimeMillis())).primaryTerm(0, primaryTerm) |
| 100 | + .build(); |
97 | 101 |
|
98 | 102 | RoutingTable.Builder routing = new RoutingTable.Builder();
|
99 | 103 | routing.addAsNew(indexMetaData);
|
@@ -138,12 +142,19 @@ public static ClusterState state(String index, boolean activePrimaryLocal, Shard
|
138 | 142 | TestShardRouting.newShardRouting(index, shardId.id(), replicaNode, relocatingNode, false, replicaState,
|
139 | 143 | unassignedInfo));
|
140 | 144 | }
|
| 145 | + final IndexShardRoutingTable indexShardRoutingTable = indexShardRoutingBuilder.build(); |
| 146 | + |
| 147 | + IndexMetaData.Builder indexMetaDataBuilder = new IndexMetaData.Builder(indexMetaData); |
| 148 | + indexMetaDataBuilder.putInSyncAllocationIds(0, |
| 149 | + indexShardRoutingTable.activeShards().stream().map(ShardRouting::allocationId).map(AllocationId::getId) |
| 150 | + .collect(Collectors.toSet()) |
| 151 | + ); |
141 | 152 |
|
142 | 153 | ClusterState.Builder state = ClusterState.builder(new ClusterName("test"));
|
143 | 154 | state.nodes(discoBuilder);
|
144 |
| - state.metaData(MetaData.builder().put(indexMetaData, false).generateClusterUuidIfNeeded()); |
| 155 | + state.metaData(MetaData.builder().put(indexMetaDataBuilder.build(), false).generateClusterUuidIfNeeded()); |
145 | 156 | state.routingTable(RoutingTable.builder().add(IndexRoutingTable.builder(indexMetaData.getIndex())
|
146 |
| - .addIndexShard(indexShardRoutingBuilder.build())).build()); |
| 157 | + .addIndexShard(indexShardRoutingTable)).build()); |
147 | 158 | return state.build();
|
148 | 159 | }
|
149 | 160 |
|
@@ -272,21 +283,21 @@ public static ClusterState stateWithAssignedPrimariesAndOneReplica(String index,
|
272 | 283 | state.routingTable(RoutingTable.builder().add(indexRoutingTableBuilder.build()).build());
|
273 | 284 | return state.build();
|
274 | 285 | }
|
275 |
| - |
276 |
| - |
| 286 | + |
| 287 | + |
277 | 288 | /**
|
278 | 289 | * Creates cluster state with several indexes, shards and replicas and all shards STARTED.
|
279 | 290 | */
|
280 | 291 | public static ClusterState stateWithAssignedPrimariesAndReplicas(String[] indices, int numberOfShards, int numberOfReplicas) {
|
281 | 292 |
|
282 |
| - int numberOfDataNodes = numberOfReplicas + 1; |
| 293 | + int numberOfDataNodes = numberOfReplicas + 1; |
283 | 294 | DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder();
|
284 | 295 | for (int i = 0; i < numberOfDataNodes + 1; i++) {
|
285 | 296 | final DiscoveryNode node = newNode(i);
|
286 | 297 | discoBuilder = discoBuilder.add(node);
|
287 | 298 | }
|
288 | 299 | discoBuilder.localNodeId(newNode(0).getId());
|
289 |
| - discoBuilder.masterNodeId(newNode(numberOfDataNodes + 1).getId()); |
| 300 | + discoBuilder.masterNodeId(newNode(numberOfDataNodes + 1).getId()); |
290 | 301 | ClusterState.Builder state = ClusterState.builder(new ClusterName("test"));
|
291 | 302 | state.nodes(discoBuilder);
|
292 | 303 | Builder routingTableBuilder = RoutingTable.builder();
|
@@ -316,7 +327,7 @@ public static ClusterState stateWithAssignedPrimariesAndReplicas(String[] indice
|
316 | 327 | state.metaData(metadataBuilder);
|
317 | 328 | state.routingTable(routingTableBuilder.build());
|
318 | 329 | return state.build();
|
319 |
| - } |
| 330 | + } |
320 | 331 |
|
321 | 332 | /**
|
322 | 333 | * Creates cluster state with and index that has one shard and as many replicas as numberOfReplicas.
|
|
0 commit comments