com.twitter.common.net.loadbalancing
Class MarkDeadStrategy<S>

java.lang.Object
  extended by com.twitter.common.net.loadbalancing.MarkDeadStrategy<S>
All Implemented Interfaces:
LoadBalancingStrategy<S>
Direct Known Subclasses:
MarkDeadStrategyWithHostCheck

public class MarkDeadStrategy<S>
extends Object
implements LoadBalancingStrategy<S>

A load balancer that serves as a layer above another load balancer to mark hosts as dead, and prevent them from being visible to the wrapped load balancer. If all backends become marked as dead, they will all be unmarked.


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.twitter.common.net.loadbalancing.LoadBalancingStrategy
LoadBalancingStrategy.ConnectionResult
 
Field Summary
protected  Predicate<S> hostChecker
           
 
Constructor Summary
MarkDeadStrategy(LoadBalancingStrategy<S> wrappedStrategy, Function<S,BackoffDecider> backoffFactory)
          Constructor that uses a default predicate host checker that always returns true.
MarkDeadStrategy(LoadBalancingStrategy<S> wrappedStrategy, Function<S,BackoffDecider> backoffFactory, Predicate<S> hostChecker)
          Creates a mark dead strategy with a wrapped strategy, backoff decider factory and a predicate host checker.
 
Method Summary
 void addConnectResult(S backendKey, LoadBalancingStrategy.ConnectionResult result, long connectTimeNanos)
          Offers information about a connection result.
 void addRequestResult(S requestKey, RequestTracker.RequestResult result, long requestTimeNanos)
          Offers information about a request result.
 void connectionReturned(S backendKey)
          Offers information about a connection that was returned.
 S nextBackend()
          Gets the next backend that a request should be sent to.
 void offerBackends(Set<S> offeredBackends, Closure<Collection<S>> onBackendsChosen)
          Offers a set of backends that the load balancer should choose from to distribute load amongst.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

hostChecker

protected final Predicate<S> hostChecker
Constructor Detail

MarkDeadStrategy

public MarkDeadStrategy(LoadBalancingStrategy<S> wrappedStrategy,
                        Function<S,BackoffDecider> backoffFactory,
                        Predicate<S> hostChecker)
Creates a mark dead strategy with a wrapped strategy, backoff decider factory and a predicate host checker. Use this constructor if you want to pass in the your own implementation of the host checker.

Parameters:
wrappedStrategy - one of the implementations of the load balancing strategy.
backoffFactory - backoff decider factory per host.
hostChecker - predicate that returns true if the host is alive, otherwise returns false.

MarkDeadStrategy

public MarkDeadStrategy(LoadBalancingStrategy<S> wrappedStrategy,
                        Function<S,BackoffDecider> backoffFactory)
Constructor that uses a default predicate host checker that always returns true. This is the default constructor that all consumers of MarkDeadStrategy currently use.

Parameters:
wrappedStrategy - one of the implementations of the load balancing strategy.
backoffFactory - backoff decider factory per host.
Method Detail

offerBackends

public void offerBackends(Set<S> offeredBackends,
                          Closure<Collection<S>> onBackendsChosen)
Description copied from interface: LoadBalancingStrategy
Offers a set of backends that the load balancer should choose from to distribute load amongst.

Specified by:
offerBackends in interface LoadBalancingStrategy<S>
Parameters:
offeredBackends - Backends to choose from.
onBackendsChosen - A callback that should be notified when the offered backends have been (re)chosen from.

addConnectResult

public void addConnectResult(S backendKey,
                             LoadBalancingStrategy.ConnectionResult result,
                             long connectTimeNanos)
Description copied from interface: LoadBalancingStrategy
Offers information about a connection result.

Specified by:
addConnectResult in interface LoadBalancingStrategy<S>
Parameters:
backendKey - Backend key.
result - Connection result.
connectTimeNanos - Time spent waiting for connection to be established.

connectionReturned

public void connectionReturned(S backendKey)
Description copied from interface: LoadBalancingStrategy
Offers information about a connection that was returned.

Specified by:
connectionReturned in interface LoadBalancingStrategy<S>
Parameters:
backendKey - Backend key.

addRequestResult

public void addRequestResult(S requestKey,
                             RequestTracker.RequestResult result,
                             long requestTimeNanos)
Description copied from interface: LoadBalancingStrategy
Offers information about a request result.

Specified by:
addRequestResult in interface LoadBalancingStrategy<S>
Parameters:
requestKey - Backend key.
result - Request result.
requestTimeNanos - Time spent waiting for a connection to be established.

nextBackend

public S nextBackend()
              throws ResourceExhaustedException
Description copied from interface: LoadBalancingStrategy
Gets the next backend that a request should be sent to.

Specified by:
nextBackend in interface LoadBalancingStrategy<S>
Returns:
Next backend to send a request.
Throws:
ResourceExhaustedException - If there are no available backends.