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

java.lang.Object
  extended by com.netflix.astyanax.connectionpool.impl.SimpleHostConnectionPool<CL>
All Implemented Interfaces:
HostConnectionPool<CL>

public class SimpleHostConnectionPool<CL>
extends java.lang.Object
implements HostConnectionPool<CL>

Pool of connections for a single host and implements the HostConnectionPool interface

Salient Features

The class provides a bunch of counters for visibility into the connection pool status e.g no of that are available / active / pending / blocked etc.

This class also provides an async mechanism to create / prime and borrow Connection(s) using a LinkedBlockingQueue
Clients borrowing connections can wait at the end of the queue for a connection to be available. They send a tryOpenAsync() request to create a new connection before waiting, but don't necessarily wait for the same connection to be opened, since they could be unblocked by another client returning a previously used Connection The class also provides a markAsDown(ConnectionException) method which helps purge all connections and then attempts to init a new set of connections to the host.


Nested Class Summary
static interface SimpleHostConnectionPool.Listener<CL>
          Interface to notify the owning connection pool of up/down state changes.
 
Field Summary
protected  ConnectionPoolConfiguration config
           
 
Constructor Summary
SimpleHostConnectionPool(Host host, ConnectionFactory<CL> factory, ConnectionPoolMonitor monitor, ConnectionPoolConfiguration config, SimpleHostConnectionPool.Listener<CL> listener)
           
 
Method Summary
 void addLatencySample(long latency, long now)
          Add a single latency sample after an operation on a connection belonging to this pool
 Connection<CL> borrowConnection(int timeout)
          Create a connection as long the max hasn't been reached
 boolean closeConnection(Connection<CL> connection)
          Close this connection and update internal state
 int getActiveConnectionCount()
           
 int getBlockedThreadCount()
           
 int getBusyConnectionCount()
           
 int getClosedConnectionCount()
           
 int getConnectAttemptCount()
           
 int getErrorsSinceLastSuccess()
           
 int getFailedOpenConnectionCount()
           
 Host getHost()
           
 int getIdleConnectionCount()
           
 int getOpenedConnectionCount()
           
 int getPendingConnectionCount()
           
 double getScore()
           
 boolean isActive()
           
 boolean isReconnecting()
           
 boolean isShutdown()
           
 void markAsDown(ConnectionException reason)
          Mark the host as down.
 int primeConnections(int numConnections)
          Create numConnections new connections and add them to the
 boolean returnConnection(Connection<CL> connection)
          Return a connection to this host
 void shutdown()
          Completely shut down this connection pool as part of a client shutdown
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

config

protected final ConnectionPoolConfiguration config
Constructor Detail

SimpleHostConnectionPool

public SimpleHostConnectionPool(Host host,
                                ConnectionFactory<CL> factory,
                                ConnectionPoolMonitor monitor,
                                ConnectionPoolConfiguration config,
                                SimpleHostConnectionPool.Listener<CL> listener)
Method Detail

primeConnections

public int primeConnections(int numConnections)
                     throws ConnectionException,
                            java.lang.InterruptedException
Description copied from interface: HostConnectionPool
Create numConnections new connections and add them to the

Specified by:
primeConnections in interface HostConnectionPool<CL>
Throws:
ConnectionException
java.lang.InterruptedException

borrowConnection

public Connection<CL> borrowConnection(int timeout)
                                throws ConnectionException
Create a connection as long the max hasn't been reached

Specified by:
borrowConnection in interface HostConnectionPool<CL>
Parameters:
timeout - - Max wait timeout if max connections have been allocated and pool is empty. 0 to throw a MaxConnsPerHostReachedException.
Returns:
Throws:
TimeoutException - if timeout specified and no new connection is available MaxConnsPerHostReachedException if max connections created and no timeout was specified
ConnectionException

returnConnection

public boolean returnConnection(Connection<CL> connection)
Return a connection to this host

