Ayaka Koshibe

base for mastership term implementation

Change-Id: Ie354096ad8835536839e3c1504e19a2cbff866c1
......@@ -56,6 +56,13 @@ public interface MastershipService {
Set<DeviceId> getDevicesOf(NodeId nodeId);
/**
* Returns the mastership term service for getting term information.
*
* @return the MastershipTermService for this mastership manager
*/
MastershipTermService requestTermService();
/**
* Adds the specified mastership change listener.
*
* @param listener the mastership listener
......
......@@ -55,4 +55,13 @@ public interface MastershipStore extends Store<MastershipEvent, MastershipStoreD
* @return a mastership event
*/
MastershipEvent setMaster(NodeId nodeId, DeviceId deviceId);
/**
* Returns the current master and number of past mastership hand-offs
* (terms) for a device.
*
* @param deviceId the device identifier
* @return the current master's ID and the term value for device, or null
*/
MastershipTerm getTermFor(DeviceId deviceId);
}
......
package org.onlab.onos.cluster;
public class MastershipTerm {
private final NodeId master = null;
import java.util.Objects;
public final class MastershipTerm {
private final NodeId master;
private int termNumber;
private MastershipTerm(NodeId master, int term) {
this.master = master;
this.termNumber = term;
}
public static MastershipTerm of(NodeId master, int term) {
return new MastershipTerm(master, term);
}
public NodeId master() {
return master;
}
public int termNumber() {
return termNumber;
}
@Override
public int hashCode() {
return Objects.hash(master, termNumber);
}
@Override
public boolean equals(Object other) {
if (other instanceof MastershipTerm) {
MastershipTerm that = (MastershipTerm) other;
if (!this.master.equals(that.master)) {
return false;
}
if (this.termNumber != that.termNumber) {
return false;
}
return true;
}
return false;
}
}
......
......@@ -40,4 +40,9 @@ public class MastershipServiceAdapter implements MastershipService {
@Override
public void removeListener(MastershipListener listener) {
}
@Override
public MastershipTermService requestTermService() {
return null;
}
}
......
......@@ -12,6 +12,8 @@ import org.onlab.onos.cluster.MastershipEvent;
import org.onlab.onos.cluster.MastershipListener;
import org.onlab.onos.cluster.MastershipService;
import org.onlab.onos.cluster.MastershipStore;
import org.onlab.onos.cluster.MastershipTerm;
import org.onlab.onos.cluster.MastershipTermService;
import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.event.AbstractListenerRegistry;
import org.onlab.onos.event.EventDeliveryService;
......@@ -103,6 +105,12 @@ public class MastershipManager
return store.getDevices(nodeId);
}
@Override
public MastershipTermService requestTermService() {
return new InternalMastershipTermService();
}
@Override
public void addListener(MastershipListener listener) {
checkNotNull(listener);
......@@ -124,4 +132,13 @@ public class MastershipManager
}
}
private class InternalMastershipTermService implements MastershipTermService {
@Override
public MastershipTerm getMastershipTerm(DeviceId deviceId) {
return store.getTermFor(deviceId);
}
}
}
......
......@@ -15,6 +15,7 @@ import org.onlab.onos.cluster.ClusterService;
import org.onlab.onos.cluster.MastershipEvent;
import org.onlab.onos.cluster.MastershipStore;
import org.onlab.onos.cluster.MastershipStoreDelegate;
import org.onlab.onos.cluster.MastershipTerm;
import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.MastershipRole;
......@@ -115,4 +116,10 @@ public class DistributedMastershipStore
return nodeId.equals(master) ? MastershipRole.MASTER : MastershipRole.STANDBY;
}
@Override
public MastershipTerm getTermFor(DeviceId deviceId) {
// TODO Auto-generated method stub
return null;
}
}
......
......@@ -3,11 +3,13 @@ package org.onlab.onos.net.trivial.impl;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
......@@ -18,6 +20,7 @@ import org.onlab.onos.cluster.DefaultControllerNode;
import org.onlab.onos.cluster.MastershipEvent;
import org.onlab.onos.cluster.MastershipStore;
import org.onlab.onos.cluster.MastershipStoreDelegate;
import org.onlab.onos.cluster.MastershipTerm;
import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.MastershipRole;
......@@ -47,6 +50,7 @@ public class SimpleMastershipStore
//devices mapped to their masters, to emulate multiple nodes
protected final ConcurrentMap<DeviceId, NodeId> masterMap =
new ConcurrentHashMap<>();
protected final Map<DeviceId, AtomicInteger> termMap = new HashMap<>();
@Activate
public void activate() {
......@@ -63,15 +67,21 @@ public class SimpleMastershipStore
NodeId node = masterMap.get(deviceId);
if (node == null) {
masterMap.put(deviceId, nodeId);
synchronized (this) {
masterMap.put(deviceId, nodeId);
termMap.put(deviceId, new AtomicInteger());
}
return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId);
}
if (node.equals(nodeId)) {
return null;
} else {
masterMap.put(deviceId, nodeId);
return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId);
synchronized (this) {
masterMap.put(deviceId, nodeId);
termMap.get(deviceId).incrementAndGet();
return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId);
}
}
}
......@@ -114,4 +124,13 @@ public class SimpleMastershipStore
return role;
}
@Override
public MastershipTerm getTermFor(DeviceId deviceId) {
if (masterMap.get(deviceId) == null) {
return null;
}
return MastershipTerm.of(
masterMap.get(deviceId), termMap.get(deviceId).get());
}
}
......