Search in sources :

Example 16 with Leadership

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

the class VplsOperationManagerTest method testLeadershipEvent.

/**
 * Sends leadership event to the manager and checks if the manager is
 * leader or not.
 */
@Test
public void testLeadershipEvent() {
    vplsOperationManager.isLeader = false;
    vplsOperationManager.localNodeId = NODE_ID_1;
    // leader changed to self
    Leader leader = new Leader(NODE_ID_1, 0, 0);
    Leadership leadership = new Leadership(APP_NAME, leader, ImmutableList.of());
    LeadershipEvent event = new LeadershipEvent(LeadershipEvent.Type.LEADER_CHANGED, leadership);
    ((TestLeadershipService) vplsOperationManager.leadershipService).sendEvent(event);
    assertTrue(vplsOperationManager.isLeader);
    // leader changed to other
    leader = new Leader(NODE_ID_2, 0, 0);
    leadership = new Leadership(APP_NAME, leader, ImmutableList.of());
    event = new LeadershipEvent(LeadershipEvent.Type.LEADER_CHANGED, leadership);
    ((TestLeadershipService) vplsOperationManager.leadershipService).sendEvent(event);
    assertFalse(vplsOperationManager.isLeader);
}
Also used : LeadershipEvent(org.onosproject.cluster.LeadershipEvent) Leadership(org.onosproject.cluster.Leadership) Leader(org.onosproject.cluster.Leader) Test(org.junit.Test)

Example 17 with Leadership

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

the class LeaderCommand method displayCandidates.

private void displayCandidates(Map<String, Leadership> leaderBoard) {
    print("--------------------------------------------------------------------------------------------");
    print(FMT_C, "Topic", "Leader", "Term", "Elected", "Candidates");
    print("--------------------------------------------------------------------------------------------");
    leaderBoard.entrySet().stream().filter(es -> allTopics || pattern.matcher(es.getKey()).matches()).sorted((a, b) -> leadershipComparator.compare(a.getValue(), b.getValue())).forEach(es -> {
        Leadership l = es.getValue();
        List<NodeId> candidateList = l.candidates();
        if (candidateList == null || candidateList.isEmpty()) {
            return;
        }
        print(FMT_C, es.getKey(), String.valueOf(l.leaderNodeId()), l.leader().term(), Tools.timeAgo(l.leader().termStartTime()), // formatting hacks to get it into a table
        candidateList.get(0).toString());
        candidateList.subList(1, candidateList.size()).forEach(n -> print(FMT_C, " ", " ", " ", " ", n));
        print(FMT_C, " ", " ", " ", " ", " ");
    });
    print("--------------------------------------------------------------------------------------------");
}
Also used : NodeId(org.onosproject.cluster.NodeId) Tools(org.onlab.util.Tools) LeadershipAdminService(org.onosproject.cluster.LeadershipAdminService) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Argument(org.apache.karaf.shell.api.action.Argument) Leadership(org.onosproject.cluster.Leadership) Command(org.apache.karaf.shell.api.action.Command) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) List(java.util.List) Map(java.util.Map) Service(org.apache.karaf.shell.api.action.lifecycle.Service) JsonNode(com.fasterxml.jackson.databind.JsonNode) Pattern(java.util.regex.Pattern) Option(org.apache.karaf.shell.api.action.Option) Comparator(java.util.Comparator) Leadership(org.onosproject.cluster.Leadership) NodeId(org.onosproject.cluster.NodeId)

Example 18 with Leadership

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

the class DynamicConfigManager method activate.

@Activate
public void activate() {
    NodeId localnodeId = clusterService.getLocalNode().id();
    Leadership leadership = leadershipService.runForLeadership(DCS_STORE_INIT);
    if (leadership.leaderNodeId().equals(localnodeId)) {
        initStore();
    }
    store.setDelegate(storeDelegate);
    eventDispatcher.addSink(DynamicConfigEvent.class, listenerRegistry);
    log.info("Started");
}
Also used : Leadership(org.onosproject.cluster.Leadership) NodeId(org.onosproject.cluster.NodeId) Activate(org.osgi.service.component.annotations.Activate)

Example 19 with Leadership

use of org.onosproject.cluster.Leadership in project trellis-control by opennetworkinglab.

the class DefaultRoutingHandlerTest method testShouldHandleRoutingCase5.

/*
     * Node 1 is the leader of 1A, 1B. Node 2 becomes the leader of 1A, 1B later
     * shouldP is not purged in time. This can easily happen if we dont get in
     * time cluster/mastership events. shouldProgram absorbs this negative scenario.
     */
