Package net.spy.memcached
Class MemcachedConnection
java.lang.Object
java.lang.Thread
net.spy.memcached.compat.SpyThread
net.spy.memcached.MemcachedConnection
- All Implemented Interfaces:
Runnable,ClusterConfigurationObserver,ConfigurationObserver
Connection to a cluster of memcached servers.
MemcachedConnection also acts as an observer for cluster configuration changes.
In the mode ClientMode.Dynamic, the ConfigurationPoller notifies the observers when there is
change in cluster configuration.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ConcurrentLinkedQueue<MemcachedNode> AddedQueue is used to track the QueueAttachments for which operations have recently been queued.protected final ReentrantLockprotected final FailureModeThe configuredFailureMode.protected booleanprotected NodeLocatorTheNodeLocatorto use for this connection.protected final ReentrantLockprotected final MetricCollectorTheMetricCollectorto accumulate metrics (or dummy).protected final MetricTypeThe current type of metrics to collect.protected final List<NodeEndPoint> protected List<NodeEndPoint> protected List<MemcachedNode> protected final ConcurrentLinkedQueue<MemcachedNode> Holds all nodes that are scheduled for shutdown.protected final Conditionprotected booleanTrue if not shutting down or shut down.protected SelectorHolds the currentSelectorto use.protected booleanIf the connection is alread shut down or shutting down.Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
ConstructorsConstructorDescriptionMemcachedConnection(int bufSize, ConnectionFactory f, List<InetSocketAddress> socketAddressList, Collection<ConnectionObserver> obs, FailureMode fm, OperationFactory opfactory) Construct aMemcachedConnection. -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdd a connection observer.protected voidaddOperation(String key, Operation o) Add an operation to a connection identified by the given key.protected voidaddOperation(InetSocketAddress addr, Operation o) protected voidaddOperation(MemcachedNode node, Operation o) Enqueue an operation on the given node.voidEnqueue the given list of operations on each handling node.Broadcast an operation to all nodes.broadcastOperation(BroadcastOpFactory of, Collection<MemcachedNode> nodes) Broadcast an operation to a collection of nodes.protected voidCheck to see if this connection is shutting down.Construct a String containing information about all nodes and their state.protected MemcachedNodecreateConnection(NodeEndPoint endPoint) protected List<MemcachedNode> createConnections(Collection<NodeEndPoint> endPoints) voidenqueueOperation(String key, Operation o) Enqueue the givenOperationwith the used key.voidenqueueOperation(InetSocketAddress addr, Operation o) Returns theNodeLocatorin use for this connection.voidhandleIO()Handle all IO that flows through the connection.protected voidhandleRetryInformation(byte[] retryMessage) Optionally handle retry (NOT_MY_VBUKET) responses.protected voidHelper method which gets called if the selector is woken up because of the timeout setting, if has been interrupted or if happens during regular write operation phases.voidinsertOperation(MemcachedNode node, Operation o) Insert an operation on the given node to the beginning of the queue.booleanReturns whether the connection is shut down or not.voidnotifyUpdate(ClusterConfiguration clusterConfiguration) The publisher calls all the subscribers through this method.static voidopSucceeded(Operation op) Reset the timeout counter for the given handling node.static voidopTimedOut(Operation op) Increase the timeout counter for the given handling node.protected voidqueueReconnect(MemcachedNode node) Enqueue the givenMemcachedNodefor reconnect.voidRedistribute the given operation to (potentially) other nodes.voidRedistribute the given list of operations to (potentially) other nodes.protected voidRegister Metrics for collection.booleanRemove a connection observer.voidAdd a operation to the retry queue.voidrun()Handle IO as long as the application is running.voidshutdown()Shut down all connections and do not accept further incoming ops.toString()voidWait for the configuration change applied.voidMethods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, start, startVirtualThread, stop, suspend, threadId, yield
-
Field Details
-
shutDown
protected volatile boolean shutDownIf the connection is alread shut down or shutting down. -
selector
Holds the currentSelectorto use. -
locator
TheNodeLocatorto use for this connection. -
failureMode
The configuredFailureMode. -
addedQueue
AddedQueue is used to track the QueueAttachments for which operations have recently been queued. -
running
protected volatile boolean runningTrue if not shutting down or shut down. -
nodesToAdd
-
nodesToDelete
-
lockForNodeUpdates
-
conditionLock
-
nodeUpdateCondition
-
isInitialClusterConfigApplied
protected boolean isInitialClusterConfigApplied -
nodesToShutdown
Holds all nodes that are scheduled for shutdown. -
newEndPoints
-
metrics
TheMetricCollectorto accumulate metrics (or dummy). -
metricType
The current type of metrics to collect.
-
-
Constructor Details
-
MemcachedConnection
public MemcachedConnection(int bufSize, ConnectionFactory f, List<InetSocketAddress> socketAddressList, Collection<ConnectionObserver> obs, FailureMode fm, OperationFactory opfactory) throws IOException Construct aMemcachedConnection.- Parameters:
bufSize- the size of the buffer used for reading from the serverf- the factory that will provide an operation queuesocketAddressList- the addresses of the servers to connect toobs- the initial observers to add.fm- the failure mode to use.opfactory- the operation factory.- Throws:
IOException- if a connection attempt fails early
-
-
Method Details
-
waitForInitialConfigApplied
public void waitForInitialConfigApplied() -
waitForConfigChangeApplied
public void waitForConfigChangeApplied()Description copied from interface:ClusterConfigurationObserverWait for the configuration change applied. This is invoked whenever the observers get notified except configuration change during the client initialization.- Specified by:
waitForConfigChangeAppliedin interfaceClusterConfigurationObserver
-
notifyUpdate
Description copied from interface:ClusterConfigurationObserverThe publisher calls all the subscribers through this method. This is invoked whenever there is change in cluster configuration data.- Specified by:
notifyUpdatein interfaceClusterConfigurationObserver- Parameters:
clusterConfiguration- - The parameter contains the latest information about the cluster.
-
registerMetrics
protected void registerMetrics()Register Metrics for collection. Note that these Metrics may or may not take effect, depending on theMetricCollectorimplementation. This can be controlled from theDefaultConnectionFactory. -
createConnection
- Throws:
IOException
-
createConnections
protected List<MemcachedNode> createConnections(Collection<NodeEndPoint> endPoints) throws IOException - Throws:
IOException
-
handleIO
Handle all IO that flows through the connection. This method is called in an endless loop, listens on NIO selectors and dispatches the underlying read/write calls if needed.- Throws:
IOException
-
handleWokenUpSelector
protected void handleWokenUpSelector()Helper method which gets called if the selector is woken up because of the timeout setting, if has been interrupted or if happens during regular write operation phases.This method can be overriden by child implementations to handle custom behavior on a manually woken selector, like sending pings through the channels to make sure they are alive.
Note that there is no guarantee that this method is at all or in the regular interval called, so all overriding implementations need to take that into account. Also, it needs to take into account that it may be called very often under heavy workloads, so it should not perform extensive tasks in the same thread.
-
addObserver
Add a connection observer.- Returns:
- whether the observer was successfully added.
-
removeObserver
Remove a connection observer.- Returns:
- true if the observer existed and now doesn't.
-
handleRetryInformation
protected void handleRetryInformation(byte[] retryMessage) Optionally handle retry (NOT_MY_VBUKET) responses. This method can be overridden in subclasses to handle the content of the retry message appropriately.- Parameters:
retryMessage- the body of the retry message.
-
queueReconnect
Enqueue the givenMemcachedNodefor reconnect.- Parameters:
node- the node to reconnect.
-
redistributeOperations
Redistribute the given list of operations to (potentially) other nodes. Note that operations can only be redistributed if they have not been cancelled already, timed out already or do not have definite targets (a key).- Parameters:
ops- the operations to redistribute.
-
redistributeOperation
Redistribute the given operation to (potentially) other nodes. Note that operations can only be redistributed if they have not been cancelled already, timed out already or do not have definite targets (a key).- Parameters:
op- the operation to redistribute.
-
getLocator
Returns theNodeLocatorin use for this connection.- Returns:
- the current
NodeLocator.
-
enqueueOperation
Enqueue the givenOperationwith the used key.- Parameters:
key- the key to use.o- theOperationto enqueue.
-
enqueueOperation
-
addOperation
Add an operation to a connection identified by the given key. If theMemcachedNodeis active or theFailureModeis set to retry, the primary node will be used for that key. If the primary node is not available and theFailureModecancel is used, the operation will be cancelled without further retry. For any otherFailureModemechanisms (Redistribute), another possible node is used (only if its active as well). If no other active node could be identified, the original primary node is used and retried.- Parameters:
key- the key the operation is operating upon.o- the operation to add.
-
addOperation
-
insertOperation
Insert an operation on the given node to the beginning of the queue.- Parameters:
node- the node where to insert theOperation.o- the operation to insert.
-
addOperation
Enqueue an operation on the given node.- Parameters:
node- the node where to enqueue theOperation.o- the operation to add.
-
addOperations
Enqueue the given list of operations on each handling node.- Parameters:
ops- the operations for each node.
-
broadcastOperation
Broadcast an operation to all nodes.- Returns:
- a
CountDownLatchthat will be counted down when the operations are complete.
-
broadcastOperation
Broadcast an operation to a collection of nodes.- Returns:
- a
CountDownLatchthat will be counted down when the operations are complete.
-
shutdown
Shut down all connections and do not accept further incoming ops.- Throws:
IOException
-
toString
-
connectionsStatus
Construct a String containing information about all nodes and their state.- Returns:
- a stringified representation of the connection status.
-
opTimedOut
Increase the timeout counter for the given handling node.- Parameters:
op- the operation to grab the node from.
-
opSucceeded
Reset the timeout counter for the given handling node.- Parameters:
op- the operation to grab the node from.
-
checkState
protected void checkState()Check to see if this connection is shutting down.- Throws:
IllegalStateException- when shutting down.
-
run
public void run()Handle IO as long as the application is running. -
isShutDown
public boolean isShutDown()Returns whether the connection is shut down or not.- Returns:
- true if the connection is shut down, false otherwise.
-
retryOperation
Add a operation to the retry queue. If the retry queue size is bounded and the size of the queue is reaching that boundary, the operation is cancelled rather than added to the retry queue.- Parameters:
op- the operation to retry.
-