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