@Test
public void testShouldHandleRoutingCase5() {
    expect(mockWps.leadershipService.getLeadership(DEV1A_PARTITION_ID)).andReturn(new Leadership(DEV1A_PARTITION_ID, new Leader(NODE1, 0, 0), List.of(NODE2, NODE3))).anyTimes();
    expect(mockWps.leadershipService.getLeadership(DEV1B_PARTITION_ID)).andReturn(new Leadership(DEV1B_PARTITION_ID, new Leader(NODE1, 0, 0), List.of(NODE2, NODE3))).anyTimes();
    replay(mockWps.leadershipService);
    expect(srManager.getPairDeviceId(DEV1A)).andReturn(Optional.of(DEV1B)).anyTimes();
    expect(srManager.getPairDeviceId(DEV1B)).andReturn(Optional.of(DEV1A)).anyTimes();
    replay(srManager);
    // Node 1 should program both 1A and 1B
    expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE1, IP1)).anyTimes();
    replay(srManager.clusterService);
    assertNull(dfh.shouldProgram.get(DEV1A));
    assertNull(dfh.shouldProgram.get(DEV1B));
    assertTrue(dfh.shouldProgram(DEV1A));
    assertTrue(dfh.shouldProgram(DEV1B));
    assertEquals(NODE1, dfh.shouldProgram.get(DEV1A));
    assertEquals(NODE1, dfh.shouldProgram.get(DEV1B));
    reset(srManager.clusterService);
    clearShouldProgram();
    // Node 2 should program no device
    expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE2, IP2)).anyTimes();
    replay(srManager.clusterService);
    assertNull(dfh.shouldProgram.get(DEV1A));
    assertNull(dfh.shouldProgram.get(DEV1B));
    assertFalse(dfh.shouldProgram(DEV1A));
    assertFalse(dfh.shouldProgram(DEV1B));
    assertNotEquals(NODE2, dfh.shouldProgram.get(DEV1A));
    assertNotEquals(NODE2, dfh.shouldProgram.get(DEV1B));
    // Leadership moves to Node 2
    reset(mockWps.leadershipService);
    expect(mockWps.leadershipService.getLeadership(DEV1A_PARTITION_ID)).andReturn(new Leadership(DEV1A_PARTITION_ID, new Leader(NODE2, 0, 0), List.of(NODE2, NODE3))).anyTimes();
    expect(mockWps.leadershipService.getLeadership(DEV1B_PARTITION_ID)).andReturn(new Leadership(DEV1B_PARTITION_ID, new Leader(NODE2, 0, 0), List.of(NODE2, NODE3))).anyTimes();
    replay(mockWps.leadershipService);
    reset(srManager.clusterService);
    // Node 1 should program 1A, 1B
    expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE1, IP1)).anyTimes();
    replay(srManager.clusterService);
    assertNotNull(dfh.shouldProgram.get(DEV1A));
    assertNotNull(dfh.shouldProgram.get(DEV1B));
    assertTrue(dfh.shouldProgram(DEV1A));
    assertTrue(dfh.shouldProgram(DEV1B));
    assertEquals(NODE1, dfh.shouldProgram.get(DEV1A));
    assertEquals(NODE1, dfh.shouldProgram.get(DEV1B));
    reset(srManager.clusterService);
    // Node 2 should program no device
    expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE2, IP2)).anyTimes();
    replay(srManager.clusterService);
    assertNotNull(dfh.shouldProgram.get(DEV1A));
    assertNotNull(dfh.shouldProgram.get(DEV1B));
    assertFalse(dfh.shouldProgram(DEV1A));
    assertFalse(dfh.shouldProgram(DEV1B));
    assertEquals(NODE1, dfh.shouldProgram.get(DEV1A));
    assertEquals(NODE1, dfh.shouldProgram.get(DEV1B));
}
Also used : Leadership(org.onosproject.cluster.Leadership) Leader(org.onosproject.cluster.Leader) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) Test(org.junit.Test)

Example 20 with Leadership

use of org.onosproject.cluster.Leadership in project trellis-control by opennetworkinglab.

the class DefaultRoutingHandlerTest method testShouldHandleRoutingCase3.

/*
     * Node 1 is the leader of switch 1A, 1B
     * Node 3 is the leader of switch 2
     */
