Skip to content

Commit

Permalink
Implement clustered web-session handling #7529
Browse files Browse the repository at this point in the history
save work
  • Loading branch information
rymsha committed Oct 28, 2019
1 parent 1b902cc commit 660c2b2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class ElasticsearchCluster

private BundleContext context;

protected ServiceRegistration<Client> reg;
protected volatile ServiceRegistration<Client> reg;

private final static Logger LOG = LoggerFactory.getLogger( ElasticsearchCluster.class );

Expand Down Expand Up @@ -150,7 +150,7 @@ public void disable()
unregisterClient();
}

private void registerClient()
private synchronized void registerClient()
{
if ( this.reg != null )
{
Expand All @@ -161,7 +161,7 @@ private void registerClient()
this.reg = this.context.registerService( Client.class, this.node.client(), new Hashtable<>() );
}

private void unregisterClient()
private synchronized void unregisterClient()
{
if ( this.reg == null )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,58 +33,55 @@
public class IgniteCluster
implements Cluster
{
private Ignite ignite;
private static final Logger LOG = LoggerFactory.getLogger( IgniteCluster.class );

private ServiceRegistration<Ignite> reg;
private Ignite ignite;

private BundleContext context;
private ServiceRegistration<Ignite> igniteReg;

private static final Logger LOG = LoggerFactory.getLogger( IgniteCluster.class );

private IgniteSettings igniteSettings;
private ServiceRegistration<IgniteAdminClient> igniteAdminClientReg;

private ClusterConfig clusterConfig;

@SuppressWarnings("unused")
@Activate
public void activate( final BundleContext context, final IgniteSettings igniteSettings )
{
this.context = context;
this.igniteSettings = igniteSettings;

adjustLoggingVerbosity();

final IgniteConfiguration igniteConfig = ConfigurationFactory.create().
clusterConfig( this.clusterConfig ).
igniteConfig( this.igniteSettings ).
clusterConfig( clusterConfig ).
igniteConfig( igniteSettings ).
bundleContext( context ).
build().
execute();

System.setProperty( IGNITE_NO_SHUTDOWN_HOOK, "true" );

final Thread thread = Thread.currentThread();
ClassLoader classLoader = thread.getContextClassLoader();
final ClassLoader classLoader = thread.getContextClassLoader();
try
{
thread.setContextClassLoader( Ignite.class.getClassLoader() );
this.ignite = Ignition.start( igniteConfig );
ignite = Ignition.start( igniteConfig );
}
finally
{
thread.setContextClassLoader( classLoader );
}
context.registerService( Ignite.class, ignite, new Hashtable<>() );
igniteReg = context.registerService( Ignite.class, ignite, new Hashtable<>() );
// Register admin-client to use in e.g reporting
context.registerService( IgniteAdminClient.class, new IgniteAdminClientImpl( this.ignite ), new Hashtable<>() );
igniteAdminClientReg =
context.registerService( IgniteAdminClient.class, new IgniteAdminClientImpl( this.ignite ), new Hashtable<>() );
}

@SuppressWarnings("unused")
@Deactivate
public void deactivate()
{
unregisterClient();
this.ignite.close();
igniteReg.unregister();
igniteAdminClientReg.unregister();
ignite.close();
}

@Override
Expand Down Expand Up @@ -129,49 +126,19 @@ private ClusterNodes doGetNodes()
@Override
public void enable()
{
registerService();
// Ignore. Ignite Cluster should be always enabled
}

@Override
public void disable()
{
unregisterClient();
// Ignore. Ignite Cluster should be always enabled
}

@Override
public boolean isEnabled()
{
return this.reg != null;
}

private void registerService()
{
if ( this.reg != null )
{
return;
}

LOG.info( "Cluster operational, register " + this.getId() );

this.reg = context.registerService( Ignite.class, ignite, new Hashtable<>() );
}

private void unregisterClient()
{
if ( this.reg == null )
{
return;
}

try
{
LOG.info( "Cluster not operational, unregister " + this.getId() );
this.reg.unregister();
}
finally
{
this.reg = null;
}
return true;
}

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class IgniteSessionDataStore

private String contextString;

private String currentNode;

public IgniteSessionDataStore( final Cache<String, IgniteSessionData> igniteCache )
{
this.igniteCache = igniteCache;
Expand All @@ -35,6 +37,7 @@ public void initialize( final SessionContext context )
{
super.initialize( context );
contextString = context.getCanonicalContextPath() + "_" + context.getVhost();
currentNode = context.getWorkerName();
}

@Override
Expand All @@ -45,15 +48,6 @@ public SessionData doLoad( String id )
{
LOG.trace( "Loading session {} from Ignite", id );
}
if ( igniteCache == null )
{
if ( LOG.isDebugEnabled() )
{
LOG.debug( "Ignite cache is not set" );
}

return null;
}

final IgniteSessionData igniteSessionData = igniteCache.get( getCacheKey( id ) );
if ( igniteSessionData == null )
Expand All @@ -78,15 +72,6 @@ public boolean delete( String id )
LOG.trace( "Deleting session {} from Ignite", id );
}

if ( igniteCache == null )
{
if ( LOG.isDebugEnabled() )
{
LOG.debug( "Ignite cache is not set" );
}
return false;
}

return igniteCache.remove( getCacheKey( id ) );
}

Expand All @@ -97,14 +82,7 @@ public void doStore( String id, SessionData data, long lastSaveTime )
{
LOG.trace( "Store session {} in Ignite", id );
}
if ( igniteCache == null )
{
if ( LOG.isDebugEnabled() )
{
LOG.debug( "Ignite cache is not set" );
}
return;
}

igniteCache.put( getCacheKey( id ), new IgniteSessionData( data ) );
}

Expand Down Expand Up @@ -162,7 +140,6 @@ private boolean checkCandidate( final String candidate, long now )
}

final String managerNode = data.getLastNode();
final String currentNode = _context.getWorkerName();

final boolean expired;
if ( currentNode.equals( managerNode ) )
Expand Down

0 comments on commit 660c2b2

Please sign in to comment.