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 | 43ac289 | 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 | 1a833d4 | 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) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 447 | log.debug("In storeGroupDescription"); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 448 | // Check if a group is existing with the same key |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 449 | Group existingGroup = getGroup(groupDesc.deviceId(), groupDesc.appCookie()); |
| 450 | if (existingGroup != null) { |
Saurav Das | c568c34 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 451 | log.debug("Group already exists with the same key {} in dev:{} with id:0x{}", |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 452 | groupDesc.appCookie(), groupDesc.deviceId(), |
| 453 | Integer.toHexString(existingGroup.id().id())); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 454 | return; |
| 455 | } |
| 456 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 457 | // Check if group to be created by a remote instance |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 458 | if (mastershipService.getLocalRole(groupDesc.deviceId()) != MastershipRole.MASTER) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 459 | log.debug("storeGroupDescription: Device {} local role is not MASTER", |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 460 | groupDesc.deviceId()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 461 | if (mastershipService.getMasterFor(groupDesc.deviceId()) == null) { |
Sivachidambaram Subramanian | 9f816de | 2017-06-13 07:16:54 +0530 | [diff] [blame] | 462 | log.debug("No Master for device {}..." |
| 463 | + "Queuing Group ADD request", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 464 | groupDesc.deviceId()); |
Sivachidambaram Subramanian | 9f816de | 2017-06-13 07:16:54 +0530 | [diff] [blame] | 465 | addToPendingAudit(groupDesc); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 466 | return; |
| 467 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 468 | GroupStoreMessage groupOp = GroupStoreMessage. |
| 469 | createGroupAddRequestMsg(groupDesc.deviceId(), |
| 470 | groupDesc); |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 471 | |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 472 | clusterCommunicator.unicast(groupOp, |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 473 | GroupStoreMessageSubjects.REMOTE_GROUP_OP_REQUEST, |
| 474 | clusterMsgSerializer::serialize, |
| 475 | mastershipService.getMasterFor(groupDesc.deviceId())) |
| 476 | .whenComplete((result, error) -> { |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 477 | if (error != null) { |
| 478 | log.warn("Failed to send request to master: {} to {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 479 | groupOp, |
| 480 | mastershipService.getMasterFor(groupDesc.deviceId())); |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 481 | //TODO: Send Group operation failure event |
| 482 | } else { |
| 483 | log.debug("Sent Group operation request for device {} " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 484 | + "to remote MASTER {}", |
| 485 | groupDesc.deviceId(), |
| 486 | mastershipService.getMasterFor(groupDesc.deviceId())); |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 487 | } |
| 488 | }); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 489 | return; |
| 490 | } |
| 491 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 492 | log.debug("Store group for device {} is getting handled locally", |
| 493 | groupDesc.deviceId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 494 | storeGroupDescriptionInternal(groupDesc); |
| 495 | } |
| 496 | |
Sivachidambaram Subramanian | 9f816de | 2017-06-13 07:16:54 +0530 | [diff] [blame] | 497 | private void addToPendingAudit(GroupDescription groupDesc) { |
| 498 | Integer groupIdVal = groupDesc.givenGroupId(); |
| 499 | GroupId groupId = (groupIdVal != null) ? new GroupId(groupIdVal) : dummyGroupId; |
| 500 | addToPendingKeyTable(new DefaultGroup(groupId, groupDesc)); |
| 501 | } |
| 502 | |
| 503 | private void addToPendingKeyTable(StoredGroupEntry group) { |
| 504 | group.setState(GroupState.WAITING_AUDIT_COMPLETE); |
| 505 | Map<GroupStoreKeyMapKey, StoredGroupEntry> pendingKeyTable = |
| 506 | getPendingGroupKeyTable(); |
| 507 | pendingKeyTable.put(new GroupStoreKeyMapKey(group.deviceId(), |
| 508 | group.appCookie()), |
| 509 | group); |
| 510 | } |
| 511 | |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 512 | private Group getMatchingExtraneousGroupbyId(DeviceId deviceId, Integer groupId) { |
| 513 | ConcurrentMap<GroupId, Group> extraneousMap = |
| 514 | extraneousGroupEntriesById.get(deviceId); |
| 515 | if (extraneousMap == null) { |
| 516 | return null; |
| 517 | } |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 518 | return extraneousMap.get(new GroupId(groupId)); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | private Group getMatchingExtraneousGroupbyBuckets(DeviceId deviceId, |
| 522 | GroupBuckets buckets) { |
| 523 | ConcurrentMap<GroupId, Group> extraneousMap = |
| 524 | extraneousGroupEntriesById.get(deviceId); |
| 525 | if (extraneousMap == null) { |
| 526 | return null; |
| 527 | } |
| 528 | |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 529 | for (Group extraneousGroup : extraneousMap.values()) { |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 530 | if (extraneousGroup.buckets().equals(buckets)) { |
| 531 | return extraneousGroup; |
| 532 | } |
| 533 | } |
| 534 | return null; |
| 535 | } |
| 536 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 537 | private void storeGroupDescriptionInternal(GroupDescription groupDesc) { |
| 538 | // Check if a group is existing with the same key |
| 539 | if (getGroup(groupDesc.deviceId(), groupDesc.appCookie()) != null) { |
| 540 | return; |
| 541 | } |
| 542 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 543 | if (deviceAuditStatus.get(groupDesc.deviceId()) == null) { |
| 544 | // Device group audit has not completed yet |
| 545 | // Add this group description to pending group key table |
| 546 | // Create a group entry object with Dummy Group ID |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 547 | log.debug("storeGroupDescriptionInternal: Device {} AUDIT pending...Queuing Group ADD request", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 548 | groupDesc.deviceId()); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 549 | StoredGroupEntry group = new DefaultGroup(dummyGroupId, groupDesc); |
| 550 | group.setState(GroupState.WAITING_AUDIT_COMPLETE); |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 551 | Map<GroupStoreKeyMapKey, StoredGroupEntry> pendingKeyTable = |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 552 | getPendingGroupKeyTable(); |
| 553 | pendingKeyTable.put(new GroupStoreKeyMapKey(groupDesc.deviceId(), |
| 554 | groupDesc.appCookie()), |
| 555 | group); |
| 556 | return; |
| 557 | } |
| 558 | |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 559 | Group matchingExtraneousGroup = null; |
| 560 | if (groupDesc.givenGroupId() != null) { |
| 561 | //Check if there is a extraneous group existing with the same Id |
| 562 | matchingExtraneousGroup = getMatchingExtraneousGroupbyId( |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 563 | groupDesc.deviceId(), groupDesc.givenGroupId()); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 564 | if (matchingExtraneousGroup != null) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 565 | log.debug("storeGroupDescriptionInternal: Matching extraneous group " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 566 | + "found in Device {} for group id 0x{}", |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 567 | groupDesc.deviceId(), |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 568 | Integer.toHexString(groupDesc.givenGroupId())); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 569 | //Check if the group buckets matches with user provided buckets |
| 570 | if (matchingExtraneousGroup.buckets().equals(groupDesc.buckets())) { |
| 571 | //Group is already existing with the same buckets and Id |
| 572 | // Create a group entry object |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 573 | log.debug("storeGroupDescriptionInternal: Buckets also matching " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 574 | + "in Device {} for group id 0x{}", |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 575 | groupDesc.deviceId(), |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 576 | Integer.toHexString(groupDesc.givenGroupId())); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 577 | StoredGroupEntry group = new DefaultGroup( |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 578 | matchingExtraneousGroup.id(), groupDesc); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 579 | // Insert the newly created group entry into key and id maps |
| 580 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 581 | put(new GroupStoreKeyMapKey(groupDesc.deviceId(), |
| 582 | groupDesc.appCookie()), group); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 583 | // Ensure it also inserted into group id based table to |
| 584 | // avoid any chances of duplication in group id generation |
| 585 | getGroupIdTable(groupDesc.deviceId()). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 586 | put(matchingExtraneousGroup.id(), group); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 587 | addOrUpdateGroupEntry(matchingExtraneousGroup); |
| 588 | removeExtraneousGroupEntry(matchingExtraneousGroup); |
| 589 | return; |
| 590 | } else { |
| 591 | //Group buckets are not matching. Update group |
| 592 | //with user provided buckets. |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 593 | log.debug("storeGroupDescriptionInternal: Buckets are not " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 594 | + "matching in Device {} for group id 0x{}", |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 595 | groupDesc.deviceId(), |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 596 | Integer.toHexString(groupDesc.givenGroupId())); |
| 597 | StoredGroupEntry modifiedGroup = new DefaultGroup( |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 598 | matchingExtraneousGroup.id(), groupDesc); |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 599 | modifiedGroup.setState(GroupState.PENDING_UPDATE); |
| 600 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 601 | put(new GroupStoreKeyMapKey(groupDesc.deviceId(), |
| 602 | groupDesc.appCookie()), modifiedGroup); |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 603 | // Ensure it also inserted into group id based table to |
| 604 | // avoid any chances of duplication in group id generation |
| 605 | getGroupIdTable(groupDesc.deviceId()). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 606 | put(matchingExtraneousGroup.id(), modifiedGroup); |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 607 | removeExtraneousGroupEntry(matchingExtraneousGroup); |
| 608 | log.debug("storeGroupDescriptionInternal: Triggering Group " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 609 | + "UPDATE request for {} in device {}", |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 610 | matchingExtraneousGroup.id(), |
| 611 | groupDesc.deviceId()); |
| 612 | notifyDelegate(new GroupEvent(Type.GROUP_UPDATE_REQUESTED, modifiedGroup)); |
| 613 | return; |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | } else { |
| 617 | //Check if there is an extraneous group with user provided buckets |
| 618 | matchingExtraneousGroup = getMatchingExtraneousGroupbyBuckets( |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 619 | groupDesc.deviceId(), groupDesc.buckets()); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 620 | if (matchingExtraneousGroup != null) { |
| 621 | //Group is already existing with the same buckets. |
| 622 | //So reuse this group. |
| 623 | log.debug("storeGroupDescriptionInternal: Matching extraneous group found in Device {}", |
| 624 | groupDesc.deviceId()); |
| 625 | //Create a group entry object |
| 626 | StoredGroupEntry group = new DefaultGroup( |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 627 | matchingExtraneousGroup.id(), groupDesc); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 628 | // Insert the newly created group entry into key and id maps |
| 629 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 630 | put(new GroupStoreKeyMapKey(groupDesc.deviceId(), |
| 631 | groupDesc.appCookie()), group); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 632 | // Ensure it also inserted into group id based table to |
| 633 | // avoid any chances of duplication in group id generation |
| 634 | getGroupIdTable(groupDesc.deviceId()). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 635 | put(matchingExtraneousGroup.id(), group); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 636 | addOrUpdateGroupEntry(matchingExtraneousGroup); |
| 637 | removeExtraneousGroupEntry(matchingExtraneousGroup); |
| 638 | return; |
| 639 | } else { |
| 640 | //TODO: Check if there are any empty groups that can be used here |
| 641 | log.debug("storeGroupDescriptionInternal: No matching extraneous groups found in Device {}", |
| 642 | groupDesc.deviceId()); |
| 643 | } |
| 644 | } |
| 645 | |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 646 | GroupId id = null; |
| 647 | if (groupDesc.givenGroupId() == null) { |
| 648 | // Get a new group identifier |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 649 | id = new GroupId(getFreeGroupIdValue(groupDesc.deviceId())); |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 650 | } else { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 651 | // we need to use the identifier passed in by caller, but check if |
| 652 | // already used |
| 653 | Group existing = getGroup(groupDesc.deviceId(), |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 654 | new GroupId(groupDesc.givenGroupId())); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 655 | if (existing != null) { |
| 656 | log.warn("Group already exists with the same id: 0x{} in dev:{} " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 657 | + "but with different key: {} (request gkey: {})", |
| 658 | Integer.toHexString(groupDesc.givenGroupId()), |
| 659 | groupDesc.deviceId(), |
| 660 | existing.appCookie(), |
| 661 | groupDesc.appCookie()); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 662 | return; |
| 663 | } |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 664 | id = new GroupId(groupDesc.givenGroupId()); |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 665 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 666 | // Create a group entry object |
| 667 | StoredGroupEntry group = new DefaultGroup(id, groupDesc); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 668 | // Insert the newly created group entry into key and id maps |
| 669 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 670 | put(new GroupStoreKeyMapKey(groupDesc.deviceId(), |
| 671 | groupDesc.appCookie()), group); |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 672 | // Ensure it also inserted into group id based table to |
| 673 | // avoid any chances of duplication in group id generation |
| 674 | getGroupIdTable(groupDesc.deviceId()). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 675 | put(id, group); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 676 | log.debug("storeGroupDescriptionInternal: Processing Group ADD request for Id {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 677 | id, |
| 678 | groupDesc.deviceId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 679 | notifyDelegate(new GroupEvent(GroupEvent.Type.GROUP_ADD_REQUESTED, |
| 680 | group)); |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * Updates the existing group entry with the information |
| 685 | * from group description. |
| 686 | * |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 687 | * @param deviceId the device ID |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 688 | * @param oldAppCookie the current group key |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 689 | * @param type update type |
| 690 | * @param newBuckets group buckets for updates |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 691 | * @param newAppCookie optional new group key |
| 692 | */ |
| 693 | @Override |
| 694 | public void updateGroupDescription(DeviceId deviceId, |
| 695 | GroupKey oldAppCookie, |
| 696 | UpdateType type, |
| 697 | GroupBuckets newBuckets, |
| 698 | GroupKey newAppCookie) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 699 | // Check if group update to be done by a remote instance |
sangho | 52abe3a | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 700 | if (mastershipService.getMasterFor(deviceId) != null && |
| 701 | mastershipService.getLocalRole(deviceId) != MastershipRole.MASTER) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 702 | log.debug("updateGroupDescription: Device {} local role is not MASTER", |
| 703 | deviceId); |
| 704 | if (mastershipService.getMasterFor(deviceId) == null) { |
| 705 | log.error("No Master for device {}..." |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 706 | + "Can not perform update group operation", |
| 707 | deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 708 | //TODO: Send Group operation failure event |
| 709 | return; |
| 710 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 711 | GroupStoreMessage groupOp = GroupStoreMessage. |
| 712 | createGroupUpdateRequestMsg(deviceId, |
| 713 | oldAppCookie, |
| 714 | type, |
| 715 | newBuckets, |
| 716 | newAppCookie); |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 717 | |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 718 | clusterCommunicator.unicast(groupOp, |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 719 | GroupStoreMessageSubjects.REMOTE_GROUP_OP_REQUEST, |
| 720 | clusterMsgSerializer::serialize, |
| 721 | mastershipService.getMasterFor(deviceId)).whenComplete((result, error) -> { |
| 722 | if (error != null) { |
| 723 | log.warn("Failed to send request to master: {} to {}", |
| 724 | groupOp, |
| 725 | mastershipService.getMasterFor(deviceId), error); |
| 726 | } |
| 727 | //TODO: Send Group operation failure event |
| 728 | }); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 729 | return; |
| 730 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 731 | log.debug("updateGroupDescription for device {} is getting handled locally", |
| 732 | deviceId); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 733 | updateGroupDescriptionInternal(deviceId, |
| 734 | oldAppCookie, |
| 735 | type, |
| 736 | newBuckets, |
| 737 | newAppCookie); |
| 738 | } |
| 739 | |
| 740 | private void updateGroupDescriptionInternal(DeviceId deviceId, |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 741 | GroupKey oldAppCookie, |
| 742 | UpdateType type, |
| 743 | GroupBuckets newBuckets, |
| 744 | GroupKey newAppCookie) { |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 745 | // Check if a group is existing with the provided key |
| 746 | Group oldGroup = getGroup(deviceId, oldAppCookie); |
| 747 | if (oldGroup == null) { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 748 | log.warn("updateGroupDescriptionInternal: Group not found...strange. " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 749 | + "GroupKey:{} DeviceId:{}", oldAppCookie, deviceId); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 750 | return; |
| 751 | } |
| 752 | |
| 753 | List<GroupBucket> newBucketList = getUpdatedBucketList(oldGroup, |
| 754 | type, |
| 755 | newBuckets); |
| 756 | if (newBucketList != null) { |
| 757 | // Create a new group object from the old group |
| 758 | GroupBuckets updatedBuckets = new GroupBuckets(newBucketList); |
| 759 | GroupKey newCookie = (newAppCookie != null) ? newAppCookie : oldAppCookie; |
| 760 | GroupDescription updatedGroupDesc = new DefaultGroupDescription( |
| 761 | oldGroup.deviceId(), |
| 762 | oldGroup.type(), |
| 763 | updatedBuckets, |
| 764 | newCookie, |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 765 | oldGroup.givenGroupId(), |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 766 | oldGroup.appId()); |
| 767 | StoredGroupEntry newGroup = new DefaultGroup(oldGroup.id(), |
| 768 | updatedGroupDesc); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 769 | log.debug("updateGroupDescriptionInternal: group entry {} in device {} moving from {} to PENDING_UPDATE", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 770 | oldGroup.id(), |
| 771 | oldGroup.deviceId(), |
| 772 | oldGroup.state()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 773 | newGroup.setState(GroupState.PENDING_UPDATE); |
| 774 | newGroup.setLife(oldGroup.life()); |
| 775 | newGroup.setPackets(oldGroup.packets()); |
| 776 | newGroup.setBytes(oldGroup.bytes()); |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 777 | //Update the group entry in groupkey based map. |
| 778 | //Update to groupid based map will happen in the |
| 779 | //groupkey based map update listener |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 780 | log.debug("updateGroupDescriptionInternal with type {}: Group updated with buckets", |
| 781 | type); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 782 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 783 | put(new GroupStoreKeyMapKey(newGroup.deviceId(), |
| 784 | newGroup.appCookie()), newGroup); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 785 | notifyDelegate(new GroupEvent(Type.GROUP_UPDATE_REQUESTED, newGroup)); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 786 | } else { |
| 787 | log.warn("updateGroupDescriptionInternal with type {}: No " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 788 | + "change in the buckets in update", type); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
| 792 | private List<GroupBucket> getUpdatedBucketList(Group oldGroup, |
| 793 | UpdateType type, |
| 794 | GroupBuckets buckets) { |
Victor Silva | 0282ab8 | 2016-11-15 16:30:27 -0300 | [diff] [blame] | 795 | if (type == UpdateType.SET) { |
| 796 | return buckets.buckets(); |
| 797 | } |
| 798 | |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame] | 799 | List<GroupBucket> oldBuckets = oldGroup.buckets().buckets(); |
| 800 | List<GroupBucket> updatedBucketList = new ArrayList<>(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 801 | boolean groupDescUpdated = false; |
| 802 | |
| 803 | if (type == UpdateType.ADD) { |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame] | 804 | List<GroupBucket> newBuckets = buckets.buckets(); |
| 805 | |
| 806 | // Add old buckets that will not be updated and check if any will be updated. |
| 807 | for (GroupBucket oldBucket : oldBuckets) { |
| 808 | int newBucketIndex = newBuckets.indexOf(oldBucket); |
| 809 | |
| 810 | if (newBucketIndex != -1) { |
| 811 | GroupBucket newBucket = newBuckets.get(newBucketIndex); |
| 812 | if (!newBucket.hasSameParameters(oldBucket)) { |
| 813 | // Bucket will be updated |
| 814 | groupDescUpdated = true; |
| 815 | } |
| 816 | } else { |
| 817 | // Old bucket will remain the same - add it. |
| 818 | updatedBucketList.add(oldBucket); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 819 | } |
| 820 | } |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame] | 821 | |
| 822 | // Add all new buckets |
| 823 | updatedBucketList.addAll(newBuckets); |
| 824 | if (!oldBuckets.containsAll(newBuckets)) { |
| 825 | groupDescUpdated = true; |
| 826 | } |
| 827 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 828 | } else if (type == UpdateType.REMOVE) { |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame] | 829 | List<GroupBucket> bucketsToRemove = buckets.buckets(); |
| 830 | |
| 831 | // Check which old buckets should remain |
| 832 | for (GroupBucket oldBucket : oldBuckets) { |
| 833 | if (!bucketsToRemove.contains(oldBucket)) { |
| 834 | updatedBucketList.add(oldBucket); |
| 835 | } else { |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 836 | groupDescUpdated = true; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | if (groupDescUpdated) { |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame] | 842 | return updatedBucketList; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 843 | } else { |
| 844 | return null; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * Triggers deleting the existing group entry. |
| 850 | * |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 851 | * @param deviceId the device ID |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 852 | * @param appCookie the group key |
| 853 | */ |
| 854 | @Override |
| 855 | public void deleteGroupDescription(DeviceId deviceId, |
| 856 | GroupKey appCookie) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 857 | // Check if group to be deleted by a remote instance |
| 858 | if (mastershipService. |
| 859 | getLocalRole(deviceId) != MastershipRole.MASTER) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 860 | log.debug("deleteGroupDescription: Device {} local role is not MASTER", |
| 861 | deviceId); |
| 862 | if (mastershipService.getMasterFor(deviceId) == null) { |
| 863 | log.error("No Master for device {}..." |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 864 | + "Can not perform delete group operation", |
| 865 | deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 866 | //TODO: Send Group operation failure event |
| 867 | return; |
| 868 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 869 | GroupStoreMessage groupOp = GroupStoreMessage. |
| 870 | createGroupDeleteRequestMsg(deviceId, |
| 871 | appCookie); |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 872 | |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 873 | clusterCommunicator.unicast(groupOp, |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 874 | GroupStoreMessageSubjects.REMOTE_GROUP_OP_REQUEST, |
| 875 | clusterMsgSerializer::serialize, |
| 876 | mastershipService.getMasterFor(deviceId)).whenComplete((result, error) -> { |
| 877 | if (error != null) { |
| 878 | log.warn("Failed to send request to master: {} to {}", |
| 879 | groupOp, |
| 880 | mastershipService.getMasterFor(deviceId), error); |
| 881 | } |
| 882 | //TODO: Send Group operation failure event |
| 883 | }); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 884 | return; |
| 885 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 886 | log.debug("deleteGroupDescription in device {} is getting handled locally", |
| 887 | deviceId); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 888 | deleteGroupDescriptionInternal(deviceId, appCookie); |
| 889 | } |
| 890 | |
| 891 | private void deleteGroupDescriptionInternal(DeviceId deviceId, |
| 892 | GroupKey appCookie) { |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 893 | // Check if a group is existing with the provided key |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 894 | StoredGroupEntry existing = getStoredGroupEntry(deviceId, appCookie); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 895 | if (existing == null) { |
| 896 | return; |
| 897 | } |
| 898 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 899 | log.debug("deleteGroupDescriptionInternal: group entry {} in device {} moving from {} to PENDING_DELETE", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 900 | existing.id(), |
| 901 | existing.deviceId(), |
| 902 | existing.state()); |
pier | 1e80f36 | 2020-04-03 12:44:21 +0200 | [diff] [blame] | 903 | // TODO is this really safe ? |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 904 | synchronized (existing) { |
| 905 | existing.setState(GroupState.PENDING_DELETE); |
Saurav Das | 80980c7 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 906 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 907 | put(new GroupStoreKeyMapKey(existing.deviceId(), existing.appCookie()), |
| 908 | existing); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 909 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 910 | log.debug("deleteGroupDescriptionInternal: in device {} issuing GROUP_REMOVE_REQUESTED", |
| 911 | deviceId); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 912 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVE_REQUESTED, existing)); |
| 913 | } |
| 914 | |
| 915 | /** |
pier | 1e80f36 | 2020-04-03 12:44:21 +0200 | [diff] [blame] | 916 | * Updates the stats of an existing group entry. |
| 917 | * |
| 918 | * @param group the new stats |
| 919 | * @param existing the existing group |
| 920 | */ |
| 921 | private void updateGroupEntryStatsInternal(Group group, StoredGroupEntry existing) { |
| 922 | for (GroupBucket bucket : group.buckets().buckets()) { |
| 923 | Optional<GroupBucket> matchingBucket = |
| 924 | existing.buckets().buckets() |
| 925 | .stream() |
| 926 | .filter((existingBucket) -> (existingBucket.equals(bucket))) |
| 927 | .findFirst(); |
| 928 | if (matchingBucket.isPresent()) { |
| 929 | ((StoredGroupBucketEntry) matchingBucket. |
| 930 | get()).setPackets(bucket.packets()); |
| 931 | ((StoredGroupBucketEntry) matchingBucket. |
| 932 | get()).setBytes(bucket.bytes()); |
| 933 | } else { |
| 934 | log.warn("updateGroupEntryStatsInternal: No matching bucket {}" + |
| 935 | " to update stats", bucket); |
| 936 | } |
| 937 | } |
| 938 | existing.setLife(group.life()); |
| 939 | existing.setPackets(group.packets()); |
| 940 | existing.setBytes(group.bytes()); |
| 941 | existing.setReferenceCount(group.referenceCount()); |
| 942 | existing.setFailedRetryCount(0); |
| 943 | } |
| 944 | |
| 945 | /** |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 946 | * Stores a new group entry, or updates an existing entry. |
| 947 | * |
| 948 | * @param group group entry |
| 949 | */ |
| 950 | @Override |
| 951 | public void addOrUpdateGroupEntry(Group group) { |
| 952 | // check if this new entry is an update to an existing entry |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 953 | StoredGroupEntry existing = getStoredGroupEntry(group.deviceId(), |
| 954 | group.id()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 955 | GroupEvent event = null; |
| 956 | |
| 957 | if (existing != null) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 958 | log.trace("addOrUpdateGroupEntry: updating group entry {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 959 | group.id(), |
| 960 | group.deviceId()); |
pier | 1e80f36 | 2020-04-03 12:44:21 +0200 | [diff] [blame] | 961 | // TODO is this really safe ? |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 962 | synchronized (existing) { |
pier | 1e80f36 | 2020-04-03 12:44:21 +0200 | [diff] [blame] | 963 | // Update stats |
| 964 | updateGroupEntryStatsInternal(group, existing); |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 965 | if ((existing.state() == GroupState.PENDING_ADD) || |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 966 | (existing.state() == GroupState.PENDING_ADD_RETRY)) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 967 | log.trace("addOrUpdateGroupEntry: group entry {} in device {} moving from {} to ADDED", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 968 | existing.id(), |
| 969 | existing.deviceId(), |
| 970 | existing.state()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 971 | existing.setState(GroupState.ADDED); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 972 | existing.setIsGroupStateAddedFirstTime(true); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 973 | event = new GroupEvent(Type.GROUP_ADDED, existing); |
| 974 | } else { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 975 | log.trace("addOrUpdateGroupEntry: group entry {} in device {} moving from {} to ADDED", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 976 | existing.id(), |
| 977 | existing.deviceId(), |
| 978 | GroupState.PENDING_UPDATE); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 979 | existing.setState(GroupState.ADDED); |
| 980 | existing.setIsGroupStateAddedFirstTime(false); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 981 | event = new GroupEvent(Type.GROUP_UPDATED, existing); |
| 982 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 983 | //Re-PUT map entries to trigger map update events |
| 984 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 985 | put(new GroupStoreKeyMapKey(existing.deviceId(), |
| 986 | existing.appCookie()), existing); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 987 | } |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 988 | } else { |
pier | 1e80f36 | 2020-04-03 12:44:21 +0200 | [diff] [blame] | 989 | log.warn("addOrUpdateGroupEntry: Group update {} " + |
| 990 | "happening for a non-existing entry in the map", group); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 991 | } |
| 992 | |
pier | 1e80f36 | 2020-04-03 12:44:21 +0200 | [diff] [blame] | 993 | // TODO if map is going to trigger event, is this one needed? |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 994 | if (event != null) { |
| 995 | notifyDelegate(event); |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | /** |
pier | 1e80f36 | 2020-04-03 12:44:21 +0200 | [diff] [blame] | 1000 | * Updates stats of an existing entry. |
| 1001 | * |
| 1002 | * @param group group entry |
| 1003 | */ |
| 1004 | private void updateGroupEntryStats(Group group) { |
| 1005 | // check if this new entry is an update to an existing entry |
| 1006 | StoredGroupEntry existing = getStoredGroupEntry(group.deviceId(), |
| 1007 | group.id()); |
| 1008 | if (existing != null) { |
| 1009 | log.trace("updateStatsGroupEntry: updating group entry {} in device {}", |
| 1010 | group.id(), |
| 1011 | group.deviceId()); |
| 1012 | // TODO is this really safe ? |
| 1013 | synchronized (existing) { |
| 1014 | // We don't make further update - it will be gone after the next update |
| 1015 | if (existing.state() == GroupState.PENDING_DELETE) { |
| 1016 | log.trace("updateStatsGroupEntry: group entry {} in device {} is in {} not updated", |
| 1017 | existing.id(), |
| 1018 | existing.deviceId(), |
| 1019 | existing.state()); |
| 1020 | return; |
| 1021 | } |
| 1022 | // Update stats |
| 1023 | updateGroupEntryStatsInternal(group, existing); |
| 1024 | if ((existing.state() == GroupState.PENDING_ADD) || |
| 1025 | (existing.state() == GroupState.PENDING_ADD_RETRY)) { |
| 1026 | log.trace("updateStatsGroupEntry: group entry {} in device {} moving from {} to ADDED", |
| 1027 | existing.id(), |
| 1028 | existing.deviceId(), |
| 1029 | existing.state()); |
| 1030 | existing.setState(GroupState.ADDED); |
| 1031 | existing.setIsGroupStateAddedFirstTime(true); |
| 1032 | } else { |
| 1033 | log.trace("updateStatsGroupEntry: group entry {} in device {} moving from {} to ADDED", |
| 1034 | existing.id(), |
| 1035 | existing.deviceId(), |
| 1036 | GroupState.PENDING_UPDATE); |
| 1037 | existing.setState(GroupState.ADDED); |
| 1038 | existing.setIsGroupStateAddedFirstTime(false); |
| 1039 | } |
| 1040 | //Re-PUT map entries to trigger map update events |
| 1041 | getGroupStoreKeyMap(). |
| 1042 | put(new GroupStoreKeyMapKey(existing.deviceId(), |
| 1043 | existing.appCookie()), existing); |
| 1044 | } |
| 1045 | } else { |
| 1046 | log.warn("updateStatsGroupEntry: Group update {} " |
| 1047 | + "happening for a non-existing entry in the map", group); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | /** |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1052 | * Removes the group entry from store. |
| 1053 | * |
| 1054 | * @param group group entry |
| 1055 | */ |
| 1056 | @Override |
| 1057 | public void removeGroupEntry(Group group) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1058 | StoredGroupEntry existing = getStoredGroupEntry(group.deviceId(), |
| 1059 | group.id()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1060 | |
| 1061 | if (existing != null) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1062 | log.debug("removeGroupEntry: removing group entry {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1063 | group.id(), |
| 1064 | group.deviceId()); |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 1065 | //Removal from groupid based map will happen in the |
| 1066 | //map update listener |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1067 | getGroupStoreKeyMap().remove(new GroupStoreKeyMapKey(existing.deviceId(), |
| 1068 | existing.appCookie())); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1069 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVED, existing)); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1070 | } else { |
| 1071 | log.warn("removeGroupEntry for {} in device{} is " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1072 | + "not existing in our maps", |
| 1073 | group.id(), |
| 1074 | group.deviceId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
Victor Silva | 4e8b783 | 2016-08-17 17:11:19 -0300 | [diff] [blame] | 1078 | private void purgeGroupEntries(Set<Entry<GroupStoreKeyMapKey, StoredGroupEntry>> entries) { |
| 1079 | entries.forEach(entry -> { |
| 1080 | groupStoreEntriesByKey.remove(entry.getKey()); |
| 1081 | }); |
| 1082 | } |
| 1083 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1084 | @Override |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1085 | public void purgeGroupEntry(DeviceId deviceId) { |
Victor Silva | 4e8b783 | 2016-08-17 17:11:19 -0300 | [diff] [blame] | 1086 | Set<Entry<GroupStoreKeyMapKey, StoredGroupEntry>> entriesPendingRemove = |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1087 | new HashSet<>(); |
| 1088 | |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1089 | getGroupStoreKeyMap().entrySet().stream() |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1090 | .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
Victor Silva | 4e8b783 | 2016-08-17 17:11:19 -0300 | [diff] [blame] | 1091 | .forEach(entriesPendingRemove::add); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1092 | |
Victor Silva | 4e8b783 | 2016-08-17 17:11:19 -0300 | [diff] [blame] | 1093 | purgeGroupEntries(entriesPendingRemove); |
| 1094 | } |
| 1095 | |
| 1096 | @Override |
Daniele Moro | 43ac289 | 2021-07-15 17:02:59 +0200 | [diff] [blame^] | 1097 | public void purgeGroupEntries(DeviceId deviceId, ApplicationId appId) { |
| 1098 | Set<Entry<GroupStoreKeyMapKey, StoredGroupEntry>> entriesPendingRemove = |
| 1099 | new HashSet<>(); |
| 1100 | |
| 1101 | getGroupStoreKeyMap().entrySet().stream() |
| 1102 | .filter(entry -> entry.getKey().deviceId().equals(deviceId) && entry.getValue().appId().equals(appId)) |
| 1103 | .forEach(entriesPendingRemove::add); |
| 1104 | |
| 1105 | purgeGroupEntries(entriesPendingRemove); |
| 1106 | } |
| 1107 | |
| 1108 | @Override |
Victor Silva | 4e8b783 | 2016-08-17 17:11:19 -0300 | [diff] [blame] | 1109 | public void purgeGroupEntries() { |
| 1110 | purgeGroupEntries(getGroupStoreKeyMap().entrySet()); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | @Override |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1114 | public void deviceInitialAuditCompleted(DeviceId deviceId, |
| 1115 | boolean completed) { |
| 1116 | synchronized (deviceAuditStatus) { |
| 1117 | if (completed) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1118 | log.debug("AUDIT completed for device {}", |
| 1119 | deviceId); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1120 | deviceAuditStatus.put(deviceId, true); |
| 1121 | // Execute all pending group requests |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1122 | List<StoredGroupEntry> pendingGroupRequests = |
| 1123 | getPendingGroupKeyTable().values() |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1124 | .stream() |
| 1125 | .filter(g -> g.deviceId().equals(deviceId)) |
| 1126 | .collect(Collectors.toList()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1127 | log.debug("processing pending group add requests for device {} and number of pending requests {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1128 | deviceId, |
| 1129 | pendingGroupRequests.size()); |
| 1130 | for (Group group : pendingGroupRequests) { |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1131 | GroupDescription tmp = new DefaultGroupDescription( |
| 1132 | group.deviceId(), |
| 1133 | group.type(), |
| 1134 | group.buckets(), |
| 1135 | group.appCookie(), |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 1136 | group.givenGroupId(), |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1137 | group.appId()); |
| 1138 | storeGroupDescriptionInternal(tmp); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1139 | getPendingGroupKeyTable(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1140 | remove(new GroupStoreKeyMapKey(deviceId, group.appCookie())); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1141 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1142 | } else { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 1143 | Boolean audited = deviceAuditStatus.get(deviceId); |
| 1144 | if (audited != null && audited) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1145 | log.debug("Clearing AUDIT status for device {}", deviceId); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1146 | deviceAuditStatus.put(deviceId, false); |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | @Override |
| 1153 | public boolean deviceInitialAuditStatus(DeviceId deviceId) { |
| 1154 | synchronized (deviceAuditStatus) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 1155 | Boolean audited = deviceAuditStatus.get(deviceId); |
| 1156 | return audited != null && audited; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | @Override |
| 1161 | public void groupOperationFailed(DeviceId deviceId, GroupOperation operation) { |
| 1162 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1163 | StoredGroupEntry existing = getStoredGroupEntry(deviceId, |
| 1164 | operation.groupId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1165 | |
| 1166 | if (existing == null) { |
| 1167 | log.warn("No group entry with ID {} found ", operation.groupId()); |
| 1168 | return; |
| 1169 | } |
| 1170 | |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1171 | log.warn("groupOperationFailed: group operation {} failed in state {} " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1172 | + "for group {} in device {} with code {}", |
| 1173 | operation.opType(), |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1174 | existing.state(), |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1175 | existing.id(), |
| 1176 | existing.deviceId(), |
| 1177 | operation.failureCode()); |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1178 | if (operation.failureCode() == GroupOperation.GroupMsgErrorCode.GROUP_EXISTS) { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1179 | if (operation.buckets().equals(existing.buckets())) { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1180 | if (existing.state() == GroupState.PENDING_ADD || |
| 1181 | existing.state() == GroupState.PENDING_ADD_RETRY) { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1182 | log.info("GROUP_EXISTS: GroupID and Buckets match for group in pending " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1183 | + "add state - moving to ADDED for group {} in device {}", |
| 1184 | existing.id(), deviceId); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1185 | addOrUpdateGroupEntry(existing); |
| 1186 | return; |
| 1187 | } else { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1188 | log.warn("GROUP_EXISTS: GroupId and Buckets match but existing" |
| 1189 | + "group in state: {}", existing.state()); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1190 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1191 | } else { |
| 1192 | log.warn("GROUP EXISTS: Group ID matched but buckets did not. " |
| 1193 | + "Operation: {} Existing: {}", operation.buckets(), |
| 1194 | existing.buckets()); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1195 | } |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1196 | } |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1197 | if (operation.failureCode() == GroupOperation.GroupMsgErrorCode.INVALID_GROUP) { |
| 1198 | existing.incrFailedRetryCount(); |
| 1199 | if (existing.failedRetryCount() < MAX_FAILED_ATTEMPTS) { |
| 1200 | log.warn("Group {} programming failed {} of {} times in dev {}, " |
| 1201 | + "retrying ..", existing.id(), |
| 1202 | existing.failedRetryCount(), MAX_FAILED_ATTEMPTS, |
| 1203 | deviceId); |
| 1204 | return; |
| 1205 | } |
| 1206 | log.warn("Group {} programming failed {} of {} times in dev {}, " |
| 1207 | + "removing group from store", existing.id(), |
| 1208 | existing.failedRetryCount(), MAX_FAILED_ATTEMPTS, |
| 1209 | deviceId); |
| 1210 | // fall through to case |
| 1211 | } |
| 1212 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1213 | switch (operation.opType()) { |
| 1214 | case ADD: |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1215 | if (existing.state() == GroupState.PENDING_ADD |
| 1216 | || existing.state() == GroupState.PENDING_ADD_RETRY) { |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1217 | notifyDelegate(new GroupEvent(Type.GROUP_ADD_FAILED, existing)); |
| 1218 | log.warn("groupOperationFailed: cleaningup " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1219 | + "group {} from store in device {}....", |
| 1220 | existing.id(), |
| 1221 | existing.deviceId()); |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1222 | //Removal from groupid based map will happen in the |
| 1223 | //map update listener |
| 1224 | getGroupStoreKeyMap().remove(new GroupStoreKeyMapKey(existing.deviceId(), |
| 1225 | existing.appCookie())); |
| 1226 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1227 | break; |
| 1228 | case MODIFY: |
| 1229 | notifyDelegate(new GroupEvent(Type.GROUP_UPDATE_FAILED, existing)); |
| 1230 | break; |
| 1231 | case DELETE: |
| 1232 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVE_FAILED, existing)); |
| 1233 | break; |
| 1234 | default: |
| 1235 | log.warn("Unknown group operation type {}", operation.opType()); |
| 1236 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | @Override |
| 1240 | public void addOrUpdateExtraneousGroupEntry(Group group) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1241 | log.debug("add/update extraneous group entry {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1242 | group.id(), |
| 1243 | group.deviceId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1244 | ConcurrentMap<GroupId, Group> extraneousIdTable = |
| 1245 | getExtraneousGroupIdTable(group.deviceId()); |
| 1246 | extraneousIdTable.put(group.id(), group); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 1247 | // Don't remove the extraneous groups, instead re-use it when |
| 1248 | // a group request comes with the same set of buckets |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | @Override |
| 1252 | public void removeExtraneousGroupEntry(Group group) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1253 | log.debug("remove extraneous group entry {} of device {} from store", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1254 | group.id(), |
| 1255 | group.deviceId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1256 | ConcurrentMap<GroupId, Group> extraneousIdTable = |
| 1257 | getExtraneousGroupIdTable(group.deviceId()); |
| 1258 | extraneousIdTable.remove(group.id()); |
| 1259 | } |
| 1260 | |
| 1261 | @Override |
| 1262 | public Iterable<Group> getExtraneousGroups(DeviceId deviceId) { |
| 1263 | // flatten and make iterator unmodifiable |
| 1264 | return FluentIterable.from( |
| 1265 | getExtraneousGroupIdTable(deviceId).values()); |
| 1266 | } |
| 1267 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1268 | /** |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 1269 | * 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] | 1270 | */ |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 1271 | private class GroupStoreKeyMapListener implements |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1272 | MapEventListener<GroupStoreKeyMapKey, StoredGroupEntry> { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1273 | |
| 1274 | @Override |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1275 | public void event(MapEvent<GroupStoreKeyMapKey, StoredGroupEntry> mapEvent) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1276 | GroupEvent groupEvent = null; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1277 | GroupStoreKeyMapKey key = mapEvent.key(); |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1278 | StoredGroupEntry group = Versioned.valueOrNull(mapEvent.newValue()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1279 | if ((key == null) && (group == null)) { |
| 1280 | log.error("GroupStoreKeyMapListener: Received " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1281 | + "event {} with null entry", mapEvent.type()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1282 | return; |
| 1283 | } else if (group == null) { |
| 1284 | group = getGroupIdTable(key.deviceId()).values() |
| 1285 | .stream() |
| 1286 | .filter((storedGroup) -> (storedGroup.appCookie().equals(key.appCookie))) |
Yuta HIGUCHI | 6e5f470 | 2016-11-21 11:42:11 -0800 | [diff] [blame] | 1287 | .findFirst().orElse(null); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1288 | if (group == null) { |
| 1289 | log.error("GroupStoreKeyMapListener: Received " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1290 | + "event {} with null entry... can not process", mapEvent.type()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1291 | return; |
| 1292 | } |
| 1293 | } |
| 1294 | log.trace("received groupid map event {} for id {} in device {}", |
| 1295 | mapEvent.type(), |
| 1296 | group.id(), |
jaegonkim | 68e080c | 2016-12-01 22:31:01 +0900 | [diff] [blame] | 1297 | (key != null ? key.deviceId() : null)); |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1298 | if (mapEvent.type() == MapEvent.Type.INSERT || mapEvent.type() == MapEvent.Type.UPDATE) { |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 1299 | // Update the group ID table |
| 1300 | getGroupIdTable(group.deviceId()).put(group.id(), group); |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1301 | StoredGroupEntry value = Versioned.valueOrNull(mapEvent.newValue()); |
| 1302 | if (value.state() == Group.GroupState.ADDED) { |
| 1303 | if (value.isGroupStateAddedFirstTime()) { |
| 1304 | groupEvent = new GroupEvent(Type.GROUP_ADDED, value); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1305 | log.trace("Received first time GROUP_ADDED state update for id {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1306 | group.id(), |
| 1307 | group.deviceId()); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1308 | } else { |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1309 | groupEvent = new GroupEvent(Type.GROUP_UPDATED, value); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1310 | log.trace("Received following GROUP_ADDED state update for id {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1311 | group.id(), |
| 1312 | group.deviceId()); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1313 | } |
| 1314 | } |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1315 | } else if (mapEvent.type() == MapEvent.Type.REMOVE) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1316 | groupEvent = new GroupEvent(Type.GROUP_REMOVED, group); |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 1317 | // Remove the entry from the group ID table |
| 1318 | getGroupIdTable(group.deviceId()).remove(group.id(), group); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | if (groupEvent != null) { |
| 1322 | notifyDelegate(groupEvent); |
| 1323 | } |
| 1324 | } |
| 1325 | } |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 1326 | |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 1327 | private void processGroupMessage(GroupStoreMessage message) { |
| 1328 | if (message.type() == GroupStoreMessage.Type.FAILOVER) { |
| 1329 | // FIXME: groupStoreEntriesByKey inaccessible here |
| 1330 | getGroupIdTable(message.deviceId()).values() |
| 1331 | .stream() |
| 1332 | .filter((storedGroup) -> (storedGroup.appCookie().equals(message.appCookie()))) |
| 1333 | .findFirst().ifPresent(group -> notifyDelegate(new GroupEvent(Type.GROUP_BUCKET_FAILOVER, group))); |
| 1334 | } |
| 1335 | } |
| 1336 | |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 1337 | private void process(GroupStoreMessage groupOp) { |
| 1338 | log.debug("Received remote group operation {} request for device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1339 | groupOp.type(), |
| 1340 | groupOp.deviceId()); |
| 1341 | if (!mastershipService.isLocalMaster(groupOp.deviceId())) { |
| 1342 | log.warn("This node is not MASTER for device {}", groupOp.deviceId()); |
| 1343 | return; |
| 1344 | } |
| 1345 | if (groupOp.type() == GroupStoreMessage.Type.ADD) { |
| 1346 | storeGroupDescriptionInternal(groupOp.groupDesc()); |
| 1347 | } else if (groupOp.type() == GroupStoreMessage.Type.UPDATE) { |
| 1348 | updateGroupDescriptionInternal(groupOp.deviceId(), |
| 1349 | groupOp.appCookie(), |
| 1350 | groupOp.updateType(), |
| 1351 | groupOp.updateBuckets(), |
| 1352 | groupOp.newAppCookie()); |
| 1353 | } else if (groupOp.type() == GroupStoreMessage.Type.DELETE) { |
| 1354 | deleteGroupDescriptionInternal(groupOp.deviceId(), |
| 1355 | groupOp.appCookie()); |
| 1356 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | /** |
| 1360 | * Flattened map key to be used to store group entries. |
| 1361 | */ |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 1362 | protected static class GroupStoreMapKey { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1363 | private final DeviceId deviceId; |
| 1364 | |
| 1365 | public GroupStoreMapKey(DeviceId deviceId) { |
| 1366 | this.deviceId = deviceId; |
| 1367 | } |
| 1368 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1369 | public DeviceId deviceId() { |
| 1370 | return deviceId; |
| 1371 | } |
| 1372 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1373 | @Override |
| 1374 | public boolean equals(Object o) { |
| 1375 | if (this == o) { |
| 1376 | return true; |
| 1377 | } |
| 1378 | if (!(o instanceof GroupStoreMapKey)) { |
| 1379 | return false; |
| 1380 | } |
| 1381 | GroupStoreMapKey that = (GroupStoreMapKey) o; |
| 1382 | return this.deviceId.equals(that.deviceId); |
| 1383 | } |
| 1384 | |
| 1385 | @Override |
| 1386 | public int hashCode() { |
| 1387 | int result = 17; |
| 1388 | |
| 1389 | result = 31 * result + Objects.hash(this.deviceId); |
| 1390 | |
| 1391 | return result; |
| 1392 | } |
| 1393 | } |
| 1394 | |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 1395 | protected static class GroupStoreKeyMapKey extends GroupStoreMapKey { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1396 | private final GroupKey appCookie; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1397 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1398 | public GroupStoreKeyMapKey(DeviceId deviceId, |
| 1399 | GroupKey appCookie) { |
| 1400 | super(deviceId); |
| 1401 | this.appCookie = appCookie; |
| 1402 | } |
| 1403 | |
| 1404 | @Override |
| 1405 | public boolean equals(Object o) { |
| 1406 | if (this == o) { |
| 1407 | return true; |
| 1408 | } |
| 1409 | if (!(o instanceof GroupStoreKeyMapKey)) { |
| 1410 | return false; |
| 1411 | } |
| 1412 | GroupStoreKeyMapKey that = (GroupStoreKeyMapKey) o; |
| 1413 | return (super.equals(that) && |
| 1414 | this.appCookie.equals(that.appCookie)); |
| 1415 | } |
| 1416 | |
| 1417 | @Override |
| 1418 | public int hashCode() { |
| 1419 | int result = 17; |
| 1420 | |
| 1421 | result = 31 * result + super.hashCode() + Objects.hash(this.appCookie); |
| 1422 | |
| 1423 | return result; |
| 1424 | } |
| 1425 | } |
| 1426 | |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 1427 | protected static class GroupStoreIdMapKey extends GroupStoreMapKey { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1428 | private final GroupId groupId; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1429 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1430 | public GroupStoreIdMapKey(DeviceId deviceId, |
| 1431 | GroupId groupId) { |
| 1432 | super(deviceId); |
| 1433 | this.groupId = groupId; |
| 1434 | } |
| 1435 | |
| 1436 | @Override |
| 1437 | public boolean equals(Object o) { |
| 1438 | if (this == o) { |
| 1439 | return true; |
| 1440 | } |
| 1441 | if (!(o instanceof GroupStoreIdMapKey)) { |
| 1442 | return false; |
| 1443 | } |
| 1444 | GroupStoreIdMapKey that = (GroupStoreIdMapKey) o; |
| 1445 | return (super.equals(that) && |
| 1446 | this.groupId.equals(that.groupId)); |
| 1447 | } |
| 1448 | |
| 1449 | @Override |
| 1450 | public int hashCode() { |
| 1451 | int result = 17; |
| 1452 | |
| 1453 | result = 31 * result + super.hashCode() + Objects.hash(this.groupId); |
| 1454 | |
| 1455 | return result; |
| 1456 | } |
| 1457 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1458 | |
| 1459 | @Override |
| 1460 | public void pushGroupMetrics(DeviceId deviceId, |
| 1461 | Collection<Group> groupEntries) { |
| 1462 | boolean deviceInitialAuditStatus = |
| 1463 | deviceInitialAuditStatus(deviceId); |
| 1464 | Set<Group> southboundGroupEntries = |
| 1465 | Sets.newHashSet(groupEntries); |
| 1466 | Set<StoredGroupEntry> storedGroupEntries = |
| 1467 | Sets.newHashSet(getStoredGroups(deviceId)); |
| 1468 | Set<Group> extraneousStoredEntries = |
| 1469 | Sets.newHashSet(getExtraneousGroups(deviceId)); |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 1470 | NodeId master; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1471 | |
Sho SHIMIZU | 695bac6 | 2016-08-15 12:41:59 -0700 | [diff] [blame] | 1472 | if (log.isTraceEnabled()) { |
| 1473 | log.trace("pushGroupMetrics: Displaying all ({}) southboundGroupEntries for device {}", |
| 1474 | southboundGroupEntries.size(), |
| 1475 | deviceId); |
| 1476 | for (Group group : southboundGroupEntries) { |
| 1477 | log.trace("Group {} in device {}", group, deviceId); |
| 1478 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1479 | |
Sho SHIMIZU | 695bac6 | 2016-08-15 12:41:59 -0700 | [diff] [blame] | 1480 | log.trace("Displaying all ({}) stored group entries for device {}", |
| 1481 | storedGroupEntries.size(), |
| 1482 | deviceId); |
| 1483 | for (StoredGroupEntry group : storedGroupEntries) { |
| 1484 | log.trace("Stored Group {} for device {}", group, deviceId); |
| 1485 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1488 | garbageCollect(deviceId, southboundGroupEntries, storedGroupEntries); |
| 1489 | |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 1490 | // update stats |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1491 | for (Iterator<Group> it2 = southboundGroupEntries.iterator(); it2.hasNext();) { |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 1492 | // Mastership change can occur during this iteration |
| 1493 | master = mastershipService.getMasterFor(deviceId); |
| 1494 | if (!Objects.equals(local, master)) { |
| 1495 | log.warn("Tried to update the group stats while the node was not the master"); |
| 1496 | return; |
| 1497 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1498 | Group group = it2.next(); |
| 1499 | if (storedGroupEntries.remove(group)) { |
| 1500 | // we both have the group, let's update some info then. |
| 1501 | log.trace("Group AUDIT: group {} exists in both planes for device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1502 | group.id(), deviceId); |
| 1503 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1504 | groupAdded(group); |
| 1505 | it2.remove(); |
| 1506 | } |
| 1507 | } |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 1508 | |
| 1509 | // extraneous groups in the dataplane |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1510 | for (Group group : southboundGroupEntries) { |
| 1511 | if (getGroup(group.deviceId(), group.id()) != null) { |
| 1512 | // There is a group existing with the same id |
| 1513 | // It is possible that group update is |
| 1514 | // in progress while we got a stale info from switch |
| 1515 | if (!storedGroupEntries.remove(getGroup( |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1516 | group.deviceId(), group.id()))) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1517 | log.warn("Group AUDIT: Inconsistent state:" |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1518 | + "Group exists in ID based table while " |
| 1519 | + "not present in key based table"); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1520 | } |
| 1521 | } else { |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 1522 | // Mastership change can occur during this iteration |
| 1523 | master = mastershipService.getMasterFor(deviceId); |
| 1524 | if (!Objects.equals(local, master)) { |
| 1525 | log.warn("Tried to process extraneous groups while the node was not the master"); |
| 1526 | return; |
| 1527 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1528 | // there are groups in the switch that aren't in the store |
| 1529 | log.debug("Group AUDIT: extraneous group {} exists in data plane for device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1530 | group.id(), deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1531 | extraneousStoredEntries.remove(group); |
Kavitha Alagesan | c56cded | 2017-01-13 10:48:18 +0530 | [diff] [blame] | 1532 | if (allowExtraneousGroups) { |
| 1533 | extraneousGroup(group); |
| 1534 | } else { |
| 1535 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVE_REQUESTED, group)); |
| 1536 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1537 | } |
| 1538 | } |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 1539 | |
| 1540 | // missing groups in the dataplane |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 1541 | for (StoredGroupEntry group : storedGroupEntries) { |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 1542 | // Mastership change can occur during this iteration |
| 1543 | master = mastershipService.getMasterFor(deviceId); |
| 1544 | if (!Objects.equals(local, master)) { |
| 1545 | log.warn("Tried to process missing groups while the node was not the master"); |
| 1546 | return; |
| 1547 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1548 | // there are groups in the store that aren't in the switch |
| 1549 | log.debug("Group AUDIT: group {} missing in data plane for device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1550 | group.id(), deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1551 | groupMissing(group); |
| 1552 | } |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 1553 | |
| 1554 | // extraneous groups in the store |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1555 | for (Group group : extraneousStoredEntries) { |
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 node extraneous 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 extraneous store that |
| 1563 | // aren't in the switch |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1564 | log.debug("Group AUDIT: clearing extraneous group {} from store for device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1565 | group.id(), deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1566 | removeExtraneousGroupEntry(group); |
| 1567 | } |
| 1568 | |
| 1569 | if (!deviceInitialAuditStatus) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1570 | log.info("Group AUDIT: Setting device {} initial AUDIT completed", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1571 | deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1572 | deviceInitialAuditCompleted(deviceId, true); |
| 1573 | } |
| 1574 | } |
| 1575 | |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 1576 | @Override |
| 1577 | public void notifyOfFailovers(Collection<Group> failoverGroups) { |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 1578 | failoverGroups.forEach(group -> { |
| 1579 | if (group.type() == Group.Type.FAILOVER) { |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 1580 | groupTopic.publish(GroupStoreMessage.createGroupFailoverMsg( |
| 1581 | group.deviceId(), group)); |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 1582 | } |
| 1583 | }); |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1586 | private void garbageCollect(DeviceId deviceId, |
| 1587 | Set<Group> southboundGroupEntries, |
| 1588 | Set<StoredGroupEntry> storedGroupEntries) { |
| 1589 | if (!garbageCollect) { |
| 1590 | return; |
| 1591 | } |
| 1592 | |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 1593 | NodeId master; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1594 | Iterator<StoredGroupEntry> it = storedGroupEntries.iterator(); |
| 1595 | while (it.hasNext()) { |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 1596 | // Mastership change can occur during this iteration |
| 1597 | master = mastershipService.getMasterFor(deviceId); |
| 1598 | if (!Objects.equals(local, master)) { |
| 1599 | log.warn("Tried to run garbage collector while the node was not the master"); |
| 1600 | return; |
| 1601 | } |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1602 | StoredGroupEntry group = it.next(); |
| 1603 | if (group.state() != GroupState.PENDING_DELETE && checkGroupRefCount(group)) { |
| 1604 | log.debug("Garbage collecting group {} on {}", group, deviceId); |
| 1605 | deleteGroupDescription(deviceId, group.appCookie()); |
| 1606 | southboundGroupEntries.remove(group); |
| 1607 | it.remove(); |
| 1608 | } |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | private boolean checkGroupRefCount(Group group) { |
| 1613 | return (group.referenceCount() == 0 && group.age() >= gcThresh); |
| 1614 | } |
| 1615 | |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 1616 | private void groupMissing(StoredGroupEntry group) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1617 | switch (group.state()) { |
| 1618 | case PENDING_DELETE: |
| 1619 | log.debug("Group {} delete confirmation from device {}", |
| 1620 | group, group.deviceId()); |
| 1621 | removeGroupEntry(group); |
| 1622 | break; |
| 1623 | case ADDED: |
| 1624 | case PENDING_ADD: |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1625 | case PENDING_ADD_RETRY: |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1626 | case PENDING_UPDATE: |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1627 | 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] | 1628 | group.id(), |
| 1629 | group.deviceId(), |
| 1630 | group.state()); |
| 1631 | group.setState(Group.GroupState.PENDING_ADD_RETRY); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1632 | //Re-PUT map entries to trigger map update events |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 1633 | getGroupStoreKeyMap().put(new GroupStoreKeyMapKey(group.deviceId(), group.appCookie()), group); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1634 | notifyDelegate(new GroupEvent(GroupEvent.Type.GROUP_ADD_REQUESTED, |
| 1635 | group)); |
| 1636 | break; |
| 1637 | default: |
| 1638 | log.debug("Group {} has not been installed.", group); |
| 1639 | break; |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | private void extraneousGroup(Group group) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1644 | log.trace("Group {} is on device {} but not in store.", |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1645 | group, group.deviceId()); |
| 1646 | addOrUpdateExtraneousGroupEntry(group); |
| 1647 | } |
| 1648 | |
| 1649 | private void groupAdded(Group group) { |
| 1650 | log.trace("Group {} Added or Updated in device {}", |
| 1651 | group, group.deviceId()); |
pier | 1e80f36 | 2020-04-03 12:44:21 +0200 | [diff] [blame] | 1652 | updateGroupEntryStats(group); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1653 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1654 | } |