Search in sources :

Example 51 with NodeId

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

the class DefaultClusterMetadataProvider method activate.

@Activate
public void activate() {
    String localIp = getSiteLocalAddress();
    ControllerNode localNode = new DefaultControllerNode(new NodeId(localIp), IpAddress.valueOf(localIp), DEFAULT_ONOS_PORT);
    ClusterMetadata metadata = new ClusterMetadata(PROVIDER_ID, "default", localNode, ImmutableSet.of(), ImmutableSet.of(), UUID.randomUUID().toString());
    long version = System.currentTimeMillis();
    cachedMetadata.set(new Versioned<>(metadata, version));
    providerRegistry.register(this);
    log.info("Started");
}
Also used : ClusterMetadata(org.onosproject.cluster.ClusterMetadata) NodeId(org.onosproject.cluster.NodeId) ControllerNode(org.onosproject.cluster.ControllerNode) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) Activate(org.osgi.service.component.annotations.Activate)

Example 52 with NodeId

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

the class MastershipManagerTest method checkDeviceMasters.

private void checkDeviceMasters(Set<DeviceId> deviceIds, Set<NodeId> expectedMasters, Consumer<DeviceId> checkRole) {
    // each device's master must be contained in the list of expectedMasters
    deviceIds.forEach(deviceId -> {
        assertTrue("wrong master:", expectedMasters.contains(mgr.getMasterFor(deviceId)));
        if (checkRole != null) {
            checkRole.accept(deviceId);
        }
    });
    // each node in expectedMasters must have approximately the same number of devices
    if (expectedMasters.size() > 1) {
        int minValue = Integer.MAX_VALUE;
        int maxDevices = -1;
        for (NodeId nodeId : expectedMasters) {
            int numDevicesManagedByNode = mgr.getDevicesOf(nodeId).size();
            if (numDevicesManagedByNode < minValue) {
                minValue = numDevicesManagedByNode;
            }
            if (numDevicesManagedByNode > maxDevices) {
                maxDevices = numDevicesManagedByNode;
            }
            assertTrue("not balanced:", maxDevices - minValue <= 1);
        }
    }
}
Also used : NodeId(org.onosproject.cluster.NodeId)

Example 53 with NodeId

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

the class MastershipManagerTest method balanceWithRegion1.

@Test
public void balanceWithRegion1() {
    // set up region - 2 sets of masters with 1 node in each
    Set<NodeId> masterSet1 = ImmutableSet.of(NID1);
    Set<NodeId> masterSet2 = ImmutableSet.of(NID2);
    List<Set<NodeId>> masters = ImmutableList.of(masterSet1, masterSet2);
    Region r = regionManager.createRegion(RID1, "R1", METRO, masters);
    regionManager.addDevices(RID1, ImmutableSet.of(DID1, DID2));
    Set<DeviceId> deviceIds = regionManager.getRegionDevices(RID1);
    assertEquals("incorrect device count", 2, deviceIds.size());
    testClusterService.put(CNODE1, ControllerNode.State.ACTIVE);
    testClusterService.put(CNODE2, ControllerNode.State.ACTIVE);
    // set master to non region nodes
    mgr.setRole(NID_LOCAL, DID1, MASTER);
    mgr.setRole(NID_LOCAL, DID2, MASTER);
    assertEquals("wrong local role:", MASTER, mgr.getLocalRole(DID1));
    assertEquals("wrong local role:", MASTER, mgr.getLocalRole(DID2));
    assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DID1));
    assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DID2));
    // do region balancing
    mgr.useRegionForBalanceRoles = true;
    mgr.balanceRoles();
    assertEquals("wrong master:", NID1, mgr.getMasterFor(DID1));
    assertEquals("wrong master:", NID1, mgr.getMasterFor(DID2));
    // make N1 inactive
    testClusterService.put(CNODE1, ControllerNode.State.INACTIVE);
    mgr.balanceRoles();
    assertEquals("wrong master:", NID2, mgr.getMasterFor(DID1));
    assertEquals("wrong master:", NID2, mgr.getMasterFor(DID2));
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) DeviceId(org.onosproject.net.DeviceId) NodeId(org.onosproject.cluster.NodeId) Region(org.onosproject.net.region.Region) Test(org.junit.Test)

Example 54 with NodeId

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

the class MastershipManagerTest method balanceWithRegion2.

