Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cluster.impl; |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 17 | |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 18 | import java.util.List; |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 19 | import java.util.Set; |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 20 | import java.util.function.Consumer; |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 21 | |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
| 23 | import com.google.common.collect.ImmutableSet; |
Victor Silva | f2b9d03 | 2016-09-19 19:43:20 -0300 | [diff] [blame] | 24 | import org.easymock.EasyMock; |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 28 | import org.onlab.junit.TestUtils; |
Ray Milkey | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame] | 29 | import org.onlab.packet.IpAddress; |
Victor Silva | f2b9d03 | 2016-09-19 19:43:20 -0300 | [diff] [blame] | 30 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.cluster.ClusterService; |
| 32 | import org.onosproject.cluster.ControllerNode; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.cluster.DefaultControllerNode; |
| 34 | import org.onosproject.cluster.NodeId; |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 35 | import org.onosproject.common.event.impl.TestEventDispatcher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.mastership.MastershipService; |
| 37 | import org.onosproject.mastership.MastershipStore; |
| 38 | import org.onosproject.mastership.MastershipTermService; |
| 39 | import org.onosproject.net.DeviceId; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 40 | import org.onosproject.net.config.NetworkConfigServiceAdapter; |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 41 | import org.onosproject.net.region.Region; |
| 42 | import org.onosproject.net.region.RegionId; |
| 43 | import org.onosproject.net.region.RegionStore; |
| 44 | import org.onosproject.net.region.impl.RegionManager; |
| 45 | import org.onosproject.store.cluster.StaticClusterService; |
| 46 | import org.onosproject.store.region.impl.DistributedRegionStore; |
| 47 | import org.onosproject.store.service.TestStorageService; |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 48 | import org.onosproject.store.trivial.SimpleMastershipStore; |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 49 | |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 50 | import com.google.common.collect.Sets; |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 51 | import com.google.common.util.concurrent.Futures; |
Jordan Halterman | 61aeb35 | 2017-10-18 16:22:17 -0700 | [diff] [blame] | 52 | import org.onosproject.upgrade.impl.UpgradeServiceAdapter; |
Ayaka Koshibe | ea5b4ce | 2014-10-11 14:17:17 -0700 | [diff] [blame] | 53 | |
Victor Silva | f2b9d03 | 2016-09-19 19:43:20 -0300 | [diff] [blame] | 54 | import static org.easymock.EasyMock.anyObject; |
| 55 | import static org.easymock.EasyMock.expect; |
| 56 | import static org.easymock.EasyMock.expectLastCall; |
| 57 | import static org.easymock.EasyMock.replay; |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 58 | import static org.junit.Assert.*; |
Ray Milkey | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame] | 59 | import static org.onosproject.net.MastershipRole.MASTER; |
| 60 | import static org.onosproject.net.MastershipRole.NONE; |
| 61 | import static org.onosproject.net.MastershipRole.STANDBY; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 62 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 63 | import static org.onosproject.net.region.Region.Type.METRO; |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Test codifying the mastership service contracts. |
| 67 | */ |
| 68 | public class MastershipManagerTest { |
| 69 | |
| 70 | private static final NodeId NID_LOCAL = new NodeId("local"); |
| 71 | private static final NodeId NID_OTHER = new NodeId("foo"); |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 72 | private static final IpAddress LOCALHOST = IpAddress.valueOf("127.0.0.1"); |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 73 | private static final DeviceId DEV_MASTER = DeviceId.deviceId("of:1"); |
| 74 | private static final DeviceId DEV_OTHER = DeviceId.deviceId("of:2"); |
| 75 | |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 76 | private static final RegionId RID1 = RegionId.regionId("r1"); |
| 77 | private static final RegionId RID2 = RegionId.regionId("r2"); |
| 78 | private static final DeviceId DID1 = DeviceId.deviceId("foo:d1"); |
| 79 | private static final DeviceId DID2 = DeviceId.deviceId("foo:d2"); |
| 80 | private static final DeviceId DID3 = DeviceId.deviceId("foo:d3"); |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 81 | private static final DeviceId DID4 = DeviceId.deviceId("foo:d4"); |
| 82 | private static final DeviceId DID5 = DeviceId.deviceId("foo:d5"); |
| 83 | private static final DeviceId DID6 = DeviceId.deviceId("foo:d6"); |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 84 | private static final NodeId NID1 = NodeId.nodeId("n1"); |
| 85 | private static final NodeId NID2 = NodeId.nodeId("n2"); |
| 86 | private static final NodeId NID3 = NodeId.nodeId("n3"); |
| 87 | private static final NodeId NID4 = NodeId.nodeId("n4"); |
| 88 | private static final ControllerNode CNODE1 = |
| 89 | new DefaultControllerNode(NID1, IpAddress.valueOf("127.0.1.1")); |
| 90 | private static final ControllerNode CNODE2 = |
| 91 | new DefaultControllerNode(NID2, IpAddress.valueOf("127.0.1.2")); |
| 92 | private static final ControllerNode CNODE3 = |
| 93 | new DefaultControllerNode(NID3, IpAddress.valueOf("127.0.1.3")); |
| 94 | private static final ControllerNode CNODE4 = |
| 95 | new DefaultControllerNode(NID4, IpAddress.valueOf("127.0.1.4")); |
| 96 | |
| 97 | |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 98 | private MastershipManager mgr; |
| 99 | protected MastershipService service; |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 100 | private TestRegionManager regionManager; |
| 101 | private RegionStore regionStore; |
| 102 | private TestClusterService testClusterService; |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 103 | |
| 104 | @Before |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 105 | public void setUp() throws Exception { |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 106 | mgr = new MastershipManager(); |
| 107 | service = mgr; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 108 | injectEventDispatcher(mgr, new TestEventDispatcher()); |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 109 | testClusterService = new TestClusterService(); |
| 110 | mgr.clusterService = testClusterService; |
Jordan Halterman | 61aeb35 | 2017-10-18 16:22:17 -0700 | [diff] [blame] | 111 | mgr.upgradeService = new UpgradeServiceAdapter(); |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 112 | mgr.store = new TestSimpleMastershipStore(mgr.clusterService); |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 113 | regionStore = new DistributedRegionStore(); |
| 114 | TestUtils.setField(regionStore, "storageService", new TestStorageService()); |
| 115 | TestUtils.callMethod(regionStore, "activate", |
| 116 | new Class<?>[] {}); |
| 117 | regionManager = new TestRegionManager(); |
| 118 | TestUtils.setField(regionManager, "store", regionStore); |
| 119 | regionManager.activate(); |
| 120 | mgr.regionService = regionManager; |
Victor Silva | f2b9d03 | 2016-09-19 19:43:20 -0300 | [diff] [blame] | 121 | |
| 122 | ComponentConfigService mockConfigService = |
| 123 | EasyMock.createMock(ComponentConfigService.class); |
| 124 | expect(mockConfigService.getProperties(anyObject())).andReturn(ImmutableSet.of()); |
| 125 | mockConfigService.registerProperties(mgr.getClass()); |
| 126 | expectLastCall(); |
| 127 | mockConfigService.unregisterProperties(mgr.getClass(), false); |
| 128 | expectLastCall(); |
| 129 | expect(mockConfigService.getProperties(anyObject())).andReturn(ImmutableSet.of()); |
| 130 | mgr.cfgService = mockConfigService; |
| 131 | replay(mockConfigService); |
| 132 | |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 133 | mgr.activate(); |
| 134 | } |
| 135 | |
| 136 | @After |
| 137 | public void tearDown() { |
| 138 | mgr.deactivate(); |
| 139 | mgr.clusterService = null; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 140 | injectEventDispatcher(mgr, null); |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 141 | regionManager.deactivate(); |
| 142 | mgr.regionService = null; |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 143 | mgr.store = null; |
| 144 | } |
| 145 | |
| 146 | @Test |
| 147 | public void setRole() { |
| 148 | mgr.setRole(NID_OTHER, DEV_MASTER, MASTER); |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 149 | assertEquals("wrong local role:", NONE, mgr.getLocalRole(DEV_MASTER)); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 150 | assertEquals("wrong obtained role:", STANDBY, Futures.getUnchecked(mgr.requestRoleFor(DEV_MASTER))); |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 151 | |
| 152 | //set to master |
| 153 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 154 | assertEquals("wrong local role:", MASTER, mgr.getLocalRole(DEV_MASTER)); |
| 155 | } |
| 156 | |
| 157 | @Test |
| 158 | public void relinquishMastership() { |
Ayaka Koshibe | b62aab5 | 2014-10-24 13:15:25 -0700 | [diff] [blame] | 159 | //no backups - should just turn to NONE for device. |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 160 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 161 | assertEquals("wrong role:", MASTER, mgr.getLocalRole(DEV_MASTER)); |
| 162 | mgr.relinquishMastership(DEV_MASTER); |
| 163 | assertNull("wrong master:", mgr.getMasterFor(DEV_OTHER)); |
Ayaka Koshibe | b62aab5 | 2014-10-24 13:15:25 -0700 | [diff] [blame] | 164 | assertEquals("wrong role:", NONE, mgr.getLocalRole(DEV_MASTER)); |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 165 | |
| 166 | //not master, nothing should happen |
Ayaka Koshibe | b62aab5 | 2014-10-24 13:15:25 -0700 | [diff] [blame] | 167 | mgr.setRole(NID_LOCAL, DEV_OTHER, NONE); |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 168 | mgr.relinquishMastership(DEV_OTHER); |
| 169 | assertNull("wrong role:", mgr.getMasterFor(DEV_OTHER)); |
| 170 | |
| 171 | //provide NID_OTHER as backup and relinquish |
| 172 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 173 | assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_MASTER)); |
| 174 | mgr.setRole(NID_OTHER, DEV_MASTER, STANDBY); |
| 175 | mgr.relinquishMastership(DEV_MASTER); |
| 176 | assertEquals("wrong master:", NID_OTHER, mgr.getMasterFor(DEV_MASTER)); |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Test |
| 180 | public void requestRoleFor() { |
| 181 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 182 | mgr.setRole(NID_OTHER, DEV_OTHER, MASTER); |
| 183 | |
| 184 | //local should be master for one but standby for other |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 185 | assertEquals("wrong role:", MASTER, Futures.getUnchecked(mgr.requestRoleFor(DEV_MASTER))); |
| 186 | assertEquals("wrong role:", STANDBY, Futures.getUnchecked(mgr.requestRoleFor(DEV_OTHER))); |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Test |
| 190 | public void getMasterFor() { |
| 191 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 192 | mgr.setRole(NID_OTHER, DEV_OTHER, MASTER); |
| 193 | assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_MASTER)); |
| 194 | assertEquals("wrong master:", NID_OTHER, mgr.getMasterFor(DEV_OTHER)); |
| 195 | |
| 196 | //have NID_OTHER hand over DEV_OTHER to NID_LOCAL |
| 197 | mgr.setRole(NID_LOCAL, DEV_OTHER, MASTER); |
| 198 | assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_OTHER)); |
| 199 | } |
| 200 | |
| 201 | @Test |
| 202 | public void getDevicesOf() { |
| 203 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 204 | mgr.setRole(NID_LOCAL, DEV_OTHER, STANDBY); |
| 205 | assertEquals("should be one device:", 1, mgr.getDevicesOf(NID_LOCAL).size()); |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 206 | //hand both devices to NID_LOCAL |
| 207 | mgr.setRole(NID_LOCAL, DEV_OTHER, MASTER); |
| 208 | assertEquals("should be two devices:", 2, mgr.getDevicesOf(NID_LOCAL).size()); |
| 209 | } |
| 210 | |
Ayaka Koshibe | 48239b0 | 2014-09-25 17:12:31 -0700 | [diff] [blame] | 211 | @Test |
| 212 | public void termService() { |
Yuta HIGUCHI | bcac499 | 2014-11-22 19:27:57 -0800 | [diff] [blame] | 213 | MastershipTermService ts = mgr; |
Ayaka Koshibe | 48239b0 | 2014-09-25 17:12:31 -0700 | [diff] [blame] | 214 | |
Yuta HIGUCHI | dfe6e3b | 2014-10-30 11:31:51 -0700 | [diff] [blame] | 215 | //term = 1 for both |
Ayaka Koshibe | 48239b0 | 2014-09-25 17:12:31 -0700 | [diff] [blame] | 216 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
Yuta HIGUCHI | dfe6e3b | 2014-10-30 11:31:51 -0700 | [diff] [blame] | 217 | assertEquals("inconsistent term: ", 1, ts.getMastershipTerm(DEV_MASTER).termNumber()); |
Ayaka Koshibe | 48239b0 | 2014-09-25 17:12:31 -0700 | [diff] [blame] | 218 | |
Yuta HIGUCHI | dfe6e3b | 2014-10-30 11:31:51 -0700 | [diff] [blame] | 219 | //hand devices to NID_LOCAL and back: term = 1 + 2 |
Ayaka Koshibe | 48239b0 | 2014-09-25 17:12:31 -0700 | [diff] [blame] | 220 | mgr.setRole(NID_OTHER, DEV_MASTER, MASTER); |
| 221 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
Yuta HIGUCHI | dfe6e3b | 2014-10-30 11:31:51 -0700 | [diff] [blame] | 222 | assertEquals("inconsistent terms: ", 3, ts.getMastershipTerm(DEV_MASTER).termNumber()); |
Ayaka Koshibe | 48239b0 | 2014-09-25 17:12:31 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 225 | @Test |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 226 | public void balanceWithOrphans() { |
| 227 | // Setup cluster of three nodes |
| 228 | testClusterService.put(CNODE1, ControllerNode.State.ACTIVE); |
| 229 | testClusterService.put(CNODE2, ControllerNode.State.INACTIVE); |
| 230 | testClusterService.put(CNODE3, ControllerNode.State.ACTIVE); |
| 231 | |
| 232 | // Pre-assign some devices to each of the node |
| 233 | // Leave some devices as orphans assigned to a downed node |
| 234 | assignRoles(NID1, ImmutableSet.of(DID1, DID2, DID3, DID4)); |
| 235 | assignRoles(NID2, ImmutableSet.of(DID5)); |
| 236 | assignRoles(NID3, ImmutableSet.of(DID6)); |
| 237 | |
| 238 | // Trigger load balancing |
| 239 | mgr.balanceRoles(); |
| 240 | |
| 241 | // Make sure we have a balanced load |
| 242 | // Make sure that we no longer have any orphans |
| 243 | assertEquals("incorrect balance for node 1", 3, mgr.getDevicesOf(NID1).size()); |
| 244 | assertEquals("incorrect balance for node 2", 0, mgr.getDevicesOf(NID2).size()); |
| 245 | assertEquals("incorrect balance for node 3", 3, mgr.getDevicesOf(NID3).size()); |
| 246 | } |
| 247 | |
| 248 | private void assignRoles(NodeId nid, Set<DeviceId> deviceIds) { |
| 249 | Set<DeviceId> all = ImmutableSet.of(DID1, DID2, DID3, DID4, DID5, DID6); |
| 250 | for (DeviceId did : all) { |
| 251 | mgr.setRole(nid, did, deviceIds.contains(did) ? MASTER : STANDBY); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | @Test |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 256 | public void balanceWithRegion1() { |
| 257 | //set up region - 2 sets of masters with 1 node in each |
| 258 | Set<NodeId> masterSet1 = ImmutableSet.of(NID1); |
| 259 | Set<NodeId> masterSet2 = ImmutableSet.of(NID2); |
| 260 | List<Set<NodeId>> masters = ImmutableList.of(masterSet1, masterSet2); |
| 261 | Region r = regionManager.createRegion(RID1, "R1", METRO, masters); |
| 262 | regionManager.addDevices(RID1, ImmutableSet.of(DID1, DID2)); |
| 263 | Set<DeviceId> deviceIds = regionManager.getRegionDevices(RID1); |
| 264 | assertEquals("incorrect device count", 2, deviceIds.size()); |
| 265 | |
| 266 | testClusterService.put(CNODE1, ControllerNode.State.ACTIVE); |
| 267 | testClusterService.put(CNODE2, ControllerNode.State.ACTIVE); |
| 268 | |
| 269 | //set master to non region nodes |
| 270 | mgr.setRole(NID_LOCAL, DID1, MASTER); |
| 271 | mgr.setRole(NID_LOCAL, DID2, MASTER); |
| 272 | assertEquals("wrong local role:", MASTER, mgr.getLocalRole(DID1)); |
| 273 | assertEquals("wrong local role:", MASTER, mgr.getLocalRole(DID2)); |
| 274 | assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DID1)); |
| 275 | assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DID2)); |
| 276 | |
| 277 | //do region balancing |
| 278 | mgr.useRegionForBalanceRoles = true; |
| 279 | mgr.balanceRoles(); |
| 280 | assertEquals("wrong master:", NID1, mgr.getMasterFor(DID1)); |
| 281 | assertEquals("wrong master:", NID1, mgr.getMasterFor(DID2)); |
| 282 | |
| 283 | // make N1 inactive |
| 284 | testClusterService.put(CNODE1, ControllerNode.State.INACTIVE); |
| 285 | mgr.balanceRoles(); |
| 286 | assertEquals("wrong master:", NID2, mgr.getMasterFor(DID1)); |
| 287 | assertEquals("wrong master:", NID2, mgr.getMasterFor(DID2)); |
| 288 | |
| 289 | } |
| 290 | |
| 291 | @Test |
| 292 | public void balanceWithRegion2() { |
| 293 | //set up region - 2 sets of masters with (3 nodes, 1 node) |
| 294 | Set<NodeId> masterSet1 = ImmutableSet.of(NID1, NID3, NID4); |
| 295 | Set<NodeId> masterSet2 = ImmutableSet.of(NID2); |
| 296 | List<Set<NodeId>> masters = ImmutableList.of(masterSet1, masterSet2); |
| 297 | Region r = regionManager.createRegion(RID1, "R1", METRO, masters); |
| 298 | Set<DeviceId> deviceIdsOrig = ImmutableSet.of(DID1, DID2, DID3, DEV_OTHER); |
| 299 | regionManager.addDevices(RID1, deviceIdsOrig); |
| 300 | Set<DeviceId> deviceIds = regionManager.getRegionDevices(RID1); |
| 301 | assertEquals("incorrect device count", deviceIdsOrig.size(), deviceIds.size()); |
| 302 | assertEquals("incorrect devices in region", deviceIdsOrig, deviceIds); |
| 303 | |
| 304 | testClusterService.put(CNODE1, ControllerNode.State.ACTIVE); |
| 305 | testClusterService.put(CNODE2, ControllerNode.State.ACTIVE); |
| 306 | testClusterService.put(CNODE3, ControllerNode.State.ACTIVE); |
| 307 | testClusterService.put(CNODE4, ControllerNode.State.ACTIVE); |
| 308 | |
| 309 | //set master to non region nodes |
| 310 | deviceIdsOrig.forEach(deviceId1 -> mgr.setRole(NID_LOCAL, deviceId1, MASTER)); |
| 311 | checkDeviceMasters(deviceIds, Sets.newHashSet(NID_LOCAL), deviceId -> |
| 312 | assertEquals("wrong local role:", MASTER, mgr.getLocalRole(deviceId))); |
| 313 | |
| 314 | //do region balancing |
| 315 | mgr.useRegionForBalanceRoles = true; |
| 316 | mgr.balanceRoles(); |
| 317 | Set<NodeId> expectedMasters = Sets.newHashSet(NID1, NID3, NID4); |
| 318 | checkDeviceMasters(deviceIds, expectedMasters); |
| 319 | |
| 320 | // make N1 inactive |
| 321 | testClusterService.put(CNODE1, ControllerNode.State.INACTIVE); |
| 322 | expectedMasters.remove(NID1); |
| 323 | mgr.balanceRoles(); |
| 324 | checkDeviceMasters(deviceIds, expectedMasters); |
| 325 | |
| 326 | // make N4 inactive |
| 327 | testClusterService.put(CNODE4, ControllerNode.State.INACTIVE); |
| 328 | expectedMasters.remove(NID4); |
| 329 | mgr.balanceRoles(); |
| 330 | checkDeviceMasters(deviceIds, expectedMasters); |
| 331 | |
| 332 | // make N3 inactive |
| 333 | testClusterService.put(CNODE3, ControllerNode.State.INACTIVE); |
| 334 | expectedMasters = Sets.newHashSet(NID2); |
| 335 | mgr.balanceRoles(); |
| 336 | checkDeviceMasters(deviceIds, expectedMasters); |
| 337 | |
| 338 | // make N3 active |
| 339 | testClusterService.put(CNODE3, ControllerNode.State.ACTIVE); |
| 340 | expectedMasters = Sets.newHashSet(NID3); |
| 341 | mgr.balanceRoles(); |
| 342 | checkDeviceMasters(deviceIds, expectedMasters); |
| 343 | |
| 344 | // make N4 active |
| 345 | testClusterService.put(CNODE4, ControllerNode.State.ACTIVE); |
| 346 | expectedMasters.add(NID4); |
| 347 | mgr.balanceRoles(); |
| 348 | checkDeviceMasters(deviceIds, expectedMasters); |
| 349 | |
| 350 | // make N1 active |
| 351 | testClusterService.put(CNODE1, ControllerNode.State.ACTIVE); |
| 352 | expectedMasters.add(NID1); |
| 353 | mgr.balanceRoles(); |
| 354 | checkDeviceMasters(deviceIds, expectedMasters); |
| 355 | } |
| 356 | |
| 357 | private void checkDeviceMasters(Set<DeviceId> deviceIds, Set<NodeId> expectedMasters) { |
| 358 | checkDeviceMasters(deviceIds, expectedMasters, null); |
| 359 | } |
| 360 | |
| 361 | private void checkDeviceMasters(Set<DeviceId> deviceIds, Set<NodeId> expectedMasters, |
| 362 | Consumer<DeviceId> checkRole) { |
| 363 | // each device's master must be contained in the list of expectedMasters |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 364 | deviceIds.forEach(deviceId -> { |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 365 | assertTrue("wrong master:", expectedMasters.contains(mgr.getMasterFor(deviceId))); |
| 366 | if (checkRole != null) { |
| 367 | checkRole.accept(deviceId); |
| 368 | } |
| 369 | }); |
| 370 | // each node in expectedMasters must have approximately the same number of devices |
| 371 | if (expectedMasters.size() > 1) { |
| 372 | int minValue = Integer.MAX_VALUE; |
| 373 | int maxDevices = -1; |
| 374 | for (NodeId nodeId: expectedMasters) { |
| 375 | int numDevicesManagedByNode = mgr.getDevicesOf(nodeId).size(); |
| 376 | if (numDevicesManagedByNode < minValue) { |
| 377 | minValue = numDevicesManagedByNode; |
| 378 | } |
| 379 | if (numDevicesManagedByNode > maxDevices) { |
| 380 | maxDevices = numDevicesManagedByNode; |
| 381 | } |
| 382 | assertTrue("not balanced:", maxDevices - minValue <= 1); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | private final class TestClusterService extends StaticClusterService { |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 388 | |
| 389 | ControllerNode local = new DefaultControllerNode(NID_LOCAL, LOCALHOST); |
| 390 | |
| 391 | @Override |
| 392 | public ControllerNode getLocalNode() { |
| 393 | return local; |
| 394 | } |
| 395 | |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 396 | public void put(ControllerNode cn, ControllerNode.State state) { |
| 397 | nodes.put(cn.id(), cn); |
| 398 | nodeStates.put(cn.id(), state); |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 399 | } |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 400 | } |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 401 | |
| 402 | private final class TestSimpleMastershipStore extends SimpleMastershipStore |
| 403 | implements MastershipStore { |
| 404 | |
| 405 | public TestSimpleMastershipStore(ClusterService clusterService) { |
| 406 | super.clusterService = clusterService; |
| 407 | } |
| 408 | } |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 409 | |
| 410 | private class TestRegionManager extends RegionManager { |
| 411 | TestRegionManager() { |
| 412 | eventDispatcher = new TestEventDispatcher(); |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 413 | networkConfigService = new NetworkConfigServiceAdapter(); |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame] | 416 | } |