Skip to content

Commit 5034d5d

Browse files
[Fix Bug] Allocation and Rebalance Constraints of WeightFunction are incorrectly reset (opensearch-project#19012)
* fix primary balance strategy unset Signed-off-by: guojialiang <guojialiang.2012@bytedance.com> * Add change log Signed-off-by: guojialiang <guojialiang.2012@bytedance.com> * refactor Signed-off-by: guojialiang <guojialiang.2012@bytedance.com> * refactor Signed-off-by: guojialiang <guojialiang.2012@bytedance.com> * refactor Signed-off-by: guojialiang <guojialiang.2012@bytedance.com> --------- Signed-off-by: guojialiang <guojialiang.2012@bytedance.com>
1 parent df704b7 commit 5034d5d

5 files changed

Lines changed: 68 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1515
- Omit maxScoreCollector in SimpleTopDocsCollectorContext when concurrent segment search enabled ([#19584](https://github.com/opensearch-project/OpenSearch/pull/19584))
1616

1717
### Fixed
18+
- Fix Allocation and Rebalance Constraints of WeightFunction are incorrectly reset ([#19012](https://github.com/opensearch-project/OpenSearch/pull/19012))
1819
- Fix flaky test FieldDataLoadingIT.testIndicesFieldDataCacheSizeSetting ([#19571](https://github.com/opensearch-project/OpenSearch/pull/19571))
1920
- Avoid primary shard failure caused by merged segment warmer exceptions ([#19436](https://github.com/opensearch-project/OpenSearch/pull/19436))
2021

server/src/internalClusterTest/java/org/opensearch/indices/replication/SegmentReplicationAllocationIT.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ public void testSingleIndexShardAllocation() throws Exception {
161161
}
162162
enablePreferPrimaryBalance();
163163

164+
// Modify other configurations, expecting that the primary balance strategy will not be affected.
165+
assertAcked(
166+
client().admin()
167+
.cluster()
168+
.prepareUpdateSettings()
169+
.setPersistentSettings(Settings.builder().put(PRIMARY_SHARD_REBALANCE_BUFFER.getKey(), 0.2))
170+
);
171+
164172
ClusterState state;
165173
createIndex("test", maxShardCount, maxReplicaCount, true);
166174
ensureGreen(TimeValue.timeValueSeconds(60));

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ private void updateWeightFunction() {
340340
this.indexBalanceFactor,
341341
this.shardBalanceFactor,
342342
this.preferPrimaryShardRebalanceBuffer,
343-
this.primaryConstraintThreshold
343+
this.primaryConstraintThreshold,
344+
this.preferPrimaryShardBalance,
345+
this.preferPrimaryShardRebalance
344346
);
345347
}
346348

@@ -552,7 +554,14 @@ static class WeightFunction {
552554
private AllocationConstraints constraints;
553555
private RebalanceConstraints rebalanceConstraints;
554556

555-
WeightFunction(float indexBalance, float shardBalance, float preferPrimaryBalanceBuffer, long primaryConstraintThreshold) {
557+
WeightFunction(
558+
float indexBalance,
559+
float shardBalance,
560+
float preferPrimaryBalanceBuffer,
561+
long primaryConstraintThreshold,
562+
boolean preferPrimaryShardBalance,
563+
boolean preferPrimaryShardRebalance
564+
) {
556565
float sum = indexBalance + shardBalance;
557566
if (sum <= 0.0f) {
558567
throw new IllegalArgumentException("Balance factors must sum to a value > 0 but was: " + sum);
@@ -567,6 +576,10 @@ static class WeightFunction {
567576
this.rebalanceConstraints = new RebalanceConstraints(rebalanceParameter);
568577
// Enable index shard per node breach constraint
569578
updateAllocationConstraint(INDEX_SHARD_PER_NODE_BREACH_CONSTRAINT_ID, true);
579+
updateAllocationConstraint(INDEX_PRIMARY_SHARD_BALANCE_CONSTRAINT_ID, preferPrimaryShardBalance);
580+
updateAllocationConstraint(CLUSTER_PRIMARY_SHARD_BALANCE_CONSTRAINT_ID, preferPrimaryShardBalance);
581+
updateRebalanceConstraint(INDEX_PRIMARY_SHARD_BALANCE_CONSTRAINT_ID, preferPrimaryShardBalance);
582+
updateRebalanceConstraint(CLUSTER_PRIMARY_SHARD_REBALANCE_CONSTRAINT_ID, preferPrimaryShardRebalance);
570583
}
571584

572585
public float weightWithAllocationConstraints(ShardsBalancer balancer, ModelNode node, String index) {

server/src/test/java/org/opensearch/cluster/routing/allocation/BalanceConfigurationTests.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,35 @@ public void testPrimaryBalanceWithPreferPrimaryBalanceSetting() {
235235
final int numberOfRuns = 5;
236236
int balanceFailed = 0;
237237

238-
AllocationService strategy = createAllocationService(getSettingsBuilderForPrimaryBalance().build(), new TestGatewayAllocator());
238+
Settings settings = getSettingsBuilderForPrimaryBalance().build();
239+
ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
240+
AllocationService strategy = createAllocationService(
241+
settings,
242+
clusterSettings,
243+
new TestGatewayAllocator(),
244+
SNAPSHOT_INFO_SERVICE_WITH_NO_SHARD_SIZES
245+
);
246+
for (int i = 0; i < numberOfRuns; i++) {
247+
ClusterState clusterState = initCluster(strategy, numberOfIndices, numberOfNodes, numberOfShards, numberOfReplicas);
248+
clusterState = removeOneNode(clusterState, strategy);
249+
logger.info(ShardAllocations.printShardDistribution(clusterState));
250+
try {
251+
verifyPerIndexPrimaryBalance(clusterState);
252+
} catch (AssertionError e) {
253+
balanceFailed++;
254+
logger.info("Unexpected assertion failure");
255+
}
256+
}
257+
assertTrue(balanceFailed <= 1);
258+
259+
// Update settings & apply
260+
Settings updatedSettings = getSettingsBuilderForPrimaryBalance().put(
261+
BalancedShardsAllocator.PRIMARY_SHARD_REBALANCE_BUFFER.getKey(),
262+
BalancedShardsAllocator.PRIMARY_SHARD_REBALANCE_BUFFER.get(settings) + 0.01f
263+
).build();
264+
clusterSettings.applySettings(updatedSettings);
265+
266+
// Double check primary shard balance should still work
239267
for (int i = 0; i < numberOfRuns; i++) {
240268
ClusterState clusterState = initCluster(strategy, numberOfIndices, numberOfNodes, numberOfShards, numberOfReplicas);
241269
clusterState = removeOneNode(clusterState, strategy);

test/framework/src/main/java/org/opensearch/cluster/OpenSearchAllocationTestCase.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,21 @@ public static MockAllocationService createAllocationService(
142142
);
143143
}
144144

145+
public static MockAllocationService createAllocationService(
146+
Settings settings,
147+
ClusterSettings clusterSettings,
148+
GatewayAllocator gatewayAllocator,
149+
SnapshotsInfoService snapshotsInfoService
150+
) {
151+
return new MockAllocationService(
152+
randomAllocationDeciders(settings, EMPTY_CLUSTER_SETTINGS, random()),
153+
gatewayAllocator,
154+
new BalancedShardsAllocator(settings, clusterSettings),
155+
EmptyClusterInfoService.INSTANCE,
156+
snapshotsInfoService
157+
);
158+
}
159+
145160
public static AllocationDeciders randomAllocationDeciders(Settings settings, ClusterSettings clusterSettings, Random random) {
146161
List<AllocationDecider> deciders = new ArrayList<>(
147162
ClusterModule.createAllocationDeciders(settings, clusterSettings, Collections.emptyList())

0 commit comments

Comments
 (0)