SockIOPool Class Reference

Collaboration diagram for SockIOPool:

Collaboration graph
[legend]

List of all members.

Classes

class  MaintThread
class  SockIO

Public Member Functions

boolean getAliveCheck ()
SockIO getConnection (String host)
boolean getFailback ()
boolean getFailover ()
int getHashingAlg ()
String getHost (String key, Integer hashcode)
String getHost (String key)
int getInitConn ()
long getMaintSleep ()
long getMaxBusy ()
int getMaxConn ()
long getMaxIdle ()
int getMinConn ()
boolean getNagle ()
String[] getServers ()
SockIO getSock (String key, Integer hashCode)
SockIO getSock (String key)
int getSocketConnectTO ()
int getSocketTO ()
Integer[] getWeights ()
void initialize ()
boolean isInitialized ()
void setAliveCheck (boolean aliveCheck)
void setFailback (boolean failback)
void setFailover (boolean failover)
void setHashingAlg (int alg)
void setInitConn (int initConn)
void setMaintSleep (long maintSleep)
void setMaxBusyTime (long maxBusyTime)
void setMaxConn (int maxConn)
void setMaxIdle (long maxIdle)
void setMinConn (int minConn)
void setNagle (boolean nagle)
void setServers (String[] servers)
void setSocketConnectTO (int socketConnectTO)
void setSocketTO (int socketTO)
void setWeights (Integer[] weights)
void shutDown ()

Static Public Member Functions

static SockIOPool getInstance ()
static synchronized SockIOPool getInstance (String poolName)

Static Public Attributes

static final int CONSISTENT_HASH = 3
static final long MAX_RETRY_DELAY = 10 * 60 * 1000
static final int NATIVE_HASH = 0
static final int NEW_COMPAT_HASH = 2
static final int OLD_COMPAT_HASH = 1

Protected Member Functions

void addSocketToPool (Map< String, Map< SockIO, Long >> pool, String host, SockIO socket)
void clearHostFromPool (Map< String, Map< SockIO, Long >> pool, String host)
void closePool (Map< String, Map< SockIO, Long >> pool)
SockIO createSocket (String host)
void removeSocketFromPool (Map< String, Map< SockIO, Long >> pool, String host, SockIO socket)
void selfMaint ()
 SockIOPool ()
void startMaintThread ()
void stopMaintThread ()

Private Member Functions

void checkIn (SockIO socket)
void checkIn (SockIO socket, boolean addToAvail)
Long findPointFor (Long hv)
long getBucket (String key, Integer hashCode)
long getHash (String key, Integer hashCode)
void populateBuckets ()
void populateConsistentBuckets ()

Static Private Member Functions

static long md5HashingAlg (String key)
static long newCompatHashingAlg (String key)
static long origCompatHashingAlg (String key)

Private Attributes

boolean aliveCheck = false
Map< String, Map< SockIO, Long > > availPool
List< Stringbuckets
Map< String, Map< SockIO, Long > > busyPool
TreeMap< Long, StringconsistentBuckets
Map< SockIO, Integer > deadPool
boolean failback = true
boolean failover = true
int hashingAlg = NATIVE_HASH
Map< String, Date > hostDead
Map< String, Long > hostDeadDur
final ReentrantLock hostDeadLock = new ReentrantLock()
int initConn = 1
boolean initialized = false
long maintSleep = 1000 * 30
MaintThread maintThread
long maxBusyTime = 1000 * 30
int maxConn = 10
int maxCreate = 1
long maxIdle = 1000 * 60 * 5
int minConn = 1
boolean nagle = true
int poolMultiplier = 3
String[] servers
int socketConnectTO = 1000 * 3
int socketTO = 1000 * 30
Integer totalWeight = 0
Integer[] weights

Static Private Attributes

static Logger log
static ThreadLocal< MessageDigest > MD5
static Map< String, SockIOPoolpools
static final Integer ZERO = new Integer( 0 )


