apply changes after review #1571

Signed-off-by: olivier lamy <olamy@webtide.com>
This commit is contained in:
olivier lamy 2017-06-01 16:52:52 +10:00
parent 0c3b3dffce
commit a63e91d324
15 changed files with 105 additions and 137 deletions

View File

@ -78,16 +78,16 @@ Opening the `start.d/session-store-hazelcast-remote.ini` will show a list of all
# ---------------------------------------
--module=session-store-hazelcast-remote
#jetty.session.hazelcast.jettySessionMapName=jetty_sessions
#jetty.session.hazelcast.mapName=jetty_sessions
#jetty.session.hazelcast.onlyClient=true
#jetty.session.hazelcast.configurationLocation=
#jetty.session.gracePeriod.seconds=3600
#jetty.session.savePeriod.seconds=0
----
jetty.session.hazelcast.jettySessionMapName::
jetty.session.hazelcast.mapName::
Name of the Map in Hazelcast where sessions will be stored.
jetty.session.hazelcast.idleTimeout.onlyClient::
jetty.session.hazelcast.onlyClient::
Hazelcast instance will be configured in client mode
jetty.session.hazelcast.configurationLocation::
Path to an an Hazelcast xml configuration file
@ -160,12 +160,12 @@ Opening the `start.d/start.d/session-store-hazelcast-embedded.ini` will show a l
# ---------------------------------------
--module=session-store-hazelcast-embedded
#jetty.session.hazelcast.jettySessionMapName=jetty_sessions
#jetty.session.hazelcast.mapName=jetty_sessions
#jetty.session.hazelcast.configurationLocation=
#jetty.session.gracePeriod.seconds=3600
#jetty.session.savePeriod.seconds=0
----
jetty.session.hazelcast.jettySessionMapName::
jetty.session.hazelcast.mapName::
Name of the Map in Hazelcast where sessions will be stored.
jetty.session.gracePeriod.seconds::
Amount of time, in seconds, to wait for other nodes to be checked to verify an expired session is in fact expired throughout the cluster before closing it.

View File

@ -4,14 +4,6 @@
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- ===================================================================== -->
<!-- Get a reference to the default local Hazelcast Instance. -->
<!-- ===================================================================== -->
<!--
<New id="hazelcastInstance" class="com.hazelcast.core.HazelcastInstance">
</New>
-->
<!-- ===================================================================== -->
<!-- Configure a factory for HazelcastSessionDataStore using -->
<!-- an embedded Hazelcast Instance -->
@ -19,14 +11,10 @@
<Call name="addBean">
<Arg>
<New id="sessionDataStoreFactory" class="org.eclipse.jetty.hazelcast.session.HazelcastSessionDataStoreFactory">
<!--
<Set name="hazelcastInstance"><Ref id="hazelcastInstance"/></Set>
-->
<!--
<Set name="configurationLocation">
<Property name="jetty.session.configurationLocation" default="0"/>
</Set>
-->
<Set name="mapName"><Property name="jetty.session.hazelcast.mapName" default="jetty-distributed-session-map" /></Set>
<Set name="hazelcastInstanceName"><Property name="jetty.session.hazelcast.hazelcastInstanceName" default="JETTY_DISTRIBUTED_SESSION_INSTANCE" /></Set>
<Set name="gracePeriodSec"><Property name="jetty.session.gracePeriod.seconds" default="3600" /></Set>
<Set name="savePeriodSec"><Property name="jetty.session.savePeriod.seconds" default="0" /></Set>
</New>
</Arg>
</Call>

View File

@ -3,15 +3,6 @@
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- ===================================================================== -->
<!-- Get a reference to the default local Hazelcast Instance. -->
<!-- ===================================================================== -->
<!--
<New id="hazelcastInstance" class="com.hazelcast.core.HazelcastInstance">
</New>
-->
<!-- ===================================================================== -->
<!-- Configure a factory for HazelcastSessionDataStore using -->
<!-- an embedded Hazelcast Instance -->
@ -19,14 +10,11 @@
<Call name="addBean">
<Arg>
<New id="sessionDataStoreFactory" class="org.eclipse.jetty.hazelcast.session.HazelcastSessionDataStoreFactory">
<!--
<Set name="hazelcastInstance"><Ref id="hazelcastInstance"/></Set>
-->
<!--
<Set name="configurationLocation">
<Property name="jetty.session.configurationLocation" default="0"/>
</Set>
-->
<Set name="mapName"><Property name="jetty.session.hazelcast.mapName" default="jetty-distributed-session-map" /></Set>
<Set name="hazelcastInstanceName"><Property name="jetty.session.hazelcast.hazelcastInstanceName" default="JETTY_DISTRIBUTED_SESSION_INSTANCE" /></Set>
<Set name="gracePeriodSec"><Property name="jetty.session.gracePeriod.seconds" default="3600" /></Set>
<Set name="savePeriodSec"><Property name="jetty.session.savePeriod.seconds" default="0" /></Set>
<Set name="onlyClient"><Property name="jetty.session.hazelcast.onlyClient" default="true" /></Set>
</New>
</Arg>
</Call>

