public class MemcachedConnection extends SpyThread implements ClusterConfigurationObserver
| Modifier and Type | Field and Description |
|---|---|
protected java.util.concurrent.ConcurrentLinkedQueue<MemcachedNode> |
addedQueue
AddedQueue is used to track the QueueAttachments for which operations
have recently been queued.
|
protected java.util.concurrent.locks.ReentrantLock |
conditionLock |
protected FailureMode |
failureMode
The configured
FailureMode. |
protected boolean |
isInitialClusterConfigApplied |
protected NodeLocator |
locator
The
NodeLocator to use for this connection. |
protected java.util.concurrent.locks.ReentrantLock |
lockForNodeUpdates |
protected MetricCollector |
metrics
The
MetricCollector to accumulate metrics (or dummy). |
protected MetricType |
metricType
The current type of metrics to collect.
|
protected java.util.List<NodeEndPoint> |
newEndPoints |
protected java.util.List<NodeEndPoint> |
nodesToAdd |
protected java.util.List<MemcachedNode> |
nodesToDelete |
protected java.util.concurrent.ConcurrentLinkedQueue<MemcachedNode> |
nodesToShutdown
Holds all nodes that are scheduled for shutdown.
|
protected java.util.concurrent.locks.Condition |
nodeUpdateCondition |
protected boolean |
running
True if not shutting down or shut down.
|
protected java.nio.channels.Selector |
selector
Holds the current
Selector to use. |
protected boolean |
shutDown
If the connection is alread shut down or shutting down.
|
| Constructor and Description |
|---|
MemcachedConnection(int bufSize,
ConnectionFactory f,
java.util.List<java.net.InetSocketAddress> socketAddressList,
java.util.Collection<ConnectionObserver> obs,
FailureMode fm,
OperationFactory opfactory)
Construct a
MemcachedConnection. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
addObserver(ConnectionObserver obs)
Add a connection observer.
|
protected void |
addOperation(java.net.InetSocketAddress addr,
Operation o) |
protected void |
addOperation(MemcachedNode node,
Operation o)
Enqueue an operation on the given node.
|
protected void |
addOperation(java.lang.String key,
Operation o)
Add an operation to a connection identified by the given key.
|
void |
addOperations(java.util.Map<MemcachedNode,Operation> ops)
Enqueue the given list of operations on each handling node.
|
java.util.concurrent.CountDownLatch |
broadcastOperation(BroadcastOpFactory of)
Broadcast an operation to all nodes.
|
java.util.concurrent.CountDownLatch |
broadcastOperation(BroadcastOpFactory of,
java.util.Collection<MemcachedNode> nodes)
Broadcast an operation to a collection of nodes.
|
protected void |
checkState()
Check to see if this connection is shutting down.
|
java.lang.String |
connectionsStatus()
Construct a String containing information about all nodes and their state.
|
protected MemcachedNode |
createConnection(NodeEndPoint endPoint) |
protected java.util.List<MemcachedNode> |
createConnections(java.util.Collection<NodeEndPoint> endPoints) |
void |
enqueueOperation(java.net.InetSocketAddress addr,
Operation o) |
void |
enqueueOperation(java.lang.String key,
Operation o)
Enqueue the given
Operation with the used key. |
NodeLocator |
getLocator()
Returns the
NodeLocator in use for this connection. |
void |
handleIO()
Handle all IO that flows through the connection.
|
protected void |
handleRetryInformation(byte[] retryMessage)
Optionally handle retry (NOT_MY_VBUKET) responses.
|
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.
|
void |
insertOperation(MemcachedNode node,
Operation o)
Insert an operation on the given node to the beginning of the queue.
|
boolean |
isShutDown()
Returns whether the connection is shut down or not.
|
void |
notifyUpdate(ClusterConfiguration clusterConfiguration)
The publisher calls all the subscribers through this method.
|
static void |
opSucceeded(Operation op)
Reset the timeout counter for the given handling node.
|
static void |
opTimedOut(Operation op)
Increase the timeout counter for the given handling node.
|
protected void |
queueReconnect(MemcachedNode node)
Enqueue the given
MemcachedNode for reconnect. |
void |
redistributeOperation(Operation op)
Redistribute the given operation to (potentially) other nodes.
|
void |
redistributeOperations(java.util.Collection<Operation> ops)
Redistribute the given list of operations to (potentially) other nodes.
|
protected void |
registerMetrics()
Register Metrics for collection.
|
boolean |
removeObserver(ConnectionObserver obs)
Remove a connection observer.
|
void |
retryOperation(Operation op)
Add a operation to the retry queue.
|
void |
run()
Handle IO as long as the application is running.
|
void |
shutdown()
Shut down all connections and do not accept further incoming ops.
|
java.lang.String |
toString() |
void |
waitForConfigChangeApplied()
Wait for the configuration change applied.
|
void |
waitForInitialConfigApplied() |
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, yieldprotected volatile boolean shutDown
protected java.nio.channels.Selector selector
Selector to use.protected NodeLocator locator
NodeLocator to use for this connection.protected final FailureMode failureMode
FailureMode.protected final java.util.concurrent.ConcurrentLinkedQueue<MemcachedNode> addedQueue
protected volatile boolean running
protected java.util.List<NodeEndPoint> nodesToAdd
protected java.util.List<MemcachedNode> nodesToDelete
protected final java.util.concurrent.locks.ReentrantLock lockForNodeUpdates
protected final java.util.concurrent.locks.ReentrantLock conditionLock
protected final java.util.concurrent.locks.Condition nodeUpdateCondition
protected boolean isInitialClusterConfigApplied
protected final java.util.concurrent.ConcurrentLinkedQueue<MemcachedNode> nodesToShutdown
protected final java.util.List<NodeEndPoint> newEndPoints
protected final MetricCollector metrics
MetricCollector to accumulate metrics (or dummy).protected final MetricType metricType
public MemcachedConnection(int bufSize,
ConnectionFactory f,
java.util.List<java.net.InetSocketAddress> socketAddressList,
java.util.Collection<ConnectionObserver> obs,
FailureMode fm,
OperationFactory opfactory)
throws java.io.IOException
MemcachedConnection.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.java.io.IOException - if a connection attempt fails earlypublic void waitForInitialConfigApplied()
public void waitForConfigChangeApplied()
ClusterConfigurationObserverwaitForConfigChangeApplied in interface ClusterConfigurationObserverpublic void notifyUpdate(ClusterConfiguration clusterConfiguration)
ClusterConfigurationObservernotifyUpdate in interface ClusterConfigurationObserverclusterConfiguration - - The parameter contains the latest information about the cluster.protected void registerMetrics()
MetricCollector implementation. This can be controlled from
the DefaultConnectionFactory.protected MemcachedNode createConnection(NodeEndPoint endPoint) throws java.io.IOException
java.io.IOExceptionprotected java.util.List<MemcachedNode> createConnections(java.util.Collection<NodeEndPoint> endPoints) throws java.io.IOException
java.io.IOExceptionpublic void handleIO()
throws java.io.IOException
java.io.IOExceptionprotected void handleWokenUpSelector()
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.
public boolean addObserver(ConnectionObserver obs)
public boolean removeObserver(ConnectionObserver obs)
protected void handleRetryInformation(byte[] retryMessage)
retryMessage - the body of the retry message.protected void queueReconnect(MemcachedNode node)
MemcachedNode for reconnect.node - the node to reconnect.public void redistributeOperations(java.util.Collection<Operation> ops)
ops - the operations to redistribute.public void redistributeOperation(Operation op)
op - the operation to redistribute.public NodeLocator getLocator()
NodeLocator in use for this connection.NodeLocator.public void enqueueOperation(java.lang.String key,
Operation o)
Operation with the used key.key - the key to use.o - the Operation to enqueue.public void enqueueOperation(java.net.InetSocketAddress addr,
Operation o)
protected void addOperation(java.lang.String key,
Operation o)
MemcachedNode is active or the FailureMode is set
to retry, the primary node will be used for that key. If the primary
node is not available and the FailureMode cancel is used, the
operation will be cancelled without further retry.
For any other FailureMode mechanisms (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.key - the key the operation is operating upon.o - the operation to add.protected void addOperation(java.net.InetSocketAddress addr,
Operation o)
public void insertOperation(MemcachedNode node, Operation o)
node - the node where to insert the Operation.o - the operation to insert.protected void addOperation(MemcachedNode node, Operation o)
node - the node where to enqueue the Operation.o - the operation to add.public void addOperations(java.util.Map<MemcachedNode,Operation> ops)
ops - the operations for each node.public java.util.concurrent.CountDownLatch broadcastOperation(BroadcastOpFactory of)
CountDownLatch that will be counted down when the
operations are complete.public java.util.concurrent.CountDownLatch broadcastOperation(BroadcastOpFactory of, java.util.Collection<MemcachedNode> nodes)
CountDownLatch that will be counted down when the
operations are complete.public void shutdown()
throws java.io.IOException
java.io.IOExceptionpublic java.lang.String toString()
toString in class java.lang.Threadpublic java.lang.String connectionsStatus()
public static void opTimedOut(Operation op)
op - the operation to grab the node from.public static void opSucceeded(Operation op)
op - the operation to grab the node from.protected void checkState()
java.lang.IllegalStateException - when shutting down.public void run()
run in interface java.lang.Runnablerun in class java.lang.Threadpublic boolean isShutDown()
public void retryOperation(Operation op)
op - the operation to retry.Copyright © 2006-2009 Dustin Sallings, 2009-2013 Couchbase, Inc.