Detailed Description

This class is a connection pool for maintaning a pool of persistent connections
to memcached servers.

The pool must be initialized prior to use. This should typically be early on
in the lifecycle of the JVM instance.

An example of initializing using defaults:

	static {
		String[] serverlist = { "cache0.server.com:12345", "cache1.server.com:12345" };

		SockIOPool pool = SockIOPool.getInstance();
		pool.setServers(serverlist);
		pool.initialize();	
	}
 

An example of initializing using defaults and providing weights for servers:

	static {
		String[] serverlist = { "cache0.server.com:12345", "cache1.server.com:12345" };
		Integer[] weights   = { new Integer(5), new Integer(2) };

		SockIOPool pool = SockIOPool.getInstance();
		pool.setServers(serverlist);
		pool.setWeights(weights);	
		pool.initialize();	
	}
  

An example of initializing overriding defaults:

	static {
		String[] serverlist     = { "cache0.server.com:12345", "cache1.server.com:12345" };
		Integer[] weights       = { new Integer(5), new Integer(2) };	
		int initialConnections  = 10;
		int minSpareConnections = 5;
		int maxSpareConnections = 50;	
		long maxIdleTime        = 1000 * 60 * 30;	// 30 minutes
		long maxBusyTime        = 1000 * 60 * 5;	// 5 minutes
		long maintThreadSleep   = 1000 * 5;			// 5 seconds
		int	socketTimeOut       = 1000 * 3;			// 3 seconds to block on reads
		int	socketConnectTO     = 1000 * 3;			// 3 seconds to block on initial connections.  If 0, then will use blocking connect (default)
		boolean failover        = false;			// turn off auto-failover in event of server down	
		boolean nagleAlg        = false;			// turn off Nagle's algorithm on all sockets in pool	
		boolean aliveCheck      = false;			// disable health check of socket on checkout

		SockIOPool pool = SockIOPool.getInstance();
		pool.setServers( serverlist );
		pool.setWeights( weights );	
		pool.setInitConn( initialConnections );
		pool.setMinConn( minSpareConnections );
		pool.setMaxConn( maxSpareConnections );
		pool.setMaxIdle( maxIdleTime );
		pool.setMaxBusyTime( maxBusyTime );
		pool.setMaintSleep( maintThreadSleep );
		pool.setSocketTO( socketTimeOut );
		pool.setNagle( nagleAlg );	
		pool.setHashingAlg( SockIOPool.NEW_COMPAT_HASH );
		pool.setAliveCheck( true );
		pool.initialize();	
	}
  
The easiest manner in which to initialize the pool is to set the servers and rely on defaults as in the first example.
After pool is initialized, a client will request a SockIO object by calling getSock with the cache key
The client must always close the SockIO object when finished, which will return the connection back to the pool.

An example of retrieving a SockIO object:

		SockIOPool.SockIO sock = SockIOPool.getInstance().getSock( key );
		try {
			sock.write( "version\r\n" );	
			sock.flush();	
			System.out.println( "Version: " + sock.readLine() );	
		}
		catch (IOException ioe) { System.out.println( "io exception thrown" ) };

		sock.close();	
 

Author:
greg whalin <greg@whalin.com>
Version:
1.5

Constructor & Destructor Documentation

SockIOPool (  )  [protected]


Member Function Documentation

void addSocketToPool ( Map< String, Map< SockIO, Long >>  pool,
String  host,
SockIO  socket 
) [protected]

Adds a socket to a given pool for the given host. THIS METHOD IS NOT THREADSAFE, SO BE CAREFUL WHEN USING!

Internal utility method.

Parameters:
pool pool to add to
host host this socket is connected to
socket socket to add

void checkIn ( SockIO  socket  )  [private]

Returns a socket to the avail pool.

This is called from SockIO.close(). Calling this method
directly without closing the SockIO object first
will cause an IOException to be thrown.

