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