
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< String > | buckets |
| Map< String, Map< SockIO, Long > > | busyPool |
| TreeMap< Long, String > | consistentBuckets |
| 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, SockIOPool > | pools |
| static final Integer | ZERO = new Integer( 0 ) |
The pool must be initialized prior to use. This should typically be early on
in the lifecycle of the JVM instance.
static {
String[] serverlist = { "cache0.server.com:12345", "cache1.server.com:12345" };
SockIOPool pool = SockIOPool.getInstance(); pool.setServers(serverlist); pool.initialize(); }
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(); }
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.
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();
| SockIOPool | ( | ) | [protected] |
| 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.
| 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.
| 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.
| socket | socket to return | |
| addToAvail | add to avail pool if true |
Closes and removes all sockets from specified pool for host. THIS METHOD IS NOT THREADSAFE, SO BE CAREFUL WHEN USING!
Internal utility method.
| pool | pool to clear | |
| host | host to clear |
Closes all sockets in the passed in pool.
Internal utility method.
| pool | pool to close |
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.
| host | host:port to connect to |
| 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
| k | key |
| boolean getAliveCheck | ( | ) |
Returns the current status of the aliveCheck flag.
| long getBucket | ( | String | key, | |
| Integer | hashCode | |||
| ) | [private] |
| boolean getFailback | ( | ) |
Returns current state of failover flag.
| boolean getFailover | ( | ) |
Returns current state of failover flag.
| long getHash | ( | String | key, | |
| Integer | hashCode | |||
| ) | [private] |
Returns a bucket to check for a given key.
| key | String key cache is stored under |
| int getHashingAlg | ( | ) |
Returns current status of customHash flag
Gets the host that a particular key / hashcode resides on.
| key | ||
| hashcode |
| int getInitConn | ( | ) |
Returns the current setting for the initial number of connections per server in the available pool.
| static SockIOPool getInstance | ( | ) | [static] |
Single argument version of factory used for back compat. Simply creates a pool named "default".
| static synchronized SockIOPool getInstance | ( | String | poolName | ) | [static] |
Factory to create/retrieve new pools given a unique poolName.
| poolName | unique name of the pool |
| long getMaintSleep | ( | ) |
Returns the current maint thread sleep time.
| long getMaxBusy | ( | ) |
Returns the current max busy setting.
| int getMaxConn | ( | ) |
Returns the maximum number of spare connections allowed in available pool.
| long getMaxIdle | ( | ) |
Returns the current max idle setting.
| int getMinConn | ( | ) |
Returns the minimum number of spare connections in available pool.
| boolean getNagle | ( | ) |
Returns current status of nagle flag
| String [] getServers | ( | ) |
Returns the current list of all cache servers.
| int getSocketConnectTO | ( | ) |
Returns the socket timeout for connect.
| int getSocketTO | ( | ) |
Returns the socket timeout for reads.
| Integer [] getWeights | ( | ) |
Returns the current list of weights.
| void initialize | ( | ) |
Initializes the pool.
| boolean isInitialized | ( | ) |
Returns state of pool.
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.
| key |
| 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
| key |
| 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.
| key | String to hash |
| 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.
| 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.
| 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.
| 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.
| 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)
| alg | int value representing hashing algorithm |
| void setInitConn | ( | int | initConn | ) |
Sets the initial number of connections per server in the available pool.
| 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.
| maintSleep | sleep time in ms |
| void setMaxBusyTime | ( | long | maxBusyTime | ) |
Sets the max busy time for threads in the busy pool.
| maxBusyTime | idle time in ms |
| void setMaxConn | ( | int | maxConn | ) |
Sets the maximum number of spare connections allowed in our available pool.
| maxConn | number of connections |
| void setMaxIdle | ( | long | maxIdle | ) |
Sets the max idle time for threads in the available pool.
| maxIdle | idle time in ms |
| void setMinConn | ( | int | minConn | ) |
Sets the minimum number of spare connections to maintain in our available pool.
| 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.
| nagle | true/false |
| void setServers | ( | String[] | servers | ) |
Sets the list of all cache servers.
| servers | String array of servers [host:port] |
| void setSocketConnectTO | ( | int | socketConnectTO | ) |
Sets the socket timeout for connect.
| socketConnectTO | timeout in ms |
| void setSocketTO | ( | int | socketTO | ) |
Sets the socket timeout for reads.
| 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.
| 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.
boolean aliveCheck = false [private] |
final int CONSISTENT_HASH = 3 [static] |
TreeMap<Long,String> consistentBuckets [private] |
boolean failback = true [private] |
boolean failover = true [private] |
int hashingAlg = NATIVE_HASH [private] |
Map<String,Long> hostDeadDur [private] |
final ReentrantLock hostDeadLock = new ReentrantLock() [private] |
int initConn = 1 [private] |
boolean initialized = false [private] |
Initial value:
Logger.getLogger( SockIOPool.class.getName() )
long maintSleep = 1000 * 30 [private] |
MaintThread maintThread [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>()
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] |
1.5.8