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");
}
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);
}
}
}
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));
}
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);
}
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;
}
Aggregations