Issue #352 Integrate session idling for MongoSessionManager

This commit is contained in:
Jan Bartel 2016-02-22 15:08:29 +01:00
parent 89ead7561e
commit ef6d0194b9
6 changed files with 478 additions and 31 deletions

View File

@ -18,6 +18,7 @@
package org.eclipse.jetty.nosql;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
@ -32,7 +33,9 @@ import org.eclipse.jetty.util.log.Logger;
/* ------------------------------------------------------------ */
public class NoSqlSession extends MemSession
{
private final static Logger __log = Log.getLogger("org.eclipse.jetty.server.session");
private final static Logger LOG = Log.getLogger("org.eclipse.jetty.server.session");
private enum IdleState {NOT_IDLE, IDLE, IDLING, DEIDLING};
private final NoSqlSessionManager _manager;
private Set<String> _dirty;
@ -40,6 +43,10 @@ public class NoSqlSession extends MemSession
private Object _version;
private long _lastSync;
private IdleState _idle = IdleState.NOT_IDLE;
private boolean _deIdleFailed;
/* ------------------------------------------------------------ */
public NoSqlSession(NoSqlSessionManager manager, HttpServletRequest request)
{
@ -73,7 +80,7 @@ public class NoSqlSession extends MemSession
}
/* ------------------------------------------------------------ */
@Override
public void setAttribute(String name, Object value)
{
@ -93,7 +100,7 @@ public class NoSqlSession extends MemSession
}
/* ------------------------------------------------------------ */
@Override
protected void timeout() throws IllegalStateException
{
@ -106,6 +113,10 @@ public class NoSqlSession extends MemSession
@Override
protected void checkValid() throws IllegalStateException
{
//whenever a method is called on the session, check that it was not idled and
//reinflate it if necessary
if (!isDeIdleFailed() && _manager.getIdlePeriod() > 0 && isIdle())
deIdle();
super.checkValid();
}
@ -113,7 +124,8 @@ public class NoSqlSession extends MemSession
@Override
protected boolean access(long time)
{
__log.debug("NoSqlSession:access:active {} time {}", _active, time);
if (LOG.isDebugEnabled())
LOG.debug("NoSqlSession:access:active {} time {}", _active, time);
if (_active.incrementAndGet()==1)
{
long period=_manager.getStalePeriod()*1000L;
@ -122,7 +134,8 @@ public class NoSqlSession extends MemSession
else if (period>0)
{
long stale=time-_lastSync;
__log.debug("NoSqlSession:access:stale "+stale);
if (LOG.isDebugEnabled())
LOG.debug("NoSqlSession:access:stale "+stale);
if (stale>period)
refresh();
}
@ -170,7 +183,112 @@ public class NoSqlSession extends MemSession
_lastSync=getAccessed();
}
}
/* ------------------------------------------------------------ */
public void idle ()
{
synchronized (this)
{
if (!isIdle() && !isIdling()) //don't re-idle an idle session as the attribute map will be empty
{
if (LOG.isDebugEnabled())
LOG.debug("Idling {}", super.getId());
setIdling();
save(false);
willPassivate();
clearAttributes();
setIdle(true);
}
}
}
/* ------------------------------------------------------------ */
public synchronized void deIdle()
{
if (LOG.isDebugEnabled())
LOG.debug("Checking before de-idling {}, isidle:{}, isDeidleFailed:", super.getId(), isIdle(), isDeIdleFailed());
if (isIdle() && !isDeIdleFailed())
{
setDeIdling();
if (LOG.isDebugEnabled())
LOG.debug("De-idling " + super.getId());
// Update access time to prevent race with idling period
super.access(System.currentTimeMillis());
//access may have expired and invalidated the session, so only deidle if it is still valid
if (isValid())
{
try
{
setIdle(false);
_version=_manager.refresh(this, new Long(0)); //ensure version should not match to force refresh
if (_version == null)
setDeIdleFailed(true);
}
catch (Exception e)
{
setDeIdleFailed(true);
LOG.warn("Problem de-idling session " + super.getId(), e);
invalidate();
}
}
}
}
/* ------------------------------------------------------------ */
public synchronized boolean isIdle ()
{
return _idle == IdleState.IDLE;
}
/* ------------------------------------------------------------ */
public synchronized boolean isIdling ()
{
return _idle == IdleState.IDLING;
}
/* ------------------------------------------------------------ */
public synchronized boolean isDeIdling()
{
return _idle == IdleState.DEIDLING;
}
public synchronized void setIdling ()
{
_idle = IdleState.IDLING;
}
public synchronized void setDeIdling ()
{
_idle = IdleState.DEIDLING;
}
/* ------------------------------------------------------------ */
public synchronized void setIdle (boolean idle)
{
if (idle)
_idle = IdleState.IDLE;
else
_idle = IdleState.NOT_IDLE;
}
public boolean isDeIdleFailed()
{
return _deIdleFailed;
}
public void setDeIdleFailed(boolean _deIdleFailed)
{
this._deIdleFailed = _deIdleFailed;
}
/* ------------------------------------------------------------ */
protected void refresh()
@ -209,13 +327,17 @@ public class NoSqlSession extends MemSession
{
return _version;
}
/* ------------------------------------------------------------ */
@Override
public void setClusterId(String clusterId)
{
super.setClusterId(clusterId);
}
/* ------------------------------------------------------------ */
@Override
public void setNodeId(String nodeId)
{

View File

@ -45,6 +45,7 @@ public abstract class NoSqlSessionManager extends AbstractSessionManager impleme
private int _stalePeriod=0;
private int _savePeriod=0;
private int _idlePeriod=-1;
private boolean _deidleBeforeExpiry = true;
private boolean _invalidateOnStop;
private boolean _preserveOnStop = true;
private boolean _saveAllAttributes;
@ -82,6 +83,8 @@ public abstract class NoSqlSessionManager extends AbstractSessionManager impleme
if (session==null)
{
__log.debug("Session {} is not in memory", idInCluster);
//session not in this node's memory, load it
session=loadSession(idInCluster);
@ -109,6 +112,8 @@ public abstract class NoSqlSessionManager extends AbstractSessionManager impleme
else
__log.debug("session does not exist {}", idInCluster);
}
else
session.deIdle();
return session;
}
@ -206,8 +211,15 @@ public abstract class NoSqlSessionManager extends AbstractSessionManager impleme
//we need to expire the session with its listeners, so load it
session = loadSession(idInCluster);
}
else
{
//deidle if the session was idled
if (isDeidleBeforeExpiry())
session.deIdle();
}
if (session != null)
//check that session is still valid after potential de-idle
if (session != null && session.isValid())
session.timeout();
}
catch (Exception e)
@ -304,7 +316,6 @@ public abstract class NoSqlSessionManager extends AbstractSessionManager impleme
* The Idle Period is the time in seconds before an in memory session is passivated.
* When this period is exceeded, the session will be passivated and removed from memory. If the session was dirty, it will be written to the DB.
* If the idle period is set to a value &lt; 0, then the session is never idled.
* If the save period is set to 0, then the session is idled whenever the active request count goes from 1 to 0.
* @return the idlePeriod
*/
public int getIdlePeriod()
@ -317,7 +328,6 @@ public abstract class NoSqlSessionManager extends AbstractSessionManager impleme
* The Idle Period is the time in seconds before an in memory session is passivated.
* When this period is exceeded, the session will be passivated and removed from memory. If the session was dirty, it will be written to the DB.
* If the idle period is set to a value &lt; 0, then the session is never idled.
* If the save period is set to 0, then the session is idled whenever the active request count goes from 1 to 0.
* @param idlePeriod the idlePeriod in seconds
*/
public void setIdlePeriod(int idlePeriod)
@ -385,6 +395,18 @@ public abstract class NoSqlSessionManager extends AbstractSessionManager impleme
_saveAllAttributes = saveAllAttributes;
}
/* ------------------------------------------------------------ */
public boolean isDeidleBeforeExpiry()
{
return _deidleBeforeExpiry;
}
/* ------------------------------------------------------------ */
public void setDeidleBeforeExpiry(boolean deidleBeforeExpiry)
{
_deidleBeforeExpiry = deidleBeforeExpiry;
}
/* ------------------------------------------------------------ */
@Override
public void renewSessionId(String oldClusterId, String oldNodeId, String newClusterId, String newNodeId)

