Search in sources :

Example 66 with NodeId

use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.

the class ConsistentVirtualDeviceMastershipStore method getRole.

@Override
public MastershipRole getRole(NetworkId networkId, NodeId nodeId, DeviceId deviceId) {
    checkArgument(networkId != null, NETWORK_ID_NULL);
    checkArgument(nodeId != null, NODE_ID_NULL);
    checkArgument(deviceId != null, DEVICE_ID_NULL);
    String leadershipTopic = createDeviceMastershipTopic(networkId, deviceId);
    Leadership leadership = leadershipService.getLeadership(leadershipTopic);
    NodeId leader = leadership == null ? null : leadership.leaderNodeId();
    List<NodeId> candidates = leadership == null ? ImmutableList.of() : ImmutableList.copyOf(leadership.candidates());
    return Objects.equal(nodeId, leader) ? MastershipRole.MASTER : candidates.contains(nodeId) ? MastershipRole.STANDBY : MastershipRole.NONE;
}
Also used : Leadership(org.onosproject.cluster.Leadership) NodeId(org.onosproject.cluster.NodeId)

Example 67 with NodeId

use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.

the class ConsistentVirtualDeviceMastershipStore method setStandby.

@Override
public CompletableFuture<MastershipEvent> setStandby(NetworkId networkId, NodeId nodeId, DeviceId deviceId) {
    checkArgument(networkId != null, NETWORK_ID_NULL);
    checkArgument(nodeId != null, NODE_ID_NULL);
    checkArgument(deviceId != null, DEVICE_ID_NULL);
    NodeId currentMaster = getMaster(networkId, deviceId);
    if (!nodeId.equals(currentMaster)) {
        return CompletableFuture.completedFuture(null);
    }
    String leadershipTopic = createDeviceMastershipTopic(networkId, deviceId);
    List<NodeId> candidates = leadershipService.getCandidates(leadershipTopic);
    NodeId newMaster = candidates.stream().filter(candidate -> !Objects.equal(nodeId, candidate)).findFirst().orElse(null);
    log.info("Transitioning to role {} for {}. Next master: {}", newMaster != null ? MastershipRole.STANDBY : MastershipRole.NONE, deviceId, newMaster);
    if (newMaster != null) {
        return setMaster(networkId, newMaster, deviceId);
    }
    return relinquishRole(networkId, nodeId, deviceId);
}
Also used : NodeId(org.onosproject.cluster.NodeId)

Example 68 with NodeId

use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.

the class SimpleVirtualMastershipStore method getRole.

@Override
public MastershipRole getRole(NetworkId networkId, NodeId nodeId, DeviceId deviceId) {
    Map<DeviceId, NodeId> masterMap = getMasterMap(networkId);
    Map<DeviceId, List<NodeId>> backups = getBackups(networkId);
    // just query
    NodeId current = masterMap.get(deviceId);
    MastershipRole role;
    if (current != null && current.equals(nodeId)) {
        return MastershipRole.MASTER;
    }
    if (backups.getOrDefault(deviceId, Collections.emptyList()).contains(nodeId)) {
        role = MastershipRole.STANDBY;
    } else {
        role = MastershipRole.NONE;
    }
    return role;
}
Also used : DeviceId(org.onosproject.net.DeviceId) NodeId(org.onosproject.cluster.NodeId) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) MastershipRole(org.onosproject.net.MastershipRole)

Example 69 with NodeId

use of org.onosproject.cluster.NodeId 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);
}
Also used : DeviceId(org.onosproject.net.DeviceId) MastershipEvent(org.onosproject.mastership.MastershipEvent) NodeId(org.onosproject.cluster.NodeId) MastershipRole(org.onosproject.net.MastershipRole)

Example 70 with NodeId

use of org.onosproject.cluster.NodeId 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);
}
Also used : DeviceId(org.onosproject.net.DeviceId) MastershipEvent(org.onosproject.mastership.MastershipEvent) NodeId(org.onosproject.cluster.NodeId) MastershipRole(org.onosproject.net.MastershipRole)

Aggregations

NodeId (org.onosproject.cluster.NodeId)145 ClusterService (org.onosproject.cluster.ClusterService)36 DeviceId (org.onosproject.net.DeviceId)36 Set (java.util.Set)26 MastershipRole (org.onosproject.net.MastershipRole)23 ControllerNode (org.onosproject.cluster.ControllerNode)22 Test (org.junit.Test)18 Activate (org.osgi.service.component.annotations.Activate)18 List (java.util.List)16 MastershipService (org.onosproject.mastership.MastershipService)15 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)13 Map (java.util.Map)12 ImmutableSet (com.google.common.collect.ImmutableSet)11 ArrayList (java.util.ArrayList)11 Collectors (java.util.stream.Collectors)11 HashSet (java.util.HashSet)10 Optional (java.util.Optional)10 ClusterCommunicationService (org.onosproject.store.cluster.messaging.ClusterCommunicationService)10 Component (org.osgi.service.component.annotations.Component)9 Deactivate (org.osgi.service.component.annotations.Deactivate)9