Specified by:
returnConnection in interface HostConnectionPool<CL>
Parameters:
connection -
Returns:
True if connection was closed

closeConnection

public boolean closeConnection(Connection<CL> connection)
Description copied from interface: HostConnectionPool
Close this connection and update internal state

Specified by:
closeConnection in interface HostConnectionPool<CL>

markAsDown

public void markAsDown(ConnectionException reason)
Mark the host as down. No new connections will be created from this host. Connections currently in use will be allowed to continue processing.

Specified by:
markAsDown in interface HostConnectionPool<CL>

shutdown

public void shutdown()
Description copied from interface: HostConnectionPool
Completely shut down this connection pool as part of a client shutdown

Specified by:
shutdown in interface HostConnectionPool<CL>

isShutdown

public boolean isShutdown()
Specified by:
isShutdown in interface HostConnectionPool<CL>
Returns:
Return true if the has been shut down and is no longer accepting traffic.

isReconnecting

public boolean isReconnecting()
Specified by:
isReconnecting in interface HostConnectionPool<CL>
Returns:
Return true if the pool is marked as down and is trying to reconnect

getHost

public Host getHost()
Specified by:
getHost in interface HostConnectionPool<CL>
Returns:
Get the host to which this pool is associated

getActiveConnectionCount

public int getActiveConnectionCount()
Specified by:
getActiveConnectionCount in interface HostConnectionPool<CL>
Returns:
Get number of open connections including any that are currently borrowed and those that are currently idel

getIdleConnectionCount

public int getIdleConnectionCount()
Specified by:
getIdleConnectionCount in interface HostConnectionPool<CL>
Returns:
Return the number of idle active connections. These are connections that can be borrowed immediatley without having to make a new connection to the remote server.

getPendingConnectionCount

public int getPendingConnectionCount()
Specified by:
getPendingConnectionCount in interface HostConnectionPool<CL>
Returns:
Get the number of pending connection open attempts

getBlockedThreadCount

public int getBlockedThreadCount()
Specified by:
getBlockedThreadCount in interface HostConnectionPool<CL>
Returns:
Get number of threads blocked waiting for a free connection

getOpenedConnectionCount

public int getOpenedConnectionCount()
Specified by:
getOpenedConnectionCount in interface HostConnectionPool<CL>
Returns:
Get total number of connections opened since the pool was created

getFailedOpenConnectionCount

public int getFailedOpenConnectionCount()
Specified by:
getFailedOpenConnectionCount in interface HostConnectionPool<CL>
Returns:
Get the total number of failed connection open attempts

getClosedConnectionCount

public int getClosedConnectionCount()
Specified by:
getClosedConnectionCount in interface HostConnectionPool<CL>
Returns:
Get total number of connections closed

getConnectAttemptCount

public int getConnectAttemptCount()
Specified by:
getConnectAttemptCount in interface HostConnectionPool<CL>
Returns:
Return the number of open connection attempts

getBusyConnectionCount

public int getBusyConnectionCount()
Specified by:
getBusyConnectionCount in interface HostConnectionPool<CL>
Returns:
Get number of currently borrowed connections

getScore

public double getScore()
Specified by:
getScore in interface HostConnectionPool<CL>
Returns:
Return implementation specific score to be used by weighted pool selection algorithms

addLatencySample

public void addLatencySample(long latency,
                             long now)
Description copied from interface: HostConnectionPool
Add a single latency sample after an operation on a connection belonging to this pool

Specified by:
addLatencySample in interface HostConnectionPool<CL>

getErrorsSinceLastSuccess

public int getErrorsSinceLastSuccess()
Specified by:
getErrorsSinceLastSuccess in interface HostConnectionPool<CL>
Returns:
Get number of errors since the last successful operation

toString

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

isActive

public boolean isActive()
Specified by:
isActive in interface HostConnectionPool<CL>
Returns:
Return true if the pool is active.