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