1 22 package org.jboss.ha.singleton; 23 24 import java.util.List ; 25 26 import javax.management.Notification ; 27 28 import org.jboss.ha.jmx.HAServiceMBeanSupport; 29 30 38 public class HASingletonSupport extends HAServiceMBeanSupport 39 implements HASingletonMBean, HASingleton 40 { 41 43 private boolean isMasterNode = false; 44 private HASingletonElectionPolicyMBean mElectionPolicyMB = null; 45 46 48 51 public HASingletonSupport() 52 { 53 } 55 56 58 63 public boolean isMasterNode() 64 { 65 return isMasterNode; 66 } 67 68 71 public void setHASingletonElectionPolicyMBean(HASingletonElectionPolicyMBean mb) 72 { 73 this.mElectionPolicyMB = mb; 74 } 75 76 79 public HASingletonElectionPolicyMBean getHASingletonElectionPolicyMBean() 80 { 81 return this.mElectionPolicyMB; 82 } 83 84 86 100 public void startSingleton() 101 { 102 if (log.isDebugEnabled()) 103 log.debug("startSingleton() : elected for master singleton node"); 104 105 } 107 108 116 public void stopSingleton() 117 { 118 if (log.isDebugEnabled()) 119 log.debug("stopSingleton() : another node in the partition (if any) is elected for master"); 120 121 } 123 124 125 132 public void partitionTopologyChanged(List newReplicants, int newViewID) 133 { 134 boolean isElectedNewMaster; 135 if (this.mElectionPolicyMB != null) 136 isElectedNewMaster = this.mElectionPolicyMB.isElectedMaster(this.getPartition()); 137 else 138 isElectedNewMaster = isDRMMasterReplica(); 139 140 if (log.isDebugEnabled()) 141 { 142 log.debug("partitionTopologyChanged, isElectedNewMaster=" + isElectedNewMaster 143 + ", isMasterNode=" + isMasterNode + ", viewID=" + newViewID); 144 } 145 146 if (isElectedNewMaster && isMasterNode) 148 { 149 return; 150 } 151 else if (isElectedNewMaster && !isMasterNode) 153 { 154 makeThisNodeMaster(); 155 } 156 else if (isMasterNode == true) 158 { 159 _stopOldMaster(); 160 } 161 } 162 163 167 public void _stopOldMaster() 168 { 169 log.debug("_stopOldMaster, isMasterNode=" + isMasterNode); 170 171 try 172 { 173 if (isMasterNode == true) 176 { 177 isMasterNode = false; 178 179 sendLocalNotification(HASINGLETON_STOPPING_NOTIFICATION); 181 182 stopSingleton(); 184 185 sendLocalNotification(HASINGLETON_STOPPED_NOTIFICATION); 187 } 188 } 189 catch (Exception ex) 190 { 191 log.error( 192 "_stopOldMaster failed. Will still try to start new master. " + 193 "You need to examine the reason why the old master wouldn't stop and resolve it. " + 194 "It is bad that the old singleton may still be running while we are starting a new one, " + 195 "so you need to resolve this ASAP.", ex); 196 } 197 } 198 199 201 protected void makeThisNodeMaster() 202 { 203 try 204 { 205 207 callAsyncMethodOnPartition("_stopOldMaster", new Object [0], new Class [0]); 211 212 isMasterNode = true; 213 214 sendLocalNotification(HASINGLETON_STARTING_NOTIFICATION); 216 217 startSingleton(); 219 220 sendLocalNotification(HASINGLETON_STARTED_NOTIFICATION); 222 } 223 catch (Exception ex) 224 { 225 log.error("_stopOldMaster failed. New master singleton will not start.", ex); 226 } 227 } 228 229 231 private void sendLocalNotification(String type) 232 { 233 Notification n = new Notification (type, this, getNextNotificationSequenceNumber()); 234 super.sendNotificationToLocalListeners(n); 235 } 236 } 237 | Popular Tags |