View File

@ -76,10 +76,10 @@ public class MongoSessionIdManager extends AbstractSessionIdManager
final DBCollection _sessions;
protected Server _server;
private Scheduler _scheduler;
private boolean _ownScheduler;
private Scheduler.Task _scavengerTask;
private Scheduler.Task _purgerTask;
protected Scheduler _scheduler;
protected boolean _ownScheduler;
protected Scheduler.Task _scavengerTask;
protected Scheduler.Task _purgerTask;
@ -134,6 +134,7 @@ public class MongoSessionIdManager extends AbstractSessionIdManager
try
{
scavenge();
idle();
}
finally
{
@ -687,6 +688,26 @@ public class MongoSessionIdManager extends AbstractSessionIdManager
}
}
public void idle ()
{
//tell all contexts to passivate out idle sessions
Handler[] contexts = _server.getChildHandlersByClass(ContextHandler.class);
for (int i=0; contexts!=null && i<contexts.length; i++)
{
SessionHandler sessionHandler = ((ContextHandler)contexts[i]).getChildHandlerByClass(SessionHandler.class);
if (sessionHandler != null)
{
SessionManager manager = sessionHandler.getSessionManager();
if (manager != null && manager instanceof MongoSessionManager)
{
((MongoSessionManager)manager).idle();
}
}
}
}
/* ------------------------------------------------------------ */
@Override
public void renewSessionId(String oldClusterId, String oldNodeId, HttpServletRequest request)

