Package org.apache.zookeeper.server
Class SessionTrackerImpl
- java.lang.Object
-
- java.lang.Thread
-
- org.apache.zookeeper.server.ZooKeeperThread
-
- org.apache.zookeeper.server.ZooKeeperCriticalThread
-
- org.apache.zookeeper.server.SessionTrackerImpl
-
- All Implemented Interfaces:
java.lang.Runnable
,SessionTracker
- Direct Known Subclasses:
LocalSessionTracker
public class SessionTrackerImpl extends ZooKeeperCriticalThread implements SessionTracker
This is a full featured SessionTracker. It tracks session in grouped by tick interval. It always rounds up the tick interval to provide a sort of grace period. Sessions are thus expired in batches made up of sessions that expire in a given interval.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SessionTrackerImpl.SessionImpl
-
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
-
Nested classes/interfaces inherited from interface org.apache.zookeeper.server.SessionTracker
SessionTracker.Session, SessionTracker.SessionExpirer
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.concurrent.ConcurrentHashMap<java.lang.Long,SessionTrackerImpl.SessionImpl>
sessionsById
-
Constructor Summary
Constructors Constructor Description SessionTrackerImpl(SessionTracker.SessionExpirer expirer, java.util.concurrent.ConcurrentMap<java.lang.Long,java.lang.Integer> sessionsWithTimeout, int tickTime, long serverId, ZooKeeperServerListener listener)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addGlobalSession(long id, int sessionTimeout)
Add a global session to those being tracked.boolean
addSession(long id, int sessionTimeout)
Add a session to those being tracked.void
checkGlobalSession(long sessionId, java.lang.Object owner)
Strictly check that a given session is a global session or notvoid
checkSession(long sessionId, java.lang.Object owner)
Checks whether the SessionTracker is aware of this session, the session is still active, and the owner matches.long
createSession(int sessionTimeout)
void
dumpSessions(java.io.PrintWriter pwriter)
Text dump of session information, suitable for debugging.java.util.Map<java.lang.Long,java.util.Set<java.lang.Long>>
getSessionExpiryMap()
Returns a mapping from time to session IDs of sessions expiring at that time.int
getSessionTimeout(long sessionId)
static long
initializeNextSession(long id)
Generates an initial sessionId.boolean
isTrackingSession(long sessionId)
void
removeSession(long sessionId)
void
run()
void
setOwner(long id, java.lang.Object owner)
void
setSessionClosing(long sessionId)
Mark that the session is in the process of closing.void
shutdown()
java.lang.String
toString()
boolean
touchSession(long sessionId, int timeout)
-
Methods inherited from class org.apache.zookeeper.server.ZooKeeperCriticalThread
handleException
-
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, yield
-
-
-
-
Field Detail
-
sessionsById
protected final java.util.concurrent.ConcurrentHashMap<java.lang.Long,SessionTrackerImpl.SessionImpl> sessionsById
-
-
Constructor Detail
-
SessionTrackerImpl
public SessionTrackerImpl(SessionTracker.SessionExpirer expirer, java.util.concurrent.ConcurrentMap<java.lang.Long,java.lang.Integer> sessionsWithTimeout, int tickTime, long serverId, ZooKeeperServerListener listener)
-
-
Method Detail
-
initializeNextSession
public static long initializeNextSession(long id)
Generates an initial sessionId. High order byte is serverId, next 5 5 bytes are from timestamp, and low order 2 bytes are 0s.
-
dumpSessions
public void dumpSessions(java.io.PrintWriter pwriter)
Description copied from interface:SessionTracker
Text dump of session information, suitable for debugging.- Specified by:
dumpSessions
in interfaceSessionTracker
- Parameters:
pwriter
- the output writer
-
getSessionExpiryMap
public java.util.Map<java.lang.Long,java.util.Set<java.lang.Long>> getSessionExpiryMap()
Returns a mapping from time to session IDs of sessions expiring at that time.- Specified by:
getSessionExpiryMap
in interfaceSessionTracker
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Thread
-
run
public void run()
- Specified by:
run
in interfacejava.lang.Runnable
- Overrides:
run
in classjava.lang.Thread
-
touchSession
public boolean touchSession(long sessionId, int timeout)
- Specified by:
touchSession
in interfaceSessionTracker
- Returns:
- false if session is no longer active
-
getSessionTimeout
public int getSessionTimeout(long sessionId)
-
setSessionClosing
public void setSessionClosing(long sessionId)
Description copied from interface:SessionTracker
Mark that the session is in the process of closing.- Specified by:
setSessionClosing
in interfaceSessionTracker
-
removeSession
public void removeSession(long sessionId)
- Specified by:
removeSession
in interfaceSessionTracker
-
shutdown
public void shutdown()
- Specified by:
shutdown
in interfaceSessionTracker
-
createSession
public long createSession(int sessionTimeout)
- Specified by:
createSession
in interfaceSessionTracker
-
addGlobalSession
public boolean addGlobalSession(long id, int sessionTimeout)
Description copied from interface:SessionTracker
Add a global session to those being tracked.- Specified by:
addGlobalSession
in interfaceSessionTracker
- Parameters:
id
- sessionIdsessionTimeout
- sessionTimeout- Returns:
- whether the session was newly added (if false, already existed)
-
addSession
public boolean addSession(long id, int sessionTimeout)
Description copied from interface:SessionTracker
Add a session to those being tracked. The session is added as a local session if they are enabled, otherwise as global.- Specified by:
addSession
in interfaceSessionTracker
- Parameters:
id
- sessionIdsessionTimeout
- sessionTimeout- Returns:
- whether the session was newly added (if false, already existed)
-
isTrackingSession
public boolean isTrackingSession(long sessionId)
- Specified by:
isTrackingSession
in interfaceSessionTracker
- Returns:
- whether or not the SessionTracker is aware of this session
-
checkSession
public void checkSession(long sessionId, java.lang.Object owner) throws KeeperException.SessionExpiredException, KeeperException.SessionMovedException, KeeperException.UnknownSessionException
Description copied from interface:SessionTracker
Checks whether the SessionTracker is aware of this session, the session is still active, and the owner matches. If the owner wasn't previously set, this sets the owner of the session. UnknownSessionException should never been thrown to the client. It is only used internally to deal with possible local session from other machine- Specified by:
checkSession
in interfaceSessionTracker
- Throws:
KeeperException.SessionExpiredException
KeeperException.SessionMovedException
KeeperException.UnknownSessionException
-
setOwner
public void setOwner(long id, java.lang.Object owner) throws KeeperException.SessionExpiredException
- Specified by:
setOwner
in interfaceSessionTracker
- Throws:
KeeperException.SessionExpiredException
-
checkGlobalSession
public void checkGlobalSession(long sessionId, java.lang.Object owner) throws KeeperException.SessionExpiredException, KeeperException.SessionMovedException
Description copied from interface:SessionTracker
Strictly check that a given session is a global session or not- Specified by:
checkGlobalSession
in interfaceSessionTracker
- Throws:
KeeperException.SessionExpiredException
KeeperException.SessionMovedException
-
-