Package org.eclipse.jetty.nosql.mongodb
Class MongoSessionIdManager
- java.lang.Object
-
- org.eclipse.jetty.util.component.AbstractLifeCycle
-
- org.eclipse.jetty.server.session.AbstractSessionIdManager
-
- org.eclipse.jetty.nosql.mongodb.MongoSessionIdManager
-
- All Implemented Interfaces:
SessionIdManager
,org.eclipse.jetty.util.component.LifeCycle
public class MongoSessionIdManager extends AbstractSessionIdManager
Based partially on the JDBCSessionIdManager.Theory is that we really only need the session id manager for the local instance so we have something to scavenge on, namely the list of known ids
This class has a timer that runs a periodic scavenger thread to query for all id's known to this node whose precalculated expiry time has passed.
These found sessions are then run through the invalidateAll(id) method that is a bit hinky but is supposed to notify all handlers this id is now DOA and ought to be cleaned up. this ought to result in a save operation on the session that will change the valid field to false (this conjecture is unvalidated atm)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
MongoSessionIdManager.Purger
Purgerprotected class
MongoSessionIdManager.Scavenger
Scavenger
-
Field Summary
Fields Modifier and Type Field Description protected boolean
_ownScheduler
protected org.eclipse.jetty.util.thread.Scheduler.Task
_purgerTask
protected org.eclipse.jetty.util.thread.Scheduler.Task
_scavengerTask
protected org.eclipse.jetty.util.thread.Scheduler
_scheduler
protected Server
_server
protected Set<String>
_sessionsIds
the collection of session ids known to this manager-
Fields inherited from class org.eclipse.jetty.server.session.AbstractSessionIdManager
_random, _reseed, _weakRandom, _workerAttr, _workerName
-
-
Constructor Summary
Constructors Constructor Description MongoSessionIdManager(Server server)
MongoSessionIdManager(Server server, com.mongodb.DBCollection sessions)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addSession(javax.servlet.http.HttpSession session)
protected void
doStart()
protected void
doStop()
void
expireAll(String sessionId)
Expire this session for all contexts that are sharing the session id.long
getPurgeInvalidAge()
int
getPurgeLimit()
long
getPurgeValidAge()
int
getScavengeBlockSize()
com.mongodb.DBCollection
getSessions()
boolean
idInUse(String sessionId)
Searches database to find if the session id known to mongo, and is it validvoid
idle()
void
invalidateAll(String sessionId)
Remove the session id from the list of in-use sessions.boolean
isPurgeEnabled()
protected void
purge()
Purge is a process that cleans the mongodb cluster of old sessions that are no longer valid.protected void
purgeFully()
Purge is a process that cleans the mongodb cluster of old sessions that are no longer valid.void
removeSession(javax.servlet.http.HttpSession session)
void
renewSessionId(String oldClusterId, String oldNodeId, javax.servlet.http.HttpServletRequest request)
protected void
scavenge()
Scavenge is a process that periodically checks the tracked session ids of this given instance of the session id manager to see if they are past the point of expiration.protected void
scavengeBlock(long atTime, Set<String> ids)
Check a block of session ids for expiry and thus scavenge.protected void
scavengeFully()
ScavengeFully will expire all sessions.void
setPurge(boolean purge)
void
setPurgeDelay(long purgeDelay)
void
setPurgeInvalidAge(long purgeValidAge)
sets how old a session is to be persisted past the point it is no longer validvoid
setPurgeLimit(int purgeLimit)
The maximum number of items to return from a purge query.void
setPurgeValidAge(long purgeValidAge)
sets how old a session is to be persist past the point it is considered no longer viable and should be removed NOTE: set this value to 0 to disable purging of valid sessionsvoid
setScavengeBlockSize(int size)
When scavenging, the max number of session ids in the query.void
setScavengePeriod(long scavengePeriod)
The period in seconds between scavenge checks.-
Methods inherited from class org.eclipse.jetty.server.session.AbstractSessionIdManager
getClusterId, getNodeId, getRandom, getReseed, getWorkerName, initRandom, newSessionId, newSessionId, setRandom, setReseed, setWorkerName
-
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, setStopTimeout, start, stop
-
-
-
-
Field Detail
-
_server
protected Server _server
-
_scheduler
protected org.eclipse.jetty.util.thread.Scheduler _scheduler
-
_ownScheduler
protected boolean _ownScheduler
-
_scavengerTask
protected org.eclipse.jetty.util.thread.Scheduler.Task _scavengerTask
-
_purgerTask
protected org.eclipse.jetty.util.thread.Scheduler.Task _purgerTask
-
-
Constructor Detail
-
MongoSessionIdManager
public MongoSessionIdManager(Server server) throws UnknownHostException, com.mongodb.MongoException
- Throws:
UnknownHostException
com.mongodb.MongoException
-
MongoSessionIdManager
public MongoSessionIdManager(Server server, com.mongodb.DBCollection sessions)
-
-
Method Detail
-
scavenge
protected void scavenge()
Scavenge is a process that periodically checks the tracked session ids of this given instance of the session id manager to see if they are past the point of expiration.
-
scavengeBlock
protected void scavengeBlock(long atTime, Set<String> ids)
Check a block of session ids for expiry and thus scavenge.- Parameters:
atTime
- purge at timeids
- set of session ids
-
scavengeFully
protected void scavengeFully()
ScavengeFully will expire all sessions. In most circumstances you should never need to call this method. USE WITH CAUTION
-
purge
protected void purge()
Purge is a process that cleans the mongodb cluster of old sessions that are no longer valid. There are two checks being done here: - if the accessed time is older than the current time minus the purge invalid age and it is no longer valid then remove that session - if the accessed time is older then the current time minus the purge valid age then we consider this a lost record and remove it NOTE: if your system supports long lived sessions then the purge valid age should be set to zero so the check is skipped. The second check was added to catch sessions that were being managed on machines that might have crashed without marking their sessions as 'valid=false'
-
purgeFully
protected void purgeFully()
Purge is a process that cleans the mongodb cluster of old sessions that are no longer valid.
-
getSessions
public com.mongodb.DBCollection getSessions()
-
isPurgeEnabled
public boolean isPurgeEnabled()
-
setPurge
public void setPurge(boolean purge)
-
setScavengePeriod
public void setScavengePeriod(long scavengePeriod)
The period in seconds between scavenge checks.- Parameters:
scavengePeriod
- the scavenge period in seconds
-
setScavengeBlockSize
public void setScavengeBlockSize(int size)
When scavenging, the max number of session ids in the query.- Parameters:
size
- the scavenge block size
-
getScavengeBlockSize
public int getScavengeBlockSize()
-
setPurgeLimit
public void setPurgeLimit(int purgeLimit)
The maximum number of items to return from a purge query. If <= 0 there is no limit. Defaults to 0- Parameters:
purgeLimit
- the purge limit
-
getPurgeLimit
public int getPurgeLimit()
-
setPurgeDelay
public void setPurgeDelay(long purgeDelay)
-
getPurgeInvalidAge
public long getPurgeInvalidAge()
-
setPurgeInvalidAge
public void setPurgeInvalidAge(long purgeValidAge)
sets how old a session is to be persisted past the point it is no longer valid- Parameters:
purgeValidAge
- the purge valid age
-
getPurgeValidAge
public long getPurgeValidAge()
-
setPurgeValidAge
public void setPurgeValidAge(long purgeValidAge)
sets how old a session is to be persist past the point it is considered no longer viable and should be removed NOTE: set this value to 0 to disable purging of valid sessions- Parameters:
purgeValidAge
- the purge valid age
-
doStart
protected void doStart() throws Exception
- Overrides:
doStart
in classAbstractSessionIdManager
- Throws:
Exception
-
doStop
protected void doStop() throws Exception
- Overrides:
doStop
in classAbstractSessionIdManager
- Throws:
Exception
-
idInUse
public boolean idInUse(String sessionId)
Searches database to find if the session id known to mongo, and is it valid
-
addSession
public void addSession(javax.servlet.http.HttpSession session)
-
removeSession
public void removeSession(javax.servlet.http.HttpSession session)
-
invalidateAll
public void invalidateAll(String sessionId)
Remove the session id from the list of in-use sessions. Inform all other known contexts that sessions with the same id should be invalidated.
-
expireAll
public void expireAll(String sessionId)
Expire this session for all contexts that are sharing the session id.- Parameters:
sessionId
- the session id
-
idle
public void idle()
-
renewSessionId
public void renewSessionId(String oldClusterId, String oldNodeId, javax.servlet.http.HttpServletRequest request)
- Specified by:
renewSessionId
in interfaceSessionIdManager
- Specified by:
renewSessionId
in classAbstractSessionIdManager
-
-