
org.eclipse.jetty.server.session.AbstractSessionDataStore Maven / Gradle / Ivy
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.server.session;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* AbstractSessionDataStore
*/
@ManagedObject
public abstract class AbstractSessionDataStore extends ContainerLifeCycle implements SessionDataStore
{
private static final Logger LOG = LoggerFactory.getLogger(AbstractSessionDataStore.class);
protected SessionContext _context; //context associated with this session data store
protected int _gracePeriodSec = 60 * 60; //default of 1hr
protected long _lastExpiryCheckTime = 0; //last time in ms that getExpired was called
protected long _lastOrphanSweepTime = 0; //last time in ms that we deleted orphaned sessions
protected int _savePeriodSec = 0; //time in sec between saves
/**
* Small utility class to allow us to
* return a result and an Exception
* from invocation of Runnables.
*
* @param the type of the result.
*/
private class Result
{
private V _result;
private Exception _exception;
public void setResult(V result)
{
_result = result;
}
public void setException(Exception exception)
{
_exception = exception;
}
private void throwIfException() throws Exception
{
if (_exception != null)
throw _exception;
}
public V getOrThrow() throws Exception
{
throwIfException();
return _result;
}
}
/**
* Check if a session for the given id exists.
*
* @param id the session id to check
* @return true if the session exists in the persistent store, false otherwise
*/
public abstract boolean doExists(String id) throws Exception;
/**
* Store the session data persistently.
*
* @param id identity of session to store
* @param data info of the session
* @param lastSaveTime time of previous save or 0 if never saved
* @throws Exception if unable to store data
*/
public abstract void doStore(String id, SessionData data, long lastSaveTime) throws Exception;
/**
* Load the session from persistent store.
*
* @param id the id of the session to load
* @return the re-inflated session
* @throws Exception if unable to load the session
*/
public abstract SessionData doLoad(String id) throws Exception;
/**
* Implemented by subclasses to resolve which sessions in this context
* that are being managed by this node that should be expired.
*
* @param candidates the ids of sessions the SessionCache thinks has expired
* @param time the time at which to check for expiry
* @return the reconciled set of session ids that have been checked in the store
*/
public abstract Set doCheckExpired(Set candidates, long time);
/**
* Implemented by subclasses to find sessions for this context in the store
* that expired at or before the time limit and thus not being actively
* managed by any node. This method is only called periodically (the period
* is configurable) to avoid putting too much load on the store.
*
* @param before the upper limit of expiry times to check. Sessions expired
* at or before this timestamp will match.
*
* @return the empty set if there are no sessions expired as at the time, or
* otherwise a set of session ids.
*/
public abstract Set doGetExpired(long before);
/**
* Implemented by subclasses to delete sessions for other contexts that
* expired at or before the timeLimit. These are 'orphaned' sessions that
* are no longer being actively managed by any node. These are explicitly
* sessions that do NOT belong to this context (other mechanisms such as
* doGetExpired take care of those). As they don't belong to this context,
* they cannot be loaded by us.
*
* This is called only periodically to avoid placing excessive load on the
* store.
*
* @param time the upper limit of the expiry time to check in msec
*/
public abstract void doCleanOrphans(long time);
@Override
public void initialize(SessionContext context) throws Exception
{
if (isStarted())
throw new IllegalStateException("Context set after SessionDataStore started");
_context = context;
}
/**
* Remove all sessions for any context that expired at or before the given time.
* @param timeLimit the time before which the sessions must have expired.
*/
public void cleanOrphans(long timeLimit)
{
if (!isStarted())
throw new IllegalStateException("Not started");
Runnable r = () ->
{
doCleanOrphans(timeLimit);
};
_context.run(r);
}
@Override
public SessionData load(String id) throws Exception
{
if (!isStarted())
throw new IllegalStateException("Not started");
final Result result = new Result<>();
Runnable r = () ->
{
try
{
result.setResult(doLoad(id));
}
catch (Exception e)
{
result.setException(e);
}
};
_context.run(r);
return result.getOrThrow();
}
@Override
public void store(String id, SessionData data) throws Exception
{
if (!isStarted())
throw new IllegalStateException("Not started");
if (data == null)
return;
long lastSave = data.getLastSaved();
long savePeriodMs = (_savePeriodSec <= 0 ? 0 : TimeUnit.SECONDS.toMillis(_savePeriodSec));
if (LOG.isDebugEnabled())
{
LOG.debug("Store: id={}, mdirty={}, dirty={}, lsave={}, period={}, elapsed={}", id, data.isMetaDataDirty(),
data.isDirty(), data.getLastSaved(), savePeriodMs, (System.currentTimeMillis() - lastSave));
}
//save session if attribute changed, never been saved or metadata changed (eg expiry time) and save interval exceeded
if (data.isDirty() || (lastSave <= 0) ||
(data.isMetaDataDirty() && ((System.currentTimeMillis() - lastSave) >= savePeriodMs)))
{
//set the last saved time to now
data.setLastSaved(System.currentTimeMillis());
final Result
© 2015 - 2025 Weber Informatics LLC | Privacy Policy