Class SessionManager
java.lang.Object
com.loomcache.server.cp.SessionManager
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classRepresents a session managed by the SessionManager.static interfaceCallback invoked when a resource (e.g., lock or semaphore permit) is released due to session expiration.static final recordstatic interfacestatic interface -
Constructor Summary
ConstructorsConstructorDescriptionSessionManager(long sessionTTLMs) Creates a new SessionManager with the given TTL. -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddHeldPermits(String sessionId, String semaphoreName, int permits) Registers semaphore permits as held by an active session.voidDrops all local session state without invoking resource cleanup callbacks.booleancloseSession(String sessionId) Explicitly closes a session and releases all held resources.createSession(String ownerId) Creates a new session.voiddisableLocalExpiryProcessing(String reason) Disables timeout-driven expiry cleanup on this local JVM.Gets all active sessions.@Nullable SessionManager.ManagedSessiongetSession(String sessionId) Gets a session by ID.getStats()Gets statistics about current and historical sessions.booleanRecords a heartbeat for the given session, extending its expiration time.booleanvoidPauses background session expiry processing.booleanregisterHeldLock(String sessionId, String lockName) Registers a lock as held by an active session.voidrestoreSessions(Map<String, SessionManager.SessionSnapshot> sessionSnapshots) Rebuilds the managed session table from a snapshot.voidResumes background session expiry processing.voidsetResourceReleaseCallback(@Nullable SessionManager.ResourceReleaseCallback callback) Sets the callback to be invoked when session resources are released on expiry.voidsetSessionTerminationCallback(@Nullable SessionManager.SessionTerminationCallback callback) Sets the callback to be invoked after a session reaches a terminal state and its resources are released.voidsetSnapshotMutationGuard(@Nullable SessionManager.SnapshotMutationGuard guard) voidshutdown()Shuts down the session manager and stops the expiration thread.toString()
-
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
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
-
createSession
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 nullIllegalArgumentException- if ownerId is empty
-
pauseExpiry
public void pauseExpiry()Pauses background session expiry processing. -
resumeExpiry
public void resumeExpiry()Resumes background session expiry processing. -
disableLocalExpiryProcessing
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
Rebuilds the managed session table from a snapshot.- Parameters:
sessionSnapshots- session table keyed by session ID
-
registerHeldLock
-
addHeldPermits
Registers semaphore permits as held by an active session.- Parameters:
sessionId- the owning session IDsemaphoreName- the semaphore namepermits- the number of permits held- Returns:
- true if the permit ownership was registered, false if the session is missing or inactive
-
heartbeat
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
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
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
Gets statistics about current and historical sessions.- Returns:
- SessionStats with current metrics
-
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
-