Package net.spy.memcached.protocol
Class TCPMemcachedNodeImpl
- java.lang.Object
-
- net.spy.memcached.compat.SpyObject
-
- net.spy.memcached.protocol.TCPMemcachedNodeImpl
-
- All Implemented Interfaces:
MemcachedNode
- Direct Known Subclasses:
AsciiMemcachedNodeImpl,BinaryMemcachedNodeImpl
public abstract class TCPMemcachedNodeImpl extends SpyObject implements MemcachedNode
Represents a node with the memcached cluster, along with buffering and operation queues.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.CountDownLatchauthLatchprivate longauthWaitTimeprivate java.nio.channels.SocketChannelchannelprivate MemcachedConnectionconnectionprivate ConnectionFactoryconnectionFactoryprivate java.util.concurrent.atomic.AtomicIntegercontinuousTimeoutprivate longdefaultOpTimeoutprivate java.util.concurrent.BlockingQueue<Operation>inputQueueprivate longlastReadTimestampprivate longopQueueMaxBlockTimeprotected OperationoptimizedOpprivate java.nio.ByteBufferrbufprivate java.util.concurrent.BlockingQueue<Operation>readQprivate java.util.concurrent.atomic.AtomicIntegerreconnectAttemptprivate java.util.ArrayList<Operation>reconnectBlockedprivate booleanshouldAuthprivate java.nio.channels.SelectionKeyskprivate java.net.SocketAddresssocketAddressprivate inttoWriteprivate java.nio.ByteBufferwbufprotected java.util.concurrent.BlockingQueue<Operation>writeQ
-
Constructor Summary
Constructors Constructor Description TCPMemcachedNodeImpl(java.net.SocketAddress sa, java.nio.channels.SocketChannel c, int bufSize, java.util.concurrent.BlockingQueue<Operation> rq, java.util.concurrent.BlockingQueue<Operation> wq, java.util.concurrent.BlockingQueue<Operation> iq, long opQueueMaxBlockTime, boolean waitForAuth, long dt, long authWaitTime, ConnectionFactory fact)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddOp(Operation op)Add an operation to the queue.voidauthComplete()Let the node know that auth is complete.voidcompletedRead()Mark this node as having just completed a read.voidconnected()Notify this node that it has reconnected.voidcopyInputQueue()Move all of the operations delivered via addOperation into the internal write queue.java.util.Collection<Operation>destroyInputQueue()Extract all queued items for this node destructively.voidfillWriteBuffer(boolean shouldOptimize)Fill the write buffer with data from the next operations in the queue.voidfixupOps()Fix up the selection ops on the selection key.intgetBytesRemainingToWrite()Get the number of bytes remaining to write.java.nio.channels.SocketChannelgetChannel()Get the SocketChannel for this connection.MemcachedConnectiongetConnection()intgetContinuousTimeout()OperationgetCurrentReadOp()Get the operation at the top of the queue that is requiring input.OperationgetCurrentWriteOp()Get the operation at the top of the queue that has information available to write.private OperationgetNextWritableOp()java.nio.ByteBuffergetRbuf()Get the buffer used for reading data from this node.intgetReconnectCount()Get the current reconnect count.intgetSelectionOps()Compute the appropriate selection operations for the channel this MemcachedNode holds to the server.java.nio.channels.SelectionKeygetSk()Get the selection key from this node.java.net.SocketAddressgetSocketAddress()Get the SocketAddress of the server to which this node is connected.java.nio.ByteBuffergetWbuf()Get the buffer used for writing data to this node.booleanhasReadOp()True if an operation is available to read.booleanhasWriteOp()True if an operation is available to write.voidinsertOp(Operation op)Insert an operation to the beginning of the queue.booleanisActive()True if this node isactive.
i.e.booleanisAuthenticated()True if this node isauthenticated.
longlastReadDelta()Number of milliseconds since the last read of this node completed.protected abstract voidoptimize()private booleanpreparePending()voidreconnecting()Notify this node that it will be reconnecting.voidregisterChannel(java.nio.channels.SocketChannel ch, java.nio.channels.SelectionKey skey)Register a channel with this node.OperationremoveCurrentReadOp()Remove the operation at the top of the queue that is requiring input.OperationremoveCurrentWriteOp()Remove the operation at the top of the queue that has information available to write.voidsetChannel(java.nio.channels.SocketChannel to)Set the SocketChannel this node uses.voidsetConnection(MemcachedConnection connection)voidsetContinuousTimeout(boolean timedOut)Count 'time out' exceptions to drop connections that fail perpetually.voidsetSk(java.nio.channels.SelectionKey to)Set the selection key for this node.voidsetupForAuth()Tell a node to set up for authentication.voidsetupResend()Clear the queue of currently processing operations by either cancelling them or setting them up to be reapplied after a reconnect.java.lang.StringtoString()voidtransitionWriteItem()Transition the current write item into a read state.intwriteSome()Write some bytes and return the number of bytes written.
-
-
-
Field Detail
-
socketAddress
private final java.net.SocketAddress socketAddress
-
rbuf
private final java.nio.ByteBuffer rbuf
-
wbuf
private final java.nio.ByteBuffer wbuf
-
writeQ
protected final java.util.concurrent.BlockingQueue<Operation> writeQ
-
readQ
private final java.util.concurrent.BlockingQueue<Operation> readQ
-
inputQueue
private final java.util.concurrent.BlockingQueue<Operation> inputQueue
-
opQueueMaxBlockTime
private final long opQueueMaxBlockTime
-
authWaitTime
private final long authWaitTime
-
connectionFactory
private final ConnectionFactory connectionFactory
-
reconnectAttempt
private java.util.concurrent.atomic.AtomicInteger reconnectAttempt
-
channel
private java.nio.channels.SocketChannel channel
-
toWrite
private int toWrite
-
optimizedOp
protected Operation optimizedOp
-
sk
private volatile java.nio.channels.SelectionKey sk
-
shouldAuth
private boolean shouldAuth
-
authLatch
private java.util.concurrent.CountDownLatch authLatch
-
reconnectBlocked
private java.util.ArrayList<Operation> reconnectBlocked
-
defaultOpTimeout
private long defaultOpTimeout
-
lastReadTimestamp
private volatile long lastReadTimestamp
-
connection
private MemcachedConnection connection
-
continuousTimeout
private final java.util.concurrent.atomic.AtomicInteger continuousTimeout
-
-
Constructor Detail
-
TCPMemcachedNodeImpl
public TCPMemcachedNodeImpl(java.net.SocketAddress sa, java.nio.channels.SocketChannel c, int bufSize, java.util.concurrent.BlockingQueue<Operation> rq, java.util.concurrent.BlockingQueue<Operation> wq, java.util.concurrent.BlockingQueue<Operation> iq, long opQueueMaxBlockTime, boolean waitForAuth, long dt, long authWaitTime, ConnectionFactory fact)
-
-
Method Detail
-
copyInputQueue
public final void copyInputQueue()
Description copied from interface:MemcachedNodeMove all of the operations delivered via addOperation into the internal write queue.- Specified by:
copyInputQueuein interfaceMemcachedNode
-
destroyInputQueue
public java.util.Collection<Operation> destroyInputQueue()
Description copied from interface:MemcachedNodeExtract all queued items for this node destructively. This is useful for redistributing items.- Specified by:
destroyInputQueuein interfaceMemcachedNode
-
setupResend
public final void setupResend()
Description copied from interface:MemcachedNodeClear the queue of currently processing operations by either cancelling them or setting them up to be reapplied after a reconnect.- Specified by:
setupResendin interfaceMemcachedNode
-
preparePending
private boolean preparePending()
-
fillWriteBuffer
public final void fillWriteBuffer(boolean shouldOptimize)
Description copied from interface:MemcachedNodeFill the write buffer with data from the next operations in the queue.- Specified by:
fillWriteBufferin interfaceMemcachedNode- Parameters:
shouldOptimize- if true, combine sequential gets into a single multi-key get
-
getNextWritableOp
private Operation getNextWritableOp()
-
transitionWriteItem
public final void transitionWriteItem()
Description copied from interface:MemcachedNodeTransition the current write item into a read state.- Specified by:
transitionWriteItemin interfaceMemcachedNode
-
optimize
protected abstract void optimize()
-
getCurrentReadOp
public final Operation getCurrentReadOp()
Description copied from interface:MemcachedNodeGet the operation at the top of the queue that is requiring input.- Specified by:
getCurrentReadOpin interfaceMemcachedNode
-
removeCurrentReadOp
public final Operation removeCurrentReadOp()
Description copied from interface:MemcachedNodeRemove the operation at the top of the queue that is requiring input.- Specified by:
removeCurrentReadOpin interfaceMemcachedNode
-
getCurrentWriteOp
public final Operation getCurrentWriteOp()
Description copied from interface:MemcachedNodeGet the operation at the top of the queue that has information available to write.- Specified by:
getCurrentWriteOpin interfaceMemcachedNode
-
removeCurrentWriteOp
public final Operation removeCurrentWriteOp()
Description copied from interface:MemcachedNodeRemove the operation at the top of the queue that has information available to write.- Specified by:
removeCurrentWriteOpin interfaceMemcachedNode
-
hasReadOp
public final boolean hasReadOp()
Description copied from interface:MemcachedNodeTrue if an operation is available to read.- Specified by:
hasReadOpin interfaceMemcachedNode
-
hasWriteOp
public final boolean hasWriteOp()
Description copied from interface:MemcachedNodeTrue if an operation is available to write.- Specified by:
hasWriteOpin interfaceMemcachedNode
-
addOp
public final void addOp(Operation op)
Description copied from interface:MemcachedNodeAdd an operation to the queue. Authentication operations should never be added to the queue, but this is not checked.- Specified by:
addOpin interfaceMemcachedNode
-
insertOp
public final void insertOp(Operation op)
Description copied from interface:MemcachedNodeInsert an operation to the beginning of the queue. This method is meant to be invoked rarely.- Specified by:
insertOpin interfaceMemcachedNode
-
getSelectionOps
public final int getSelectionOps()
Description copied from interface:MemcachedNodeCompute the appropriate selection operations for the channel this MemcachedNode holds to the server.- Specified by:
getSelectionOpsin interfaceMemcachedNode
-
getRbuf
public final java.nio.ByteBuffer getRbuf()
Description copied from interface:MemcachedNodeGet the buffer used for reading data from this node.- Specified by:
getRbufin interfaceMemcachedNode
-
getWbuf
public final java.nio.ByteBuffer getWbuf()
Description copied from interface:MemcachedNodeGet the buffer used for writing data to this node.- Specified by:
getWbufin interfaceMemcachedNode
-
getSocketAddress
public final java.net.SocketAddress getSocketAddress()
Description copied from interface:MemcachedNodeGet the SocketAddress of the server to which this node is connected.- Specified by:
getSocketAddressin interfaceMemcachedNode
-
isActive
public final boolean isActive()
Description copied from interface:MemcachedNodeTrue if this node isactive.
i.e. is is currently connected and expected to be able to process requests- Specified by:
isActivein interfaceMemcachedNode
-
isAuthenticated
public boolean isAuthenticated()
Description copied from interface:MemcachedNodeTrue if this node isauthenticated.
- Specified by:
isAuthenticatedin interfaceMemcachedNode
-
reconnecting
public final void reconnecting()
Description copied from interface:MemcachedNodeNotify this node that it will be reconnecting.- Specified by:
reconnectingin interfaceMemcachedNode
-
connected
public final void connected()
Description copied from interface:MemcachedNodeNotify this node that it has reconnected.- Specified by:
connectedin interfaceMemcachedNode
-
getReconnectCount
public final int getReconnectCount()
Description copied from interface:MemcachedNodeGet the current reconnect count.- Specified by:
getReconnectCountin interfaceMemcachedNode
-
toString
public final java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
registerChannel
public final void registerChannel(java.nio.channels.SocketChannel ch, java.nio.channels.SelectionKey skey)Description copied from interface:MemcachedNodeRegister a channel with this node.- Specified by:
registerChannelin interfaceMemcachedNode
-
setChannel
public final void setChannel(java.nio.channels.SocketChannel to)
Description copied from interface:MemcachedNodeSet the SocketChannel this node uses.- Specified by:
setChannelin interfaceMemcachedNode
-
getChannel
public final java.nio.channels.SocketChannel getChannel()
Description copied from interface:MemcachedNodeGet the SocketChannel for this connection.- Specified by:
getChannelin interfaceMemcachedNode
-
setSk
public final void setSk(java.nio.channels.SelectionKey to)
Description copied from interface:MemcachedNodeSet the selection key for this node.- Specified by:
setSkin interfaceMemcachedNode
-
getSk
public final java.nio.channels.SelectionKey getSk()
Description copied from interface:MemcachedNodeGet the selection key from this node.- Specified by:
getSkin interfaceMemcachedNode
-
getBytesRemainingToWrite
public final int getBytesRemainingToWrite()
Description copied from interface:MemcachedNodeGet the number of bytes remaining to write.- Specified by:
getBytesRemainingToWritein interfaceMemcachedNode
-
writeSome
public final int writeSome() throws java.io.IOExceptionDescription copied from interface:MemcachedNodeWrite some bytes and return the number of bytes written.- Specified by:
writeSomein interfaceMemcachedNode- Returns:
- the number of bytes written
- Throws:
java.io.IOException- if there's a problem writing
-
setContinuousTimeout
public void setContinuousTimeout(boolean timedOut)
Description copied from interface:MemcachedNodeCount 'time out' exceptions to drop connections that fail perpetually.- Specified by:
setContinuousTimeoutin interfaceMemcachedNode
-
getContinuousTimeout
public int getContinuousTimeout()
- Specified by:
getContinuousTimeoutin interfaceMemcachedNode
-
fixupOps
public final void fixupOps()
Description copied from interface:MemcachedNodeFix up the selection ops on the selection key.- Specified by:
fixupOpsin interfaceMemcachedNode
-
authComplete
public final void authComplete()
Description copied from interface:MemcachedNodeLet the node know that auth is complete. Typically this would mean the node can start processing and accept new operations to its input queue.- Specified by:
authCompletein interfaceMemcachedNode
-
setupForAuth
public final void setupForAuth()
Description copied from interface:MemcachedNodeTell a node to set up for authentication. Typically this would mean blocking additions to the queue. In a reconnect situation this may mean putting any queued operations on hold to get to an auth complete state.- Specified by:
setupForAuthin interfaceMemcachedNode
-
lastReadDelta
public long lastReadDelta()
Number of milliseconds since the last read of this node completed.- Specified by:
lastReadDeltain interfaceMemcachedNode- Returns:
- milliseconds since last read.
-
completedRead
public void completedRead()
Mark this node as having just completed a read.- Specified by:
completedReadin interfaceMemcachedNode
-
getConnection
public MemcachedConnection getConnection()
- Specified by:
getConnectionin interfaceMemcachedNode
-
setConnection
public void setConnection(MemcachedConnection connection)
- Specified by:
setConnectionin interfaceMemcachedNode
-
-