@@ -85,7 +85,10 @@ private void allocateAllUnassignedBatch(final RoutingAllocation allocation) {
85
85
final RoutingNodes .UnassignedShards .UnassignedIterator iterator = allocation .routingNodes ().unassigned ().iterator ();
86
86
List <ShardRouting > shardsToBatch = new ArrayList <>();
87
87
while (iterator .hasNext ()) {
88
- shardsToBatch .add (iterator .next ());
88
+ ShardRouting unassignedShardRouting = iterator .next ();
89
+ if (unassignedShardRouting .primary ()) {
90
+ shardsToBatch .add (unassignedShardRouting );
91
+ }
89
92
}
90
93
batchAllocator .allocateUnassignedBatch (shardsToBatch , allocation );
91
94
}
@@ -180,6 +183,35 @@ public void testInitializePrimaryShards() {
180
183
assertEquals (2 , routingAllocation .routingNodes ().getInitialPrimariesIncomingRecoveries (node1 .getId ()));
181
184
}
182
185
186
+ public void testInitializeOnlyPrimaryUnassignedShardsIgnoreReplicaShards () {
187
+ ClusterSettings clusterSettings = new ClusterSettings (Settings .EMPTY , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
188
+ AllocationDeciders allocationDeciders = randomAllocationDeciders (Settings .builder ().build (), clusterSettings , random ());
189
+ setUpShards (1 );
190
+ final RoutingAllocation routingAllocation = routingAllocationWithOnePrimary (allocationDeciders , CLUSTER_RECOVERED , "allocId-0" );
191
+
192
+ for (ShardId shardId : shardsInBatch ) {
193
+ batchAllocator .addShardData (
194
+ node1 ,
195
+ "allocId-0" ,
196
+ shardId ,
197
+ true ,
198
+ new ReplicationCheckpoint (shardId , 20 , 101 , 1 , Codec .getDefault ().getName ()),
199
+ null
200
+ );
201
+ }
202
+
203
+ allocateAllUnassignedBatch (routingAllocation );
204
+
205
+ List <ShardRouting > initializingShards = routingAllocation .routingNodes ().shardsWithState (ShardRoutingState .INITIALIZING );
206
+ assertEquals (1 , initializingShards .size ());
207
+ assertTrue (shardsInBatch .contains (initializingShards .get (0 ).shardId ()));
208
+ assertTrue (initializingShards .get (0 ).primary ());
209
+ assertEquals (1 , routingAllocation .routingNodes ().getInitialPrimariesIncomingRecoveries (node1 .getId ()));
210
+ List <ShardRouting > unassignedShards = routingAllocation .routingNodes ().shardsWithState (ShardRoutingState .UNASSIGNED );
211
+ assertEquals (1 , unassignedShards .size ());
212
+ assertTrue (!unassignedShards .get (0 ).primary ());
213
+ }
214
+
183
215
public void testAllocateUnassignedBatchThrottlingAllocationDeciderIsHonoured () {
184
216
ClusterSettings clusterSettings = new ClusterSettings (Settings .EMPTY , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
185
217
AllocationDeciders allocationDeciders = randomAllocationDeciders (
@@ -258,7 +290,7 @@ private RoutingAllocation routingAllocationWithOnePrimary(
258
290
.routingTable (routingTableBuilder .build ())
259
291
.nodes (DiscoveryNodes .builder ().add (node1 ).add (node2 ).add (node3 ))
260
292
.build ();
261
- return new RoutingAllocation (deciders , new RoutingNodes (state , false ), state , null , null , System .nanoTime ());
293
+ return new RoutingAllocation (deciders , new RoutingNodes (state , false ), state , ClusterInfo . EMPTY , null , System .nanoTime ());
262
294
}
263
295
264
296
private RoutingAllocation routingAllocationWithMultiplePrimaries (
0 commit comments