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