Trait LoadBalancingPolicy

Source
pub trait LoadBalancingPolicy:
    Send
    + Sync
    + Debug {
    // Required methods
    fn pick<'a>(
        &'a self,
        request: &'a RoutingInfo<'_>,
        cluster: &'a ClusterState,
    ) -> Option<(NodeRef<'a>, Option<Shard>)>;
    fn fallback<'a>(
        &'a self,
        request: &'a RoutingInfo<'_>,
        cluster: &'a ClusterState,
    ) -> FallbackPlan<'a>;
    fn name(&self) -> String;

    // Provided methods
    fn on_request_success(
        &self,
        _request: &RoutingInfo<'_>,
        _latency: Duration,
        _node: NodeRef<'_>,
    ) { ... }
    fn on_request_failure(
        &self,
        _request: &RoutingInfo<'_>,
        _latency: Duration,
        _node: NodeRef<'_>,
        _error: &RequestAttemptError,
    ) { ... }
}
Expand description

Policy that decides which nodes and shards to contact for each request.

When a request is prepared to be sent to ScyllaDB/Cassandra, a LoadBalancingPolicy implementation constructs a load balancing plan. That plan is a list of targets (target is a node + an optional shard) to which the driver will try to send the request. The first elements of the plan are the targets which are the best to contact (e.g. they might have the lowest latency).

Most requests are sent on the first try, so the request execution layer rarely needs to know more than one target from plan. To better optimize that case, LoadBalancingPolicy has two methods: pick and fallback. pick returns the first target to contact for a given request, fallback returns the rest of the load balancing plan.

fallback is called not only if a send to picked node failed (or when executing speculatively), but also if pick returns None.

Usually the driver needs only the first node from load balancing plan (most requests are send successfully, and there is no need to retry).

This trait is used to produce an iterator of nodes to contact for a given request.

Required Methods§

Source

fn pick<'a>( &'a self, request: &'a RoutingInfo<'_>, cluster: &'a ClusterState, ) -> Option<(NodeRef<'a>, Option<Shard>)>

Returns the first node to contact for a given request.

Source

fn fallback<'a>( &'a self, request: &'a RoutingInfo<'_>, cluster: &'a ClusterState, ) -> FallbackPlan<'a>

Returns all contact-appropriate nodes for a given request.

Source

fn name(&self) -> String

Returns the name of load balancing policy.

Provided Methods§

Source

fn on_request_success( &self, _request: &RoutingInfo<'_>, _latency: Duration, _node: NodeRef<'_>, )

Invoked each time a request succeeds.

Source

fn on_request_failure( &self, _request: &RoutingInfo<'_>, _latency: Duration, _node: NodeRef<'_>, _error: &RequestAttemptError, )

Invoked each time a request fails.

Implementors§