blob: cee2431205802a4e813f03fbfec90eea208977b2 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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 */
Thomas Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070017
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080018import java.util.ArrayList;
19import java.util.List;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070020import java.util.Set;
21import java.util.concurrent.atomic.AtomicInteger;
22
pierventrecdcd91c2021-05-06 09:27:00 +020023import com.google.common.collect.Lists;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070024import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.cluster.NodeId;
28import org.onosproject.mastership.MastershipEvent;
29import org.onosproject.mastership.MastershipTerm;
30import org.onosproject.net.DeviceId;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070031
32import com.google.common.collect.Sets;
Madan Jampanif7536ab2015-05-07 23:23:23 -070033import com.google.common.util.concurrent.Futures;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070034
35import static org.junit.Assert.assertEquals;
36import static org.junit.Assert.assertNull;
37import static org.junit.Assert.assertTrue;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import static org.onosproject.mastership.MastershipEvent.Type.*;
39import static org.onosproject.net.MastershipRole.*;
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070040
41/**
42 * Test for the simple MastershipStore implementation.
43 */
44public class SimpleMastershipStoreTest {
45
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070046 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");
49 private static final DeviceId DID4 = DeviceId.deviceId("of:04");
50
51 private static final NodeId N1 = new NodeId("local");
52 private static final NodeId N2 = new NodeId("other");
pierventrecdcd91c2021-05-06 09:27:00 +020053 private static final NodeId N3 = new NodeId("other2");
54 private static final NodeId N4 = new NodeId("other3");
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070055
56 private SimpleMastershipStore sms;
57
58 @Before
59 public void setUp() throws Exception {
60 sms = new SimpleMastershipStore();
61 sms.activate();
62 }
63
64 @After
65 public void tearDown() throws Exception {
66 sms.deactivate();
67 }
68
69 @Test
70 public void getRole() {
71 //special case, no backup or master
72 put(DID1, N1, false, false);
73 assertEquals("wrong role", NONE, sms.getRole(N1, DID1));
74
75 //backup exists but we aren't mapped
76 put(DID2, N1, false, true);
77 assertEquals("wrong role", STANDBY, sms.getRole(N1, DID2));
78
79 //N2 is master
80 put(DID3, N2, true, true);
81 assertEquals("wrong role", MASTER, sms.getRole(N2, DID3));
82
83 //N2 is master but N1 is only in backups set
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080084 put(DID4, N1, false, true);
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -070085 put(DID4, N2, true, false);
86 assertEquals("wrong role", STANDBY, sms.getRole(N1, DID4));
87 }
88
89 @Test
90 public void getMaster() {
91 put(DID3, N2, true, true);
92 assertEquals("wrong role", MASTER, sms.getRole(N2, DID3));
93 assertEquals("wrong device", N2, sms.getMaster(DID3));
94 }
95
96 @Test
97 public void setMaster() {
98 put(DID1, N1, false, false);
Madan Jampanif7536ab2015-05-07 23:23:23 -070099 assertEquals("wrong event", MASTER_CHANGED, Futures.getUnchecked(sms.setMaster(N1, DID1)).type());
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700100 assertEquals("wrong role", MASTER, sms.getRole(N1, DID1));
101 //set node that's already master - should be ignored
Madan Jampanif7536ab2015-05-07 23:23:23 -0700102 assertNull("wrong event", Futures.getUnchecked(sms.setMaster(N1, DID1)));
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700103
104 //set STANDBY to MASTER
105 put(DID2, N1, false, true);
106 assertEquals("wrong role", STANDBY, sms.getRole(N1, DID2));
Madan Jampanif7536ab2015-05-07 23:23:23 -0700107 assertEquals("wrong event", MASTER_CHANGED, Futures.getUnchecked(sms.setMaster(N1, DID2)).type());
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700108 assertEquals("wrong role", MASTER, sms.getRole(N1, DID2));
109 }
110
111 @Test
112 public void getDevices() {
113 Set<DeviceId> d = Sets.newHashSet(DID1, DID2);
114
115 put(DID1, N2, true, true);
116 put(DID2, N2, true, true);
117 put(DID3, N1, true, true);
118 assertTrue("wrong devices", d.equals(sms.getDevices(N2)));
119 }
120
121 @Test
122 public void getTermFor() {
123 put(DID1, N1, true, true);
124 assertEquals("wrong term", MastershipTerm.of(N1, 0), sms.getTermFor(DID1));
125
126 //switch to N2 and back - 2 term switches
127 sms.setMaster(N2, DID1);
128 sms.setMaster(N1, DID1);
129 assertEquals("wrong term", MastershipTerm.of(N1, 2), sms.getTermFor(DID1));
130 }
131
132 @Test
133 public void requestRole() {
134 //NONE - become MASTER
135 put(DID1, N1, false, false);
Madan Jampanide003d92015-05-11 17:14:20 -0700136 assertEquals("wrong role", MASTER, Futures.getUnchecked(sms.requestRole(DID1)));
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700137
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800138 //was STANDBY - become MASTER
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700139 put(DID2, N1, false, true);
Madan Jampanide003d92015-05-11 17:14:20 -0700140 assertEquals("wrong role", MASTER, Futures.getUnchecked(sms.requestRole(DID2)));
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700141
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800142 //other MASTER - stay STANDBY
143 put(DID3, N2, true, false);
Madan Jampanide003d92015-05-11 17:14:20 -0700144 assertEquals("wrong role", STANDBY, Futures.getUnchecked(sms.requestRole(DID3)));
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700145
146 //local (N1) is MASTER - stay MASTER
147 put(DID4, N1, true, true);
Madan Jampanide003d92015-05-11 17:14:20 -0700148 assertEquals("wrong role", MASTER, Futures.getUnchecked(sms.requestRole(DID4)));
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700149 }
150
151 @Test
152 public void unsetMaster() {
153 //NONE - record backup but take no other action
154 put(DID1, N1, false, false);
Ayaka Koshibec4047702014-10-07 14:43:52 -0700155 sms.setStandby(N1, DID1);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800156 assertTrue("not backed up", sms.backups.get(DID1).contains(N1));
157 int prev = sms.termMap.get(DID1).get();
Ayaka Koshibec4047702014-10-07 14:43:52 -0700158 sms.setStandby(N1, DID1);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800159 assertEquals("term should not change", prev, sms.termMap.get(DID1).get());
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700160
161 //no backup, MASTER
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800162 put(DID1, N1, true, false);
Madan Jampanif7536ab2015-05-07 23:23:23 -0700163 assertNull("expect no MASTER event", Futures.getUnchecked(sms.setStandby(N1, DID1)).roleInfo().master());
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700164 assertNull("wrong node", sms.masterMap.get(DID1));
165
166 //backup, switch
167 sms.masterMap.clear();
168 put(DID1, N1, true, true);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800169 put(DID1, N2, false, true);
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700170 put(DID2, N2, true, true);
Madan Jampanif7536ab2015-05-07 23:23:23 -0700171 MastershipEvent event = Futures.getUnchecked(sms.setStandby(N1, DID1));
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800172 assertEquals("wrong event", MASTER_CHANGED, event.type());
173 assertEquals("wrong master", N2, event.roleInfo().master());
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700174 }
175
pierventrecdcd91c2021-05-06 09:27:00 +0200176 @Test
177 public void demote() {
178 put(DID1, N1, true, false);
179 put(DID1, N2, false, true);
180 put(DID1, N3, false, true);
181 List<NodeId> stdbys = Lists.newArrayList(N2, N3);
182 // N1 master, N2 and N3 backups
183 assertEquals("wrong role", MASTER, sms.getRole(N1, DID1));
184 assertEquals("wrong backups", stdbys, sms.backups.getOrDefault(DID1, new ArrayList<>()));
185 // No effect, it is the master
186 sms.demote(N1, DID1);
187 assertEquals("wrong role", MASTER, sms.getRole(N1, DID1));
188 assertEquals("wrong backups", stdbys, sms.backups.getOrDefault(DID1, new ArrayList<>()));
189 // No effect, it is not part of the mastership
190 sms.demote(N4, DID1);
191 assertEquals("wrong role", MASTER, sms.getRole(N1, DID1));
192 assertEquals("wrong backups", stdbys, sms.backups.getOrDefault(DID1, new ArrayList<>()));
193 // Demote N2
194 stdbys = Lists.newArrayList(N3, N2);
195 sms.demote(N2, DID1);
196 assertEquals("wrong role", MASTER, sms.getRole(N1, DID1));
197 assertEquals("wrong backups", stdbys, sms.backups.getOrDefault(DID1, new ArrayList<>()));
198 }
199
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700200 //helper to populate master/backup structures
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800201 private void put(DeviceId dev, NodeId node, boolean master, boolean backup) {
202 if (master) {
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700203 sms.masterMap.put(dev, node);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800204 } else if (backup) {
205 List<NodeId> stbys = sms.backups.getOrDefault(dev, new ArrayList<>());
206 stbys.add(node);
207 sms.backups.put(dev, stbys);
Ayaka Koshibe1743ddb2014-09-30 15:24:48 -0700208 }
209 sms.termMap.put(dev, new AtomicInteger());
210 }
211}