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