com.netflix.loadbalancer
Class BaseLoadBalancer

java.lang.Object
  extended by com.netflix.loadbalancer.AbstractLoadBalancer
      extended by com.netflix.loadbalancer.BaseLoadBalancer
All Implemented Interfaces:
IClientConfigAware, PrimeConnections.PrimeConnectionListener, ILoadBalancer
Direct Known Subclasses:
DynamicServerListLoadBalancer

public class BaseLoadBalancer
extends AbstractLoadBalancer
implements PrimeConnections.PrimeConnectionListener, IClientConfigAware

A basic implementation of the load balancer where an arbitrary list of servers can be set as the server pool. A ping can be set to determine the liveness of a server. Internally, this class maintains an "all" server list and an "up" server list and use them depending on what the caller asks for.


Nested Class Summary
 
Nested classes/interfaces inherited from class com.netflix.loadbalancer.AbstractLoadBalancer
AbstractLoadBalancer.ServerGroup
 
Field Summary
protected  java.util.List<Server> allServerList
           
protected  java.util.concurrent.locks.ReadWriteLock allServerLock
           
protected  LoadBalancerStats lbStats
           
protected  java.util.Timer lbTimer
           
protected  int maxTotalPingTimeSeconds
           
protected  java.lang.String name
           
protected  IPing ping
           
protected  java.util.concurrent.atomic.AtomicBoolean pingInProgress
           
protected  int pingIntervalSeconds
           
protected  IRule rule
           
protected  java.util.Comparator<Server> serverComparator
           
protected  java.util.List<Server> upServerList
           
protected  java.util.concurrent.locks.ReadWriteLock upServerLock
           
 
Constructor Summary
BaseLoadBalancer()
          Default constructor which sets name as "default", sets null ping, and RoundRobinRule as the rule.
BaseLoadBalancer(IClientConfig config)
           
BaseLoadBalancer(IPing ping, IRule rule)
           
BaseLoadBalancer(java.lang.String lbName, IRule rule, LoadBalancerStats lbStats)
           
BaseLoadBalancer(java.lang.String name, IRule rule, LoadBalancerStats stats, IPing ping)
           
 
Method Summary
 void addServer(Server newServer)
          Add a server to the 'allServer' list; does not verify uniqueness, so you could give a server a greater share by adding it more than once.
 void addServers(java.util.List<Server> newServers)
          Add a list of servers to the 'allServer' list; does not verify uniqueness, so you could give a server a greater share by adding it more than once
 void cancelPingTask()
           
 java.lang.String choose(java.lang.Object key)
           
 Server chooseServer(java.lang.Object key)
          Choose a server from load balancer.
 void forceQuickPing()
           
 LoadBalancerStats getLoadBalancerStats()
          Obtain LoadBalancer related Statistics
 int getMaxTotalPingTime()
           
 java.lang.String getName()
           
 IPing getPing()
           
 int getPingInterval()
           
 PrimeConnections getPrimeConnections()
           
 IRule getRule()
           
 Server getServerByIndex(int index, boolean availableOnly)
          return the server
 int getServerCount(boolean onlyAvailable)
          get the count of servers.
 java.util.List<Server> getServerList(AbstractLoadBalancer.ServerGroup serverGroup)
          List of servers that this Loadbalancer knows about
 java.util.List<Server> getServerList(boolean availableOnly)
           
protected  void init()
          Register with monitors and start priming connections if it is set.
 void initWithNiwsConfig(IClientConfig clientConfig)
          Concrete implementation should implement this method so that the configuration set via IClientConfig (which in turn were set via Archaius properties) will be taken into consideration
 boolean isEnablePrimingConnections()
           
 boolean isPingInProgress()
           
 java.util.concurrent.locks.Lock lockAllServerList(boolean write)
           
 java.util.concurrent.locks.Lock lockUpServerList(boolean write)
           
 void markServerDown(Server server)
          To be called by the clients of the load balancer to notify that a Server is down else, the LB will think its still Alive until the next Ping cycle - potentially (assuming that the LB Impl does a ping)
 void markServerDown(java.lang.String id)
           
 void primeCompleted(Server s, java.lang.Throwable lastException)
           
 void setEnablePrimingConnections(boolean enablePrimingConnections)
           
 void setLoadBalancerStats(LoadBalancerStats lbStats)
           
 void setMaxTotalPingTime(int maxTotalPingTimeSeconds)
           
 void setPing(IPing ping)
           
 void setPingInterval(int pingIntervalSeconds)
           
 void setPrimeConnections(PrimeConnections primeConnections)
           
 void setRule(IRule rule)
           
 void setServersList(java.util.List lsrv)
          Set the list of servers used as the server pool.
 java.lang.String toString()
           
 
Methods inherited from class com.netflix.loadbalancer.AbstractLoadBalancer
chooseServer
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

rule

protected IRule rule

ping

protected IPing ping

allServerList

@Monitor(name="LoadBalancer_AllServerList",
         type=INFORMATIONAL)
protected volatile java.util.List<Server> allServerList

upServerList

@Monitor(name="LoadBalancer_UpServerList",
         type=INFORMATIONAL)
protected volatile java.util.List<Server> upServerList

allServerLock

protected java.util.concurrent.locks.ReadWriteLock allServerLock

upServerLock

protected java.util.concurrent.locks.ReadWriteLock upServerLock

name

protected java.lang.String name

