com.twitter.common.net.loadbalancing
Class LoadBalancerImpl<K>

java.lang.Object
  extended by com.twitter.common.net.loadbalancing.LoadBalancerImpl<K>
All Implemented Interfaces:
LoadBalancer<K>, RequestTracker<K>

public class LoadBalancerImpl<K>
extends Object
implements LoadBalancer<K>

Implementation of a load balancer, that uses a pluggable LoadBalancingStrategy to define actual load balancing behavior. This class handles the responsibility of associating connections with backends. Calls to connected(Object, long), RequestTracker.requestResult(Object, RequestResult, long), and released(Object) will not be forwarded for unknown backends/connections.


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.twitter.common.net.loadbalancing.RequestTracker
RequestTracker.RequestResult
 
Constructor Summary
LoadBalancerImpl(LoadBalancingStrategy<K> strategy)
          Creates a new load balancer that will use the given strategy.
 
Method Summary
 void connected(K backend, long connectTimeNanos)
          Signals the load balancer that a connection was made.
 void connectFailed(K backend, LoadBalancingStrategy.ConnectionResult result)
          Signals the load balancer that a connection was attempted, but failed.
static
<K> LoadBalancerImpl<K>
create(LoadBalancingStrategy<K> strategy)
          Convenience method to create a new load balancer.
 K 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.
 void released(K backend)
          Signals the load balancer that a connection was released, and is idle.
 void requestResult(K backend, RequestTracker.RequestResult result, long requestTimeNanos)
          Informs the tracker of a completed request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LoadBalancerImpl

public LoadBalancerImpl(LoadBalancingStrategy<K> strategy)
Creates a new load balancer that will use the given strategy.

Parameters:
strategy - Strategy to delegate load balancing work to.
Method Detail

offerBackends

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

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

nextBackend

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

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

connected

public void connected(K backend,
                      long connectTimeNanos)
Description copied from interface: LoadBalancer
Signals the load balancer that a connection was made.

Specified by:
connected in interface LoadBalancer<K>
Parameters:
backend - The backend that was connected to.
connectTimeNanos - The time spent waiting for the connection to be established.

connectFailed

public void connectFailed(K backend,
                          LoadBalancingStrategy.ConnectionResult result)
Description copied from interface: LoadBalancer
Signals the load balancer that a connection was attempted, but failed.

Specified by:
connectFailed in interface LoadBalancer<K>
Parameters:
backend - The backend to which connection attempt was made.
result - The result of the connection attempt (only FAILED and TIMEOUT are permitted).

released

public void released(K backend)
Description copied from interface: LoadBalancer
Signals the load balancer that a connection was released, and is idle.

Specified by:
released in interface LoadBalancer<K>
Parameters:
backend - Idle connection.

requestResult

public void requestResult(K backend,
                          RequestTracker.RequestResult result,
                          long requestTimeNanos)
Description copied from interface: RequestTracker
Informs the tracker of a completed request.

Specified by:
requestResult in interface RequestTracker<K>
Parameters:
backend - Key to identify the owner of the request.
result - Result of the request.
requestTimeNanos - Time duration spent waiting for the request to complete.

create

public static <K> LoadBalancerImpl<K> create(LoadBalancingStrategy<K> strategy)
Convenience method to create a new load balancer.

Type Parameters:
K - Backend type.
Parameters:
strategy - Strategy to use.
Returns:
A new load balancer.