Search in sources :

Example 1 with MastershipInfo

use of org.onosproject.mastership.MastershipInfo in project onos by opennetworkinglab.

the class ConsistentDeviceMastershipStore method getNodes.

@Override
public RoleInfo getNodes(DeviceId deviceId) {
    checkArgument(deviceId != null, DEVICE_ID_NULL);
    MastershipInfo mastership = getMastership(deviceId);
    return new RoleInfo(mastership.master().orElse(null), mastership.backups());
}
Also used : RoleInfo(org.onosproject.cluster.RoleInfo) MastershipInfo(org.onosproject.mastership.MastershipInfo)

Example 2 with MastershipInfo

use of org.onosproject.mastership.MastershipInfo in project onos by opennetworkinglab.

the class ReplicaInfoManagerTest method testReplicaInfoEvent.

@Test
public void testReplicaInfoEvent() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    service.addListener(new MasterNodeCheck(latch, DID1, NID1));
    // fake MastershipEvent
    eventDispatcher.post(new MastershipEvent(Type.MASTER_CHANGED, DID1, new MastershipInfo(1, Optional.of(NID1), ImmutableMap.of(NID1, MastershipRole.MASTER))));
    assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : MastershipEvent(org.onosproject.mastership.MastershipEvent) MastershipInfo(org.onosproject.mastership.MastershipInfo) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with MastershipInfo

use of org.onosproject.mastership.MastershipInfo in project onos by opennetworkinglab.

the class GeneralDeviceProvider method roleChanged.

@Override
public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
    final MastershipInfo mastershipInfo = mastershipService.getMastershipFor(deviceId);
    final NodeId localNodeId = clusterService.getLocalNode().id();
    if (!mastershipInfo.getRole(localNodeId).equals(newRole)) {
        log.warn("Inconsistent mastership info for {}! Requested {}, but " + "mastership service reports {}, will apply the latter...", deviceId, newRole, mastershipInfo.getRole(localNodeId));
        newRole = mastershipInfo.getRole(localNodeId);
    }
    final DeviceHandshaker handshaker = getBehaviour(deviceId, DeviceHandshaker.class);
    if (handshaker == null) {
        log.error("Null handshaker. Unable to notify role {} to {}", newRole, deviceId);
        return;
    }
    // Derive preference value.
    final int preference;
    switch(newRole) {
        case MASTER:
            preference = 0;
            break;
        case STANDBY:
            preference = mastershipInfo.backups().indexOf(localNodeId) + 1;
            if (preference == 0) {
                // Not found in list.
                log.error("Unable to derive mastership preference for {}, " + "requested role {} but local node ID was " + "not found among list of backup nodes " + "reported by mastership service", deviceId, newRole);
                return;
            }
            break;
        case NONE:
            // No preference for NONE, apply as is.
            Pair<MastershipRole, Integer> pairRolePref = Pair.of(newRole, -1);
            if (log.isDebugEnabled()) {
                log.debug("Notifying role {} to {}", newRole, deviceId);
            } else if (!pairRolePref.equals(lastRoleRequest.get(deviceId))) {
                log.info("Notifying role {} to {}", newRole, deviceId);
            }
            lastRoleRequest.put(deviceId, pairRolePref);
            handshaker.roleChanged(newRole);
            return;
        default:
            log.error("Unrecognized mastership role {}", newRole);
            return;
    }
    Pair<MastershipRole, Integer> pairRolePref = Pair.of(newRole, preference);
    if (log.isDebugEnabled()) {
        log.debug("Notifying role {} (preference {}) for term {} to {}", newRole, preference, mastershipInfo.term(), deviceId);
    } else if (!pairRolePref.equals(lastRoleRequest.get(deviceId))) {
        log.info("Notifying role {} (preference {}) for term {} to {}", newRole, preference, mastershipInfo.term(), deviceId);
    }
    lastRoleRequest.put(deviceId, pairRolePref);
    try {
        handshaker.roleChanged(preference, mastershipInfo.term());
    } catch (UnsupportedOperationException e) {
        // Preference-based method not supported.
        handshaker.roleChanged(newRole);
    }
}
Also used : DeviceHandshaker(org.onosproject.net.device.DeviceHandshaker) NodeId(org.onosproject.cluster.NodeId) MastershipInfo(org.onosproject.mastership.MastershipInfo) MastershipRole(org.onosproject.net.MastershipRole)

Example 4 with MastershipInfo

use of org.onosproject.mastership.MastershipInfo 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 5 with MastershipInfo

use of org.onosproject.mastership.MastershipInfo 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)

Aggregations

MastershipInfo (org.onosproject.mastership.MastershipInfo)6 NodeId (org.onosproject.cluster.NodeId)4 MastershipRole (org.onosproject.net.MastershipRole)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 RoleInfo (org.onosproject.cluster.RoleInfo)2 MastershipEvent (org.onosproject.mastership.MastershipEvent)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1