use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.
the class MastershipManager method getRegionsPreferredMasters.
/**
* Get region's preferred set of master nodes - the first master node set that has at
* least one active node.
*
* @param region region for which preferred set of master nodes is requested
* @param devicesInMasters device set to track devices in preferred set of master nodes
* @param allControllerDevices controller nodes to devices map
* @return region's preferred master nodes (and devices that use them as masters)
*/
private Map<ControllerNode, Set<DeviceId>> getRegionsPreferredMasters(Region region, Set<DeviceId> devicesInMasters, Map<ControllerNode, Set<DeviceId>> allControllerDevices) {
Map<ControllerNode, Set<DeviceId>> regionalControllerDevices = new HashMap<>();
int listIndex = 0;
for (Set<NodeId> masterSet : region.masters()) {
log.info("Region {} masters set {} has {} nodes.", region.id(), listIndex, masterSet.size());
if (masterSet.isEmpty()) {
// nothing on this level
listIndex++;
continue;
}
// Create buckets reflecting current ownership.
for (NodeId nodeId : masterSet) {
if (clusterService.getState(nodeId).isActive()) {
ControllerNode controllerNode = clusterService.getNode(nodeId);
Set<DeviceId> devicesOf = new HashSet<>(allControllerDevices.get(controllerNode));
regionalControllerDevices.put(controllerNode, devicesOf);
devicesInMasters.addAll(devicesOf);
log.info("Active Node {} has {} devices.", nodeId, devicesOf.size());
}
}
if (!regionalControllerDevices.isEmpty()) {
// now have a set of >0 active controllers
break;
}
// keep on looking
listIndex++;
}
return regionalControllerDevices;
}
use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.
the class SimpleMastershipStore method activate.
@Activate
public void activate() {
if (clusterService == null) {
// just for ease of unit test
final ControllerNode instance = new DefaultControllerNode(new NodeId("local"), IpAddress.valueOf("127.0.0.1"));
clusterService = new ClusterService() {
private final Instant creationTime = Instant.now();
@Override
public ControllerNode getLocalNode() {
return instance;
}
@Override
public Set<ControllerNode> getNodes() {
return ImmutableSet.of(instance);
}
@Override
public Set<Node> getConsensusNodes() {
return ImmutableSet.of();
}
@Override
public ControllerNode getNode(NodeId nodeId) {
if (instance.id().equals(nodeId)) {
return instance;
}
return null;
}
@Override
public State getState(NodeId nodeId) {
if (instance.id().equals(nodeId)) {
return State.ACTIVE;
} else {
return State.INACTIVE;
}
}
@Override
public Version getVersion(NodeId nodeId) {
if (instance.id().equals(nodeId)) {
return versionService.version();
}
return null;
}
@Override
public Instant getLastUpdatedInstant(NodeId nodeId) {
return creationTime;
}
@Override
public void addListener(ClusterEventListener listener) {
}
@Override
public void removeListener(ClusterEventListener listener) {
}
};
}
log.info("Started");
}
use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.
the class SimpleMastershipStore method demote.
@Override
public void demote(NodeId instance, DeviceId deviceId) {
if (instance == null) {
return;
}
NodeId master = masterMap.get(deviceId);
if (master.equals(instance)) {
return;
}
List<NodeId> stbys = backups.getOrDefault(deviceId, new ArrayList<>());
if (!stbys.contains(instance)) {
return;
}
stbys.remove(instance);
stbys.add(instance);
backups.put(deviceId, stbys);
}
use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.
the class SimpleMastershipStore method setStandby.
@Override
public synchronized CompletableFuture<MastershipEvent> setStandby(NodeId nodeId, DeviceId deviceId) {
MastershipRole role = getRole(nodeId, deviceId);
switch(role) {
case MASTER:
NodeId backup = reelect(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(deviceId)));
} else {
NodeId prevMaster = masterMap.put(deviceId, backup);
incrementTerm(deviceId);
addToBackup(deviceId, prevMaster);
return CompletableFuture.completedFuture(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(deviceId)));
}
case STANDBY:
case NONE:
boolean modified = addToBackup(deviceId, nodeId);
if (modified) {
return CompletableFuture.completedFuture(new MastershipEvent(BACKUPS_CHANGED, deviceId, getMastership(deviceId)));
}
break;
default:
log.warn("unknown Mastership Role {}", role);
}
return null;
}
use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.
the class SimpleMastershipStore method reelect.
// dumbly selects next-available node that's not the current one
// emulate leader election
private synchronized NodeId reelect(DeviceId did, NodeId nodeId) {
List<NodeId> stbys = backups.getOrDefault(did, Collections.emptyList());
NodeId backup = null;
for (NodeId n : stbys) {
if (!n.equals(nodeId)) {
backup = n;
break;
}
}
stbys.remove(backup);
return backup;
}
Aggregations