Yuta HIGUCHI

allow null Master in MastershipTerm

Change-Id: I840354eb6d0b5a1bac91887a41626c33c49d592c
......@@ -55,13 +55,8 @@ public final class MastershipTerm {
}
if (other instanceof MastershipTerm) {
MastershipTerm that = (MastershipTerm) other;
if (!this.master.equals(that.master)) {
return false;
}
if (this.termNumber != that.termNumber) {
return false;
}
return true;
return Objects.equals(this.master, that.master) &&
Objects.equals(this.termNumber, that.termNumber);
}
return false;
}
......
......@@ -257,7 +257,8 @@ public class DeviceManager
MastershipTerm term = mastershipService.requestTermService()
.getMastershipTerm(deviceId);
if (!term.master().equals(clusterService.getLocalNode().id())) {
final NodeId myNodeId = clusterService.getLocalNode().id();
if (!myNodeId.equals(term.master())) {
// lost mastership after requestRole told this instance was MASTER.
log.info("lost mastership before getting term info.");
return;
......
......@@ -139,14 +139,14 @@ public class MastershipManagerTest {
public void termService() {
MastershipTermService ts = mgr.requestTermService();
//term = 0 for both
//term = 1 for both
mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
assertEquals("inconsistent term: ", 0, ts.getMastershipTerm(DEV_MASTER).termNumber());
assertEquals("inconsistent term: ", 1, ts.getMastershipTerm(DEV_MASTER).termNumber());
//hand devices to NID_LOCAL and back: term = 2
//hand devices to NID_LOCAL and back: term = 1 + 2
mgr.setRole(NID_OTHER, DEV_MASTER, MASTER);
mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER);
assertEquals("inconsistent terms: ", 2, ts.getMastershipTerm(DEV_MASTER).termNumber());
assertEquals("inconsistent terms: ", 3, ts.getMastershipTerm(DEV_MASTER).termNumber());
}
private final class TestClusterService implements ClusterService {
......
......@@ -60,8 +60,10 @@ public class DistributedMastershipStore
extends AbstractHazelcastStore<MastershipEvent, MastershipStoreDelegate>
implements MastershipStore {
//term number representing that master has never been chosen yet
private static final Integer NOTHING = 0;
//initial term/TTL value
private static final Integer INIT = 0;
private static final Integer INIT = 1;
//device to node roles
protected SMap<DeviceId, RoleValue> roleMap;
......@@ -256,8 +258,8 @@ implements MastershipStore {
RoleValue rv = getRoleValue(deviceId);
final Integer term = terms.get(deviceId);
final NodeId master = rv.get(MASTER);
if ((master == null) || (term == null)) {
return null;
if (term == null) {
return MastershipTerm.of(null, NOTHING);
}
return MastershipTerm.of(master, term);
} finally {
......
......@@ -149,7 +149,7 @@ public class DistributedMastershipStoreTest {
assertEquals("wrong role for NONE:", MASTER, dms.requestRole(DID1));
assertTrue("wrong state for store:", !dms.terms.isEmpty());
assertEquals("wrong term",
MastershipTerm.of(N1, 0), dms.getTermFor(DID1));
MastershipTerm.of(N1, 1), dms.getTermFor(DID1));
//CN2 now local. DID2 has N1 as MASTER so N2 is STANDBY
testStore.setCurrent(CN2);
......@@ -172,15 +172,15 @@ public class DistributedMastershipStoreTest {
//switch over to N2
assertEquals("wrong event:", Type.MASTER_CHANGED, dms.setMaster(N2, DID1).type());
System.out.println(dms.getTermFor(DID1).master() + ":" + dms.getTermFor(DID1).termNumber());
assertEquals("wrong term", MastershipTerm.of(N2, 1), dms.getTermFor(DID1));
assertEquals("wrong term", MastershipTerm.of(N2, 2), dms.getTermFor(DID1));
//orphan switch - should be rare case
assertEquals("wrong event:", Type.MASTER_CHANGED, dms.setMaster(N2, DID2).type());
assertEquals("wrong term", MastershipTerm.of(N2, 0), dms.getTermFor(DID2));
assertEquals("wrong term", MastershipTerm.of(N2, 1), dms.getTermFor(DID2));
//disconnect and reconnect - sign of failing re-election or single-instance channel
dms.roleMap.clear();
dms.setMaster(N2, DID2);
assertEquals("wrong term", MastershipTerm.of(N2, 1), dms.getTermFor(DID2));
assertEquals("wrong term", MastershipTerm.of(N2, 2), dms.getTermFor(DID2));
}
@Test
......
......@@ -38,15 +38,14 @@ public class MastershipTermSerializer extends Serializer<MastershipTerm> {
@Override
public MastershipTerm read(Kryo kryo, Input input, Class<MastershipTerm> type) {
final NodeId node = new NodeId(input.readString());
final NodeId node = (NodeId) kryo.readClassAndObject(input);
final int term = input.readInt();
return MastershipTerm.of(node, term);
}
@Override
public void write(Kryo kryo, Output output, MastershipTerm object) {
output.writeString(object.master().toString());
kryo.writeClassAndObject(output, object.master());
output.writeInt(object.termNumber());
}
}
......
......@@ -201,6 +201,7 @@ public class KryoSerializerTest {
@Test
public void testMastershipTerm() {
testSerialized(MastershipTerm.of(new NodeId("foo"), 2));
testSerialized(MastershipTerm.of(null, 0));
}
@Test
......
......@@ -62,6 +62,9 @@ public class SimpleMastershipStore
public static final IpAddress LOCALHOST = IpAddress.valueOf("127.0.0.1");
private static final int NOTHING = 0;
private static final int INIT = 1;
private ControllerNode instance =
new DefaultControllerNode(new NodeId("local"), LOCALHOST);
......@@ -97,7 +100,7 @@ public class SimpleMastershipStore
break;
case NONE:
masterMap.put(deviceId, nodeId);
termMap.put(deviceId, new AtomicInteger());
termMap.put(deviceId, new AtomicInteger(INIT));
backups.add(nodeId);
break;
default:
......@@ -149,7 +152,7 @@ public class SimpleMastershipStore
NodeId rel = reelect(node);
if (rel == null) {
masterMap.put(deviceId, node);
termMap.put(deviceId, new AtomicInteger());
termMap.put(deviceId, new AtomicInteger(INIT));
role = MastershipRole.MASTER;
}
backups.add(node);
......@@ -159,7 +162,7 @@ public class SimpleMastershipStore
//first to get to it, say we are master
synchronized (this) {
masterMap.put(deviceId, node);
termMap.put(deviceId, new AtomicInteger());
termMap.put(deviceId, new AtomicInteger(INIT));
backups.add(node);
role = MastershipRole.MASTER;
}
......@@ -194,9 +197,8 @@ public class SimpleMastershipStore
@Override
public MastershipTerm getTermFor(DeviceId deviceId) {
if ((masterMap.get(deviceId) == null) ||
(termMap.get(deviceId) == null)) {
return null;
if ((termMap.get(deviceId) == null)) {
return MastershipTerm.of(masterMap.get(deviceId), NOTHING);
}
return MastershipTerm.of(
masterMap.get(deviceId), termMap.get(deviceId).get());
......@@ -220,7 +222,7 @@ public class SimpleMastershipStore
case STANDBY:
case NONE:
if (!termMap.containsKey(deviceId)) {
termMap.put(deviceId, new AtomicInteger());
termMap.put(deviceId, new AtomicInteger(INIT));
}
backups.add(nodeId);
break;
......