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