Search in sources :

Example 21 with MastershipRole

use of org.onosproject.net.MastershipRole in project onos by opennetworkinglab.

the class ECDeviceStore method removeDevice.

@Override
public DeviceEvent removeDevice(DeviceId deviceId) {
    NodeId master = mastershipService.getMasterFor(deviceId);
    // if there exist a master, forward
    // if there is no master, try to become one and process
    boolean relinquishAtEnd = false;
    if (master == null) {
        final MastershipRole myRole = mastershipService.getLocalRole(deviceId);
        if (myRole != MastershipRole.NONE) {
            relinquishAtEnd = true;
        }
        log.debug("Temporarily requesting role for {} to remove", deviceId);
        MastershipRole role = Futures.getUnchecked(mastershipService.requestRoleFor(deviceId));
        if (role == MastershipRole.MASTER) {
            master = localNodeId;
        }
    }
    if (!localNodeId.equals(master)) {
        log.debug("{} has control of {}, forwarding remove request", master, deviceId);
        clusterCommunicator.unicast(deviceId, DEVICE_REMOVE_REQ, SERIALIZER::encode, master).whenComplete((r, e) -> {
            if (e != null) {
                log.error("Failed to forward {} remove request to its master", deviceId, e);
            }
        });
        return null;
    }
    // I have control..
    DeviceEvent event = null;
    final DeviceKey deviceKey = new DeviceKey(getPrimaryProviderId(deviceId), deviceId);
    DeviceDescription removedDeviceDescription = deviceDescriptions.remove(deviceKey);
    if (removedDeviceDescription != null) {
        event = purgeDeviceCache(deviceId);
    }
    if (relinquishAtEnd) {
        log.debug("Relinquishing temporary role acquired for {}", deviceId);
        mastershipService.relinquishMastership(deviceId);
    }
    return event;
}
Also used : DeviceEvent(org.onosproject.net.device.DeviceEvent) DeviceDescription(org.onosproject.net.device.DeviceDescription) NodeId(org.onosproject.cluster.NodeId) MastershipRole(org.onosproject.net.MastershipRole)

Example 22 with MastershipRole

use of org.onosproject.net.MastershipRole in project onos by opennetworkinglab.

the class DeviceManager method mastershipCheck.

/**
 * Checks if all the reachable devices have a valid mastership role.
 */
