Skip to content

Commit 6e7d2e0

Browse files
committed
Move balancer settings testing to use BalancerSettings
1 parent 8bc0aba commit 6e7d2e0

File tree

4 files changed

+27
-31
lines changed

4 files changed

+27
-31
lines changed

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -239,27 +239,6 @@ private void failAllocationOfNewPrimaries(RoutingAllocation allocation) {
239239
}
240240
}
241241

242-
/**
243-
* Returns the currently configured delta threshold
244-
*/
245-
public float getThreshold() {
246-
return balancerSettings.getThreshold();
247-
}
248-
249-
/**
250-
* Returns the index related weight factor.
251-
*/
252-
public float getIndexBalance() {
253-
return balancerSettings.getIndexBalanceFactor();
254-
}
255-
256-
/**
257-
* Returns the shard related weight factor.
258-
*/
259-
public float getShardBalance() {
260-
return balancerSettings.getShardBalanceFactor();
261-
}
262-
263242
/**
264243
* A {@link Balancer}
265244
*/

server/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancerSettings.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ public BalancerSettings(ClusterSettings clusterSettings) {
3434
clusterSettings.initializeAndWatch(THRESHOLD_SETTING, value -> this.threshold = value);
3535
}
3636

37+
/**
38+
* Returns the index related weight factor.
39+
*/
3740
public float getIndexBalanceFactor() {
3841
return indexBalanceFactor;
3942
}
4043

44+
/**
45+
* Returns the shard related weight factor.
46+
*/
4147
public float getShardBalanceFactor() {
4248
return shardBalanceFactor;
4349
}
@@ -50,6 +56,9 @@ public float getDiskUsageBalanceFactor() {
5056
return diskUsageBalanceFactor;
5157
}
5258

59+
/**
60+
* Returns the currently configured delta threshold
61+
*/
5362
public float getThreshold() {
5463
return threshold;
5564
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.cluster.routing.RoutingTable;
2323
import org.elasticsearch.cluster.routing.ShardRouting;
2424
import org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator;
25+
import org.elasticsearch.cluster.routing.allocation.allocator.BalancerSettings;
2526
import org.elasticsearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider;
2627
import org.elasticsearch.common.settings.ClusterSettings;
2728
import org.elasticsearch.common.settings.Settings;
@@ -286,9 +287,9 @@ public void testPersistedSettings() {
286287
settings.put(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.getKey(), 0.3);
287288
settings.put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), 2.0);
288289
ClusterSettings service = new ClusterSettings(settings.build(), ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
289-
BalancedShardsAllocator allocator = new BalancedShardsAllocator(service, WriteLoadForecaster.DEFAULT);
290-
assertThat(allocator.getIndexBalance(), Matchers.equalTo(0.2f));
291-
assertThat(allocator.getShardBalance(), Matchers.equalTo(0.3f));
290+
BalancerSettings allocator = new BalancerSettings(service);
291+
assertThat(allocator.getIndexBalanceFactor(), Matchers.equalTo(0.2f));
292+
assertThat(allocator.getShardBalanceFactor(), Matchers.equalTo(0.3f));
292293
assertThat(allocator.getThreshold(), Matchers.equalTo(2.0f));
293294

294295
settings = Settings.builder();
@@ -300,17 +301,17 @@ public void testPersistedSettings() {
300301
ClusterRebalanceAllocationDecider.ClusterRebalanceType.ALWAYS.toString()
301302
);
302303
service.applySettings(settings.build());
303-
assertThat(allocator.getIndexBalance(), Matchers.equalTo(0.2f));
304-
assertThat(allocator.getShardBalance(), Matchers.equalTo(0.3f));
304+
assertThat(allocator.getIndexBalanceFactor(), Matchers.equalTo(0.2f));
305+
assertThat(allocator.getShardBalanceFactor(), Matchers.equalTo(0.3f));
305306
assertThat(allocator.getThreshold(), Matchers.equalTo(2.0f));
306307

307308
settings = Settings.builder();
308309
settings.put(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING.getKey(), 0.5);
309310
settings.put(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING.getKey(), 0.1);
310311
settings.put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), 3.0);
311312
service.applySettings(settings.build());
312-
assertThat(allocator.getIndexBalance(), Matchers.equalTo(0.5f));
313-
assertThat(allocator.getShardBalance(), Matchers.equalTo(0.1f));
313+
assertThat(allocator.getIndexBalanceFactor(), Matchers.equalTo(0.5f));
314+
assertThat(allocator.getShardBalanceFactor(), Matchers.equalTo(0.1f));
314315
assertThat(allocator.getThreshold(), Matchers.equalTo(3.0f));
315316
}
316317
}

server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancedShardsAllocatorTests.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,14 +558,21 @@ public void testThresholdLimit() {
558558
final var badValue = (float) randomDoubleBetween(0.0, Math.nextDown(1.0f), true);
559559
expectThrows(
560560
IllegalArgumentException.class,
561-
() -> new BalancedShardsAllocator(Settings.builder().put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), badValue).build())
561+
() -> new BalancerSettings(
562+
ClusterSettings.createBuiltInClusterSettings(
563+
Settings.builder().put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), badValue).build()
564+
)
565+
)
562566
);
563567

564568
final var goodValue = (float) randomDoubleBetween(1.0, 10.0, true);
565569
assertEquals(
566570
goodValue,
567-
new BalancedShardsAllocator(Settings.builder().put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), goodValue).build())
568-
.getThreshold(),
571+
new BalancerSettings(
572+
ClusterSettings.createBuiltInClusterSettings(
573+
Settings.builder().put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), goodValue).build()
574+
)
575+
).getThreshold(),
569576
0.0f
570577
);
571578
}

0 commit comments

Comments
 (0)