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
RedistributeFailureMode is always used. If other FailureModes are needed, look at the ConnectionFactoryBuilder.
-
-
Field Summary
Fields Modifier and Type Field Description static longDEFAULT_AUTH_WAIT_TIMEThe time to wait for the authentication to complete when a operation is written in milliseconds.static FailureModeDEFAULT_FAILURE_MODEDefault failure mode.static HashAlgorithmDEFAULT_HASHDefault hash algorithm.static longDEFAULT_MAX_RECONNECT_DELAYMaximum amount of time (in seconds) to wait between reconnect attempts.static intDEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLDMaximum number + 2 of timeout exception for shutdown connection.static MetricTypeDEFAULT_METRIC_TYPETurn off metric collection by default.static intDEFAULT_OP_QUEUE_LENMaximum length of the operation queue returned by this connection factory.static longDEFAULT_OP_QUEUE_MAX_BLOCK_TIMEThe maximum time to block waiting for op queue operations to complete, in milliseconds.static longDEFAULT_OPERATION_TIMEOUTDefault operation timeout in milliseconds.static intDEFAULT_READ_BUFFER_SIZEThe read buffer size for each server connection from this factory.private java.util.concurrent.ExecutorServiceexecutorServiceThe ExecutorService in which the listener callbacks will be executed.private HashAlgorithmhashAlgprivate MetricCollectormetricsprotected intopQueueLenprivate intreadBufSize
-
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 MemcachedConnectioncreateConnection(java.util.List<java.net.InetSocketAddress> addrs)Create a MemcachedConnection for the given SocketAddresses.NodeLocatorcreateLocator(java.util.List<MemcachedNode> nodes)Create a NodeLocator instance for the given list of nodes.MemcachedNodecreateMemcachedNode(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.MetricTypeenableMetrics()If true, metric collections are enabled.AuthDescriptorgetAuthDescriptor()Authenticate connections using the given auth descriptor.longgetAuthWaitTime()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.FailureModegetFailureMode()Get the default failure mode for the underlying connection.HashAlgorithmgetHashAlg()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.ExecutorServicegetListenerExecutorService()Returns the storedExecutorServicefor listeners.longgetMaxReconnectDelay()Maximum number of milliseconds to wait between reconnect attempts.MetricCollectorgetMetricCollector()The currently activeMetricCollector.protected java.lang.StringgetName()OperationFactorygetOperationFactory()Get the operation factory for connections built by this connection factory.longgetOperationTimeout()Get the operation timeout used by this connection.intgetOpQueueLen()Get the op queue length set at construct time.longgetOpQueueMaxBlockTime()Get the maximum amount of time (in milliseconds) a client is willing to wait to add a new item to a queue.intgetReadBufSize()intgetTimeoutExceptionThreshold()Maximum number of timeout exception for shutdown connection.booleanisDaemon()If true, the IO thread should be a daemon thread.booleanisDefaultExecutorService()Returns true if the default providedExecutorServicehas not been overriden through the builder.booleanshouldOptimize()If true, low-level optimization is in effect.java.lang.StringtoString()booleanuseNagleAlgorithm()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:ConnectionFactoryCreate a new memcached node.- Specified by:
createMemcachedNodein interfaceConnectionFactory
-
createConnection
public MemcachedConnection createConnection(java.util.List<java.net.InetSocketAddress> addrs) throws java.io.IOException
Description copied from interface:ConnectionFactoryCreate a MemcachedConnection for the given SocketAddresses.- Specified by:
createConnectionin 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:ConnectionFactoryGet the default failure mode for the underlying connection.- Specified by:
getFailureModein interfaceConnectionFactory
-
createOperationQueue
public java.util.concurrent.BlockingQueue<Operation> createOperationQueue()
Description copied from interface:ConnectionFactoryCreate a BlockingQueue for operations for a connection.- Specified by:
createOperationQueuein interfaceConnectionFactory
-
createReadOperationQueue
public java.util.concurrent.BlockingQueue<Operation> createReadOperationQueue()
Description copied from interface:ConnectionFactoryCreate a BlockingQueue for the operations currently expecting to read responses from memcached.- Specified by:
createReadOperationQueuein interfaceConnectionFactory
-
createWriteOperationQueue
public java.util.concurrent.BlockingQueue<Operation> createWriteOperationQueue()
Description copied from interface:ConnectionFactoryCreate a BlockingQueue for the operations currently expecting to write requests to memcached.- Specified by:
createWriteOperationQueuein interfaceConnectionFactory
-
createLocator
public NodeLocator createLocator(java.util.List<MemcachedNode> nodes)
Description copied from interface:ConnectionFactoryCreate a NodeLocator instance for the given list of nodes.- Specified by:
createLocatorin interfaceConnectionFactory
-
getOpQueueLen
public int getOpQueueLen()
Get the op queue length set at construct time.
-
getOpQueueMaxBlockTime
public long getOpQueueMaxBlockTime()
Description copied from interface:ConnectionFactoryGet the maximum amount of time (in milliseconds) a client is willing to wait to add a new item to a queue.- Specified by:
getOpQueueMaxBlockTimein 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:ConnectionFactoryThe time to wait until authentication completes when an operation is inserted.- Specified by:
getAuthWaitTimein 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 storedExecutorServicefor listeners. By default, aThreadPoolExecutoris 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:
getListenerExecutorServicein interfaceConnectionFactory- Returns:
- the stored
ExecutorService.
-
isDefaultExecutorService
public boolean isDefaultExecutorService()
Description copied from interface:ConnectionFactoryReturns true if the default providedExecutorServicehas not been overriden through the builder.- Specified by:
isDefaultExecutorServicein interfaceConnectionFactory
-
getReadBufSize
public int getReadBufSize()
- Specified by:
getReadBufSizein interfaceConnectionFactory
-
getHashAlg
public HashAlgorithm getHashAlg()
Description copied from interface:ConnectionFactoryGet the hash algorithm to be used.- Specified by:
getHashAlgin interfaceConnectionFactory
-
getOperationFactory
public OperationFactory getOperationFactory()
Description copied from interface:ConnectionFactoryGet the operation factory for connections built by this connection factory.- Specified by:
getOperationFactoryin interfaceConnectionFactory
-
getOperationTimeout
public long getOperationTimeout()
Description copied from interface:ConnectionFactoryGet the operation timeout used by this connection.- Specified by:
getOperationTimeoutin interfaceConnectionFactory
-
isDaemon
public boolean isDaemon()
Description copied from interface:ConnectionFactoryIf true, the IO thread should be a daemon thread.- Specified by:
isDaemonin interfaceConnectionFactory
-
getInitialObservers
public java.util.Collection<ConnectionObserver> getInitialObservers()
Description copied from interface:ConnectionFactoryObservers that should be established at the time of connection instantiation. These observers will see the first connection established.- Specified by:
getInitialObserversin interfaceConnectionFactory
-
getDefaultTranscoder
public Transcoder<java.lang.Object> getDefaultTranscoder()
Description copied from interface:ConnectionFactoryGet the default transcoder to be used in connections created by this factory.- Specified by:
getDefaultTranscoderin interfaceConnectionFactory
-
useNagleAlgorithm
public boolean useNagleAlgorithm()
Description copied from interface:ConnectionFactoryIf true, the nagle algorithm will be used on connected sockets.See
Socket.setTcpNoDelay(boolean)for more information.- Specified by:
useNagleAlgorithmin interfaceConnectionFactory
-
shouldOptimize
public boolean shouldOptimize()
Description copied from interface:ConnectionFactoryIf true, low-level optimization is in effect.- Specified by:
shouldOptimizein interfaceConnectionFactory
-
getMaxReconnectDelay
public long getMaxReconnectDelay()
Description copied from interface:ConnectionFactoryMaximum number of milliseconds to wait between reconnect attempts.- Specified by:
getMaxReconnectDelayin interfaceConnectionFactory
-
getAuthDescriptor
public AuthDescriptor getAuthDescriptor()
Description copied from interface:ConnectionFactoryAuthenticate connections using the given auth descriptor.- Specified by:
getAuthDescriptorin interfaceConnectionFactory- Returns:
- null if no authentication should take place
-
getTimeoutExceptionThreshold
public int getTimeoutExceptionThreshold()
Description copied from interface:ConnectionFactoryMaximum number of timeout exception for shutdown connection.- Specified by:
getTimeoutExceptionThresholdin interfaceConnectionFactory
-
enableMetrics
public MetricType enableMetrics()
Description copied from interface:ConnectionFactoryIf true, metric collections are enabled.- Specified by:
enableMetricsin interfaceConnectionFactory
-
getMetricCollector
public MetricCollector getMetricCollector()
Description copied from interface:ConnectionFactoryThe currently activeMetricCollector.- Specified by:
getMetricCollectorin interfaceConnectionFactory
-
getName
protected java.lang.String getName()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-