|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.danga.MemCached.SockIOPool
public class SockIOPool
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.
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();
Nested Class Summary | |
---|---|
protected static class |
SockIOPool.MaintThread
Class which extends thread and handles maintenance of the pool. |
static class |
SockIOPool.SockIO
MemCached Java client, utility class for Socket IO. |
Field Summary | |
---|---|
private boolean |
aliveCheck
|
private java.util.Map<java.lang.String,java.util.Map<SockIOPool.SockIO,java.lang.Long>> |
availPool
|
private java.util.List<java.lang.String> |
buckets
|
private java.util.Map<java.lang.String,java.util.Map<SockIOPool.SockIO,java.lang.Long>> |
busyPool
|
static int |
CONSISTENT_HASH
|
private java.util.TreeMap<java.lang.Long,java.lang.String> |
consistentBuckets
|
private java.util.Map<SockIOPool.SockIO,java.lang.Integer> |
deadPool
|
private boolean |
failback
|
private boolean |
failover
|
private int |
hashingAlg
|
private java.util.Map<java.lang.String,java.util.Date> |
hostDead
|
private java.util.Map<java.lang.String,java.lang.Long> |
hostDeadDur
|
private java.util.concurrent.locks.ReentrantLock |
hostDeadLock
|
private int |
initConn
|
private boolean |
initialized
|
private static org.apache.log4j.Logger |
log
|
private long |
maintSleep
|
private SockIOPool.MaintThread |
maintThread
|
static long |
MAX_RETRY_DELAY
|
private long |
maxBusyTime
|
private int |
maxConn
|
private int |
maxCreate
|
private long |
maxIdle
|
private static java.lang.ThreadLocal<java.security.MessageDigest> |
MD5
|
private int |
minConn
|
private boolean |
nagle
|
static int |
NATIVE_HASH
|
static int |
NEW_COMPAT_HASH
|
static int |
OLD_COMPAT_HASH
|
private int |
poolMultiplier
|
private static java.util.Map<java.lang.String,SockIOPool> |
pools
|
private java.lang.String[] |
servers
|
private int |
socketConnectTO
|
private int |
socketTO
|
private java.lang.Integer |
totalWeight
|
private java.lang.Integer[] |
weights
|
private static java.lang.Integer |
ZERO
|
Constructor Summary | |
---|---|
protected |
SockIOPool()
|
Method Summary | |
---|---|
protected void |
addSocketToPool(java.util.Map<java.lang.String,java.util.Map<SockIOPool.SockIO,java.lang.Long>> pool,
java.lang.String host,
SockIOPool.SockIO socket)
Adds a socket to a given pool for the given host. |
private void |
checkIn(SockIOPool.SockIO socket)
Returns a socket to the avail pool. |
private void |
checkIn(SockIOPool.SockIO socket,
boolean addToAvail)
Checks a SockIO object in with the pool. |
protected void |
clearHostFromPool(java.util.Map<java.lang.String,java.util.Map<SockIOPool.SockIO,java.lang.Long>> pool,
java.lang.String host)
Closes and removes all sockets from specified pool for host. |
protected void |
closePool(java.util.Map<java.lang.String,java.util.Map<SockIOPool.SockIO,java.lang.Long>> pool)
Closes all sockets in the passed in pool. |
protected SockIOPool.SockIO |
createSocket(java.lang.String host)
Creates a new SockIO obj for the given server. |
private java.lang.Long |
findPointFor(java.lang.Long hv)
Gets the first available key equal or above the given one, if none found, returns the first k in the bucket |
boolean |
getAliveCheck()
Returns the current status of the aliveCheck flag. |
private long |
getBucket(java.lang.String key,
java.lang.Integer hashCode)
|
SockIOPool.SockIO |
getConnection(java.lang.String host)
Returns a SockIO object from the pool for the passed in host. |
boolean |
getFailback()
Returns current state of failover flag. |
boolean |
getFailover()
Returns current state of failover flag. |
private long |
getHash(java.lang.String key,
java.lang.Integer hashCode)
Returns a bucket to check for a given key. |
int |
getHashingAlg()
Returns current status of customHash flag |
java.lang.String |
getHost(java.lang.String key)
|
java.lang.String |
getHost(java.lang.String key,
java.lang.Integer hashcode)
Gets the host that a particular key / hashcode resides on. |
int |
getInitConn()
Returns the current setting for the initial number of connections per server in the available pool. |
static SockIOPool |
getInstance()
Single argument version of factory used for back compat. |
static SockIOPool |
getInstance(java.lang.String poolName)
Factory to create/retrieve new pools given a unique poolName. |
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 |
java.lang.String[] |
getServers()
Returns the current list of all cache servers. |
SockIOPool.SockIO |
getSock(java.lang.String key)
Returns appropriate SockIO object given string cache key. |
SockIOPool.SockIO |
getSock(java.lang.String key,
java.lang.Integer hashCode)
Returns appropriate SockIO object given string cache key and optional hashcode. |
int |
getSocketConnectTO()
Returns the socket timeout for connect. |
int |
getSocketTO()
Returns the socket timeout for reads. |
java.lang.Integer[] |
getWeights()
Returns the current list of weights. |
void |
initialize()
Initializes the pool. |
boolean |
isInitialized()
Returns state of pool. |
private static long |
md5HashingAlg(java.lang.String key)
Internal private hashing method. |
private static long |
newCompatHashingAlg(java.lang.String key)
Internal private hashing method. |
private static long |
origCompatHashingAlg(java.lang.String key)
Internal private hashing method. |
private void |
populateBuckets()
|
private void |
populateConsistentBuckets()
|
protected void |
removeSocketFromPool(java.util.Map<java.lang.String,java.util.Map<SockIOPool.SockIO,java.lang.Long>> pool,
java.lang.String host,
SockIOPool.SockIO socket)
Removes a socket from specified pool for host. |
protected void |
selfMaint()
Runs self maintenance on all internal pools. |
void |
setAliveCheck(boolean aliveCheck)
Sets the aliveCheck flag for the pool. |
void |
setFailback(boolean failback)
Sets the failback flag for the pool. |
void |
setFailover(boolean failover)
Sets the failover flag for the pool. |
void |
setHashingAlg(int alg)
Sets the hashing algorithm we will use. |
void |
setInitConn(int initConn)
Sets the initial number of connections per server in the available pool. |
void |
setMaintSleep(long maintSleep)
Set the sleep time between runs of the pool maintenance thread. |
void |
setMaxBusyTime(long maxBusyTime)
Sets the max busy time for threads in the busy pool. |
void |
setMaxConn(int maxConn)
Sets the maximum number of spare connections allowed in our available pool. |
void |
setMaxIdle(long maxIdle)
Sets the max idle time for threads in the available pool. |
void |
setMinConn(int minConn)
Sets the minimum number of spare connections to maintain in our available pool. |
void |
setNagle(boolean nagle)
Sets the Nagle alg flag for the pool. |
void |
setServers(java.lang.String[] servers)
Sets the list of all cache servers. |
void |
setSocketConnectTO(int socketConnectTO)
Sets the socket timeout for connect. |
void |
setSocketTO(int socketTO)
Sets the socket timeout for reads. |
void |
setWeights(java.lang.Integer[] weights)
Sets the list of weights to apply to the server list. |
void |
shutDown()
Shuts down the pool. |
protected void |
startMaintThread()
Starts the maintenance thread. |
protected void |
stopMaintThread()
Stops the maintenance thread. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static org.apache.log4j.Logger log
private static java.util.Map<java.lang.String,SockIOPool> pools
private static java.lang.ThreadLocal<java.security.MessageDigest> MD5
private static final java.lang.Integer ZERO
public static final int NATIVE_HASH
public static final int OLD_COMPAT_HASH
public static final int NEW_COMPAT_HASH
public static final int CONSISTENT_HASH
public static final long MAX_RETRY_DELAY
private SockIOPool.MaintThread maintThread
private boolean initialized
private int maxCreate
private int poolMultiplier
private int initConn
private int minConn
private int maxConn
private long maxIdle
private long maxBusyTime
private long maintSleep
private int socketTO
private int socketConnectTO
private boolean aliveCheck
private boolean failover
private boolean failback
private boolean nagle
private int hashingAlg
private final java.util.concurrent.locks.ReentrantLock hostDeadLock
private java.lang.String[] servers
private java.lang.Integer[] weights
private java.lang.Integer totalWeight
private java.util.List<java.lang.String> buckets
private java.util.TreeMap<java.lang.Long,java.lang.String> consistentBuckets
private java.util.Map<java.lang.String,java.util.Date> hostDead
private java.util.Map<java.lang.String,java.lang.Long> hostDeadDur
private java.util.Map<java.lang.String,java.util.Map<SockIOPool.SockIO,java.lang.Long>> availPool
private java.util.Map<java.lang.String,java.util.Map<SockIOPool.SockIO,java.lang.Long>> busyPool
private java.util.Map<SockIOPool.SockIO,java.lang.Integer> deadPool
Constructor Detail |
---|
protected SockIOPool()
Method Detail |
---|
public static SockIOPool getInstance(java.lang.String poolName)
poolName
- unique name of the pool
public static SockIOPool getInstance()
public void setServers(java.lang.String[] servers)
servers
- String array of servers [host:port]public java.lang.String[] getServers()
public void setWeights(java.lang.Integer[] weights)
weights
- Integer array of weightspublic java.lang.Integer[] getWeights()
public void setInitConn(int initConn)
initConn
- int number of connectionspublic int getInitConn()
public void setMinConn(int minConn)
minConn
- number of connectionspublic int getMinConn()
public void setMaxConn(int maxConn)
maxConn
- number of connectionspublic int getMaxConn()
public void setMaxIdle(long maxIdle)
maxIdle
- idle time in mspublic long getMaxIdle()
public void setMaxBusyTime(long maxBusyTime)
maxBusyTime
- idle time in mspublic long getMaxBusy()
public void setMaintSleep(long maintSleep)
maintSleep
- sleep time in mspublic long getMaintSleep()
public void setSocketTO(int socketTO)
socketTO
- timeout in mspublic int getSocketTO()
public void setSocketConnectTO(int socketConnectTO)
socketConnectTO
- timeout in mspublic int getSocketConnectTO()
public void setFailover(boolean failover)
failover
- true/falsepublic boolean getFailover()
public void setFailback(boolean failback)
failback
- true/falsepublic boolean getFailback()
public void setAliveCheck(boolean aliveCheck)
aliveCheck
- true/falsepublic boolean getAliveCheck()
public void setNagle(boolean nagle)
nagle
- true/falsepublic boolean getNagle()
public void setHashingAlg(int alg)
alg
- int value representing hashing algorithmpublic int getHashingAlg()
private static long origCompatHashingAlg(java.lang.String key)
key
- String to hash
private static long newCompatHashingAlg(java.lang.String key)
key
-
private static long md5HashingAlg(java.lang.String key)
key
-
private long getHash(java.lang.String key, java.lang.Integer hashCode)
key
- String key cache is stored under
private long getBucket(java.lang.String key, java.lang.Integer hashCode)
private java.lang.Long findPointFor(java.lang.Long hv)
k
- key
public void initialize()
private void populateBuckets()
private void populateConsistentBuckets()
public boolean isInitialized()
true
if initialized.protected SockIOPool.SockIO createSocket(java.lang.String host)
host
- host:port to connect to
public java.lang.String getHost(java.lang.String key)
key
-
public java.lang.String getHost(java.lang.String key, java.lang.Integer hashcode)
key
- hashcode
-
public SockIOPool.SockIO getSock(java.lang.String key)
key
- hashcode for cache key
public SockIOPool.SockIO getSock(java.lang.String key, java.lang.Integer hashCode)
key
- hashcode for cache keyhashCode
- if not null, then the int hashcode to use
public SockIOPool.SockIO getConnection(java.lang.String host)
host
- host from which to retrieve object
protected void addSocketToPool(java.util.Map<java.lang.String,java.util.Map<SockIOPool.SockIO,java.lang.Long>> pool, java.lang.String host, SockIOPool.SockIO socket)
pool
- pool to add tohost
- host this socket is connected tosocket
- socket to addprotected void removeSocketFromPool(java.util.Map<java.lang.String,java.util.Map<SockIOPool.SockIO,java.lang.Long>> pool, java.lang.String host, SockIOPool.SockIO socket)
pool
- pool to remove fromhost
- host poolsocket
- socket to removeprotected void clearHostFromPool(java.util.Map<java.lang.String,java.util.Map<SockIOPool.SockIO,java.lang.Long>> pool, java.lang.String host)
pool
- pool to clearhost
- host to clearprivate void checkIn(SockIOPool.SockIO socket, boolean addToAvail)
socket
- socket to returnaddToAvail
- add to avail pool if trueprivate void checkIn(SockIOPool.SockIO socket)
socket
- socket to returnprotected void closePool(java.util.Map<java.lang.String,java.util.Map<SockIOPool.SockIO,java.lang.Long>> pool)
pool
- pool to closepublic void shutDown()
protected void startMaintThread()
protected void stopMaintThread()
protected void selfMaint()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |