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