blob: 17456873f6d76eb37a6d9d7d6cb9c406af241939 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.mastership.impl;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070017
Ayaka Koshibec4047702014-10-07 14:43:52 -070018import java.util.Map;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070019import java.util.Set;
Ayaka Koshibe5c0f2372014-10-02 17:59:04 -070020import java.util.concurrent.CountDownLatch;
21import java.util.concurrent.TimeUnit;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070022
23import org.junit.After;
24import org.junit.AfterClass;
25import org.junit.Before;
26import org.junit.BeforeClass;
Ayaka Koshibe5c0f2372014-10-02 17:59:04 -070027import org.junit.Ignore;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070028import org.junit.Test;
Brian O'Connor17367492015-03-03 17:11:54 -080029import org.onlab.junit.TestTools;
Ray Milkeycc53abd2015-02-19 12:31:33 -080030import org.onlab.packet.IpAddress;
31import org.onosproject.cluster.ClusterServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.cluster.ControllerNode;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.cluster.DefaultControllerNode;
34import org.onosproject.cluster.NodeId;
35import org.onosproject.mastership.MastershipEvent;
Ray Milkeycc53abd2015-02-19 12:31:33 -080036import org.onosproject.mastership.MastershipEvent.Type;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.mastership.MastershipStoreDelegate;
38import org.onosproject.mastership.MastershipTerm;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.DeviceId;
40import org.onosproject.net.MastershipRole;
41import org.onosproject.store.hz.StoreManager;
42import org.onosproject.store.hz.StoreService;
43import org.onosproject.store.hz.TestStoreManager;
44import org.onosproject.store.serializers.KryoSerializer;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070045
46import com.google.common.collect.Sets;
Madan Jampanif7536ab2015-05-07 23:23:23 -070047import com.google.common.util.concurrent.Futures;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070048
Ray Milkeycc53abd2015-02-19 12:31:33 -080049import static org.junit.Assert.assertEquals;
50import static org.junit.Assert.assertNull;
51import static org.junit.Assert.assertTrue;
52import static org.onosproject.net.MastershipRole.MASTER;
53import static org.onosproject.net.MastershipRole.NONE;
54import static org.onosproject.net.MastershipRole.STANDBY;
55
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070056/**
57 * Test of the Hazelcast-based distributed MastershipStore implementation.
58 */
59public class DistributedMastershipStoreTest {
60
61 private static final DeviceId DID1 = DeviceId.deviceId("of:01");
62 private static final DeviceId DID2 = DeviceId.deviceId("of:02");
63 private static final DeviceId DID3 = DeviceId.deviceId("of:03");
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070064
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070065 private static final IpAddress IP = IpAddress.valueOf("127.0.0.1");
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070066
67 private static final NodeId N1 = new NodeId("node1");
68 private static final NodeId N2 = new NodeId("node2");
69
70 private static final ControllerNode CN1 = new DefaultControllerNode(N1, IP);
71 private static final ControllerNode CN2 = new DefaultControllerNode(N2, IP);
72
73 private DistributedMastershipStore dms;
74 private TestDistributedMastershipStore testStore;
Ayaka Koshibe4c891272014-10-08 17:14:16 -070075 private KryoSerializer serializationMgr;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070076 private StoreManager storeMgr;
77
78 @BeforeClass
79 public static void setUpBeforeClass() throws Exception {
80 }
81
82 @AfterClass
83 public static void tearDownAfterClass() throws Exception {
84 }
85
86 @Before
87 public void setUp() throws Exception {
88 // TODO should find a way to clean Hazelcast instance without shutdown.
Yuta HIGUCHI151cad82015-02-04 23:26:50 -080089 TestStoreManager testStoreMgr = new TestStoreManager();
90 testStoreMgr.setHazelcastInstance(testStoreMgr.initSingleInstance());
91 storeMgr = testStoreMgr;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070092 storeMgr.activate();
93
Ayaka Koshibe4c891272014-10-08 17:14:16 -070094 serializationMgr = new KryoSerializer();
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070095
96 dms = new TestDistributedMastershipStore(storeMgr, serializationMgr);
97 dms.clusterService = new TestClusterService();
98 dms.activate();
99
100 testStore = (TestDistributedMastershipStore) dms;
101 }
102
103 @After
104 public void tearDown() throws Exception {
105 dms.deactivate();
106
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700107 storeMgr.deactivate();
108 }
109
110 @Test
Ray Milkey0811bdd2015-03-11 10:21:55 -0700111 @Ignore("Disabled this test due to intermittent failures seen on Jenkins runs")
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700112 public void getRole() {
113 assertEquals("wrong role:", NONE, dms.getRole(N1, DID1));
Ayaka Koshibec4047702014-10-07 14:43:52 -0700114 testStore.put(DID1, N1, true, false, true);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700115 assertEquals("wrong role:", MASTER, dms.getRole(N1, DID1));
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700116 testStore.put(DID1, N2, false, true, false);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700117 assertEquals("wrong role:", STANDBY, dms.getRole(N2, DID1));
118 }
119
120 @Test
121 public void getMaster() {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700122 assertTrue("wrong store state:", dms.roleMap.isEmpty());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700123
124 testStore.put(DID1, N1, true, false, false);
Brian O'Connor17367492015-03-03 17:11:54 -0800125 TestTools.assertAfter(100, () -> //wait for up to 100ms
126 assertEquals("wrong master:", N1, dms.getMaster(DID1)));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700127 assertNull("wrong master:", dms.getMaster(DID2));
128 }
129
130 @Test
131 public void getDevices() {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700132 assertTrue("wrong store state:", dms.roleMap.isEmpty());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700133
134 testStore.put(DID1, N1, true, false, false);
135 testStore.put(DID2, N1, true, false, false);
136 testStore.put(DID3, N2, true, false, false);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700137 assertEquals("wrong devices",
138 Sets.newHashSet(DID1, DID2), dms.getDevices(N1));
139 }
140
141 @Test
142 public void requestRoleAndTerm() {
143 //CN1 is "local"
144 testStore.setCurrent(CN1);
145
146 //if already MASTER, nothing should happen
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700147 testStore.put(DID2, N1, true, false, true);
Madan Jampanide003d92015-05-11 17:14:20 -0700148 assertEquals("wrong role for MASTER:", MASTER, Futures.getUnchecked(dms.requestRole(DID2)));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700149
150 //populate maps with DID1, N1 thru NONE case
Madan Jampanide003d92015-05-11 17:14:20 -0700151 assertEquals("wrong role for NONE:", MASTER, Futures.getUnchecked(dms.requestRole(DID1)));
Ayaka Koshibec4047702014-10-07 14:43:52 -0700152 assertTrue("wrong state for store:", !dms.terms.isEmpty());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700153 assertEquals("wrong term",
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700154 MastershipTerm.of(N1, 1), dms.getTermFor(DID1));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700155
156 //CN2 now local. DID2 has N1 as MASTER so N2 is STANDBY
157 testStore.setCurrent(CN2);
Madan Jampanide003d92015-05-11 17:14:20 -0700158 assertEquals("wrong role for STANDBY:", STANDBY, Futures.getUnchecked(dms.requestRole(DID2)));
Ayaka Koshibec4047702014-10-07 14:43:52 -0700159 assertEquals("wrong number of entries:", 2, dms.terms.size());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700160
161 //change term and requestRole() again; should persist
162 testStore.increment(DID2);
Madan Jampanide003d92015-05-11 17:14:20 -0700163 assertEquals("wrong role for STANDBY:", STANDBY, Futures.getUnchecked(dms.requestRole(DID2)));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700164 assertEquals("wrong term", MastershipTerm.of(N1, 1), dms.getTermFor(DID2));
165 }
166
167 @Test
168 public void setMaster() {
169 //populate maps with DID1, N1 as MASTER thru NONE case
170 testStore.setCurrent(CN1);
Madan Jampanide003d92015-05-11 17:14:20 -0700171 assertEquals("wrong role for NONE:", MASTER, Futures.getUnchecked(dms.requestRole(DID1)));
Madan Jampanif7536ab2015-05-07 23:23:23 -0700172 assertNull("wrong event:", Futures.getUnchecked(dms.setMaster(N1, DID1)));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700173
174 //switch over to N2
Madan Jampanif7536ab2015-05-07 23:23:23 -0700175 assertEquals("wrong event:", Type.MASTER_CHANGED, Futures.getUnchecked(dms.setMaster(N2, DID1)).type());
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700176 System.out.println(dms.getTermFor(DID1).master() + ":" + dms.getTermFor(DID1).termNumber());
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700177 assertEquals("wrong term", MastershipTerm.of(N2, 2), dms.getTermFor(DID1));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700178
179 //orphan switch - should be rare case
Madan Jampanif7536ab2015-05-07 23:23:23 -0700180 assertEquals("wrong event:", Type.MASTER_CHANGED, Futures.getUnchecked(dms.setMaster(N2, DID2)).type());
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700181 assertEquals("wrong term", MastershipTerm.of(N2, 1), dms.getTermFor(DID2));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700182 //disconnect and reconnect - sign of failing re-election or single-instance channel
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700183 dms.roleMap.clear();
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700184 dms.setMaster(N2, DID2);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700185 assertEquals("wrong term", MastershipTerm.of(N2, 2), dms.getTermFor(DID2));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700186 }
187
188 @Test
Ayaka Koshibec4047702014-10-07 14:43:52 -0700189 public void relinquishRole() {
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700190 //populate maps with DID1, N1 as MASTER thru NONE case
191 testStore.setCurrent(CN1);
Madan Jampanide003d92015-05-11 17:14:20 -0700192 assertEquals("wrong role for NONE:", MASTER, Futures.getUnchecked(dms.requestRole(DID1)));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700193 //no backup, no new MASTER/event
Madan Jampanif7536ab2015-05-07 23:23:23 -0700194 assertNull("wrong event:", Futures.getUnchecked(dms.relinquishRole(N1, DID1)));
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700195
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700196 dms.requestRole(DID1);
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700197
198 //add backup CN2, get it elected MASTER by relinquishing
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700199 testStore.setCurrent(CN2);
Madan Jampanide003d92015-05-11 17:14:20 -0700200 assertEquals("wrong role for NONE:", STANDBY, Futures.getUnchecked(dms.requestRole(DID1)));
Madan Jampanif7536ab2015-05-07 23:23:23 -0700201 assertEquals("wrong event:", Type.MASTER_CHANGED, Futures.getUnchecked(dms.relinquishRole(N1, DID1)).type());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700202 assertEquals("wrong master", N2, dms.getMaster(DID1));
203
Ayaka Koshibec4047702014-10-07 14:43:52 -0700204 //all nodes "give up" on device, which goes back to NONE.
Madan Jampanif7536ab2015-05-07 23:23:23 -0700205 assertNull("wrong event:", Futures.getUnchecked(dms.relinquishRole(N2, DID1)));
Ayaka Koshibec4047702014-10-07 14:43:52 -0700206 assertEquals("wrong role for node:", NONE, dms.getRole(N2, DID1));
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700207
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700208 assertEquals("wrong number of retired nodes", 2,
209 dms.roleMap.get(DID1).nodesOfRole(NONE).size());
Ayaka Koshibec4047702014-10-07 14:43:52 -0700210
211 //bring nodes back
Madan Jampanide003d92015-05-11 17:14:20 -0700212 assertEquals("wrong role for NONE:", MASTER, Futures.getUnchecked(dms.requestRole(DID1)));
Ayaka Koshibec4047702014-10-07 14:43:52 -0700213 testStore.setCurrent(CN1);
Madan Jampanide003d92015-05-11 17:14:20 -0700214 assertEquals("wrong role for NONE:", STANDBY, Futures.getUnchecked(dms.requestRole(DID1)));
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700215 assertEquals("wrong number of backup nodes", 1,
216 dms.roleMap.get(DID1).nodesOfRole(STANDBY).size());
Ayaka Koshibec4047702014-10-07 14:43:52 -0700217
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700218 //If STANDBY, should drop to NONE
Madan Jampanif7536ab2015-05-07 23:23:23 -0700219 assertEquals("wrong event:", Type.BACKUPS_CHANGED, Futures.getUnchecked(dms.relinquishRole(N1, DID1)).type());
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700220 assertEquals("wrong role for node:", NONE, dms.getRole(N1, DID1));
221
Ayaka Koshibec4047702014-10-07 14:43:52 -0700222 //NONE - nothing happens
Madan Jampanif7536ab2015-05-07 23:23:23 -0700223 assertEquals("wrong event:", Type.BACKUPS_CHANGED, Futures.getUnchecked(dms.relinquishRole(N1, DID2)).type());
Ayaka Koshibec4047702014-10-07 14:43:52 -0700224 assertEquals("wrong role for node:", NONE, dms.getRole(N1, DID2));
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700225
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700226 }
227
Ayaka Koshibe5c0f2372014-10-02 17:59:04 -0700228 @Ignore("Ignore until Delegate spec. is clear.")
229 @Test
230 public void testEvents() throws InterruptedException {
231 //shamelessly copy other distributed store tests
232 final CountDownLatch addLatch = new CountDownLatch(1);
233
234 MastershipStoreDelegate checkAdd = new MastershipStoreDelegate() {
235 @Override
236 public void notify(MastershipEvent event) {
237 assertEquals("wrong event:", Type.MASTER_CHANGED, event.type());
238 assertEquals("wrong subject", DID1, event.subject());
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700239 assertEquals("wrong subject", N1, event.roleInfo().master());
Ayaka Koshibe5c0f2372014-10-02 17:59:04 -0700240 addLatch.countDown();
241 }
242 };
243
244 dms.setDelegate(checkAdd);
245 dms.setMaster(N1, DID1);
246 //this will fail until we do something about single-instance-ness
247 assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
248 }
249
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700250 private class TestDistributedMastershipStore extends
251 DistributedMastershipStore {
252 public TestDistributedMastershipStore(StoreService storeService,
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700253 KryoSerializer kryoSerialization) {
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700254 this.storeService = storeService;
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700255 this.serializer = kryoSerialization;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700256 }
257
258 //helper to populate master/backup structures
259 public void put(DeviceId dev, NodeId node,
Ayaka Koshibec4047702014-10-07 14:43:52 -0700260 boolean master, boolean backup, boolean term) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700261 RoleValue rv = dms.roleMap.get(dev);
262 if (rv == null) {
263 rv = new RoleValue();
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700264 }
Ayaka Koshibec4047702014-10-07 14:43:52 -0700265
266 if (master) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700267 rv.add(MASTER, node);
268 rv.reassign(node, STANDBY, NONE);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700269 }
270 if (backup) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700271 rv.add(STANDBY, node);
272 rv.remove(MASTER, node);
273 rv.remove(NONE, node);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700274 }
275 if (term) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700276 dms.terms.put(dev, 0);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700277 }
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700278 dms.roleMap.put(dev, rv);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700279 }
280
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700281 //a dumb utility function.
Ayaka Koshibec4047702014-10-07 14:43:52 -0700282 public void dump() {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700283 for (Map.Entry<DeviceId, RoleValue> el : dms.roleMap.entrySet()) {
284 System.out.println("DID: " + el.getKey());
285 for (MastershipRole role : MastershipRole.values()) {
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700286 System.out.println("\t" + role.toString() + ":");
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700287 for (NodeId n : el.getValue().nodesOfRole(role)) {
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700288 System.out.println("\t\t" + n);
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700289 }
290 }
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700291 }
292 }
293
294 //increment term for a device
295 public void increment(DeviceId dev) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700296 Integer t = dms.terms.get(dev);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700297 if (t != null) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700298 dms.terms.put(dev, ++t);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700299 }
300 }
301
302 //sets the "local" node
303 public void setCurrent(ControllerNode node) {
304 ((TestClusterService) clusterService).current = node;
305 }
306 }
307
Ray Milkeycc53abd2015-02-19 12:31:33 -0800308 private class TestClusterService extends ClusterServiceAdapter {
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700309
310 protected ControllerNode current;
311
312 @Override
313 public ControllerNode getLocalNode() {
314 return current;
315 }
316
317 @Override
318 public Set<ControllerNode> getNodes() {
319 return Sets.newHashSet(CN1, CN2);
320 }
321
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700322 }
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700323
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700324}