Search in sources :

Example 26 with NodeId

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

the class GossipDeviceStore method removeDevice.

@Override
public synchronized DeviceEvent removeDevice(DeviceId deviceId) {
    final NodeId myId = clusterService.getLocalNode().id();
    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);
        if (mastershipService.requestRoleFor(deviceId).join() == MastershipRole.MASTER) {
            master = myId;
        }
    }
    boolean isMaster = myId.equals(master);
    // If this node is not the master, forward the request.
    if (!isMaster) {
        log.debug("{} has control of {}, forwarding remove request", master, deviceId);
        // TODO check unicast return value
        clusterCommunicator.unicast(deviceId, DEVICE_REMOVE_REQ, SERIALIZER::encode, master);
    /* error log:
             log.error("Failed to forward {} remove request to {}", deviceId, master, e);
             */
    }
    // If this node is the master, get a timestamp. Otherwise, default to the current device timestamp.
    Timestamp timestamp = isMaster ? deviceClockService.getTimestamp(deviceId) : null;
    DeviceEvent event = removeDeviceInternal(deviceId, timestamp);
    // If this node is the master, update peers.
    if (isMaster && event != null) {
        log.debug("Notifying peers of a device removed topology event for deviceId: {}", deviceId);
        notifyPeers(new InternalDeviceRemovedEvent(deviceId, timestamp));
    }
    notifyDelegateIfNotNull(event);
    // Relinquish mastership if acquired to remove the device.
    if (relinquishAtEnd) {
        log.debug("Relinquishing temporary role acquired for {}", deviceId);
        mastershipService.relinquishMastership(deviceId);
    }
    return event;
}
Also used : DeviceEvent(org.onosproject.net.device.DeviceEvent) ControllerNodeToNodeId.toNodeId(org.onosproject.cluster.ControllerNodeToNodeId.toNodeId) NodeId(org.onosproject.cluster.NodeId) MastershipRole(org.onosproject.net.MastershipRole) WallClockTimestamp(org.onosproject.store.service.WallClockTimestamp) Timestamp(org.onosproject.store.Timestamp) MastershipBasedTimestamp(org.onosproject.store.impl.MastershipBasedTimestamp) MultiValuedTimestamp(org.onosproject.store.service.MultiValuedTimestamp)

Example 27 with NodeId

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

the class UpgradeManagerTest method createUpgradeManager.

/**
 * Creates a new upgrade manager to test.
 *
 * @param version the local node version
 * @param state the initial upgrade state
 * @param versions a list of controller node versions
 * @return the activated upgrade manager
 */
