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