Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -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 | */ |
| 16 | package org.onosproject.store.group.impl; |
| 17 | |
Sivachidambaram Subramanian | 9f816de | 2017-06-13 07:16:54 +0530 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.testing.EqualsTester; |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.junit.TestUtils; |
pier | 7aceddf | 2019-12-19 16:04:23 +0100 | [diff] [blame^] | 25 | import org.onlab.packet.Ip4Address; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 26 | import org.onosproject.cfg.ComponentConfigAdapter; |
pier | 7aceddf | 2019-12-19 16:04:23 +0100 | [diff] [blame^] | 27 | import org.onosproject.cluster.ClusterService; |
| 28 | import org.onosproject.cluster.ControllerNode; |
Charles Chan | f4838a7 | 2015-12-07 18:13:45 -0800 | [diff] [blame] | 29 | import org.onosproject.cluster.NodeId; |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 30 | import org.onosproject.core.GroupId; |
| 31 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.MastershipRole; |
| 34 | import org.onosproject.net.PortNumber; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onosproject.net.flow.TrafficTreatment; |
| 37 | import org.onosproject.net.group.DefaultGroup; |
| 38 | import org.onosproject.net.group.DefaultGroupBucket; |
| 39 | import org.onosproject.net.group.DefaultGroupDescription; |
| 40 | import org.onosproject.net.group.DefaultGroupKey; |
| 41 | import org.onosproject.net.group.Group; |
Saurav Das | f1027d4 | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 42 | import org.onosproject.net.group.Group.GroupState; |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 43 | import org.onosproject.net.group.GroupBucket; |
| 44 | import org.onosproject.net.group.GroupBuckets; |
| 45 | import org.onosproject.net.group.GroupDescription; |
| 46 | import org.onosproject.net.group.GroupEvent; |
| 47 | import org.onosproject.net.group.GroupKey; |
| 48 | import org.onosproject.net.group.GroupOperation; |
| 49 | import org.onosproject.net.group.GroupStore; |
| 50 | import org.onosproject.net.group.GroupStoreDelegate; |
Saurav Das | f1027d4 | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 51 | import org.onosproject.net.group.GroupOperation.GroupMsgErrorCode; |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 52 | import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter; |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 53 | import org.onosproject.store.service.ConsistentMap; |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 54 | import org.onosproject.store.service.TestStorageService; |
| 55 | |
Sivachidambaram Subramanian | 9f816de | 2017-06-13 07:16:54 +0530 | [diff] [blame] | 56 | import java.util.ArrayList; |
| 57 | import java.util.LinkedList; |
| 58 | import java.util.List; |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 59 | |
pier | 7aceddf | 2019-12-19 16:04:23 +0100 | [diff] [blame^] | 60 | import static org.easymock.EasyMock.*; |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 61 | import static org.hamcrest.MatcherAssert.assertThat; |
| 62 | import static org.hamcrest.Matchers.hasSize; |
| 63 | import static org.hamcrest.Matchers.instanceOf; |
| 64 | import static org.hamcrest.Matchers.is; |
| 65 | import static org.hamcrest.Matchers.notNullValue; |
| 66 | import static org.hamcrest.Matchers.nullValue; |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame] | 67 | import static org.junit.Assert.assertEquals; |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 68 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 69 | import static org.onosproject.net.NetTestTools.did; |
Sivachidambaram Subramanian | 9f816de | 2017-06-13 07:16:54 +0530 | [diff] [blame] | 70 | import static org.onosproject.net.group.GroupDescription.Type.ALL; |
| 71 | import static org.onosproject.net.group.GroupDescription.Type.INDIRECT; |
| 72 | import static org.onosproject.net.group.GroupDescription.Type.SELECT; |
| 73 | import static org.onosproject.net.group.GroupStore.UpdateType.ADD; |
| 74 | import static org.onosproject.net.group.GroupStore.UpdateType.SET; |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Distributed group store test. |
| 77 | */ |
| 78 | public class DistributedGroupStoreTest { |
| 79 | |
| 80 | DeviceId deviceId1 = did("dev1"); |
| 81 | DeviceId deviceId2 = did("dev2"); |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 82 | GroupId groupId1 = new GroupId(1); |
| 83 | GroupId groupId2 = new GroupId(2); |
| 84 | GroupId groupId3 = new GroupId(3); |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 85 | GroupKey groupKey1 = new DefaultGroupKey("abc".getBytes()); |
| 86 | GroupKey groupKey2 = new DefaultGroupKey("def".getBytes()); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 87 | GroupKey groupKey3 = new DefaultGroupKey("ghi".getBytes()); |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 88 | |
| 89 | TrafficTreatment treatment = |
| 90 | DefaultTrafficTreatment.emptyTreatment(); |
| 91 | GroupBucket selectGroupBucket = |
| 92 | DefaultGroupBucket.createSelectGroupBucket(treatment); |
| 93 | GroupBucket failoverGroupBucket = |
| 94 | DefaultGroupBucket.createFailoverGroupBucket(treatment, |
| 95 | PortNumber.IN_PORT, groupId1); |
| 96 | |
| 97 | GroupBuckets buckets = new GroupBuckets(ImmutableList.of(selectGroupBucket)); |
| 98 | GroupDescription groupDescription1 = new DefaultGroupDescription( |
| 99 | deviceId1, |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 100 | ALL, |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 101 | buckets, |
| 102 | groupKey1, |
| 103 | groupId1.id(), |
| 104 | APP_ID); |
| 105 | GroupDescription groupDescription2 = new DefaultGroupDescription( |
| 106 | deviceId2, |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 107 | INDIRECT, |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 108 | buckets, |
| 109 | groupKey2, |
| 110 | groupId2.id(), |
| 111 | APP_ID); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 112 | GroupDescription groupDescription3 = new DefaultGroupDescription( |
| 113 | deviceId2, |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 114 | INDIRECT, |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 115 | buckets, |
| 116 | groupKey3, |
| 117 | groupId3.id(), |
| 118 | APP_ID); |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 119 | |
| 120 | DistributedGroupStore groupStoreImpl; |
| 121 | GroupStore groupStore; |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 122 | ConsistentMap auditPendingReqQueue; |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 123 | |
| 124 | static class MasterOfAll extends MastershipServiceAdapter { |
| 125 | @Override |
| 126 | public MastershipRole getLocalRole(DeviceId deviceId) { |
| 127 | return MastershipRole.MASTER; |
| 128 | } |
Charles Chan | f4838a7 | 2015-12-07 18:13:45 -0800 | [diff] [blame] | 129 | |
| 130 | @Override |
| 131 | public NodeId getMasterFor(DeviceId deviceId) { |
pier | 7aceddf | 2019-12-19 16:04:23 +0100 | [diff] [blame^] | 132 | return new NodeId(NODE_ID); |
Charles Chan | f4838a7 | 2015-12-07 18:13:45 -0800 | [diff] [blame] | 133 | } |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Sivachidambaram Subramanian | 9f816de | 2017-06-13 07:16:54 +0530 | [diff] [blame] | 136 | static class MasterNull extends MastershipServiceAdapter { |
| 137 | @Override |
| 138 | public MastershipRole getLocalRole(DeviceId deviceId) { |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public NodeId getMasterFor(DeviceId deviceId) { |
| 144 | return null; |
| 145 | } |
| 146 | } |
| 147 | |
pier | 7aceddf | 2019-12-19 16:04:23 +0100 | [diff] [blame^] | 148 | private static class MockControllerNode implements ControllerNode { |
| 149 | final NodeId id; |
| 150 | |
| 151 | public MockControllerNode(NodeId id) { |
| 152 | this.id = id; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public NodeId id() { |
| 157 | return this.id; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Ip4Address ip() { |
| 162 | return Ip4Address.valueOf("127.0.0.1"); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public int tcpPort() { |
| 167 | return 0; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | private static final String NODE_ID = "foo"; |
| 172 | |
| 173 | |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 174 | @Before |
| 175 | public void setUp() throws Exception { |
| 176 | groupStoreImpl = new DistributedGroupStore(); |
| 177 | groupStoreImpl.storageService = new TestStorageService(); |
| 178 | groupStoreImpl.clusterCommunicator = new ClusterCommunicationServiceAdapter(); |
| 179 | groupStoreImpl.mastershipService = new MasterOfAll(); |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 180 | groupStoreImpl.cfgService = new ComponentConfigAdapter(); |
pier | 7aceddf | 2019-12-19 16:04:23 +0100 | [diff] [blame^] | 181 | |
| 182 | ClusterService mockClusterService = createMock(ClusterService.class); |
| 183 | NodeId nodeId = new NodeId(NODE_ID); |
| 184 | MockControllerNode mockControllerNode = new MockControllerNode(nodeId); |
| 185 | expect(mockClusterService.getLocalNode()) |
| 186 | .andReturn(mockControllerNode).anyTimes(); |
| 187 | replay(mockClusterService); |
| 188 | |
| 189 | groupStoreImpl.clusterService = mockClusterService; |
sisubram | 4beea65 | 2017-08-09 10:38:14 +0000 | [diff] [blame] | 190 | groupStoreImpl.activate(null); |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 191 | groupStore = groupStoreImpl; |
| 192 | auditPendingReqQueue = |
| 193 | TestUtils.getField(groupStoreImpl, "auditPendingReqQueue"); |
| 194 | } |
| 195 | |
| 196 | @After |
| 197 | public void tearDown() throws Exception { |
| 198 | groupStoreImpl.deactivate(); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Tests the initial state of the store. |
| 203 | */ |
| 204 | @Test |
| 205 | public void testEmptyStore() { |
| 206 | assertThat(groupStore.getGroupCount(deviceId1), is(0)); |
| 207 | assertThat(groupStore.getGroup(deviceId1, groupId1), nullValue()); |
| 208 | assertThat(groupStore.getGroup(deviceId1, groupKey1), nullValue()); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Tests adding a pending group. |
| 213 | */ |
| 214 | @Test |
| 215 | public void testAddPendingGroup() throws Exception { |
| 216 | // Make sure the pending list starts out empty |
| 217 | assertThat(auditPendingReqQueue.size(), is(0)); |
| 218 | |
| 219 | // Add a new pending group. Make sure that the store remains empty |
| 220 | groupStore.storeGroupDescription(groupDescription1); |
| 221 | assertThat(groupStore.getGroupCount(deviceId1), is(0)); |
| 222 | assertThat(groupStore.getGroup(deviceId1, groupId1), nullValue()); |
| 223 | assertThat(groupStore.getGroup(deviceId1, groupKey1), nullValue()); |
| 224 | |
| 225 | // Make sure the group is pending |
| 226 | assertThat(auditPendingReqQueue.size(), is(1)); |
| 227 | |
| 228 | groupStore.deviceInitialAuditCompleted(deviceId1, true); |
| 229 | |
| 230 | // Make sure the group isn't pending anymore |
| 231 | assertThat(auditPendingReqQueue.size(), is(0)); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * Tests adding and removing a group. |
| 237 | */ |
| 238 | @Test |
| 239 | public void testAddRemoveGroup() throws Exception { |
| 240 | groupStore.deviceInitialAuditCompleted(deviceId1, true); |
| 241 | assertThat(groupStore.deviceInitialAuditStatus(deviceId1), is(true)); |
| 242 | |
| 243 | // Make sure the pending list starts out empty |
| 244 | assertThat(auditPendingReqQueue.size(), is(0)); |
| 245 | |
| 246 | groupStore.storeGroupDescription(groupDescription1); |
| 247 | assertThat(groupStore.getGroupCount(deviceId1), is(1)); |
| 248 | assertThat(groupStore.getGroup(deviceId1, groupId1), notNullValue()); |
| 249 | assertThat(groupStore.getGroup(deviceId1, groupKey1), notNullValue()); |
| 250 | |
| 251 | // Make sure that nothing is pending |
| 252 | assertThat(auditPendingReqQueue.size(), is(0)); |
| 253 | |
| 254 | Group groupById = groupStore.getGroup(deviceId1, groupId1); |
| 255 | Group groupByKey = groupStore.getGroup(deviceId1, groupKey1); |
| 256 | assertThat(groupById, notNullValue()); |
| 257 | assertThat(groupByKey, notNullValue()); |
| 258 | assertThat(groupById, is(groupByKey)); |
| 259 | assertThat(groupById.deviceId(), is(did("dev1"))); |
| 260 | |
| 261 | groupStore.removeGroupEntry(groupById); |
| 262 | |
| 263 | assertThat(groupStore.getGroupCount(deviceId1), is(0)); |
| 264 | assertThat(groupStore.getGroup(deviceId1, groupId1), nullValue()); |
| 265 | assertThat(groupStore.getGroup(deviceId1, groupKey1), nullValue()); |
| 266 | |
| 267 | // Make sure that nothing is pending |
| 268 | assertThat(auditPendingReqQueue.size(), is(0)); |
| 269 | } |
| 270 | |
| 271 | /** |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 272 | * Tests removing all groups on the given device. |
| 273 | */ |
| 274 | @Test |
| 275 | public void testRemoveGroupOnDevice() throws Exception { |
| 276 | groupStore.deviceInitialAuditCompleted(deviceId1, true); |
| 277 | assertThat(groupStore.deviceInitialAuditStatus(deviceId1), is(true)); |
| 278 | groupStore.deviceInitialAuditCompleted(deviceId2, true); |
| 279 | assertThat(groupStore.deviceInitialAuditStatus(deviceId2), is(true)); |
| 280 | |
| 281 | // Make sure the pending list starts out empty |
| 282 | assertThat(auditPendingReqQueue.size(), is(0)); |
| 283 | |
| 284 | groupStore.storeGroupDescription(groupDescription1); |
| 285 | groupStore.storeGroupDescription(groupDescription2); |
| 286 | groupStore.storeGroupDescription(groupDescription3); |
| 287 | assertThat(groupStore.getGroupCount(deviceId1), is(1)); |
| 288 | assertThat(groupStore.getGroupCount(deviceId2), is(2)); |
| 289 | |
| 290 | groupStore.purgeGroupEntry(deviceId2); |
| 291 | assertThat(groupStore.getGroupCount(deviceId1), is(1)); |
| 292 | assertThat(groupStore.getGroupCount(deviceId2), is(0)); |
Victor Silva | 4e8b783 | 2016-08-17 17:11:19 -0300 | [diff] [blame] | 293 | |
| 294 | groupStore.purgeGroupEntries(); |
| 295 | assertThat(groupStore.getGroupCount(deviceId1), is(0)); |
| 296 | assertThat(groupStore.getGroupCount(deviceId2), is(0)); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 300 | * Tests adding and removing a group. |
| 301 | */ |
| 302 | @Test |
| 303 | public void testRemoveGroupDescription() throws Exception { |
| 304 | groupStore.deviceInitialAuditCompleted(deviceId1, true); |
| 305 | |
| 306 | groupStore.storeGroupDescription(groupDescription1); |
| 307 | |
| 308 | groupStore.deleteGroupDescription(deviceId1, groupKey1); |
| 309 | |
| 310 | // Group should still be there, marked for removal |
| 311 | assertThat(groupStore.getGroupCount(deviceId1), is(1)); |
| 312 | Group queriedGroup = groupStore.getGroup(deviceId1, groupId1); |
| 313 | assertThat(queriedGroup.state(), is(Group.GroupState.PENDING_DELETE)); |
| 314 | |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Tests pushing group metrics. |
| 319 | */ |
| 320 | @Test |
| 321 | public void testPushGroupMetrics() { |
| 322 | groupStore.deviceInitialAuditCompleted(deviceId1, true); |
| 323 | groupStore.deviceInitialAuditCompleted(deviceId2, true); |
| 324 | |
| 325 | GroupDescription groupDescription3 = new DefaultGroupDescription( |
| 326 | deviceId1, |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 327 | SELECT, |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 328 | buckets, |
| 329 | new DefaultGroupKey("aaa".getBytes()), |
| 330 | null, |
| 331 | APP_ID); |
| 332 | |
| 333 | groupStore.storeGroupDescription(groupDescription1); |
| 334 | groupStore.storeGroupDescription(groupDescription2); |
| 335 | groupStore.storeGroupDescription(groupDescription3); |
| 336 | Group group1 = groupStore.getGroup(deviceId1, groupId1); |
| 337 | |
| 338 | assertThat(group1, instanceOf(DefaultGroup.class)); |
| 339 | DefaultGroup defaultGroup1 = (DefaultGroup) group1; |
| 340 | defaultGroup1.setPackets(55L); |
| 341 | defaultGroup1.setBytes(66L); |
| 342 | groupStore.pushGroupMetrics(deviceId1, ImmutableList.of(group1)); |
| 343 | |
| 344 | // Make sure the group was updated. |
| 345 | |
| 346 | Group requeryGroup1 = groupStore.getGroup(deviceId1, groupId1); |
| 347 | assertThat(requeryGroup1.packets(), is(55L)); |
| 348 | assertThat(requeryGroup1.bytes(), is(66L)); |
| 349 | |
| 350 | } |
| 351 | |
| 352 | class TestDelegate implements GroupStoreDelegate { |
| 353 | private List<GroupEvent> eventsSeen = new LinkedList<>(); |
| 354 | @Override |
| 355 | public void notify(GroupEvent event) { |
| 356 | eventsSeen.add(event); |
| 357 | } |
| 358 | |
| 359 | public List<GroupEvent> eventsSeen() { |
| 360 | return eventsSeen; |
| 361 | } |
| 362 | |
| 363 | public void resetEvents() { |
| 364 | eventsSeen.clear(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Tests group operation failed interface. |
| 370 | */ |
| 371 | @Test |
| 372 | public void testGroupOperationFailed() { |
| 373 | TestDelegate delegate = new TestDelegate(); |
| 374 | groupStore.setDelegate(delegate); |
| 375 | groupStore.deviceInitialAuditCompleted(deviceId1, true); |
| 376 | groupStore.deviceInitialAuditCompleted(deviceId2, true); |
| 377 | |
| 378 | groupStore.storeGroupDescription(groupDescription1); |
| 379 | groupStore.storeGroupDescription(groupDescription2); |
| 380 | |
| 381 | List<GroupEvent> eventsAfterAdds = delegate.eventsSeen(); |
| 382 | assertThat(eventsAfterAdds, hasSize(2)); |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 383 | eventsAfterAdds.forEach(event -> assertThat(event.type(), is(GroupEvent.Type.GROUP_ADD_REQUESTED))); |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 384 | delegate.resetEvents(); |
| 385 | |
| 386 | GroupOperation opAdd = |
| 387 | GroupOperation.createAddGroupOperation(groupId1, |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 388 | INDIRECT, |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 389 | buckets); |
| 390 | groupStore.groupOperationFailed(deviceId1, opAdd); |
| 391 | |
| 392 | List<GroupEvent> eventsAfterAddFailed = delegate.eventsSeen(); |
| 393 | assertThat(eventsAfterAddFailed, hasSize(2)); |
| 394 | assertThat(eventsAfterAddFailed.get(0).type(), |
| 395 | is(GroupEvent.Type.GROUP_ADD_FAILED)); |
| 396 | assertThat(eventsAfterAddFailed.get(1).type(), |
| 397 | is(GroupEvent.Type.GROUP_REMOVED)); |
| 398 | delegate.resetEvents(); |
| 399 | |
| 400 | GroupOperation opModify = |
| 401 | GroupOperation.createModifyGroupOperation(groupId2, |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 402 | INDIRECT, |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 403 | buckets); |
| 404 | groupStore.groupOperationFailed(deviceId2, opModify); |
| 405 | List<GroupEvent> eventsAfterModifyFailed = delegate.eventsSeen(); |
| 406 | assertThat(eventsAfterModifyFailed, hasSize(1)); |
| 407 | assertThat(eventsAfterModifyFailed.get(0).type(), |
| 408 | is(GroupEvent.Type.GROUP_UPDATE_FAILED)); |
| 409 | delegate.resetEvents(); |
| 410 | |
| 411 | GroupOperation opDelete = |
| 412 | GroupOperation.createDeleteGroupOperation(groupId2, |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 413 | INDIRECT); |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 414 | groupStore.groupOperationFailed(deviceId2, opDelete); |
| 415 | List<GroupEvent> eventsAfterDeleteFailed = delegate.eventsSeen(); |
| 416 | assertThat(eventsAfterDeleteFailed, hasSize(1)); |
| 417 | assertThat(eventsAfterDeleteFailed.get(0).type(), |
| 418 | is(GroupEvent.Type.GROUP_REMOVE_FAILED)); |
| 419 | delegate.resetEvents(); |
| 420 | } |
| 421 | |
| 422 | /** |
Saurav Das | f1027d4 | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 423 | * Tests group operation failed interface, with error codes for failures. |
| 424 | */ |
| 425 | @Test |
| 426 | public void testGroupOperationFailedWithErrorCode() { |
| 427 | TestDelegate delegate = new TestDelegate(); |
| 428 | groupStore.setDelegate(delegate); |
| 429 | groupStore.deviceInitialAuditCompleted(deviceId1, true); |
| 430 | groupStore.storeGroupDescription(groupDescription1); |
| 431 | groupStore.deviceInitialAuditCompleted(deviceId2, true); |
| 432 | groupStore.storeGroupDescription(groupDescription2); |
| 433 | |
| 434 | List<GroupEvent> eventsAfterAdds = delegate.eventsSeen(); |
| 435 | assertThat(eventsAfterAdds, hasSize(2)); |
| 436 | eventsAfterAdds.forEach(event -> assertThat(event |
| 437 | .type(), is(GroupEvent.Type.GROUP_ADD_REQUESTED))); |
| 438 | delegate.resetEvents(); |
| 439 | |
| 440 | // test group exists |
| 441 | GroupOperation opAdd = GroupOperation |
| 442 | .createAddGroupOperation(groupId1, INDIRECT, buckets); |
| 443 | GroupOperation addFailedExists = GroupOperation |
| 444 | .createFailedGroupOperation(opAdd, GroupMsgErrorCode.GROUP_EXISTS); |
| 445 | groupStore.groupOperationFailed(deviceId1, addFailedExists); |
| 446 | |
| 447 | List<GroupEvent> eventsAfterAddFailed = delegate.eventsSeen(); |
| 448 | assertThat(eventsAfterAddFailed, hasSize(2)); |
| 449 | assertThat(eventsAfterAddFailed.get(0).type(), |
| 450 | is(GroupEvent.Type.GROUP_ADDED)); |
| 451 | assertThat(eventsAfterAddFailed.get(1).type(), |
| 452 | is(GroupEvent.Type.GROUP_ADDED)); |
| 453 | Group g1 = groupStore.getGroup(deviceId1, groupId1); |
| 454 | assertEquals(0, g1.failedRetryCount()); |
| 455 | delegate.resetEvents(); |
| 456 | |
| 457 | // test invalid group |
| 458 | Group g2 = groupStore.getGroup(deviceId2, groupId2); |
| 459 | assertEquals(0, g2.failedRetryCount()); |
| 460 | assertEquals(GroupState.PENDING_ADD, g2.state()); |
| 461 | GroupOperation opAdd1 = GroupOperation |
| 462 | .createAddGroupOperation(groupId2, INDIRECT, buckets); |
| 463 | GroupOperation addFailedInvalid = GroupOperation |
| 464 | .createFailedGroupOperation(opAdd1, GroupMsgErrorCode.INVALID_GROUP); |
| 465 | |
| 466 | groupStore.groupOperationFailed(deviceId2, addFailedInvalid); |
| 467 | groupStore.pushGroupMetrics(deviceId2, ImmutableList.of()); |
| 468 | List<GroupEvent> eventsAfterAddFailed1 = delegate.eventsSeen(); |
| 469 | assertThat(eventsAfterAddFailed1, hasSize(1)); |
| 470 | assertThat(eventsAfterAddFailed.get(0).type(), |
| 471 | is(GroupEvent.Type.GROUP_ADD_REQUESTED)); |
| 472 | g2 = groupStore.getGroup(deviceId2, groupId2); |
| 473 | assertEquals(1, g2.failedRetryCount()); |
| 474 | assertEquals(GroupState.PENDING_ADD_RETRY, g2.state()); |
| 475 | delegate.resetEvents(); |
| 476 | |
| 477 | groupStore.groupOperationFailed(deviceId2, addFailedInvalid); |
| 478 | groupStore.pushGroupMetrics(deviceId2, ImmutableList.of()); |
| 479 | List<GroupEvent> eventsAfterAddFailed2 = delegate.eventsSeen(); |
| 480 | assertThat(eventsAfterAddFailed2, hasSize(1)); |
| 481 | assertThat(eventsAfterAddFailed.get(0).type(), |
| 482 | is(GroupEvent.Type.GROUP_ADD_REQUESTED)); |
| 483 | g2 = groupStore.getGroup(deviceId2, groupId2); |
| 484 | assertEquals(2, g2.failedRetryCount()); |
| 485 | assertEquals(GroupState.PENDING_ADD_RETRY, g2.state()); |
| 486 | delegate.resetEvents(); |
| 487 | |
| 488 | groupStore.groupOperationFailed(deviceId2, addFailedInvalid); |
| 489 | groupStore.pushGroupMetrics(deviceId2, ImmutableList.of()); |
| 490 | List<GroupEvent> eventsAfterAddFailed3 = delegate.eventsSeen(); |
| 491 | assertThat(eventsAfterAddFailed3, hasSize(2)); |
| 492 | assertThat(eventsAfterAddFailed.get(0).type(), |
| 493 | is(GroupEvent.Type.GROUP_ADD_FAILED)); |
| 494 | assertThat(eventsAfterAddFailed.get(1).type(), |
| 495 | is(GroupEvent.Type.GROUP_REMOVED)); |
| 496 | g2 = groupStore.getGroup(deviceId2, groupId2); |
| 497 | assertEquals(null, g2); |
| 498 | delegate.resetEvents(); |
| 499 | } |
| 500 | |
| 501 | /** |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 502 | * Tests extraneous group operations. |
| 503 | */ |
| 504 | @Test |
| 505 | public void testExtraneousOperations() { |
| 506 | ArrayList<Group> extraneous; |
| 507 | groupStore.deviceInitialAuditCompleted(deviceId1, true); |
| 508 | |
| 509 | groupStore.storeGroupDescription(groupDescription1); |
| 510 | Group group1 = groupStore.getGroup(deviceId1, groupId1); |
| 511 | |
| 512 | extraneous = Lists.newArrayList(groupStore.getExtraneousGroups(deviceId1)); |
| 513 | assertThat(extraneous, hasSize(0)); |
| 514 | |
| 515 | groupStore.addOrUpdateExtraneousGroupEntry(group1); |
| 516 | extraneous = Lists.newArrayList(groupStore.getExtraneousGroups(deviceId1)); |
| 517 | assertThat(extraneous, hasSize(1)); |
| 518 | |
| 519 | groupStore.removeExtraneousGroupEntry(group1); |
| 520 | extraneous = Lists.newArrayList(groupStore.getExtraneousGroups(deviceId1)); |
| 521 | assertThat(extraneous, hasSize(0)); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Tests updating of group descriptions. |
| 526 | */ |
| 527 | @Test |
| 528 | public void testUpdateGroupDescription() { |
| 529 | |
| 530 | GroupBuckets buckets = |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame] | 531 | new GroupBuckets(ImmutableList.of(failoverGroupBucket, selectGroupBucket)); |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 532 | |
| 533 | groupStore.deviceInitialAuditCompleted(deviceId1, true); |
| 534 | groupStore.storeGroupDescription(groupDescription1); |
| 535 | |
| 536 | GroupKey newKey = new DefaultGroupKey("123".getBytes()); |
| 537 | groupStore.updateGroupDescription(deviceId1, |
| 538 | groupKey1, |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 539 | ADD, |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 540 | buckets, |
| 541 | newKey); |
| 542 | Group group1 = groupStore.getGroup(deviceId1, groupId1); |
| 543 | assertThat(group1.appCookie(), is(newKey)); |
| 544 | assertThat(group1.buckets().buckets(), hasSize(2)); |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame] | 545 | |
| 546 | short weight = 5; |
| 547 | GroupBucket selectGroupBucketWithWeight = |
| 548 | DefaultGroupBucket.createSelectGroupBucket(treatment, weight); |
| 549 | buckets = new GroupBuckets(ImmutableList.of(failoverGroupBucket, |
| 550 | selectGroupBucketWithWeight)); |
| 551 | |
| 552 | groupStore.updateGroupDescription(deviceId1, |
| 553 | newKey, |
| 554 | ADD, |
| 555 | buckets, |
| 556 | newKey); |
| 557 | |
| 558 | group1 = groupStore.getGroup(deviceId1, groupId1); |
| 559 | assertThat(group1.appCookie(), is(newKey)); |
| 560 | assertThat(group1.buckets().buckets(), hasSize(2)); |
| 561 | for (GroupBucket bucket : group1.buckets().buckets()) { |
| 562 | if (bucket.type() == SELECT) { |
| 563 | assertEquals(weight, bucket.weight()); |
| 564 | } |
| 565 | } |
Victor Silva | 0282ab8 | 2016-11-15 16:30:27 -0300 | [diff] [blame] | 566 | |
| 567 | buckets = new GroupBuckets(ImmutableList.of(selectGroupBucketWithWeight)); |
| 568 | |
| 569 | groupStore.updateGroupDescription(deviceId1, |
| 570 | newKey, |
| 571 | SET, |
| 572 | buckets, |
| 573 | newKey); |
| 574 | |
| 575 | group1 = groupStore.getGroup(deviceId1, groupId1); |
| 576 | assertThat(group1.appCookie(), is(newKey)); |
| 577 | assertThat(group1.buckets().buckets(), hasSize(1)); |
| 578 | GroupBucket onlyBucket = group1.buckets().buckets().iterator().next(); |
| 579 | assertEquals(weight, onlyBucket.weight()); |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | @Test |
| 583 | public void testEqualsGroupStoreIdMapKey() { |
| 584 | DistributedGroupStore.GroupStoreIdMapKey key1 = |
| 585 | new DistributedGroupStore.GroupStoreIdMapKey(deviceId1, groupId1); |
| 586 | DistributedGroupStore.GroupStoreIdMapKey sameAsKey1 = |
| 587 | new DistributedGroupStore.GroupStoreIdMapKey(deviceId1, groupId1); |
| 588 | DistributedGroupStore.GroupStoreIdMapKey key2 = |
| 589 | new DistributedGroupStore.GroupStoreIdMapKey(deviceId2, groupId1); |
| 590 | DistributedGroupStore.GroupStoreIdMapKey key3 = |
| 591 | new DistributedGroupStore.GroupStoreIdMapKey(deviceId1, groupId2); |
| 592 | |
| 593 | new EqualsTester() |
| 594 | .addEqualityGroup(key1, sameAsKey1) |
| 595 | .addEqualityGroup(key2) |
| 596 | .addEqualityGroup(key3) |
| 597 | .testEquals(); |
| 598 | } |
| 599 | |
| 600 | @Test |
| 601 | public void testEqualsGroupStoreKeyMapKey() { |
| 602 | DistributedGroupStore.GroupStoreKeyMapKey key1 = |
| 603 | new DistributedGroupStore.GroupStoreKeyMapKey(deviceId1, groupKey1); |
| 604 | DistributedGroupStore.GroupStoreKeyMapKey sameAsKey1 = |
| 605 | new DistributedGroupStore.GroupStoreKeyMapKey(deviceId1, groupKey1); |
| 606 | DistributedGroupStore.GroupStoreKeyMapKey key2 = |
| 607 | new DistributedGroupStore.GroupStoreKeyMapKey(deviceId2, groupKey1); |
| 608 | DistributedGroupStore.GroupStoreKeyMapKey key3 = |
| 609 | new DistributedGroupStore.GroupStoreKeyMapKey(deviceId1, groupKey2); |
| 610 | |
| 611 | new EqualsTester() |
| 612 | .addEqualityGroup(key1, sameAsKey1) |
| 613 | .addEqualityGroup(key2) |
| 614 | .addEqualityGroup(key3) |
| 615 | .testEquals(); |
| 616 | } |
| 617 | |
| 618 | @Test |
| 619 | public void testEqualsGroupStoreMapKey() { |
| 620 | DistributedGroupStore.GroupStoreMapKey key1 = |
| 621 | new DistributedGroupStore.GroupStoreMapKey(deviceId1); |
| 622 | DistributedGroupStore.GroupStoreMapKey sameAsKey1 = |
| 623 | new DistributedGroupStore.GroupStoreMapKey(deviceId1); |
| 624 | DistributedGroupStore.GroupStoreMapKey key2 = |
| 625 | new DistributedGroupStore.GroupStoreMapKey(deviceId2); |
| 626 | DistributedGroupStore.GroupStoreMapKey key3 = |
| 627 | new DistributedGroupStore.GroupStoreMapKey(did("dev3")); |
| 628 | |
| 629 | new EqualsTester() |
| 630 | .addEqualityGroup(key1, sameAsKey1) |
| 631 | .addEqualityGroup(key2) |
| 632 | .addEqualityGroup(key3) |
| 633 | .testEquals(); |
| 634 | } |
Sivachidambaram Subramanian | 9f816de | 2017-06-13 07:16:54 +0530 | [diff] [blame] | 635 | |
| 636 | @Test |
| 637 | public void testMasterNull() throws Exception { |
| 638 | groupStore.deviceInitialAuditCompleted(deviceId1, true); |
| 639 | assertThat(groupStore.deviceInitialAuditStatus(deviceId1), is(true)); |
| 640 | // Make sure the pending list starts out empty |
| 641 | assertThat(auditPendingReqQueue.size(), is(0)); |
| 642 | //Simulate master null |
| 643 | groupStoreImpl.mastershipService = new MasterNull(); |
| 644 | //Add a group |
| 645 | groupStore.storeGroupDescription(groupDescription1); |
| 646 | assertThat(groupStore.getGroupCount(deviceId1), is(0)); |
| 647 | assertThat(groupStore.getGroup(deviceId1, groupId1), nullValue()); |
| 648 | assertThat(groupStore.getGroup(deviceId1, groupKey1), nullValue()); |
| 649 | //reset master |
| 650 | groupStoreImpl.mastershipService = new MasterOfAll(); |
| 651 | // Master was null when the group add attempt is made. |
| 652 | // So size of the pending list should be 1 now. |
| 653 | assertThat(auditPendingReqQueue.size(), is(1)); |
| 654 | groupStore.deviceInitialAuditCompleted(deviceId1, true); |
| 655 | //After the audit , the group should be removed from pending audit queue |
| 656 | assertThat(auditPendingReqQueue.size(), is(0)); |
| 657 | //test whether the group is added to the store |
| 658 | assertThat(groupStore.getGroupCount(deviceId1), is(1)); |
| 659 | assertThat(groupStore.getGroup(deviceId1, groupId1), notNullValue()); |
| 660 | assertThat(groupStore.getGroup(deviceId1, groupKey1), notNullValue()); |
| 661 | } |
| 662 | |
| 663 | |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 664 | } |