Search in sources :

Example 1 with MASTER

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

the class MastershipManager method balanceRolesInRegion.

/**
 * Balances the nodes in specified region.
 *
 * @param region               region in which nodes are to be balanced
 * @param allControllerDevices controller nodes to devices map
 * @return controller nodes that were balanced
 */
private Map<ControllerNode, Set<DeviceId>> balanceRolesInRegion(Region region, Map<ControllerNode, Set<DeviceId>> allControllerDevices) {
    // Retrieve all devices associated with specified region
    Set<DeviceId> devicesInRegion = regionService.getRegionDevices(region.id());
    log.info("Region {} has {} devices.", region.id(), devicesInRegion.size());
    if (devicesInRegion.isEmpty()) {
        // no devices in this region, so nothing to balance.
        return new HashMap<>();
    }
    List<Set<NodeId>> mastersList = region.masters();
    log.info("Region {} has {} sets of masters.", region.id(), mastersList.size());
    if (mastersList.isEmpty()) {
        // for now just leave devices alone
        return new HashMap<>();
    }
    // Get the region's preferred set of masters
    Set<DeviceId> devicesInMasters = Sets.newHashSet();
    Map<ControllerNode, Set<DeviceId>> regionalControllerDevices = getRegionsPreferredMasters(region, devicesInMasters, allControllerDevices);
    // Now re-balance the buckets until they are roughly even.
    List<CompletableFuture<Void>> balanceBucketsFutures = Lists.newArrayList();
    balanceControllerNodes(regionalControllerDevices, devicesInMasters.size(), balanceBucketsFutures);
    // Handle devices that are not currently mastered by the master node set
    Set<DeviceId> devicesNotMasteredWithControllers = Sets.difference(devicesInRegion, devicesInMasters);
    if (!devicesNotMasteredWithControllers.isEmpty()) {
        // active controllers in master node set are already balanced, just
        // assign device mastership in sequence
        List<ControllerNode> sorted = new ArrayList<>(regionalControllerDevices.keySet());
        Collections.sort(sorted, Comparator.comparingInt(o -> (regionalControllerDevices.get(o)).size()));
        int deviceIndex = 0;
        for (DeviceId deviceId : devicesNotMasteredWithControllers) {
            ControllerNode cnode = sorted.get(deviceIndex % sorted.size());
            balanceBucketsFutures.add(setRole(cnode.id(), deviceId, MASTER));
            regionalControllerDevices.get(cnode).add(deviceId);
            deviceIndex++;
        }
    }
    CompletableFuture<Void> balanceRolesFuture = allOf(balanceBucketsFutures.toArray(new CompletableFuture[balanceBucketsFutures.size()]));
    Futures.getUnchecked(balanceRolesFuture);
    // Update the map before returning
    regionalControllerDevices.forEach((controllerNode, deviceIds) -> {
        regionalControllerDevices.put(controllerNode, new HashSet<>(getDevicesOf(controllerNode.id())));
    });
    return regionalControllerDevices;
}
Also used : CLUSTER_READ(org.onosproject.security.AppPermission.Type.CLUSTER_READ) RegionService(org.onosproject.net.region.RegionService) MASTER(org.onosproject.net.MastershipRole.MASTER) OsgiPropertyConstants(org.onosproject.net.OsgiPropertyConstants) Map(java.util.Map) MastershipService(org.onosproject.mastership.MastershipService) NodeId(org.onosproject.cluster.NodeId) Region(org.onosproject.net.region.Region) UpgradeService(org.onosproject.upgrade.UpgradeService) AbstractListenerManager(org.onosproject.event.AbstractListenerManager) Deactivate(org.osgi.service.component.annotations.Deactivate) Collection(java.util.Collection) MastershipTermService(org.onosproject.mastership.MastershipTermService) MastershipInfo(org.onosproject.mastership.MastershipInfo) Set(java.util.Set) Sets(com.google.common.collect.Sets) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) MastershipStore(org.onosproject.mastership.MastershipStore) Timer(com.codahale.metrics.Timer) ClusterService(org.onosproject.cluster.ClusterService) DeviceId(org.onosproject.net.DeviceId) MastershipStoreDelegate(org.onosproject.mastership.MastershipStoreDelegate) MetricsUtil.stopTimer(org.onlab.metrics.MetricsUtil.stopTimer) AppGuard.checkPermission(org.onosproject.security.AppGuard.checkPermission) MetricsService(org.onlab.metrics.MetricsService) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ControllerNode(org.onosproject.cluster.ControllerNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MetricsHelper(org.onosproject.core.MetricsHelper) MastershipEvent(org.onosproject.mastership.MastershipEvent) Component(org.osgi.service.component.annotations.Component) Lists(com.google.common.collect.Lists) CompletableFuture.allOf(java.util.concurrent.CompletableFuture.allOf) Context(com.codahale.metrics.Timer.Context) Activate(org.osgi.service.component.annotations.Activate) ComponentConfigService(org.onosproject.cfg.ComponentConfigService) UpgradeEventListener(org.onosproject.upgrade.UpgradeEventListener) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) MetricsUtil.startTimer(org.onlab.metrics.MetricsUtil.startTimer) MastershipRole(org.onosproject.net.MastershipRole) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) CLUSTER_WRITE(org.onosproject.security.AppPermission.Type.CLUSTER_WRITE) MastershipTerm(org.onosproject.mastership.MastershipTerm) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) UpgradeEvent(org.onosproject.upgrade.UpgradeEvent) Futures(com.google.common.util.concurrent.Futures) ConfigProperty(org.onosproject.cfg.ConfigProperty) Modified(org.osgi.service.component.annotations.Modified) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) RoleInfo(org.onosproject.cluster.RoleInfo) MastershipListener(org.onosproject.mastership.MastershipListener) Reference(org.osgi.service.component.annotations.Reference) Comparator(java.util.Comparator) MastershipAdminService(org.onosproject.mastership.MastershipAdminService) Collections(java.util.Collections) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) DeviceId(org.onosproject.net.DeviceId) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) ControllerNode(org.onosproject.cluster.ControllerNode) CompletableFuture(java.util.concurrent.CompletableFuture)

Example 2 with MASTER

use of org.onosproject.net.MastershipRole.MASTER 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)

Aggregations

Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 Lists (com.google.common.collect.Lists)2 Futures (com.google.common.util.concurrent.Futures)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ClusterService (org.onosproject.cluster.ClusterService)2 ControllerNode (org.onosproject.cluster.ControllerNode)2 NodeId (org.onosproject.cluster.NodeId)2 MastershipAdminService (org.onosproject.mastership.MastershipAdminService)2 MastershipEvent (org.onosproject.mastership.MastershipEvent)2 MastershipListener (org.onosproject.mastership.MastershipListener)2 MastershipService (org.onosproject.mastership.MastershipService)2 MastershipTerm (org.onosproject.mastership.MastershipTerm)2 Timer (com.codahale.metrics.Timer)1 Context (com.codahale.metrics.Timer.Context)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1