com.netflix.astyanax.connectionpool.impl
Class AbstractHostPartitionConnectionPool<CL>

java.lang.Object
  extended by com.netflix.astyanax.connectionpool.impl.AbstractHostPartitionConnectionPool<CL>
Type Parameters:
CL -
All Implemented Interfaces:
ConnectionPool<CL>, SimpleHostConnectionPool.Listener<CL>
Direct Known Subclasses:
BagOfConnectionsConnectionPoolImpl, RoundRobinConnectionPoolImpl, TokenAwareConnectionPoolImpl

public abstract class AbstractHostPartitionConnectionPool<CL>
extends java.lang.Object
implements ConnectionPool<CL>, SimpleHostConnectionPool.Listener<CL>

Base for all connection pools that keep a separate pool of connections for each host.

Set of host connection pools
The class maintains the set of all HostConnectionPools for all hosts in the token ring using a non blocking hashmap and a Topology The hashmap tracks the basic set of hosts and their corresponding connection pools. The topology is used to track the internal state of the token ring for the cassandra cluster.
The class uses these 2 structures to determine whether there has been a change to the system when a host joins or leaves the ring, or even if an existing host just receives an update for the token ring partition that it owns. Hence the class can actively rebuild all the partitions associated with each as and when there is a change.
See addHost(Host, boolean) removeHost(Host, boolean) setHosts(Collection) for host changes and the corresponding trigger to rebuildPartitions() when this happens.
Note that when the connection pool is started it fetches the list of seed hosts from config (if any) and then inits it's data structures using these seed hosts.
It also employs a listener to the latency score updates so that it can rebuild partitions as and when it receives updates

Execute with failover
The class also provides a basic implementation of executeWithFailover(Operation, RetryPolicy) by repeatedly consulting the RetryPolicy. Note that extending classes must provide impl for the actual execute with failover. e.g the RoundRobinConnectionPoolImpl fails over to the next HostConnectionPool in the list.

See Also:
RoundRobinConnectionPoolImpl} {@link BagOfConnectionsConnectionPoolImpl} {@link TokenAwareConnectionPoolImpl} for details on impls that extend this class, TokenPartitionedTopology} for how the internal topology structure is maintained across the set of hosts

Field Summary
protected  ConnectionPoolConfiguration config
           
protected  ConnectionFactory<CL> factory
           
protected  org.cliffc.high_scale_lib.NonBlockingHashMap<Host,HostConnectionPool<CL>> hosts
           
protected  ConnectionPoolMonitor monitor
           
protected  Partitioner partitioner
           
protected  Topology<CL> topology
           
 
Constructor Summary
AbstractHostPartitionConnectionPool(ConnectionPoolConfiguration config, ConnectionFactory<CL> factory, ConnectionPoolMonitor monitor)
           
 
Method Summary
 boolean addHost(Host host, boolean refresh)
          Add host to the system.
<R> OperationResult<R>
executeWithFailover(Operation<CL,R> op, RetryPolicy retry)
          Executes the operation using failover and retry strategy
 java.util.List<HostConnectionPool<CL>> getActivePools()
          list of all active pools
 HostConnectionPool<CL> getHostPool(Host host)
           
 Partitioner getPartitioner()
           
 java.util.List<HostConnectionPool<CL>> getPools()
           
 Topology<CL> getTopology()
           
 boolean hasHost(Host host)
           
 boolean isHostUp(Host host)
           
protected abstract
<R> ExecuteWithFailover<CL,R>
newExecuteWithFailover(Operation<CL,R> op)
          Return a new failover context.
protected  HostConnectionPool<CL> newHostConnectionPool(Host host, ConnectionFactory<CL> factory, ConnectionPoolConfiguration config)
           
 void onHostDown(HostConnectionPool<CL> pool)
          Host is marked as down
 void onHostUp(HostConnectionPool<CL> pool)
          Host is marked as up
protected  void rebuildPartitions()
          Called every time a host is added, removed or is marked as down
 boolean removeHost(Host host, boolean refresh)
          Remove host from the system.
 void setHosts(java.util.Collection<Host> ring)
          Set the complete set of hosts in the ring
 void shutdown()
          Clean up resources associated with the conn pool
 void start()
          Starts the conn pool and resources associated with it
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

