/** * Calculate a unique id for this session across the cluster. * * Unique id is composed of: contextpath_virtualhost0_sessionid * @param data * @return */ private String calculateRowId (Session data) { String rowId = canonicalize(_context.getContextPath()); rowId = rowId + "_" + getVirtualHost(_context); rowId = rowId+"_"+data.getId(); return rowId; }
/** * Delete an existing session, both from the in-memory map and * the database. * * @see org.eclipse.jetty.server.session.AbstractSessionManager#removeSession(java.lang.String) */ @Override protected boolean removeSession(String idInCluster) { Session session = (Session)_sessions.remove(idInCluster); try { if (session != null) deleteSession(session); } catch (Exception e) { LOG.warn("Problem deleting session id="+idInCluster, e); } return session!=null; }
/** * Update the node on which the session was last seen to be my node. * * @param data the session * @throws Exception if unable to update the session node */ protected void updateSessionNode (Session data) throws Exception { String nodeId = getSessionIdManager().getWorkerName(); try (Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement(_jdbcSessionIdMgr._updateSessionNode)) { connection.setAutoCommit(true); statement.setString(1, nodeId); statement.setString(2, data.getRowId()); statement.executeUpdate(); } if (LOG.isDebugEnabled()) LOG.debug("Updated last node for session id="+data.getId()+", lastNode = "+nodeId); }
protected void expireCandidates (Set<String> candidateIds) { Iterator<String> itor = candidateIds.iterator(); long now = System.currentTimeMillis(); while (itor.hasNext()) { String id = itor.next(); //check if expired in db try { Session memSession = _sessions.get(id); if (memSession == null) { continue; //no longer in memory } Session s = loadSession(id, canonicalize(_context.getContextPath()), getVirtualHost(_context)); if (s == null) { //session no longer exists, can be safely expired memSession.timeout(); } } catch (Exception e) { LOG.warn("Error checking db for expiry for session {}", id); } } }
" interval="+(_saveIntervalSec * 1000L)+ " lastNode="+memSession._lastNode+ " thisNode="+getSessionIdManager().getWorkerName()+ " difference="+(now - memSession._lastSaved)); session = loadSession(idInCluster, canonicalize(_context.getContextPath()), getVirtualHost(_context)); session = loadSession(idInCluster, canonicalize(_context.getContextPath()), getVirtualHost(_context)); if (!session.getLastNode().equals(getSessionIdManager().getWorkerName()) || memSession==null) LOG.debug("getSession("+idInCluster+"): lastNode="+session.getLastNode()+" thisNode="+getSessionIdManager().getWorkerName()); session.setLastNode(getSessionIdManager().getWorkerName()); _sessions.put(idInCluster, session); updateSessionNode(session); session.didActivate();
try (Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement(_jdbcSessionIdMgr._insertSession)) String rowId = calculateRowId(session); statement.setString(3, session.getCanonicalContext()); //context path statement.setString(4, session.getVirtualHost()); //first vhost statement.setString(5, getSessionIdManager().getWorkerName());//my node id statement.setLong(6, session.getAccessed());//accessTime statement.setLong(7, session.getLastAccessedTime()); //lastAccessTime
if (isStopping() || isStopped()) return null; session = loadSession(sessionId, canonicalize(_context.getContextPath()), getVirtualHost(_context)); if (session != null)
updateSession(_data); didActivate(); else if ((_data._accessed - _data._lastSaved) >= (getSaveInterval() * 1000L))
if (isStopping() || isStopped()) return;
/** * Get a connection from the driver. * @return * @throws SQLException */ private Connection getConnection () throws SQLException { return ((JDBCSessionIdManager)getSessionIdManager()).getConnection(); }
/** * Invalidate the session matching the id on all contexts. * * @see org.eclipse.jetty.server.SessionIdManager#invalidateAll(java.lang.String) */ public void invalidateAll(String id) { //take the id out of the list of known sessionids for this node removeSession(id); synchronized (_sessionIds) { //tell all contexts that may have a session object with this id to //get rid of them Handler[] contexts = _server.getChildHandlersByClass(ContextHandler.class); for (int i=0; contexts!=null && i<contexts.length; i++) { SessionHandler sessionHandler = (SessionHandler)((ContextHandler)contexts[i]).getChildHandlerByClass(SessionHandler.class); if (sessionHandler != null) { SessionManager manager = sessionHandler.getSessionManager(); if (manager != null && manager instanceof JDBCSessionManager) { ((JDBCSessionManager)manager).invalidateSession(id); } } } } }
for (SessionManager m:managers) Set<String> successfullyExpiredIds = ((JDBCSessionManager)m).expire(expiredSessionIds); if (successfullyExpiredIds != null)
/** * Delete a session from the database. Should only be called * when the session has been invalidated. * * @param data the session data * @throws Exception if unable to delete the session */ protected void deleteSession (Session data) throws Exception { try (Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement(_jdbcSessionIdMgr._deleteSession)) { connection.setAutoCommit(true); statement.setString(1, data.getRowId()); statement.executeUpdate(); if (LOG.isDebugEnabled()) LOG.debug("Deleted Session "+data); } }
else if ((getAccessed() - _lastSaved) >= (getSaveInterval() * 1000L))
" interval="+(_saveIntervalSec * 1000L)+ " lastNode="+memSession._lastNode+ " thisNode="+getSessionIdManager().getWorkerName()+ " difference="+(now - memSession._lastSaved)); session = loadSession(idInCluster, canonicalize(_context.getContextPath()), getVirtualHost(_context)); session = loadSession(idInCluster, canonicalize(_context.getContextPath()), getVirtualHost(_context)); if (!session.getLastNode().equals(getSessionIdManager().getWorkerName()) || memSession==null) LOG.debug("getSession("+idInCluster+"): lastNode="+session.getLastNode()+" thisNode="+getSessionIdManager().getWorkerName()); session.setLastNode(getSessionIdManager().getWorkerName()); _sessions.put(idInCluster, session); updateSessionNode(session); session.didActivate();
try (Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement(_jdbcSessionIdMgr._insertSession)) String rowId = calculateRowId(session); statement.setString(3, session.getCanonicalContext()); //context path statement.setString(4, session.getVirtualHost()); //first vhost statement.setString(5, getSessionIdManager().getWorkerName());//my node id statement.setLong(6, session.getAccessed());//accessTime statement.setLong(7, session.getLastAccessedTime()); //lastAccessTime
if (isStopping() || isStopped()) return null; session = loadSession(sessionId, canonicalize(_context.getContextPath()), getVirtualHost(_context)); if (session != null)
updateSession(_data); didActivate(); else if ((_data._accessed - _data._lastSaved) >= (getSaveInterval() * 1000L))
if (isStopping() || isStopped()) return;
public SessionData (String sessionId) { _id=sessionId; _created=System.currentTimeMillis(); _accessed = _created; _attributes = new HashMap<String,Object>(); _lastNode = getSessionIdManager().getWorkerName(); }