Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [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 | */ |
| 16 | package org.onosproject.store.intent.impl; |
| 17 | |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
| 20 | import org.onlab.junit.NullScheduledExecutor; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.cluster.ClusterServiceAdapter; |
| 23 | import org.onosproject.cluster.ControllerNode; |
| 24 | import org.onosproject.cluster.DefaultControllerNode; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 25 | import org.onosproject.cluster.Leader; |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 26 | import org.onosproject.cluster.Leadership; |
| 27 | import org.onosproject.cluster.LeadershipEvent; |
| 28 | import org.onosproject.cluster.LeadershipEventListener; |
| 29 | import org.onosproject.cluster.LeadershipService; |
| 30 | import org.onosproject.cluster.LeadershipServiceAdapter; |
| 31 | import org.onosproject.cluster.NodeId; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 32 | import org.onosproject.common.event.impl.TestEventDispatcher; |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 33 | import org.onosproject.net.intent.Key; |
| 34 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 35 | import java.util.Arrays; |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 36 | import java.util.HashSet; |
Madan Jampani | a4a5994 | 2016-05-02 11:25:34 -0700 | [diff] [blame] | 37 | import java.util.List; |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 38 | import java.util.Objects; |
| 39 | import java.util.Set; |
Madan Jampani | a4a5994 | 2016-05-02 11:25:34 -0700 | [diff] [blame] | 40 | |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 41 | import static junit.framework.TestCase.assertFalse; |
| 42 | import static org.easymock.EasyMock.anyObject; |
| 43 | import static org.easymock.EasyMock.anyString; |
| 44 | import static org.easymock.EasyMock.createMock; |
| 45 | import static org.easymock.EasyMock.expect; |
| 46 | import static org.easymock.EasyMock.expectLastCall; |
| 47 | import static org.easymock.EasyMock.replay; |
| 48 | import static org.easymock.EasyMock.reset; |
| 49 | import static org.easymock.EasyMock.verify; |
| 50 | import static org.junit.Assert.assertTrue; |
| 51 | |
| 52 | /** |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 53 | * Unit tests for the WorkPartitionManager class. |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 54 | */ |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 55 | public class WorkPartitionManagerTest { |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 56 | |
| 57 | private final LeadershipEvent event |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 58 | = new LeadershipEvent(LeadershipEvent.Type.CANDIDATES_CHANGED, |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 59 | new Leadership(ELECTION_PREFIX + "0", |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 60 | new Leader(MY_NODE_ID, 0, 0), |
| 61 | Arrays.asList(MY_NODE_ID, OTHER_NODE_ID))); |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 62 | |
| 63 | private static final NodeId MY_NODE_ID = new NodeId("local"); |
| 64 | private static final NodeId OTHER_NODE_ID = new NodeId("other"); |
| 65 | private static final NodeId INACTIVE_NODE_ID = new NodeId("inactive"); |
| 66 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 67 | private static final String ELECTION_PREFIX = "work-partition-"; |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 68 | |
| 69 | private LeadershipService leadershipService; |
| 70 | private LeadershipEventListener leaderListener; |
| 71 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 72 | private WorkPartitionManager partitionManager; |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 73 | |
| 74 | @Before |
| 75 | public void setUp() { |
| 76 | leadershipService = createMock(LeadershipService.class); |
| 77 | |
| 78 | leadershipService.addListener(anyObject(LeadershipEventListener.class)); |
| 79 | expectLastCall().andDelegateTo(new TestLeadershipService()); |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 80 | for (int i = 0; i < WorkPartitionManager.NUM_PARTITIONS; i++) { |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 81 | expect(leadershipService.runForLeadership(ELECTION_PREFIX + i)) |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 82 | .andReturn(null) |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 83 | .times(1); |
| 84 | } |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 85 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 86 | partitionManager = new WorkPartitionManager() |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 87 | .withScheduledExecutor(new NullScheduledExecutor()); |
| 88 | |
| 89 | partitionManager.clusterService = new TestClusterService(); |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 90 | partitionManager.localNodeId = MY_NODE_ID; |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 91 | partitionManager.leadershipService = leadershipService; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 92 | partitionManager.eventDispatcher = new TestEventDispatcher(); |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Configures a mock leadership service to have the specified number of |
| 97 | * partitions owned by the local node and all other partitions owned by a |
| 98 | * (fake) remote node. |
| 99 | * |
| 100 | * @param numMine number of partitions that should be owned by the local node |
| 101 | */ |
| 102 | private void setUpLeadershipService(int numMine) { |
Madan Jampani | a4a5994 | 2016-05-02 11:25:34 -0700 | [diff] [blame] | 103 | List<NodeId> allNodes = Arrays.asList(MY_NODE_ID, OTHER_NODE_ID); |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 104 | for (int i = 0; i < numMine; i++) { |
Madan Jampani | a4a5994 | 2016-05-02 11:25:34 -0700 | [diff] [blame] | 105 | expect(leadershipService.getLeadership(ELECTION_PREFIX + i)) |
| 106 | .andReturn(new Leadership(ELECTION_PREFIX + i, |
| 107 | new Leader(MY_NODE_ID, 1, 1000), |
| 108 | allNodes)) |
| 109 | .anyTimes(); |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 112 | for (int i = numMine; i < WorkPartitionManager.NUM_PARTITIONS; i++) { |
Madan Jampani | a4a5994 | 2016-05-02 11:25:34 -0700 | [diff] [blame] | 113 | expect(leadershipService.getLeadership(ELECTION_PREFIX + i)) |
| 114 | .andReturn(new Leadership(ELECTION_PREFIX + i, |
| 115 | new Leader(OTHER_NODE_ID, 1, 1000), |
| 116 | allNodes)) |
| 117 | .anyTimes(); |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 118 | } |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 119 | for (int i = 0; i < WorkPartitionManager.NUM_PARTITIONS; i++) { |
Madan Jampani | 783d3d2 | 2016-06-13 17:40:02 -0700 | [diff] [blame] | 120 | expect(leadershipService.getCandidates(ELECTION_PREFIX + i)) |
| 121 | .andReturn(Arrays.asList(MY_NODE_ID, OTHER_NODE_ID)) |
| 122 | .anyTimes(); |
| 123 | } |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Tests that the PartitionManager's activate method correctly runs for |
| 128 | * all the leader elections that it should. |
| 129 | */ |
| 130 | @Test |
| 131 | public void testActivate() { |
| 132 | reset(leadershipService); |
| 133 | |
| 134 | leadershipService.addListener(anyObject(LeadershipEventListener.class)); |
| 135 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 136 | for (int i = 0; i < WorkPartitionManager.NUM_PARTITIONS; i++) { |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 137 | expect(leadershipService.runForLeadership(ELECTION_PREFIX + i)) |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 138 | .andReturn(null) |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 139 | .times(1); |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | replay(leadershipService); |
| 143 | |
| 144 | partitionManager.activate(); |
| 145 | |
| 146 | verify(leadershipService); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Tests that the isMine method returns the correct result based on the |
| 151 | * underlying leadership service data. |
| 152 | */ |
| 153 | @Test |
| 154 | public void testIsMine() { |
| 155 | // We'll own only the first partition |
| 156 | setUpLeadershipService(1); |
| 157 | replay(leadershipService); |
| 158 | |
| 159 | Key myKey = new ControllableHashKey(0); |
| 160 | Key notMyKey = new ControllableHashKey(1); |
| 161 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 162 | assertTrue(partitionManager.isMine(myKey, Key::hash)); |
| 163 | assertFalse(partitionManager.isMine(notMyKey, Key::hash)); |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 164 | |
| 165 | // Make us the owner of 4 partitions now |
| 166 | reset(leadershipService); |
| 167 | setUpLeadershipService(4); |
| 168 | replay(leadershipService); |
| 169 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 170 | assertTrue(partitionManager.isMine(myKey, Key::hash)); |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 171 | // notMyKey is now my key because because we're in control of that |
| 172 | // partition now |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 173 | assertTrue(partitionManager.isMine(notMyKey, Key::hash)); |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 174 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 175 | assertFalse(partitionManager.isMine(new ControllableHashKey(4), Key::hash)); |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Tests sending in LeadershipServiceEvents in the case when we have |
| 180 | * too many partitions. The event will trigger the partition manager to |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 181 | * schedule a rebalancing activity. |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 182 | */ |
| 183 | @Test |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 184 | public void testRebalanceScheduling() { |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 185 | // We have all the partitions so we'll need to relinquish some |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 186 | setUpLeadershipService(WorkPartitionManager.NUM_PARTITIONS); |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 187 | |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 188 | replay(leadershipService); |
| 189 | |
| 190 | partitionManager.activate(); |
| 191 | // Send in the event |
| 192 | leaderListener.event(event); |
| 193 | |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 194 | assertTrue(partitionManager.rebalanceScheduled.get()); |
| 195 | |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 196 | verify(leadershipService); |
| 197 | } |
| 198 | |
| 199 | /** |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 200 | * Tests rebalance will trigger the right now of leadership withdraw calls. |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 201 | */ |
| 202 | @Test |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 203 | public void testRebalance() { |
| 204 | // We have all the partitions so we'll need to relinquish some |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 205 | setUpLeadershipService(WorkPartitionManager.NUM_PARTITIONS); |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 206 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 207 | leadershipService.withdraw(anyString()); |
| 208 | expectLastCall().times(7); |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 209 | |
| 210 | replay(leadershipService); |
| 211 | |
| 212 | partitionManager.activate(); |
| 213 | |
| 214 | // trigger rebalance |
| 215 | partitionManager.doRebalance(); |
| 216 | |
| 217 | verify(leadershipService); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Tests that attempts to rebalance when the paritions are already |
| 222 | * evenly distributed does not result in any relinquish attempts. |
| 223 | */ |
| 224 | @Test |
| 225 | public void testNoRebalance() { |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 226 | // Partitions are already perfectly balanced among the two active instances |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 227 | setUpLeadershipService(WorkPartitionManager.NUM_PARTITIONS / 2); |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 228 | replay(leadershipService); |
| 229 | |
| 230 | partitionManager.activate(); |
| 231 | |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 232 | // trigger rebalance |
| 233 | partitionManager.doRebalance(); |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 234 | |
| 235 | verify(leadershipService); |
| 236 | |
| 237 | reset(leadershipService); |
| 238 | // We have a smaller share than we should |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 239 | setUpLeadershipService(WorkPartitionManager.NUM_PARTITIONS / 2 - 1); |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 240 | replay(leadershipService); |
| 241 | |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 242 | // trigger rebalance |
| 243 | partitionManager.doRebalance(); |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 244 | |
| 245 | verify(leadershipService); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * LeadershipService that allows us to grab a reference to |
| 250 | * PartitionManager's LeadershipEventListener. |
| 251 | */ |
| 252 | public class TestLeadershipService extends LeadershipServiceAdapter { |
| 253 | @Override |
| 254 | public void addListener(LeadershipEventListener listener) { |
| 255 | leaderListener = listener; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * ClusterService set up with a very simple cluster - 3 nodes, one is the |
| 261 | * current node, one is a different active node, and one is an inactive node. |
| 262 | */ |
| 263 | private class TestClusterService extends ClusterServiceAdapter { |
| 264 | |
| 265 | private final ControllerNode self = |
| 266 | new DefaultControllerNode(MY_NODE_ID, IpAddress.valueOf(1)); |
| 267 | private final ControllerNode otherNode = |
| 268 | new DefaultControllerNode(OTHER_NODE_ID, IpAddress.valueOf(2)); |
| 269 | private final ControllerNode inactiveNode = |
| 270 | new DefaultControllerNode(INACTIVE_NODE_ID, IpAddress.valueOf(3)); |
| 271 | |
| 272 | Set<ControllerNode> nodes; |
| 273 | |
| 274 | public TestClusterService() { |
| 275 | nodes = new HashSet<>(); |
| 276 | nodes.add(self); |
| 277 | nodes.add(otherNode); |
| 278 | nodes.add(inactiveNode); |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public ControllerNode getLocalNode() { |
| 283 | return self; |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public Set<ControllerNode> getNodes() { |
| 288 | return nodes; |
| 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public ControllerNode getNode(NodeId nodeId) { |
| 293 | return nodes.stream() |
| 294 | .filter(c -> c.id().equals(nodeId)) |
| 295 | .findFirst() |
| 296 | .get(); |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public ControllerNode.State getState(NodeId nodeId) { |
| 301 | return nodeId.equals(INACTIVE_NODE_ID) ? ControllerNode.State.INACTIVE : |
| 302 | ControllerNode.State.ACTIVE; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * A key that always hashes to a value provided to the constructor. This |
| 308 | * allows us to control the hash of the key for unit tests. |
| 309 | */ |
| 310 | private class ControllableHashKey extends Key { |
| 311 | |
| 312 | protected ControllableHashKey(long hash) { |
| 313 | super(hash); |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public int hashCode() { |
| 318 | return Objects.hash(hash()); |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public boolean equals(Object obj) { |
| 323 | if (!(obj instanceof ControllableHashKey)) { |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | ControllableHashKey that = (ControllableHashKey) obj; |
| 328 | |
| 329 | return Objects.equals(this.hash(), that.hash()); |
| 330 | } |
Simon Hunt | e2b6a2b | 2015-10-27 11:38:51 -0700 | [diff] [blame] | 331 | |
| 332 | @Override |
| 333 | public int compareTo(Key o) { |
| 334 | Long thisHash = hash(); |
| 335 | return thisHash.compareTo(o.hash()); |
| 336 | } |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 337 | } |
| 338 | } |