blob: 956b0be05d5c780235cd0264466d8bc470ac8fe1 [file] [log] [blame]
Yuta HIGUCHI80912e62014-10-12 00:15:47 -07001package org.onlab.onos.store.mastership.impl;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -07002
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertNull;
5import static org.junit.Assert.assertTrue;
6import static org.onlab.onos.net.MastershipRole.*;
7
Ayaka Koshibec4047702014-10-07 14:43:52 -07008import java.util.Map;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -07009import java.util.Set;
Ayaka Koshibe5c0f2372014-10-02 17:59:04 -070010import java.util.concurrent.CountDownLatch;
11import java.util.concurrent.TimeUnit;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070012
13import org.junit.After;
14import org.junit.AfterClass;
15import org.junit.Before;
16import org.junit.BeforeClass;
Ayaka Koshibe5c0f2372014-10-02 17:59:04 -070017import org.junit.Ignore;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070018import org.junit.Test;
19import org.onlab.onos.cluster.ClusterEventListener;
20import org.onlab.onos.cluster.ClusterService;
21import org.onlab.onos.cluster.ControllerNode;
22import org.onlab.onos.cluster.ControllerNode.State;
23import org.onlab.onos.cluster.DefaultControllerNode;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070024import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070025import org.onlab.onos.mastership.MastershipEvent;
26import org.onlab.onos.mastership.MastershipStoreDelegate;
27import org.onlab.onos.mastership.MastershipTerm;
28import org.onlab.onos.mastership.MastershipEvent.Type;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070029import org.onlab.onos.net.DeviceId;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070030import org.onlab.onos.net.MastershipRole;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070031import org.onlab.onos.store.common.StoreManager;
32import org.onlab.onos.store.common.StoreService;
33import org.onlab.onos.store.common.TestStoreManager;
Ayaka Koshibe4c891272014-10-08 17:14:16 -070034import org.onlab.onos.store.serializers.KryoSerializer;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070035import org.onlab.packet.IpPrefix;
36
37import com.google.common.collect.Sets;
38import com.hazelcast.config.Config;
39import com.hazelcast.core.Hazelcast;
40
41/**
42 * Test of the Hazelcast-based distributed MastershipStore implementation.
43 */
44public class DistributedMastershipStoreTest {
45
46 private static final DeviceId DID1 = DeviceId.deviceId("of:01");
47 private static final DeviceId DID2 = DeviceId.deviceId("of:02");
48 private static final DeviceId DID3 = DeviceId.deviceId("of:03");
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070049
50 private static final IpPrefix IP = IpPrefix.valueOf("127.0.0.1");
51
52 private static final NodeId N1 = new NodeId("node1");
53 private static final NodeId N2 = new NodeId("node2");
54
55 private static final ControllerNode CN1 = new DefaultControllerNode(N1, IP);
56 private static final ControllerNode CN2 = new DefaultControllerNode(N2, IP);
57
58 private DistributedMastershipStore dms;
59 private TestDistributedMastershipStore testStore;
Ayaka Koshibe4c891272014-10-08 17:14:16 -070060 private KryoSerializer serializationMgr;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070061 private StoreManager storeMgr;
62
63 @BeforeClass
64 public static void setUpBeforeClass() throws Exception {
65 }
66
67 @AfterClass
68 public static void tearDownAfterClass() throws Exception {
69 }
70
71 @Before
72 public void setUp() throws Exception {
73 // TODO should find a way to clean Hazelcast instance without shutdown.
74 Config config = TestStoreManager.getTestConfig();
75
76 storeMgr = new TestStoreManager(Hazelcast.newHazelcastInstance(config));
77 storeMgr.activate();
78
Ayaka Koshibe4c891272014-10-08 17:14:16 -070079 serializationMgr = new KryoSerializer();
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070080
81 dms = new TestDistributedMastershipStore(storeMgr, serializationMgr);
82 dms.clusterService = new TestClusterService();
83 dms.activate();
84
85 testStore = (TestDistributedMastershipStore) dms;
86 }
87
88 @After
89 public void tearDown() throws Exception {
90 dms.deactivate();
91
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070092 storeMgr.deactivate();
93 }
94
95 @Test
96 public void getRole() {
97 assertEquals("wrong role:", NONE, dms.getRole(N1, DID1));
Ayaka Koshibec4047702014-10-07 14:43:52 -070098 testStore.put(DID1, N1, true, false, true);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070099 assertEquals("wrong role:", MASTER, dms.getRole(N1, DID1));
100 assertEquals("wrong role:", STANDBY, dms.getRole(N2, DID1));
101 }
102
103 @Test
104 public void getMaster() {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700105 assertTrue("wrong store state:", dms.roleMap.isEmpty());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700106
107 testStore.put(DID1, N1, true, false, false);
108 assertEquals("wrong master:", N1, dms.getMaster(DID1));
109 assertNull("wrong master:", dms.getMaster(DID2));
110 }
111
112 @Test
113 public void getDevices() {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700114 assertTrue("wrong store state:", dms.roleMap.isEmpty());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700115
116 testStore.put(DID1, N1, true, false, false);
117 testStore.put(DID2, N1, true, false, false);
118 testStore.put(DID3, N2, true, false, false);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700119 assertEquals("wrong devices",
120 Sets.newHashSet(DID1, DID2), dms.getDevices(N1));
121 }
122
123 @Test
124 public void requestRoleAndTerm() {
125 //CN1 is "local"
126 testStore.setCurrent(CN1);
127
128 //if already MASTER, nothing should happen
129 testStore.put(DID2, N1, true, false, false);
130 assertEquals("wrong role for MASTER:", MASTER, dms.requestRole(DID2));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700131
132 //populate maps with DID1, N1 thru NONE case
133 assertEquals("wrong role for NONE:", MASTER, dms.requestRole(DID1));
Ayaka Koshibec4047702014-10-07 14:43:52 -0700134 assertTrue("wrong state for store:", !dms.terms.isEmpty());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700135 assertEquals("wrong term",
136 MastershipTerm.of(N1, 0), dms.getTermFor(DID1));
137
138 //CN2 now local. DID2 has N1 as MASTER so N2 is STANDBY
139 testStore.setCurrent(CN2);
140 assertEquals("wrong role for STANDBY:", STANDBY, dms.requestRole(DID2));
Ayaka Koshibec4047702014-10-07 14:43:52 -0700141 assertEquals("wrong number of entries:", 2, dms.terms.size());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700142
143 //change term and requestRole() again; should persist
144 testStore.increment(DID2);
145 assertEquals("wrong role for STANDBY:", STANDBY, dms.requestRole(DID2));
146 assertEquals("wrong term", MastershipTerm.of(N1, 1), dms.getTermFor(DID2));
147 }
148
149 @Test
150 public void setMaster() {
151 //populate maps with DID1, N1 as MASTER thru NONE case
152 testStore.setCurrent(CN1);
153 assertEquals("wrong role for NONE:", MASTER, dms.requestRole(DID1));
154 assertNull("wrong event:", dms.setMaster(N1, DID1));
155
156 //switch over to N2
157 assertEquals("wrong event:", Type.MASTER_CHANGED, dms.setMaster(N2, DID1).type());
158 assertEquals("wrong term", MastershipTerm.of(N2, 1), dms.getTermFor(DID1));
159
160 //orphan switch - should be rare case
161 assertEquals("wrong event:", Type.MASTER_CHANGED, dms.setMaster(N2, DID2).type());
162 assertEquals("wrong term", MastershipTerm.of(N2, 0), dms.getTermFor(DID2));
163 //disconnect and reconnect - sign of failing re-election or single-instance channel
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700164 dms.roleMap.clear();
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700165 dms.setMaster(N2, DID2);
166 assertEquals("wrong term", MastershipTerm.of(N2, 1), dms.getTermFor(DID2));
167 }
168
169 @Test
Ayaka Koshibec4047702014-10-07 14:43:52 -0700170 public void relinquishRole() {
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700171 //populate maps with DID1, N1 as MASTER thru NONE case
172 testStore.setCurrent(CN1);
173 assertEquals("wrong role for NONE:", MASTER, dms.requestRole(DID1));
174 //no backup, no new MASTER/event
Ayaka Koshibec4047702014-10-07 14:43:52 -0700175 assertNull("wrong event:", dms.relinquishRole(N1, DID1));
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700176
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700177 dms.requestRole(DID1);
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700178
179 //add backup CN2, get it elected MASTER by relinquishing
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700180 testStore.setCurrent(CN2);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700181 assertEquals("wrong role for NONE:", STANDBY, dms.requestRole(DID1));
182 assertEquals("wrong event:", Type.MASTER_CHANGED, dms.relinquishRole(N1, DID1).type());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700183 assertEquals("wrong master", N2, dms.getMaster(DID1));
184
185 //STANDBY - nothing here, either
Ayaka Koshibec4047702014-10-07 14:43:52 -0700186 assertNull("wrong event:", dms.relinquishRole(N1, DID1));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700187 assertEquals("wrong role for node:", STANDBY, dms.getRole(N1, DID1));
188
Ayaka Koshibec4047702014-10-07 14:43:52 -0700189 //all nodes "give up" on device, which goes back to NONE.
190 assertNull("wrong event:", dms.relinquishRole(N2, DID1));
191 assertEquals("wrong role for node:", NONE, dms.getRole(N2, DID1));
192 assertEquals("wrong role for node:", NONE, dms.getRole(N1, DID1));
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700193
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700194 assertEquals("wrong number of retired nodes", 2,
195 dms.roleMap.get(DID1).nodesOfRole(NONE).size());
Ayaka Koshibec4047702014-10-07 14:43:52 -0700196
197 //bring nodes back
198 assertEquals("wrong role for NONE:", MASTER, dms.requestRole(DID1));
199 testStore.setCurrent(CN1);
200 assertEquals("wrong role for NONE:", STANDBY, dms.requestRole(DID1));
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700201 assertEquals("wrong number of backup nodes", 1,
202 dms.roleMap.get(DID1).nodesOfRole(STANDBY).size());
Ayaka Koshibec4047702014-10-07 14:43:52 -0700203
204 //NONE - nothing happens
205 assertNull("wrong event:", dms.relinquishRole(N1, DID2));
206 assertEquals("wrong role for node:", NONE, dms.getRole(N1, DID2));
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700207
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700208 }
209
Ayaka Koshibe5c0f2372014-10-02 17:59:04 -0700210 @Ignore("Ignore until Delegate spec. is clear.")
211 @Test
212 public void testEvents() throws InterruptedException {
213 //shamelessly copy other distributed store tests
214 final CountDownLatch addLatch = new CountDownLatch(1);
215
216 MastershipStoreDelegate checkAdd = new MastershipStoreDelegate() {
217 @Override
218 public void notify(MastershipEvent event) {
219 assertEquals("wrong event:", Type.MASTER_CHANGED, event.type());
220 assertEquals("wrong subject", DID1, event.subject());
221 assertEquals("wrong subject", N1, event.master());
222 addLatch.countDown();
223 }
224 };
225
226 dms.setDelegate(checkAdd);
227 dms.setMaster(N1, DID1);
228 //this will fail until we do something about single-instance-ness
229 assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
230 }
231
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700232 private class TestDistributedMastershipStore extends
233 DistributedMastershipStore {
234 public TestDistributedMastershipStore(StoreService storeService,
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700235 KryoSerializer kryoSerialization) {
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700236 this.storeService = storeService;
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700237 this.serializer = kryoSerialization;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700238 }
239
240 //helper to populate master/backup structures
241 public void put(DeviceId dev, NodeId node,
Ayaka Koshibec4047702014-10-07 14:43:52 -0700242 boolean master, boolean backup, boolean term) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700243 RoleValue rv = dms.roleMap.get(dev);
244 if (rv == null) {
245 rv = new RoleValue();
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700246 }
Ayaka Koshibec4047702014-10-07 14:43:52 -0700247
248 if (master) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700249 rv.add(MASTER, node);
250 rv.reassign(node, STANDBY, NONE);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700251 }
252 if (backup) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700253 rv.add(STANDBY, node);
254 rv.remove(MASTER, node);
255 rv.remove(NONE, node);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700256 }
257 if (term) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700258 dms.terms.put(dev, 0);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700259 }
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700260 dms.roleMap.put(dev, rv);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700261 }
262
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700263 //a dumb utility function.
Ayaka Koshibec4047702014-10-07 14:43:52 -0700264 public void dump() {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700265 for (Map.Entry<DeviceId, RoleValue> el : dms.roleMap.entrySet()) {
266 System.out.println("DID: " + el.getKey());
267 for (MastershipRole role : MastershipRole.values()) {
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700268 System.out.println("\t" + role.toString() + ":");
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700269 for (NodeId n : el.getValue().nodesOfRole(role)) {
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700270 System.out.println("\t\t" + n);
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700271 }
272 }
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700273 }
274 }
275
276 //increment term for a device
277 public void increment(DeviceId dev) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700278 Integer t = dms.terms.get(dev);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700279 if (t != null) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700280 dms.terms.put(dev, ++t);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700281 }
282 }
283
284 //sets the "local" node
285 public void setCurrent(ControllerNode node) {
286 ((TestClusterService) clusterService).current = node;
287 }
288 }
289
290 private class TestClusterService implements ClusterService {
291
292 protected ControllerNode current;
293
294 @Override
295 public ControllerNode getLocalNode() {
296 return current;
297 }
298
299 @Override
300 public Set<ControllerNode> getNodes() {
301 return Sets.newHashSet(CN1, CN2);
302 }
303
304 @Override
305 public ControllerNode getNode(NodeId nodeId) {
306 return null;
307 }
308
309 @Override
310 public State getState(NodeId nodeId) {
311 return null;
312 }
313
314 @Override
315 public void addListener(ClusterEventListener listener) {
316 }
317
318 @Override
319 public void removeListener(ClusterEventListener listener) {
320 }
321
322 }
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700323
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700324}