Ayaka Koshibe

base for mastership term implementation

Change-Id: Ie354096ad8835536839e3c1504e19a2cbff866c1
...@@ -56,6 +56,13 @@ public interface MastershipService { ...@@ -56,6 +56,13 @@ public interface MastershipService {
56 Set<DeviceId> getDevicesOf(NodeId nodeId); 56 Set<DeviceId> getDevicesOf(NodeId nodeId);
57 57
58 /** 58 /**
59 + * Returns the mastership term service for getting term information.
60 + *
61 + * @return the MastershipTermService for this mastership manager
62 + */
63 + MastershipTermService requestTermService();
64 +
65 + /**
59 * Adds the specified mastership change listener. 66 * Adds the specified mastership change listener.
60 * 67 *
61 * @param listener the mastership listener 68 * @param listener the mastership listener
......
...@@ -55,4 +55,13 @@ public interface MastershipStore extends Store<MastershipEvent, MastershipStoreD ...@@ -55,4 +55,13 @@ public interface MastershipStore extends Store<MastershipEvent, MastershipStoreD
55 * @return a mastership event 55 * @return a mastership event
56 */ 56 */
57 MastershipEvent setMaster(NodeId nodeId, DeviceId deviceId); 57 MastershipEvent setMaster(NodeId nodeId, DeviceId deviceId);
58 +
59 + /**
60 + * Returns the current master and number of past mastership hand-offs
61 + * (terms) for a device.
62 + *
63 + * @param deviceId the device identifier
64 + * @return the current master's ID and the term value for device, or null
65 + */
66 + MastershipTerm getTermFor(DeviceId deviceId);
58 } 67 }
......
1 package org.onlab.onos.cluster; 1 package org.onlab.onos.cluster;
2 2
3 -public class MastershipTerm { 3 +import java.util.Objects;
4 - private final NodeId master = null; 4 +
5 +public final class MastershipTerm {
6 +
7 + private final NodeId master;
5 private int termNumber; 8 private int termNumber;
9 +
10 + private MastershipTerm(NodeId master, int term) {
11 + this.master = master;
12 + this.termNumber = term;
13 + }
14 +
15 + public static MastershipTerm of(NodeId master, int term) {
16 + return new MastershipTerm(master, term);
17 + }
18 +
19 + public NodeId master() {
20 + return master;
21 + }
22 +
23 + public int termNumber() {
24 + return termNumber;
25 + }
26 +
27 + @Override
28 + public int hashCode() {
29 + return Objects.hash(master, termNumber);
30 + }
31 +
32 + @Override
33 + public boolean equals(Object other) {
34 + if (other instanceof MastershipTerm) {
35 + MastershipTerm that = (MastershipTerm) other;
36 + if (!this.master.equals(that.master)) {
37 + return false;
38 + }
39 + if (this.termNumber != that.termNumber) {
40 + return false;
41 + }
42 + return true;
43 + }
44 + return false;
45 + }
6 } 46 }
......
...@@ -40,4 +40,9 @@ public class MastershipServiceAdapter implements MastershipService { ...@@ -40,4 +40,9 @@ public class MastershipServiceAdapter implements MastershipService {
40 @Override 40 @Override
41 public void removeListener(MastershipListener listener) { 41 public void removeListener(MastershipListener listener) {
42 } 42 }
43 +
44 + @Override
45 + public MastershipTermService requestTermService() {
46 + return null;
47 + }
43 } 48 }
......
...@@ -12,6 +12,8 @@ import org.onlab.onos.cluster.MastershipEvent; ...@@ -12,6 +12,8 @@ import org.onlab.onos.cluster.MastershipEvent;
12 import org.onlab.onos.cluster.MastershipListener; 12 import org.onlab.onos.cluster.MastershipListener;
13 import org.onlab.onos.cluster.MastershipService; 13 import org.onlab.onos.cluster.MastershipService;
14 import org.onlab.onos.cluster.MastershipStore; 14 import org.onlab.onos.cluster.MastershipStore;
15 +import org.onlab.onos.cluster.MastershipTerm;
16 +import org.onlab.onos.cluster.MastershipTermService;
15 import org.onlab.onos.cluster.NodeId; 17 import org.onlab.onos.cluster.NodeId;
16 import org.onlab.onos.event.AbstractListenerRegistry; 18 import org.onlab.onos.event.AbstractListenerRegistry;
17 import org.onlab.onos.event.EventDeliveryService; 19 import org.onlab.onos.event.EventDeliveryService;
...@@ -103,6 +105,12 @@ public class MastershipManager ...@@ -103,6 +105,12 @@ public class MastershipManager
103 return store.getDevices(nodeId); 105 return store.getDevices(nodeId);
104 } 106 }
105 107
108 +
109 + @Override
110 + public MastershipTermService requestTermService() {
111 + return new InternalMastershipTermService();
112 + }
113 +
106 @Override 114 @Override
107 public void addListener(MastershipListener listener) { 115 public void addListener(MastershipListener listener) {
108 checkNotNull(listener); 116 checkNotNull(listener);
...@@ -124,4 +132,13 @@ public class MastershipManager ...@@ -124,4 +132,13 @@ public class MastershipManager
124 } 132 }
125 } 133 }
126 134
135 + private class InternalMastershipTermService implements MastershipTermService {
136 +
137 + @Override
138 + public MastershipTerm getMastershipTerm(DeviceId deviceId) {
139 + return store.getTermFor(deviceId);
140 + }
141 +
142 + }
143 +
127 } 144 }
......
...@@ -15,6 +15,7 @@ import org.onlab.onos.cluster.ClusterService; ...@@ -15,6 +15,7 @@ import org.onlab.onos.cluster.ClusterService;
15 import org.onlab.onos.cluster.MastershipEvent; 15 import org.onlab.onos.cluster.MastershipEvent;
16 import org.onlab.onos.cluster.MastershipStore; 16 import org.onlab.onos.cluster.MastershipStore;
17 import org.onlab.onos.cluster.MastershipStoreDelegate; 17 import org.onlab.onos.cluster.MastershipStoreDelegate;
18 +import org.onlab.onos.cluster.MastershipTerm;
18 import org.onlab.onos.cluster.NodeId; 19 import org.onlab.onos.cluster.NodeId;
19 import org.onlab.onos.net.DeviceId; 20 import org.onlab.onos.net.DeviceId;
20 import org.onlab.onos.net.MastershipRole; 21 import org.onlab.onos.net.MastershipRole;
...@@ -115,4 +116,10 @@ public class DistributedMastershipStore ...@@ -115,4 +116,10 @@ public class DistributedMastershipStore
115 return nodeId.equals(master) ? MastershipRole.MASTER : MastershipRole.STANDBY; 116 return nodeId.equals(master) ? MastershipRole.MASTER : MastershipRole.STANDBY;
116 } 117 }
117 118
119 + @Override
120 + public MastershipTerm getTermFor(DeviceId deviceId) {
121 + // TODO Auto-generated method stub
122 + return null;
123 + }
124 +
118 } 125 }
......
...@@ -3,11 +3,13 @@ package org.onlab.onos.net.trivial.impl; ...@@ -3,11 +3,13 @@ package org.onlab.onos.net.trivial.impl;
3 import static org.slf4j.LoggerFactory.getLogger; 3 import static org.slf4j.LoggerFactory.getLogger;
4 4
5 import java.util.Collections; 5 import java.util.Collections;
6 +import java.util.HashMap;
6 import java.util.HashSet; 7 import java.util.HashSet;
7 import java.util.Map; 8 import java.util.Map;
8 import java.util.Set; 9 import java.util.Set;
9 import java.util.concurrent.ConcurrentHashMap; 10 import java.util.concurrent.ConcurrentHashMap;
10 import java.util.concurrent.ConcurrentMap; 11 import java.util.concurrent.ConcurrentMap;
12 +import java.util.concurrent.atomic.AtomicInteger;
11 13
12 import org.apache.felix.scr.annotations.Activate; 14 import org.apache.felix.scr.annotations.Activate;
13 import org.apache.felix.scr.annotations.Component; 15 import org.apache.felix.scr.annotations.Component;
...@@ -18,6 +20,7 @@ import org.onlab.onos.cluster.DefaultControllerNode; ...@@ -18,6 +20,7 @@ import org.onlab.onos.cluster.DefaultControllerNode;
18 import org.onlab.onos.cluster.MastershipEvent; 20 import org.onlab.onos.cluster.MastershipEvent;
19 import org.onlab.onos.cluster.MastershipStore; 21 import org.onlab.onos.cluster.MastershipStore;
20 import org.onlab.onos.cluster.MastershipStoreDelegate; 22 import org.onlab.onos.cluster.MastershipStoreDelegate;
23 +import org.onlab.onos.cluster.MastershipTerm;
21 import org.onlab.onos.cluster.NodeId; 24 import org.onlab.onos.cluster.NodeId;
22 import org.onlab.onos.net.DeviceId; 25 import org.onlab.onos.net.DeviceId;
23 import org.onlab.onos.net.MastershipRole; 26 import org.onlab.onos.net.MastershipRole;
...@@ -47,6 +50,7 @@ public class SimpleMastershipStore ...@@ -47,6 +50,7 @@ public class SimpleMastershipStore
47 //devices mapped to their masters, to emulate multiple nodes 50 //devices mapped to their masters, to emulate multiple nodes
48 protected final ConcurrentMap<DeviceId, NodeId> masterMap = 51 protected final ConcurrentMap<DeviceId, NodeId> masterMap =
49 new ConcurrentHashMap<>(); 52 new ConcurrentHashMap<>();
53 + protected final Map<DeviceId, AtomicInteger> termMap = new HashMap<>();
50 54
51 @Activate 55 @Activate
52 public void activate() { 56 public void activate() {
...@@ -63,15 +67,21 @@ public class SimpleMastershipStore ...@@ -63,15 +67,21 @@ public class SimpleMastershipStore
63 67
64 NodeId node = masterMap.get(deviceId); 68 NodeId node = masterMap.get(deviceId);
65 if (node == null) { 69 if (node == null) {
66 - masterMap.put(deviceId, nodeId); 70 + synchronized (this) {
71 + masterMap.put(deviceId, nodeId);
72 + termMap.put(deviceId, new AtomicInteger());
73 + }
67 return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId); 74 return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId);
68 } 75 }
69 76
70 if (node.equals(nodeId)) { 77 if (node.equals(nodeId)) {
71 return null; 78 return null;
72 } else { 79 } else {
73 - masterMap.put(deviceId, nodeId); 80 + synchronized (this) {
74 - return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId); 81 + masterMap.put(deviceId, nodeId);
82 + termMap.get(deviceId).incrementAndGet();
83 + return new MastershipEvent(MASTER_CHANGED, deviceId, nodeId);
84 + }
75 } 85 }
76 } 86 }
77 87
...@@ -114,4 +124,13 @@ public class SimpleMastershipStore ...@@ -114,4 +124,13 @@ public class SimpleMastershipStore
114 return role; 124 return role;
115 } 125 }
116 126
127 + @Override
128 + public MastershipTerm getTermFor(DeviceId deviceId) {
129 + if (masterMap.get(deviceId) == null) {
130 + return null;
131 + }
132 + return MastershipTerm.of(
133 + masterMap.get(deviceId), termMap.get(deviceId).get());
134 + }
135 +
117 } 136 }
......