SessionTrackerImpl (Apache ZooKeeper - Server 3.7.0 API)
Date: 2021-03-17

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:
    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 and Description
    --- | ---
    `static class` | `SessionTrackerImpl.SessionImpl`

    - ### Nested classes/interfaces inherited from class java.lang.Thread

      `Thread.State, Thread.UncaughtExceptionHandler`

    - ### Nested classes/interfaces inherited from interface org.apache.zookeeper.server.SessionTracker

      `SessionTracker.Session, SessionTracker.SessionExpirer`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected ConcurrentHashMap<Long,SessionTrackerImpl.SessionImpl>` | `sessionsById`

    - ### Fields inherited from class java.lang.Thread

      `MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `SessionTrackerImpl(SessionTracker.SessionExpirer expirer, ConcurrentMap<Long,Integer> sessionsWithTimeout, int tickTime, long serverId, ZooKeeperServerListener listener)`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `checkGlobalSession(long sessionId, Object owner)` Strictly check that a given session is a global session or not
    `void` | `checkSession(long sessionId, Object owner)` Checks whether the SessionTracker is aware of this session, the session is still active, and the owner matches.
    `boolean` | `commitSession(long id, int sessionTimeout)` Add the session to the local session map or global one in zkDB.
    `long` | `createSession(int sessionTimeout)`
    `void` | `dumpSessions(PrintWriter pwriter)` Text dump of session information, suitable for debugging.
    `long` | `getLocalSessionCount()` If this session tracker supports local sessions, return how many.
    `Map<Long,Set<Long>>` | `getSessionExpiryMap()` Returns a mapping from time to session IDs of sessions expiring at that time.
    `int` | `getSessionTimeout(long sessionId)`
    `static long` | `initializeNextSessionId(long id)` Generates an initial sessionId.
    `boolean` | `isLocalSessionsEnabled()`
    `boolean` | `isTrackingSession(long sessionId)`
    `void` | `removeSession(long sessionId)`
    `void` | `run()`
    `void` | `setOwner(long id, Object owner)`
    `void` | `setSessionClosing(long sessionId)` Mark that the session is in the process of closing.
    `void` | `shutdown()`
    `String` | `toString()`
    `boolean` | `touchSession(long sessionId, int timeout)`
    `boolean` | `trackSession(long id, int sessionTimeout)` Track the session expire, not add to ZkDb.

    - ### Methods inherited from class org.apache.zookeeper.server.ZooKeeperCriticalThread

      `handleException`

    - ### Methods inherited from class java.lang.Thread

      `activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, yield`

    - ### Methods inherited from class java.lang.Object

      `equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### sessionsById

          protected final ConcurrentHashMap<Long,SessionTrackerImpl.SessionImpl> sessionsById

  - ### Constructor Detail

    - #### SessionTrackerImpl

          public SessionTrackerImpl(SessionTracker.SessionExpirer expirer,
                                    ConcurrentMap<Long,Integer> sessionsWithTimeout,
                                    int tickTime,
                                    long serverId,
                                    ZooKeeperServerListener listener)

  - ### Method Detail

    - #### initializeNextSessionId

          public static long initializeNextSessionId(long id)

      Generates an initial sessionId.

      High order 1 byte is serverId, next 5 bytes are from timestamp, and low order 2 bytes are 0s. Use ">>> 8", not ">> 8" to make sure that the high order 1 byte is entirely up to the server Id.

      See also http://jira.apache.org/jira/browse/ZOOKEEPER-1622

      Parameters:
        `id` - server Id
      Returns:
        the session Id

    - #### dumpSessions

          public void dumpSessions(PrintWriter pwriter)

      Description copied from interface: `SessionTracker`
      Text dump of session information, suitable for debugging.
      Specified by:
        `dumpSessions` in interface `SessionTracker`
      Parameters:
        `pwriter` - the output writer

    - #### getSessionExpiryMap

          public Map<Long,Set<Long>> getSessionExpiryMap()

      Returns a mapping from time to session IDs of sessions expiring at that time.
      Specified by:
        `getSessionExpiryMap` in interface `SessionTracker`

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Thread`

    - #### run

          public void run()

      Specified by:
        `run` in interface `Runnable`
      Overrides:
        `run` in class `Thread`

    - #### touchSession

          public boolean touchSession(long sessionId,
                                      int timeout)

      Specified by:
        `touchSession` in interface `SessionTracker`
      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 interface `SessionTracker`

    - #### removeSession

          public void removeSession(long sessionId)

      Specified by:
        `removeSession` in interface `SessionTracker`

    - #### shutdown

          public void shutdown()

      Specified by:
        `shutdown` in interface `SessionTracker`

    - #### createSession

          public long createSession(int sessionTimeout)

      Specified by:
        `createSession` in interface `SessionTracker`

    - #### trackSession

          public boolean trackSession(long id,
                                      int sessionTimeout)

      Description copied from interface: `SessionTracker`
      Track the session expire, not add to ZkDb.
      Specified by:
        `trackSession` in interface `SessionTracker`
      Parameters:
        `id` - sessionId
        `sessionTimeout` - sessionTimeout
      Returns:
        whether the session was newly tracked (if false, already tracked)

    - #### commitSession

          public boolean commitSession(long id,
                                       int sessionTimeout)

      Description copied from interface: `SessionTracker`
      Add the session to the local session map or global one in zkDB.
      Specified by:
        `commitSession` in interface `SessionTracker`
      Parameters:
        `id` - sessionId
        `sessionTimeout` - sessionTimeout
      Returns:
        whether the session was newly added (if false, already existed)

    - #### isTrackingSession

          public boolean isTrackingSession(long sessionId)

      Specified by:
        `isTrackingSession` in interface `SessionTracker`
      Returns:
        whether or not the SessionTracker is aware of this session

    - #### checkSession

          public void checkSession(long sessionId,
                                   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 interface `SessionTracker`
      Throws:
        `KeeperException.SessionExpiredException`
        `KeeperException.SessionMovedException`
        `KeeperException.UnknownSessionException`

    - #### setOwner

          public void setOwner(long id,
                               Object owner)
                        throws KeeperException.SessionExpiredException

      Specified by:
        `setOwner` in interface `SessionTracker`
      Throws:
        `KeeperException.SessionExpiredException`

    - #### checkGlobalSession

          public void checkGlobalSession(long sessionId,
                                         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 interface `SessionTracker`
      Throws:
        `KeeperException.SessionExpiredException`
        `KeeperException.SessionMovedException`

    - #### getLocalSessionCount

          public long getLocalSessionCount()

      Description copied from interface: `SessionTracker`
      If this session tracker supports local sessions, return how many. otherwise returns 0;
      Specified by:
        `getLocalSessionCount` in interface `SessionTracker`

    - #### isLocalSessionsEnabled

          public boolean isLocalSessionsEnabled()

      Specified by:
        `isLocalSessionsEnabled` in interface `SessionTracker`

Copyright © 2008–2021 The Apache Software Foundation. All rights reserved.