Skip to content

Commit 0679990

Browse files
committed
Allow balancer weights factory to be provided by ClusterPlugin
1 parent f186c56 commit 0679990

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

server/src/main/java/org/elasticsearch/cluster/ClusterModule.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.elasticsearch.cluster.metadata.MetadataMappingService;
2727
import org.elasticsearch.cluster.metadata.NodesShutdownMetadata;
2828
import org.elasticsearch.cluster.metadata.RepositoriesMetadata;
29-
import org.elasticsearch.cluster.node.DiscoveryNode;
3029
import org.elasticsearch.cluster.project.ProjectResolver;
3130
import org.elasticsearch.cluster.routing.DelayedAllocationService;
3231
import org.elasticsearch.cluster.routing.ShardRouting;
@@ -44,7 +43,6 @@
4443
import org.elasticsearch.cluster.routing.allocation.allocator.DesiredBalanceShardsAllocator.DesiredBalanceReconcilerAction;
4544
import org.elasticsearch.cluster.routing.allocation.allocator.GlobalBalancingWeightsFactory;
4645
import org.elasticsearch.cluster.routing.allocation.allocator.ShardsAllocator;
47-
import org.elasticsearch.cluster.routing.allocation.allocator.StatelessBalancingWeightsFactory;
4846
import org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider;
4947
import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders;
5048
import org.elasticsearch.cluster.routing.allocation.decider.AwarenessAllocationDecider;
@@ -150,11 +148,11 @@ public ClusterModule(
150148
this.deciderList = createAllocationDeciders(settings, clusterService.getClusterSettings(), clusterPlugins);
151149
this.allocationDeciders = new AllocationDeciders(deciderList);
152150
final BalancerSettings balancerSettings = new BalancerSettings(clusterService.getClusterSettings());
153-
// I'm aware that the following is an anti-pattern and will implement as an SPI provider or plugin
154-
// if we decide to go ahead with this.
155-
final BalancingWeightsFactory balancingWeightsFactory = DiscoveryNode.isStateless(settings)
156-
? new StatelessBalancingWeightsFactory(balancerSettings, clusterService.getClusterSettings())
157-
: new GlobalBalancingWeightsFactory(balancerSettings);
151+
final BalancingWeightsFactory balancingWeightsFactory = getBalancingWeightsFactory(
152+
clusterPlugins,
153+
balancerSettings,
154+
clusterService.getClusterSettings()
155+
);
158156
var nodeAllocationStatsAndWeightsCalculator = new NodeAllocationStatsAndWeightsCalculator(
159157
writeLoadForecaster,
160158
balancingWeightsFactory
@@ -216,6 +214,22 @@ public ShardRouting.Role newEmptyRole(int copyIndex) {
216214
};
217215
}
218216

217+
static BalancingWeightsFactory getBalancingWeightsFactory(
218+
List<ClusterPlugin> clusterPlugins,
219+
BalancerSettings balancerSettings,
220+
ClusterSettings clusterSettings
221+
) {
222+
final var strategies = clusterPlugins.stream()
223+
.map(pl -> pl.getBalancingWeightsFactory(balancerSettings, clusterSettings))
224+
.filter(Objects::nonNull)
225+
.toList();
226+
return switch (strategies.size()) {
227+
case 0 -> new GlobalBalancingWeightsFactory(balancerSettings);
228+
case 1 -> strategies.getFirst();
229+
default -> throw new IllegalArgumentException("multiple plugins define balancing weights factories, which is not permitted");
230+
};
231+
}
232+
219233
private ClusterState reconcile(ClusterState clusterState, RerouteStrategy rerouteStrategy) {
220234
return allocationService.executeWithRoutingAllocation(clusterState, "reconcile-desired-balance", rerouteStrategy);
221235
}

server/src/main/java/org/elasticsearch/plugins/ClusterPlugin.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.elasticsearch.cluster.routing.ShardRoutingRoleStrategy;
1313
import org.elasticsearch.cluster.routing.allocation.ExistingShardsAllocator;
1414
import org.elasticsearch.cluster.routing.allocation.WriteLoadForecaster;
15+
import org.elasticsearch.cluster.routing.allocation.allocator.BalancerSettings;
16+
import org.elasticsearch.cluster.routing.allocation.allocator.BalancingWeightsFactory;
1517
import org.elasticsearch.cluster.routing.allocation.allocator.ShardsAllocator;
1618
import org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider;
1719
import org.elasticsearch.common.settings.ClusterSettings;
@@ -75,6 +77,10 @@ default ShardRoutingRoleStrategy getShardRoutingRoleStrategy() {
7577
return null;
7678
}
7779

80+
default BalancingWeightsFactory getBalancingWeightsFactory(BalancerSettings balancerSettings, ClusterSettings clusterSettings) {
81+
return null;
82+
}
83+
7884
/**
7985
* Called when the node is started
8086
*/

0 commit comments

Comments
 (0)