Search in sources :

Example 46 with NodeId

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;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) DeviceId(org.onosproject.net.DeviceId) NodeId(org.onosproject.cluster.NodeId) ControllerNode(org.onosproject.cluster.ControllerNode) HashSet(java.util.HashSet)

Example 47 with NodeId

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");
}
Also used : ClusterService(org.onosproject.cluster.ClusterService) HashSet(java.util.HashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Version(org.onosproject.core.Version) State(org.onosproject.cluster.ControllerNode.State) Instant(java.time.Instant) NodeId(org.onosproject.cluster.NodeId) ControllerNode(org.onosproject.cluster.ControllerNode) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) ClusterEventListener(org.onosproject.cluster.ClusterEventListener) Activate(org.osgi.service.component.annotations.Activate)

Example 48 with NodeId

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);
}
Also used : NodeId(org.onosproject.cluster.NodeId)

Example 49 with NodeId

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

Example 50 with NodeId

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;
}
Also used : NodeId(org.onosproject.cluster.NodeId)

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