private void mastershipCheck() {
    log.debug("Checking mastership");
    for (Device device : getDevices()) {
        final DeviceId deviceId = device.id();
        MastershipRole myRole = mastershipService.getLocalRole(deviceId);
        log.trace("Checking device {}. Current role is {}", deviceId, myRole);
        log.debug("Device {} local status is {}", deviceId, localStatus(deviceId));
        final boolean isGracePeriodOn = inGracePeriod(deviceId);
        final boolean isReachable = isReachable(deviceId, isGracePeriodOn);
        // Passed the grace period and it is still not reachable
        if (!isGracePeriodOn && !isReachable) {
            if (myRole != NONE) {
                // Verify if the device is fully disconnected from the cluster
                if (updateMastershipFor(deviceId) == null && myRole == MASTER && isAvailable(deviceId)) {
                    log.info("Local Role {}, Marking unreachable device {} offline", MASTER, deviceId);
                    // Following the deviceDisconnected method logic (line 734) we are marking also all the
                    // ports as disabled.
                    List<PortDescription> descs = store.getPortDescriptions(getProvider(deviceId).id(), deviceId).map(desc -> ensurePortEnabledState(desc, false)).collect(Collectors.toList());
                    store.updatePorts(getProvider(deviceId).id(), deviceId, descs);
                    post(store.markOffline(deviceId));
                }
            } else {
                // never be hit unless in a device removed phase for NONE mastership roles.
                try {
                    mastershipService.requestRoleFor(deviceId).get();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    log.error("Interrupted waiting for Mastership", e);
                } catch (ExecutionException e) {
                    log.error("Encountered an error waiting for Mastership", e);
                }
                MastershipTerm term = termService.getMastershipTerm(deviceId);
                if (updateMastershipFor(deviceId) == null && term != null && localNodeId.equals(term.master()) && isAvailable(deviceId)) {
                    log.info("Marking unreachable device {} offline", deviceId);
                    // Following the deviceDisconnected method logic (line 734) we are marking also all the
                    // ports as disabled.
                    List<PortDescription> descs = store.getPortDescriptions(getProvider(deviceId).id(), deviceId).map(desc -> ensurePortEnabledState(desc, false)).collect(Collectors.toList());
                    store.updatePorts(getProvider(deviceId).id(), deviceId, descs);
                    post(store.markOffline(deviceId));
                }
            }
            roleToAcknowledge.remove(deviceId);
        } else if (isReachable) {
            // If this node is the master, ensure the device is marked online.
            if (myRole == MASTER && canMarkOnline(device)) {
                log.debug("Can mark online {}", deviceId);
                post(store.markOnline(deviceId));
            }
            log.debug("{} is reachable - reasserting the role", deviceId);
            // Device is still reachable. It is useful for some protocols
            // to reassert the role. Note: NONE triggers request to MastershipService
            reassertRole(deviceId, myRole);
        } else {
            // Do not proceed furthermore if the grace period is still on
            log.debug("Skipping mastership check for {}", deviceId);
        }
    }
}
Also used : NetworkConfigService(org.onosproject.net.config.NetworkConfigService) Config(org.onosproject.net.config.Config) BasicDeviceConfig(org.onosproject.net.config.basics.BasicDeviceConfig) PortNumber(org.onosproject.net.PortNumber) DeviceService(org.onosproject.net.device.DeviceService) Tools.groupedThreads(org.onlab.util.Tools.groupedThreads) DeviceStore(org.onosproject.net.device.DeviceStore) AbstractProviderService(org.onosproject.net.provider.AbstractProviderService) MASTER(org.onosproject.net.MastershipRole.MASTER) PortConfigOperator(org.onosproject.net.config.PortConfigOperator) NONE(org.onosproject.net.MastershipRole.NONE) ConnectPoint(org.onosproject.net.ConnectPoint) Executors.newSingleThreadScheduledExecutor(java.util.concurrent.Executors.newSingleThreadScheduledExecutor) Port(org.onosproject.net.Port) Map(java.util.Map) DeviceProvider(org.onosproject.net.device.DeviceProvider) MessageSubject(org.onosproject.store.cluster.messaging.MessageSubject) DeviceProviderService(org.onosproject.net.device.DeviceProviderService) KryoNamespaces(org.onosproject.store.serializers.KryoNamespaces) MastershipService(org.onosproject.mastership.MastershipService) DeviceStoreDelegate(org.onosproject.net.device.DeviceStoreDelegate) DeviceDescription(org.onosproject.net.device.DeviceDescription) NodeId(org.onosproject.cluster.NodeId) DeviceAnnotationConfig(org.onosproject.net.config.basics.DeviceAnnotationConfig) Serializer(org.onosproject.store.service.Serializer) UpgradeService(org.onosproject.upgrade.UpgradeService) Device(org.onosproject.net.Device) Deactivate(org.osgi.service.component.annotations.Deactivate) Multimaps.newListMultimap(com.google.common.collect.Multimaps.newListMultimap) Collection(java.util.Collection) MastershipTermService(org.onosproject.mastership.MastershipTermService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Executors.newSingleThreadExecutor(java.util.concurrent.Executors.newSingleThreadExecutor) PortConfigOperatorRegistry(org.onosproject.net.config.PortConfigOperatorRegistry) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) AbstractListenerProviderRegistry(org.onosproject.net.provider.AbstractListenerProviderRegistry) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) List(java.util.List) DeviceEvent(org.onosproject.net.device.DeviceEvent) ClusterCommunicationService(org.onosproject.store.cluster.messaging.ClusterCommunicationService) Optional(java.util.Optional) ClusterService(org.onosproject.cluster.ClusterService) DeviceId(org.onosproject.net.DeviceId) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) System.currentTimeMillis(java.lang.System.currentTimeMillis) Tools(org.onlab.util.Tools) PortStatistics(org.onosproject.net.device.PortStatistics) PortDescriptionsConfig(org.onosproject.net.config.basics.PortDescriptionsConfig) NetworkConfigEvent(org.onosproject.net.config.NetworkConfigEvent) AppGuard.checkPermission(org.onosproject.security.AppGuard.checkPermission) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) DeviceProviderRegistry(org.onosproject.net.device.DeviceProviderRegistry) Multimap(com.google.common.collect.Multimap) KryoNamespace(org.onlab.util.KryoNamespace) ControllerNode(org.onosproject.cluster.ControllerNode) HashSet(java.util.HashSet) MastershipEvent(org.onosproject.mastership.MastershipEvent) Component(org.osgi.service.component.annotations.Component) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) PortDescription(org.onosproject.net.device.PortDescription) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Activate(org.osgi.service.component.annotations.Activate) Multimaps.synchronizedListMultimap(com.google.common.collect.Multimaps.synchronizedListMultimap) ExecutorService(java.util.concurrent.ExecutorService) Type(org.onosproject.net.Device.Type) STANDBY(org.onosproject.net.MastershipRole.STANDBY) DeviceListener(org.onosproject.net.device.DeviceListener) Logger(org.slf4j.Logger) MastershipRole(org.onosproject.net.MastershipRole) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) DEVICE_READ(org.onosproject.security.AppPermission.Type.DEVICE_READ) ProviderId(org.onosproject.net.provider.ProviderId) Maps(com.google.common.collect.Maps) MastershipTerm(org.onosproject.mastership.MastershipTerm) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) DefaultPortDescription(org.onosproject.net.device.DefaultPortDescription) DeviceAdminService(org.onosproject.net.device.DeviceAdminService) Provider(org.onosproject.net.provider.Provider) PortAnnotationConfig(org.onosproject.net.config.basics.PortAnnotationConfig) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) MastershipListener(org.onosproject.mastership.MastershipListener) Reference(org.osgi.service.component.annotations.Reference) MastershipAdminService(org.onosproject.mastership.MastershipAdminService) NetworkConfigListener(org.onosproject.net.config.NetworkConfigListener) MastershipTerm(org.onosproject.mastership.MastershipTerm) Device(org.onosproject.net.Device) DeviceId(org.onosproject.net.DeviceId) PortDescription(org.onosproject.net.device.PortDescription) DefaultPortDescription(org.onosproject.net.device.DefaultPortDescription) ExecutionException(java.util.concurrent.ExecutionException) MastershipRole(org.onosproject.net.MastershipRole)

