[jboss-cvs] JBossAS SVN: r69526 - trunk/cluster/src/main/org/jboss/ha/singleton.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Feb 1 05:19:47 EST 2008
Author: galder.zamarreno at jboss.com
Date: 2008-02-01 05:19:47 -0500 (Fri, 01 Feb 2008)
New Revision: 69526
Modified:
trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicy.java
trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicyBase.java
trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicySimple.java
trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonSupport.java
trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonSupportMBean.java
Log:
[JBAS-4919] Base ha singleton election policy now requests the list of cluster nodes where the singleton can deploy and elects from there. HAPartition and singleton name for the election policy are now set on creation of HASingletonSupport. Set/Get operations for singleton object have been removed.
Modified: trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicy.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicy.java 2008-02-01 07:58:17 UTC (rev 69525)
+++ trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicy.java 2008-02-01 10:19:47 UTC (rev 69526)
@@ -3,27 +3,45 @@
import org.jboss.ha.framework.interfaces.ClusterNode;
import org.jboss.ha.framework.interfaces.HAPartition;
+/**
+ * HASingletonElectionPolicy.
+ *
+ * @author <a href="mailto:Alex.Fu at novell.com">Alex Fu</a>
+ * @author Brian Stansberry
+ * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
+ */
public interface HASingletonElectionPolicy
{
+ /**
+ * Called by the HASingleton, during the create service phase, to set the
+ * name with which the singleton is registered with the HAPartition.
+ *
+ * @param serviceName the singleton service name.
+ */
+ void setSingletonName(String serviceName);
/**
- * Called by the HASingleton to provide the election policy a reference to
- * itself. A policy that was designed to elect a particular kind of singleton
- * could downcast this object to a particular type and then access the
- * singleton for state information needed for the election decision.
+ * Get the singleton name for this election policy.
+ *
+ * @return the singleton service name.
*/
- void setManagedSingleton(Object singleton);
+ String getSingletonName();
- Object getManagedSingleton();
-
/**
- * Sets the HAPartition; from this the election policy can gain
- * access to the DistributedReplicantManager for tracking the
- * deployment topology for the singleton service and to the HAPartition
- * for making group RPC calls.
+ * Called by the HASingleton, during the create service phase to sets the
+ * HAPartition; from this the election policy can gain access to the
+ * DistributedReplicantManager for tracking the deployment topology for
+ * the singleton service and to the HAPartition for making group RPC calls.
*/
void setHAPartition(HAPartition partition);
+ /**
+ * Get the HA partition where the singleton is deployed, which can be used,
+ * amongst other things, to find out which nodes is the singleton available
+ * in.
+ *
+ * @return the HA partition.
+ */
HAPartition getHAPartition();
/**
@@ -34,33 +52,16 @@
*/
void setPreferredMaster(String node);
+ /**
+ * Get the preferred master node.
+ *
+ * @return preferred master node in ip_address:port_number format.
+ */
String getPreferredMaster();
/**
* Return the elected master node.
* @return the master node
*/
- ClusterNode pickSingleton();
-
- /**
- * Given the HAPartition, return the elected master node.
- * @param partition
- * @return the master node
- */
- ClusterNode pickSingleton(HAPartition partition);
-
- /**
- * Conducts an election and returns whether the managed service
- * is the master, based on the current view of partition.
- * @return true only if the managed service is the master
- */
- boolean isElectedMaster();
-
- /**
- * Given the HAPartition, return whether the managed service is the master.
- * @param partition
- * @return true only if the managed service is the master
- */
- boolean isElectedMaster(HAPartition partition);
-
+ ClusterNode elect();
}
\ No newline at end of file
Modified: trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicyBase.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicyBase.java 2008-02-01 07:58:17 UTC (rev 69525)
+++ trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicyBase.java 2008-02-01 10:19:47 UTC (rev 69526)
@@ -21,6 +21,8 @@
*/
package org.jboss.ha.singleton;
+import java.util.List;
+
import org.jboss.ha.framework.interfaces.ClusterNode;
import org.jboss.ha.framework.interfaces.HAPartition;
import org.jboss.system.ServiceMBeanSupport;
@@ -30,50 +32,52 @@
* the master node to run certain HASingleton service.
*
* @author <a href="mailto:afu at novell.com">Alex Fu</a>
+ * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
* @version $Revision$
*/
public abstract class HASingletonElectionPolicyBase
extends ServiceMBeanSupport
implements HASingletonElectionPolicyMBean
{
- private Object mManagedSingleton;
+ private String mSingletonName;
private HAPartition mPartition;
private String mPreferredMaster;
/**
- * @see HASingletonElectionPolicyMBean#setManagedSingleton(Object)
+ * @see HASingletonElectionPolicy#setSingletonName
*/
- public void setManagedSingleton(Object singleton)
+ public void setSingletonName(String singletonName)
{
- this.mManagedSingleton = singleton;
- }
-
+ this.mSingletonName = singletonName;
+ log.debug("set singleton name to " + this.mSingletonName);
+ }
+
/**
- * @see HASingletonElectionPolicyMBean#getManagedSingleton()
+ * @see HASingletonElectionPolicy#getSingletonName
*/
- public Object getManagedSingleton()
+ public String getSingletonName()
{
- return this.mManagedSingleton;
+ return this.mSingletonName;
}
-
+
/**
- * @see HASingletonElectionPolicyMBean#setPreferredMaster(ClusterNode)
+ * @see HASingletonElectionPolicy#setPreferredMaster(ClusterNode)
*/
public void setPreferredMaster(String node)
{
- this.mPreferredMaster = node;
+ this.mPreferredMaster = node;
}
/**
- * @see HASingletonElectionPolicyMBean#getPreferredMaster()
+ * @see HASingletonElectionPolicy#getPreferredMaster()
*/
public String getPreferredMaster()
{
- return this.mPreferredMaster;
+ return this.mPreferredMaster;
}
/**
- * @see HASingletonElectionPolicyMBean#setHAPartition(HAPartition)
+ * @see HASingletonElectionPolicy#setHAPartition(HAPartition)
*/
public void setHAPartition(HAPartition partition)
{
@@ -81,65 +85,56 @@
}
/**
- * @see HASingletonElectionPolicyMBean#getHAPartition()
+ * @see HASingletonElectionPolicy#getHAPartition()
*/
public HAPartition getHAPartition()
{
return this.mPartition;
}
-
+
/**
- * @see HASingletonElectionPolicyMBean#isElectedMaster()
+ * @see HASingletonElectionPolicy#elect()
*/
- public boolean isElectedMaster()
+ public ClusterNode elect()
{
- if (null == this.mPartition)
- throw new IllegalStateException("HAPartition is not set");
+ List<ClusterNode> candidates = getCandidates();
+ if (candidates == null)
+ {
+ throw new IllegalStateException("list of cluster node candidates where to run the singleton cannot be null");
+ }
- return pickSingleton().equals(this.mPartition.getClusterNode());
+ // If preferred master is defined and contained in cluster, return it
+ if (null != this.mPreferredMaster) {
+ for (ClusterNode node : candidates)
+ {
+ if (node.getName().equals(this.mPreferredMaster))
+ {
+ return node;
+ }
+ }
+ }
+
+ // Preferred master is not available, election policy is in effect
+ return elect(candidates);
}
-
- /**
- * @see HASingletonElectionPolicyMBean#isElectedMaster(HAPartition)
- */
- public boolean isElectedMaster(HAPartition partition)
- {
- if (null == partition)
- throw new IllegalStateException("parameter cannot be null");
-
- return pickSingleton(partition).equals(partition.getClusterNode());
- }
/**
- * @see HASingletonElectionPolicyMBean#pickSingleton()
+ * Get the list of candidate {@link ClusterNode} instances where the
+ * singleton could be deployed.
+ *
+ * @return List of {@link ClusterNode} instances.
*/
- public ClusterNode pickSingleton()
+ protected List<ClusterNode> getCandidates()
{
- if (null == this.mPartition)
- throw new IllegalStateException("HAPartition is not set");
-
- return pickSingleton(this.mPartition);
+ return getHAPartition().getDistributedReplicantManager().lookupReplicantsNodes(getSingletonName());
}
/**
- * @see HASingletonElectionPolicyMBean#pickSingleton(HAPartition)
+ * Given a List of candidate {@link ClusterNode} instances, return the
+ * elected node where the singleton should run.
+ *
+ * @param candidates List of {@link ClusterNode}.
+ * @return {@link ClusterNode} instance.
*/
- public ClusterNode pickSingleton(HAPartition partition)
- {
- if (null == partition)
- throw new IllegalStateException("parameter cannot be null");
-
- // If preferred master is defined and contained in cluster, return it
- if (null != this.mPreferredMaster) {
- ClusterNode[] nodes = partition.getClusterNodes();
- for (int i = 0; i < nodes.length; i++) {
- if (nodes[i].getName().equals(this.mPreferredMaster))
- return nodes[i];
- }
- }
- // Preferred master is not available, election policy is in effect
- return elect(partition);
- }
-
- protected abstract ClusterNode elect(HAPartition partition);
+ protected abstract ClusterNode elect(List<ClusterNode> candidates);
}
Modified: trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicySimple.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicySimple.java 2008-02-01 07:58:17 UTC (rev 69525)
+++ trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicySimple.java 2008-02-01 10:19:47 UTC (rev 69526)
@@ -22,8 +22,9 @@
package org.jboss.ha.singleton;
+import java.util.List;
+
import org.jboss.ha.framework.interfaces.ClusterNode;
-import org.jboss.ha.framework.interfaces.HAPartition;
/**
* A simple concrete policy service that decides which node in the cluster should be
@@ -46,6 +47,7 @@
* <attribute name="Position">2</attribute>
*
* @author <a href="mailto:Alex.Fu at novell.com">Alex Fu</a>
+ * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
* @version $Revision$
*/
public class HASingletonElectionPolicySimple
@@ -71,13 +73,11 @@
return this.mPosition;
}
- protected ClusterNode elect(HAPartition partition)
+ protected ClusterNode elect(List<ClusterNode> candidates)
{
- ClusterNode[] nodes = partition.getClusterNodes();
-
- int size = nodes.length;
+ int size = candidates.size();
int remainder = ((this.mPosition % size) + size) % size;
- return nodes[remainder];
+ return candidates.get(remainder);
}
}
Modified: trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonSupport.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonSupport.java 2008-02-01 07:58:17 UTC (rev 69525)
+++ trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonSupport.java 2008-02-01 10:19:47 UTC (rev 69526)
@@ -33,6 +33,7 @@
* @author <a href="mailto:ivelin at apache.org">Ivelin Ivanov</a>
* @author <a href="mailto:scott.stark at jboss.org">Scott Stark</a>
* @author <a href="mailto:dimitris at jboss.org">Dimitris Andreadis</a>
+ * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
* @version $Revision$
*/
public class HASingletonSupport extends HAServiceMBeanSupport
@@ -164,8 +165,8 @@
public void partitionTopologyChanged(List newReplicants, int newViewID, boolean merge)
{
boolean isElectedNewMaster;
- if (this.mElectionPolicyMB != null)
- isElectedNewMaster = this.mElectionPolicyMB.isElectedMaster(this.getHAPartition());
+ if (mElectionPolicyMB != null)
+ isElectedNewMaster = mElectionPolicyMB.elect().equals(getHAPartition().getClusterNode());
else
isElectedNewMaster = isDRMMasterReplica();
@@ -272,6 +273,18 @@
startNewMaster();
}
+ @Override
+ protected void createService() throws Exception
+ {
+ super.createService();
+
+ if (mElectionPolicyMB != null)
+ {
+ mElectionPolicyMB.setHAPartition(getHAPartition());
+ mElectionPolicyMB.setSingletonName(getServiceHAName());
+ }
+ }
+
// Private -------------------------------------------------------
private void sendLocalNotification(String type)
Modified: trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonSupportMBean.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonSupportMBean.java 2008-02-01 07:58:17 UTC (rev 69525)
+++ trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonSupportMBean.java 2008-02-01 10:19:47 UTC (rev 69526)
@@ -25,13 +25,16 @@
* The management interface for the singleton support service.
*
* @author <a href="mailto:Alex.Fu at novell.com">Alex Fu</a>
+ * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
* @version $Revision$
*/
public interface HASingletonSupportMBean extends HASingletonMBean
{
/**
* Sets the policy used to determine which cluster node will
- * become the master when the service topology changes.
+ * become the master when the service topology changes. Injection of common
+ * attributes into the election policy should be done during createService()
+ * method call.
*
* @param policy the policy. Can be <code>null</code>.
*/
More information about the jboss-cvs-commits
mailing list