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