Class SessionManager

java.lang.Object
com.loomcache.server.cp.SessionManager

public final class SessionManager extends Object
Manages CP session lifecycle: creation, heartbeat, expiration, and cleanup.

Purpose: SessionManager tracks session state, enforces session timeouts, and automatically releases resources held by expired sessions (locks, semaphore permits). A background virtual thread checks for expired sessions every 5 seconds.

Lifecycle:

  • Session created with initial TTL (default 300 seconds)
  • Heartbeat extends the session timeout
  • Background thread checks for expiration every 5 seconds
  • On expiry: all locks released, semaphore permits returned, state = EXPIRED
  • On explicit close: state = CLOSED, resources released

Thread Safety: This class is thread-safe. Multiple threads can safely create sessions and interact with them concurrently.

Since:
1.0
  • Constructor Details

    • SessionManager

      public SessionManager(long sessionTTLMs)
      Creates a new SessionManager with the given TTL.
      Parameters:
      sessionTTLMs - the session time-to-live in milliseconds (must be positive)
      Throws:
      IllegalArgumentException - if sessionTTLMs invalid input: '<'= 0
  • Method Details

    • setResourceReleaseCallback

      public void setResourceReleaseCallback(@Nullable SessionManager.ResourceReleaseCallback callback)
      Sets the callback to be invoked when session resources are released on expiry.
      Parameters:
      callback - the callback to invoke, or null to clear
    • setSessionTerminationCallback

      public void setSessionTerminationCallback(@Nullable SessionManager.SessionTerminationCallback callback)
      Sets the callback to be invoked after a session reaches a terminal state and its resources are released.
      Parameters:
      callback - the callback to invoke, or null to clear
    • setSnapshotMutationGuard

      public void setSnapshotMutationGuard(@Nullable SessionManager.SnapshotMutationGuard guard)
    • createSession

      public SessionManager.ManagedSession createSession(String ownerId)
      Creates a new session.
      Parameters:
      ownerId - the owner ID (client identifier, must not be null or empty)
      Returns:
      a new ManagedSession
      Throws:
      NullPointerException - if ownerId is null
      IllegalArgumentException - if ownerId is empty
    • pauseExpiry

      public void pauseExpiry()
      Pauses background session expiry processing.
    • resumeExpiry

      public void resumeExpiry()
      Resumes background session expiry processing.
    • disableLocalExpiryProcessing

      public void disableLocalExpiryProcessing(String reason)
      Disables timeout-driven expiry cleanup on this local JVM. Production CP resources must not be released by a follower-local wall-clock task; callers should use explicit, replicated session close/force-close until expiry itself is driven through the CP log.
    • isLocalExpiryProcessingEnabled

      public boolean isLocalExpiryProcessingEnabled()
    • clearAllSessions

      public void clearAllSessions()
      Drops all local session state without invoking resource cleanup callbacks.
    • restoreSessions

      public void restoreSessions(Map<String, SessionManager.SessionSnapshot> sessionSnapshots)
      Rebuilds the managed session table from a snapshot.
      Parameters:
      sessionSnapshots - session table keyed by session ID
    • registerHeldLock

      public boolean registerHeldLock(String sessionId, String lockName)
      Registers a lock as held by an active session.
      Parameters:
      sessionId - the owning session ID
      lockName - the lock name
      Returns:
      true if the lock ownership was registered, false if the session is missing or inactive
    • addHeldPermits

      public boolean addHeldPermits(String sessionId, String semaphoreName, int permits)
      Registers semaphore permits as held by an active session.
      Parameters:
      sessionId - the owning session ID
      semaphoreName - the semaphore name
      permits - the number of permits held
      Returns:
      true if the permit ownership was registered, false if the session is missing or inactive
    • heartbeat

      public boolean heartbeat(String sessionId)
      Records a heartbeat for the given session, extending its expiration time.
      Parameters:
      sessionId - the session ID (must not be null)
      Returns:
      true if the session exists and is active, false if not found or expired
      Throws:
      NullPointerException - if sessionId is null
    • getSession

      public @Nullable SessionManager.ManagedSession getSession(String sessionId)
      Gets a session by ID.
      Parameters:
      sessionId - the session ID (must not be null)
      Returns:
      the ManagedSession, or null if not found
      Throws:
      NullPointerException - if sessionId is null
    • closeSession

      public boolean closeSession(String sessionId)
      Explicitly closes a session and releases all held resources.
      Parameters:
      sessionId - the session ID (must not be null)
      Returns:
      true if the session was closed, false if not found
      Throws:
      NullPointerException - if sessionId is null
    • getStats

      public SessionStats getStats()
      Gets statistics about current and historical sessions.
      Returns:
      SessionStats with current metrics
    • getActiveSessions

      public Collection<SessionManager.ManagedSession> getActiveSessions()
      Gets all active sessions.
      Returns:
      a collection of all active ManagedSessions
    • shutdown

      public void shutdown()
      Shuts down the session manager and stops the expiration thread.
    • toString

      public String toString()
      Overrides:
      toString in class Object