@SuppressWarnings("unchecked")
private UpgradeManager createUpgradeManager(Version version, Upgrade state, List<Version> versions) {
    UpgradeManager upgradeManager = new UpgradeManager();
    injectEventDispatcher(upgradeManager, new TestEventDispatcher());
    upgradeManager.membershipService = new MembershipServiceAdapter() {

        @Override
        public MembershipGroup getLocalGroup() {
            return getGroups().stream().filter(group -> group.version().equals(version)).findFirst().get();
        }

        @Override
        public Collection<MembershipGroup> getGroups() {
            AtomicInteger nodeCounter = new AtomicInteger();
            Map<Version, Set<Member>> groups = Maps.newHashMap();
            versions.stream().forEach(version -> {
                groups.computeIfAbsent(version, k -> Sets.newHashSet()).add(new Member(NodeId.nodeId(String.valueOf(nodeCounter.getAndIncrement())), version));
            });
            return Maps.transformEntries(groups, MembershipGroup::new).values();
        }
    };
    upgradeManager.clusterService = new ClusterServiceAdapter() {

        @Override
        public Set<ControllerNode> getNodes() {
            AtomicInteger nodeCounter = new AtomicInteger();
            return versions.stream().map(v -> {
                int nodeId = nodeCounter.getAndIncrement();
                return new DefaultControllerNode(NodeId.nodeId(String.valueOf(nodeId)), IpAddress.valueOf("127.0.0.1"), nodeId);
            }).collect(Collectors.toSet());
        }

        @Override
        public ControllerNode getNode(NodeId nodeId) {
            return getNodes().stream().filter(node -> node.id().equals(nodeId)).findFirst().orElse(null);
        }

        @Override
        public Version getVersion(NodeId nodeId) {
            return versions.get(Integer.parseInt(nodeId.id()));
        }
    };
    upgradeManager.versionService = new VersionServiceAdapter() {

        @Override
        public Version version() {
            return version;
        }
    };
    upgradeManager.coordinationService = new CoordinationServiceAdapter() {

        @Override
        public <V> AtomicValueBuilder<V> atomicValueBuilder() {
            return new AtomicValueBuilder<V>() {

                @Override
                public AsyncAtomicValue<V> build() {
                    return new AsyncAtomicValueAdapter() {

                        @Override
                        public AtomicValue asAtomicValue() {
                            return new AtomicValueAdapter() {

                                private Object value = state;

                                @Override
                                public void set(Object value) {
                                    this.value = value;
                                }

                                @Override
                                public Object get() {
                                    return value;
                                }

                                @Override
                                public boolean compareAndSet(Object expect, Object update) {
                                    if ((value == null && expect == null) || (value != null && value.equals(expect))) {
                                        value = update;
                                        return true;
                                    }
                                    return false;
                                }
                            };
                        }
                    };
                }
            };
        }
    };
    upgradeManager.activate();
    return upgradeManager;
}
Also used : Arrays(java.util.Arrays) NetTestTools.injectEventDispatcher(org.onosproject.net.NetTestTools.injectEventDispatcher) Member(org.onosproject.cluster.Member) ClusterServiceAdapter(org.onosproject.cluster.ClusterServiceAdapter) ControllerNode(org.onosproject.cluster.ControllerNode) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) MembershipGroup(org.onosproject.cluster.MembershipGroup) TestEventDispatcher(org.onosproject.common.event.impl.TestEventDispatcher) AtomicValue(org.onosproject.store.service.AtomicValue) AtomicValueBuilder(org.onosproject.store.service.AtomicValueBuilder) Upgrade(org.onosproject.upgrade.Upgrade) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Version(org.onosproject.core.Version) Assert.fail(org.junit.Assert.fail) AsyncAtomicValue(org.onosproject.store.service.AsyncAtomicValue) IpAddress(org.onlab.packet.IpAddress) NodeId(org.onosproject.cluster.NodeId) VersionServiceAdapter(org.onosproject.core.VersionServiceAdapter) AtomicValueAdapter(org.onosproject.store.service.AtomicValueAdapter) ClusterEvent(org.onosproject.cluster.ClusterEvent) CoordinationServiceAdapter(org.onosproject.store.service.CoordinationServiceAdapter) Collection(java.util.Collection) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) AsyncAtomicValueAdapter(org.onosproject.store.service.AsyncAtomicValueAdapter) Sets(com.google.common.collect.Sets) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) MembershipServiceAdapter(org.onosproject.cluster.MembershipServiceAdapter) Set(java.util.Set) ControllerNode(org.onosproject.cluster.ControllerNode) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) AtomicValue(org.onosproject.store.service.AtomicValue) AsyncAtomicValue(org.onosproject.store.service.AsyncAtomicValue) AsyncAtomicValue(org.onosproject.store.service.AsyncAtomicValue) CoordinationServiceAdapter(org.onosproject.store.service.CoordinationServiceAdapter) Version(org.onosproject.core.Version) Member(org.onosproject.cluster.Member) TestEventDispatcher(org.onosproject.common.event.impl.TestEventDispatcher) MembershipServiceAdapter(org.onosproject.cluster.MembershipServiceAdapter) AtomicValueBuilder(org.onosproject.store.service.AtomicValueBuilder) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) AtomicValueAdapter(org.onosproject.store.service.AtomicValueAdapter) AsyncAtomicValueAdapter(org.onosproject.store.service.AsyncAtomicValueAdapter) AsyncAtomicValueAdapter(org.onosproject.store.service.AsyncAtomicValueAdapter) MembershipGroup(org.onosproject.cluster.MembershipGroup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterServiceAdapter(org.onosproject.cluster.ClusterServiceAdapter) NodeId(org.onosproject.cluster.NodeId) Collection(java.util.Collection) VersionServiceAdapter(org.onosproject.core.VersionServiceAdapter) Map(java.util.Map)

Example 28 with NodeId

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

the class RoleInfoProtoTranslator method translate.

/**
 * Translates gRPC RoleInfo to {@link RoleInfo}.
 *
 * @param roleInfo gRPC message
 * @return {@link RoleInfo}
 */
public static RoleInfo translate(RoleInfoProtoOuterClass.RoleInfoProto roleInfo) {
    NodeId master = NodeIdProtoTranslator.translate(roleInfo.getMaster());
    List<NodeId> backups = Lists.newArrayList();
    backups = roleInfo.getBackupsList().stream().map(r -> NodeIdProtoTranslator.translate(r)).collect(Collectors.toList());
    return new RoleInfo(master, backups);
}
Also used : RoleInfo(org.onosproject.cluster.RoleInfo) NodeId(org.onosproject.cluster.NodeId)

Example 29 with NodeId

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

the class CoordinationManager method eventuallyConsistentMapBuilder.

@Override
public <K, V> EventuallyConsistentMapBuilder<K, V> eventuallyConsistentMapBuilder() {
    checkPermission(STORAGE_WRITE);
    final NodeId localNodeId = clusterService.getLocalNode().id();
    Supplier<List<NodeId>> peersSupplier = () -> clusterService.getNodes().stream().map(ControllerNode::id).filter(nodeId -> !nodeId.equals(localNodeId)).filter(id -> clusterService.getState(id).isActive()).collect(Collectors.toList());
    Supplier<List<NodeId>> bootstrapPeersSupplier = () -> clusterService.getNodes().stream().map(ControllerNode::id).filter(id -> !localNodeId.equals(id)).filter(id -> clusterService.getState(id).isActive()).collect(Collectors.toList());
    return new EventuallyConsistentMapBuilderImpl<>(localNodeId, clusterCommunicator, persistenceService, peersSupplier, bootstrapPeersSupplier);
}
Also used : WorkQueue(org.onosproject.store.service.WorkQueue) STORAGE_WRITE(org.onosproject.security.AppPermission.Type.STORAGE_WRITE) PersistenceService(org.onosproject.persistence.PersistenceService) TopicBuilder(org.onosproject.store.service.TopicBuilder) AppGuard.checkPermission(org.onosproject.security.AppGuard.checkPermission) EventuallyConsistentMapBuilder(org.onosproject.store.service.EventuallyConsistentMapBuilder) CoordinationService(org.onosproject.store.service.CoordinationService) AtomixManager(org.onosproject.store.atomix.impl.AtomixManager) Supplier(java.util.function.Supplier) ControllerNode(org.onosproject.cluster.ControllerNode) Atomix(io.atomix.core.Atomix) AtomicCounterMapBuilder(org.onosproject.store.service.AtomicCounterMapBuilder) Component(org.osgi.service.component.annotations.Component) AtomicValueBuilder(org.onosproject.store.service.AtomicValueBuilder) ConsistentMultimapBuilder(org.onosproject.store.service.ConsistentMultimapBuilder) Activate(org.osgi.service.component.annotations.Activate) AtomicCounterBuilder(org.onosproject.store.service.AtomicCounterBuilder) NodeId(org.onosproject.cluster.NodeId) Serializer(org.onosproject.store.service.Serializer) LeaderElectorBuilder(org.onosproject.store.service.LeaderElectorBuilder) AsyncDocumentTree(org.onosproject.store.service.AsyncDocumentTree) DocumentTreeBuilder(org.onosproject.store.service.DocumentTreeBuilder) WorkQueueBuilder(org.onosproject.store.service.WorkQueueBuilder) Logger(org.slf4j.Logger) Topic(org.onosproject.store.service.Topic) AsyncConsistentMultimap(org.onosproject.store.service.AsyncConsistentMultimap) Deactivate(org.osgi.service.component.annotations.Deactivate) ConsistentMapBuilder(org.onosproject.store.service.ConsistentMapBuilder) PartitionGroup(io.atomix.primitive.partition.PartitionGroup) Collectors(java.util.stream.Collectors) ConsistentTreeMapBuilder(org.onosproject.store.service.ConsistentTreeMapBuilder) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) List(java.util.List) TransactionContextBuilder(org.onosproject.store.service.TransactionContextBuilder) AsyncConsistentTreeMap(org.onosproject.store.service.AsyncConsistentTreeMap) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ClusterCommunicationService(org.onosproject.store.cluster.messaging.ClusterCommunicationService) ClusterService(org.onosproject.cluster.ClusterService) DistributedSetBuilder(org.onosproject.store.service.DistributedSetBuilder) AtomicIdGeneratorBuilder(org.onosproject.store.service.AtomicIdGeneratorBuilder) Reference(org.osgi.service.component.annotations.Reference) NodeId(org.onosproject.cluster.NodeId) List(java.util.List)

