@@ -47,16 +47,19 @@ public class DesiredBalanceReconciler {
47
47
private final RoutingAllocation allocation ; // name chosen to align with code in BalancedShardsAllocator but TODO rename
48
48
private final RoutingNodes routingNodes ;
49
49
private final NodeAllocationOrdering allocationOrdering ;
50
+ private final NodeAllocationOrdering moveOrdering ;
50
51
51
52
DesiredBalanceReconciler (
52
53
DesiredBalance desiredBalance ,
53
54
RoutingAllocation routingAllocation ,
54
- NodeAllocationOrdering allocationOrdering
55
+ NodeAllocationOrdering allocationOrdering ,
56
+ NodeAllocationOrdering moveOrdering
55
57
) {
56
58
this .desiredBalance = desiredBalance ;
57
59
this .allocation = routingAllocation ;
58
60
this .routingNodes = routingAllocation .routingNodes ();
59
61
this .allocationOrdering = allocationOrdering ;
62
+ this .moveOrdering = moveOrdering ;
60
63
}
61
64
62
65
void run () {
@@ -220,9 +223,7 @@ private void allocateUnassigned() {
220
223
final var decision = allocation .deciders ().canAllocate (shard , routingNode , allocation );
221
224
switch (decision .type ()) {
222
225
case YES -> {
223
- if (logger .isTraceEnabled ()) {
224
- logger .trace ("Assigned shard [{}] to [{}]" , shard , desiredNodeId );
225
- }
226
+ logger .debug ("Assigning shard [{}] to [{}]" , shard , desiredNodeId );
226
227
final long shardSize = DiskThresholdDecider .getExpectedShardSize (
227
228
shard ,
228
229
ShardRouting .UNAVAILABLE_EXPECTED_SHARD_SIZE ,
@@ -253,9 +254,7 @@ private void allocateUnassigned() {
253
254
}
254
255
}
255
256
256
- if (logger .isTraceEnabled ()) {
257
- logger .trace ("No eligible node found to assign shard [{}] amongst [{}]" , shard , assignment );
258
- }
257
+ logger .debug ("No eligible node found to assign shard [{}] amongst [{}]" , shard , assignment );
259
258
260
259
final UnassignedInfo .AllocationStatus allocationStatus ;
261
260
if (assignment == null || assignment .isIgnored (shard .primary ())) {
@@ -284,20 +283,16 @@ private void allocateUnassigned() {
284
283
285
284
private Iterable <String > getDesiredNodesIds (ShardRouting shard , ShardAssignment assignment ) {
286
285
return allocationOrdering .sort (allocation .deciders ().getForcedInitialShardAllocationToNodes (shard , allocation ).map (forced -> {
287
- if (logger .isDebugEnabled ()) {
288
- logger .debug ("Shard [{}] assignment is ignored. Initial allocation forced to {}" , shard .shardId (), forced );
289
- }
286
+ logger .debug ("Shard [{}] assignment is ignored. Initial allocation forced to {}" , shard .shardId (), forced );
290
287
return forced ;
291
288
}).orElse (assignment .nodeIds ()));
292
289
}
293
290
294
291
private Iterable <String > getFallbackNodeIds (ShardRouting shard , AtomicBoolean isThrottled ) {
295
292
return () -> {
296
293
if (shard .primary () && isThrottled .get () == false ) {
297
- var fallbackNodeIds = allocation .routingNodes ().stream ().map (RoutingNode ::nodeId ).toList ();
298
- if (logger .isDebugEnabled ()) {
299
- logger .trace ("Shard [{}] assignment is temporary not possible. Falling back to {}" , shard .shardId (), fallbackNodeIds );
300
- }
294
+ var fallbackNodeIds = allocation .routingNodes ().getAllNodeIds ();
295
+ logger .debug ("Shard [{}] assignment is temporary not possible. Falling back to {}" , shard .shardId (), fallbackNodeIds );
301
296
return allocationOrdering .sort (fallbackNodeIds ).iterator ();
302
297
} else {
303
298
return Collections .emptyIterator ();
@@ -306,10 +301,9 @@ private Iterable<String> getFallbackNodeIds(ShardRouting shard, AtomicBoolean is
306
301
}
307
302
308
303
private void moveShards () {
309
- // Iterate over the started shards interleaving between nodes, and check if they can remain. In the presence of throttling
310
- // shard movements, the goal of this iteration order is to achieve a fairer movement of shards from the nodes that are
311
- // offloading the shards.
312
- for (final var iterator = routingNodes .nodeInterleavedShardIterator (); iterator .hasNext ();) {
304
+ // Iterate over all started shards and check if they can remain. In the presence of throttling shard movements,
305
+ // the goal of this iteration order is to achieve a fairer movement of shards from the nodes that are offloading the shards.
306
+ for (final var iterator = OrderedShardsIterator .create (routingNodes , moveOrdering ); iterator .hasNext ();) {
313
307
final var shardRouting = iterator .next ();
314
308
315
309
if (shardRouting .started () == false ) {
@@ -343,12 +337,15 @@ private void moveShards() {
343
337
344
338
final var moveTarget = findRelocationTarget (shardRouting , assignment .nodeIds ());
345
339
if (moveTarget != null ) {
340
+ logger .debug ("Moving shard {} from {} to {}" , shardRouting .shardId (), shardRouting .currentNodeId (), moveTarget .getId ());
346
341
routingNodes .relocateOrReinitializeShard (
347
342
shardRouting ,
348
343
moveTarget .getId (),
349
344
allocation .clusterInfo ().getShardSize (shardRouting , ShardRouting .UNAVAILABLE_EXPECTED_SHARD_SIZE ),
350
345
allocation .changes ()
351
346
);
347
+ iterator .dePrioritizeNode (shardRouting .currentNodeId ());
348
+ moveOrdering .recordAllocation (shardRouting .currentNodeId ());
352
349
}
353
350
}
354
351
}
@@ -358,10 +355,9 @@ private void balance() {
358
355
return ;
359
356
}
360
357
361
- // Iterate over the started shards interleaving between nodes, and try to move any which are on undesired nodes. In the presence of
362
- // throttling shard movements, the goal of this iteration order is to achieve a fairer movement of shards from the nodes that are
363
- // offloading the shards.
364
- for (final var iterator = routingNodes .nodeInterleavedShardIterator (); iterator .hasNext ();) {
358
+ // Iterate over all started shards and try to move any which are on undesired nodes. In the presence of throttling shard movements,
359
+ // the goal of this iteration order is to achieve a fairer movement of shards from the nodes that are offloading the shards.
360
+ for (final var iterator = OrderedShardsIterator .create (routingNodes , moveOrdering ); iterator .hasNext ();) {
365
361
final var shardRouting = iterator .next ();
366
362
367
363
if (shardRouting .started () == false ) {
@@ -392,12 +388,21 @@ private void balance() {
392
388
393
389
final var rebalanceTarget = findRelocationTarget (shardRouting , assignment .nodeIds (), this ::decideCanAllocate );
394
390
if (rebalanceTarget != null ) {
391
+ logger .debug (
392
+ "Rebalancing shard {} from {} to {}" ,
393
+ shardRouting .shardId (),
394
+ shardRouting .currentNodeId (),
395
+ rebalanceTarget .getId ()
396
+ );
397
+
395
398
routingNodes .relocateOrReinitializeShard (
396
399
shardRouting ,
397
400
rebalanceTarget .getId (),
398
401
allocation .clusterInfo ().getShardSize (shardRouting , ShardRouting .UNAVAILABLE_EXPECTED_SHARD_SIZE ),
399
402
allocation .changes ()
400
403
);
404
+ iterator .dePrioritizeNode (shardRouting .currentNodeId ());
405
+ moveOrdering .recordAllocation (shardRouting .currentNodeId ());
401
406
}
402
407
}
403
408
}
0 commit comments