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
Represents a node with the memcached cluster, along with buffering and
operation queues.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate CountDownLatch
private final long
private SocketChannel
private MemcachedConnection
private final ConnectionFactory
private final AtomicInteger
private long
private final BlockingQueue
<Operation> private long
private final long
protected Operation
private final ByteBuffer
private final BlockingQueue
<Operation> private AtomicInteger
private boolean
private SelectionKey
private final SocketAddress
private int
private final ByteBuffer
protected final BlockingQueue
<Operation> -
Constructor Summary
ConstructorsConstructorDescriptionTCPMemcachedNodeImpl
(SocketAddress sa, SocketChannel c, int bufSize, BlockingQueue<Operation> rq, BlockingQueue<Operation> wq, BlockingQueue<Operation> iq, long opQueueMaxBlockTime, boolean waitForAuth, long dt, long authWaitTime, ConnectionFactory fact) -
Method Summary
Modifier and TypeMethodDescriptionfinal void
Add an operation to the queue.final void
Let the node know that auth is complete.void
Mark this node as having just completed a read.final void
Notify this node that it has reconnected.final void
Move all of the operations delivered via addOperation into the internal write queue.Extract all queued items for this node destructively.final void
fillWriteBuffer
(boolean shouldOptimize) Fill the write buffer with data from the next operations in the queue.final void
fixupOps()
Fix up the selection ops on the selection key.final int
Get the number of bytes remaining to write.final SocketChannel
Get the SocketChannel for this connection.int
final Operation
Get the operation at the top of the queue that is requiring input.final Operation
Get the operation at the top of the queue that has information available to write.private Operation
final ByteBuffer
getRbuf()
Get the buffer used for reading data from this node.final int
Get the current reconnect count.final int
Compute the appropriate selection operations for the channel this MemcachedNode holds to the server.final SelectionKey
getSk()
Get the selection key from this node.final SocketAddress
Get the SocketAddress of the server to which this node is connected.final ByteBuffer
getWbuf()
Get the buffer used for writing data to this node.final boolean
True if an operation is available to read.final boolean
True if an operation is available to write.final void
Insert an operation to the beginning of the queue.final boolean
isActive()
True if this node isactive.
i.e.boolean
True if this node isauthenticated.
long
Number of milliseconds since the last read of this node completed.protected abstract void
optimize()
private boolean
final void
Notify this node that it will be reconnecting.final void
registerChannel
(SocketChannel ch, SelectionKey skey) Register a channel with this node.final Operation
Remove the operation at the top of the queue that is requiring input.final Operation
Remove the operation at the top of the queue that has information available to write.final void
Set the SocketChannel this node uses.void
setConnection
(MemcachedConnection connection) void
setContinuousTimeout
(boolean timedOut) Count 'time out' exceptions to drop connections that fail perpetually.final void
setSk
(SelectionKey to) Set the selection key for this node.final void
Tell a node to set up for authentication.final void
Clear the queue of currently processing operations by either cancelling them or setting them up to be reapplied after a reconnect.final String
toString()
final void
Transition the current write item into a read state.final int
Write some bytes and return the number of bytes written.
-
Field Details
-
socketAddress
-
rbuf
-
wbuf
-
writeQ
-
readQ
-
inputQueue
-
opQueueMaxBlockTime
private final long opQueueMaxBlockTime -
authWaitTime
private final long authWaitTime -
connectionFactory
-
reconnectAttempt
-
channel
-
toWrite
private int toWrite -
optimizedOp
-
sk
-
shouldAuth
private boolean shouldAuth -
authLatch
-
reconnectBlocked
-
defaultOpTimeout
private long defaultOpTimeout -
lastReadTimestamp
private volatile long lastReadTimestamp -
connection
-
continuousTimeout
-
-
Constructor Details
-
TCPMemcachedNodeImpl
public TCPMemcachedNodeImpl(SocketAddress sa, SocketChannel c, int bufSize, BlockingQueue<Operation> rq, BlockingQueue<Operation> wq, BlockingQueue<Operation> iq, long opQueueMaxBlockTime, boolean waitForAuth, long dt, long authWaitTime, ConnectionFactory fact)
-
-
Method Details
-
copyInputQueue
public final void copyInputQueue()Description copied from interface:MemcachedNode
Move all of the operations delivered via addOperation into the internal write queue.- Specified by:
copyInputQueue
in interfaceMemcachedNode
-
destroyInputQueue
Description copied from interface:MemcachedNode
Extract all queued items for this node destructively. This is useful for redistributing items.- Specified by:
destroyInputQueue
in interfaceMemcachedNode
-
setupResend
public final void setupResend()Description copied from interface:MemcachedNode
Clear the queue of currently processing operations by either cancelling them or setting them up to be reapplied after a reconnect.- Specified by:
setupResend
in interfaceMemcachedNode
-
preparePending
private boolean preparePending() -
fillWriteBuffer
public final void fillWriteBuffer(boolean shouldOptimize) Description copied from interface:MemcachedNode
Fill the write buffer with data from the next operations in the queue.- Specified by:
fillWriteBuffer
in interfaceMemcachedNode
- Parameters:
shouldOptimize
- if true, combine sequential gets into a single multi-key get
-
getNextWritableOp
-
transitionWriteItem
public final void transitionWriteItem()Description copied from interface:MemcachedNode
Transition the current write item into a read state.- Specified by:
transitionWriteItem
in interfaceMemcachedNode
-
optimize
protected abstract void optimize() -
getCurrentReadOp
Description copied from interface:MemcachedNode
Get the operation at the top of the queue that is requiring input.- Specified by:
getCurrentReadOp
in interfaceMemcachedNode
-
removeCurrentReadOp
Description copied from interface:MemcachedNode
Remove the operation at the top of the queue that is requiring input.- Specified by:
removeCurrentReadOp
in interfaceMemcachedNode
-
getCurrentWriteOp
Description copied from interface:MemcachedNode
Get the operation at the top of the queue that has information available to write.- Specified by:
getCurrentWriteOp
in interfaceMemcachedNode
-
removeCurrentWriteOp
Description copied from interface:MemcachedNode
Remove the operation at the top of the queue that has information available to write.- Specified by:
removeCurrentWriteOp
in interfaceMemcachedNode
-
hasReadOp
public final boolean hasReadOp()Description copied from interface:MemcachedNode
True if an operation is available to read.- Specified by:
hasReadOp
in interfaceMemcachedNode
-
hasWriteOp
public final boolean hasWriteOp()Description copied from interface:MemcachedNode
True if an operation is available to write.- Specified by:
hasWriteOp
in interfaceMemcachedNode
-
addOp
Description copied from interface:MemcachedNode
Add an operation to the queue. Authentication operations should never be added to the queue, but this is not checked.- Specified by:
addOp
in interfaceMemcachedNode
-
insertOp
Description copied from interface:MemcachedNode
Insert an operation to the beginning of the queue. This method is meant to be invoked rarely.- Specified by:
insertOp
in interfaceMemcachedNode
-
getSelectionOps
public final int getSelectionOps()Description copied from interface:MemcachedNode
Compute the appropriate selection operations for the channel this MemcachedNode holds to the server.- Specified by:
getSelectionOps
in interfaceMemcachedNode
-
getRbuf
Description copied from interface:MemcachedNode
Get the buffer used for reading data from this node.- Specified by:
getRbuf
in interfaceMemcachedNode
-
getWbuf
Description copied from interface:MemcachedNode
Get the buffer used for writing data to this node.- Specified by:
getWbuf
in interfaceMemcachedNode
-
getSocketAddress
Description copied from interface:MemcachedNode
Get the SocketAddress of the server to which this node is connected.- Specified by:
getSocketAddress
in interfaceMemcachedNode
-
isActive
public final boolean isActive()Description copied from interface:MemcachedNode
True if this node isactive.
i.e. is is currently connected and expected to be able to process requests- Specified by:
isActive
in interfaceMemcachedNode
-
isAuthenticated
public boolean isAuthenticated()Description copied from interface:MemcachedNode
True if this node isauthenticated.
- Specified by:
isAuthenticated
in interfaceMemcachedNode
-
reconnecting
public final void reconnecting()Description copied from interface:MemcachedNode
Notify this node that it will be reconnecting.- Specified by:
reconnecting
in interfaceMemcachedNode
-
connected
public final void connected()Description copied from interface:MemcachedNode
Notify this node that it has reconnected.- Specified by:
connected
in interfaceMemcachedNode
-
getReconnectCount
public final int getReconnectCount()Description copied from interface:MemcachedNode
Get the current reconnect count.- Specified by:
getReconnectCount
in interfaceMemcachedNode
-
toString
-
registerChannel
Description copied from interface:MemcachedNode
Register a channel with this node.- Specified by:
registerChannel
in interfaceMemcachedNode
-
setChannel
Description copied from interface:MemcachedNode
Set the SocketChannel this node uses.- Specified by:
setChannel
in interfaceMemcachedNode
-
getChannel
Description copied from interface:MemcachedNode
Get the SocketChannel for this connection.- Specified by:
getChannel
in interfaceMemcachedNode
-
setSk
Description copied from interface:MemcachedNode
Set the selection key for this node.- Specified by:
setSk
in interfaceMemcachedNode
-
getSk
Description copied from interface:MemcachedNode
Get the selection key from this node.- Specified by:
getSk
in interfaceMemcachedNode
-
getBytesRemainingToWrite
public final int getBytesRemainingToWrite()Description copied from interface:MemcachedNode
Get the number of bytes remaining to write.- Specified by:
getBytesRemainingToWrite
in interfaceMemcachedNode
-
writeSome
Description copied from interface:MemcachedNode
Write some bytes and return the number of bytes written.- Specified by:
writeSome
in interfaceMemcachedNode
- Returns:
- the number of bytes written
- Throws:
IOException
- if there's a problem writing
-
setContinuousTimeout
public void setContinuousTimeout(boolean timedOut) Description copied from interface:MemcachedNode
Count 'time out' exceptions to drop connections that fail perpetually.- Specified by:
setContinuousTimeout
in interfaceMemcachedNode
- Parameters:
timedOut
-
-
getContinuousTimeout
public int getContinuousTimeout()- Specified by:
getContinuousTimeout
in interfaceMemcachedNode
-
fixupOps
public final void fixupOps()Description copied from interface:MemcachedNode
Fix up the selection ops on the selection key.- Specified by:
fixupOps
in interfaceMemcachedNode
-
authComplete
public final void authComplete()Description copied from interface:MemcachedNode
Let 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:
authComplete
in interfaceMemcachedNode
-
setupForAuth
public final void setupForAuth()Description copied from interface:MemcachedNode
Tell 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:
setupForAuth
in interfaceMemcachedNode
-
lastReadDelta
public long lastReadDelta()Number of milliseconds since the last read of this node completed.- Specified by:
lastReadDelta
in interfaceMemcachedNode
- Returns:
- milliseconds since last read.
-
completedRead
public void completedRead()Mark this node as having just completed a read.- Specified by:
completedRead
in interfaceMemcachedNode
-
getConnection
- Specified by:
getConnection
in interfaceMemcachedNode
-
setConnection
- Specified by:
setConnection
in interfaceMemcachedNode
-