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

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

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

A load balancer that maintains a fixed upper bound on the number of backends that will be made available for a wrapped load balancer. TODO(William Farner): May want to consider periodically swapping subsets. TODO(William Farner): May want to catch ResourceExhaustedExceptions from wrapped strategy and adjust subset if possible.


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.twitter.common.net.loadbalancing.LoadBalancingStrategy
LoadBalancingStrategy.ConnectionResult
 
Constructor Summary
SubsetStrategy(int maxBackends, LoadBalancingStrategy<S> wrapped)
           
 
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
 

Constructor Detail

SubsetStrategy

public SubsetStrategy(int maxBackends,
                      LoadBalancingStrategy<S> wrapped)
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.