View File

@ -28,7 +28,8 @@ http://www.apache.org/licenses/LICENSE-2.0.html
[ini-template]
#jetty.session.hazelcast.jettySessionMapName=jetty_sessions
jetty.session.hazelcast.mapName=jetty-distributed-session-map
jetty.session.hazelcast.hazelcastInstanceName=JETTY_DISTRIBUTED_SESSION_INSTANCE
#jetty.session.hazelcast.configurationLocation=
#jetty.session.gracePeriod.seconds=3600
#jetty.session.savePeriod.seconds=0
jetty.session.gracePeriod.seconds=3600
jetty.session.savePeriod.seconds=0

View File

@ -28,8 +28,9 @@ http://www.apache.org/licenses/LICENSE-2.0.html
[ini-template]
#jetty.session.hazelcast.jettySessionMapName=jetty_sessions
#jetty.session.hazelcast.onlyClient=true
jetty.session.hazelcast.mapName=jetty-distributed-session-map
jetty.session.hazelcast.hazelcastInstanceName=JETTY_DISTRIBUTED_SESSION_INSTANCE
jetty.session.hazelcast.onlyClient=true
#jetty.session.hazelcast.configurationLocation=
#jetty.session.gracePeriod.seconds=3600
#jetty.session.savePeriod.seconds=0
jetty.session.gracePeriod.seconds=3600
jetty.session.savePeriod.seconds=0

View File

