Search in sources :

Example 11 with MastershipEvent

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

the class SimpleMastershipStore method setStandby.

@Override
public synchronized CompletableFuture<MastershipEvent> setStandby(NodeId nodeId, DeviceId deviceId) {
    MastershipRole role = getRole(nodeId, deviceId);
    switch(role) {
        case MASTER:
            NodeId backup = reelect(deviceId, nodeId);
            if (backup == null) {
                // no master alternative
                masterMap.remove(deviceId);
                // TODO: Should there be new event type for no MASTER?
                return CompletableFuture.completedFuture(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(deviceId)));
            } else {
                NodeId prevMaster = masterMap.put(deviceId, backup);
                incrementTerm(deviceId);
                addToBackup(deviceId, prevMaster);
                return CompletableFuture.completedFuture(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(deviceId)));
            }
        case STANDBY:
        case NONE:
            boolean modified = addToBackup(deviceId, nodeId);
            if (modified) {
                return CompletableFuture.completedFuture(new MastershipEvent(BACKUPS_CHANGED, deviceId, getMastership(deviceId)));
            }
            break;
        default:
            log.warn("unknown Mastership Role {}", role);
    }
    return null;
}
Also used : MastershipEvent(org.onosproject.mastership.MastershipEvent) NodeId(org.onosproject.cluster.NodeId) MastershipRole(org.onosproject.net.MastershipRole)

Example 12 with MastershipEvent

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

the class SimpleMastershipStoreTest method unsetMaster.

@Test
public void unsetMaster() {
    // NONE - record backup but take no other action
    put(DID1, N1, false, false);
    sms.setStandby(N1, DID1);
    assertTrue("not backed up", sms.backups.get(DID1).contains(N1));
    int prev = sms.termMap.get(DID1).get();
    sms.setStandby(N1, DID1);
    assertEquals("term should not change", prev, sms.termMap.get(DID1).get());
    // no backup, MASTER
    put(DID1, N1, true, false);
    assertNull("expect no MASTER event", Futures.getUnchecked(sms.setStandby(N1, DID1)).roleInfo().master());
    assertNull("wrong node", sms.masterMap.get(DID1));
    // backup, switch
    sms.masterMap.clear();
    put(DID1, N1, true, true);
    put(DID1, N2, false, true);
    put(DID2, N2, true, true);
    MastershipEvent event = Futures.getUnchecked(sms.setStandby(N1, DID1));
    assertEquals("wrong event", MASTER_CHANGED, event.type());
    assertEquals("wrong master", N2, event.roleInfo().master());
}
Also used : MastershipEvent(org.onosproject.mastership.MastershipEvent) Test(org.junit.Test)

Example 13 with MastershipEvent

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

the class SimpleMastershipStore method relinquishRole.

@Override
public synchronized CompletableFuture<MastershipEvent> relinquishRole(NodeId nodeId, DeviceId deviceId) {
    MastershipRole role = getRole(nodeId, deviceId);
    switch(role) {
        case MASTER:
            NodeId backup = reelect(deviceId, nodeId);
            masterMap.put(deviceId, backup);
            incrementTerm(deviceId);
            return CompletableFuture.completedFuture(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(deviceId)));
        case STANDBY:
            if (removeFromBackups(deviceId, nodeId)) {
                return CompletableFuture.completedFuture(new MastershipEvent(BACKUPS_CHANGED, deviceId, getMastership(deviceId)));
            }
            break;
        case NONE:
            break;
        default:
            log.warn("unknown Mastership Role {}", role);
    }
    return CompletableFuture.completedFuture(null);
}
Also used : MastershipEvent(org.onosproject.mastership.MastershipEvent) NodeId(org.onosproject.cluster.NodeId) MastershipRole(org.onosproject.net.MastershipRole)

Example 14 with MastershipEvent

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

the class SimpleMastershipStore method setMaster.

@Override
public synchronized CompletableFuture<MastershipEvent> setMaster(NodeId nodeId, DeviceId deviceId) {
    MastershipRole role = getRole(nodeId, deviceId);
    switch(role) {
        case MASTER:
            // no-op
            return CompletableFuture.completedFuture(null);
        case STANDBY:
        case NONE:
            NodeId prevMaster = masterMap.put(deviceId, nodeId);
            incrementTerm(deviceId);
            removeFromBackups(deviceId, nodeId);
            addToBackup(deviceId, prevMaster);
            break;
        default:
            log.warn("unknown Mastership Role {}", role);
            return null;
    }
    return CompletableFuture.completedFuture(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(deviceId)));
}
Also used : MastershipEvent(org.onosproject.mastership.MastershipEvent) NodeId(org.onosproject.cluster.NodeId) MastershipRole(org.onosproject.net.MastershipRole)

Example 15 with MastershipEvent

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

the class SimpleMastershipStore method requestRole.

@Override
public synchronized CompletableFuture<MastershipRole> requestRole(DeviceId deviceId) {
    // query+possible reelection
    NodeId node = clusterService.getLocalNode().id();
    MastershipRole role = getRole(node, deviceId);
    switch(role) {
        case MASTER:
            return CompletableFuture.completedFuture(MastershipRole.MASTER);
        case STANDBY:
            if (getMaster(deviceId) == null) {
                // no master => become master
                masterMap.put(deviceId, node);
                incrementTerm(deviceId);
                // remove from backup list
                removeFromBackups(deviceId, node);
                notifyDelegate(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(deviceId)));
                return CompletableFuture.completedFuture(MastershipRole.MASTER);
            }
            return CompletableFuture.completedFuture(MastershipRole.STANDBY);
        case NONE:
            if (getMaster(deviceId) == null) {
                // no master => become master
                masterMap.put(deviceId, node);
                incrementTerm(deviceId);
                notifyDelegate(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(deviceId)));
                return CompletableFuture.completedFuture(MastershipRole.MASTER);
            }
            // add to backup list
            if (addToBackup(deviceId, node)) {
                notifyDelegate(new MastershipEvent(BACKUPS_CHANGED, deviceId, getMastership(deviceId)));
            }
            return CompletableFuture.completedFuture(MastershipRole.STANDBY);
        default:
            log.warn("unknown Mastership Role {}", role);
    }
    return CompletableFuture.completedFuture(role);
}
Also used : MastershipEvent(org.onosproject.mastership.MastershipEvent) NodeId(org.onosproject.cluster.NodeId) MastershipRole(org.onosproject.net.MastershipRole)

Aggregations

MastershipEvent (org.onosproject.mastership.MastershipEvent)15 NodeId (org.onosproject.cluster.NodeId)8 MastershipRole (org.onosproject.net.MastershipRole)8 DeviceId (org.onosproject.net.DeviceId)4 Test (org.junit.Test)3 ClusterEvent (org.onosproject.cluster.ClusterEvent)2 Event (org.onosproject.event.Event)2 Link (org.onosproject.net.Link)2 DeviceEvent (org.onosproject.net.device.DeviceEvent)2 HostEvent (org.onosproject.net.host.HostEvent)2 IntentEvent (org.onosproject.net.intent.IntentEvent)2 LinkEvent (org.onosproject.net.link.LinkEvent)2 Topology (org.onosproject.net.topology.Topology)2 TopologyEvent (org.onosproject.net.topology.TopologyEvent)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 MoreObjects (com.google.common.base.MoreObjects)1 ImmutableList (com.google.common.collect.ImmutableList)1