Parameters:
socket socket to return

void checkIn ( SockIO  socket,
boolean  addToAvail 
) [private]

Checks a SockIO object in with the pool.

This will remove SocketIO from busy pool, and optionally
add to avail pool.

Parameters:
socket socket to return
addToAvail add to avail pool if true

void clearHostFromPool ( Map< String, Map< SockIO, Long >>  pool,
String  host 
) [protected]

Closes and removes all sockets from specified pool for host. THIS METHOD IS NOT THREADSAFE, SO BE CAREFUL WHEN USING!

Internal utility method.

Parameters:
pool pool to clear
host host to clear

void closePool ( Map< String, Map< SockIO, Long >>  pool  )  [protected]

Closes all sockets in the passed in pool.

Internal utility method.

Parameters:
pool pool to close

SockIO createSocket ( String  host  )  [protected]

Creates a new SockIO obj for the given server.

If server fails to connect, then return null and do not try
again until a duration has passed. This duration will grow
by doubling after each failed attempt to connect.

Parameters:
host host:port to connect to
Returns:
SockIO obj or null if failed to create

Long findPointFor ( Long  hv  )  [private]

Gets the first available key equal or above the given one, if none found, returns the first k in the bucket

Parameters:
k key
Returns:

boolean getAliveCheck (  ) 

Returns the current status of the aliveCheck flag.

Returns:
true / false

long getBucket ( String  key,
Integer  hashCode 
) [private]

SockIO getConnection ( String  host  ) 

Returns a SockIO object from the pool for the passed in host.

Meant to be called from a more intelligent method
which handles choosing appropriate server
and failover.

Parameters:
host host from which to retrieve object
Returns:
SockIO object or null if fail to retrieve one

boolean getFailback (  ) 

Returns current state of failover flag.

Returns:
true/false

boolean getFailover (  ) 

Returns current state of failover flag.

Returns:
true/false

long getHash ( String  key,
Integer  hashCode 
) [private]

Returns a bucket to check for a given key.

Parameters:
key String key cache is stored under
Returns:
int bucket

int getHashingAlg (  ) 

Returns current status of customHash flag

Returns:
true/false

String getHost ( String  key,
Integer  hashcode 
)

Gets the host that a particular key / hashcode resides on.

Parameters:
key 
hashcode 
Returns:

String getHost ( String  key  ) 

Parameters:
key 
Returns:

int getInitConn (  ) 

Returns the current setting for the initial number of connections per server in the available pool.

Returns:
number of connections

static SockIOPool getInstance (  )  [static]

Single argument version of factory used for back compat. Simply creates a pool named "default".

Returns:
instance of SockIOPool

static synchronized SockIOPool getInstance ( String  poolName  )  [static]

Factory to create/retrieve new pools given a unique poolName.

Parameters:
poolName unique name of the pool
Returns:
instance of SockIOPool

long getMaintSleep (  ) 

Returns the current maint thread sleep time.

Returns:
sleep time in ms

long getMaxBusy (  ) 

Returns the current max busy setting.

Returns:
max busy setting in ms

int getMaxConn (  ) 

Returns the maximum number of spare connections allowed in available pool.

Returns:
number of connections

long getMaxIdle (  ) 

Returns the current max idle setting.

Returns:
max idle setting in ms

int getMinConn (  ) 

Returns the minimum number of spare connections in available pool.

Returns:
number of connections

boolean getNagle (  ) 

Returns current status of nagle flag

Returns:
true/false

String [] getServers (  ) 

Returns the current list of all cache servers.

Returns:
String array of servers [host:port]

SockIO getSock ( String  key,
Integer  hashCode 
)

Returns appropriate SockIO object given string cache key and optional hashcode.

Trys to get SockIO from pool. Fails over to additional pools in event of server failure.

