Package net.rubyeye.xmemcached.impl
Class MemcachedConnector
- java.lang.Object
-
- com.google.code.yanf4j.core.impl.AbstractController
-
- com.google.code.yanf4j.nio.impl.NioController
-
- com.google.code.yanf4j.nio.impl.SocketChannelController
-
- net.rubyeye.xmemcached.impl.MemcachedConnector
-
- All Implemented Interfaces:
Controller
,ControllerLifeCycle
,SelectionKeyHandler
,Connector
public class MemcachedConnector extends SocketChannelController implements Connector
Connected session manager
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) class
MemcachedConnector.InnerControllerStateListener
Inner state listenner,manage session monitor.(package private) class
MemcachedConnector.SessionMonitor
Session monitor for healing sessions.
-
Field Summary
Fields Modifier and Type Field Description private BufferAllocator
bufferAllocator
private CommandFactory
commandFactory
private int
connectionPoolSize
private boolean
enableHealSession
private boolean
failureMode
private FlowControl
flowControl
private long
healSessionInterval
private MemcachedOptimizer
optimiezer
protected Protocol
protocol
private java.util.Random
random
private java.util.Set<java.net.InetSocketAddress>
removedAddrSet
protected MemcachedSessionComparator
sessionComparator
protected MemcachedSessionLocator
sessionLocator
protected java.util.concurrent.ConcurrentHashMap<java.net.InetSocketAddress,java.util.Queue<Session>>
sessionMap
private MemcachedConnector.SessionMonitor
sessionMonitor
private boolean
shuttingDown
private java.util.concurrent.ConcurrentHashMap<java.net.InetSocketAddress,java.util.List<Session>>
standbySessionMap
private java.util.concurrent.DelayQueue<ReconnectRequest>
waitingQueue
-
Fields inherited from class com.google.code.yanf4j.nio.impl.SocketChannelController
soLingerOn
-
Fields inherited from class com.google.code.yanf4j.nio.impl.NioController
selectorManager, selectorPoolSize
-
Fields inherited from class com.google.code.yanf4j.core.impl.AbstractController
codecFactory, configuration, dispatchMessageDispatcher, dispatchMessageThreadCount, handler, handleReadWriteConcurrently, localSocketAddress, log, readEventDispatcher, readThreadCount, sessionSet, sessionTimeout, socketOptions, soTimeout, started, stateListeners, statistics, statisticsInterval, writeEventDispatcher, writeThreadCount
-
-
Constructor Summary
Constructors Constructor Description MemcachedConnector(Configuration configuration, MemcachedSessionLocator locator, MemcachedSessionComparator comparator, BufferAllocator allocator, CommandFactory commandFactory, int poolSize, int maxQueuedNoReplyOperations)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addMainSession(Session session, java.net.InetSocketAddress lastReolvedAddr, InetSocketAddressWrapper addrWrapper)
void
addSession(Session session)
private void
addStandbySession(Session session, java.net.InetSocketAddress mainNodeAddress, java.net.InetSocketAddress lastResolvedMainAddr, InetSocketAddressWrapper addrWrapper)
void
addToWatingQueue(ReconnectRequest request)
protected java.util.Queue<WriteMessage>
buildQueue()
Build write queue for sessionprotected NioSession
buildSession(java.nio.channels.SocketChannel sc)
private void
cancelKey(java.nio.channels.SelectionKey key)
void
closeChannel(java.nio.channels.Selector selector)
java.util.concurrent.Future<java.lang.Boolean>
connect(InetSocketAddressWrapper addressWrapper)
protected MemcachedTCPSession
createSession(java.nio.channels.SocketChannel socketChannel, InetSocketAddressWrapper wrapper)
protected void
doStart()
Inner startupSession
findSessionByKey(java.lang.String key)
private MemcachedSession
findStandbySession(MemcachedSession session)
BufferAllocator
getBufferAllocator()
long
getHealSessionInterval()
FlowControl
getNoReplyOpsFlowControl()
Returns the noreply operations flow control manager.Protocol
getProtocol()
java.util.Queue<ReconnectRequest>
getReconnectRequestQueue()
java.util.Collection<java.net.InetSocketAddress>
getServerAddresses()
java.util.Queue<Session>
getSessionByAddress(java.net.InetSocketAddress addr)
Get session by InetSocketAddressjava.util.List<Session>
getSessionListBySocketAddress(java.net.InetSocketAddress inetSocketAddress)
java.util.Set<Session>
getSessionSet()
java.util.List<Session>
getStandbySessionListByMainNodeAddr(java.net.InetSocketAddress addr)
Returns main node's standby session list.void
onConnect(java.nio.channels.SelectionKey key)
void
quitAllSessions()
Make all connection sending a quit command to memcachedprivate void
removeMainSession(Session session)
void
removeReconnectRequest(java.net.InetSocketAddress inetSocketAddress)
void
removeSession(Session session)
private void
removeStandbySession(Session session, java.net.InetSocketAddress mainNodeAddr)
Session
send(Command msg)
void
setBufferAllocator(BufferAllocator allocator)
void
setConnectionPoolSize(int poolSize)
void
setEnableHealSession(boolean enableHealSession)
void
setFailureMode(boolean failureMode)
void
setHealSessionInterval(long healConnectionInterval)
void
setMergeFactor(int mergeFactor)
void
setOptimizeGet(boolean optimiezeGet)
void
setOptimizeMergeBuffer(boolean optimizeMergeBuffer)
void
setSessionComparator(MemcachedSessionComparator sessionComparator)
void
setSessionLocator(MemcachedSessionLocator sessionLocator)
void
shuttingDown()
void
updateSessions()
-
Methods inherited from class com.google.code.yanf4j.nio.impl.SocketChannelController
configureSocketChannel, dispatchReadEvent, dispatchWriteEvent, setSoLinger
-
Methods inherited from class com.google.code.yanf4j.nio.impl.NioController
bind, buildSessionConfig, closeSelectionKey, getSelectorManager, getSelectorPoolSize, initialSelectorManager, onRead, onWrite, setSelectorPoolSize, start0, stop0
-
Methods inherited from class com.google.code.yanf4j.core.impl.AbstractController
addStateListener, bind, checkStatisticsForRestart, getCodecFactory, getConfiguration, getDispatchMessageThreadCount, getHandler, getLocalSocketAddress, getPort, getReadThreadCount, getReceiveThroughputLimit, getSendThroughputLimit, getSessionIdleTimeout, getSessionTimeout, getSocketOption, getSoTimeout, getStatistics, getWriteThreadCount, isHandleReadWriteConcurrently, isStarted, notifyAllSessionClosed, notifyException, notifyReady, notifyStarted, notifyStopped, onAccept, registerSession, removeStateListener, setCodecFactory, setConfiguration, setDispatchMessageThreadCount, setHandler, setHandleReadWriteConcurrently, setLocalSocketAddress, setReadThreadCount, setReceiveThroughputLimit, setSendThroughputLimit, setSessionIdleTimeout, setSessionTimeout, setSocketOption, setSocketOptions, setSoTimeout, setWriteThreadCount, start, stop, unregisterSession
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface net.rubyeye.xmemcached.networking.Connector
setSocketOptions
-
Methods inherited from interface com.google.code.yanf4j.core.Controller
addStateListener, getCodecFactory, getDispatchMessageThreadCount, getHandler, getLocalSocketAddress, getPort, getReadThreadCount, getReceiveThroughputLimit, getSendThroughputLimit, getSessionIdleTimeout, getSessionTimeout, getSoTimeout, getStatistics, getWriteThreadCount, isHandleReadWriteConcurrently, isStarted, removeStateListener, setCodecFactory, setDispatchMessageThreadCount, setHandler, setHandleReadWriteConcurrently, setLocalSocketAddress, setReadThreadCount, setReceiveThroughputLimit, setSendThroughputLimit, setSessionIdleTimeout, setSessionTimeout, setSocketOption, setSoTimeout, setWriteThreadCount, start, stop
-
Methods inherited from interface com.google.code.yanf4j.nio.SelectionKeyHandler
onAccept
-
-
-
-
Field Detail
-
waitingQueue
private final java.util.concurrent.DelayQueue<ReconnectRequest> waitingQueue
-
bufferAllocator
private BufferAllocator bufferAllocator
-
removedAddrSet
private final java.util.Set<java.net.InetSocketAddress> removedAddrSet
-
optimiezer
private final MemcachedOptimizer optimiezer
-
healSessionInterval
private long healSessionInterval
-
connectionPoolSize
private int connectionPoolSize
-
protocol
protected Protocol protocol
-
enableHealSession
private boolean enableHealSession
-
commandFactory
private final CommandFactory commandFactory
-
failureMode
private boolean failureMode
-
standbySessionMap
private final java.util.concurrent.ConcurrentHashMap<java.net.InetSocketAddress,java.util.List<Session>> standbySessionMap
-
flowControl
private final FlowControl flowControl
-
shuttingDown
private volatile boolean shuttingDown
-
sessionLocator
protected MemcachedSessionLocator sessionLocator
-
sessionComparator
protected MemcachedSessionComparator sessionComparator
-
sessionMap
protected final java.util.concurrent.ConcurrentHashMap<java.net.InetSocketAddress,java.util.Queue<Session>> sessionMap
-
random
private final java.util.Random random
-
sessionMonitor
private final MemcachedConnector.SessionMonitor sessionMonitor
-
-
Constructor Detail
-
MemcachedConnector
public MemcachedConnector(Configuration configuration, MemcachedSessionLocator locator, MemcachedSessionComparator comparator, BufferAllocator allocator, CommandFactory commandFactory, int poolSize, int maxQueuedNoReplyOperations)
-
-
Method Detail
-
shuttingDown
public void shuttingDown()
-
setSessionLocator
public void setSessionLocator(MemcachedSessionLocator sessionLocator)
- Specified by:
setSessionLocator
in interfaceConnector
-
setSessionComparator
public void setSessionComparator(MemcachedSessionComparator sessionComparator)
- Specified by:
setSessionComparator
in interfaceConnector
-
setEnableHealSession
public void setEnableHealSession(boolean enableHealSession)
- Specified by:
setEnableHealSession
in interfaceConnector
-
getReconnectRequestQueue
public java.util.Queue<ReconnectRequest> getReconnectRequestQueue()
- Specified by:
getReconnectRequestQueue
in interfaceConnector
-
getSessionSet
public java.util.Set<Session> getSessionSet()
- Specified by:
getSessionSet
in interfaceConnector
- Overrides:
getSessionSet
in classAbstractController
-
setHealSessionInterval
public final void setHealSessionInterval(long healConnectionInterval)
- Specified by:
setHealSessionInterval
in interfaceConnector
-
getHealSessionInterval
public long getHealSessionInterval()
- Specified by:
getHealSessionInterval
in interfaceConnector
-
setOptimizeGet
public void setOptimizeGet(boolean optimiezeGet)
- Specified by:
setOptimizeGet
in interfaceConnector
-
setOptimizeMergeBuffer
public void setOptimizeMergeBuffer(boolean optimizeMergeBuffer)
- Specified by:
setOptimizeMergeBuffer
in interfaceConnector
-
getProtocol
public Protocol getProtocol()
-
addSession
public void addSession(Session session)
-
addMainSession
private void addMainSession(Session session, java.net.InetSocketAddress lastReolvedAddr, InetSocketAddressWrapper addrWrapper)
-
addStandbySession
private void addStandbySession(Session session, java.net.InetSocketAddress mainNodeAddress, java.net.InetSocketAddress lastResolvedMainAddr, InetSocketAddressWrapper addrWrapper)
-
getSessionListBySocketAddress
public java.util.List<Session> getSessionListBySocketAddress(java.net.InetSocketAddress inetSocketAddress)
-
removeReconnectRequest
public void removeReconnectRequest(java.net.InetSocketAddress inetSocketAddress)
- Specified by:
removeReconnectRequest
in interfaceConnector
-
updateSessions
public final void updateSessions()
- Specified by:
updateSessions
in interfaceConnector
-
removeSession
public void removeSession(Session session)
- Specified by:
removeSession
in interfaceConnector
-
removeMainSession
private void removeMainSession(Session session)
-
removeStandbySession
private void removeStandbySession(Session session, java.net.InetSocketAddress mainNodeAddr)
-
doStart
protected void doStart() throws java.io.IOException
Description copied from class:NioController
Inner startup- Specified by:
doStart
in classNioController
- Throws:
java.io.IOException
-
onConnect
public void onConnect(java.nio.channels.SelectionKey key) throws java.io.IOException
- Specified by:
onConnect
in interfaceSelectionKeyHandler
- Overrides:
onConnect
in classAbstractController
- Throws:
java.io.IOException
-
cancelKey
private void cancelKey(java.nio.channels.SelectionKey key) throws java.io.IOException
- Throws:
java.io.IOException
-
createSession
protected MemcachedTCPSession createSession(java.nio.channels.SocketChannel socketChannel, InetSocketAddressWrapper wrapper)
-
addToWatingQueue
public void addToWatingQueue(ReconnectRequest request)
- Specified by:
addToWatingQueue
in interfaceConnector
-
connect
public java.util.concurrent.Future<java.lang.Boolean> connect(InetSocketAddressWrapper addressWrapper)
-
closeChannel
public void closeChannel(java.nio.channels.Selector selector) throws java.io.IOException
- Specified by:
closeChannel
in interfaceSelectionKeyHandler
- Throws:
java.io.IOException
-
send
public Session send(Command msg) throws MemcachedException
- Specified by:
send
in interfaceConnector
- Throws:
MemcachedException
-
findStandbySession
private MemcachedSession findStandbySession(MemcachedSession session)
-
getStandbySessionListByMainNodeAddr
public java.util.List<Session> getStandbySessionListByMainNodeAddr(java.net.InetSocketAddress addr)
Returns main node's standby session list.- Specified by:
getStandbySessionListByMainNodeAddr
in interfaceConnector
- Parameters:
addr
-- Returns:
-
findSessionByKey
public final Session findSessionByKey(java.lang.String key)
-
getSessionByAddress
public final java.util.Queue<Session> getSessionByAddress(java.net.InetSocketAddress addr)
Get session by InetSocketAddress- Specified by:
getSessionByAddress
in interfaceConnector
- Parameters:
addr
-- Returns:
-
setConnectionPoolSize
public final void setConnectionPoolSize(int poolSize)
- Specified by:
setConnectionPoolSize
in interfaceConnector
-
setMergeFactor
public void setMergeFactor(int mergeFactor)
- Specified by:
setMergeFactor
in interfaceConnector
-
getNoReplyOpsFlowControl
public FlowControl getNoReplyOpsFlowControl()
Description copied from interface:Connector
Returns the noreply operations flow control manager.- Specified by:
getNoReplyOpsFlowControl
in interfaceConnector
- Returns:
-
buildSession
protected NioSession buildSession(java.nio.channels.SocketChannel sc)
- Overrides:
buildSession
in classSocketChannelController
-
buildQueue
protected java.util.Queue<WriteMessage> buildQueue()
Build write queue for session- Overrides:
buildQueue
in classAbstractController
- Returns:
-
getBufferAllocator
public BufferAllocator getBufferAllocator()
-
quitAllSessions
public void quitAllSessions()
Description copied from interface:Connector
Make all connection sending a quit command to memcached- Specified by:
quitAllSessions
in interfaceConnector
-
setFailureMode
public void setFailureMode(boolean failureMode)
- Specified by:
setFailureMode
in interfaceConnector
-
setBufferAllocator
public void setBufferAllocator(BufferAllocator allocator)
- Specified by:
setBufferAllocator
in interfaceConnector
-
getServerAddresses
public java.util.Collection<java.net.InetSocketAddress> getServerAddresses()
-
-