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