lbTimer

protected java.util.Timer lbTimer

pingIntervalSeconds

protected int pingIntervalSeconds

maxTotalPingTimeSeconds

protected int maxTotalPingTimeSeconds

serverComparator

protected java.util.Comparator<Server> serverComparator

pingInProgress

protected java.util.concurrent.atomic.AtomicBoolean pingInProgress

lbStats

protected LoadBalancerStats lbStats
Constructor Detail

BaseLoadBalancer

public BaseLoadBalancer()
Default constructor which sets name as "default", sets null ping, and RoundRobinRule as the rule.

This constructor is mainly used by ClientFactory. Calling this constructor must be followed by calling init() or #initWithNiwsConfig(NiwsClientConfig) to complete initialization. This constructor is provided for reflection. When constructing programatically, it is recommended to use other constructors.


BaseLoadBalancer

public BaseLoadBalancer(java.lang.String lbName,
                        IRule rule,
                        LoadBalancerStats lbStats)

BaseLoadBalancer

public BaseLoadBalancer(IPing ping,
                        IRule rule)

BaseLoadBalancer

public BaseLoadBalancer(java.lang.String name,
                        IRule rule,
                        LoadBalancerStats stats,
                        IPing ping)

BaseLoadBalancer

public BaseLoadBalancer(IClientConfig config)
Method Detail

initWithNiwsConfig

public void initWithNiwsConfig(IClientConfig clientConfig)
Description copied from interface: IClientConfigAware
Concrete implementation should implement this method so that the configuration set via IClientConfig (which in turn were set via Archaius properties) will be taken into consideration

Specified by:
initWithNiwsConfig in interface IClientConfigAware

getName

public java.lang.String getName()

getLoadBalancerStats

public LoadBalancerStats getLoadBalancerStats()
Description copied from class: AbstractLoadBalancer
Obtain LoadBalancer related Statistics

Specified by:
getLoadBalancerStats in class AbstractLoadBalancer
Returns:

setLoadBalancerStats

public void setLoadBalancerStats(LoadBalancerStats lbStats)

lockAllServerList

public java.util.concurrent.locks.Lock lockAllServerList(boolean write)

lockUpServerList

public java.util.concurrent.locks.Lock lockUpServerList(boolean write)

setPingInterval

public void setPingInterval(int pingIntervalSeconds)

getPingInterval

public int getPingInterval()

setMaxTotalPingTime

public void setMaxTotalPingTime(int maxTotalPingTimeSeconds)

getMaxTotalPingTime

public int getMaxTotalPingTime()

getPing

public IPing getPing()

getRule

public IRule getRule()

isPingInProgress

public boolean isPingInProgress()

setPing

public void setPing(IPing ping)

setRule

public void setRule(IRule rule)

getServerCount

public int getServerCount(boolean onlyAvailable)
get the count of servers.

Parameters:
onlyAvailable - if true, return only up servers.
Returns:

addServer

public void addServer(Server newServer)
Add a server to the 'allServer' list; does not verify uniqueness, so you could give a server a greater share by adding it more than once.


addServers

public void addServers(java.util.List<Server> newServers)
Add a list of servers to the 'allServer' list; does not verify uniqueness, so you could give a server a greater share by adding it more than once

Specified by:
addServers in interface ILoadBalancer
Parameters:
newServers - new servers to add

setServersList

public void setServersList(java.util.List lsrv)
Set the list of servers used as the server pool. This overrides existing server list.


getServerByIndex

public Server getServerByIndex(int index,
                               boolean availableOnly)
return the server

Parameters:
index -
availableOnly -
Returns:

getServerList

public java.util.List<Server> getServerList(boolean availableOnly)

getServerList

public java.util.List<Server> getServerList(AbstractLoadBalancer.ServerGroup serverGroup)
Description copied from class: AbstractLoadBalancer
List of servers that this Loadbalancer knows about

Specified by:
getServerList in class AbstractLoadBalancer
Returns:

cancelPingTask

public void cancelPingTask()

chooseServer

public Server chooseServer(java.lang.Object key)
Description copied from interface: ILoadBalancer
Choose a server from load balancer.

Specified by:
chooseServer in interface ILoadBalancer
Parameters:
key - An object that the load balancer may use to determine which server to return. null if the load balancer does not use this parameter.
Returns:
server chosen

choose

public java.lang.String choose(java.lang.Object key)

markServerDown

public void markServerDown(Server server)
Description copied from interface: ILoadBalancer
To be called by the clients of the load balancer to notify that a Server is down else, the LB will think its still Alive until the next Ping cycle - potentially (assuming that the LB Impl does a ping)

Specified by:
markServerDown in interface ILoadBalancer
Parameters:
server - Server to mark as down

markServerDown

public void markServerDown(java.lang.String id)

forceQuickPing

public void forceQuickPing()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

init

protected void init()
Register with monitors and start priming connections if it is set.


getPrimeConnections

public final PrimeConnections getPrimeConnections()

setPrimeConnections

public final void setPrimeConnections(PrimeConnections primeConnections)

primeCompleted

public void primeCompleted(Server s,
                           java.lang.Throwable lastException)
Specified by:
primeCompleted in interface PrimeConnections.PrimeConnectionListener

isEnablePrimingConnections

public final boolean isEnablePrimingConnections()

setEnablePrimingConnections

public final void setEnablePrimingConnections(boolean enablePrimingConnections)