@@ -139,6 +139,14 @@ public class BalancedShardsAllocator implements ShardsAllocator {
139
139
Property .NodeScope
140
140
);
141
141
142
+ public static final Setting <Long > PRIMARY_CONSTRAINT_THRESHOLD_SETTING = Setting .longSetting (
143
+ "cluster.routing.allocation.primary_constraint.threshold" ,
144
+ 10 ,
145
+ 0 ,
146
+ Property .Dynamic ,
147
+ Property .NodeScope
148
+ );
149
+
142
150
/**
143
151
* This setting governs whether primary shards balance is desired during allocation. This is used by {@link ConstraintTypes#isPerIndexPrimaryShardsPerNodeBreached()}
144
152
* and {@link ConstraintTypes#isPrimaryShardsPerNodeBreached} which is used during unassigned shard allocation
@@ -201,6 +209,7 @@ public class BalancedShardsAllocator implements ShardsAllocator {
201
209
private volatile float shardBalanceFactor ;
202
210
private volatile WeightFunction weightFunction ;
203
211
private volatile float threshold ;
212
+ private volatile long primaryConstraintThreshold ;
204
213
205
214
private volatile boolean ignoreThrottleInRestore ;
206
215
private volatile TimeValue allocatorTimeout ;
@@ -219,6 +228,7 @@ public BalancedShardsAllocator(Settings settings, ClusterSettings clusterSetting
219
228
setIgnoreThrottleInRestore (IGNORE_THROTTLE_FOR_REMOTE_RESTORE .get (settings ));
220
229
updateWeightFunction ();
221
230
setThreshold (THRESHOLD_SETTING .get (settings ));
231
+ setPrimaryConstraintThresholdSetting (PRIMARY_CONSTRAINT_THRESHOLD_SETTING .get (settings ));
222
232
setPreferPrimaryShardBalance (PREFER_PRIMARY_SHARD_BALANCE .get (settings ));
223
233
setPreferPrimaryShardRebalance (PREFER_PRIMARY_SHARD_REBALANCE .get (settings ));
224
234
setShardMovementStrategy (SHARD_MOVEMENT_STRATEGY_SETTING .get (settings ));
@@ -231,6 +241,7 @@ public BalancedShardsAllocator(Settings settings, ClusterSettings clusterSetting
231
241
clusterSettings .addSettingsUpdateConsumer (PRIMARY_SHARD_REBALANCE_BUFFER , this ::updatePreferPrimaryShardBalanceBuffer );
232
242
clusterSettings .addSettingsUpdateConsumer (PREFER_PRIMARY_SHARD_REBALANCE , this ::setPreferPrimaryShardRebalance );
233
243
clusterSettings .addSettingsUpdateConsumer (THRESHOLD_SETTING , this ::setThreshold );
244
+ clusterSettings .addSettingsUpdateConsumer (PRIMARY_CONSTRAINT_THRESHOLD_SETTING , this ::setPrimaryConstraintThresholdSetting );
234
245
clusterSettings .addSettingsUpdateConsumer (IGNORE_THROTTLE_FOR_REMOTE_RESTORE , this ::setIgnoreThrottleInRestore );
235
246
clusterSettings .addSettingsUpdateConsumer (ALLOCATOR_TIMEOUT_SETTING , this ::setAllocatorTimeout );
236
247
}
@@ -294,7 +305,12 @@ private void updatePreferPrimaryShardBalanceBuffer(float preferPrimaryShardBalan
294
305
}
295
306
296
307
private void updateWeightFunction () {
297
- weightFunction = new WeightFunction (this .indexBalanceFactor , this .shardBalanceFactor , this .preferPrimaryShardRebalanceBuffer );
308
+ weightFunction = new WeightFunction (
309
+ this .indexBalanceFactor ,
310
+ this .shardBalanceFactor ,
311
+ this .preferPrimaryShardRebalanceBuffer ,
312
+ this .primaryConstraintThreshold
313
+ );
298
314
}
299
315
300
316
/**
@@ -317,6 +333,11 @@ private void setThreshold(float threshold) {
317
333
this .threshold = threshold ;
318
334
}
319
335
336
+ private void setPrimaryConstraintThresholdSetting (long threshold ) {
337
+ this .primaryConstraintThreshold = threshold ;
338
+ this .weightFunction .updatePrimaryConstraintThreshold (threshold );
339
+ }
340
+
320
341
private void setAllocatorTimeout (TimeValue allocatorTimeout ) {
321
342
this .allocatorTimeout = allocatorTimeout ;
322
343
}
@@ -489,10 +510,11 @@ static class WeightFunction {
489
510
private final float shardBalance ;
490
511
private final float theta0 ;
491
512
private final float theta1 ;
513
+ private long primaryConstraintThreshold ;
492
514
private AllocationConstraints constraints ;
493
515
private RebalanceConstraints rebalanceConstraints ;
494
516
495
- WeightFunction (float indexBalance , float shardBalance , float preferPrimaryBalanceBuffer ) {
517
+ WeightFunction (float indexBalance , float shardBalance , float preferPrimaryBalanceBuffer , long primaryConstraintThreshold ) {
496
518
float sum = indexBalance + shardBalance ;
497
519
if (sum <= 0.0f ) {
498
520
throw new IllegalArgumentException ("Balance factors must sum to a value > 0 but was: " + sum );
@@ -501,6 +523,7 @@ static class WeightFunction {
501
523
theta1 = indexBalance / sum ;
502
524
this .indexBalance = indexBalance ;
503
525
this .shardBalance = shardBalance ;
526
+ this .primaryConstraintThreshold = primaryConstraintThreshold ;
504
527
RebalanceParameter rebalanceParameter = new RebalanceParameter (preferPrimaryBalanceBuffer );
505
528
this .constraints = new AllocationConstraints ();
506
529
this .rebalanceConstraints = new RebalanceConstraints (rebalanceParameter );
@@ -510,12 +533,12 @@ static class WeightFunction {
510
533
511
534
public float weightWithAllocationConstraints (ShardsBalancer balancer , ModelNode node , String index ) {
512
535
float balancerWeight = weight (balancer , node , index );
513
- return balancerWeight + constraints .weight (balancer , node , index );
536
+ return balancerWeight + constraints .weight (balancer , node , index , primaryConstraintThreshold );
514
537
}
515
538
516
539
public float weightWithRebalanceConstraints (ShardsBalancer balancer , ModelNode node , String index ) {
517
540
float balancerWeight = weight (balancer , node , index );
518
- return balancerWeight + rebalanceConstraints .weight (balancer , node , index );
541
+ return balancerWeight + rebalanceConstraints .weight (balancer , node , index , primaryConstraintThreshold );
519
542
}
520
543
521
544
float weight (ShardsBalancer balancer , ModelNode node , String index ) {
@@ -531,6 +554,10 @@ void updateAllocationConstraint(String constraint, boolean enable) {
531
554
void updateRebalanceConstraint (String constraint , boolean add ) {
532
555
this .rebalanceConstraints .updateRebalanceConstraint (constraint , add );
533
556
}
557
+
558
+ void updatePrimaryConstraintThreshold (long primaryConstraintThreshold ) {
559
+ this .primaryConstraintThreshold = primaryConstraintThreshold ;
560
+ }
534
561
}
535
562
536
563
/**
0 commit comments