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