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