alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
alshabib | 1058080 | 2015-02-18 18:30:33 -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.group.impl; |
| 17 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 18 | import com.google.common.collect.FluentIterable; |
Charles Chan | f4838a7 | 2015-12-07 18:13:45 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Iterables; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 22 | import org.onlab.util.KryoNamespace; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 23 | import org.onosproject.cfg.ComponentConfigService; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 24 | import org.onosproject.cluster.ClusterService; |
Charles Chan | f4838a7 | 2015-12-07 18:13:45 -0800 | [diff] [blame] | 25 | import org.onosproject.cluster.NodeId; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 26 | import org.onosproject.core.GroupId; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 27 | import org.onosproject.mastership.MastershipService; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 29 | import org.onosproject.net.MastershipRole; |
jaegonkim | 9477a9d | 2018-04-01 16:36:36 +0900 | [diff] [blame] | 30 | import org.onosproject.net.driver.DriverService; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 31 | import org.onosproject.net.group.DefaultGroup; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 32 | import org.onosproject.net.group.DefaultGroupBucket; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 33 | import org.onosproject.net.group.DefaultGroupDescription; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 34 | import org.onosproject.net.group.DefaultGroupKey; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 35 | import org.onosproject.net.group.Group; |
| 36 | import org.onosproject.net.group.Group.GroupState; |
| 37 | import org.onosproject.net.group.GroupBucket; |
| 38 | import org.onosproject.net.group.GroupBuckets; |
| 39 | import org.onosproject.net.group.GroupDescription; |
| 40 | import org.onosproject.net.group.GroupEvent; |
| 41 | import org.onosproject.net.group.GroupEvent.Type; |
| 42 | import org.onosproject.net.group.GroupKey; |
| 43 | import org.onosproject.net.group.GroupOperation; |
| 44 | import org.onosproject.net.group.GroupStore; |
| 45 | import org.onosproject.net.group.GroupStoreDelegate; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 46 | import org.onosproject.net.group.StoredGroupBucketEntry; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 47 | import org.onosproject.net.group.StoredGroupEntry; |
| 48 | import org.onosproject.store.AbstractStore; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 49 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 50 | import org.onosproject.store.serializers.KryoNamespaces; |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 51 | import org.onosproject.store.service.ConsistentMap; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 52 | import org.onosproject.store.service.DistributedPrimitive.Status; |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 53 | import org.onosproject.store.service.MapEvent; |
| 54 | import org.onosproject.store.service.MapEventListener; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 55 | import org.onosproject.store.service.MultiValuedTimestamp; |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 56 | import org.onosproject.store.service.Serializer; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 57 | import org.onosproject.store.service.StorageService; |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 58 | import org.onosproject.store.service.Topic; |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 59 | import org.onosproject.store.service.Versioned; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 60 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 61 | import org.osgi.service.component.annotations.Activate; |
| 62 | import org.osgi.service.component.annotations.Component; |
| 63 | import org.osgi.service.component.annotations.Deactivate; |
| 64 | import org.osgi.service.component.annotations.Modified; |
| 65 | import org.osgi.service.component.annotations.Reference; |
| 66 | import org.osgi.service.component.annotations.ReferenceCardinality; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 67 | import org.slf4j.Logger; |
| 68 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 69 | import java.util.ArrayList; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 70 | import java.util.Collection; |
Charles Chan | f4838a7 | 2015-12-07 18:13:45 -0800 | [diff] [blame] | 71 | import java.util.Collections; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 72 | import java.util.Dictionary; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 73 | import java.util.HashMap; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 74 | import java.util.HashSet; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 75 | import java.util.Iterator; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 76 | import java.util.List; |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 77 | import java.util.Map; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 78 | import java.util.Map.Entry; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 79 | import java.util.Objects; |
Sho SHIMIZU | 30d639b | 2015-05-05 09:30:35 -0700 | [diff] [blame] | 80 | import java.util.Optional; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 81 | import java.util.Properties; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 82 | import java.util.Set; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 83 | import java.util.concurrent.ConcurrentHashMap; |
| 84 | import java.util.concurrent.ConcurrentMap; |
| 85 | import java.util.concurrent.ExecutorService; |
| 86 | import java.util.concurrent.Executors; |
Kavitha Alagesan | c884c3ef | 2017-01-19 12:32:26 +0530 | [diff] [blame] | 87 | import java.util.concurrent.ScheduledExecutorService; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 88 | import java.util.concurrent.atomic.AtomicInteger; |
Kavitha Alagesan | c884c3ef | 2017-01-19 12:32:26 +0530 | [diff] [blame] | 89 | import java.util.function.Consumer; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 90 | import java.util.stream.Collectors; |
| 91 | |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 92 | import static com.google.common.base.Strings.isNullOrEmpty; |
Kavitha Alagesan | c884c3ef | 2017-01-19 12:32:26 +0530 | [diff] [blame] | 93 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 94 | import static org.onlab.util.Tools.get; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 95 | import static org.onlab.util.Tools.groupedThreads; |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 96 | import static org.onosproject.store.OsgiPropertyConstants.ALLOW_EXTRANEOUS_GROUPS; |
| 97 | import static org.onosproject.store.OsgiPropertyConstants.ALLOW_EXTRANEOUS_GROUPS_DEFAULT; |
| 98 | import static org.onosproject.store.OsgiPropertyConstants.GARBAGE_COLLECT; |
| 99 | import static org.onosproject.store.OsgiPropertyConstants.GARBAGE_COLLECT_DEFAULT; |
| 100 | import static org.onosproject.store.OsgiPropertyConstants.GARBAGE_COLLECT_THRESH; |
| 101 | import static org.onosproject.store.OsgiPropertyConstants.GARBAGE_COLLECT_THRESH_DEFAULT; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 102 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 103 | |
| 104 | /** |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 105 | * Manages inventory of group entries using distributed group stores from the |
| 106 | * storage service. |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 107 | */ |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 108 | @Component( |
| 109 | immediate = true, |
| 110 | service = GroupStore.class, |
| 111 | property = { |
Ray Milkey | 2d7bca1 | 2018-10-17 14:51:52 -0700 | [diff] [blame] | 112 | GARBAGE_COLLECT + ":Boolean=" + GARBAGE_COLLECT_DEFAULT, |
| 113 | GARBAGE_COLLECT_THRESH + ":Integer=" + GARBAGE_COLLECT_THRESH_DEFAULT, |
| 114 | ALLOW_EXTRANEOUS_GROUPS + ":Boolean=" + ALLOW_EXTRANEOUS_GROUPS_DEFAULT |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 115 | } |
| 116 | ) |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 117 | public class DistributedGroupStore |
| 118 | extends AbstractStore<GroupEvent, GroupStoreDelegate> |
| 119 | implements GroupStore { |
| 120 | |
| 121 | private final Logger log = getLogger(getClass()); |
| 122 | |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 123 | private static final int MAX_FAILED_ATTEMPTS = 3; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 124 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 125 | private final int dummyId = 0xffffffff; |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 126 | private final GroupId dummyGroupId = new GroupId(dummyId); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 127 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 129 | protected ClusterCommunicationService clusterCommunicator; |
| 130 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 132 | protected ClusterService clusterService; |
| 133 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 135 | protected StorageService storageService; |
| 136 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 137 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 138 | protected MastershipService mastershipService; |
| 139 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 141 | protected ComponentConfigService cfgService; |
| 142 | |
jaegonkim | 9477a9d | 2018-04-01 16:36:36 +0900 | [diff] [blame] | 143 | // Guarantees enabling DriverService before enabling GroupStore |
| 144 | // (DriverService is used in serializing/de-serializing DefaultGroup) |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 145 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 9477a9d | 2018-04-01 16:36:36 +0900 | [diff] [blame] | 146 | protected DriverService driverService; |
| 147 | |
Kavitha Alagesan | c884c3ef | 2017-01-19 12:32:26 +0530 | [diff] [blame] | 148 | private ScheduledExecutorService executor; |
| 149 | private Consumer<Status> statusChangeListener; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 150 | // Per device group table with (device id + app cookie) as key |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 151 | private ConsistentMap<GroupStoreKeyMapKey, |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 152 | StoredGroupEntry> groupStoreEntriesByKey = null; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 153 | // Per device group table with (device id + group id) as key |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 154 | private final ConcurrentMap<DeviceId, ConcurrentMap<GroupId, StoredGroupEntry>> |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 155 | groupEntriesById = new ConcurrentHashMap<>(); |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 156 | private ConsistentMap<GroupStoreKeyMapKey, |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 157 | StoredGroupEntry> auditPendingReqQueue = null; |
Frank Wang | e0eb5ce | 2016-07-01 18:21:25 +0800 | [diff] [blame] | 158 | private MapEventListener<GroupStoreKeyMapKey, StoredGroupEntry> |
| 159 | mapListener = new GroupStoreKeyMapListener(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 160 | private final ConcurrentMap<DeviceId, ConcurrentMap<GroupId, Group>> |
| 161 | extraneousGroupEntriesById = new ConcurrentHashMap<>(); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 162 | private ExecutorService messageHandlingExecutor; |
| 163 | private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 1; |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 164 | |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 165 | private final HashMap<DeviceId, Boolean> deviceAuditStatus = new HashMap<>(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 166 | |
| 167 | private final AtomicInteger groupIdGen = new AtomicInteger(); |
| 168 | |
HIGUCHI Yuta | 180d70f | 2015-10-01 16:13:56 -0700 | [diff] [blame] | 169 | private KryoNamespace clusterMsgSerializer; |
| 170 | |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 171 | private static Topic<GroupStoreMessage> groupTopic; |
| 172 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 173 | //@Property(name = "garbageCollect", boolValue = GARBAGE_COLLECT, |
| 174 | // label = "Enable group garbage collection") |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 175 | private boolean garbageCollect = GARBAGE_COLLECT_DEFAULT; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 176 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 177 | //@Property(name = "gcThresh", intValue = GC_THRESH, |
| 178 | // label = "Number of rounds for group garbage collection") |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 179 | private int gcThresh = GARBAGE_COLLECT_THRESH_DEFAULT; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 180 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 181 | //@Property(name = "allowExtraneousGroups", boolValue = ALLOW_EXTRANEOUS_GROUPS, |
| 182 | // label = "Allow groups in switches not installed by ONOS") |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 183 | private boolean allowExtraneousGroups = ALLOW_EXTRANEOUS_GROUPS_DEFAULT; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 184 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 185 | @Activate |
sisubram | 4beea65 | 2017-08-09 10:38:14 +0000 | [diff] [blame] | 186 | public void activate(ComponentContext context) { |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 187 | cfgService.registerProperties(getClass()); |
sisubram | 4beea65 | 2017-08-09 10:38:14 +0000 | [diff] [blame] | 188 | modified(context); |
HIGUCHI Yuta | 3a84b32 | 2016-05-18 13:38:07 -0700 | [diff] [blame] | 189 | KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder() |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 190 | .register(KryoNamespaces.API) |
HIGUCHI Yuta | 3a84b32 | 2016-05-18 13:38:07 -0700 | [diff] [blame] | 191 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 192 | .register(DefaultGroup.class, |
| 193 | DefaultGroupBucket.class, |
| 194 | DefaultGroupDescription.class, |
| 195 | DefaultGroupKey.class, |
| 196 | GroupDescription.Type.class, |
| 197 | Group.GroupState.class, |
| 198 | GroupBuckets.class, |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 199 | GroupStoreMessage.class, |
| 200 | GroupStoreMessage.Type.class, |
| 201 | UpdateType.class, |
| 202 | GroupStoreMessageSubjects.class, |
| 203 | MultiValuedTimestamp.class, |
| 204 | GroupStoreKeyMapKey.class, |
| 205 | GroupStoreIdMapKey.class, |
| 206 | GroupStoreMapKey.class |
| 207 | ); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 208 | |
HIGUCHI Yuta | 3a84b32 | 2016-05-18 13:38:07 -0700 | [diff] [blame] | 209 | clusterMsgSerializer = kryoBuilder.build("GroupStore"); |
| 210 | Serializer serializer = Serializer.using(clusterMsgSerializer); |
HIGUCHI Yuta | 180d70f | 2015-10-01 16:13:56 -0700 | [diff] [blame] | 211 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 212 | messageHandlingExecutor = Executors. |
| 213 | newFixedThreadPool(MESSAGE_HANDLER_THREAD_POOL_SIZE, |
| 214 | groupedThreads("onos/store/group", |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 215 | "message-handlers", |
| 216 | log)); |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 217 | |
| 218 | clusterCommunicator.addSubscriber(GroupStoreMessageSubjects.REMOTE_GROUP_OP_REQUEST, |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 219 | clusterMsgSerializer::deserialize, |
| 220 | this::process, |
| 221 | messageHandlingExecutor); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 222 | |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 223 | log.debug("Creating Consistent map onos-group-store-keymap"); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 224 | |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 225 | groupStoreEntriesByKey = storageService.<GroupStoreKeyMapKey, StoredGroupEntry>consistentMapBuilder() |
| 226 | .withName("onos-group-store-keymap") |
HIGUCHI Yuta | 3a84b32 | 2016-05-18 13:38:07 -0700 | [diff] [blame] | 227 | .withSerializer(serializer) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 228 | .build(); |
Frank Wang | e0eb5ce | 2016-07-01 18:21:25 +0800 | [diff] [blame] | 229 | groupStoreEntriesByKey.addListener(mapListener); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 230 | log.debug("Current size of groupstorekeymap:{}", |
| 231 | groupStoreEntriesByKey.size()); |
Thiago Santos | fb73c50 | 2016-08-18 18:15:13 -0300 | [diff] [blame] | 232 | synchronizeGroupStoreEntries(); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 233 | |
Kavitha Alagesan | c884c3ef | 2017-01-19 12:32:26 +0530 | [diff] [blame] | 234 | log.debug("Creating GroupStoreId Map From GroupStoreKey Map"); |
| 235 | matchGroupEntries(); |
| 236 | executor = newSingleThreadScheduledExecutor(groupedThreads("onos/group", "store", log)); |
| 237 | statusChangeListener = status -> { |
| 238 | if (status == Status.ACTIVE) { |
| 239 | executor.execute(this::matchGroupEntries); |
| 240 | } |
| 241 | }; |
| 242 | groupStoreEntriesByKey.addStatusChangeListener(statusChangeListener); |
| 243 | |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 244 | log.debug("Creating Consistent map pendinggroupkeymap"); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 245 | |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 246 | auditPendingReqQueue = storageService.<GroupStoreKeyMapKey, StoredGroupEntry>consistentMapBuilder() |
| 247 | .withName("onos-pending-group-keymap") |
HIGUCHI Yuta | 3a84b32 | 2016-05-18 13:38:07 -0700 | [diff] [blame] | 248 | .withSerializer(serializer) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 249 | .build(); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 250 | log.debug("Current size of pendinggroupkeymap:{}", |
| 251 | auditPendingReqQueue.size()); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 252 | |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 253 | groupTopic = getOrCreateGroupTopic(serializer); |
| 254 | groupTopic.subscribe(this::processGroupMessage); |
| 255 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 256 | log.info("Started"); |
| 257 | } |
| 258 | |
| 259 | @Deactivate |
| 260 | public void deactivate() { |
Frank Wang | e0eb5ce | 2016-07-01 18:21:25 +0800 | [diff] [blame] | 261 | groupStoreEntriesByKey.removeListener(mapListener); |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 262 | cfgService.unregisterProperties(getClass(), false); |
HIGUCHI Yuta | 180d70f | 2015-10-01 16:13:56 -0700 | [diff] [blame] | 263 | clusterCommunicator.removeSubscriber(GroupStoreMessageSubjects.REMOTE_GROUP_OP_REQUEST); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 264 | log.info("Stopped"); |
| 265 | } |
| 266 | |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 267 | @Modified |
| 268 | public void modified(ComponentContext context) { |
| 269 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 270 | |
| 271 | try { |
| 272 | String s = get(properties, "garbageCollect"); |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 273 | garbageCollect = isNullOrEmpty(s) ? GARBAGE_COLLECT_DEFAULT : Boolean.parseBoolean(s.trim()); |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 274 | |
| 275 | s = get(properties, "gcThresh"); |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 276 | gcThresh = isNullOrEmpty(s) ? GARBAGE_COLLECT_THRESH_DEFAULT : Integer.parseInt(s.trim()); |
Kavitha Alagesan | c56cded | 2017-01-13 10:48:18 +0530 | [diff] [blame] | 277 | |
| 278 | s = get(properties, "allowExtraneousGroups"); |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 279 | allowExtraneousGroups = isNullOrEmpty(s) ? ALLOW_EXTRANEOUS_GROUPS_DEFAULT : Boolean.parseBoolean(s.trim()); |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 280 | } catch (Exception e) { |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 281 | gcThresh = GARBAGE_COLLECT_THRESH_DEFAULT; |
| 282 | garbageCollect = GARBAGE_COLLECT_DEFAULT; |
| 283 | allowExtraneousGroups = ALLOW_EXTRANEOUS_GROUPS_DEFAULT; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 287 | private Topic<GroupStoreMessage> getOrCreateGroupTopic(Serializer serializer) { |
| 288 | if (groupTopic == null) { |
| 289 | return storageService.getTopic("group-failover-notif", serializer); |
| 290 | } else { |
| 291 | return groupTopic; |
| 292 | } |
Sho SHIMIZU | a628554 | 2017-01-12 15:08:24 -0800 | [diff] [blame] | 293 | } |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 294 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 295 | /** |
Kavitha Alagesan | c884c3ef | 2017-01-19 12:32:26 +0530 | [diff] [blame] | 296 | * Updating values of groupEntriesById. |
| 297 | */ |
| 298 | private void matchGroupEntries() { |
| 299 | for (Entry<GroupStoreKeyMapKey, StoredGroupEntry> entry : groupStoreEntriesByKey.asJavaMap().entrySet()) { |
| 300 | StoredGroupEntry group = entry.getValue(); |
| 301 | getGroupIdTable(entry.getKey().deviceId()).put(group.id(), group); |
| 302 | } |
| 303 | } |
| 304 | |
Thiago Santos | fb73c50 | 2016-08-18 18:15:13 -0300 | [diff] [blame] | 305 | |
| 306 | private void synchronizeGroupStoreEntries() { |
| 307 | Map<GroupStoreKeyMapKey, StoredGroupEntry> groupEntryMap = groupStoreEntriesByKey.asJavaMap(); |
| 308 | for (Entry<GroupStoreKeyMapKey, StoredGroupEntry> entry : groupEntryMap.entrySet()) { |
Thiago Santos | fb73c50 | 2016-08-18 18:15:13 -0300 | [diff] [blame] | 309 | StoredGroupEntry value = entry.getValue(); |
Thiago Santos | fb73c50 | 2016-08-18 18:15:13 -0300 | [diff] [blame] | 310 | ConcurrentMap<GroupId, StoredGroupEntry> groupIdTable = getGroupIdTable(value.deviceId()); |
| 311 | groupIdTable.put(value.id(), value); |
| 312 | } |
| 313 | } |
| 314 | |
Kavitha Alagesan | c884c3ef | 2017-01-19 12:32:26 +0530 | [diff] [blame] | 315 | /** |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 316 | * Returns the group store eventual consistent key map. |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 317 | * |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 318 | * @return Map representing group key table. |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 319 | */ |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 320 | private Map<GroupStoreKeyMapKey, StoredGroupEntry> |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 321 | getGroupStoreKeyMap() { |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 322 | return groupStoreEntriesByKey.asJavaMap(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /** |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 326 | * Returns the group id table for specified device. |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 327 | * |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 328 | * @param deviceId identifier of the device |
| 329 | * @return Map representing group key table of given device. |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 330 | */ |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 331 | private ConcurrentMap<GroupId, StoredGroupEntry> getGroupIdTable(DeviceId deviceId) { |
Yuta HIGUCHI | c2e6815 | 2016-08-16 13:47:36 -0700 | [diff] [blame] | 332 | return groupEntriesById.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /** |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 336 | * Returns the pending group request table. |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 337 | * |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 338 | * @return Map representing group key table. |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 339 | */ |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 340 | private Map<GroupStoreKeyMapKey, StoredGroupEntry> |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 341 | getPendingGroupKeyTable() { |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 342 | return auditPendingReqQueue.asJavaMap(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Returns the extraneous group id table for specified device. |
| 347 | * |
| 348 | * @param deviceId identifier of the device |
| 349 | * @return Map representing group key table of given device. |
| 350 | */ |
| 351 | private ConcurrentMap<GroupId, Group> |
| 352 | getExtraneousGroupIdTable(DeviceId deviceId) { |
Yuta HIGUCHI | c2e6815 | 2016-08-16 13:47:36 -0700 | [diff] [blame] | 353 | return extraneousGroupEntriesById.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Returns the number of groups for the specified device in the store. |
| 358 | * |
| 359 | * @return number of groups for the specified device |
| 360 | */ |
| 361 | @Override |
| 362 | public int getGroupCount(DeviceId deviceId) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 363 | return (getGroups(deviceId) != null) ? |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 364 | Iterables.size(getGroups(deviceId)) : 0; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Returns the groups associated with a device. |
| 369 | * |
| 370 | * @param deviceId the device ID |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 371 | * @return the group entries |
| 372 | */ |
| 373 | @Override |
| 374 | public Iterable<Group> getGroups(DeviceId deviceId) { |
Charles Chan | f4838a7 | 2015-12-07 18:13:45 -0800 | [diff] [blame] | 375 | // Let ImmutableSet.copyOf do the type conversion |
| 376 | return ImmutableSet.copyOf(getStoredGroups(deviceId)); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 379 | private Iterable<StoredGroupEntry> getStoredGroups(DeviceId deviceId) { |
Charles Chan | f4838a7 | 2015-12-07 18:13:45 -0800 | [diff] [blame] | 380 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 381 | if (master == null) { |
| 382 | log.debug("Failed to getGroups: No master for {}", deviceId); |
| 383 | return Collections.emptySet(); |
| 384 | } |
| 385 | |
| 386 | Set<StoredGroupEntry> storedGroups = getGroupStoreKeyMap().values() |
| 387 | .stream() |
| 388 | .filter(input -> input.deviceId().equals(deviceId)) |
| 389 | .collect(Collectors.toSet()); |
| 390 | return ImmutableSet.copyOf(storedGroups); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 391 | } |
| 392 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 393 | /** |
| 394 | * Returns the stored group entry. |
| 395 | * |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 396 | * @param deviceId the device ID |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 397 | * @param appCookie the group key |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 398 | * @return a group associated with the key |
| 399 | */ |
| 400 | @Override |
| 401 | public Group getGroup(DeviceId deviceId, GroupKey appCookie) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 402 | return getStoredGroupEntry(deviceId, appCookie); |
| 403 | } |
| 404 | |
| 405 | private StoredGroupEntry getStoredGroupEntry(DeviceId deviceId, |
| 406 | GroupKey appCookie) { |
| 407 | return getGroupStoreKeyMap().get(new GroupStoreKeyMapKey(deviceId, |
| 408 | appCookie)); |
| 409 | } |
| 410 | |
| 411 | @Override |
| 412 | public Group getGroup(DeviceId deviceId, GroupId groupId) { |
| 413 | return getStoredGroupEntry(deviceId, groupId); |
| 414 | } |
| 415 | |
| 416 | private StoredGroupEntry getStoredGroupEntry(DeviceId deviceId, |
| 417 | GroupId groupId) { |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 418 | return getGroupIdTable(deviceId).get(groupId); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | private int getFreeGroupIdValue(DeviceId deviceId) { |
| 422 | int freeId = groupIdGen.incrementAndGet(); |
| 423 | |
| 424 | while (true) { |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 425 | Group existing = getGroup(deviceId, new GroupId(freeId)); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 426 | if (existing == null) { |
| 427 | existing = ( |
| 428 | extraneousGroupEntriesById.get(deviceId) != null) ? |
| 429 | extraneousGroupEntriesById.get(deviceId). |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 430 | get(new GroupId(freeId)) : |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 431 | null; |
| 432 | } |
| 433 | if (existing != null) { |
| 434 | freeId = groupIdGen.incrementAndGet(); |
| 435 | } else { |
| 436 | break; |
| 437 | } |
| 438 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 439 | log.debug("getFreeGroupIdValue: Next Free ID is {}", freeId); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 440 | return freeId; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Stores a new group entry using the information from group description. |
| 445 | * |
| 446 | * @param groupDesc group description to be used to create group entry |
| 447 | */ |
| 448 | @Override |
| 449 | public void storeGroupDescription(GroupDescription groupDesc) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 450 | log.debug("In storeGroupDescription"); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 451 | // Check if a group is existing with the same key |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 452 | Group existingGroup = getGroup(groupDesc.deviceId(), groupDesc.appCookie()); |
| 453 | if (existingGroup != null) { |
Saurav Das | c568c34 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 454 | log.debug("Group already exists with the same key {} in dev:{} with id:0x{}", |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 455 | groupDesc.appCookie(), groupDesc.deviceId(), |
| 456 | Integer.toHexString(existingGroup.id().id())); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 460 | // Check if group to be created by a remote instance |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 461 | if (mastershipService.getLocalRole(groupDesc.deviceId()) != MastershipRole.MASTER) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 462 | log.debug("storeGroupDescription: Device {} local role is not MASTER", |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 463 | groupDesc.deviceId()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 464 | if (mastershipService.getMasterFor(groupDesc.deviceId()) == null) { |
Sivachidambaram Subramanian | 9f816de | 2017-06-13 07:16:54 +0530 | [diff] [blame] | 465 | log.debug("No Master for device {}..." |
| 466 | + "Queuing Group ADD request", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 467 | groupDesc.deviceId()); |
Sivachidambaram Subramanian | 9f816de | 2017-06-13 07:16:54 +0530 | [diff] [blame] | 468 | addToPendingAudit(groupDesc); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 469 | return; |
| 470 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 471 | GroupStoreMessage groupOp = GroupStoreMessage. |
| 472 | createGroupAddRequestMsg(groupDesc.deviceId(), |
| 473 | groupDesc); |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 474 | |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 475 | clusterCommunicator.unicast(groupOp, |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 476 | GroupStoreMessageSubjects.REMOTE_GROUP_OP_REQUEST, |
| 477 | clusterMsgSerializer::serialize, |
| 478 | mastershipService.getMasterFor(groupDesc.deviceId())) |
| 479 | .whenComplete((result, error) -> { |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 480 | if (error != null) { |
| 481 | log.warn("Failed to send request to master: {} to {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 482 | groupOp, |
| 483 | mastershipService.getMasterFor(groupDesc.deviceId())); |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 484 | //TODO: Send Group operation failure event |
| 485 | } else { |
| 486 | log.debug("Sent Group operation request for device {} " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 487 | + "to remote MASTER {}", |
| 488 | groupDesc.deviceId(), |
| 489 | mastershipService.getMasterFor(groupDesc.deviceId())); |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 490 | } |
| 491 | }); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 492 | return; |
| 493 | } |
| 494 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 495 | log.debug("Store group for device {} is getting handled locally", |
| 496 | groupDesc.deviceId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 497 | storeGroupDescriptionInternal(groupDesc); |
| 498 | } |
| 499 | |
Sivachidambaram Subramanian | 9f816de | 2017-06-13 07:16:54 +0530 | [diff] [blame] | 500 | private void addToPendingAudit(GroupDescription groupDesc) { |
| 501 | Integer groupIdVal = groupDesc.givenGroupId(); |
| 502 | GroupId groupId = (groupIdVal != null) ? new GroupId(groupIdVal) : dummyGroupId; |
| 503 | addToPendingKeyTable(new DefaultGroup(groupId, groupDesc)); |
| 504 | } |
| 505 | |
| 506 | private void addToPendingKeyTable(StoredGroupEntry group) { |
| 507 | group.setState(GroupState.WAITING_AUDIT_COMPLETE); |
| 508 | Map<GroupStoreKeyMapKey, StoredGroupEntry> pendingKeyTable = |
| 509 | getPendingGroupKeyTable(); |
| 510 | pendingKeyTable.put(new GroupStoreKeyMapKey(group.deviceId(), |
| 511 | group.appCookie()), |
| 512 | group); |
| 513 | } |
| 514 | |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 515 | private Group getMatchingExtraneousGroupbyId(DeviceId deviceId, Integer groupId) { |
| 516 | ConcurrentMap<GroupId, Group> extraneousMap = |
| 517 | extraneousGroupEntriesById.get(deviceId); |
| 518 | if (extraneousMap == null) { |
| 519 | return null; |
| 520 | } |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 521 | return extraneousMap.get(new GroupId(groupId)); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | private Group getMatchingExtraneousGroupbyBuckets(DeviceId deviceId, |
| 525 | GroupBuckets buckets) { |
| 526 | ConcurrentMap<GroupId, Group> extraneousMap = |
| 527 | extraneousGroupEntriesById.get(deviceId); |
| 528 | if (extraneousMap == null) { |
| 529 | return null; |
| 530 | } |
| 531 | |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 532 | for (Group extraneousGroup : extraneousMap.values()) { |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 533 | if (extraneousGroup.buckets().equals(buckets)) { |
| 534 | return extraneousGroup; |
| 535 | } |
| 536 | } |
| 537 | return null; |
| 538 | } |
| 539 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 540 | private void storeGroupDescriptionInternal(GroupDescription groupDesc) { |
| 541 | // Check if a group is existing with the same key |
| 542 | if (getGroup(groupDesc.deviceId(), groupDesc.appCookie()) != null) { |
| 543 | return; |
| 544 | } |
| 545 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 546 | if (deviceAuditStatus.get(groupDesc.deviceId()) == null) { |
| 547 | // Device group audit has not completed yet |
| 548 | // Add this group description to pending group key table |
| 549 | // Create a group entry object with Dummy Group ID |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 550 | log.debug("storeGroupDescriptionInternal: Device {} AUDIT pending...Queuing Group ADD request", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 551 | groupDesc.deviceId()); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 552 | StoredGroupEntry group = new DefaultGroup(dummyGroupId, groupDesc); |
| 553 | group.setState(GroupState.WAITING_AUDIT_COMPLETE); |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 554 | Map<GroupStoreKeyMapKey, StoredGroupEntry> pendingKeyTable = |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 555 | getPendingGroupKeyTable(); |
| 556 | pendingKeyTable.put(new GroupStoreKeyMapKey(groupDesc.deviceId(), |
| 557 | groupDesc.appCookie()), |
| 558 | group); |
| 559 | return; |
| 560 | } |
| 561 | |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 562 | Group matchingExtraneousGroup = null; |
| 563 | if (groupDesc.givenGroupId() != null) { |
| 564 | //Check if there is a extraneous group existing with the same Id |
| 565 | matchingExtraneousGroup = getMatchingExtraneousGroupbyId( |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 566 | groupDesc.deviceId(), groupDesc.givenGroupId()); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 567 | if (matchingExtraneousGroup != null) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 568 | log.debug("storeGroupDescriptionInternal: Matching extraneous group " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 569 | + "found in Device {} for group id 0x{}", |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 570 | groupDesc.deviceId(), |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 571 | Integer.toHexString(groupDesc.givenGroupId())); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 572 | //Check if the group buckets matches with user provided buckets |
| 573 | if (matchingExtraneousGroup.buckets().equals(groupDesc.buckets())) { |
| 574 | //Group is already existing with the same buckets and Id |
| 575 | // Create a group entry object |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 576 | log.debug("storeGroupDescriptionInternal: Buckets also matching " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 577 | + "in Device {} for group id 0x{}", |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 578 | groupDesc.deviceId(), |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 579 | Integer.toHexString(groupDesc.givenGroupId())); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 580 | StoredGroupEntry group = new DefaultGroup( |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 581 | matchingExtraneousGroup.id(), groupDesc); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 582 | // Insert the newly created group entry into key and id maps |
| 583 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 584 | put(new GroupStoreKeyMapKey(groupDesc.deviceId(), |
| 585 | groupDesc.appCookie()), group); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 586 | // Ensure it also inserted into group id based table to |
| 587 | // avoid any chances of duplication in group id generation |
| 588 | getGroupIdTable(groupDesc.deviceId()). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 589 | put(matchingExtraneousGroup.id(), group); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 590 | addOrUpdateGroupEntry(matchingExtraneousGroup); |
| 591 | removeExtraneousGroupEntry(matchingExtraneousGroup); |
| 592 | return; |
| 593 | } else { |
| 594 | //Group buckets are not matching. Update group |
| 595 | //with user provided buckets. |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 596 | log.debug("storeGroupDescriptionInternal: Buckets are not " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 597 | + "matching in Device {} for group id 0x{}", |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 598 | groupDesc.deviceId(), |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 599 | Integer.toHexString(groupDesc.givenGroupId())); |
| 600 | StoredGroupEntry modifiedGroup = new DefaultGroup( |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 601 | matchingExtraneousGroup.id(), groupDesc); |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 602 | modifiedGroup.setState(GroupState.PENDING_UPDATE); |
| 603 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 604 | put(new GroupStoreKeyMapKey(groupDesc.deviceId(), |
| 605 | groupDesc.appCookie()), modifiedGroup); |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 606 | // Ensure it also inserted into group id based table to |
| 607 | // avoid any chances of duplication in group id generation |
| 608 | getGroupIdTable(groupDesc.deviceId()). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 609 | put(matchingExtraneousGroup.id(), modifiedGroup); |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 610 | removeExtraneousGroupEntry(matchingExtraneousGroup); |
| 611 | log.debug("storeGroupDescriptionInternal: Triggering Group " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 612 | + "UPDATE request for {} in device {}", |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 613 | matchingExtraneousGroup.id(), |
| 614 | groupDesc.deviceId()); |
| 615 | notifyDelegate(new GroupEvent(Type.GROUP_UPDATE_REQUESTED, modifiedGroup)); |
| 616 | return; |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | } else { |
| 620 | //Check if there is an extraneous group with user provided buckets |
| 621 | matchingExtraneousGroup = getMatchingExtraneousGroupbyBuckets( |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 622 | groupDesc.deviceId(), groupDesc.buckets()); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 623 | if (matchingExtraneousGroup != null) { |
| 624 | //Group is already existing with the same buckets. |
| 625 | //So reuse this group. |
| 626 | log.debug("storeGroupDescriptionInternal: Matching extraneous group found in Device {}", |
| 627 | groupDesc.deviceId()); |
| 628 | //Create a group entry object |
| 629 | StoredGroupEntry group = new DefaultGroup( |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 630 | matchingExtraneousGroup.id(), groupDesc); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 631 | // Insert the newly created group entry into key and id maps |
| 632 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 633 | put(new GroupStoreKeyMapKey(groupDesc.deviceId(), |
| 634 | groupDesc.appCookie()), group); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 635 | // Ensure it also inserted into group id based table to |
| 636 | // avoid any chances of duplication in group id generation |
| 637 | getGroupIdTable(groupDesc.deviceId()). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 638 | put(matchingExtraneousGroup.id(), group); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 639 | addOrUpdateGroupEntry(matchingExtraneousGroup); |
| 640 | removeExtraneousGroupEntry(matchingExtraneousGroup); |
| 641 | return; |
| 642 | } else { |
| 643 | //TODO: Check if there are any empty groups that can be used here |
| 644 | log.debug("storeGroupDescriptionInternal: No matching extraneous groups found in Device {}", |
| 645 | groupDesc.deviceId()); |
| 646 | } |
| 647 | } |
| 648 | |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 649 | GroupId id = null; |
| 650 | if (groupDesc.givenGroupId() == null) { |
| 651 | // Get a new group identifier |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 652 | id = new GroupId(getFreeGroupIdValue(groupDesc.deviceId())); |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 653 | } else { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 654 | // we need to use the identifier passed in by caller, but check if |
| 655 | // already used |
| 656 | Group existing = getGroup(groupDesc.deviceId(), |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 657 | new GroupId(groupDesc.givenGroupId())); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 658 | if (existing != null) { |
| 659 | log.warn("Group already exists with the same id: 0x{} in dev:{} " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 660 | + "but with different key: {} (request gkey: {})", |
| 661 | Integer.toHexString(groupDesc.givenGroupId()), |
| 662 | groupDesc.deviceId(), |
| 663 | existing.appCookie(), |
| 664 | groupDesc.appCookie()); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 665 | return; |
| 666 | } |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 667 | id = new GroupId(groupDesc.givenGroupId()); |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 668 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 669 | // Create a group entry object |
| 670 | StoredGroupEntry group = new DefaultGroup(id, groupDesc); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 671 | // Insert the newly created group entry into key and id maps |
| 672 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 673 | put(new GroupStoreKeyMapKey(groupDesc.deviceId(), |
| 674 | groupDesc.appCookie()), group); |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 675 | // Ensure it also inserted into group id based table to |
| 676 | // avoid any chances of duplication in group id generation |
| 677 | getGroupIdTable(groupDesc.deviceId()). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 678 | put(id, group); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 679 | log.debug("storeGroupDescriptionInternal: Processing Group ADD request for Id {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 680 | id, |
| 681 | groupDesc.deviceId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 682 | notifyDelegate(new GroupEvent(GroupEvent.Type.GROUP_ADD_REQUESTED, |
| 683 | group)); |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * Updates the existing group entry with the information |
| 688 | * from group description. |
| 689 | * |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 690 | * @param deviceId the device ID |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 691 | * @param oldAppCookie the current group key |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 692 | * @param type update type |
| 693 | * @param newBuckets group buckets for updates |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 694 | * @param newAppCookie optional new group key |
| 695 | */ |
| 696 | @Override |
| 697 | public void updateGroupDescription(DeviceId deviceId, |
| 698 | GroupKey oldAppCookie, |
| 699 | UpdateType type, |
| 700 | GroupBuckets newBuckets, |
| 701 | GroupKey newAppCookie) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 702 | // Check if group update to be done by a remote instance |
sangho | 52abe3a | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 703 | if (mastershipService.getMasterFor(deviceId) != null && |
| 704 | mastershipService.getLocalRole(deviceId) != MastershipRole.MASTER) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 705 | log.debug("updateGroupDescription: Device {} local role is not MASTER", |
| 706 | deviceId); |
| 707 | if (mastershipService.getMasterFor(deviceId) == null) { |
| 708 | log.error("No Master for device {}..." |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 709 | + "Can not perform update group operation", |
| 710 | deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 711 | //TODO: Send Group operation failure event |
| 712 | return; |
| 713 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 714 | GroupStoreMessage groupOp = GroupStoreMessage. |
| 715 | createGroupUpdateRequestMsg(deviceId, |
| 716 | oldAppCookie, |
| 717 | type, |
| 718 | newBuckets, |
| 719 | newAppCookie); |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 720 | |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 721 | clusterCommunicator.unicast(groupOp, |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 722 | GroupStoreMessageSubjects.REMOTE_GROUP_OP_REQUEST, |
| 723 | clusterMsgSerializer::serialize, |
| 724 | mastershipService.getMasterFor(deviceId)).whenComplete((result, error) -> { |
| 725 | if (error != null) { |
| 726 | log.warn("Failed to send request to master: {} to {}", |
| 727 | groupOp, |
| 728 | mastershipService.getMasterFor(deviceId), error); |
| 729 | } |
| 730 | //TODO: Send Group operation failure event |
| 731 | }); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 732 | return; |
| 733 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 734 | log.debug("updateGroupDescription for device {} is getting handled locally", |
| 735 | deviceId); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 736 | updateGroupDescriptionInternal(deviceId, |
| 737 | oldAppCookie, |
| 738 | type, |
| 739 | newBuckets, |
| 740 | newAppCookie); |
| 741 | } |
| 742 | |
| 743 | private void updateGroupDescriptionInternal(DeviceId deviceId, |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 744 | GroupKey oldAppCookie, |
| 745 | UpdateType type, |
| 746 | GroupBuckets newBuckets, |
| 747 | GroupKey newAppCookie) { |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 748 | // Check if a group is existing with the provided key |
| 749 | Group oldGroup = getGroup(deviceId, oldAppCookie); |
| 750 | if (oldGroup == null) { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 751 | log.warn("updateGroupDescriptionInternal: Group not found...strange. " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 752 | + "GroupKey:{} DeviceId:{}", oldAppCookie, deviceId); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 753 | return; |
| 754 | } |
| 755 | |
| 756 | List<GroupBucket> newBucketList = getUpdatedBucketList(oldGroup, |
| 757 | type, |
| 758 | newBuckets); |
| 759 | if (newBucketList != null) { |
| 760 | // Create a new group object from the old group |
| 761 | GroupBuckets updatedBuckets = new GroupBuckets(newBucketList); |
| 762 | GroupKey newCookie = (newAppCookie != null) ? newAppCookie : oldAppCookie; |
| 763 | GroupDescription updatedGroupDesc = new DefaultGroupDescription( |
| 764 | oldGroup.deviceId(), |
| 765 | oldGroup.type(), |
| 766 | updatedBuckets, |
| 767 | newCookie, |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 768 | oldGroup.givenGroupId(), |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 769 | oldGroup.appId()); |
| 770 | StoredGroupEntry newGroup = new DefaultGroup(oldGroup.id(), |
| 771 | updatedGroupDesc); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 772 | log.debug("updateGroupDescriptionInternal: group entry {} in device {} moving from {} to PENDING_UPDATE", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 773 | oldGroup.id(), |
| 774 | oldGroup.deviceId(), |
| 775 | oldGroup.state()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 776 | newGroup.setState(GroupState.PENDING_UPDATE); |
| 777 | newGroup.setLife(oldGroup.life()); |
| 778 | newGroup.setPackets(oldGroup.packets()); |
| 779 | newGroup.setBytes(oldGroup.bytes()); |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 780 | //Update the group entry in groupkey based map. |
| 781 | //Update to groupid based map will happen in the |
| 782 | //groupkey based map update listener |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 783 | log.debug("updateGroupDescriptionInternal with type {}: Group updated with buckets", |
| 784 | type); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 785 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 786 | put(new GroupStoreKeyMapKey(newGroup.deviceId(), |
| 787 | newGroup.appCookie()), newGroup); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 788 | notifyDelegate(new GroupEvent(Type.GROUP_UPDATE_REQUESTED, newGroup)); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 789 | } else { |
| 790 | log.warn("updateGroupDescriptionInternal with type {}: No " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 791 | + "change in the buckets in update", type); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
| 795 | private List<GroupBucket> getUpdatedBucketList(Group oldGroup, |
| 796 | UpdateType type, |
| 797 | GroupBuckets buckets) { |
Victor Silva | 0282ab8 | 2016-11-15 16:30:27 -0300 | [diff] [blame] | 798 | if (type == UpdateType.SET) { |
| 799 | return buckets.buckets(); |
| 800 | } |
| 801 | |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame] | 802 | List<GroupBucket> oldBuckets = oldGroup.buckets().buckets(); |
| 803 | List<GroupBucket> updatedBucketList = new ArrayList<>(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 804 | boolean groupDescUpdated = false; |
| 805 | |
| 806 | if (type == UpdateType.ADD) { |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame] | 807 | List<GroupBucket> newBuckets = buckets.buckets(); |
| 808 | |
| 809 | // Add old buckets that will not be updated and check if any will be updated. |
| 810 | for (GroupBucket oldBucket : oldBuckets) { |
| 811 | int newBucketIndex = newBuckets.indexOf(oldBucket); |
| 812 | |
| 813 | if (newBucketIndex != -1) { |
| 814 | GroupBucket newBucket = newBuckets.get(newBucketIndex); |
| 815 | if (!newBucket.hasSameParameters(oldBucket)) { |
| 816 | // Bucket will be updated |
| 817 | groupDescUpdated = true; |
| 818 | } |
| 819 | } else { |
| 820 | // Old bucket will remain the same - add it. |
| 821 | updatedBucketList.add(oldBucket); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 822 | } |
| 823 | } |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame] | 824 | |
| 825 | // Add all new buckets |
| 826 | updatedBucketList.addAll(newBuckets); |
| 827 | if (!oldBuckets.containsAll(newBuckets)) { |
| 828 | groupDescUpdated = true; |
| 829 | } |
| 830 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 831 | } else if (type == UpdateType.REMOVE) { |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame] | 832 | List<GroupBucket> bucketsToRemove = buckets.buckets(); |
| 833 | |
| 834 | // Check which old buckets should remain |
| 835 | for (GroupBucket oldBucket : oldBuckets) { |
| 836 | if (!bucketsToRemove.contains(oldBucket)) { |
| 837 | updatedBucketList.add(oldBucket); |
| 838 | } else { |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 839 | groupDescUpdated = true; |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | if (groupDescUpdated) { |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame] | 845 | return updatedBucketList; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 846 | } else { |
| 847 | return null; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * Triggers deleting the existing group entry. |
| 853 | * |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 854 | * @param deviceId the device ID |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 855 | * @param appCookie the group key |
| 856 | */ |
| 857 | @Override |
| 858 | public void deleteGroupDescription(DeviceId deviceId, |
| 859 | GroupKey appCookie) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 860 | // Check if group to be deleted by a remote instance |
| 861 | if (mastershipService. |
| 862 | getLocalRole(deviceId) != MastershipRole.MASTER) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 863 | log.debug("deleteGroupDescription: Device {} local role is not MASTER", |
| 864 | deviceId); |
| 865 | if (mastershipService.getMasterFor(deviceId) == null) { |
| 866 | log.error("No Master for device {}..." |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 867 | + "Can not perform delete group operation", |
| 868 | deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 869 | //TODO: Send Group operation failure event |
| 870 | return; |
| 871 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 872 | GroupStoreMessage groupOp = GroupStoreMessage. |
| 873 | createGroupDeleteRequestMsg(deviceId, |
| 874 | appCookie); |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 875 | |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 876 | clusterCommunicator.unicast(groupOp, |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 877 | GroupStoreMessageSubjects.REMOTE_GROUP_OP_REQUEST, |
| 878 | clusterMsgSerializer::serialize, |
| 879 | mastershipService.getMasterFor(deviceId)).whenComplete((result, error) -> { |
| 880 | if (error != null) { |
| 881 | log.warn("Failed to send request to master: {} to {}", |
| 882 | groupOp, |
| 883 | mastershipService.getMasterFor(deviceId), error); |
| 884 | } |
| 885 | //TODO: Send Group operation failure event |
| 886 | }); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 887 | return; |
| 888 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 889 | log.debug("deleteGroupDescription in device {} is getting handled locally", |
| 890 | deviceId); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 891 | deleteGroupDescriptionInternal(deviceId, appCookie); |
| 892 | } |
| 893 | |
| 894 | private void deleteGroupDescriptionInternal(DeviceId deviceId, |
| 895 | GroupKey appCookie) { |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 896 | // Check if a group is existing with the provided key |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 897 | StoredGroupEntry existing = getStoredGroupEntry(deviceId, appCookie); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 898 | if (existing == null) { |
| 899 | return; |
| 900 | } |
| 901 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 902 | log.debug("deleteGroupDescriptionInternal: group entry {} in device {} moving from {} to PENDING_DELETE", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 903 | existing.id(), |
| 904 | existing.deviceId(), |
| 905 | existing.state()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 906 | synchronized (existing) { |
| 907 | existing.setState(GroupState.PENDING_DELETE); |
Saurav Das | 80980c7 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 908 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 909 | put(new GroupStoreKeyMapKey(existing.deviceId(), existing.appCookie()), |
| 910 | existing); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 911 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 912 | log.debug("deleteGroupDescriptionInternal: in device {} issuing GROUP_REMOVE_REQUESTED", |
| 913 | deviceId); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 914 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVE_REQUESTED, existing)); |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * Stores a new group entry, or updates an existing entry. |
| 919 | * |
| 920 | * @param group group entry |
| 921 | */ |
| 922 | @Override |
| 923 | public void addOrUpdateGroupEntry(Group group) { |
| 924 | // check if this new entry is an update to an existing entry |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 925 | StoredGroupEntry existing = getStoredGroupEntry(group.deviceId(), |
| 926 | group.id()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 927 | GroupEvent event = null; |
| 928 | |
| 929 | if (existing != null) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 930 | log.trace("addOrUpdateGroupEntry: updating group entry {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 931 | group.id(), |
| 932 | group.deviceId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 933 | synchronized (existing) { |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 934 | for (GroupBucket bucket : group.buckets().buckets()) { |
Sho SHIMIZU | 30d639b | 2015-05-05 09:30:35 -0700 | [diff] [blame] | 935 | Optional<GroupBucket> matchingBucket = |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 936 | existing.buckets().buckets() |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 937 | .stream() |
| 938 | .filter((existingBucket) -> (existingBucket.equals(bucket))) |
| 939 | .findFirst(); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 940 | if (matchingBucket.isPresent()) { |
| 941 | ((StoredGroupBucketEntry) matchingBucket. |
| 942 | get()).setPackets(bucket.packets()); |
| 943 | ((StoredGroupBucketEntry) matchingBucket. |
| 944 | get()).setBytes(bucket.bytes()); |
| 945 | } else { |
| 946 | log.warn("addOrUpdateGroupEntry: No matching " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 947 | + "buckets to update stats"); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 948 | } |
| 949 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 950 | existing.setLife(group.life()); |
| 951 | existing.setPackets(group.packets()); |
| 952 | existing.setBytes(group.bytes()); |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 953 | existing.setReferenceCount(group.referenceCount()); |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 954 | existing.setFailedRetryCount(0); |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 955 | if ((existing.state() == GroupState.PENDING_ADD) || |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 956 | (existing.state() == GroupState.PENDING_ADD_RETRY)) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 957 | log.trace("addOrUpdateGroupEntry: group entry {} in device {} moving from {} to ADDED", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 958 | existing.id(), |
| 959 | existing.deviceId(), |
| 960 | existing.state()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 961 | existing.setState(GroupState.ADDED); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 962 | existing.setIsGroupStateAddedFirstTime(true); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 963 | event = new GroupEvent(Type.GROUP_ADDED, existing); |
| 964 | } else { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 965 | log.trace("addOrUpdateGroupEntry: group entry {} in device {} moving from {} to ADDED", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 966 | existing.id(), |
| 967 | existing.deviceId(), |
| 968 | GroupState.PENDING_UPDATE); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 969 | existing.setState(GroupState.ADDED); |
| 970 | existing.setIsGroupStateAddedFirstTime(false); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 971 | event = new GroupEvent(Type.GROUP_UPDATED, existing); |
| 972 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 973 | //Re-PUT map entries to trigger map update events |
| 974 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 975 | put(new GroupStoreKeyMapKey(existing.deviceId(), |
| 976 | existing.appCookie()), existing); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 977 | } |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 978 | } else { |
| 979 | log.warn("addOrUpdateGroupEntry: Group update " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 980 | + "happening for a non-existing entry in the map"); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 981 | } |
| 982 | |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 983 | // XXX if map is going to trigger event, is this one needed? |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 984 | if (event != null) { |
| 985 | notifyDelegate(event); |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * Removes the group entry from store. |
| 991 | * |
| 992 | * @param group group entry |
| 993 | */ |
| 994 | @Override |
| 995 | public void removeGroupEntry(Group group) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 996 | StoredGroupEntry existing = getStoredGroupEntry(group.deviceId(), |
| 997 | group.id()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 998 | |
| 999 | if (existing != null) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1000 | log.debug("removeGroupEntry: removing group entry {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1001 | group.id(), |
| 1002 | group.deviceId()); |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 1003 | //Removal from groupid based map will happen in the |
| 1004 | //map update listener |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1005 | getGroupStoreKeyMap().remove(new GroupStoreKeyMapKey(existing.deviceId(), |
| 1006 | existing.appCookie())); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1007 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVED, existing)); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1008 | } else { |
| 1009 | log.warn("removeGroupEntry for {} in device{} is " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1010 | + "not existing in our maps", |
| 1011 | group.id(), |
| 1012 | group.deviceId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | |
Victor Silva | 4e8b783 | 2016-08-17 17:11:19 -0300 | [diff] [blame] | 1016 | private void purgeGroupEntries(Set<Entry<GroupStoreKeyMapKey, StoredGroupEntry>> entries) { |
| 1017 | entries.forEach(entry -> { |
| 1018 | groupStoreEntriesByKey.remove(entry.getKey()); |
| 1019 | }); |
| 1020 | } |
| 1021 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1022 | @Override |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1023 | public void purgeGroupEntry(DeviceId deviceId) { |
Victor Silva | 4e8b783 | 2016-08-17 17:11:19 -0300 | [diff] [blame] | 1024 | Set<Entry<GroupStoreKeyMapKey, StoredGroupEntry>> entriesPendingRemove = |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1025 | new HashSet<>(); |
| 1026 | |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1027 | getGroupStoreKeyMap().entrySet().stream() |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1028 | .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
Victor Silva | 4e8b783 | 2016-08-17 17:11:19 -0300 | [diff] [blame] | 1029 | .forEach(entriesPendingRemove::add); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1030 | |
Victor Silva | 4e8b783 | 2016-08-17 17:11:19 -0300 | [diff] [blame] | 1031 | purgeGroupEntries(entriesPendingRemove); |
| 1032 | } |
| 1033 | |
| 1034 | @Override |
| 1035 | public void purgeGroupEntries() { |
| 1036 | purgeGroupEntries(getGroupStoreKeyMap().entrySet()); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | @Override |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1040 | public void deviceInitialAuditCompleted(DeviceId deviceId, |
| 1041 | boolean completed) { |
| 1042 | synchronized (deviceAuditStatus) { |
| 1043 | if (completed) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1044 | log.debug("AUDIT completed for device {}", |
| 1045 | deviceId); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1046 | deviceAuditStatus.put(deviceId, true); |
| 1047 | // Execute all pending group requests |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1048 | List<StoredGroupEntry> pendingGroupRequests = |
| 1049 | getPendingGroupKeyTable().values() |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1050 | .stream() |
| 1051 | .filter(g -> g.deviceId().equals(deviceId)) |
| 1052 | .collect(Collectors.toList()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1053 | log.debug("processing pending group add requests for device {} and number of pending requests {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1054 | deviceId, |
| 1055 | pendingGroupRequests.size()); |
| 1056 | for (Group group : pendingGroupRequests) { |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1057 | GroupDescription tmp = new DefaultGroupDescription( |
| 1058 | group.deviceId(), |
| 1059 | group.type(), |
| 1060 | group.buckets(), |
| 1061 | group.appCookie(), |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 1062 | group.givenGroupId(), |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1063 | group.appId()); |
| 1064 | storeGroupDescriptionInternal(tmp); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1065 | getPendingGroupKeyTable(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1066 | remove(new GroupStoreKeyMapKey(deviceId, group.appCookie())); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1067 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1068 | } else { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 1069 | Boolean audited = deviceAuditStatus.get(deviceId); |
| 1070 | if (audited != null && audited) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1071 | log.debug("Clearing AUDIT status for device {}", deviceId); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1072 | deviceAuditStatus.put(deviceId, false); |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | @Override |
| 1079 | public boolean deviceInitialAuditStatus(DeviceId deviceId) { |
| 1080 | synchronized (deviceAuditStatus) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 1081 | Boolean audited = deviceAuditStatus.get(deviceId); |
| 1082 | return audited != null && audited; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | @Override |
| 1087 | public void groupOperationFailed(DeviceId deviceId, GroupOperation operation) { |
| 1088 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1089 | StoredGroupEntry existing = getStoredGroupEntry(deviceId, |
| 1090 | operation.groupId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1091 | |
| 1092 | if (existing == null) { |
| 1093 | log.warn("No group entry with ID {} found ", operation.groupId()); |
| 1094 | return; |
| 1095 | } |
| 1096 | |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1097 | log.warn("groupOperationFailed: group operation {} failed in state {} " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1098 | + "for group {} in device {} with code {}", |
| 1099 | operation.opType(), |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1100 | existing.state(), |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1101 | existing.id(), |
| 1102 | existing.deviceId(), |
| 1103 | operation.failureCode()); |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1104 | if (operation.failureCode() == GroupOperation.GroupMsgErrorCode.GROUP_EXISTS) { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1105 | if (operation.buckets().equals(existing.buckets())) { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1106 | if (existing.state() == GroupState.PENDING_ADD || |
| 1107 | existing.state() == GroupState.PENDING_ADD_RETRY) { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1108 | log.info("GROUP_EXISTS: GroupID and Buckets match for group in pending " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1109 | + "add state - moving to ADDED for group {} in device {}", |
| 1110 | existing.id(), deviceId); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1111 | addOrUpdateGroupEntry(existing); |
| 1112 | return; |
| 1113 | } else { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1114 | log.warn("GROUP_EXISTS: GroupId and Buckets match but existing" |
| 1115 | + "group in state: {}", existing.state()); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1116 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1117 | } else { |
| 1118 | log.warn("GROUP EXISTS: Group ID matched but buckets did not. " |
| 1119 | + "Operation: {} Existing: {}", operation.buckets(), |
| 1120 | existing.buckets()); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1121 | } |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1122 | } |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1123 | if (operation.failureCode() == GroupOperation.GroupMsgErrorCode.INVALID_GROUP) { |
| 1124 | existing.incrFailedRetryCount(); |
| 1125 | if (existing.failedRetryCount() < MAX_FAILED_ATTEMPTS) { |
| 1126 | log.warn("Group {} programming failed {} of {} times in dev {}, " |
| 1127 | + "retrying ..", existing.id(), |
| 1128 | existing.failedRetryCount(), MAX_FAILED_ATTEMPTS, |
| 1129 | deviceId); |
| 1130 | return; |
| 1131 | } |
| 1132 | log.warn("Group {} programming failed {} of {} times in dev {}, " |
| 1133 | + "removing group from store", existing.id(), |
| 1134 | existing.failedRetryCount(), MAX_FAILED_ATTEMPTS, |
| 1135 | deviceId); |
| 1136 | // fall through to case |
| 1137 | } |
| 1138 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1139 | switch (operation.opType()) { |
| 1140 | case ADD: |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1141 | if (existing.state() == GroupState.PENDING_ADD |
| 1142 | || existing.state() == GroupState.PENDING_ADD_RETRY) { |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1143 | notifyDelegate(new GroupEvent(Type.GROUP_ADD_FAILED, existing)); |
| 1144 | log.warn("groupOperationFailed: cleaningup " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1145 | + "group {} from store in device {}....", |
| 1146 | existing.id(), |
| 1147 | existing.deviceId()); |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1148 | //Removal from groupid based map will happen in the |
| 1149 | //map update listener |
| 1150 | getGroupStoreKeyMap().remove(new GroupStoreKeyMapKey(existing.deviceId(), |
| 1151 | existing.appCookie())); |
| 1152 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1153 | break; |
| 1154 | case MODIFY: |
| 1155 | notifyDelegate(new GroupEvent(Type.GROUP_UPDATE_FAILED, existing)); |
| 1156 | break; |
| 1157 | case DELETE: |
| 1158 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVE_FAILED, existing)); |
| 1159 | break; |
| 1160 | default: |
| 1161 | log.warn("Unknown group operation type {}", operation.opType()); |
| 1162 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | @Override |
| 1166 | public void addOrUpdateExtraneousGroupEntry(Group group) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1167 | log.debug("add/update extraneous group entry {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1168 | group.id(), |
| 1169 | group.deviceId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1170 | ConcurrentMap<GroupId, Group> extraneousIdTable = |
| 1171 | getExtraneousGroupIdTable(group.deviceId()); |
| 1172 | extraneousIdTable.put(group.id(), group); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 1173 | // Don't remove the extraneous groups, instead re-use it when |
| 1174 | // a group request comes with the same set of buckets |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | @Override |
| 1178 | public void removeExtraneousGroupEntry(Group group) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1179 | log.debug("remove extraneous group entry {} of device {} from store", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1180 | group.id(), |
| 1181 | group.deviceId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1182 | ConcurrentMap<GroupId, Group> extraneousIdTable = |
| 1183 | getExtraneousGroupIdTable(group.deviceId()); |
| 1184 | extraneousIdTable.remove(group.id()); |
| 1185 | } |
| 1186 | |
| 1187 | @Override |
| 1188 | public Iterable<Group> getExtraneousGroups(DeviceId deviceId) { |
| 1189 | // flatten and make iterator unmodifiable |
| 1190 | return FluentIterable.from( |
| 1191 | getExtraneousGroupIdTable(deviceId).values()); |
| 1192 | } |
| 1193 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1194 | /** |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 1195 | * Map handler to receive any events when the group key map is updated. |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1196 | */ |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 1197 | private class GroupStoreKeyMapListener implements |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1198 | MapEventListener<GroupStoreKeyMapKey, StoredGroupEntry> { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1199 | |
| 1200 | @Override |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1201 | public void event(MapEvent<GroupStoreKeyMapKey, StoredGroupEntry> mapEvent) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1202 | GroupEvent groupEvent = null; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1203 | GroupStoreKeyMapKey key = mapEvent.key(); |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1204 | StoredGroupEntry group = Versioned.valueOrNull(mapEvent.newValue()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1205 | if ((key == null) && (group == null)) { |
| 1206 | log.error("GroupStoreKeyMapListener: Received " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1207 | + "event {} with null entry", mapEvent.type()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1208 | return; |
| 1209 | } else if (group == null) { |
| 1210 | group = getGroupIdTable(key.deviceId()).values() |
| 1211 | .stream() |
| 1212 | .filter((storedGroup) -> (storedGroup.appCookie().equals(key.appCookie))) |
Yuta HIGUCHI | 6e5f470 | 2016-11-21 11:42:11 -0800 | [diff] [blame] | 1213 | .findFirst().orElse(null); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1214 | if (group == null) { |
| 1215 | log.error("GroupStoreKeyMapListener: Received " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1216 | + "event {} with null entry... can not process", mapEvent.type()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1217 | return; |
| 1218 | } |
| 1219 | } |
| 1220 | log.trace("received groupid map event {} for id {} in device {}", |
| 1221 | mapEvent.type(), |
| 1222 | group.id(), |
jaegonkim | 68e080c | 2016-12-01 22:31:01 +0900 | [diff] [blame] | 1223 | (key != null ? key.deviceId() : null)); |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1224 | if (mapEvent.type() == MapEvent.Type.INSERT || mapEvent.type() == MapEvent.Type.UPDATE) { |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 1225 | // Update the group ID table |
| 1226 | getGroupIdTable(group.deviceId()).put(group.id(), group); |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1227 | StoredGroupEntry value = Versioned.valueOrNull(mapEvent.newValue()); |
| 1228 | if (value.state() == Group.GroupState.ADDED) { |
| 1229 | if (value.isGroupStateAddedFirstTime()) { |
| 1230 | groupEvent = new GroupEvent(Type.GROUP_ADDED, value); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1231 | log.trace("Received first time GROUP_ADDED state update for id {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1232 | group.id(), |
| 1233 | group.deviceId()); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1234 | } else { |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1235 | groupEvent = new GroupEvent(Type.GROUP_UPDATED, value); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1236 | log.trace("Received following GROUP_ADDED state update for id {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1237 | group.id(), |
| 1238 | group.deviceId()); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1239 | } |
| 1240 | } |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1241 | } else if (mapEvent.type() == MapEvent.Type.REMOVE) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1242 | groupEvent = new GroupEvent(Type.GROUP_REMOVED, group); |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 1243 | // Remove the entry from the group ID table |
| 1244 | getGroupIdTable(group.deviceId()).remove(group.id(), group); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | if (groupEvent != null) { |
| 1248 | notifyDelegate(groupEvent); |
| 1249 | } |
| 1250 | } |
| 1251 | } |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 1252 | |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 1253 | private void processGroupMessage(GroupStoreMessage message) { |
| 1254 | if (message.type() == GroupStoreMessage.Type.FAILOVER) { |
| 1255 | // FIXME: groupStoreEntriesByKey inaccessible here |
| 1256 | getGroupIdTable(message.deviceId()).values() |
| 1257 | .stream() |
| 1258 | .filter((storedGroup) -> (storedGroup.appCookie().equals(message.appCookie()))) |
| 1259 | .findFirst().ifPresent(group -> notifyDelegate(new GroupEvent(Type.GROUP_BUCKET_FAILOVER, group))); |
| 1260 | } |
| 1261 | } |
| 1262 | |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 1263 | private void process(GroupStoreMessage groupOp) { |
| 1264 | log.debug("Received remote group operation {} request for device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1265 | groupOp.type(), |
| 1266 | groupOp.deviceId()); |
| 1267 | if (!mastershipService.isLocalMaster(groupOp.deviceId())) { |
| 1268 | log.warn("This node is not MASTER for device {}", groupOp.deviceId()); |
| 1269 | return; |
| 1270 | } |
| 1271 | if (groupOp.type() == GroupStoreMessage.Type.ADD) { |
| 1272 | storeGroupDescriptionInternal(groupOp.groupDesc()); |
| 1273 | } else if (groupOp.type() == GroupStoreMessage.Type.UPDATE) { |
| 1274 | updateGroupDescriptionInternal(groupOp.deviceId(), |
| 1275 | groupOp.appCookie(), |
| 1276 | groupOp.updateType(), |
| 1277 | groupOp.updateBuckets(), |
| 1278 | groupOp.newAppCookie()); |
| 1279 | } else if (groupOp.type() == GroupStoreMessage.Type.DELETE) { |
| 1280 | deleteGroupDescriptionInternal(groupOp.deviceId(), |
| 1281 | groupOp.appCookie()); |
| 1282 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | /** |
| 1286 | * Flattened map key to be used to store group entries. |
| 1287 | */ |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 1288 | protected static class GroupStoreMapKey { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1289 | private final DeviceId deviceId; |
| 1290 | |
| 1291 | public GroupStoreMapKey(DeviceId deviceId) { |
| 1292 | this.deviceId = deviceId; |
| 1293 | } |
| 1294 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1295 | public DeviceId deviceId() { |
| 1296 | return deviceId; |
| 1297 | } |
| 1298 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1299 | @Override |
| 1300 | public boolean equals(Object o) { |
| 1301 | if (this == o) { |
| 1302 | return true; |
| 1303 | } |
| 1304 | if (!(o instanceof GroupStoreMapKey)) { |
| 1305 | return false; |
| 1306 | } |
| 1307 | GroupStoreMapKey that = (GroupStoreMapKey) o; |
| 1308 | return this.deviceId.equals(that.deviceId); |
| 1309 | } |
| 1310 | |
| 1311 | @Override |
| 1312 | public int hashCode() { |
| 1313 | int result = 17; |
| 1314 | |
| 1315 | result = 31 * result + Objects.hash(this.deviceId); |
| 1316 | |
| 1317 | return result; |
| 1318 | } |
| 1319 | } |
| 1320 | |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 1321 | protected static class GroupStoreKeyMapKey extends GroupStoreMapKey { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1322 | private final GroupKey appCookie; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1323 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1324 | public GroupStoreKeyMapKey(DeviceId deviceId, |
| 1325 | GroupKey appCookie) { |
| 1326 | super(deviceId); |
| 1327 | this.appCookie = appCookie; |
| 1328 | } |
| 1329 | |
| 1330 | @Override |
| 1331 | public boolean equals(Object o) { |
| 1332 | if (this == o) { |
| 1333 | return true; |
| 1334 | } |
| 1335 | if (!(o instanceof GroupStoreKeyMapKey)) { |
| 1336 | return false; |
| 1337 | } |
| 1338 | GroupStoreKeyMapKey that = (GroupStoreKeyMapKey) o; |
| 1339 | return (super.equals(that) && |
| 1340 | this.appCookie.equals(that.appCookie)); |
| 1341 | } |
| 1342 | |
| 1343 | @Override |
| 1344 | public int hashCode() { |
| 1345 | int result = 17; |
| 1346 | |
| 1347 | result = 31 * result + super.hashCode() + Objects.hash(this.appCookie); |
| 1348 | |
| 1349 | return result; |
| 1350 | } |
| 1351 | } |
| 1352 | |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 1353 | protected static class GroupStoreIdMapKey extends GroupStoreMapKey { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1354 | private final GroupId groupId; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1355 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1356 | public GroupStoreIdMapKey(DeviceId deviceId, |
| 1357 | GroupId groupId) { |
| 1358 | super(deviceId); |
| 1359 | this.groupId = groupId; |
| 1360 | } |
| 1361 | |
| 1362 | @Override |
| 1363 | public boolean equals(Object o) { |
| 1364 | if (this == o) { |
| 1365 | return true; |
| 1366 | } |
| 1367 | if (!(o instanceof GroupStoreIdMapKey)) { |
| 1368 | return false; |
| 1369 | } |
| 1370 | GroupStoreIdMapKey that = (GroupStoreIdMapKey) o; |
| 1371 | return (super.equals(that) && |
| 1372 | this.groupId.equals(that.groupId)); |
| 1373 | } |
| 1374 | |
| 1375 | @Override |
| 1376 | public int hashCode() { |
| 1377 | int result = 17; |
| 1378 | |
| 1379 | result = 31 * result + super.hashCode() + Objects.hash(this.groupId); |
| 1380 | |
| 1381 | return result; |
| 1382 | } |
| 1383 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1384 | |
| 1385 | @Override |
| 1386 | public void pushGroupMetrics(DeviceId deviceId, |
| 1387 | Collection<Group> groupEntries) { |
| 1388 | boolean deviceInitialAuditStatus = |
| 1389 | deviceInitialAuditStatus(deviceId); |
| 1390 | Set<Group> southboundGroupEntries = |
| 1391 | Sets.newHashSet(groupEntries); |
| 1392 | Set<StoredGroupEntry> storedGroupEntries = |
| 1393 | Sets.newHashSet(getStoredGroups(deviceId)); |
| 1394 | Set<Group> extraneousStoredEntries = |
| 1395 | Sets.newHashSet(getExtraneousGroups(deviceId)); |
| 1396 | |
Sho SHIMIZU | 695bac6 | 2016-08-15 12:41:59 -0700 | [diff] [blame] | 1397 | if (log.isTraceEnabled()) { |
| 1398 | log.trace("pushGroupMetrics: Displaying all ({}) southboundGroupEntries for device {}", |
| 1399 | southboundGroupEntries.size(), |
| 1400 | deviceId); |
| 1401 | for (Group group : southboundGroupEntries) { |
| 1402 | log.trace("Group {} in device {}", group, deviceId); |
| 1403 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1404 | |
Sho SHIMIZU | 695bac6 | 2016-08-15 12:41:59 -0700 | [diff] [blame] | 1405 | log.trace("Displaying all ({}) stored group entries for device {}", |
| 1406 | storedGroupEntries.size(), |
| 1407 | deviceId); |
| 1408 | for (StoredGroupEntry group : storedGroupEntries) { |
| 1409 | log.trace("Stored Group {} for device {}", group, deviceId); |
| 1410 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1413 | garbageCollect(deviceId, southboundGroupEntries, storedGroupEntries); |
| 1414 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1415 | for (Iterator<Group> it2 = southboundGroupEntries.iterator(); it2.hasNext();) { |
| 1416 | Group group = it2.next(); |
| 1417 | if (storedGroupEntries.remove(group)) { |
| 1418 | // we both have the group, let's update some info then. |
| 1419 | log.trace("Group AUDIT: group {} exists in both planes for device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1420 | group.id(), deviceId); |
| 1421 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1422 | groupAdded(group); |
| 1423 | it2.remove(); |
| 1424 | } |
| 1425 | } |
| 1426 | for (Group group : southboundGroupEntries) { |
| 1427 | if (getGroup(group.deviceId(), group.id()) != null) { |
| 1428 | // There is a group existing with the same id |
| 1429 | // It is possible that group update is |
| 1430 | // in progress while we got a stale info from switch |
| 1431 | if (!storedGroupEntries.remove(getGroup( |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1432 | group.deviceId(), group.id()))) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1433 | log.warn("Group AUDIT: Inconsistent state:" |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1434 | + "Group exists in ID based table while " |
| 1435 | + "not present in key based table"); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1436 | } |
| 1437 | } else { |
| 1438 | // there are groups in the switch that aren't in the store |
| 1439 | log.debug("Group AUDIT: extraneous group {} exists in data plane for device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1440 | group.id(), deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1441 | extraneousStoredEntries.remove(group); |
Kavitha Alagesan | c56cded | 2017-01-13 10:48:18 +0530 | [diff] [blame] | 1442 | if (allowExtraneousGroups) { |
| 1443 | extraneousGroup(group); |
| 1444 | } else { |
| 1445 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVE_REQUESTED, group)); |
| 1446 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1447 | } |
| 1448 | } |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 1449 | for (StoredGroupEntry group : storedGroupEntries) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1450 | // there are groups in the store that aren't in the switch |
| 1451 | log.debug("Group AUDIT: group {} missing in data plane for device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1452 | group.id(), deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1453 | groupMissing(group); |
| 1454 | } |
| 1455 | for (Group group : extraneousStoredEntries) { |
| 1456 | // there are groups in the extraneous store that |
| 1457 | // aren't in the switch |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1458 | log.debug("Group AUDIT: clearing extraneous group {} from store for device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1459 | group.id(), deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1460 | removeExtraneousGroupEntry(group); |
| 1461 | } |
| 1462 | |
| 1463 | if (!deviceInitialAuditStatus) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1464 | log.info("Group AUDIT: Setting device {} initial AUDIT completed", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1465 | deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1466 | deviceInitialAuditCompleted(deviceId, true); |
| 1467 | } |
| 1468 | } |
| 1469 | |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 1470 | @Override |
| 1471 | public void notifyOfFailovers(Collection<Group> failoverGroups) { |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 1472 | failoverGroups.forEach(group -> { |
| 1473 | if (group.type() == Group.Type.FAILOVER) { |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 1474 | groupTopic.publish(GroupStoreMessage.createGroupFailoverMsg( |
| 1475 | group.deviceId(), group)); |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 1476 | } |
| 1477 | }); |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 1478 | } |
| 1479 | |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1480 | private void garbageCollect(DeviceId deviceId, |
| 1481 | Set<Group> southboundGroupEntries, |
| 1482 | Set<StoredGroupEntry> storedGroupEntries) { |
| 1483 | if (!garbageCollect) { |
| 1484 | return; |
| 1485 | } |
| 1486 | |
| 1487 | Iterator<StoredGroupEntry> it = storedGroupEntries.iterator(); |
| 1488 | while (it.hasNext()) { |
| 1489 | StoredGroupEntry group = it.next(); |
| 1490 | if (group.state() != GroupState.PENDING_DELETE && checkGroupRefCount(group)) { |
| 1491 | log.debug("Garbage collecting group {} on {}", group, deviceId); |
| 1492 | deleteGroupDescription(deviceId, group.appCookie()); |
| 1493 | southboundGroupEntries.remove(group); |
| 1494 | it.remove(); |
| 1495 | } |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | private boolean checkGroupRefCount(Group group) { |
| 1500 | return (group.referenceCount() == 0 && group.age() >= gcThresh); |
| 1501 | } |
| 1502 | |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 1503 | private void groupMissing(StoredGroupEntry group) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1504 | switch (group.state()) { |
| 1505 | case PENDING_DELETE: |
| 1506 | log.debug("Group {} delete confirmation from device {}", |
| 1507 | group, group.deviceId()); |
| 1508 | removeGroupEntry(group); |
| 1509 | break; |
| 1510 | case ADDED: |
| 1511 | case PENDING_ADD: |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1512 | case PENDING_ADD_RETRY: |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1513 | case PENDING_UPDATE: |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1514 | log.debug("groupMissing: group entry {} in device {} moving from {} to PENDING_ADD_RETRY", |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 1515 | group.id(), |
| 1516 | group.deviceId(), |
| 1517 | group.state()); |
| 1518 | group.setState(Group.GroupState.PENDING_ADD_RETRY); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1519 | //Re-PUT map entries to trigger map update events |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 1520 | getGroupStoreKeyMap().put(new GroupStoreKeyMapKey(group.deviceId(), group.appCookie()), group); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1521 | notifyDelegate(new GroupEvent(GroupEvent.Type.GROUP_ADD_REQUESTED, |
| 1522 | group)); |
| 1523 | break; |
| 1524 | default: |
| 1525 | log.debug("Group {} has not been installed.", group); |
| 1526 | break; |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | private void extraneousGroup(Group group) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1531 | log.trace("Group {} is on device {} but not in store.", |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1532 | group, group.deviceId()); |
| 1533 | addOrUpdateExtraneousGroupEntry(group); |
| 1534 | } |
| 1535 | |
| 1536 | private void groupAdded(Group group) { |
| 1537 | log.trace("Group {} Added or Updated in device {}", |
| 1538 | group, group.deviceId()); |
| 1539 | addOrUpdateGroupEntry(group); |
| 1540 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1541 | } |