@ -18,21 +18,18 @@
package org.eclipse.jetty.hazelcast.session;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import org.eclipse.jetty.server.session.AbstractSessionDataStore;
import org.eclipse.jetty.server.session.SessionContext;
import org.eclipse.jetty.server.session.SessionData;
import org.eclipse.jetty.server.session.SessionDataMap;
import org.eclipse.jetty.server.session.SessionDataStore;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
/**
@ -46,10 +43,6 @@ public class HazelcastSessionDataStore
private final static Logger LOG = Log.getLogger( "org.eclipse.jetty.server.session");
private HazelcastInstance hazelcastInstance;
private String jettySessionMapName;
private IMap<String, SessionData> sessionDataMap;
public HazelcastSessionDataStore()
@ -57,46 +50,53 @@ public class HazelcastSessionDataStore
// no op
}
public HazelcastSessionDataStore( HazelcastInstance hazelcastInstance, String jettySessionMapName )
{
this.hazelcastInstance = hazelcastInstance;
this.jettySessionMapName = jettySessionMapName;
this.sessionDataMap = hazelcastInstance.getMap( getJettySessionMapName() );
}
public HazelcastInstance getHazelcastInstance()
{
return hazelcastInstance;
}
public void setHazelcastInstance( HazelcastInstance hazelcastInstance )
{
this.hazelcastInstance = hazelcastInstance;
}
public String getJettySessionMapName()
{
return jettySessionMapName;
}
public void setJettySessionMapName( String jettySessionMapName )
{
this.jettySessionMapName = jettySessionMapName;
}
@Override
public SessionData load( String id )
throws Exception
{
return sessionDataMap == null ? null : sessionDataMap.get( id );
final AtomicReference<SessionData> reference = new AtomicReference<SessionData>();
final AtomicReference<Exception> exception = new AtomicReference<Exception>();
//ensure the load runs in the context classloader scope
_context.run( () -> {
try
{
if (LOG.isDebugEnabled())
{
LOG.debug( "Loading session {} from hazelcast", id );
}
SessionData sd = sessionDataMap.get( getCacheKey( id ) );
reference.set(sd);
}
catch (Exception e)
{
exception.set(e);
}
} );
if (exception.get() != null)
{
throw exception.get();
}
return reference.get();
}
@Override
public boolean delete( String id )
throws Exception
{
return sessionDataMap == null ? false : sessionDataMap.remove( id ) != null;
return sessionDataMap == null ? false : sessionDataMap.remove( getCacheKey( id ) ) != null;
}
public IMap<String, SessionData> getSessionDataMap()
{
return sessionDataMap;
}
public void setSessionDataMap( IMap<String, SessionData> sessionDataMap )
{
this.sessionDataMap = sessionDataMap;
}
@Override
@ -104,30 +104,19 @@ public class HazelcastSessionDataStore
throws Exception
{
_context = context;
if (this.sessionDataMap == null)
{
this.sessionDataMap = getHazelcastInstance().getMap( getJettySessionMapName() );
}
}
@Override
public void store( String id, SessionData data )
throws Exception
{
this.sessionDataMap.put( id, data );
}
@Override
public void doStore( String id, SessionData data, long lastSaveTime )
throws Exception
{
this.sessionDataMap.put( id, data);
this.sessionDataMap.put( getCacheKey( id ), data);
}
@Override
public boolean isPassivating()
{
return false;
return true;
}
@Override
@ -206,6 +195,11 @@ public class HazelcastSessionDataStore
public boolean exists( String id )
throws Exception
{
return this.sessionDataMap.containsKey( id );
return this.sessionDataMap.containsKey( getCacheKey( id ) );
}
public String getCacheKey( String id )
{
return _context.getCanonicalContextPath() + "_" + _context.getVhost() + "_" + id;
}
}

View File

@ -41,15 +41,13 @@ public class HazelcastSessionDataStoreFactory
implements SessionDataStoreFactory
{
public static final String DEFAULT_HAZELCAST_INSTANCE_NAME = "JETTY_DISTRIBUTED_SESSION_INSTANCE";
public static final String DEFAULT_HAZELCAST_MAP_NAME = "jetty-distributed-session-map";
private String hazelcastInstanceName = "JETTY_DISTRIBUTED_SESSION_INSTANCE";
private boolean onlyClient;
private String configurationLocation;
private String jettySessionMapName = DEFAULT_HAZELCAST_MAP_NAME;
private String mapName = "jetty-distributed-session-map";
private HazelcastInstance hazelcastInstance;
@ -70,13 +68,12 @@ public class HazelcastSessionDataStoreFactory
{
if ( configurationLocation == null )
{
hazelcastSessionDataStore.setHazelcastInstance(
HazelcastClient.newHazelcastClient( new ClientConfig() ) );
hazelcastInstance = HazelcastClient.newHazelcastClient( new ClientConfig() );
}
else
{
hazelcastSessionDataStore.setHazelcastInstance( HazelcastClient.newHazelcastClient(
new XmlClientConfigBuilder( configurationLocation ).build() ) );
hazelcastInstance = HazelcastClient.newHazelcastClient(
new XmlClientConfigBuilder( configurationLocation ).build() );
}
}
@ -90,12 +87,12 @@ public class HazelcastSessionDataStoreFactory
if ( mapConfig == null )
{
mapConfig = new MapConfig();
mapConfig.setName( jettySessionMapName );
mapConfig.setName( mapName );
}
else
{
// otherwise we reuse the name
jettySessionMapName = mapConfig.getName();
mapName = mapConfig.getName();
}
config.addMapConfig( mapConfig );
}
@ -103,8 +100,8 @@ public class HazelcastSessionDataStoreFactory
{
config = new XmlConfigBuilder( configurationLocation ).build();
}
config.setInstanceName( DEFAULT_HAZELCAST_INSTANCE_NAME );
hazelcastSessionDataStore.setHazelcastInstance( Hazelcast.getOrCreateHazelcastInstance( config ) );
config.setInstanceName( hazelcastInstanceName );
hazelcastInstance = Hazelcast.getOrCreateHazelcastInstance( config );
}
}
catch ( IOException e )
@ -112,14 +109,10 @@ public class HazelcastSessionDataStoreFactory
throw new RuntimeException( e.getMessage(), e );
}
}
else
{
hazelcastSessionDataStore.setHazelcastInstance( hazelcastInstance );
}
hazelcastSessionDataStore.setJettySessionMapName( jettySessionMapName );
// initialize the map
hazelcastSessionDataStore.getHazelcastInstance().getMap( jettySessionMapName );
hazelcastSessionDataStore.setSessionDataMap(hazelcastInstance.getMap( mapName ) );
hazelcastSessionDataStore.setGracePeriodSec( getGracePeriodSec() );
hazelcastSessionDataStore.setSavePeriodSec( getSavePeriodSec() );
return hazelcastSessionDataStore;
}
@ -143,14 +136,14 @@ public class HazelcastSessionDataStoreFactory
this.configurationLocation = configurationLocation;
}
public String getJettySessionMapName()
public String getMapName()
{
return jettySessionMapName;
return mapName;
}
public void setJettySessionMapName( String jettySessionMapName )
public void setMapName( String mapName )
{
this.jettySessionMapName = jettySessionMapName;
this.mapName = mapName;
}
public HazelcastInstance getHazelcastInstance()
@ -172,4 +165,14 @@ public class HazelcastSessionDataStoreFactory
{
this.mapConfig = mapConfig;
}
public String getHazelcastInstanceName()
{
return hazelcastInstanceName;
}
public void setHazelcastInstanceName( String hazelcastInstanceName )
{
this.hazelcastInstanceName = hazelcastInstanceName;
}
}

View File

@ -25,15 +25,8 @@ import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.session.AbstractSessionCache;
import org.eclipse.jetty.server.session.CachingSessionDataStore;
import org.eclipse.jetty.server.session.DefaultSessionCache;
import org.eclipse.jetty.server.session.NullSessionDataStore;
import org.eclipse.jetty.server.session.Session;
import org.eclipse.jetty.server.session.SessionContext;
import org.eclipse.jetty.server.session.SessionData;
import org.eclipse.jetty.server.session.SessionDataMap;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.After;
@ -100,6 +93,7 @@ public class TestHazelcastSessions
}
private HazelcastSessionDataStore hazelcastSessionDataStore;
private HazelcastSessionDataStoreFactory hazelcastSessionDataStoreFactory;
private Server server;
@ -123,7 +117,7 @@ public class TestHazelcastSessions
SessionContext sessionContext = new SessionContext( "foo", null );
HazelcastSessionDataStoreFactory hazelcastSessionDataStoreFactory = new HazelcastSessionDataStoreFactory();
hazelcastSessionDataStoreFactory = new HazelcastSessionDataStoreFactory();
hazelcastSessionDataStore = (HazelcastSessionDataStore) hazelcastSessionDataStoreFactory.getSessionDataStore(
context.getSessionHandler() );
hazelcastSessionDataStore.initialize( sessionContext );
@ -141,7 +135,7 @@ public class TestHazelcastSessions
public void shutdown()
throws Exception
{
hazelcastSessionDataStore.getHazelcastInstance().shutdown();
hazelcastSessionDataStoreFactory.getHazelcastInstance().shutdown();
server.stop();
}

View File

@ -61,7 +61,7 @@ public class ClientLastAccessTimeTest
{
HazelcastSessionDataStoreFactory factory = new HazelcastSessionDataStoreFactory();
factory.setOnlyClient( true );
factory.setJettySessionMapName( MAP_NAME );
factory.setMapName( MAP_NAME );
return factory;
}

View File

@ -90,7 +90,7 @@ public class ClientNonClusteredSessionScavengingTest
{
HazelcastSessionDataStoreFactory factory = new HazelcastSessionDataStoreFactory();
factory.setOnlyClient( true );
factory.setJettySessionMapName( MAP_NAME );
factory.setMapName( MAP_NAME );
return factory;
}
}

View File

@ -62,7 +62,7 @@ public class ClientOrphanedSessionTest
{
HazelcastSessionDataStoreFactory factory = new HazelcastSessionDataStoreFactory();
factory.setOnlyClient( true );
factory.setJettySessionMapName( MAP_NAME );
factory.setMapName( MAP_NAME );
return factory;
}

View File

@ -28,7 +28,6 @@ import org.eclipse.jetty.server.session.AbstractSessionExpiryTest;
import org.eclipse.jetty.server.session.SessionDataStoreFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ClientSessionExpiryTest
extends AbstractSessionExpiryTest
@ -64,7 +63,7 @@ public class ClientSessionExpiryTest
{
HazelcastSessionDataStoreFactory factory = new HazelcastSessionDataStoreFactory();
factory.setOnlyClient( true );
factory.setJettySessionMapName( MAP_NAME );
factory.setMapName( MAP_NAME );
return factory;
}

View File

@ -62,7 +62,7 @@ public class ClientSessionInvalidateCreateScavengeTest
{
HazelcastSessionDataStoreFactory factory = new HazelcastSessionDataStoreFactory();
factory.setOnlyClient( true );
factory.setJettySessionMapName( MAP_NAME );
factory.setMapName( MAP_NAME );
return factory;
}
}

View File

@ -65,7 +65,7 @@ public class ClientSessionMigrationTest
{
HazelcastSessionDataStoreFactory factory = new HazelcastSessionDataStoreFactory();
factory.setOnlyClient( true );
factory.setJettySessionMapName( MAP_NAME );
factory.setMapName( MAP_NAME );
return factory;
}
}

View File

@ -63,7 +63,7 @@ public class ClientSessionScavengingTest
{
HazelcastSessionDataStoreFactory factory = new HazelcastSessionDataStoreFactory();
factory.setOnlyClient( true );
factory.setJettySessionMapName( MAP_NAME );
factory.setMapName( MAP_NAME );
return factory;
}
}