@Test
public void balanceWithRegion2() {
    // set up region - 2 sets of masters with (3 nodes, 1 node)
    Set<NodeId> masterSet1 = ImmutableSet.of(NID1, NID3, NID4);
    Set<NodeId> masterSet2 = ImmutableSet.of(NID2);
    List<Set<NodeId>> masters = ImmutableList.of(masterSet1, masterSet2);
    Region r = regionManager.createRegion(RID1, "R1", METRO, masters);
    Set<DeviceId> deviceIdsOrig = ImmutableSet.of(DID1, DID2, DID3, DEV_OTHER);
    regionManager.addDevices(RID1, deviceIdsOrig);
    Set<DeviceId> deviceIds = regionManager.getRegionDevices(RID1);
    assertEquals("incorrect device count", deviceIdsOrig.size(), deviceIds.size());
    assertEquals("incorrect devices in region", deviceIdsOrig, deviceIds);
    testClusterService.put(CNODE1, ControllerNode.State.ACTIVE);
    testClusterService.put(CNODE2, ControllerNode.State.ACTIVE);
    testClusterService.put(CNODE3, ControllerNode.State.ACTIVE);
    testClusterService.put(CNODE4, ControllerNode.State.ACTIVE);
    // set master to non region nodes
    deviceIdsOrig.forEach(deviceId1 -> mgr.setRole(NID_LOCAL, deviceId1, MASTER));
    checkDeviceMasters(deviceIds, Sets.newHashSet(NID_LOCAL), deviceId -> assertEquals("wrong local role:", MASTER, mgr.getLocalRole(deviceId)));
    // do region balancing
    mgr.useRegionForBalanceRoles = true;
    mgr.balanceRoles();
    Set<NodeId> expectedMasters = Sets.newHashSet(NID1, NID3, NID4);
    checkDeviceMasters(deviceIds, expectedMasters);
    // make N1 inactive
    testClusterService.put(CNODE1, ControllerNode.State.INACTIVE);
    expectedMasters.remove(NID1);
    mgr.balanceRoles();
    checkDeviceMasters(deviceIds, expectedMasters);
    // make N4 inactive
    testClusterService.put(CNODE4, ControllerNode.State.INACTIVE);
    expectedMasters.remove(NID4);
    mgr.balanceRoles();
    checkDeviceMasters(deviceIds, expectedMasters);
    // make N3 inactive
    testClusterService.put(CNODE3, ControllerNode.State.INACTIVE);
    expectedMasters = Sets.newHashSet(NID2);
    mgr.balanceRoles();
    checkDeviceMasters(deviceIds, expectedMasters);
    // make N3 active
    testClusterService.put(CNODE3, ControllerNode.State.ACTIVE);
    expectedMasters = Sets.newHashSet(NID3);
    mgr.balanceRoles();
    checkDeviceMasters(deviceIds, expectedMasters);
    // make N4 active
    testClusterService.put(CNODE4, ControllerNode.State.ACTIVE);
    expectedMasters.add(NID4);
    mgr.balanceRoles();
    checkDeviceMasters(deviceIds, expectedMasters);
    // make N1 active
    testClusterService.put(CNODE1, ControllerNode.State.ACTIVE);
    expectedMasters.add(NID1);
    mgr.balanceRoles();
    checkDeviceMasters(deviceIds, expectedMasters);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) DeviceId(org.onosproject.net.DeviceId) NodeId(org.onosproject.cluster.NodeId) Region(org.onosproject.net.region.Region) Test(org.junit.Test)

Example 55 with NodeId

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

the class ConsistentDeviceMastershipStore method getRole.

@Override
public MastershipRole getRole(NodeId nodeId, DeviceId deviceId) {
    checkArgument(nodeId != null, NODE_ID_NULL);
    checkArgument(deviceId != null, DEVICE_ID_NULL);
    String leadershipTopic = createDeviceMastershipTopic(deviceId);
    Leadership leadership = leadershipService.getLeadership(leadershipTopic);
    NodeId leader = leadership == null ? null : leadership.leaderNodeId();
    List<NodeId> candidates = leadership == null ? ImmutableList.of() : ImmutableList.copyOf(leadership.candidates());
    return Objects.equals(nodeId, leader) ? MastershipRole.MASTER : candidates.contains(nodeId) ? MastershipRole.STANDBY : MastershipRole.NONE;
}
Also used : Leadership(org.onosproject.cluster.Leadership) 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