Package net.spy.memcached
Class DefaultConnectionFactory
- java.lang.Object
-
- net.spy.memcached.compat.SpyObject
-
- net.spy.memcached.DefaultConnectionFactory
-
- All Implemented Interfaces:
ConnectionFactory
- Direct Known Subclasses:
BinaryConnectionFactory
,KetamaConnectionFactory
public class DefaultConnectionFactory extends SpyObject implements ConnectionFactory
Default implementation of ConnectionFactory.This implementation creates connections where the operation queue is an ArrayBlockingQueue and the read and write queues are unbounded LinkedBlockingQueues. The
Redistribute
FailureMode is always used. If other FailureModes are needed, look at the ConnectionFactoryBuilder.
-
-
Field Summary
Fields Modifier and Type Field Description static long
DEFAULT_AUTH_WAIT_TIME
The time to wait for the authentication to complete when a operation is written in milliseconds.static FailureMode
DEFAULT_FAILURE_MODE
Default failure mode.static HashAlgorithm
DEFAULT_HASH
Default hash algorithm.static long
DEFAULT_MAX_RECONNECT_DELAY
Maximum amount of time (in seconds) to wait between reconnect attempts.static int
DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD
Maximum number + 2 of timeout exception for shutdown connection.static MetricType
DEFAULT_METRIC_TYPE
Turn off metric collection by default.static int
DEFAULT_OP_QUEUE_LEN
Maximum length of the operation queue returned by this connection factory.static long
DEFAULT_OP_QUEUE_MAX_BLOCK_TIME
The maximum time to block waiting for op queue operations to complete, in milliseconds.static long
DEFAULT_OPERATION_TIMEOUT
Default operation timeout in milliseconds.static int
DEFAULT_READ_BUFFER_SIZE
The read buffer size for each server connection from this factory.private java.util.concurrent.ExecutorService
executorService
The ExecutorService in which the listener callbacks will be executed.private HashAlgorithm
hashAlg
private MetricCollector
metrics
protected int
opQueueLen
private int
readBufSize
-
Constructor Summary
Constructors Constructor Description DefaultConnectionFactory()
Create a DefaultConnectionFactory with the default parameters.DefaultConnectionFactory(int qLen, int bufSize)
Create a DefaultConnectionFactory with the given maximum operation queue length, and the given read buffer size.DefaultConnectionFactory(int qLen, int bufSize, HashAlgorithm hash)
Construct a DefaultConnectionFactory with the given parameters.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MemcachedConnection
createConnection(java.util.List<java.net.InetSocketAddress> addrs)
Create a MemcachedConnection for the given SocketAddresses.NodeLocator
createLocator(java.util.List<MemcachedNode> nodes)
Create a NodeLocator instance for the given list of nodes.MemcachedNode
createMemcachedNode(java.net.SocketAddress sa, java.nio.channels.SocketChannel c, int bufSize)
Create a new memcached node.java.util.concurrent.BlockingQueue<Operation>
createOperationQueue()
Create a BlockingQueue for operations for a connection.java.util.concurrent.BlockingQueue<Operation>
createReadOperationQueue()
Create a BlockingQueue for the operations currently expecting to read responses from memcached.java.util.concurrent.BlockingQueue<Operation>
createWriteOperationQueue()
Create a BlockingQueue for the operations currently expecting to write requests to memcached.MetricType
enableMetrics()
If true, metric collections are enabled.AuthDescriptor
getAuthDescriptor()
Authenticate connections using the given auth descriptor.long
getAuthWaitTime()
The time to wait until authentication completes when an operation is inserted.Transcoder<java.lang.Object>
getDefaultTranscoder()
Get the default transcoder to be used in connections created by this factory.FailureMode
getFailureMode()
Get the default failure mode for the underlying connection.HashAlgorithm
getHashAlg()
Get the hash algorithm to be used.java.util.Collection<ConnectionObserver>
getInitialObservers()
Observers that should be established at the time of connection instantiation.java.util.concurrent.ExecutorService
getListenerExecutorService()
Returns the storedExecutorService
for listeners.long
getMaxReconnectDelay()
Maximum number of milliseconds to wait between reconnect attempts.MetricCollector
getMetricCollector()
The currently activeMetricCollector
.protected java.lang.String
getName()
OperationFactory
getOperationFactory()
Get the operation factory for connections built by this connection factory.long
getOperationTimeout()
Get the operation timeout used by this connection.int
getOpQueueLen()
Get the op queue length set at construct time.long
getOpQueueMaxBlockTime()
Get the maximum amount of time (in milliseconds) a client is willing to wait to add a new item to a queue.int
getReadBufSize()
int
getTimeoutExceptionThreshold()
Maximum number of timeout exception for shutdown connection.boolean
isDaemon()
If true, the IO thread should be a daemon thread.boolean
isDefaultExecutorService()
Returns true if the default providedExecutorService
has not been overriden through the builder.boolean
shouldOptimize()
If true, low-level optimization is in effect.java.lang.String
toString()
boolean
useNagleAlgorithm()
If true, the nagle algorithm will be used on connected sockets.
-
-
-
Field Detail
-
DEFAULT_FAILURE_MODE
public static final FailureMode DEFAULT_FAILURE_MODE
Default failure mode.
-
DEFAULT_HASH
public static final HashAlgorithm DEFAULT_HASH
Default hash algorithm.
-
DEFAULT_OP_QUEUE_LEN
public static final int DEFAULT_OP_QUEUE_LEN
Maximum length of the operation queue returned by this connection factory.- See Also:
- Constant Field Values
-
DEFAULT_OP_QUEUE_MAX_BLOCK_TIME
public static final long DEFAULT_OP_QUEUE_MAX_BLOCK_TIME
The maximum time to block waiting for op queue operations to complete, in milliseconds. The default has been set with the expectation that most requests are interactive and waiting for more than a few seconds is thus more undesirable than failing the request.
-
DEFAULT_READ_BUFFER_SIZE
public static final int DEFAULT_READ_BUFFER_SIZE
The read buffer size for each server connection from this factory.- See Also:
- Constant Field Values
-
DEFAULT_OPERATION_TIMEOUT
public static final long DEFAULT_OPERATION_TIMEOUT
Default operation timeout in milliseconds.- See Also:
- Constant Field Values
-
DEFAULT_MAX_RECONNECT_DELAY
public static final long DEFAULT_MAX_RECONNECT_DELAY
Maximum amount of time (in seconds) to wait between reconnect attempts.- See Also:
- Constant Field Values
-
DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD
public static final int DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD
Maximum number + 2 of timeout exception for shutdown connection.- See Also:
- Constant Field Values
-
DEFAULT_METRIC_TYPE
public static final MetricType DEFAULT_METRIC_TYPE
Turn off metric collection by default.
-
DEFAULT_AUTH_WAIT_TIME
public static final long DEFAULT_AUTH_WAIT_TIME
The time to wait for the authentication to complete when a operation is written in milliseconds.- See Also:
- Constant Field Values
-
opQueueLen
protected final int opQueueLen
-
readBufSize
private final int readBufSize
-
hashAlg
private final HashAlgorithm hashAlg
-
metrics
private MetricCollector metrics
-
executorService
private java.util.concurrent.ExecutorService executorService
The ExecutorService in which the listener callbacks will be executed.
-
-
Constructor Detail
-
DefaultConnectionFactory
public DefaultConnectionFactory(int qLen, int bufSize, HashAlgorithm hash)
Construct a DefaultConnectionFactory with the given parameters.- Parameters:
qLen
- the queue length.bufSize
- the buffer sizehash
- the algorithm to use for hashing
-
DefaultConnectionFactory
public DefaultConnectionFactory(int qLen, int bufSize)
Create a DefaultConnectionFactory with the given maximum operation queue length, and the given read buffer size.
-
DefaultConnectionFactory
public DefaultConnectionFactory()
Create a DefaultConnectionFactory with the default parameters.
-
-
Method Detail
-
createMemcachedNode
public MemcachedNode createMemcachedNode(java.net.SocketAddress sa, java.nio.channels.SocketChannel c, int bufSize)
Description copied from interface:ConnectionFactory
Create a new memcached node.- Specified by:
createMemcachedNode
in interfaceConnectionFactory
-
createConnection
public MemcachedConnection createConnection(java.util.List<java.net.InetSocketAddress> addrs) throws java.io.IOException
Description copied from interface:ConnectionFactory
Create a MemcachedConnection for the given SocketAddresses.- Specified by:
createConnection
in interfaceConnectionFactory
- Parameters:
addrs
- the addresses of the memcached servers- Returns:
- a new MemcachedConnection connected to those addresses
- Throws:
java.io.IOException
- for problems initializing the memcached connections
-
getFailureMode
public FailureMode getFailureMode()
Description copied from interface:ConnectionFactory
Get the default failure mode for the underlying connection.- Specified by:
getFailureMode
in interfaceConnectionFactory
-
createOperationQueue
public java.util.concurrent.BlockingQueue<Operation> createOperationQueue()
Description copied from interface:ConnectionFactory
Create a BlockingQueue for operations for a connection.- Specified by:
createOperationQueue
in interfaceConnectionFactory
-
createReadOperationQueue
public java.util.concurrent.BlockingQueue<Operation> createReadOperationQueue()
Description copied from interface:ConnectionFactory
Create a BlockingQueue for the operations currently expecting to read responses from memcached.- Specified by:
createReadOperationQueue
in interfaceConnectionFactory
-
createWriteOperationQueue
public java.util.concurrent.BlockingQueue<Operation> createWriteOperationQueue()
Description copied from interface:ConnectionFactory
Create a BlockingQueue for the operations currently expecting to write requests to memcached.- Specified by:
createWriteOperationQueue
in interfaceConnectionFactory
-
createLocator
public NodeLocator createLocator(java.util.List<MemcachedNode> nodes)
Description copied from interface:ConnectionFactory
Create a NodeLocator instance for the given list of nodes.- Specified by:
createLocator
in interfaceConnectionFactory
-
getOpQueueLen
public int getOpQueueLen()
Get the op queue length set at construct time.
-
getOpQueueMaxBlockTime
public long getOpQueueMaxBlockTime()
Description copied from interface:ConnectionFactory
Get the maximum amount of time (in milliseconds) a client is willing to wait to add a new item to a queue.- Specified by:
getOpQueueMaxBlockTime
in interfaceConnectionFactory
- Returns:
- the maximum time to block waiting for op queue operations to complete, in milliseconds, or null for no waiting.
-
getAuthWaitTime
public long getAuthWaitTime()
Description copied from interface:ConnectionFactory
The time to wait until authentication completes when an operation is inserted.- Specified by:
getAuthWaitTime
in interfaceConnectionFactory
- Returns:
- the time to wait for the authentication to complete when a operation is written in milliseconds.
-
getListenerExecutorService
public java.util.concurrent.ExecutorService getListenerExecutorService()
Returns the storedExecutorService
for listeners. By default, aThreadPoolExecutor
is used that acts exactly like a default cachedThreadPool, but defines the upper limit of Threads to be created as the number of available processors to prevent resource exhaustion.- Specified by:
getListenerExecutorService
in interfaceConnectionFactory
- Returns:
- the stored
ExecutorService
.
-
isDefaultExecutorService
public boolean isDefaultExecutorService()
Description copied from interface:ConnectionFactory
Returns true if the default providedExecutorService
has not been overriden through the builder.- Specified by:
isDefaultExecutorService
in interfaceConnectionFactory
-
getReadBufSize
public int getReadBufSize()
- Specified by:
getReadBufSize
in interfaceConnectionFactory
-
getHashAlg
public HashAlgorithm getHashAlg()
Description copied from interface:ConnectionFactory
Get the hash algorithm to be used.- Specified by:
getHashAlg
in interfaceConnectionFactory
-
getOperationFactory
public OperationFactory getOperationFactory()
Description copied from interface:ConnectionFactory
Get the operation factory for connections built by this connection factory.- Specified by:
getOperationFactory
in interfaceConnectionFactory
-
getOperationTimeout
public long getOperationTimeout()
Description copied from interface:ConnectionFactory
Get the operation timeout used by this connection.- Specified by:
getOperationTimeout
in interfaceConnectionFactory
-
isDaemon
public boolean isDaemon()
Description copied from interface:ConnectionFactory
If true, the IO thread should be a daemon thread.- Specified by:
isDaemon
in interfaceConnectionFactory
-
getInitialObservers
public java.util.Collection<ConnectionObserver> getInitialObservers()
Description copied from interface:ConnectionFactory
Observers that should be established at the time of connection instantiation. These observers will see the first connection established.- Specified by:
getInitialObservers
in interfaceConnectionFactory
-
getDefaultTranscoder
public Transcoder<java.lang.Object> getDefaultTranscoder()
Description copied from interface:ConnectionFactory
Get the default transcoder to be used in connections created by this factory.- Specified by:
getDefaultTranscoder
in interfaceConnectionFactory
-
useNagleAlgorithm
public boolean useNagleAlgorithm()
Description copied from interface:ConnectionFactory
If true, the nagle algorithm will be used on connected sockets.See
Socket.setTcpNoDelay(boolean)
for more information.- Specified by:
useNagleAlgorithm
in interfaceConnectionFactory
-
shouldOptimize
public boolean shouldOptimize()
Description copied from interface:ConnectionFactory
If true, low-level optimization is in effect.- Specified by:
shouldOptimize
in interfaceConnectionFactory
-
getMaxReconnectDelay
public long getMaxReconnectDelay()
Description copied from interface:ConnectionFactory
Maximum number of milliseconds to wait between reconnect attempts.- Specified by:
getMaxReconnectDelay
in interfaceConnectionFactory
-
getAuthDescriptor
public AuthDescriptor getAuthDescriptor()
Description copied from interface:ConnectionFactory
Authenticate connections using the given auth descriptor.- Specified by:
getAuthDescriptor
in interfaceConnectionFactory
- Returns:
- null if no authentication should take place
-
getTimeoutExceptionThreshold
public int getTimeoutExceptionThreshold()
Description copied from interface:ConnectionFactory
Maximum number of timeout exception for shutdown connection.- Specified by:
getTimeoutExceptionThreshold
in interfaceConnectionFactory
-
enableMetrics
public MetricType enableMetrics()
Description copied from interface:ConnectionFactory
If true, metric collections are enabled.- Specified by:
enableMetrics
in interfaceConnectionFactory
-
getMetricCollector
public MetricCollector getMetricCollector()
Description copied from interface:ConnectionFactory
The currently activeMetricCollector
.- Specified by:
getMetricCollector
in interfaceConnectionFactory
-
getName
protected java.lang.String getName()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-