public interface LoadBalancingPolicy extends AutoCloseable
|Modifier and Type||Interface and Description|
An object that the policy uses to signal decisions it makes about node distances.
|Modifier and Type||Method and Description|
Called when the cluster that this policy is associated with closes.
Initializes this policy with the nodes discovered during driver initialization.
Returns the coordinators to use for a new query.
Called when a node is added to the cluster.
Called when a node is determined to be down.
Called when a node is removed from the cluster.
Called when a node is determined to be up.
void init(@NonNull Map<UUID,Node> nodes, @NonNull LoadBalancingPolicy.DistanceReporter distanceReporter)
This method is guaranteed to be called exactly once per instance, and before any other
method in this class. At this point, the driver has successfully connected to one of the
contact points, and performed a first refresh of topology information (by default, the contents
system.peers), to discover other nodes in the cluster.
This method must call
distanceReporter.setDistance for each provided node (otherwise that node will stay at distance
IGNORED, and the driver won't open connections to it). Note that
state can be either
UP (for the
successful contact point),
DOWN (for contact points that were tried
UNKNOWN (for contact points that weren't tried,
or any other node discovered from the topology refresh). Node states may be updated
concurrently while this method executes, but if so this policy will get notified after this
method has returned, through other methods such as
nodes- all the nodes that are known to exist in the cluster (regardless of their state) at the time of invocation.
distanceReporter- an object that will be used by the policy to signal distance changes. Implementations will typically store a this in a field, since new nodes may get
addedlater and will need to have their distance set (or the policy might change distances dynamically over time).
@NonNull Queue<Node> newQueryPlan(@Nullable Request request, @Nullable Session session)
Each new query will call this method, and try the returned nodes sequentially.
request- the request that is being routed. Note that this can be null for some internal uses.
session- the session that is executing the request. Note that this can be null for some internal uses.
ConcurrentLinkedQueueis a good choice.
void onAdd(@NonNull Node node)
The new node will be at distance
IGNORED, and have the state
If this method assigns an active distance to the node, the driver will try to create a
connection pool to it (resulting in a state change to
DOWN depending on the outcome).
If it leaves it at distance
IGNORED, the driver won't attempt
any connection. The node state will remain unknown, but might be updated later if a topology
event is received from the cluster.
void onUp(@NonNull Node node)
void onDown(@NonNull Node node)
void onRemove(@NonNull Node node)
Copyright © 2017–2023. All rights reserved.