Example 23 with MastershipRole

use of org.onosproject.net.MastershipRole 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());
}
Also used : HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ControllerNode(org.onosproject.cluster.ControllerNode) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MastershipEvent(org.onosproject.mastership.MastershipEvent) Component(org.osgi.service.component.annotations.Component) ImmutableList(com.google.common.collect.ImmutableList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Node(org.onosproject.cluster.Node) VersionService(org.onosproject.core.VersionService) BACKUPS_CHANGED(org.onosproject.mastership.MastershipEvent.Type.BACKUPS_CHANGED) Map(java.util.Map) Version(org.onosproject.core.Version) NetworkId(org.onosproject.incubator.net.virtual.NetworkId) Activate(org.osgi.service.component.annotations.Activate) IpAddress(org.onlab.packet.IpAddress) NodeId(org.onosproject.cluster.NodeId) ClusterEventListener(org.onosproject.cluster.ClusterEventListener) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) MASTER_CHANGED(org.onosproject.mastership.MastershipEvent.Type.MASTER_CHANGED) ImmutableMap(com.google.common.collect.ImmutableMap) MastershipRole(org.onosproject.net.MastershipRole) Deactivate(org.osgi.service.component.annotations.Deactivate) VirtualNetworkMastershipStore(org.onosproject.incubator.net.virtual.VirtualNetworkMastershipStore) MastershipInfo(org.onosproject.mastership.MastershipInfo) Set(java.util.Set) Instant(java.time.Instant) MastershipTerm(org.onosproject.mastership.MastershipTerm) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) Objects(java.util.Objects) List(java.util.List) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Optional(java.util.Optional) ClusterService(org.onosproject.cluster.ClusterService) RoleInfo(org.onosproject.cluster.RoleInfo) DeviceId(org.onosproject.net.DeviceId) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) MastershipStoreDelegate(org.onosproject.mastership.MastershipStoreDelegate) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeviceId(org.onosproject.net.DeviceId) NodeId(org.onosproject.cluster.NodeId) MastershipInfo(org.onosproject.mastership.MastershipInfo) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) MastershipRole(org.onosproject.net.MastershipRole) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 24 with MastershipRole

use of org.onosproject.net.MastershipRole 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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NodeId(org.onosproject.cluster.NodeId) MastershipInfo(org.onosproject.mastership.MastershipInfo) MastershipRole(org.onosproject.net.MastershipRole) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 25 with MastershipRole

use of org.onosproject.net.MastershipRole in project onos by opennetworkinglab.

the class SimpleMastershipStore method getRole.

@Override
public MastershipRole getRole(NodeId nodeId, DeviceId deviceId) {
    // 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 : NodeId(org.onosproject.cluster.NodeId) MastershipRole(org.onosproject.net.MastershipRole)

Aggregations

MastershipRole (org.onosproject.net.MastershipRole)32 NodeId (org.onosproject.cluster.NodeId)19 MastershipEvent (org.onosproject.mastership.MastershipEvent)10 DeviceId (org.onosproject.net.DeviceId)10 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 MastershipTerm (org.onosproject.mastership.MastershipTerm)4 ImmutableList (com.google.common.collect.ImmutableList)3 List (java.util.List)3 Map (java.util.Map)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ControllerNode (org.onosproject.cluster.ControllerNode)3 MastershipAdminService (org.onosproject.mastership.MastershipAdminService)3 MastershipInfo (org.onosproject.mastership.MastershipInfo)3 MastershipService (org.onosproject.mastership.MastershipService)3 DeviceEvent (org.onosproject.net.device.DeviceEvent)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Instant (java.time.Instant)2 HashSet (java.util.HashSet)2 Objects (java.util.Objects)2