hosts

protected final org.cliffc.high_scale_lib.NonBlockingHashMap<Host,HostConnectionPool<CL>> hosts

config

protected final ConnectionPoolConfiguration config

factory

protected final ConnectionFactory<CL> factory

monitor

protected final ConnectionPoolMonitor monitor

topology

protected final Topology<CL> topology

partitioner

protected final Partitioner partitioner
Constructor Detail

AbstractHostPartitionConnectionPool

public AbstractHostPartitionConnectionPool(ConnectionPoolConfiguration config,
                                           ConnectionFactory<CL> factory,
                                           ConnectionPoolMonitor monitor)
Parameters:
config -
factory -
monitor -
Method Detail

start

public void start()
Starts the conn pool and resources associated with it

Specified by:
start in interface ConnectionPool<CL>

shutdown

public void shutdown()
Clean up resources associated with the conn pool

Specified by:
shutdown in interface ConnectionPool<CL>

newHostConnectionPool

protected HostConnectionPool<CL> newHostConnectionPool(Host host,
                                                       ConnectionFactory<CL> factory,
                                                       ConnectionPoolConfiguration config)

onHostDown

public void onHostDown(HostConnectionPool<CL> pool)
Host is marked as down

Specified by:
onHostDown in interface SimpleHostConnectionPool.Listener<CL>
Parameters:
pool -

onHostUp

public void onHostUp(HostConnectionPool<CL> pool)
Host is marked as up

Specified by:
onHostUp in interface SimpleHostConnectionPool.Listener<CL>
Parameters:
pool -

addHost

public final boolean addHost(Host host,
                             boolean refresh)
Add host to the system. May need to rebuild the partition map of the system

Specified by:
addHost in interface ConnectionPool<CL>
Parameters:
host -
refresh -

isHostUp

public boolean isHostUp(Host host)
Specified by:
isHostUp in interface ConnectionPool<CL>
Returns:
boolean

hasHost

public boolean hasHost(Host host)
Specified by:
hasHost in interface ConnectionPool<CL>
Returns:
boolean

getActivePools

public java.util.List<HostConnectionPool<CL>> getActivePools()
list of all active pools

Specified by:
getActivePools in interface ConnectionPool<CL>
Returns:
List

getPools

public java.util.List<HostConnectionPool<CL>> getPools()
Specified by:
getPools in interface ConnectionPool<CL>
Returns:
List

removeHost

public boolean removeHost(Host host,
                          boolean refresh)
Remove host from the system. Shuts down pool associated with the host and rebuilds partition map

Specified by:
removeHost in interface ConnectionPool<CL>
Parameters:
host -
refresh -

getHostPool

public HostConnectionPool<CL> getHostPool(Host host)
Specified by:
getHostPool in interface ConnectionPool<CL>
Parameters:
host -
Returns:
HostConnectionPool

setHosts

public void setHosts(java.util.Collection<Host> ring)
Description copied from interface: ConnectionPool
Set the complete set of hosts in the ring

Specified by:
setHosts in interface ConnectionPool<CL>
Parameters:
ring -

executeWithFailover

public <R> OperationResult<R> executeWithFailover(Operation<CL,R> op,
                                                  RetryPolicy retry)
                                       throws ConnectionException
Executes the operation using failover and retry strategy

Specified by:
executeWithFailover in interface ConnectionPool<CL>
Parameters:
op -
retry -
Returns:
OperationResult
Throws:
ConnectionException
OperationException

newExecuteWithFailover

protected abstract <R> ExecuteWithFailover<CL,R> newExecuteWithFailover(Operation<CL,R> op)
                                                             throws ConnectionException
Return a new failover context. The context captures the connection pool state and implements the necessary failover logic.

Type Parameters:
R -
Returns:
Throws:
ConnectionException

rebuildPartitions

protected void rebuildPartitions()
Called every time a host is added, removed or is marked as down


getTopology

public Topology<CL> getTopology()
Specified by:
getTopology in interface ConnectionPool<CL>
Returns:
Topology

getPartitioner

public Partitioner getPartitioner()
Specified by:
getPartitioner in interface ConnectionPool<CL>
Returns:
Partitioner