Class DefaultLoadBalancer<T>
- java.lang.Object
-
- io.hekate.messaging.loadbalance.DefaultLoadBalancer<T>
-
- Type Parameters:
T
- Base type of messages that can be handled by this load balancer.
- All Implemented Interfaces:
LoadBalancer<T>
- Direct Known Subclasses:
DefaultRpcLoadBalancer
public class DefaultLoadBalancer<T> extends Object implements LoadBalancer<T>
Default implementation of theLoadBalancer
interface.This load balancer uses
nonAffinityRoute(Object, LoadBalancerContext)
andaffinityRoute(Object, LoadBalancerContext)
methods to perform actual load balancing. Which of those methods will be used depends on whether the load balanced operation has anaffinity key
or not. Please see the documentation of those methods for more details about their logic.
-
-
Constructor Summary
Constructors Constructor Description DefaultLoadBalancer()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected ClusterNode
affinityRoute(T msg, LoadBalancerContext ctx)
Selects a node based on thepartition
mapping of the operation'saffinity key
.protected ClusterNode
nonAffinityRoute(T msg, LoadBalancerContext ctx)
Selects arandom
node from the load balancer context.ClusterNodeId
route(T msg, LoadBalancerContext ctx)
Selects one of the cluster nodes from the load balancer context.String
toString()
-
-
-
Method Detail
-
route
public ClusterNodeId route(T msg, LoadBalancerContext ctx) throws LoadBalancerException
Description copied from interface:LoadBalancer
Selects one of the cluster nodes from the load balancer context. Note that the provided context contains only those nodes that are capable of receiving messages of this channel (i.e. haveMessageReceiver
) and match the channel'stopology filtering
rules.Returning
null
from this method (if target node can't be selected for some reason) will cause message sending operation to fail withLoadBalancerException
.- Specified by:
route
in interfaceLoadBalancer<T>
- Parameters:
msg
- Message.ctx
- Load balancer context.- Returns:
- Node that should be used for the messaging operation.
- Throws:
LoadBalancerException
- if failed to perform load balancing.
-
nonAffinityRoute
protected ClusterNode nonAffinityRoute(T msg, LoadBalancerContext ctx) throws LoadBalancerException
Selects arandom
node from the load balancer context. If the selected node is known to befailed
then another random non-failed node will be selected. If all nodes are known to be failed then this method will fallback to the initially selected node.- Parameters:
msg
- Message.ctx
- Load balancer context.- Returns:
- Selected node.
- Throws:
LoadBalancerException
- if failed to perform load balancing.
-
affinityRoute
protected ClusterNode affinityRoute(T msg, LoadBalancerContext ctx) throws LoadBalancerException
Selects a node based on thepartition
mapping of the operation'saffinity key
.This method tries to use the
primary node
of the selectedPartition
. If the selected node is known to befailed
then abackup node
will be selected. If partition doesn't have any backup nodes or if it is known that all backup nodes failed then this method will fallback to the initially selected node.- Parameters:
msg
- Message.ctx
- Load balancer context.- Returns:
- Selected node.
- Throws:
LoadBalancerException
- if failed to perform load balancing.
-
-