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