fix relinquishment behavior in SimpleMastershipStore
Change-Id: Ibc9eeae397b7acc9e08cc569f9c8a642557bf4f9
Showing
2 changed files
with
26 additions
and
4 deletions
| ... | @@ -68,15 +68,15 @@ public class MastershipManagerTest { | ... | @@ -68,15 +68,15 @@ public class MastershipManagerTest { |
| 68 | 68 | ||
| 69 | @Test | 69 | @Test |
| 70 | public void relinquishMastership() { | 70 | public void relinquishMastership() { |
| 71 | - //no backups - should turn to standby and no master for device | 71 | + //no backups - should just turn to NONE for device. |
| 72 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); | 72 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 73 | assertEquals("wrong role:", MASTER, mgr.getLocalRole(DEV_MASTER)); | 73 | assertEquals("wrong role:", MASTER, mgr.getLocalRole(DEV_MASTER)); |
| 74 | mgr.relinquishMastership(DEV_MASTER); | 74 | mgr.relinquishMastership(DEV_MASTER); |
| 75 | assertNull("wrong master:", mgr.getMasterFor(DEV_OTHER)); | 75 | assertNull("wrong master:", mgr.getMasterFor(DEV_OTHER)); |
| 76 | - assertEquals("wrong role:", STANDBY, mgr.getLocalRole(DEV_MASTER)); | 76 | + assertEquals("wrong role:", NONE, mgr.getLocalRole(DEV_MASTER)); |
| 77 | 77 | ||
| 78 | //not master, nothing should happen | 78 | //not master, nothing should happen |
| 79 | - mgr.setRole(NID_LOCAL, DEV_OTHER, STANDBY); | 79 | + mgr.setRole(NID_LOCAL, DEV_OTHER, NONE); |
| 80 | mgr.relinquishMastership(DEV_OTHER); | 80 | mgr.relinquishMastership(DEV_OTHER); |
| 81 | assertNull("wrong role:", mgr.getMasterFor(DEV_OTHER)); | 81 | assertNull("wrong role:", mgr.getMasterFor(DEV_OTHER)); |
| 82 | 82 | ... | ... |
| ... | @@ -226,12 +226,34 @@ public class SimpleMastershipStore | ... | @@ -226,12 +226,34 @@ public class SimpleMastershipStore |
| 226 | break; | 226 | break; |
| 227 | } | 227 | } |
| 228 | } | 228 | } |
| 229 | + backups.remove(backup); | ||
| 229 | return backup; | 230 | return backup; |
| 230 | } | 231 | } |
| 231 | 232 | ||
| 232 | @Override | 233 | @Override |
| 233 | public MastershipEvent relinquishRole(NodeId nodeId, DeviceId deviceId) { | 234 | public MastershipEvent relinquishRole(NodeId nodeId, DeviceId deviceId) { |
| 234 | - return setStandby(nodeId, deviceId); | 235 | + MastershipRole role = getRole(nodeId, deviceId); |
| 236 | + synchronized (this) { | ||
| 237 | + switch (role) { | ||
| 238 | + case MASTER: | ||
| 239 | + NodeId backup = reelect(nodeId); | ||
| 240 | + backups.remove(nodeId); | ||
| 241 | + if (backup == null) { | ||
| 242 | + masterMap.remove(deviceId); | ||
| 243 | + } else { | ||
| 244 | + masterMap.put(deviceId, backup); | ||
| 245 | + termMap.get(deviceId).incrementAndGet(); | ||
| 246 | + return new MastershipEvent(MASTER_CHANGED, deviceId, | ||
| 247 | + new RoleInfo(backup, Lists.newLinkedList(backups))); | ||
| 248 | + } | ||
| 249 | + case STANDBY: | ||
| 250 | + backups.remove(nodeId); | ||
| 251 | + case NONE: | ||
| 252 | + default: | ||
| 253 | + log.warn("unknown Mastership Role {}", role); | ||
| 254 | + } | ||
| 255 | + } | ||
| 256 | + return null; | ||
| 235 | } | 257 | } |
| 236 | 258 | ||
| 237 | } | 259 | } | ... | ... |
-
Please register or login to post a comment