View File

@ -322,7 +322,8 @@ public class MongoSessionManager extends NoSqlSessionManager
@Override
protected Object refresh(NoSqlSession session, Object version)
{
__log.debug("MongoSessionManager:refresh session {}", session.getId());
if (__log.isDebugEnabled())
__log.debug("MongoSessionManager:refresh session {}", session.getId());
// check if our in memory version is the same as what is on the disk
if (version != null)
@ -335,7 +336,8 @@ public class MongoSessionManager extends NoSqlSessionManager
if (saved != null && saved.equals(version))
{
__log.debug("MongoSessionManager:refresh not needed session {}", session.getId());
if (__log.isDebugEnabled())
__log.debug("MongoSessionManager:refresh not needed session {}", session.getId());
return version;
}
version = saved;
@ -347,8 +349,9 @@ public class MongoSessionManager extends NoSqlSessionManager
// If it doesn't exist, invalidate
if (o == null)
{
__log.debug("MongoSessionManager:refresh:marking session {} invalid, no object", session.getClusterId());
{
if (__log.isDebugEnabled())
__log.debug("MongoSessionManager:refresh:marking session {} invalid, no object", session.getClusterId());
session.invalidate();
return null;
}
@ -357,7 +360,8 @@ public class MongoSessionManager extends NoSqlSessionManager
Boolean valid = (Boolean)o.get(__VALID);
if (valid == null || !valid)
{
__log.debug("MongoSessionManager:refresh:marking session {} invalid, valid flag {}", session.getClusterId(), valid);
if (__log.isDebugEnabled())
__log.debug("MongoSessionManager:refresh:marking session {} invalid, valid flag {}", session.getClusterId(), valid);
session.invalidate();
return null;
}
@ -386,7 +390,7 @@ public class MongoSessionManager extends NoSqlSessionManager
Object value = decodeValue(attrs.get(name));
//session does not already contain this attribute, so bind it
if (session.getAttribute(attr) == null)
if (session.doGet(attr) == null)
{
session.doPutOrRemove(attr,value);
session.bindValue(attr,value);
@ -435,19 +439,26 @@ public class MongoSessionManager extends NoSqlSessionManager
return null;
}
/*------------------------------------------------------------ */
@Override
protected synchronized NoSqlSession loadSession(String clusterId)
{
DBObject o = _dbSessions.findOne(new BasicDBObject(__ID,clusterId));
__log.debug("MongoSessionManager:id={} loaded={}", clusterId, o);
if (__log.isDebugEnabled())
__log.debug("MongoSessionManager:id={} loaded={}", clusterId, o);
if (o == null)
return null;
Boolean valid = (Boolean)o.get(__VALID);
__log.debug("MongoSessionManager:id={} valid={}", clusterId, valid);
if (__log.isDebugEnabled())
__log.debug("MongoSessionManager:id={} valid={}", clusterId, valid);
if (valid == null || !valid)
return null;
@ -461,11 +472,12 @@ public class MongoSessionManager extends NoSqlSessionManager
// get the session for the context
DBObject attrs = (DBObject)getNestedValue(o,getContextKey());
__log.debug("MongoSessionManager:attrs {}", attrs);
if (__log.isDebugEnabled())
__log.debug("MongoSessionManager:attrs {}", attrs);
if (attrs != null)
{
__log.debug("MongoSessionManager: session {} present for context {}", clusterId, getContextKey());
if (__log.isDebugEnabled())
__log.debug("MongoSessionManager: session {} present for context {}", clusterId, getContextKey());
//only load a session if it exists for this context
session = new NoSqlSession(this,created,accessed,clusterId,version);
@ -483,7 +495,7 @@ public class MongoSessionManager extends NoSqlSessionManager
}
session.didActivate();
}
else
else if (__log.isDebugEnabled())
__log.debug("MongoSessionManager: session {} not present for context {}",clusterId, getContextKey());
return session;
@ -505,7 +517,8 @@ public class MongoSessionManager extends NoSqlSessionManager
@Override
protected boolean remove(NoSqlSession session)
{
__log.debug("MongoSessionManager:remove:session {} for context {}",session.getClusterId(), getContextKey());
if (__log.isDebugEnabled())
__log.debug("MongoSessionManager:remove:session {} for context {}",session.getClusterId(), getContextKey());
/*
* Check if the session exists and if it does remove the context
@ -539,7 +552,8 @@ public class MongoSessionManager extends NoSqlSessionManager
@Override
protected void expire (String idInCluster)
{
__log.debug("MongoSessionManager:expire session {} ", idInCluster);
if (__log.isDebugEnabled())
__log.debug("MongoSessionManager:expire session {} ", idInCluster);
//Expire the session for this context
super.expire(idInCluster);
@ -562,6 +576,34 @@ public class MongoSessionManager extends NoSqlSessionManager
}
/**
* Passivate out any sessions that are not expired, but have been idle
* longer than the idle timeout
*/
protected void idle ()
{
//no idle timout set, so don't idle out any sessions
if (getIdlePeriod() <= 0)
return;
long idleMs = getIdlePeriod()*1000L;
long now = System.currentTimeMillis();
synchronized (this) //necessary?
{
for (NoSqlSession session:_sessions.values())
{
if (session.getAccessed()+ idleMs < now)
{
//idle the session by passivating the session to mongo, then clearing the session's attribute map in memory
session.idle();
}
}
}
}
/*------------------------------------------------------------ */
/**
* Change the session id. Note that this will change the session id for all contexts for which the session id is in use.

View File

@ -0,0 +1,236 @@
//
// ========================================================================
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.nosql.mongodb;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.nosql.NoSqlSession;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.Test;
/**
* IdleSessionTest
*
* Test that mongo sessions can be passivated if idle longer than a configurable
* interval (which should be shorter than the expiry interval!)
*
*/
public class IdleSessionTest
{
public static TestServlet _servlet = new TestServlet();
public MongoTestServer createServer(int port, int max, int scavenge)
{
MongoTestServer server = new MongoTestServer(port,max,scavenge);
return server;
}
public void pause (int sec)
{
try
{
Thread.sleep(sec * 1000L);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
@Test
public void testIdleSession() throws Exception
{
String contextPath = "";
String servletMapping = "/server";
int inactivePeriod = 20; //sessions expire after 20 seconds
int scavengePeriod = 1; //look for expired sessions every second
int idlePeriod = 3; //after 3 seconds of inactivity, idle to disk
MongoTestServer server1 = createServer(0, inactivePeriod, scavengePeriod);
ServletHolder holder = new ServletHolder(_servlet);
ServletContextHandler contextHandler = server1.addContext(contextPath);
((MongoSessionManager)contextHandler.getSessionHandler().getSessionManager()).setIdlePeriod(idlePeriod);
contextHandler.addServlet(holder, servletMapping);
server1.start();
int port1 = server1.getPort();
try
{
HttpClient client = new HttpClient();
client.start();
String url = "http://localhost:" + port1 + contextPath + servletMapping;
//make a request to set up a session on the server
ContentResponse response = client.GET(url + "?action=init");
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
String sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
//and wait until the session should be idled out
pause(idlePeriod * 2);
//check that the session is idle
checkSessionIdle();
//make another request to de-idle the session
Request request = client.newRequest(url + "?action=test");
request.getHeaders().add("Cookie", sessionCookie);
ContentResponse response2 = request.send();
assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
//check session de-idled
checkSessionDeIdle();
checkValue(2);
//wait again for the session to be idled
pause(idlePeriod * 2);
//check that it is
checkSessionIdle();
//While idle, take some action to ensure that a deidle won't work, like
//deleting all sessions in mongo
assertTrue(server1.getServer().getSessionIdManager() instanceof MongoTestServer.TestMongoSessionIdManager);
((MongoTestServer.TestMongoSessionIdManager)server1.getServer().getSessionIdManager()).deleteAll();
//now make a request for which deidle should fail
request = client.newRequest(url + "?action=testfail");
request.getHeaders().add("Cookie", sessionCookie);
response2 = request.send();
assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
//Test trying to de-idle an expired session (ie before the scavenger can get to it)
System.err.println("\n TESTING DEIDLE EXPIRED\n");
//make a request to set up a session on the server
response = client.GET(url + "?action=init");
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
//and wait until the session should be idled out
pause(idlePeriod * 2);
//stop the scavenger
((MongoTestServer.TestMongoSessionIdManager)server1.getServer().getSessionIdManager()).cancelScavenge();
System.err.println("SCAVENGE STOPPED");
//check that the session is idle
checkSessionIdle();
System.err.println("WAITING FOR EXPIRY TIME TO PASS");
//wait until the session should be expired
pause (inactivePeriod + (inactivePeriod/2));
System.err.println("EXPIRY TIME PASSED");
//make a request to try and deidle the session
//make another request to de-idle the session
request = client.newRequest(url + "?action=testfail");
request.getHeaders().add("Cookie", sessionCookie);
response2 = request.send();
assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
}
finally
{
server1.stop();
}
}
public void checkSessionIdle ()
{
assertNotNull(_servlet);
assertNotNull((NoSqlSession)_servlet._session);
assertTrue(((NoSqlSession)_servlet._session).isIdle());
}
public void checkSessionDeIdle ()
{
assertNotNull(_servlet);
assertNotNull((NoSqlSession)_servlet._session);
assertTrue(!((NoSqlSession)_servlet._session).isIdle());
assertTrue(!((NoSqlSession)_servlet._session).isDeIdleFailed());
}
public void checkValue (int value)
{
assertNotNull(_servlet);
assertEquals(value, ((Integer)_servlet._session.getAttribute("value")).intValue());
}
public static class TestServlet extends HttpServlet
{
public String originalId = null;
public HttpSession _session = null;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
{
String action = request.getParameter("action");
if ("init".equals(action))
{
HttpSession session = request.getSession(true);
session.setAttribute("value", new Integer(1));
originalId = session.getId();
assertTrue(!((NoSqlSession)session).isIdle());
_session = session;
}
else if ("test".equals(action))
{
HttpSession session = request.getSession(false);
assertTrue(session != null);
assertTrue(originalId.equals(session.getId()));
assertTrue(!((NoSqlSession)session).isIdle());
Integer v = (Integer)session.getAttribute("value");
assertNotNull(v);
session.setAttribute("value", new Integer(v.intValue()+1));
}
else if ("testfail".equals(action))
{
HttpSession session = request.getSession(false);
assertTrue(session == null);
}
}
}
}

View File

@ -26,7 +26,6 @@ import org.eclipse.jetty.server.SessionManager;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.eclipse.jetty.server.session.SessionHandler;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoException;
@ -60,6 +59,12 @@ public class MongoTestServer extends AbstractTestServer
_sessions.remove(session);
}
}
public void cancelScavenge ()
{
if (_scavengerTask != null)
_scavengerTask.cancel();
}
}
public MongoTestServer(int port)
@ -84,7 +89,6 @@ public class MongoTestServer extends AbstractTestServer
{
try
{
System.err.println("MongoTestServer:SessionIdManager scavenge: delay:"+ _scavengePeriod + " period:"+_scavengePeriod);
MongoSessionIdManager idManager = new TestMongoSessionIdManager(_server);
idManager.setWorkerName("w"+(__workers++));
idManager.setScavengePeriod(_scavengePeriod);