public class TokenAwarePolicy extends Object implements ChainableLoadBalancingPolicy, CloseableLoadBalancingPolicy
distance
method is inherited from the child policy.newQueryPlan
method will first
return the LOCAL
replicas for the query (based on Statement.getRoutingKey()
)
if possible (i.e. if the query getRoutingKey
method
doesn't return null
and if Metadata.getReplicas(java.lang.String, java.nio.ByteBuffer)
returns a non empty set of replicas for that partition key). If no
local replica can be either found or successfully contacted, the rest
of the query plan will fallback to one of the child policy.distance
method returns HostDistance.LOCAL
will be considered having
priority. For example, if you wrap DCAwareRoundRobinPolicy
with this
token aware policy, replicas from remote data centers may only be
returned after all the host of the local data center.Constructor and Description |
---|
TokenAwarePolicy(LoadBalancingPolicy childPolicy)
Creates a new
TokenAware policy with shuffling of replicas. |
TokenAwarePolicy(LoadBalancingPolicy childPolicy,
boolean shuffleReplicas)
Creates a new
TokenAware policy. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Gets invoked at cluster shutdown.
|
HostDistance |
distance(Host host)
Return the HostDistance for the provided host.
|
LoadBalancingPolicy |
getChildPolicy()
Returns the child policy.
|
void |
init(Cluster cluster,
Collection<Host> hosts)
Initialize this load balancing policy.
|
Iterator<Host> |
newQueryPlan(String loggedKeyspace,
Statement statement)
Returns the hosts to use for a new query.
|
void |
onAdd(Host host)
Called when a new node is added to the cluster.
|
void |
onDown(Host host)
Called when a node is determined to be down.
|
void |
onRemove(Host host)
Called when a node is removed from the cluster.
|
void |
onSuspected(Host host) |
void |
onUp(Host host)
Called when a node is determined to be up.
|
public TokenAwarePolicy(LoadBalancingPolicy childPolicy, boolean shuffleReplicas)
TokenAware
policy.childPolicy
- the load balancing policy to wrap with token awareness.shuffleReplicas
- whether to shuffle the replicas returned by getRoutingKey
.
Note that setting this parameter to true
might decrease the
effectiveness of caching (especially at consistency level ONE), since
the same row will be retrieved from any replica (instead of only the
"primary" replica without shuffling).
On the other hand, shuffling will better distribute writes, and can
alleviate hotspots caused by "fat" partitions.public TokenAwarePolicy(LoadBalancingPolicy childPolicy)
TokenAware
policy with shuffling of replicas.childPolicy
- the load balancing policy to wrap with token
awareness.TokenAwarePolicy(LoadBalancingPolicy, boolean)
public LoadBalancingPolicy getChildPolicy()
ChainableLoadBalancingPolicy
getChildPolicy
in interface ChainableLoadBalancingPolicy
public void init(Cluster cluster, Collection<Host> hosts)
LoadBalancingPolicy
init
in interface LoadBalancingPolicy
cluster
- the Cluster
instance for which the policy is created.hosts
- the initial hosts to use.public HostDistance distance(Host host)
distance
in interface LoadBalancingPolicy
host
- the host of which to return the distance of.host
as returned by the wrapped policy.public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement)
HostDistance
for the child policy is LOCAL
) for the query if it can determine
them (i.e. mainly if statement.getRoutingKey()
is not null
).
Following what it will return the plan of the child policy.newQueryPlan
in interface LoadBalancingPolicy
statement
- the query for which to build the plan.loggedKeyspace
- the currently logged keyspace (the one set through either
Cluster.connect(String)
or by manually doing a USE
query) for
the session on which this plan need to be built. This can be null
if
the corresponding session has no keyspace logged in.public void onUp(Host host)
Host.StateListener
onUp
in interface Host.StateListener
host
- the host that has been detected up.public void onSuspected(Host host)
onSuspected
in interface Host.StateListener
public void onDown(Host host)
Host.StateListener
onDown
in interface Host.StateListener
host
- the host that has been detected down.public void onAdd(Host host)
Host.StateListener
onAdd
in interface Host.StateListener
host
- the host that has been newly added.public void onRemove(Host host)
Host.StateListener
onRemove
in interface Host.StateListener
host
- the removed host.public void close()
CloseableLoadBalancingPolicy
close
in interface CloseableLoadBalancingPolicy