Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.elasticsearch.cluster.routing.allocation.NodeAllocationStatsAndWeightsCalculator;
import org.elasticsearch.cluster.routing.allocation.WriteLoadForecaster;
import org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator;
import org.elasticsearch.cluster.routing.allocation.allocator.BalancerSettings;
import org.elasticsearch.cluster.routing.allocation.allocator.DesiredBalanceShardsAllocator;
import org.elasticsearch.cluster.routing.allocation.allocator.DesiredBalanceShardsAllocator.DesiredBalanceReconcilerAction;
import org.elasticsearch.cluster.routing.allocation.allocator.ShardsAllocator;
Expand Down Expand Up @@ -144,13 +145,12 @@ public ClusterModule(
this.clusterPlugins = clusterPlugins;
this.deciderList = createAllocationDeciders(settings, clusterService.getClusterSettings(), clusterPlugins);
this.allocationDeciders = new AllocationDeciders(deciderList);
var nodeAllocationStatsAndWeightsCalculator = new NodeAllocationStatsAndWeightsCalculator(
writeLoadForecaster,
clusterService.getClusterSettings()
);
final BalancerSettings balancerSettings = new BalancerSettings(clusterService.getClusterSettings());
var nodeAllocationStatsAndWeightsCalculator = new NodeAllocationStatsAndWeightsCalculator(writeLoadForecaster, balancerSettings);
this.shardsAllocator = createShardsAllocator(
settings,
clusterService.getClusterSettings(),
balancerSettings,
threadPool,
clusterPlugins,
clusterService,
Expand Down Expand Up @@ -438,6 +438,7 @@ private static void addAllocationDecider(Map<Class<?>, AllocationDecider> decide
private static ShardsAllocator createShardsAllocator(
Settings settings,
ClusterSettings clusterSettings,
BalancerSettings balancerSettings,
ThreadPool threadPool,
List<ClusterPlugin> clusterPlugins,
ClusterService clusterService,
Expand All @@ -447,12 +448,12 @@ private static ShardsAllocator createShardsAllocator(
NodeAllocationStatsAndWeightsCalculator nodeAllocationStatsAndWeightsCalculator
) {
Map<String, Supplier<ShardsAllocator>> allocators = new HashMap<>();
allocators.put(BALANCED_ALLOCATOR, () -> new BalancedShardsAllocator(clusterSettings, writeLoadForecaster));
allocators.put(BALANCED_ALLOCATOR, () -> new BalancedShardsAllocator(balancerSettings, writeLoadForecaster));
allocators.put(
DESIRED_BALANCE_ALLOCATOR,
() -> new DesiredBalanceShardsAllocator(
clusterSettings,
new BalancedShardsAllocator(clusterSettings, writeLoadForecaster),
new BalancedShardsAllocator(balancerSettings, writeLoadForecaster),
threadPool,
clusterService,
reconciler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
import org.elasticsearch.cluster.routing.RoutingNode;
import org.elasticsearch.cluster.routing.RoutingNodes;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator;
import org.elasticsearch.cluster.routing.allocation.allocator.BalancerSettings;
import org.elasticsearch.cluster.routing.allocation.allocator.DesiredBalance;
import org.elasticsearch.cluster.routing.allocation.allocator.WeightFunction;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.core.Nullable;

Expand All @@ -29,11 +28,7 @@
*/
public class NodeAllocationStatsAndWeightsCalculator {
private final WriteLoadForecaster writeLoadForecaster;

private volatile float indexBalanceFactor;
private volatile float shardBalanceFactor;
private volatile float writeLoadBalanceFactor;
private volatile float diskUsageBalanceFactor;
private final BalancerSettings balancerSettings;

/**
* Node shard allocation stats and the total node weight.
Expand All @@ -47,18 +42,9 @@ public record NodeAllocationStatsAndWeight(
float currentNodeWeight
) {}

public NodeAllocationStatsAndWeightsCalculator(WriteLoadForecaster writeLoadForecaster, ClusterSettings clusterSettings) {
public NodeAllocationStatsAndWeightsCalculator(WriteLoadForecaster writeLoadForecaster, BalancerSettings balancerSettings) {
this.writeLoadForecaster = writeLoadForecaster;
clusterSettings.initializeAndWatch(BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING, value -> this.shardBalanceFactor = value);
clusterSettings.initializeAndWatch(BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING, value -> this.indexBalanceFactor = value);
clusterSettings.initializeAndWatch(
BalancedShardsAllocator.WRITE_LOAD_BALANCE_FACTOR_SETTING,
value -> this.writeLoadBalanceFactor = value
);
clusterSettings.initializeAndWatch(
BalancedShardsAllocator.DISK_USAGE_BALANCE_FACTOR_SETTING,
value -> this.diskUsageBalanceFactor = value
);
this.balancerSettings = balancerSettings;
}

/**
Expand All @@ -74,7 +60,12 @@ public Map<String, NodeAllocationStatsAndWeight> nodesAllocationStatsAndWeights(
// must not use licensed features when just starting up
writeLoadForecaster.refreshLicense();
}
var weightFunction = new WeightFunction(shardBalanceFactor, indexBalanceFactor, writeLoadBalanceFactor, diskUsageBalanceFactor);
var weightFunction = new WeightFunction(
balancerSettings.getShardBalanceFactor(),
balancerSettings.getIndexBalanceFactor(),
balancerSettings.getWriteLoadBalanceFactor(),
balancerSettings.getDiskUsageBalanceFactor()
);
var avgShardsPerNode = WeightFunction.avgShardPerNode(metadata, routingNodes);
var avgWriteLoadPerNode = WeightFunction.avgWriteLoadPerNode(writeLoadForecaster, metadata, routingNodes);
var avgDiskUsageInBytesPerNode = WeightFunction.avgDiskUsageInBytesPerNode(clusterInfo, metadata, routingNodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders;
import org.elasticsearch.cluster.routing.allocation.decider.Decision;
import org.elasticsearch.cluster.routing.allocation.decider.Decision.Type;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Setting.Property;
import org.elasticsearch.common.settings.Settings;
Expand All @@ -60,7 +59,6 @@
import static org.elasticsearch.cluster.metadata.SingleNodeShutdownMetadata.Type.REPLACE;
import static org.elasticsearch.cluster.routing.ExpectedShardSizeEstimator.getExpectedShardSize;
import static org.elasticsearch.cluster.routing.ShardRoutingState.RELOCATING;
import static org.elasticsearch.common.settings.ClusterSettings.createBuiltInClusterSettings;

/**
* The {@link BalancedShardsAllocator} allocates and balances shards on the cluster nodes using {@link WeightFunction}.
Expand Down Expand Up @@ -114,34 +112,20 @@ public class BalancedShardsAllocator implements ShardsAllocator {
Property.NodeScope
);

// TODO: deduplicate these fields, use the fields in NodeAllocationStatsAndWeightsCalculator instead.
private volatile float indexBalanceFactor;
private volatile float shardBalanceFactor;
private volatile float writeLoadBalanceFactor;
private volatile float diskUsageBalanceFactor;
private volatile float threshold;

private final BalancerSettings balancerSettings;
private final WriteLoadForecaster writeLoadForecaster;

public BalancedShardsAllocator() {
this(Settings.EMPTY);
}

public BalancedShardsAllocator(Settings settings) {
this(createBuiltInClusterSettings(settings), WriteLoadForecaster.DEFAULT);
}

public BalancedShardsAllocator(ClusterSettings clusterSettings) {
this(clusterSettings, WriteLoadForecaster.DEFAULT);
this(new BalancerSettings(settings), WriteLoadForecaster.DEFAULT);
}

@Inject
public BalancedShardsAllocator(ClusterSettings clusterSettings, WriteLoadForecaster writeLoadForecaster) {
clusterSettings.initializeAndWatch(SHARD_BALANCE_FACTOR_SETTING, value -> this.shardBalanceFactor = value);
clusterSettings.initializeAndWatch(INDEX_BALANCE_FACTOR_SETTING, value -> this.indexBalanceFactor = value);
clusterSettings.initializeAndWatch(WRITE_LOAD_BALANCE_FACTOR_SETTING, value -> this.writeLoadBalanceFactor = value);
clusterSettings.initializeAndWatch(DISK_USAGE_BALANCE_FACTOR_SETTING, value -> this.diskUsageBalanceFactor = value);
clusterSettings.initializeAndWatch(THRESHOLD_SETTING, value -> this.threshold = value);
public BalancedShardsAllocator(BalancerSettings balancerSettings, WriteLoadForecaster writeLoadForecaster) {
this.balancerSettings = balancerSettings;
this.writeLoadForecaster = writeLoadForecaster;
}

Expand All @@ -159,12 +143,12 @@ public void allocate(RoutingAllocation allocation) {
return;
}
final WeightFunction weightFunction = new WeightFunction(
shardBalanceFactor,
indexBalanceFactor,
writeLoadBalanceFactor,
diskUsageBalanceFactor
balancerSettings.getShardBalanceFactor(),
balancerSettings.getIndexBalanceFactor(),
balancerSettings.getWriteLoadBalanceFactor(),
balancerSettings.getDiskUsageBalanceFactor()
);
final Balancer balancer = new Balancer(writeLoadForecaster, allocation, weightFunction, threshold);
final Balancer balancer = new Balancer(writeLoadForecaster, allocation, weightFunction, balancerSettings.getThreshold());
balancer.allocateUnassigned();
balancer.moveShards();
balancer.balance();
Expand Down Expand Up @@ -196,12 +180,12 @@ private void collectAndRecordNodeWeightStats(Balancer balancer, WeightFunction w
@Override
public ShardAllocationDecision decideShardAllocation(final ShardRouting shard, final RoutingAllocation allocation) {
WeightFunction weightFunction = new WeightFunction(
shardBalanceFactor,
indexBalanceFactor,
writeLoadBalanceFactor,
diskUsageBalanceFactor
balancerSettings.getShardBalanceFactor(),
balancerSettings.getIndexBalanceFactor(),
balancerSettings.getWriteLoadBalanceFactor(),
balancerSettings.getDiskUsageBalanceFactor()
);
Balancer balancer = new Balancer(writeLoadForecaster, allocation, weightFunction, threshold);
Balancer balancer = new Balancer(writeLoadForecaster, allocation, weightFunction, balancerSettings.getThreshold());
AllocateUnassignedDecision allocateUnassignedDecision = AllocateUnassignedDecision.NOT_TAKEN;
MoveDecision moveDecision = MoveDecision.NOT_TAKEN;
final ProjectIndex index = new ProjectIndex(allocation, shard);
Expand Down Expand Up @@ -244,27 +228,6 @@ private void failAllocationOfNewPrimaries(RoutingAllocation allocation) {
}
}

/**
* Returns the currently configured delta threshold
*/
public float getThreshold() {
return threshold;
}

/**
* Returns the index related weight factor.
*/
public float getIndexBalance() {
return indexBalanceFactor;
}

/**
* Returns the shard related weight factor.
*/
public float getShardBalance() {
return shardBalanceFactor;
}

/**
* A {@link Balancer}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.cluster.routing.allocation.allocator;

import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Settings;

import static org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator.DISK_USAGE_BALANCE_FACTOR_SETTING;
import static org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING;
import static org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator.SHARD_BALANCE_FACTOR_SETTING;
import static org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator.THRESHOLD_SETTING;
import static org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator.WRITE_LOAD_BALANCE_FACTOR_SETTING;

public class BalancerSettings {
public static final BalancerSettings DEFAULT = new BalancerSettings(ClusterSettings.createBuiltInClusterSettings());

private volatile float indexBalanceFactor;
private volatile float shardBalanceFactor;
private volatile float writeLoadBalanceFactor;
private volatile float diskUsageBalanceFactor;
private volatile float threshold;

public BalancerSettings(Settings settings) {
this(ClusterSettings.createBuiltInClusterSettings(settings));
}

public BalancerSettings(ClusterSettings clusterSettings) {
clusterSettings.initializeAndWatch(SHARD_BALANCE_FACTOR_SETTING, value -> this.shardBalanceFactor = value);
clusterSettings.initializeAndWatch(INDEX_BALANCE_FACTOR_SETTING, value -> this.indexBalanceFactor = value);
clusterSettings.initializeAndWatch(WRITE_LOAD_BALANCE_FACTOR_SETTING, value -> this.writeLoadBalanceFactor = value);
clusterSettings.initializeAndWatch(DISK_USAGE_BALANCE_FACTOR_SETTING, value -> this.diskUsageBalanceFactor = value);
clusterSettings.initializeAndWatch(THRESHOLD_SETTING, value -> this.threshold = value);
}

/**
* Returns the index related weight factor.
*/
public float getIndexBalanceFactor() {
return indexBalanceFactor;
}

/**
* Returns the shard related weight factor.
*/
public float getShardBalanceFactor() {
return shardBalanceFactor;
}

public float getWriteLoadBalanceFactor() {
return writeLoadBalanceFactor;
}

public float getDiskUsageBalanceFactor() {
return diskUsageBalanceFactor;
}

/**
* Returns the currently configured delta threshold
*/
public float getThreshold() {
return threshold;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.cluster.routing.RoutingTable;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.routing.ShardRoutingState;
import org.elasticsearch.cluster.routing.allocation.allocator.BalancerSettings;
import org.elasticsearch.cluster.routing.allocation.allocator.DesiredBalance;
import org.elasticsearch.cluster.routing.allocation.allocator.DesiredBalanceShardsAllocator;
import org.elasticsearch.cluster.routing.allocation.allocator.ShardAssignment;
Expand Down Expand Up @@ -84,7 +85,7 @@ public void testShardStats() {
clusterService,
() -> clusterInfo,
createShardAllocator(),
new NodeAllocationStatsAndWeightsCalculator(TEST_WRITE_LOAD_FORECASTER, ClusterSettings.createBuiltInClusterSettings())
new NodeAllocationStatsAndWeightsCalculator(TEST_WRITE_LOAD_FORECASTER, BalancerSettings.DEFAULT)
);
assertThat(
service.stats(),
Expand Down Expand Up @@ -125,7 +126,7 @@ public void testRelocatingShardIsOnlyCountedOnceOnTargetNode() {
clusterService,
EmptyClusterInfoService.INSTANCE,
createShardAllocator(),
new NodeAllocationStatsAndWeightsCalculator(TEST_WRITE_LOAD_FORECASTER, ClusterSettings.createBuiltInClusterSettings())
new NodeAllocationStatsAndWeightsCalculator(TEST_WRITE_LOAD_FORECASTER, BalancerSettings.DEFAULT)
);
assertThat(
service.stats(),
Expand Down Expand Up @@ -182,7 +183,7 @@ public DesiredBalance getDesiredBalance() {
);
}
},
new NodeAllocationStatsAndWeightsCalculator(TEST_WRITE_LOAD_FORECASTER, ClusterSettings.createBuiltInClusterSettings())
new NodeAllocationStatsAndWeightsCalculator(TEST_WRITE_LOAD_FORECASTER, BalancerSettings.DEFAULT)
);
assertThat(
service.stats(),
Expand Down
Loading