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