use of org.onosproject.mastership.MastershipEvent in project onos by opennetworkinglab.
the class SimpleVirtualMastershipStore method requestRole.
@Override
public CompletableFuture<MastershipRole> requestRole(NetworkId networkId, DeviceId deviceId) {
// query+possible reelection
NodeId node = clusterService.getLocalNode().id();
MastershipRole role = getRole(networkId, node, deviceId);
Map<DeviceId, NodeId> masterMap = getMasterMap(networkId);
switch(role) {
case MASTER:
return CompletableFuture.completedFuture(MastershipRole.MASTER);
case STANDBY:
if (getMaster(networkId, deviceId) == null) {
// no master => become master
masterMap.put(deviceId, node);
incrementTerm(networkId, deviceId);
// remove from backup list
removeFromBackups(networkId, deviceId, node);
notifyDelegate(networkId, new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(networkId, deviceId)));
return CompletableFuture.completedFuture(MastershipRole.MASTER);
}
return CompletableFuture.completedFuture(MastershipRole.STANDBY);
case NONE:
if (getMaster(networkId, deviceId) == null) {
// no master => become master
masterMap.put(deviceId, node);
incrementTerm(networkId, deviceId);
notifyDelegate(networkId, new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(networkId, deviceId)));
return CompletableFuture.completedFuture(MastershipRole.MASTER);
}
// add to backup list
if (addToBackup(networkId, deviceId, node)) {
notifyDelegate(networkId, new MastershipEvent(BACKUPS_CHANGED, deviceId, getMastership(networkId, deviceId)));
}
return CompletableFuture.completedFuture(MastershipRole.STANDBY);
default:
log.warn("unknown Mastership Role {}", role);
}
return CompletableFuture.completedFuture(role);
}
use of org.onosproject.mastership.MastershipEvent in project onos by opennetworkinglab.
the class SimpleVirtualMastershipStore method relinquishRole.
@Override
public synchronized CompletableFuture<MastershipEvent> relinquishRole(NetworkId networkId, NodeId nodeId, DeviceId deviceId) {
Map<DeviceId, NodeId> masterMap = getMasterMap(networkId);
MastershipRole role = getRole(networkId, nodeId, deviceId);
switch(role) {
case MASTER:
NodeId backup = reelect(networkId, deviceId, nodeId);
masterMap.put(deviceId, backup);
incrementTerm(networkId, deviceId);
return CompletableFuture.completedFuture(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(networkId, deviceId)));
case STANDBY:
if (removeFromBackups(networkId, deviceId, nodeId)) {
return CompletableFuture.completedFuture(new MastershipEvent(BACKUPS_CHANGED, deviceId, getMastership(networkId, deviceId)));
}
break;
case NONE:
break;
default:
log.warn("unknown Mastership Role {}", role);
}
return CompletableFuture.completedFuture(null);
}
use of org.onosproject.mastership.MastershipEvent in project onos by opennetworkinglab.
the class SimpleVirtualMastershipStore method setMaster.
@Override
public synchronized CompletableFuture<MastershipEvent> setMaster(NetworkId networkId, NodeId nodeId, DeviceId deviceId) {
Map<DeviceId, NodeId> masterMap = getMasterMap(networkId);
MastershipRole role = getRole(networkId, nodeId, deviceId);
switch(role) {
case MASTER:
// no-op
return CompletableFuture.completedFuture(null);
case STANDBY:
case NONE:
NodeId prevMaster = masterMap.put(deviceId, nodeId);
incrementTerm(networkId, deviceId);
removeFromBackups(networkId, deviceId, nodeId);
addToBackup(networkId, deviceId, prevMaster);
break;
default:
log.warn("unknown Mastership Role {}", role);
return null;
}
return CompletableFuture.completedFuture(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(networkId, deviceId)));
}
use of org.onosproject.mastership.MastershipEvent in project onos by opennetworkinglab.
the class SimpleVirtualMastershipStore method setStandby.
@Override
public CompletableFuture<MastershipEvent> setStandby(NetworkId networkId, NodeId nodeId, DeviceId deviceId) {
Map<DeviceId, NodeId> masterMap = getMasterMap(networkId);
MastershipRole role = getRole(networkId, nodeId, deviceId);
switch(role) {
case MASTER:
NodeId backup = reelect(networkId, deviceId, nodeId);
if (backup == null) {
// no master alternative
masterMap.remove(deviceId);
// TODO: Should there be new event type for no MASTER?
return CompletableFuture.completedFuture(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(networkId, deviceId)));
} else {
NodeId prevMaster = masterMap.put(deviceId, backup);
incrementTerm(networkId, deviceId);
addToBackup(networkId, deviceId, prevMaster);
return CompletableFuture.completedFuture(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(networkId, deviceId)));
}
case STANDBY:
case NONE:
boolean modified = addToBackup(networkId, deviceId, nodeId);
if (modified) {
return CompletableFuture.completedFuture(new MastershipEvent(BACKUPS_CHANGED, deviceId, getMastership(networkId, deviceId)));
}
break;
default:
log.warn("unknown Mastership Role {}", role);
}
return null;
}
use of org.onosproject.mastership.MastershipEvent in project onos by opennetworkinglab.
the class SimpleMastershipStoreTest method unsetMaster.
@Test
public void unsetMaster() {
// NONE - record backup but take no other action
put(DID1, N1, false, false);
sms.setStandby(N1, DID1);
assertTrue("not backed up", sms.backups.get(DID1).contains(N1));
int prev = sms.termMap.get(DID1).get();
sms.setStandby(N1, DID1);
assertEquals("term should not change", prev, sms.termMap.get(DID1).get());
// no backup, MASTER
put(DID1, N1, true, false);
assertNull("expect no MASTER event", Futures.getUnchecked(sms.setStandby(N1, DID1)).roleInfo().master());
assertNull("wrong node", sms.masterMap.get(DID1));
// backup, switch
sms.masterMap.clear();
put(DID1, N1, true, true);
put(DID1, N2, false, true);
put(DID2, N2, true, true);
MastershipEvent event = Futures.getUnchecked(sms.setStandby(N1, DID1));
assertEquals("wrong event", MASTER_CHANGED, event.type());
assertEquals("wrong master", N2, event.roleInfo().master());
}
Aggregations