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

java.lang.Object
  extended by com.twitter.common.net.loadbalancing.LeastConnectedStrategy<S>
All Implemented Interfaces:
LoadBalancingStrategy<S>

public class LeastConnectedStrategy<S>
extends Object

A load balancer that attempts to direct load towards a backend that has the fewest leased connections.


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.twitter.common.net.loadbalancing.LoadBalancingStrategy
LoadBalancingStrategy.ConnectionResult
 
Constructor Summary
LeastConnectedStrategy()
           
 
Method Summary
 void addConnectResult(S backendKey, LoadBalancingStrategy.ConnectionResult result, long connectTimeNanos)
          Offers information about a connection result.
 void addRequestResult(K 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<K> offeredBackends, Closure<Collection<K>> onBackendsChosen)
          Offers a set of backends that the load balancer should choose from to distribute load amongst.
protected  Collection<S> onBackendsOffered(Set<S> backends)
          Subclasses must override and return a collection of the backends actually chosen for use until the next offer round.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LeastConnectedStrategy

public LeastConnectedStrategy()
Method Detail

onBackendsOffered

protected Collection<S> onBackendsOffered(Set<S> backends)
Subclasses must override and return a collection of the backends actually chosen for use until the next offer round.

Parameters:
backends - The backends offered in a offerBackends(java.util.Set, com.twitter.common.base.Closure) event.
Returns:
The collection of backends that will be used until the next offer event.

nextBackend

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

Returns:
Next backend to send a request.
Throws:
ResourceExhaustedException - If there are no available backends.

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.

offerBackends

public final void offerBackends(Set<K> offeredBackends,
                                Closure<Collection<K>> 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<K>
Parameters:
offeredBackends - Backends to choose from.
onBackendsChosen - A callback that should be notified when the offered backends have been (re)chosen from.

addRequestResult

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

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