Parameters:
key hashcode for cache key
hashCode if not null, then the int hashcode to use
Returns:
SockIO obj connected to server

SockIO getSock ( String  key  ) 

Returns appropriate SockIO object given string cache key.

Parameters:
key hashcode for cache key
Returns:
SockIO obj connected to server

int getSocketConnectTO (  ) 

Returns the socket timeout for connect.

Returns:
timeout in ms

int getSocketTO (  ) 

Returns the socket timeout for reads.

Returns:
timeout in ms

Integer [] getWeights (  ) 

Returns the current list of weights.

Returns:
int array of weights

void initialize (  ) 

Initializes the pool.

boolean isInitialized (  ) 

Returns state of pool.

Returns:
true if initialized.

static long md5HashingAlg ( String  key  )  [static, private]

Internal private hashing method.

MD5 based hash algorithm for use in the consistent hashing approach.

Parameters:
key 
Returns:

static long newCompatHashingAlg ( String  key  )  [static, private]

Internal private hashing method.

This is the new hashing algorithm from other clients. Found to be fast and have very good distribution.

UPDATE: This is dog slow under java

Parameters:
key 
Returns:

static long origCompatHashingAlg ( String  key  )  [static, private]

Internal private hashing method.

This is the original hashing algorithm from other clients. Found to be slow and have poor distribution.

Parameters:
key String to hash
Returns:
hashCode for this string using our own hashing algorithm

void populateBuckets (  )  [private]

void populateConsistentBuckets (  )  [private]

void removeSocketFromPool ( Map< String, Map< SockIO, Long >>  pool,
String  host,
SockIO  socket 
) [protected]

Removes a socket from specified pool for host. THIS METHOD IS NOT THREADSAFE, SO BE CAREFUL WHEN USING!

Internal utility method.

Parameters:
pool pool to remove from
host host pool
socket socket to remove

void selfMaint (  )  [protected]

Runs self maintenance on all internal pools.

This is typically called by the maintenance thread to manage pool size.

void setAliveCheck ( boolean  aliveCheck  ) 

Sets the aliveCheck flag for the pool.

When true, this will attempt to talk to the server on every connection checkout to make sure the connection is still valid. This adds extra network chatter and thus is defaulted off. May be useful if you want to ensure you do not have any problems talking to the server on a dead connection.

Parameters:
aliveCheck true/false

void setFailback ( boolean  failback  ) 

Sets the failback flag for the pool.

If this is true and we have marked a host as dead, will try to bring it back. If it is false, we will never try to resurrect a dead host.

Parameters:
failback true/false

void setFailover ( boolean  failover  ) 

Sets the failover flag for the pool.

If this flag is set to true, and a socket fails to connect,
the pool will attempt to return a socket from another server
if one exists. If set to false, then getting a socket
will return null if it fails to connect to the requested server.

Parameters:
failover true/false

void setHashingAlg ( int  alg  ) 

Sets the hashing algorithm we will use.

The types are as follows.

SockIOPool.NATIVE_HASH (0) - native String.hashCode() - fast (cached) but not compatible with other clients SockIOPool.OLD_COMPAT_HASH (1) - original compatibility hashing alg (works with other clients) SockIOPool.NEW_COMPAT_HASH (2) - new CRC32 based compatibility hashing algorithm (fast and works with other clients)

Parameters:
alg int value representing hashing algorithm

void setInitConn ( int  initConn  ) 

Sets the initial number of connections per server in the available pool.

Parameters:
initConn int number of connections

void setMaintSleep ( long  maintSleep  ) 

Set the sleep time between runs of the pool maintenance thread. If set to 0, then the maint thread will not be started.

Parameters:
maintSleep sleep time in ms

void setMaxBusyTime ( long  maxBusyTime  ) 

Sets the max busy time for threads in the busy pool.

Parameters:
maxBusyTime idle time in ms

void setMaxConn ( int  maxConn  ) 

Sets the maximum number of spare connections allowed in our available pool.

