[jboss-user] [Clustering/JBoss] - HASingletonElectionPolicy

smeng@unique do-not-reply at jboss.com
Tue Jan 8 13:03:20 EST 2008


I have setup 2 nodes (say Node A and B) in a cluster using Jboss 4.2.2 GA.

My requirement is:
1. Node A is set as the preferred master. If both nodes are running, Node A will always become the master i.e. in the event Node B was started before Node A, it will stop the service and Node A will take over running as the master. 
2. If the master node goes down, then the next available node in the cluster becomes the master.

My problem is:
When Nodes A & B are running, Node A as master, and Node A is properly shutdown, Node B still detects Node A in the cluster view and continues to elect Node A as the master rather than taking over.

Here is my HASingletonElectionPolicy implementation:
package com.unique.ha.singleton;
  | 
  | import org.apache.commons.logging.Log;
  | import org.apache.commons.logging.LogFactory;
  | import org.jboss.ha.framework.interfaces.ClusterNode;
  | import org.jboss.ha.framework.interfaces.HAPartition;
  | import org.jboss.ha.singleton.HASingletonElectionPolicySimple;
  | 
  | public class HASingletonElectionPolicyUnique extends HASingletonElectionPolicySimple 
  | implements HASingletonElectionPolicyUniqueMBean {
  | 
  | 	private String preferredMasterAddress;
  | 	
  | 	private static final Log log = LogFactory.getLog(HASingletonElectionPolicyUnique.class);
  | 	
  | 	public void setPreferredMasterAddress(String preferredMasterAddress) {
  | 
  | 		this.preferredMasterAddress = preferredMasterAddress;
  | 	}
  | 
  | 	public String getPreferredMasterAddress() {
  | 
  | 		return this.preferredMasterAddress;
  | 	}
  | 	
  | 	public ClusterNode pickSingleton() {
  | 
  | 		return pickSingleton(getHAPartition());
  | 	}
  | 
  | 	public ClusterNode pickSingleton(HAPartition partition) {
  | 		
  | 		log.info( "Running HASingletonElectionPolicyUnique..." );
  | 
  | 		ClusterNode[] nodes = partition.getClusterNodes();
  | 		log.info( "CurrentView for " + partition.getPartitionName() + " is " + partition.getCurrentView().toString() );
  | 		
  | 		if( this.preferredMasterAddress != null ) {
  | 			
  | 			for ( int x=0; x < nodes.length; x++ ) {
  | 				
  | 				ClusterNode node = nodes[x];
  | 			
  | 				if ( node.getIpAddress().getHostAddress().equals( this.preferredMasterAddress ) ) {
  | 
  | 					log.info("Set Master Status to preferred master address " + this.preferredMasterAddress);
  | 					return node;
  | 				}
  | 			}	
  | 		}
  | 		
  | 		log.warn("Unable to select preferred Master at address " + 
  | 				this.preferredMasterAddress + ". Will select next available node.");
  | 		
  | 		return super.pickSingleton(partition);		
  | 	}
  | 
  | 	
  | }
  | 

jboss-service.xml 
<server>
  | 
  |    <mbean code="com.unique.ha.singleton.HASingletonElectionPolicyUnique"
  |           name="com.unique.ha:name=HASingletonElectionPolicyUnique">
  |      <attribute name="PreferredMasterAddress">${unique.ha.preferred_master_address}</attribute>
  |    </mbean>
  | 
  | 	<mbean code="com.unique.system.mbean.MyService"
  | 		name="com.unique.system.mbean:name=MyService">
  | 	</mbean>
  | 
  | 	<mbean code="org.jboss.ha.singleton.HASingletonController"
  | 		name="com.unique.system.mbean:service=MyService-HASingletonController">
  | 		<depends>jboss:service=${jboss.partition.name}</depends>
  |        		<depends optional-attribute-name="ElectionPolicy" proxy-type="attribute">com.unique.ha:name=HASingletonElectionPolicyUnique</depends>
  | 		<depends optional-attribute-name="TargetName">com.unique.system.mbean:name=MyService</depends>
  | 	    	<attribute name="PartitionName">${jboss.partition.name}</attribute>
  | 		<attribute name="TargetStartMethod">startSingleton</attribute>
  | 		<attribute name="TargetStopMethod">stopSingleton</attribute>
  | 	</mbean>
  | 
  | </server>

Thanks
Meng

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4118011#4118011

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4118011



More information about the jboss-user mailing list