Class MemcachedConnection

All Implemented Interfaces:
Runnable, ClusterConfigurationObserver, ConfigurationObserver

public class MemcachedConnection extends SpyThread implements ClusterConfigurationObserver
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.
  • Field Details

    • shutDown

      protected volatile boolean shutDown
      If the connection is alread shut down or shutting down.
    • selector

      protected Selector selector
      Holds the current Selector to use.
    • locator

      protected NodeLocator locator
      The NodeLocator to use for this connection.
    • failureMode

      protected final FailureMode failureMode
      The configured FailureMode.
    • addedQueue

      protected final ConcurrentLinkedQueue<MemcachedNode> addedQueue
      AddedQueue is used to track the QueueAttachments for which operations have recently been queued.
    • running

      protected volatile boolean running
      True if not shutting down or shut down.
    • nodesToAdd

      protected List<NodeEndPoint> nodesToAdd
    • nodesToDelete

      protected List<MemcachedNode> nodesToDelete
    • lockForNodeUpdates

      protected final ReentrantLock lockForNodeUpdates
    • conditionLock

      protected final ReentrantLock conditionLock
    • nodeUpdateCondition

      protected final Condition nodeUpdateCondition
    • isInitialClusterConfigApplied

      protected boolean isInitialClusterConfigApplied
    • nodesToShutdown

      protected final ConcurrentLinkedQueue<MemcachedNode> nodesToShutdown
      Holds all nodes that are scheduled for shutdown.
    • newEndPoints

      protected final List<NodeEndPoint> newEndPoints
    • metrics

      protected final MetricCollector metrics
      The MetricCollector to accumulate metrics (or dummy).
    • metricType

      protected final MetricType metricType
      The current type of metrics to collect.
  • Constructor Details

  • Method Details

    • waitForInitialConfigApplied

      public void waitForInitialConfigApplied()
    • waitForConfigChangeApplied

      public void waitForConfigChangeApplied()
      Description copied from interface: ClusterConfigurationObserver
      Wait for the configuration change applied. This is invoked whenever the observers get notified except configuration change during the client initialization.
      Specified by:
      waitForConfigChangeApplied in interface ClusterConfigurationObserver
    • notifyUpdate

      public void notifyUpdate(ClusterConfiguration clusterConfiguration)
      Description copied from interface: ClusterConfigurationObserver
      The publisher calls all the subscribers through this method. This is invoked whenever there is change in cluster configuration data.
      Specified by:
      notifyUpdate in interface ClusterConfigurationObserver
      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 the MetricCollector implementation. This can be controlled from the DefaultConnectionFactory.
    • createConnection

      protected MemcachedNode createConnection(NodeEndPoint endPoint) throws IOException
      Throws:
      IOException
    • createConnections

      protected List<MemcachedNode> createConnections(Collection<NodeEndPoint> endPoints) throws IOException
      Throws:
      IOException
    • handleIO

      public void handleIO() throws IOException
      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

      public boolean addObserver(ConnectionObserver obs)
      Add a connection observer.
      Returns:
      whether the observer was successfully added.
    • removeObserver

      public boolean removeObserver(ConnectionObserver obs)
      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

      protected void queueReconnect(MemcachedNode node)
      Enqueue the given MemcachedNode for reconnect.
      Parameters:
      node - the node to reconnect.
    • redistributeOperations

      public void redistributeOperations(Collection<Operation> ops)
      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

      public void redistributeOperation(Operation op)
      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

      public NodeLocator getLocator()
      Returns the NodeLocator in use for this connection.
      Returns:
      the current NodeLocator.
    • enqueueOperation

      public void enqueueOperation(String key, Operation o)
      Enqueue the given Operation with the used key.
      Parameters:
      key - the key to use.
      o - the Operation to enqueue.
    • enqueueOperation

      public void enqueueOperation(InetSocketAddress addr, Operation o)
    • addOperation

      protected void addOperation(String key, Operation o)
      Add an operation to a connection identified by the given key. If the 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.
      Parameters:
      key - the key the operation is operating upon.
      o - the operation to add.
    • addOperation

      protected void addOperation(InetSocketAddress addr, Operation o)
    • insertOperation

      public void insertOperation(MemcachedNode node, Operation o)
      Insert an operation on the given node to the beginning of the queue.
      Parameters:
      node - the node where to insert the Operation.
      o - the operation to insert.
    • addOperation

      protected void addOperation(MemcachedNode node, Operation o)
      Enqueue an operation on the given node.
      Parameters:
      node - the node where to enqueue the Operation.
      o - the operation to add.
    • addOperations

      public void addOperations(Map<MemcachedNode,Operation> ops)
      Enqueue the given list of operations on each handling node.
      Parameters:
      ops - the operations for each node.
    • broadcastOperation

      public CountDownLatch broadcastOperation(BroadcastOpFactory of)
      Broadcast an operation to all nodes.
      Returns:
      a CountDownLatch that will be counted down when the operations are complete.
    • broadcastOperation

      public CountDownLatch broadcastOperation(BroadcastOpFactory of, Collection<MemcachedNode> nodes)
      Broadcast an operation to a collection of nodes.
      Returns:
      a CountDownLatch that will be counted down when the operations are complete.
    • shutdown

      public void shutdown() throws IOException
      Shut down all connections and do not accept further incoming ops.
      Throws:
      IOException
    • toString

      public String toString()
      Overrides:
      toString in class Thread
    • connectionsStatus

      public String connectionsStatus()
      Construct a String containing information about all nodes and their state.
      Returns:
      a stringified representation of the connection status.
    • opTimedOut

      public static void opTimedOut(Operation op)
      Increase the timeout counter for the given handling node.
      Parameters:
      op - the operation to grab the node from.
    • opSucceeded

      public static void opSucceeded(Operation op)
      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.
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • isShutDown

      public boolean isShutDown()
      Returns whether the connection is shut down or not.
      Returns:
      true if the connection is shut down, false otherwise.
    • retryOperation

      public void retryOperation(Operation op)
      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.