User: slaboure
Date: 01/11/09 14:49:56
Added: src/main/org/jboss/ha/hasessionstate/server
HASessionStateImpl.java HASessionStateService.java
HASessionStateServiceMBean.java
HASessionStateTopologyComputer.java
HASessionStateTopologyComputerImpl.java
PackagedSessionImpl.java
Log:
Clustering for SFSB
Revision Changes Path
1.1
jbossmx/src/main/org/jboss/ha/hasessionstate/server/HASessionStateImpl.java
Index: HASessionStateImpl.java
===================================================================
/*
* JBoss, the OpenSource J2EE WebOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.ha.hasessionstate.server;
import org.jboss.ha.framework.interfaces.HAPartition;
import org.jboss.ha.framework.server.HAPartitionImpl;
import org.jboss.ha.framework.interfaces.HAPartition;
import org.jboss.ha.framework.interfaces.DistributedReplicantManager;
import org.jboss.ha.framework.interfaces.DistributedState;
import org.jboss.ha.framework.interfaces.DistributedState.DSListener;
import org.jboss.ha.framework.interfaces.SubPartitionInfo;
import org.jboss.ha.framework.interfaces.SubPartitionsInfo;
import org.jboss.ha.hasessionstate.interfaces.PackagedSession;
import org.jboss.logging.Logger;
import JavaGroups.JChannel;
import JavaGroups.Channel;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Vector;
import java.io.Serializable;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.Reference;
import javax.naming.StringRefAddr;
import javax.naming.Name;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.NameNotFoundException;
import org.jboss.naming.NonSerializableFactory;
/**
* Default implementation of HASessionState
*
* @see HASessionState
* @author [EMAIL PROTECTED]
* @version $Revision: 1.1 $
*
* <p><b>Revisions:</b><br>
*/
public class HASessionStateImpl
implements org.jboss.ha.hasessionstate.interfaces.HASessionState,
DistributedReplicantManager.ReplicantListener,
DistributedState.DSListener, HAPartition.HAPartitionStateTransfer
{
protected String _sessionStateName = null;
protected String _jChannelProps = null;
protected Logger log = null;
protected HAPartition hapGeneral = null;
protected HAPartitionImpl hapSpecific = null;
protected JChannel channel = null;
protected String sessionStateIdentifier = null;
protected DistributedReplicantManager drm = null;
protected DistributedState ds = null;
protected String myNodeName = null;
protected ArrayList replicants = null;
protected long nodesPerSubPartition;
protected long subPartitionCreationWaitTime;
protected long beanCleaningDelay;
protected String haPartitionName = null;
protected String haPartitionJndiName = null;
protected HASessionStateTopologyComputer topologyComputer = null;
protected String topologyComputerClassName = null;
protected SubPartitionInfo currentPartition = null;
protected final String DEFAULT_PARTITION_JNDI_NAME =
org.jboss.metadata.ClusterConfigMetaData.DEFAULT_PARTITION;
protected final String JNDI_FOLDER_NAME_FOR_HASESSIONSTATE =
org.jboss.metadata.ClusterConfigMetaData.JNDI_PREFIX_FOR_SESSION_STATE;
protected final String JNDI_FOLDER_NAME_FOR_HAPARTITION = "/HAPartition/";
protected final long MAX_DELAY_BEFORE_CLEANING_UNRECLAIMED_STATE = 30L * 60L *
1000L; // 30 minutes... should be set externally or use cache settings
protected final long SUB_PARTITON_WAIT_TIME_BEFORE_CREATION = 5L * 1000L; // we
wait 5 seconds that the master node has create the group otherwise singleton groups
are formed
protected final long DEFAULT_SUBPARTITON_SIZE = 2;
protected final String DEFAULT_TOPOLOGY_COMPUTER_CLASS =
org.jboss.ha.hasessionstate.server.HASessionStateTopologyComputerImpl.class.getName ();
protected static final String HA_SESSION_STATE_STATE_TRANSFER =
"HASessionStateTransfer";
public HASessionStateImpl ()
{}
public HASessionStateImpl (String sessionStateName,
String jChannelProperties,
String mainHAPartitionName,
String topologyComputerClass,
long subPartitionSize,
long subPartitionCreationWaitTime,
long beanCleaningDelay)
{
this._jChannelProps = jChannelProperties;
if (sessionStateName == null)
this._sessionStateName =
org.jboss.metadata.ClusterConfigMetaData.DEFAULT_SESSION_STATE_NAME;
else
this._sessionStateName = sessionStateName;
this.sessionStateIdentifier = "SessionState-'" + this._sessionStateName + "'";
if (mainHAPartitionName == null)
haPartitionName = DEFAULT_PARTITION_JNDI_NAME;
else
haPartitionName = mainHAPartitionName;
haPartitionJndiName = JNDI_FOLDER_NAME_FOR_HAPARTITION + haPartitionName;
if (subPartitionSize > 1)
this.nodesPerSubPartition = subPartitionSize;
else
this.nodesPerSubPartition = DEFAULT_SUBPARTITON_SIZE;
if (subPartitionCreationWaitTime > 0)
this.subPartitionCreationWaitTime = subPartitionCreationWaitTime;
else
this.subPartitionCreationWaitTime = SUB_PARTITON_WAIT_TIME_BEFORE_CREATION;
if (beanCleaningDelay > 0)
this.beanCleaningDelay = beanCleaningDelay;
else
this.beanCleaningDelay = MAX_DELAY_BEFORE_CLEANING_UNRECLAIMED_STATE;
if (topologyComputerClass == null)
topologyComputerClassName = this.DEFAULT_TOPOLOGY_COMPUTER_CLASS;
else
topologyComputerClassName = topologyComputerClass;
}
public void init () throws Exception
{
this.log = Logger.create ("HASessionState-" + this._sessionStateName);
// we will use the default HAPartition for our communication with other
HASessionState and
// a new HAPartition for our session state exchange with our assigned
"brother" node(s).
//
// init our topolgy computer
//
topologyComputer = (HASessionStateTopologyComputer)Thread.currentThread
().getContextClassLoader ().loadClass (this.topologyComputerClassName).newInstance ();
topologyComputer.init (this.sessionStateIdentifier, this.nodesPerSubPartition);
// let's first find the default HAPartition
//
Context ctx = new InitialContext ();
this.hapGeneral = (HAPartition)ctx.lookup (haPartitionJndiName);
if (hapGeneral == null)
log.error ("Unable to get default HAPartition under name '" +
haPartitionJndiName + "'.");
this.bind (this._sessionStateName, this, HASessionStateImpl.class, ctx);
log.info ("...HAPartition initialized.");
}
protected void bind (String jndiName, Object who, Class classType, Context ctx)
throws Exception
{
// Ah ! This service isn't serializable, so we use a helper class
//
NonSerializableFactory.bind (jndiName, who);
Name n = ctx.getNameParser ("").parse (jndiName);
while (n.size () > 1)
{
String ctxName = n.get (0);
try
{
ctx = (Context)ctx.lookup (ctxName);
}
catch (NameNotFoundException e)
{
log.info ("creating Subcontext" + ctxName);
ctx = ctx.createSubcontext (ctxName);
}
n = n.getSuffix (1);
}
// The helper class NonSerializableFactory uses address type nns, we go on to
// use the helper class to bind the service object in JNDI
//
StringRefAddr addr = new StringRefAddr ("nns", jndiName);
Reference ref = new Reference ( classType.getName (), addr,
NonSerializableFactory.class.getName (), null);
ctx.bind (n.get (0), ref);
}
public void start () throws Exception
{
topologyComputer.start ();
// we do not start our HAPartition and our channel : this will depend of the
cluster splitting
//
this.myNodeName = this.hapGeneral.getNodeName ();
log.info ("HASessionState node name : " + this.myNodeName );
// we register with the DS that will allow us to know the current and future
spliting configurations
//
this.ds = this.hapGeneral.getDistributedStateService ();
this.ds.registerDSListener (this.sessionStateIdentifier, this);
// we register ourself with the general HAPartition to tell other nodes that
we are alive
//
this.drm = this.hapGeneral.getDistributedReplicantManager ();
this.replicants = this.drm.lookupReplicants (this.sessionStateIdentifier);
this.drm.registerListener (this.sessionStateIdentifier, this);
this.drm.add (this.sessionStateIdentifier, this.myNodeName );
}
public void stop () throws Exception
{
try
{
Context ctx = new InitialContext ();
ctx.unbind (this._sessionStateName);
}
catch (Exception ignored)
{}
this.unconnectFromSubPartition ();
}
public String getNodeName ()
{
return this.myNodeName ;
}
// DistributedState event handlers: we store the subpartitions topology in the
distributed state
//
public void keyHasBeenRemoved (String category, String key, Serializable
previousContent) { /* well... okay...*/}
public void valueHasChanged (String category, String key, Serializable value)
{
if (category.equals (this.sessionStateIdentifier) && key.equals
("HASubPartitionSpliting"))
this.newSplitingInfoReceived ((SubPartitionsInfo)value);
}
protected void newSplitingInfoReceived (SubPartitionsInfo newSplitingInfo)
{
log.info ("Starting repartitioning... :" + newSplitingInfo);
SubPartitionInfo subPartition = newSplitingInfo.getSubPartitionWithName
(this.myNodeName);
if (currentPartition == null)
{
// We were part of nothing: we just need to join and get a state transfer
//
log.info("We were not yet connected. We connect to sub-partition " +
subPartition.subPartitionName);
currentPartition = subPartition;
this.reconnectToSubPartition ();
topologyHasChanged (this.hapSpecific, false);
}
else
{
// we were already part of a partition before. We need to check if we need
to move to another
// partition or stay in the same partition
//
if (subPartition.subPartitionName.equals
(currentPartition.subPartitionName))
{
// we stay in the same partition! That is great!
//
log.info ("We stay in the same partition ('" +
currentPartition.subPartitionName + "'): nothing to do");
currentPartition = subPartition;
topologyHasChanged (this.hapSpecific, true);
}
else
{
// We move to another partition... too bad... some costly state transfer
is needed thus...
//
log.info ("We need to change from partition '" +
currentPartition.subPartitionName +
"' to partition '" + subPartition.subPartitionName + "'");
// we first leave our current partition
//
log.debug ("Disconnecting from old sub-partition...");
this.unconnectFromSubPartition ();
log.debug ("... Done disconnecting from old sub-partition.");
// we remember our old session states
//
Hashtable oldSessions = this.appSessions;
this.appSessions = new Hashtable ();
// we join the new one => state transfer will occur
//
currentPartition = subPartition;
log.debug ("Reconnecting to new sub-partition...");
this.reconnectToSubPartition ();
log.debug ("... Donc reconnecting to new sub-partition");
// And we add our session state from our previous subpartition to our
new hosting sub-partition
//
log.debug ("Copying owned state to new sub-partition...");
for (Enumeration keyEnum = oldSessions.keys (); keyEnum.hasMoreElements
();)
{
Object key = keyEnum.nextElement ();
Hashtable value = (Hashtable)oldSessions.get (key);
Hashtable destApp = (Hashtable)this.appSessions.get (key);
synchronized (this.lockAppSession)
{
if (destApp == null)
this.appSessions.put (key, value);
else
synchronized (destApp)
{
destApp.putAll (value);
}
}
// we also broadcast these sessions to all other nodes
// (we also could directly pass the whole set of states and reduce
this to just one call) TODO
// (at least, the possitive effect is that it locks other nodes less
time (bu more often) )
//
Object[] args =
{ key, value } ;
try
{
this.hapSpecific.callMethodOnCluster (this.sessionStateIdentifier,
"_setStates", args, true);
}
catch (Exception displayed)
{ displayed.printStackTrace (); }
}
log.debug ("...State copied to new sub-partition.");
// The JRMPContainerInvokerHA subscribes to such events to know when to
refresh its proxy generator
//
topologyHasChanged (this.hapSpecific, false);
}
}
log.info ("Repartitioning done.");
}
protected void unconnectFromSubPartition ()
{
try
{
this.hapSpecific.unsubscribeFromStateTransferEvents
(this.HA_SESSION_STATE_STATE_TRANSFER, this);
this.hapSpecific.unregisterRPCHandler (this.sessionStateIdentifier, this);
this.channel.Disconnect ();
this.hapSpecific.close ();
this.hapSpecific = null;
}
catch (Exception displayed)
{
displayed.printStackTrace ();
}
finally
{
this.channel = null;
}
}
protected void reconnectToSubPartition ()
{
// we may avoid to destroy and recreate HAPartitionImpl each time when our
framework
// will support "local" state transfer (only for a particual RPC service).
//
try
{
if (this.channel == null)
{
// we now create our specific HAPartition for session state transfer
between nodes
//
log.debug ("Creating JavaGroups JChannel for session state transfer");
this.channel = new JChannel (this._jChannelProps);
this.channel.SetOpt (Channel.LOCAL, new Boolean (false));
this.channel.SetOpt (Channel.GET_STATE_EVENTS, new Boolean (true));
this.channel.SetOpt (Channel.AUTO_RECONNECT, new Boolean (true));
this.channel.SetOpt (Channel.AUTO_GETSTATE, new Boolean (true));
}
log.debug ("Creating sub-HAPartition for HASessionState...");
this.hapSpecific = new HAPartitionImpl
(this.currentPartition.subPartitionName.replace ('/', '|'), this.channel, true
/*Deadlock detection*/);
log.debug ("...Sub-HAPartition created.");
this.hapSpecific.registerRPCHandler (this.sessionStateIdentifier, this);
this.hapSpecific.subscribeToStateTransferEvents
(this.HA_SESSION_STATE_STATE_TRANSFER, this);
log.debug ("Initing HAPartition...");
this.hapSpecific.init ();
log.debug ("...HAPartition inited.");
if (currentPartition != null)
{
if (!amITheLocalSessionStateMasterNode ())
{
log.debug ("As I am not the master node of the sub-partition to be
created, I will wait before connection to avoid singletons");
// We need a first member to create the group otherwise they each
form a singleton
//
try
{
synchronized (this)
{
this.wait (subPartitionCreationWaitTime);
}
}
catch (InterruptedException ie)
{
ie.printStackTrace ();
}
}
log.debug ("Connection to Channel...");
this.channel.Connect (currentPartition.subPartitionName);
log.debug ("... Connected to Channel.");
log.debug ("Starting sub-HAPartition...");
this.hapSpecific.start ();
log.debug ("...Sub-HAPartition started.");
}
}
catch (Exception displayed)
{
displayed.printStackTrace ();
}
}
// Used for Session state transfer
//
public Serializable getCurrentState ()
{
log.debug ("Building and returning state of HASessionState");
if (this.appSessions == null)
this.appSessions = new Hashtable ();
Serializable result = null;
synchronized (this.lockAppSession)
{
this.purgeState ();
try
{
result = deflate (this.appSessions);
} catch (Exception displayed)
{ displayed.printStackTrace (); }
}
return result;
}
public void setCurrentState (Serializable newState)
{
log.debug ("Receiving state of HASessionState");
if (this.appSessions == null)
this.appSessions = new Hashtable ();
synchronized (this.lockAppSession)
{
try
{
this.appSessions.clear (); // hope to facilitate the job of the GC
this.appSessions = (Hashtable)inflate ((byte[])newState);
} catch (Exception displayed)
{ displayed.printStackTrace (); }
}
}
public void purgeState ()
{
synchronized (this.lockAppSession)
{
for (Enumeration keyEnum = this.appSessions.keys ();
keyEnum.hasMoreElements ();)
{
// trip in apps..
//
Object key = keyEnum.nextElement ();
Hashtable value = (Hashtable)this.appSessions.get (key);
long currentTime = System.currentTimeMillis ();
for (Iterator iterSessions = value.values ().iterator ();
iterSessions.hasNext ();)
{
PackagedSession ps = (PackagedSession)iterSessions.next ();
synchronized (ps)
{
if ( (currentTime - ps.unmodifiedExistenceInVM ()) >
beanCleaningDelay )
iterSessions.remove ();
}
}
}
}
}
protected byte[] deflate (Object object) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
Deflater def = new Deflater (java.util.zip.Deflater.BEST_COMPRESSION);
DeflaterOutputStream dos = new DeflaterOutputStream (baos, def);
ObjectOutputStream out = new ObjectOutputStream (dos);
out.writeObject (object);
out.close ();
dos.finish ();
dos.close ();
return baos.toByteArray ();
}
protected Object inflate (byte[] compressedContent) throws IOException
{
if (compressedContent==null)
return null;
try
{
ObjectInputStream in = new ObjectInputStream (new InflaterInputStream (new
ByteArrayInputStream (compressedContent)));
Object object = in.readObject ();
in.close ();
return object;
}
catch (Exception e)
{
throw new IOException (e.toString ());
}
}
protected boolean amITheLocalSessionStateMasterNode ()
{
if (this.currentPartition == null || this.currentPartition.memberNodeNames ==
null || this.currentPartition.memberNodeNames.size () < 1 )
return true;
else
return this.currentPartition.memberNodeNames.get (0).equals
(this.myNodeName);
}
protected boolean amITheSessionStateMasterNode ()
{
if (this.replicants == null)
return true;
else
return amITheMasterNode (this.replicants, this.hapGeneral);
}
protected boolean amITheMasterNode (ArrayList newReplicants, HAPartition
partition)
{
// we could only check if we are the first member of the partition but it is
not sufficient
// as we may have some members of the global HAPartition that are not part of
the HASessionStateImpl
//
if (partition == null)
return true;
Vector view = partition.getCurrentView ();
if (view == null)
return true;
for (int i=0; i< view.size (); i++)
{
String nodeName = view.elementAt (i).toString ();
if (newReplicants.contains (nodeName))
if (nodeName.equals (this.myNodeName))
return true;
else
return false;
}
return true;
}
public void replicantsChanged (String key, ArrayList newReplicants)
{
// we have a new list of replicant. If we are the first in the list of
replicants,
// we have the responsibility to decide the new cluster sub-partition topology
//
if (key.equals (this.sessionStateIdentifier))
{
ArrayList allReplicants = (ArrayList)newReplicants.clone ();
this.replicants = (ArrayList)allReplicants.clone ();
if (allReplicants != null && amITheSessionStateMasterNode () )
{
// we only bother about the spliting if we are the first in the list of
replicants
//
log.info ("A new HASessionState topology needs to be computed by the
master node => this node. ");
log.info ("New nodes: " + newReplicants);
// we get the actual spliting
//
try
{
SubPartitionsInfo currentSplitingInfo =
(SubPartitionsInfo)this.ds.get (this.sessionStateIdentifier, "HASubPartitionSpliting");
if (currentSplitingInfo != null)
currentSplitingInfo = (SubPartitionsInfo)currentSplitingInfo.clone
();
else
currentSplitingInfo = new SubPartitionsInfo ();
currentSplitingInfo = topologyComputer.computeNewTopology
(currentSplitingInfo, allReplicants);
log.info ("Computed topology : " + currentSplitingInfo);
this.ds.set (this.sessionStateIdentifier, "HASubPartitionSpliting",
currentSplitingInfo);
} catch (RemoteException ignored)
{/* we do not use these objects remotely, so this won't happen */}
catch (Exception problem)
{ problem.printStackTrace ();}
}
}
}
protected Hashtable appSessions = new Hashtable ();
protected Object lockAppSession = new Object ();
protected Hashtable getHashtableForApp (String appName)
{
if (this.appSessions == null)
this.appSessions = new Hashtable (); // should never happen though...
Hashtable result = null;
synchronized (this.lockAppSession)
{
result = (Hashtable)this.appSessions.get (appName);
if (result == null)
{
result = new Hashtable ();
this.appSessions.put (appName, result);
}
}
return result;
}
public void createSession (String appName, Object keyId)
{
this._createSession (appName, keyId);
}
public PackagedSessionImpl _createSession (String appName, Object keyId)
{
Hashtable app = this.getHashtableForApp (appName);
PackagedSessionImpl result = new PackagedSessionImpl ((Serializable)keyId,
null, this.myNodeName);
app.put (keyId, result);
return result;
}
public void setState (String appName, Object keyId, byte[] state)
{
Hashtable app = this.getHashtableForApp (appName);
PackagedSession ps = (PackagedSession)app.get (keyId);
if (ps == null)
ps = _createSession (appName, keyId);
synchronized (ps)
{
if (!ps.setState (state))
{
// we need to broadcast a new state
//
Object[] args =
{appName, ps};
try
{
this.hapSpecific.callMethodOnCluster (this.sessionStateIdentifier,
"_setState", args, true);
}
catch (Exception displayed)
{ displayed.printStackTrace (); }
}
}
}
public void _setStates (String appName, Hashtable packagedSessions)
{
synchronized (this.lockAppSession)
{
Hashtable app = this.getHashtableForApp (appName);
if (app == null)
{
app = new Hashtable (packagedSessions.size ());
this.appSessions.put (appName, app);
}
app.putAll (packagedSessions);
}
}
public void _setState (String appName, PackagedSession session)
{
Hashtable app = this.getHashtableForApp (appName);
PackagedSession ps = (PackagedSession)app.get (session.getKey ());
if (ps == null)
{
ps = session;
synchronized (app)
{
app.put (ps.getKey (), ps);
}
}
else
{
synchronized (app)
{
synchronized (ps)
{
if (ps.getOwner ().equals (this.myNodeName))
{
// a modification has occured externally while we were the owner
//
ownedObjectExternallyModified (appName, session.getKey (), ps,
session);
}
ps.update (session);
}
}
}
}
public PackagedSession getState (String appName, Object keyId)
{
Hashtable app = this.getHashtableForApp (appName);
return (PackagedSession)app.get (keyId);
}
public PackagedSession getStateWithOwnership (String appName, Object keyId)
throws java.rmi.RemoteException
{
return this.localTakeOwnership (appName, keyId);
}
public PackagedSession localTakeOwnership (String appName, Object keyId) throws
java.rmi.RemoteException
{
Hashtable app = this.getHashtableForApp (appName);
PackagedSession ps = (PackagedSession)app.get (keyId);
synchronized (ps)
{
if (!ps.getOwner ().equals (this.myNodeName))
{
Object[] args =
{appName, keyId, this.sessionStateIdentifier, new Long (ps.getVersion
())};
ArrayList answers = null;
try
{
answers = this.hapSpecific.callMethodOnCluster
(this.sessionStateIdentifier, "_setOwnership", args, true);
}
catch (Exception displayed)
{ displayed.printStackTrace (); }
if (answers != null && answers.contains (Boolean.FALSE))
throw new java.rmi.RemoteException ("Concurent calls on session
object.");
else
return ps;
}
else
return ps;
}
}
public Boolean _setOwnership (String appName, Object keyId, String newOwner, Long
remoteVersion)
{
Hashtable app = this.getHashtableForApp (appName);
PackagedSession ps = (PackagedSession)app.get (keyId);
Boolean answer = Boolean.TRUE;
synchronized (ps)
{
if (!ps.getOwner ().equals (this.myNodeName))
// this is not our business... we don't care
// we do not update the owner of ps as another host may refuse the
_setOwnership call
// anyway, the update will be sent to us later if state is modified
//
//ps.setOwner (newOwner);
answer = Boolean.TRUE;
else if (ps.getVersion () > remoteVersion.longValue ())
// we are concerned and our version is more recent than the one of the
remote host!
// it means that we have concurrent calls on the same state that has not
yet been updated
// this means we will need to raise a java.rmi.RemoteException
//
answer = Boolean.FALSE;
else
{
// the remote host has the same version as us (or more recent? possible?)
// we need to update the ownership. We can do this because we know that
no other
// node can refuse the _setOwnership call
ps.setOwner (newOwner);
ownedObjectExternallyModified (appName, keyId, ps, ps);
answer = Boolean.TRUE;
}
}
return answer;
}
public void takeOwnership (String appName, Object keyId) throws
java.rmi.RemoteException
{
this.localTakeOwnership (appName, keyId);
}
public void removeSession (String appName, Object keyId)
{
Hashtable app = this.getHashtableForApp (appName);
if (app != null)
synchronized (app)
{
PackagedSession ps = (PackagedSession)app.remove (keyId);
if (ps != null)
synchronized (ps)
{
Object[] args =
{ appName, keyId };
try
{
this.hapSpecific.callMethodOnCluster
(this.sessionStateIdentifier, "_removeSession", args, true);
}
catch (Exception displayed)
{ displayed.printStackTrace (); }
}
}
}
public void _removeSession (String appName, Object keyId)
{
Hashtable app = this.getHashtableForApp (appName);
PackagedSession ps = null;
synchronized (app)
{
ps = (PackagedSession)app.remove (keyId);
}
if (ps != null && ps.getOwner ().equals (this.myNodeName))
ownedObjectExternallyModified (appName, keyId, ps, ps);
}
protected Hashtable listeners = new Hashtable ();
public synchronized void subscribe (String appName, HASessionStateListener
listener)
{
Vector members = (Vector)listeners.get (appName);
if (members == null)
{
members = new Vector ();
listeners.put (appName, members);
}
if (!members.contains (listener))
members.add (listener);
}
public synchronized void unsubscribe (String appName, HASessionStateListener
listener)
{
Vector members = (Vector)listeners.get (appName);
if ((members != null) && members.contains (listener))
members.remove (listener);
}
public void ownedObjectExternallyModified (String appName, Object key,
PackagedSession oldSession, PackagedSession newSession)
{
Vector members = (Vector)listeners.get (appName);
if (members != null)
for (int i=0; i<members.size (); i++)
((HASessionStateListener)members.elementAt
(i)).sessionExternallyModified (newSession);
}
protected Vector topologySubscribers = new Vector ();
public void subscribe (HASessionStateTopologyListener listener)
{
synchronized (this.topologySubscribers)
{
if (!this.topologySubscribers.contains (listener))
this.topologySubscribers.add (listener);
}
}
public void unsubscribe (HASessionStateTopologyListener listener)
{
synchronized (this.topologySubscribers)
{
this.topologySubscribers.remove (listener);
}
}
public void topologyHasChanged (HAPartition partition, boolean samePartition)
{
synchronized (this.topologySubscribers)
{
Iterator iter = this.topologySubscribers.iterator ();
while (iter.hasNext ())
try
{
((HASessionStateTopologyListener)iter.next
()).newSessionStateTopology (partition, samePartition);
}
catch (Exception e)
{
e.printStackTrace ();
}
}
}
public HAPartition getCurrentHAPartition ()
{
return this.hapSpecific;
}
}
1.1
jbossmx/src/main/org/jboss/ha/hasessionstate/server/HASessionStateService.java
Index: HASessionStateService.java
===================================================================
/*
* JBoss, the OpenSource J2EE WebOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.ha.hasessionstate.server;
import org.jboss.logging.Logger;
import org.jboss.system.ServiceMBeanSupport;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.MalformedObjectNameException;
import org.jboss.metadata.ClusterConfigMetaData;
import org.jboss.ha.hasessionstate.server.HASessionStateImpl;
/**
* Service class for HASessionState
*
* @see HASessionState
* @author [EMAIL PROTECTED]
* @version $Revision: 1.1 $
*
* <p><b>Revisions:</b><br>
*/
public class HASessionStateService
extends ServiceMBeanSupport
implements HASessionStateServiceMBean
{
protected String jndiName = null;
protected String subPartitionProps = "UDP:" +
"PING:" +
"FD(trace=true;timeout=5000):" +
"VERIFY_SUSPECT(trace=false;timeout=1500):" +
"MERGE:" +
"NAKACK(trace=true):" +
"UNICAST(timeout=5000;min_wait_time=2000):" +
"FRAG:" +
"FLUSH:" +
"GMS:" +
"STATE_TRANSFER(trace=true):" +
"QUEUE";
protected String haPartitionName = null;
protected long subPartitionSize = 0;
protected long subPartitionCreationWaitTime = 0;
protected long beanCleaningDelay = 0;
protected String topologyComputerClass = null;
protected HASessionStateImpl sessionState = null;
public String getName ()
{
return this.getJndiName ();
}
public String getJndiName ()
{
return this.jndiName;
}
public void setJndiName (String newName)
{
this.jndiName = newName;
}
public void setSubPartitionProperties (String newProps)
{
this.subPartitionProps = newProps;
}
public String getSubPartitionProperties ()
{
return this.subPartitionProps;
}
public String getMainHaPartitionName ()
{
return this.haPartitionName;
}
public void setMainHaPartitionName (String name)
{
this.haPartitionName = name;
}
public long getSubPartitionSize ()
{
if (this.sessionState == null)
return this.subPartitionSize;
else
return this.sessionState.nodesPerSubPartition;
}
public void setSubPartitionSize (long newSize)
{ this.subPartitionSize = newSize; }
public long getSubPartitionCreationWaitTime ()
{
if (this.sessionState == null)
return this.subPartitionCreationWaitTime;
else
return this.sessionState.subPartitionCreationWaitTime;
}
public void setSubPartitionCreationWaitTime (long newWaitTime)
{ this.subPartitionCreationWaitTime = newWaitTime; }
public long getBeanCleaningDelay ()
{
if (this.sessionState == null)
return this.beanCleaningDelay;
else
return this.sessionState.beanCleaningDelay;
}
public void setBeanCleaningDelay (long newDelay)
{ this.beanCleaningDelay = newDelay; }
public String getTopologyComputerClassName ()
{
if (this.sessionState == null)
return this.topologyComputerClass;
else
return this.sessionState.topologyComputerClassName;
}
public void setTopologyComputerClassName (String newName)
{ this.topologyComputerClass = newName; }
// ******************************************************************
public ObjectName getObjectName (MBeanServer server, ObjectName name)
throws javax.management.MalformedObjectNameException
{
return new ObjectName (OBJECT_NAME);
}
// ******************************************************************
public void initService () throws Exception
{
this.sessionState = new HASessionStateImpl (this.jndiName,
this.subPartitionProps, this.haPartitionName, this.topologyComputerClass,
this.subPartitionSize,
this.subPartitionCreationWaitTime, this.beanCleaningDelay);
this.sessionState.init ();
}
public void startService () throws Exception
{
this.sessionState.start ();
}
public void stopService ()
{
try
{
this.sessionState.stop ();
this.sessionState = null;
}
catch (Exception ignored)
{
ignored.printStackTrace ();
}
}
}
1.1
jbossmx/src/main/org/jboss/ha/hasessionstate/server/HASessionStateServiceMBean.java
Index: HASessionStateServiceMBean.java
===================================================================
/*
* JBoss, the OpenSource J2EE WebOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.ha.hasessionstate.server;
/**
* MBEAN interface for HASessionState service
*
* @see HASessionState
* @author [EMAIL PROTECTED]
* @version $Revision: 1.1 $
*
* <p><b>Revisions:</b><br>
*/
public interface HASessionStateServiceMBean extends org.jboss.system.ServiceMBean
{
// Constants -----------------------------------------------------
public static final String OBJECT_NAME = "JBOSS-SYSTEM:service=HASessionState";
public String getJndiName();
public void setJndiName(String newName);
public String getSubPartitionProperties(); // i.e. JavaGroups properties
public void setSubPartitionProperties(String newProps);
public String getMainHaPartitionName ();
public void setMainHaPartitionName (String name);
public long getSubPartitionSize ();
public void setSubPartitionSize (long newSize);
public long getSubPartitionCreationWaitTime ();
public void setSubPartitionCreationWaitTime (long newWaitTime);
public long getBeanCleaningDelay ();
public void setBeanCleaningDelay (long newDelay);
public void setTopologyComputerClassName (String newName);
public String getTopologyComputerClassName ();
}
1.1
jbossmx/src/main/org/jboss/ha/hasessionstate/server/HASessionStateTopologyComputer.java
Index: HASessionStateTopologyComputer.java
===================================================================
/*
* JBoss, the OpenSource J2EE WebOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.ha.hasessionstate.server;
import java.util.ArrayList;
import org.jboss.ha.framework.interfaces.SubPartitionInfo;
import org.jboss.ha.framework.interfaces.SubPartitionsInfo;
/**
* Helper interface to which is delegated new HASessionState topology computation
*
* @see HASessionState, HASessionStateTopologyComputerImpl
* @author [EMAIL PROTECTED]
* @version $Revision: 1.1 $
*
* <p><b>Revisions:</b><br>
*/
public interface HASessionStateTopologyComputer {
public void init (String sessionStateName, long nodesPerSubPartition);
public void start ();
public SubPartitionsInfo computeNewTopology (SubPartitionsInfo currentTopology,
ArrayList newReplicants);
}
1.1
jbossmx/src/main/org/jboss/ha/hasessionstate/server/HASessionStateTopologyComputerImpl.java
Index: HASessionStateTopologyComputerImpl.java
===================================================================
/*
* JBoss, the OpenSource J2EE WebOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.ha.hasessionstate.server;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.jboss.ha.framework.interfaces.SubPartitionInfo;
import org.jboss.ha.framework.interfaces.SubPartitionsInfo;
/**
* Default implementation of HASessionStateTopologyComputer
*
* @see HASessionState
* @author [EMAIL PROTECTED]
* @version $Revision: 1.1 $
*
* <p><b>Revisions:</b><br>
*/
public class HASessionStateTopologyComputerImpl implements
HASessionStateTopologyComputer
{
protected long nodesPerSubPartition = 0;
protected String sessionStateIdentifier = null;
/** Creates new HASessionStateTopologyComputerImpl */
public HASessionStateTopologyComputerImpl ()
{
}
public void init (String sessionStateName, long nodesPerSubPartition)
{
this.sessionStateIdentifier = sessionStateName;
this.nodesPerSubPartition = nodesPerSubPartition;
}
public void start () {}
public SubPartitionsInfo computeNewTopology (SubPartitionsInfo currentTopology,
ArrayList newReplicants)
{
if (newReplicants.size () < 1)
currentTopology.partitions = null;
else if (newReplicants.size () == 1)
{
// we are alone! Are we already in a partition? If this is the case, we do
not change!
//
if (currentTopology.partitions != null)
currentTopology = computeCompatibleComposition (currentTopology,
newReplicants);
else
{
SubPartitionInfo aPartition = new SubPartitionInfo ();
aPartition.subPartitionName = getSubPartitionName (currentTopology);
aPartition.memberNodeNames.add (newReplicants.get (0));
SubPartitionInfo[] thePartition =
{ aPartition };
currentTopology.partitions = thePartition;
}
}
else if (currentTopology == null || currentTopology.partitions == null)
// this is the first time we will have to decide of a spliting
//
currentTopology = computerFirstComposition (currentTopology, newReplicants);
else
// There is a spliting already in place: we will need to take care of it in
order to minimize group changes
// i.e. state transfer that will occur from these changes
//
currentTopology = computeCompatibleComposition (currentTopology,
newReplicants);
return currentTopology;
}
protected SubPartitionsInfo computerFirstComposition (SubPartitionsInfo
splitingInfo, ArrayList replicants)
{
int i=0;
String rep = null;
ArrayList newConfig = new ArrayList ();
SubPartitionInfo aPartition = null;
int grpNumber = 0;
// Build groups sequentially
//
for (Iterator reps = replicants.iterator (); reps.hasNext (); i++)
{
rep = (String)reps.next ();
if ( (i%nodesPerSubPartition) == 0 )
{
grpNumber++;
aPartition = new SubPartitionInfo ();
aPartition.subPartitionName = getSubPartitionName (splitingInfo);
newConfig.add (aPartition);
}
aPartition.memberNodeNames.add (rep);
}
// we don't like singleton nodes for HA...
//
if (aPartition.memberNodeNames.size () == 1)
{
rep = (String) aPartition.memberNodeNames.get (0); // get singleton info
newConfig.remove (grpNumber-1); // remove last singleton group
aPartition = (SubPartitionInfo)(newConfig.get (grpNumber-1)); // access
last built group
aPartition.memberNodeNames.add (rep); // add singleton to last built group
}
SubPartitionInfo[] newSpliting = new SubPartitionInfo[1];
newSpliting = (SubPartitionInfo[]) newConfig.toArray (newSpliting);
splitingInfo.partitions = newSpliting;
return splitingInfo;
}
protected SubPartitionsInfo computeCompatibleComposition (SubPartitionsInfo
splitingInfo, ArrayList replicants)
{
// In a first step, we purge the current spliting to remove dead members
//
SubPartitionInfo[] newSpliting = null;
ArrayList newSubParts = new ArrayList ();
for (int i=0; i<splitingInfo.partitions.length; i++)
{
SubPartitionInfo currentSubPart = splitingInfo.partitions[i];
SubPartitionInfo newCurrent = null;
Iterator iter = currentSubPart.memberNodeNames.iterator ();
while (iter.hasNext ())
{
String node = (String)iter.next ();
if (replicants.contains (node))
{
if (newCurrent == null)
{
newCurrent = (SubPartitionInfo)currentSubPart.clone ();
newCurrent.memberNodeNames.clear ();
}
newCurrent.memberNodeNames.add (node);
}
}
if (newCurrent != null)
newSubParts.add (newCurrent);
}
// we now create a list of new nodes that are not yet part of any group
//
Iterator iter = replicants.iterator ();
ArrayList newMembersNotInAGroup = new ArrayList ();
while (iter.hasNext ())
{
boolean found = false;
String aMember = (String)iter.next ();
Iterator iterNewSubPart = newSubParts.iterator ();
while (iterNewSubPart.hasNext () && !found)
if (((SubPartitionInfo)iterNewSubPart.next ()).memberNodeNames.contains
(aMember))
found = true;
if (!found)
newMembersNotInAGroup.add (aMember);
}
iter = null;
// we now have purged our current sub-partition structure from its dead members
// we now check if some sub-partitions need to be merged to remove singleton
groups
// or if there is a group with n>(nodesPerSubPartition) that may be reduced to
its ideal size
//
// we remove elements that are less than the group size and put them in a new
sorted list
//
int curs = 0;
ArrayList smallerGroups = new ArrayList ();
ArrayList correctlySizedGroups = new ArrayList ();
ArrayList biggerGroups = new ArrayList ();
for (int i=0; i<newSubParts.size (); i++)
{
int groupSize = ((SubPartitionInfo)newSubParts.get
(i)).memberNodeNames.size ();
if (groupSize < this.nodesPerSubPartition)
smallerGroups.add (newSubParts.get (i));
else if (groupSize > this.nodesPerSubPartition)
biggerGroups.add (newSubParts.get (i));
else
correctlySizedGroups.add (newSubParts.get (i));
}
// for our algo, we need to sort smallerGroups
//
java.util.Collections.sort (smallerGroups);
//
// Our algo is not perfect and could, for example, take in account, the actual
group load in order to minimize
// the synchronization time
//
// 1st step: we place newly started nodes (not yet part of a group) in
smallerGroups
// by first feeding small groups
//
iter = newMembersNotInAGroup.iterator ();
while (iter.hasNext ())
{
String member = (String)iter.next ();
SubPartitionInfo target = null;
if (smallerGroups.size () > 0)
{
target = (SubPartitionInfo)smallerGroups.get (0); // array is sorted
target.memberNodeNames.add (member);
if (target.memberNodeNames.size () == this.nodesPerSubPartition)
{
// we have a complete sub-partition, we change its owning group
//
smallerGroups.remove (0);
correctlySizedGroups.add (target);
}
}
else
{
// we create an singleton group
//
target = new SubPartitionInfo ();
target.setIsNewGroup ();
target.subPartitionName = getSubPartitionName (splitingInfo);
target.memberNodeNames.add (member);
smallerGroups.add (target);
java.util.Collections.sort (smallerGroups);
}
}
// 2nd step: we reduce the size of any too-big sub-partition (biggerGroups)
// by removing the last component and feeding elements in smallerGroups
// If smallerGroups is empty, we don't modify biggerGroups (minimize
// involved state transfer)
//
iter = biggerGroups.iterator ();
while (iter.hasNext ())
{
SubPartitionInfo big = (SubPartitionInfo)iter.next ();
if (smallerGroups.size () > 0)
{
String member = (String)big.memberNodeNames.get
(big.memberNodeNames.size ()-1); // get last one
SubPartitionInfo target = null;
target = (SubPartitionInfo)smallerGroups.get (0); // array is sorted
target.memberNodeNames.add (member);
big.memberNodeNames.remove (big.memberNodeNames.size () -1);
if (target.memberNodeNames.size () == this.nodesPerSubPartition)
{
// we have a complete sub-partition, we change its owning group
//
smallerGroups.remove (0);
correctlySizedGroups.add (target);
}
}
}
// biggerGroups is now processed, we can move it to the correctly sized group
//
correctlySizedGroups.addAll (biggerGroups);
// 3rd step: we now try to merge sub-partitions belonging to smallerGroups to
form bigger groups (up to the
// max size of a sub-partition). We travel in descending order to keep max
granularity when forming groups
//
boolean thirdStepFinished = (smallerGroups.size () == 0);
while (!thirdStepFinished)
{
//thirdStepFinished = (smallerGroups.size () == 0);
SubPartitionInfo current = (SubPartitionInfo)smallerGroups.get
(smallerGroups.size ()-1);
for (int i = smallerGroups.size ()-2; i >= 0; i--)
{
// test if the merge is possible
//
SubPartitionInfo merger = (SubPartitionInfo)smallerGroups.get (i);
if ((merger.memberNodeNames.size () + current.memberNodeNames.size ())
<= this.nodesPerSubPartition)
{
// it is possible to merge both
//
current.merge (merger);
smallerGroups.remove (i);
}
// we check if we need to go further or not
//
if (current.memberNodeNames.size () == this.nodesPerSubPartition)
break;
}
if (current.memberNodeNames.size () > 1)
{
// we only move non-singleton groups
//
smallerGroups.remove (smallerGroups.size ()-1);
correctlySizedGroups.add (current);
}
thirdStepFinished = ( (smallerGroups.size () == 0) ||
((smallerGroups.size () == 1) && ( ((SubPartitionInfo)smallerGroups.get
(0)).memberNodeNames.size () == 1)) );
}
// 4th step: if smallerGroups is not empty, it means that we have a singleton.
In that case,
// we merge it with the smallest group we can find.
//
if (smallerGroups.size () > 0)
{
if (correctlySizedGroups.size ()>0)
{
java.util.Collections.sort (correctlySizedGroups);
SubPartitionInfo merger = (SubPartitionInfo)smallerGroups.get (0);
SubPartitionInfo master = (SubPartitionInfo)correctlySizedGroups.get (0);
master.merge (merger);
}
else
{
// we have a single singleton group!
//
correctlySizedGroups.add (smallerGroups.get (0));
}
}
// we now commit our new splitting. All members will consequently receive a
message indicating
// that the spliting has changed and act accordingly
//
newSpliting = new SubPartitionInfo[1];
newSpliting = (SubPartitionInfo[])correctlySizedGroups.toArray (newSpliting);
splitingInfo.partitions = newSpliting;
return splitingInfo;
}
protected String getSubPartitionName (SubPartitionsInfo manager)
{
return this.sessionStateIdentifier + "-Group-" + manager.getNextGroupId ();
}
// testing of the above algo... can be commented...
//
/*
public static void main (String[] args)
{
HASessionStateTopologyComputerImpl tmp = new
HASessionStateTopologyComputerImpl ();
tmp.init ("test", 2);
tmp.start ();
SubPartitionsInfo splitInfo = new SubPartitionsInfo ();
ArrayList replic = new ArrayList ();
ArrayList parts = new ArrayList ();
splitInfo.partitions = new SubPartitionInfo[1];
//parts.add (new SubPartitionInfo ("SP1", helper_String ("ABC")));
//parts.add (new SubPartitionInfo ("SP2", helper_String ("DEF")));
//parts.add (new SubPartitionInfo ("SP3", helper_String ("GHI")));
parts.add (new SubPartitionInfo ("SP4", helper_String ("AB")));
parts.add (new SubPartitionInfo ("SP5", helper_String ("EF")));
//parts.add (new SubPartitionInfo ("SP1", helper_String ("Axy")));
//parts.add (new SubPartitionInfo ("SP2", helper_String ("Bmn")));
//parts.add (new SubPartitionInfo ("SP3", helper_String ("CDE")));
//parts.add (new SubPartitionInfo ("SP4", helper_String ("FGHI")));
splitInfo.partitions = (SubPartitionInfo[])parts.toArray
(splitInfo.partitions);
replic = new ArrayList (java.util.Arrays.asList (helper_String ("AEF") ));
System.out.println (replic);
System.out.println (splitInfo);
splitInfo = tmp.computeCompatibleComposition (splitInfo, replic);
System.out.println (splitInfo);
}
private static String[] helper_String (String letters)
{
String[] tabl = new String[letters.length ()];
for (int i=0; i<letters.length ();i++)
tabl[i]=letters.substring (i, i+1);
return tabl;
}
*/
}
1.1
jbossmx/src/main/org/jboss/ha/hasessionstate/server/PackagedSessionImpl.java
Index: PackagedSessionImpl.java
===================================================================
/*
* JBoss, the OpenSource J2EE WebOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.ha.hasessionstate.server;
import org.jboss.ha.hasessionstate.interfaces.PackagedSession;
import java.io.Serializable;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
/**
* Default implementation of PackagedSession
*
* @see PackagedSession, HASessionStateImpl
* @author [EMAIL PROTECTED]
* @version $Revision: 1.1 $
*
* <p><b>Revisions:</b><br>
*/
public class PackagedSessionImpl implements PackagedSession
{
protected byte[] state = null;
protected long versionId = 0;
protected String owner = null;
protected Serializable key = null;
protected transient long lastModificationTimeInVM = System.currentTimeMillis ();
public PackagedSessionImpl () { }
public PackagedSessionImpl (Serializable key, byte[] state, String owner)
{
this();
this.key = key;
this.setState (state);
this.owner = owner;
}
public byte[] getState ()
{
return this.state;
}
public boolean setState (byte[] state)
{
this.lastModificationTimeInVM = System.currentTimeMillis ();
if (isStateIdentical (state))
return true;
else
{
this.state = state;
this.versionId++;
return false;
}
}
public boolean isStateIdentical (byte[] state)
{
return java.util.Arrays.equals (state, this.state);
}
public void update (PackagedSession clone)
{
this.state = (byte[])clone.getState().clone();
this.versionId = clone.getVersion ();
this.owner = clone.getOwner ();
}
public String getOwner ()
{ return this.owner; }
public void setOwner (String owner)
{ this.owner = owner; }
public long getVersion ()
{ return this.versionId; }
public Serializable getKey ()
{ return this.key; }
public void setKey (Serializable key)
{ this.key = key; }
public long unmodifiedExistenceInVM ()
{
return this.lastModificationTimeInVM;
}
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development