blob: 1a961612c5c7799f1e928722ea0181ebb9a5aefa [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
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertNull;
20import static org.junit.Assert.assertTrue;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import static org.onosproject.net.MastershipRole.*;
Ayaka Koshibec4047702014-10-07 14:43:52 -070022import java.util.Map;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070023import java.util.Set;
Ayaka Koshibe5c0f2372014-10-02 17:59:04 -070024import java.util.concurrent.CountDownLatch;
25import java.util.concurrent.TimeUnit;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070026
27import org.junit.After;
28import org.junit.AfterClass;
29import org.junit.Before;
30import org.junit.BeforeClass;
Ayaka Koshibe5c0f2372014-10-02 17:59:04 -070031import org.junit.Ignore;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070032import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.cluster.ClusterEventListener;
34import org.onosproject.cluster.ClusterService;
35import org.onosproject.cluster.ControllerNode;
36import org.onosproject.cluster.ControllerNode.State;
37import org.onosproject.cluster.DefaultControllerNode;
38import org.onosproject.cluster.NodeId;
39import org.onosproject.mastership.MastershipEvent;
40import org.onosproject.mastership.MastershipStoreDelegate;
41import org.onosproject.mastership.MastershipTerm;
42import org.onosproject.mastership.MastershipEvent.Type;
43import org.onosproject.net.DeviceId;
44import org.onosproject.net.MastershipRole;
45import org.onosproject.store.hz.StoreManager;
46import org.onosproject.store.hz.StoreService;
47import org.onosproject.store.hz.TestStoreManager;
48import org.onosproject.store.serializers.KryoSerializer;
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070049import org.onlab.packet.IpAddress;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070050
51import com.google.common.collect.Sets;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070052
53/**
54 * Test of the Hazelcast-based distributed MastershipStore implementation.
55 */
56public class DistributedMastershipStoreTest {
57
58 private static final DeviceId DID1 = DeviceId.deviceId("of:01");
59 private static final DeviceId DID2 = DeviceId.deviceId("of:02");
60 private static final DeviceId DID3 = DeviceId.deviceId("of:03");
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070061
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070062 private static final IpAddress IP = IpAddress.valueOf("127.0.0.1");
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070063
64 private static final NodeId N1 = new NodeId("node1");
65 private static final NodeId N2 = new NodeId("node2");
66
67 private static final ControllerNode CN1 = new DefaultControllerNode(N1, IP);
68 private static final ControllerNode CN2 = new DefaultControllerNode(N2, IP);
69
70 private DistributedMastershipStore dms;
71 private TestDistributedMastershipStore testStore;
Ayaka Koshibe4c891272014-10-08 17:14:16 -070072 private KryoSerializer serializationMgr;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070073 private StoreManager storeMgr;
74
75 @BeforeClass
76 public static void setUpBeforeClass() throws Exception {
77 }
78
79 @AfterClass
80 public static void tearDownAfterClass() throws Exception {
81 }
82
83 @Before
84 public void setUp() throws Exception {
85 // TODO should find a way to clean Hazelcast instance without shutdown.
Yuta HIGUCHI151cad82015-02-04 23:26:50 -080086 TestStoreManager testStoreMgr = new TestStoreManager();
87 testStoreMgr.setHazelcastInstance(testStoreMgr.initSingleInstance());
88 storeMgr = testStoreMgr;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070089 storeMgr.activate();
90
Ayaka Koshibe4c891272014-10-08 17:14:16 -070091 serializationMgr = new KryoSerializer();
Ayaka Koshibe8583ff32014-10-02 16:25:30 -070092
93 dms = new TestDistributedMastershipStore(storeMgr, serializationMgr);
94 dms.clusterService = new TestClusterService();
95 dms.activate();
96
97 testStore = (TestDistributedMastershipStore) dms;
98 }
99
100 @After
101 public void tearDown() throws Exception {
102 dms.deactivate();
103
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700104 storeMgr.deactivate();
105 }
106
107 @Test
108 public void getRole() {
109 assertEquals("wrong role:", NONE, dms.getRole(N1, DID1));
Ayaka Koshibec4047702014-10-07 14:43:52 -0700110 testStore.put(DID1, N1, true, false, true);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700111 assertEquals("wrong role:", MASTER, dms.getRole(N1, DID1));
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700112 testStore.put(DID1, N2, false, true, false);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700113 assertEquals("wrong role:", STANDBY, dms.getRole(N2, DID1));
114 }
115
116 @Test
117 public void getMaster() {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700118 assertTrue("wrong store state:", dms.roleMap.isEmpty());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700119
120 testStore.put(DID1, N1, true, false, false);
121 assertEquals("wrong master:", N1, dms.getMaster(DID1));
122 assertNull("wrong master:", dms.getMaster(DID2));
123 }
124
125 @Test
126 public void getDevices() {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700127 assertTrue("wrong store state:", dms.roleMap.isEmpty());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700128
129 testStore.put(DID1, N1, true, false, false);
130 testStore.put(DID2, N1, true, false, false);
131 testStore.put(DID3, N2, true, false, false);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700132 assertEquals("wrong devices",
133 Sets.newHashSet(DID1, DID2), dms.getDevices(N1));
134 }
135
136 @Test
137 public void requestRoleAndTerm() {
138 //CN1 is "local"
139 testStore.setCurrent(CN1);
140
141 //if already MASTER, nothing should happen
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700142 testStore.put(DID2, N1, true, false, true);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700143 assertEquals("wrong role for MASTER:", MASTER, dms.requestRole(DID2));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700144
145 //populate maps with DID1, N1 thru NONE case
146 assertEquals("wrong role for NONE:", MASTER, dms.requestRole(DID1));
Ayaka Koshibec4047702014-10-07 14:43:52 -0700147 assertTrue("wrong state for store:", !dms.terms.isEmpty());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700148 assertEquals("wrong term",
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700149 MastershipTerm.of(N1, 1), dms.getTermFor(DID1));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700150
151 //CN2 now local. DID2 has N1 as MASTER so N2 is STANDBY
152 testStore.setCurrent(CN2);
153 assertEquals("wrong role for STANDBY:", STANDBY, dms.requestRole(DID2));
Ayaka Koshibec4047702014-10-07 14:43:52 -0700154 assertEquals("wrong number of entries:", 2, dms.terms.size());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700155
156 //change term and requestRole() again; should persist
157 testStore.increment(DID2);
158 assertEquals("wrong role for STANDBY:", STANDBY, dms.requestRole(DID2));
159 assertEquals("wrong term", MastershipTerm.of(N1, 1), dms.getTermFor(DID2));
160 }
161
162 @Test
163 public void setMaster() {
164 //populate maps with DID1, N1 as MASTER thru NONE case
165 testStore.setCurrent(CN1);
166 assertEquals("wrong role for NONE:", MASTER, dms.requestRole(DID1));
167 assertNull("wrong event:", dms.setMaster(N1, DID1));
168
169 //switch over to N2
170 assertEquals("wrong event:", Type.MASTER_CHANGED, dms.setMaster(N2, DID1).type());
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700171 System.out.println(dms.getTermFor(DID1).master() + ":" + dms.getTermFor(DID1).termNumber());
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700172 assertEquals("wrong term", MastershipTerm.of(N2, 2), dms.getTermFor(DID1));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700173
174 //orphan switch - should be rare case
175 assertEquals("wrong event:", Type.MASTER_CHANGED, dms.setMaster(N2, DID2).type());
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700176 assertEquals("wrong term", MastershipTerm.of(N2, 1), dms.getTermFor(DID2));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700177 //disconnect and reconnect - sign of failing re-election or single-instance channel
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700178 dms.roleMap.clear();
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700179 dms.setMaster(N2, DID2);
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700180 assertEquals("wrong term", MastershipTerm.of(N2, 2), dms.getTermFor(DID2));
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700181 }
182
183 @Test
Ayaka Koshibec4047702014-10-07 14:43:52 -0700184 public void relinquishRole() {
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700185 //populate maps with DID1, N1 as MASTER thru NONE case
186 testStore.setCurrent(CN1);
187 assertEquals("wrong role for NONE:", MASTER, dms.requestRole(DID1));
188 //no backup, no new MASTER/event
Ayaka Koshibec4047702014-10-07 14:43:52 -0700189 assertNull("wrong event:", dms.relinquishRole(N1, DID1));
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700190
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700191 dms.requestRole(DID1);
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700192
193 //add backup CN2, get it elected MASTER by relinquishing
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700194 testStore.setCurrent(CN2);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700195 assertEquals("wrong role for NONE:", STANDBY, dms.requestRole(DID1));
196 assertEquals("wrong event:", Type.MASTER_CHANGED, dms.relinquishRole(N1, DID1).type());
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700197 assertEquals("wrong master", N2, dms.getMaster(DID1));
198
Ayaka Koshibec4047702014-10-07 14:43:52 -0700199 //all nodes "give up" on device, which goes back to NONE.
200 assertNull("wrong event:", dms.relinquishRole(N2, DID1));
201 assertEquals("wrong role for node:", NONE, dms.getRole(N2, DID1));
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700202
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700203 assertEquals("wrong number of retired nodes", 2,
204 dms.roleMap.get(DID1).nodesOfRole(NONE).size());
Ayaka Koshibec4047702014-10-07 14:43:52 -0700205
206 //bring nodes back
207 assertEquals("wrong role for NONE:", MASTER, dms.requestRole(DID1));
208 testStore.setCurrent(CN1);
209 assertEquals("wrong role for NONE:", STANDBY, dms.requestRole(DID1));
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700210 assertEquals("wrong number of backup nodes", 1,
211 dms.roleMap.get(DID1).nodesOfRole(STANDBY).size());
Ayaka Koshibec4047702014-10-07 14:43:52 -0700212
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700213 //If STANDBY, should drop to NONE
Ayaka Koshibe98bd12f2014-11-01 20:13:37 -0700214 assertEquals("wrong event:", Type.BACKUPS_CHANGED, dms.relinquishRole(N1, DID1).type());
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700215 assertEquals("wrong role for node:", NONE, dms.getRole(N1, DID1));
216
Ayaka Koshibec4047702014-10-07 14:43:52 -0700217 //NONE - nothing happens
Ayaka Koshibe98bd12f2014-11-01 20:13:37 -0700218 assertEquals("wrong event:", Type.BACKUPS_CHANGED, dms.relinquishRole(N1, DID2).type());
Ayaka Koshibec4047702014-10-07 14:43:52 -0700219 assertEquals("wrong role for node:", NONE, dms.getRole(N1, DID2));
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700220
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700221 }
222
Ayaka Koshibe5c0f2372014-10-02 17:59:04 -0700223 @Ignore("Ignore until Delegate spec. is clear.")
224 @Test
225 public void testEvents() throws InterruptedException {
226 //shamelessly copy other distributed store tests
227 final CountDownLatch addLatch = new CountDownLatch(1);
228
229 MastershipStoreDelegate checkAdd = new MastershipStoreDelegate() {
230 @Override
231 public void notify(MastershipEvent event) {
232 assertEquals("wrong event:", Type.MASTER_CHANGED, event.type());
233 assertEquals("wrong subject", DID1, event.subject());
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700234 assertEquals("wrong subject", N1, event.roleInfo().master());
Ayaka Koshibe5c0f2372014-10-02 17:59:04 -0700235 addLatch.countDown();
236 }
237 };
238
239 dms.setDelegate(checkAdd);
240 dms.setMaster(N1, DID1);
241 //this will fail until we do something about single-instance-ness
242 assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
243 }
244
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700245 private class TestDistributedMastershipStore extends
246 DistributedMastershipStore {
247 public TestDistributedMastershipStore(StoreService storeService,
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700248 KryoSerializer kryoSerialization) {
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700249 this.storeService = storeService;
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700250 this.serializer = kryoSerialization;
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700251 }
252
253 //helper to populate master/backup structures
254 public void put(DeviceId dev, NodeId node,
Ayaka Koshibec4047702014-10-07 14:43:52 -0700255 boolean master, boolean backup, boolean term) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700256 RoleValue rv = dms.roleMap.get(dev);
257 if (rv == null) {
258 rv = new RoleValue();
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700259 }
Ayaka Koshibec4047702014-10-07 14:43:52 -0700260
261 if (master) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700262 rv.add(MASTER, node);
263 rv.reassign(node, STANDBY, NONE);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700264 }
265 if (backup) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700266 rv.add(STANDBY, node);
267 rv.remove(MASTER, node);
268 rv.remove(NONE, node);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700269 }
270 if (term) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700271 dms.terms.put(dev, 0);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700272 }
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700273 dms.roleMap.put(dev, rv);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700274 }
275
Ayaka Koshibe4c891272014-10-08 17:14:16 -0700276 //a dumb utility function.
Ayaka Koshibec4047702014-10-07 14:43:52 -0700277 public void dump() {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700278 for (Map.Entry<DeviceId, RoleValue> el : dms.roleMap.entrySet()) {
279 System.out.println("DID: " + el.getKey());
280 for (MastershipRole role : MastershipRole.values()) {
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700281 System.out.println("\t" + role.toString() + ":");
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700282 for (NodeId n : el.getValue().nodesOfRole(role)) {
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700283 System.out.println("\t\t" + n);
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700284 }
285 }
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700286 }
287 }
288
289 //increment term for a device
290 public void increment(DeviceId dev) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700291 Integer t = dms.terms.get(dev);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700292 if (t != null) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700293 dms.terms.put(dev, ++t);
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700294 }
295 }
296
297 //sets the "local" node
298 public void setCurrent(ControllerNode node) {
299 ((TestClusterService) clusterService).current = node;
300 }
301 }
302
303 private class TestClusterService implements ClusterService {
304
305 protected ControllerNode current;
306
307 @Override
308 public ControllerNode getLocalNode() {
309 return current;
310 }
311
312 @Override
313 public Set<ControllerNode> getNodes() {
314 return Sets.newHashSet(CN1, CN2);
315 }
316
317 @Override
318 public ControllerNode getNode(NodeId nodeId) {
319 return null;
320 }
321
322 @Override
323 public State getState(NodeId nodeId) {
324 return null;
325 }
326
327 @Override
328 public void addListener(ClusterEventListener listener) {
329 }
330
331 @Override
332 public void removeListener(ClusterEventListener listener) {
333 }
334
335 }
Ayaka Koshibe25fd23a2014-10-03 15:50:43 -0700336
Ayaka Koshibe8583ff32014-10-02 16:25:30 -0700337}