Skip to content

Commit 12364d5

Browse files
committed
Using gt to make constraint test during rebalance
Signed-off-by: panguixin <[email protected]>
1 parent c82fffe commit 12364d5

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

server/src/main/java/org/opensearch/cluster/routing/allocation/AllocationConstraints.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public class AllocationConstraints {
3131
public AllocationConstraints() {
3232
this.constraints = new HashMap<>();
3333
this.constraints.put(INDEX_SHARD_PER_NODE_BREACH_CONSTRAINT_ID, new Constraint(isIndexShardsPerNodeBreached()));
34-
this.constraints.put(INDEX_PRIMARY_SHARD_BALANCE_CONSTRAINT_ID, new Constraint(isPerIndexPrimaryShardsPerNodeBreached()));
35-
this.constraints.put(CLUSTER_PRIMARY_SHARD_BALANCE_CONSTRAINT_ID, new Constraint(isPrimaryShardsPerNodeBreached(0.0f)));
34+
this.constraints.put(INDEX_PRIMARY_SHARD_BALANCE_CONSTRAINT_ID, new Constraint(isPerIndexPrimaryShardsPerNodeBreached(false)));
35+
this.constraints.put(CLUSTER_PRIMARY_SHARD_BALANCE_CONSTRAINT_ID, new Constraint(isPrimaryShardsPerNodeBreached(false, 0.0f)));
3636
}
3737

3838
public void updateAllocationConstraint(String constraint, boolean enable) {

server/src/main/java/org/opensearch/cluster/routing/allocation/ConstraintTypes.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ public static Predicate<Constraint.ConstraintParams> isIndexShardsPerNodeBreache
6666
* {@link ConstraintTypes#CONSTRAINT_WEIGHT} is assigned to node resulting in lesser chances of node being selected
6767
* as allocation or rebalancing target
6868
*/
69-
public static Predicate<Constraint.ConstraintParams> isPerIndexPrimaryShardsPerNodeBreached() {
69+
public static Predicate<Constraint.ConstraintParams> isPerIndexPrimaryShardsPerNodeBreached(boolean rebalance) {
7070
return (params) -> {
7171
int perIndexPrimaryShardCount = params.getNode().numPrimaryShards(params.getIndex());
7272
int perIndexAllowedPrimaryShardCount = (int) Math.ceil(params.getBalancer().avgPrimaryShardsPerNode(params.getIndex()));
73-
return perIndexPrimaryShardCount >= perIndexAllowedPrimaryShardCount;
73+
return perIndexPrimaryShardCount > (perIndexAllowedPrimaryShardCount + (rebalance ? 0 : 1));
7474
};
7575
}
7676

@@ -79,11 +79,11 @@ public static Predicate<Constraint.ConstraintParams> isPerIndexPrimaryShardsPerN
7979
* constraint is used in weight calculation during allocation/rebalance both. When breached a high weight {@link ConstraintTypes#CONSTRAINT_WEIGHT}
8080
* is assigned to node resulting in lesser chances of node being selected as allocation/rebalance target
8181
*/
82-
public static Predicate<Constraint.ConstraintParams> isPrimaryShardsPerNodeBreached(float buffer) {
82+
public static Predicate<Constraint.ConstraintParams> isPrimaryShardsPerNodeBreached(boolean rebalance, float buffer) {
8383
return (params) -> {
8484
int primaryShardCount = params.getNode().numPrimaryShards();
8585
int allowedPrimaryShardCount = (int) Math.ceil(params.getBalancer().avgPrimaryShardsPerNode() * (1 + buffer));
86-
return primaryShardCount >= allowedPrimaryShardCount;
86+
return primaryShardCount > (allowedPrimaryShardCount + (rebalance ? 0 : 1));
8787
};
8888
}
8989

server/src/main/java/org/opensearch/cluster/routing/allocation/RebalanceConstraints.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public class RebalanceConstraints {
3131

3232
public RebalanceConstraints(RebalanceParameter rebalanceParameter) {
3333
this.constraints = new HashMap<>();
34-
this.constraints.put(INDEX_PRIMARY_SHARD_BALANCE_CONSTRAINT_ID, new Constraint(isPerIndexPrimaryShardsPerNodeBreached()));
34+
this.constraints.put(INDEX_PRIMARY_SHARD_BALANCE_CONSTRAINT_ID, new Constraint(isPerIndexPrimaryShardsPerNodeBreached(true)));
3535
this.constraints.put(
3636
CLUSTER_PRIMARY_SHARD_REBALANCE_CONSTRAINT_ID,
37-
new Constraint(isPrimaryShardsPerNodeBreached(rebalanceParameter.getPreferPrimaryBalanceBuffer()))
37+
new Constraint(isPrimaryShardsPerNodeBreached(true, rebalanceParameter.getPreferPrimaryBalanceBuffer()))
3838
);
3939
}
4040

server/src/main/java/org/opensearch/cluster/routing/allocation/allocator/BalancedShardsAllocator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public class BalancedShardsAllocator implements ShardsAllocator {
148148
);
149149

150150
/**
151-
* This setting governs whether primary shards balance is desired during allocation. This is used by {@link ConstraintTypes#isPerIndexPrimaryShardsPerNodeBreached()}
151+
* This setting governs whether primary shards balance is desired during allocation. This is used by {@link ConstraintTypes#isPerIndexPrimaryShardsPerNodeBreached(boolean)}
152152
* and {@link ConstraintTypes#isPrimaryShardsPerNodeBreached} which is used during unassigned shard allocation
153153
* {@link LocalShardsBalancer#allocateUnassigned()} and shard re-balance/relocation to a different node via {@link LocalShardsBalancer#balance()} .
154154
*/

0 commit comments

Comments
 (0)