Parameters:
maxConn number of connections

void setMaxIdle ( long  maxIdle  ) 

Sets the max idle time for threads in the available pool.

Parameters:
maxIdle idle time in ms

void setMinConn ( int  minConn  ) 

Sets the minimum number of spare connections to maintain in our available pool.

Parameters:
minConn number of connections

void setNagle ( boolean  nagle  ) 

Sets the Nagle alg flag for the pool.

If false, will turn off Nagle's algorithm on all sockets created.

Parameters:
nagle true/false

void setServers ( String[]  servers  ) 

Sets the list of all cache servers.

Parameters:
servers String array of servers [host:port]

void setSocketConnectTO ( int  socketConnectTO  ) 

Sets the socket timeout for connect.

Parameters:
socketConnectTO timeout in ms

void setSocketTO ( int  socketTO  ) 

Sets the socket timeout for reads.

Parameters:
socketTO timeout in ms

void setWeights ( Integer[]  weights  ) 

Sets the list of weights to apply to the server list.

This is an int array with each element corresponding to an element
in the same position in the server String array.

Parameters:
weights Integer array of weights

void shutDown (  ) 

Shuts down the pool.

Cleanly closes all sockets.
Stops the maint thread.
Nulls out all internal maps

void startMaintThread (  )  [protected]

Starts the maintenance thread.

This thread will manage the size of the active pool
as well as move any closed, but not checked in sockets
back to the available pool.

void stopMaintThread (  )  [protected]

Stops the maintenance thread.


Member Data Documentation

boolean aliveCheck = false [private]

Map<String,Map<SockIO,Long> > availPool [private]

List<String> buckets [private]

Map<String,Map<SockIO,Long> > busyPool [private]

final int CONSISTENT_HASH = 3 [static]

TreeMap<Long,String> consistentBuckets [private]

Map<SockIO,Integer> deadPool [private]

boolean failback = true [private]

boolean failover = true [private]

int hashingAlg = NATIVE_HASH [private]

Map<String,Date> hostDead [private]

Map<String,Long> hostDeadDur [private]

final ReentrantLock hostDeadLock = new ReentrantLock() [private]

int initConn = 1 [private]

boolean initialized = false [private]

Logger log [static, private]

Initial value:

                Logger.getLogger( SockIOPool.class.getName() )

long maintSleep = 1000 * 30 [private]

final long MAX_RETRY_DELAY = 10 * 60 * 1000 [static]

long maxBusyTime = 1000 * 30 [private]

int maxConn = 10 [private]

int maxCreate = 1 [private]

long maxIdle = 1000 * 60 * 5 [private]

ThreadLocal<MessageDigest> MD5 [static, private]

Initial value:

 new ThreadLocal<MessageDigest>() {
                @Override
                protected MessageDigest initialValue() {
                        try {
                                return MessageDigest.getInstance( "MD5" );
                        }
                        catch ( NoSuchAlgorithmException e ) {
                                log.error( "++++ no md5 algorithm found" );
                                throw new IllegalStateException( "++++ no md5 algorythm found");                        
                        }
                }
        }

int minConn = 1 [private]

boolean nagle = true [private]

final int NATIVE_HASH = 0 [static]

final int NEW_COMPAT_HASH = 2 [static]

final int OLD_COMPAT_HASH = 1 [static]

int poolMultiplier = 3 [private]

Map<String,SockIOPool> pools [static, private]

Initial value:

                new HashMap<String,SockIOPool>()

String [] servers [private]

int socketConnectTO = 1000 * 3 [private]

int socketTO = 1000 * 30 [private]

Integer totalWeight = 0 [private]

Integer [] weights [private]

final Integer ZERO = new Integer( 0 ) [static, private]


The documentation for this class was generated from the following file:

Generated on Sat May 26 06:42:31 2012 for Project Wonder by  doxygen 1.5.8