@Test
public void testShouldHandleRoutingCase3() {
    expect(mockWps.leadershipService.getLeadership(DEV1A_PARTITION_ID)).andReturn(new Leadership(DEV1A_PARTITION_ID, new Leader(NODE1, 0, 0), List.of(NODE2, NODE3))).anyTimes();
    expect(mockWps.leadershipService.getLeadership(DEV1B_PARTITION_ID)).andReturn(new Leadership(DEV1B_PARTITION_ID, new Leader(NODE1, 0, 0), List.of(NODE2, NODE3))).anyTimes();
    expect(mockWps.leadershipService.getLeadership(DEV2_PARTITION_ID)).andReturn(new Leadership(DEV2_PARTITION_ID, new Leader(NODE3, 0, 0), List.of(NODE2, NODE3))).anyTimes();
    replay(mockWps.leadershipService);
    expect(srManager.getPairDeviceId(DEV1A)).andReturn(Optional.of(DEV1B)).anyTimes();
    expect(srManager.getPairDeviceId(DEV1B)).andReturn(Optional.of(DEV1A)).anyTimes();
    expect(srManager.getPairDeviceId(DEV2)).andReturn(Optional.empty()).anyTimes();
    replay(srManager);
    // Node 1 should program 1A, 1B
    expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE1, IP1)).anyTimes();
    replay(srManager.clusterService);
    assertNull(dfh.shouldProgram.get(DEV1A));
    assertNull(dfh.shouldProgram.get(DEV1B));
    assertNull(dfh.shouldProgram.get(DEV2));
    assertTrue(dfh.shouldProgram(DEV1A));
    assertTrue(dfh.shouldProgram(DEV1B));
    assertFalse(dfh.shouldProgram(DEV2));
    assertEquals(NODE1, dfh.shouldProgram.get(DEV1A));
    assertEquals(NODE1, dfh.shouldProgram.get(DEV1B));
    assertNotEquals(NODE1, dfh.shouldProgram.get(DEV2));
    reset(srManager.clusterService);
    clearShouldProgram();
    // Node 2 should program no device
    expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE2, IP2)).anyTimes();
    replay(srManager.clusterService);
    assertNull(dfh.shouldProgram.get(DEV1A));
    assertNull(dfh.shouldProgram.get(DEV1B));
    assertNull(dfh.shouldProgram.get(DEV2));
    assertFalse(dfh.shouldProgram(DEV1A));
    assertFalse(dfh.shouldProgram(DEV1B));
    assertFalse(dfh.shouldProgram(DEV2));
    assertNotEquals(NODE2, dfh.shouldProgram.get(DEV1A));
    assertNotEquals(NODE2, dfh.shouldProgram.get(DEV1B));
    assertNotEquals(NODE2, dfh.shouldProgram.get(DEV2));
    reset(srManager.clusterService);
    clearShouldProgram();
    // Node 3 should program 2
    expect(srManager.clusterService.getLocalNode()).andReturn(new DefaultControllerNode(NODE3, IP3)).anyTimes();
    replay(srManager.clusterService);
    assertNull(dfh.shouldProgram.get(DEV1A));
    assertNull(dfh.shouldProgram.get(DEV1B));
    assertNull(dfh.shouldProgram.get(DEV2));
    assertFalse(dfh.shouldProgram(DEV1A));
    assertFalse(dfh.shouldProgram(DEV1B));
    assertTrue(dfh.shouldProgram(DEV2));
    assertNotEquals(NODE3, dfh.shouldProgram.get(DEV1A));
    assertNotEquals(NODE3, dfh.shouldProgram.get(DEV1B));
    assertEquals(NODE3, dfh.shouldProgram.get(DEV2));
}
Also used : Leadership(org.onosproject.cluster.Leadership) Leader(org.onosproject.cluster.Leader) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) Test(org.junit.Test)

Aggregations

Leadership (org.onosproject.cluster.Leadership)20 Leader (org.onosproject.cluster.Leader)7 Test (org.junit.Test)6 NodeId (org.onosproject.cluster.NodeId)6 DefaultControllerNode (org.onosproject.cluster.DefaultControllerNode)5 LeadershipEvent (org.onosproject.cluster.LeadershipEvent)3 Map (java.util.Map)2 LeadershipAdminService (org.onosproject.cluster.LeadershipAdminService)2 Activate (org.osgi.service.component.annotations.Activate)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)1 Maps (com.google.common.collect.Maps)1 Comparator (java.util.Comparator)1 Dictionary (java.util.Dictionary)1 List (java.util.List)1 Objects (java.util.Objects)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1