Ayaka Koshibe

fix relinquishment behavior in SimpleMastershipStore

Change-Id: Ibc9eeae397b7acc9e08cc569f9c8a642557bf4f9
......@@ -68,15 +68,15 @@ public class MastershipManagerTest {
@Test
public void relinquishMastership() {
//no backups - should turn to standby and no master for device
//no backups - should just turn to NONE for device.
mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
assertEquals("wrong role:", MASTER, mgr.getLocalRole(DEV_MASTER));
mgr.relinquishMastership(DEV_MASTER);
assertNull("wrong master:", mgr.getMasterFor(DEV_OTHER));
assertEquals("wrong role:", STANDBY, mgr.getLocalRole(DEV_MASTER));
assertEquals("wrong role:", NONE, mgr.getLocalRole(DEV_MASTER));
//not master, nothing should happen
mgr.setRole(NID_LOCAL, DEV_OTHER, STANDBY);
mgr.setRole(NID_LOCAL, DEV_OTHER, NONE);
mgr.relinquishMastership(DEV_OTHER);
assertNull("wrong role:", mgr.getMasterFor(DEV_OTHER));
......
......@@ -226,12 +226,34 @@ public class SimpleMastershipStore
break;
}
}
backups.remove(backup);
return backup;
}
@Override
public MastershipEvent relinquishRole(NodeId nodeId, DeviceId deviceId) {
return setStandby(nodeId, deviceId);
MastershipRole role = getRole(nodeId, deviceId);
synchronized (this) {
switch (role) {
case MASTER:
NodeId backup = reelect(nodeId);
backups.remove(nodeId);
if (backup == null) {
masterMap.remove(deviceId);
} else {
masterMap.put(deviceId, backup);
termMap.get(deviceId).incrementAndGet();
return new MastershipEvent(MASTER_CHANGED, deviceId,
new RoleInfo(backup, Lists.newLinkedList(backups)));
}
case STANDBY:
backups.remove(nodeId);
case NONE:
default:
log.warn("unknown Mastership Role {}", role);
}
}
return null;
}
}
......