Class AllocationService
AllocationService
keeps AllocationDeciders
to choose nodes
for shard allocation. This class also manages new nodes joining the cluster
and rerouting of shards.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final record
this class is used to describe results of applying a set ofAllocationCommand
static interface
-
Constructor Summary
ConstructorsConstructorDescriptionAllocationService
(AllocationDeciders allocationDeciders, ShardsAllocator shardsAllocator, ClusterInfoService clusterInfoService, SnapshotsInfoService snapshotsInfoService, ShardRoutingRoleStrategy shardRoutingRoleStrategy) AllocationService
(AllocationDeciders allocationDeciders, GatewayAllocator gatewayAllocator, ShardsAllocator shardsAllocator, ClusterInfoService clusterInfoService, SnapshotsInfoService snapshotsInfoService, ShardRoutingRoleStrategy shardRoutingRoleStrategy) -
Method Summary
Modifier and TypeMethodDescriptionadaptAutoExpandReplicas
(ClusterState clusterState) Checks if there are replicas with the auto-expand feature that need to be adapted.void
addAllocFailuresResetListenerTo
(ClusterService clusterService) Creates a cluster state listener that resets allocation failures.applyFailedShards
(ClusterState clusterState, List<FailedShard> failedShards, List<StaleShard> staleShards) Applies the failed shards.applyStartedShards
(ClusterState clusterState, List<ShardRouting> startedShards) Applies the started shards.void
protected long
override this to control time based decisions during allocationdisassociateDeadNodes
(ClusterState clusterState, boolean reroute, String reason) Unassign any shards that are associated with nodes that are no longer part of the cluster, potentially promoting replicas if needed.executeWithRoutingAllocation
(ClusterState clusterState, String reason, AllocationService.RerouteStrategy rerouteStrategy) Computes the next step towards a fully allocated and balanced cluster and records this step in the routing table of the returned state.explainShardAllocation
(ShardRouting shardRouting, RoutingAllocation allocation) static <T> String
firstListElementsToCommaDelimitedString
(List<T> elements, Function<T, String> formatter, boolean isDebugEnabled) Internal helper to cap the number of elements in a potentially long list for logging.static ClusterHealthStatus
getHealthStatus
(ClusterState clusterState) int
reroute
(ClusterState clusterState, String reason, ActionListener<Void> listener) Computes the next step towards a fully allocated and balanced cluster and records this step in the routing table of the returned state.reroute
(ClusterState clusterState, AllocationCommands commands, boolean explain, boolean retryFailed, boolean dryRun, ActionListener<Void> reroute) void
setExistingShardsAllocators
(Map<String, ExistingShardsAllocator> existingShardsAllocators) Inject theExistingShardsAllocator
s to use.
-
Constructor Details
-
AllocationService
public AllocationService(AllocationDeciders allocationDeciders, GatewayAllocator gatewayAllocator, ShardsAllocator shardsAllocator, ClusterInfoService clusterInfoService, SnapshotsInfoService snapshotsInfoService, ShardRoutingRoleStrategy shardRoutingRoleStrategy) -
AllocationService
public AllocationService(AllocationDeciders allocationDeciders, ShardsAllocator shardsAllocator, ClusterInfoService clusterInfoService, SnapshotsInfoService snapshotsInfoService, ShardRoutingRoleStrategy shardRoutingRoleStrategy)
-
-
Method Details
-
setExistingShardsAllocators
public void setExistingShardsAllocators(Map<String, ExistingShardsAllocator> existingShardsAllocators) Inject theExistingShardsAllocator
s to use. May only be called once. -
getAllocationDeciders
- Returns:
- The allocation deciders that the allocation service has been configured with.
-
getShardRoutingRoleStrategy
-
applyStartedShards
Applies the started shards. Note, only initializing ShardRouting instances that exist in the routing table should be provided as parameter and no duplicates should be contained.If the same instance of the
ClusterState
is returned, then no change has been made. -
applyFailedShards
public ClusterState applyFailedShards(ClusterState clusterState, List<FailedShard> failedShards, List<StaleShard> staleShards) Applies the failed shards. Note, only assigned ShardRouting instances that exist in the routing table should be provided as parameter. Also applies a list of allocation ids to remove from the in-sync set for shard copies for which there are no routing entries in the routing table.If the same instance of ClusterState is returned, then no change has been made.
-
disassociateDeadNodes
public ClusterState disassociateDeadNodes(ClusterState clusterState, boolean reroute, String reason) Unassign any shards that are associated with nodes that are no longer part of the cluster, potentially promoting replicas if needed. -
adaptAutoExpandReplicas
Checks if there are replicas with the auto-expand feature that need to be adapted. Returns an updated cluster state if changes were necessary, or the identical cluster if no changes were required. -
firstListElementsToCommaDelimitedString
public static <T> String firstListElementsToCommaDelimitedString(List<T> elements, Function<T, String> formatter, boolean isDebugEnabled) Internal helper to cap the number of elements in a potentially long list for logging.- Type Parameters:
T
- The list element type.- Parameters:
elements
- The elements to log. May be any non-null list. Must not be null.formatter
- A function that can convert list elements to a String. Must not be null.- Returns:
- A comma-separated string of the first few elements.
-
reroute
public AllocationService.CommandsResult reroute(ClusterState clusterState, AllocationCommands commands, boolean explain, boolean retryFailed, boolean dryRun, ActionListener<Void> reroute) -
reroute
public ClusterState reroute(ClusterState clusterState, String reason, ActionListener<Void> listener) Computes the next step towards a fully allocated and balanced cluster and records this step in the routing table of the returned state. Should be called after every change to the cluster that affects the routing table and/or the balance of shards.This method is expensive in larger clusters. Wherever possible you should invoke this method asynchronously using
RerouteService.reroute(java.lang.String, org.elasticsearch.common.Priority, org.elasticsearch.action.ActionListener<java.lang.Void>)
to batch up invocations rather than calling the method directly.- Returns:
- an updated cluster state, or the same instance that was passed as an argument if no changes were made.
-
executeWithRoutingAllocation
public ClusterState executeWithRoutingAllocation(ClusterState clusterState, String reason, AllocationService.RerouteStrategy rerouteStrategy) Computes the next step towards a fully allocated and balanced cluster and records this step in the routing table of the returned state. Should be called after every change to the cluster that affects the routing table and/or the balance of shards.This method is expensive in larger clusters. Wherever possible you should invoke this method asynchronously using
RerouteService.reroute(java.lang.String, org.elasticsearch.common.Priority, org.elasticsearch.action.ActionListener<java.lang.Void>)
to batch up invocations rather than calling the method directly.- Returns:
- an updated cluster state, or the same instance that was passed as an argument if no changes were made.
-
getHealthStatus
-
addAllocFailuresResetListenerTo
Creates a cluster state listener that resets allocation failures. For example, reset when a new node joins a cluster. Resetting counter on new node join covers a variety of use cases, such as rolling update, version change, node restarts. -
currentNanoTime
protected long currentNanoTime()override this to control time based decisions during allocation -
cleanCaches
public void cleanCaches() -
getNumberOfInFlightFetches
public int getNumberOfInFlightFetches() -
explainShardAllocation
public ShardAllocationDecision explainShardAllocation(ShardRouting shardRouting, RoutingAllocation allocation)
-