Example 30 with NodeId

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

the class EventuallyConsistentMapImpl method antiEntropyCheckLocalItems.

/**
 * Processes anti-entropy ad from peer by taking following actions:
 * 1. If peer has an old entry, updates peer.
 * 2. If peer indicates an entry is removed and has a more recent
 * timestamp than the local entry, update local state.
 */
private List<EventuallyConsistentMapEvent<K, V>> antiEntropyCheckLocalItems(AntiEntropyAdvertisement<K> ad) {
    final List<EventuallyConsistentMapEvent<K, V>> externalEvents = Lists.newLinkedList();
    final NodeId sender = ad.sender();
    final List<NodeId> peers = ImmutableList.of(sender);
    Set<K> staleOrMissing = new HashSet<>();
    Set<K> locallyUnknown = new HashSet<>(ad.digest().keySet());
    items.forEach((key, localValue) -> {
        locallyUnknown.remove(key);
        MapValue.Digest remoteValueDigest = ad.digest().get(key);
        if (remoteValueDigest == null || localValue.isNewerThan(remoteValueDigest.timestamp())) {
            // local value is more recent, push to sender
            queueUpdate(new UpdateEntry<>(key, localValue), peers);
        } else if (remoteValueDigest.isNewerThan(localValue.digest()) && remoteValueDigest.isTombstone()) {
            // remote value is more recent and a tombstone: update local value
            MapValue<V> tombstone = MapValue.tombstone(remoteValueDigest.timestamp());
            MapValue<V> previousValue = removeInternal(key, Optional.empty(), Optional.of(tombstone));
            if (previousValue != null && previousValue.isAlive()) {
                externalEvents.add(new EventuallyConsistentMapEvent<>(mapName, REMOVE, key, previousValue.get()));
            }
        } else if (remoteValueDigest.isNewerThan(localValue.digest())) {
            // Not a tombstone and remote is newer
            staleOrMissing.add(key);
        }
    });
    // Keys missing in local map
    staleOrMissing.addAll(locallyUnknown);
    // Request updates that we missed out on
    sendUpdateRequestToPeer(sender, staleOrMissing);
    return externalEvents;
}
Also used : EventuallyConsistentMapEvent(org.onosproject.store.service.EventuallyConsistentMapEvent) NodeId(org.onosproject.cluster.NodeId) HashSet(java.util.HashSet)

Aggregations

NodeId (org.onosproject.cluster.NodeId)150 DeviceId (org.onosproject.net.DeviceId)38 ClusterService (org.onosproject.cluster.ClusterService)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