use of org.onosproject.mastership.MastershipInfo in project onos by opennetworkinglab.
the class ConsistentDeviceMastershipStore method getNodes.
@Override
public RoleInfo getNodes(DeviceId deviceId) {
checkArgument(deviceId != null, DEVICE_ID_NULL);
MastershipInfo mastership = getMastership(deviceId);
return new RoleInfo(mastership.master().orElse(null), mastership.backups());
}
use of org.onosproject.mastership.MastershipInfo in project onos by opennetworkinglab.
the class ReplicaInfoManagerTest method testReplicaInfoEvent.
@Test
public void testReplicaInfoEvent() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
service.addListener(new MasterNodeCheck(latch, DID1, NID1));
// fake MastershipEvent
eventDispatcher.post(new MastershipEvent(Type.MASTER_CHANGED, DID1, new MastershipInfo(1, Optional.of(NID1), ImmutableMap.of(NID1, MastershipRole.MASTER))));
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
use of org.onosproject.mastership.MastershipInfo in project onos by opennetworkinglab.
the class GeneralDeviceProvider method roleChanged.
@Override
public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
final MastershipInfo mastershipInfo = mastershipService.getMastershipFor(deviceId);
final NodeId localNodeId = clusterService.getLocalNode().id();
if (!mastershipInfo.getRole(localNodeId).equals(newRole)) {
log.warn("Inconsistent mastership info for {}! Requested {}, but " + "mastership service reports {}, will apply the latter...", deviceId, newRole, mastershipInfo.getRole(localNodeId));
newRole = mastershipInfo.getRole(localNodeId);
}
final DeviceHandshaker handshaker = getBehaviour(deviceId, DeviceHandshaker.class);
if (handshaker == null) {
log.error("Null handshaker. Unable to notify role {} to {}", newRole, deviceId);
return;
}
// Derive preference value.
final int preference;
switch(newRole) {
case MASTER:
preference = 0;
break;
case STANDBY:
preference = mastershipInfo.backups().indexOf(localNodeId) + 1;
if (preference == 0) {
// Not found in list.
log.error("Unable to derive mastership preference for {}, " + "requested role {} but local node ID was " + "not found among list of backup nodes " + "reported by mastership service", deviceId, newRole);
return;
}
break;
case NONE:
// No preference for NONE, apply as is.
Pair<MastershipRole, Integer> pairRolePref = Pair.of(newRole, -1);
if (log.isDebugEnabled()) {
log.debug("Notifying role {} to {}", newRole, deviceId);
} else if (!pairRolePref.equals(lastRoleRequest.get(deviceId))) {
log.info("Notifying role {} to {}", newRole, deviceId);
}
lastRoleRequest.put(deviceId, pairRolePref);
handshaker.roleChanged(newRole);
return;
default:
log.error("Unrecognized mastership role {}", newRole);
return;
}
Pair<MastershipRole, Integer> pairRolePref = Pair.of(newRole, preference);
if (log.isDebugEnabled()) {
log.debug("Notifying role {} (preference {}) for term {} to {}", newRole, preference, mastershipInfo.term(), deviceId);
} else if (!pairRolePref.equals(lastRoleRequest.get(deviceId))) {
log.info("Notifying role {} (preference {}) for term {} to {}", newRole, preference, mastershipInfo.term(), deviceId);
}
lastRoleRequest.put(deviceId, pairRolePref);
try {
handshaker.roleChanged(preference, mastershipInfo.term());
} catch (UnsupportedOperationException e) {
// Preference-based method not supported.
handshaker.roleChanged(newRole);
}
}
use of org.onosproject.mastership.MastershipInfo in project onos by opennetworkinglab.
the class SimpleVirtualMastershipStore method getMastership.
@Override
public MastershipInfo getMastership(NetworkId networkId, DeviceId deviceId) {
Map<DeviceId, NodeId> masterMap = getMasterMap(networkId);
Map<DeviceId, AtomicInteger> termMap = getTermMap(networkId);
Map<DeviceId, List<NodeId>> backups = getBackups(networkId);
ImmutableMap.Builder<NodeId, MastershipRole> roleBuilder = ImmutableMap.builder();
NodeId master = masterMap.get(deviceId);
if (master != null) {
roleBuilder.put(master, MastershipRole.MASTER);
}
backups.getOrDefault(deviceId, Collections.emptyList()).forEach(nodeId -> roleBuilder.put(nodeId, MastershipRole.STANDBY));
clusterService.getNodes().stream().filter(node -> !masterMap.containsValue(node.id())).filter(node -> !backups.get(deviceId).contains(node.id())).forEach(node -> roleBuilder.put(node.id(), MastershipRole.NONE));
return new MastershipInfo(termMap.getOrDefault(deviceId, new AtomicInteger(NOTHING)).get(), Optional.ofNullable(master), roleBuilder.build());
}
use of org.onosproject.mastership.MastershipInfo in project onos by opennetworkinglab.
the class SimpleMastershipStore method getMastership.
@Override
public MastershipInfo getMastership(DeviceId deviceId) {
ImmutableMap.Builder<NodeId, MastershipRole> roleBuilder = ImmutableMap.builder();
NodeId master = masterMap.get(deviceId);
if (master != null) {
roleBuilder.put(master, MastershipRole.MASTER);
}
backups.getOrDefault(deviceId, Collections.emptyList()).forEach(nodeId -> roleBuilder.put(nodeId, MastershipRole.STANDBY));
return new MastershipInfo(termMap.getOrDefault(deviceId, new AtomicInteger(NOTHING)).get(), Optional.ofNullable(master), roleBuilder.build());
}
Aggregations