blob: d3247ecd9467b4ac78aae4ba5d87c3e2efc8e792 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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;
Ray Milkeycc53abd2015-02-19 12:31:33 -080029import org.onlab.packet.IpAddress;
30import org.onosproject.cluster.ClusterServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.cluster.ControllerNode;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.cluster.DefaultControllerNode;
33import org.onosproject.cluster.NodeId;
34import org.onosproject.mastership.MastershipEvent;
Ray Milkeycc53abd2015-02-19 12:31:33 -080035import org.onosproject.mastership.MastershipEvent.Type;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.mastership.MastershipStoreDelegate;
37import org.onosproject.mastership.MastershipTerm;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.DeviceId;
39import org.onosproject.net.MastershipRole;
40import org.onosproject.store.hz.StoreManager;
41import org.onosproject.store.hz.StoreService;
42import org.onosproject.store.hz.TestStoreManager;
43import org.onosproject.store.serializers.KryoSerializer;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070044
45import com.google.common.collect.Sets;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070046
Ray Milkeycc53abd2015-02-19 12:31:33 -080047import static org.junit.Assert.assertEquals;
48import static org.junit.Assert.assertNull;
49import static org.junit.Assert.assertTrue;
50import static org.onosproject.net.MastershipRole.MASTER;
51import static org.onosproject.net.MastershipRole.NONE;
52import static org.onosproject.net.MastershipRole.STANDBY;
53
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070054/**
55 * Test of the Hazelcast-based distributed MastershipStore implementation.
56 */
57public class DistributedMastershipStoreTest {
58
59 private static final DeviceId DID1 = DeviceId.deviceId("of:01");
60 private static final DeviceId DID2 = DeviceId.deviceId("of:02");
61 private static final DeviceId DID3 = DeviceId.deviceId("of:03");
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070062
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070063 private static final IpAddress IP = IpAddress.valueOf("127.0.0.1");
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070064
65 private static final NodeId N1 = new NodeId("node1");
66 private static final NodeId N2 = new NodeId("node2");
67
68 private static final ControllerNode CN1 = new DefaultControllerNode(N1, IP);
69 private static final ControllerNode CN2 = new DefaultControllerNode(N2, IP);
70
71 private DistributedMastershipStore dms;
72 private TestDistributedMastershipStore testStore;
Ayaka Koshibe4c891272014-10-08 17:14:16 -070073 private KryoSerializer serializationMgr;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070074 private StoreManager storeMgr;
75
76 @BeforeClass
77 public static void setUpBeforeClass() throws Exception {
78 }
79
80 @AfterClass
81 public static void tearDownAfterClass() throws Exception {
82 }
83
84 @Before
85 public void setUp() throws Exception {
86 // TODO should find a way to clean Hazelcast instance without shutdown.
Yuta HIGUCHI151cad82015-02-04 23:26:50 -080087 TestStoreManager testStoreMgr = new TestStoreManager();
88 testStoreMgr.setHazelcastInstance(testStoreMgr.initSingleInstance());
89 storeMgr = testStoreMgr;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070090 storeMgr.activate();
91
Ayaka Koshibe4c891272014-10-08 17:14:16 -070092 serializationMgr = new KryoSerializer();
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070093
94 dms = new TestDistributedMastershipStore(storeMgr, serializationMgr);
95 dms.clusterService = new TestClusterService();
96 dms.activate();
97
98 testStore = (TestDistributedMastershipStore) dms;
99 }
100
101 @After
102 public void tearDown() throws Exception {
103 dms.deactivate();
104
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700105 storeMgr.deactivate();
106 }
107
108 @Test
109 public void getRole() {
110 assertEquals("wrong role:", NONE, dms.getRole(N1, DID1));
Ayaka Koshibec4047702014-10-07 14:43:52 -0700111 testStore.put(DID1, N1, true, false, true);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700112 assertEquals("wrong role:", MASTER, dms.getRole(N1, DID1));
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700113 testStore.put(DID1, N2, false, true, false);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700114 assertEquals("wrong role:", STANDBY, dms.getRole(N2, DID1));
115 }
116
117 @Test
118 public void getMaster() {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700119 assertTrue("wrong store state:", dms.roleMap.isEmpty());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700120
121 testStore.put(DID1, N1, true, false, false);
122 assertEquals("wrong master:", N1, dms.getMaster(DID1));
123 assertNull("wrong master:", dms.getMaster(DID2));
124 }
125
126 @Test
127 public void getDevices() {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700128 assertTrue("wrong store state:", dms.roleMap.isEmpty());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700129
130 testStore.put(DID1, N1, true, false, false);
131 testStore.put(DID2, N1, true, false, false);
132 testStore.put(DID3, N2, true, false, false);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700133 assertEquals("wrong devices",
134 Sets.newHashSet(DID1, DID2), dms.getDevices(N1));
135 }
136
137 @Test
138 public void requestRoleAndTerm() {
139 //CN1 is "local"
140 testStore.setCurrent(CN1);
141
142 //if already MASTER, nothing should happen
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700143 testStore.put(DID2, N1, true, false, true);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700144 assertEquals("wrong role for MASTER:", MASTER, dms.requestRole(DID2));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700145
146 //populate maps with DID1, N1 thru NONE case
147 assertEquals("wrong role for NONE:", MASTER, dms.requestRole(DID1));
Ayaka Koshibec4047702014-10-07 14:43:52 -0700148 assertTrue("wrong state for store:", !dms.terms.isEmpty());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700149 assertEquals("wrong term",
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700150 MastershipTerm.of(N1, 1), dms.getTermFor(DID1));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700151
152 //CN2 now local. DID2 has N1 as MASTER so N2 is STANDBY
153 testStore.setCurrent(CN2);
154 assertEquals("wrong role for STANDBY:", STANDBY, dms.requestRole(DID2));
Ayaka Koshibec4047702014-10-07 14:43:52 -0700155 assertEquals("wrong number of entries:", 2, dms.terms.size());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700156
157 //change term and requestRole() again; should persist
158 testStore.increment(DID2);
159 assertEquals("wrong role for STANDBY:", STANDBY, dms.requestRole(DID2));
160 assertEquals("wrong term", MastershipTerm.of(N1, 1), dms.getTermFor(DID2));
161 }
162
163 @Test
164 public void setMaster() {
165 //populate maps with DID1, N1 as MASTER thru NONE case
166 testStore.setCurrent(CN1);
167 assertEquals("wrong role for NONE:", MASTER, dms.requestRole(DID1));
168 assertNull("wrong event:", dms.setMaster(N1, DID1));
169
170 //switch over to N2
171 assertEquals("wrong event:", Type.MASTER_CHANGED, dms.setMaster(N2, DID1).type());
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700172 System.out.println(dms.getTermFor(DID1).master() + ":" + dms.getTermFor(DID1).termNumber());
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700173 assertEquals("wrong term", MastershipTerm.of(N2, 2), dms.getTermFor(DID1));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700174
175 //orphan switch - should be rare case
176 assertEquals("wrong event:", Type.MASTER_CHANGED, dms.setMaster(N2, DID2).type());
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700177 assertEquals("wrong term", MastershipTerm.of(N2, 1), dms.getTermFor(DID2));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700178 //disconnect and reconnect - sign of failing re-election or single-instance channel
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700179 dms.roleMap.clear();
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700180 dms.setMaster(N2, DID2);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700181 assertEquals("wrong term", MastershipTerm.of(N2, 2), dms.getTermFor(DID2));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700182 }
183
184 @Test
Ayaka Koshibec4047702014-10-07 14:43:52 -0700185 public void relinquishRole() {
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700186 //populate maps with DID1, N1 as MASTER thru NONE case
187 testStore.setCurrent(CN1);
188 assertEquals("wrong role for NONE:", MASTER, dms.requestRole(DID1));
189 //no backup, no new MASTER/event
Ayaka Koshibec4047702014-10-07 14:43:52 -0700190 assertNull("wrong event:", dms.relinquishRole(N1, DID1));
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700191
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700192 dms.requestRole(DID1);
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700193
194 //add backup CN2, get it elected MASTER by relinquishing
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700195 testStore.setCurrent(CN2);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700196 assertEquals("wrong role for NONE:", STANDBY, dms.requestRole(DID1));
197 assertEquals("wrong event:", Type.MASTER_CHANGED, dms.relinquishRole(N1, DID1).type());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700198 assertEquals("wrong master", N2, dms.getMaster(DID1));
199
Ayaka Koshibec4047702014-10-07 14:43:52 -0700200 //all nodes "give up" on device, which goes back to NONE.
201 assertNull("wrong event:", dms.relinquishRole(N2, DID1));
202 assertEquals("wrong role for node:", NONE, dms.getRole(N2, DID1));
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700203
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700204 assertEquals("wrong number of retired nodes", 2,
205 dms.roleMap.get(DID1).nodesOfRole(NONE).size());
Ayaka Koshibec4047702014-10-07 14:43:52 -0700206
207 //bring nodes back
208 assertEquals("wrong role for NONE:", MASTER, dms.requestRole(DID1));
209 testStore.setCurrent(CN1);
210 assertEquals("wrong role for NONE:", STANDBY, dms.requestRole(DID1));
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700211 assertEquals("wrong number of backup nodes", 1,
212 dms.roleMap.get(DID1).nodesOfRole(STANDBY).size());
Ayaka Koshibec4047702014-10-07 14:43:52 -0700213
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700214 //If STANDBY, should drop to NONE
Ayaka Koshibe98bd12f2014-11-01 20:13:37 -0700215 assertEquals("wrong event:", Type.BACKUPS_CHANGED, dms.relinquishRole(N1, DID1).type());
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700216 assertEquals("wrong role for node:", NONE, dms.getRole(N1, DID1));
217
Ayaka Koshibec4047702014-10-07 14:43:52 -0700218 //NONE - nothing happens
Ayaka Koshibe98bd12f2014-11-01 20:13:37 -0700219 assertEquals("wrong event:", Type.BACKUPS_CHANGED, dms.relinquishRole(N1, DID2).type());
Ayaka Koshibec4047702014-10-07 14:43:52 -0700220 assertEquals("wrong role for node:", NONE, dms.getRole(N1, DID2));
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700221
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700222 }
223
Ayaka Koshibe5c0f2372014-10-02 17:59:04 -0700224 @Ignore("Ignore until Delegate spec. is clear.")
225 @Test
226 public void testEvents() throws InterruptedException {
227 //shamelessly copy other distributed store tests
228 final CountDownLatch addLatch = new CountDownLatch(1);
229
230 MastershipStoreDelegate checkAdd = new MastershipStoreDelegate() {
231 @Override
232 public void notify(MastershipEvent event) {
233 assertEquals("wrong event:", Type.MASTER_CHANGED, event.type());
234 assertEquals("wrong subject", DID1, event.subject());
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700235 assertEquals("wrong subject", N1, event.roleInfo().master());
Ayaka Koshibe5c0f2372014-10-02 17:59:04 -0700236 addLatch.countDown();
237 }
238 };
239
240 dms.setDelegate(checkAdd);
241 dms.setMaster(N1, DID1);
242 //this will fail until we do something about single-instance-ness
243 assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
244 }
245
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700246 private class TestDistributedMastershipStore extends
247 DistributedMastershipStore {
248 public TestDistributedMastershipStore(StoreService storeService,
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700249 KryoSerializer kryoSerialization) {
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700250 this.storeService = storeService;
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700251 this.serializer = kryoSerialization;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700252 }
253
254 //helper to populate master/backup structures
255 public void put(DeviceId dev, NodeId node,
Ayaka Koshibec4047702014-10-07 14:43:52 -0700256 boolean master, boolean backup, boolean term) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700257 RoleValue rv = dms.roleMap.get(dev);
258 if (rv == null) {
259 rv = new RoleValue();
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700260 }
Ayaka Koshibec4047702014-10-07 14:43:52 -0700261
262 if (master) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700263 rv.add(MASTER, node);
264 rv.reassign(node, STANDBY, NONE);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700265 }
266 if (backup) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700267 rv.add(STANDBY, node);
268 rv.remove(MASTER, node);
269 rv.remove(NONE, node);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700270 }
271 if (term) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700272 dms.terms.put(dev, 0);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700273 }
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700274 dms.roleMap.put(dev, rv);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700275 }
276
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700277 //a dumb utility function.
Ayaka Koshibec4047702014-10-07 14:43:52 -0700278 public void dump() {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700279 for (Map.Entry<DeviceId, RoleValue> el : dms.roleMap.entrySet()) {
280 System.out.println("DID: " + el.getKey());
281 for (MastershipRole role : MastershipRole.values()) {
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700282 System.out.println("\t" + role.toString() + ":");
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700283 for (NodeId n : el.getValue().nodesOfRole(role)) {
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700284 System.out.println("\t\t" + n);
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700285 }
286 }
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700287 }
288 }
289
290 //increment term for a device
291 public void increment(DeviceId dev) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700292 Integer t = dms.terms.get(dev);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700293 if (t != null) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700294 dms.terms.put(dev, ++t);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700295 }
296 }
297
298 //sets the "local" node
299 public void setCurrent(ControllerNode node) {
300 ((TestClusterService) clusterService).current = node;
301 }
302 }
303
Ray Milkeycc53abd2015-02-19 12:31:33 -0800304 private class TestClusterService extends ClusterServiceAdapter {
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700305
306 protected ControllerNode current;
307
308 @Override
309 public ControllerNode getLocalNode() {
310 return current;
311 }
312
313 @Override
314 public Set<ControllerNode> getNodes() {
315 return Sets.newHashSet(CN1, CN2);
316 }
317
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700318 }
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700319
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700320}