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 | 72ab6e5 | 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 | 72ab6e5 | 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 | 00ac83e | 2019-12-19 16:04:23 +0100 | [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 | 00ac83e | 2019-12-19 16:04:23 +0100 | [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 | 72ab6e5 | 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()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 911 | synchronized (existing) { |
| 912 | existing.setState(GroupState.PENDING_DELETE); |
Saurav Das | 80980c7 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 913 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 914 | put(new GroupStoreKeyMapKey(existing.deviceId(), existing.appCookie()), |
| 915 | existing); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 916 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 917 | log.debug("deleteGroupDescriptionInternal: in device {} issuing GROUP_REMOVE_REQUESTED", |
| 918 | deviceId); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 919 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVE_REQUESTED, existing)); |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * Stores a new group entry, or updates an existing entry. |
| 924 | * |
| 925 | * @param group group entry |
| 926 | */ |
| 927 | @Override |
| 928 | public void addOrUpdateGroupEntry(Group group) { |
| 929 | // check if this new entry is an update to an existing entry |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 930 | StoredGroupEntry existing = getStoredGroupEntry(group.deviceId(), |
| 931 | group.id()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 932 | GroupEvent event = null; |
| 933 | |
| 934 | if (existing != null) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 935 | log.trace("addOrUpdateGroupEntry: updating group entry {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 936 | group.id(), |
| 937 | group.deviceId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 938 | synchronized (existing) { |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 939 | for (GroupBucket bucket : group.buckets().buckets()) { |
Sho SHIMIZU | 30d639b | 2015-05-05 09:30:35 -0700 | [diff] [blame] | 940 | Optional<GroupBucket> matchingBucket = |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 941 | existing.buckets().buckets() |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 942 | .stream() |
| 943 | .filter((existingBucket) -> (existingBucket.equals(bucket))) |
| 944 | .findFirst(); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 945 | if (matchingBucket.isPresent()) { |
| 946 | ((StoredGroupBucketEntry) matchingBucket. |
| 947 | get()).setPackets(bucket.packets()); |
| 948 | ((StoredGroupBucketEntry) matchingBucket. |
| 949 | get()).setBytes(bucket.bytes()); |
| 950 | } else { |
| 951 | log.warn("addOrUpdateGroupEntry: No matching " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 952 | + "buckets to update stats"); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 953 | } |
| 954 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 955 | existing.setLife(group.life()); |
| 956 | existing.setPackets(group.packets()); |
| 957 | existing.setBytes(group.bytes()); |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 958 | existing.setReferenceCount(group.referenceCount()); |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 959 | existing.setFailedRetryCount(0); |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 960 | if ((existing.state() == GroupState.PENDING_ADD) || |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 961 | (existing.state() == GroupState.PENDING_ADD_RETRY)) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 962 | log.trace("addOrUpdateGroupEntry: group entry {} in device {} moving from {} to ADDED", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 963 | existing.id(), |
| 964 | existing.deviceId(), |
| 965 | existing.state()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 966 | existing.setState(GroupState.ADDED); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 967 | existing.setIsGroupStateAddedFirstTime(true); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 968 | event = new GroupEvent(Type.GROUP_ADDED, existing); |
| 969 | } else { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 970 | log.trace("addOrUpdateGroupEntry: group entry {} in device {} moving from {} to ADDED", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 971 | existing.id(), |
| 972 | existing.deviceId(), |
| 973 | GroupState.PENDING_UPDATE); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 974 | existing.setState(GroupState.ADDED); |
| 975 | existing.setIsGroupStateAddedFirstTime(false); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 976 | event = new GroupEvent(Type.GROUP_UPDATED, existing); |
| 977 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 978 | //Re-PUT map entries to trigger map update events |
| 979 | getGroupStoreKeyMap(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 980 | put(new GroupStoreKeyMapKey(existing.deviceId(), |
| 981 | existing.appCookie()), existing); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 982 | } |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 983 | } else { |
| 984 | log.warn("addOrUpdateGroupEntry: Group update " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 985 | + "happening for a non-existing entry in the map"); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 986 | } |
| 987 | |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 988 | // XXX if map is going to trigger event, is this one needed? |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 989 | if (event != null) { |
| 990 | notifyDelegate(event); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | /** |
| 995 | * Removes the group entry from store. |
| 996 | * |
| 997 | * @param group group entry |
| 998 | */ |
| 999 | @Override |
| 1000 | public void removeGroupEntry(Group group) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1001 | StoredGroupEntry existing = getStoredGroupEntry(group.deviceId(), |
| 1002 | group.id()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1003 | |
| 1004 | if (existing != null) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1005 | log.debug("removeGroupEntry: removing group entry {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1006 | group.id(), |
| 1007 | group.deviceId()); |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 1008 | //Removal from groupid based map will happen in the |
| 1009 | //map update listener |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1010 | getGroupStoreKeyMap().remove(new GroupStoreKeyMapKey(existing.deviceId(), |
| 1011 | existing.appCookie())); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1012 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVED, existing)); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1013 | } else { |
| 1014 | log.warn("removeGroupEntry for {} in device{} is " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1015 | + "not existing in our maps", |
| 1016 | group.id(), |
| 1017 | group.deviceId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | |
Victor Silva | 4e8b783 | 2016-08-17 17:11:19 -0300 | [diff] [blame] | 1021 | private void purgeGroupEntries(Set<Entry<GroupStoreKeyMapKey, StoredGroupEntry>> entries) { |
| 1022 | entries.forEach(entry -> { |
| 1023 | groupStoreEntriesByKey.remove(entry.getKey()); |
| 1024 | }); |
| 1025 | } |
| 1026 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1027 | @Override |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1028 | public void purgeGroupEntry(DeviceId deviceId) { |
Victor Silva | 4e8b783 | 2016-08-17 17:11:19 -0300 | [diff] [blame] | 1029 | Set<Entry<GroupStoreKeyMapKey, StoredGroupEntry>> entriesPendingRemove = |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1030 | new HashSet<>(); |
| 1031 | |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1032 | getGroupStoreKeyMap().entrySet().stream() |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1033 | .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
Victor Silva | 4e8b783 | 2016-08-17 17:11:19 -0300 | [diff] [blame] | 1034 | .forEach(entriesPendingRemove::add); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1035 | |
Victor Silva | 4e8b783 | 2016-08-17 17:11:19 -0300 | [diff] [blame] | 1036 | purgeGroupEntries(entriesPendingRemove); |
| 1037 | } |
| 1038 | |
| 1039 | @Override |
| 1040 | public void purgeGroupEntries() { |
| 1041 | purgeGroupEntries(getGroupStoreKeyMap().entrySet()); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | @Override |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1045 | public void deviceInitialAuditCompleted(DeviceId deviceId, |
| 1046 | boolean completed) { |
| 1047 | synchronized (deviceAuditStatus) { |
| 1048 | if (completed) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1049 | log.debug("AUDIT completed for device {}", |
| 1050 | deviceId); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1051 | deviceAuditStatus.put(deviceId, true); |
| 1052 | // Execute all pending group requests |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1053 | List<StoredGroupEntry> pendingGroupRequests = |
| 1054 | getPendingGroupKeyTable().values() |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1055 | .stream() |
| 1056 | .filter(g -> g.deviceId().equals(deviceId)) |
| 1057 | .collect(Collectors.toList()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1058 | 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] | 1059 | deviceId, |
| 1060 | pendingGroupRequests.size()); |
| 1061 | for (Group group : pendingGroupRequests) { |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1062 | GroupDescription tmp = new DefaultGroupDescription( |
| 1063 | group.deviceId(), |
| 1064 | group.type(), |
| 1065 | group.buckets(), |
| 1066 | group.appCookie(), |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 1067 | group.givenGroupId(), |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1068 | group.appId()); |
| 1069 | storeGroupDescriptionInternal(tmp); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1070 | getPendingGroupKeyTable(). |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1071 | remove(new GroupStoreKeyMapKey(deviceId, group.appCookie())); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1072 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1073 | } else { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 1074 | Boolean audited = deviceAuditStatus.get(deviceId); |
| 1075 | if (audited != null && audited) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1076 | log.debug("Clearing AUDIT status for device {}", deviceId); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1077 | deviceAuditStatus.put(deviceId, false); |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | @Override |
| 1084 | public boolean deviceInitialAuditStatus(DeviceId deviceId) { |
| 1085 | synchronized (deviceAuditStatus) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 1086 | Boolean audited = deviceAuditStatus.get(deviceId); |
| 1087 | return audited != null && audited; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | @Override |
| 1092 | public void groupOperationFailed(DeviceId deviceId, GroupOperation operation) { |
| 1093 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1094 | StoredGroupEntry existing = getStoredGroupEntry(deviceId, |
| 1095 | operation.groupId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1096 | |
| 1097 | if (existing == null) { |
| 1098 | log.warn("No group entry with ID {} found ", operation.groupId()); |
| 1099 | return; |
| 1100 | } |
| 1101 | |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1102 | log.warn("groupOperationFailed: group operation {} failed in state {} " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1103 | + "for group {} in device {} with code {}", |
| 1104 | operation.opType(), |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1105 | existing.state(), |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1106 | existing.id(), |
| 1107 | existing.deviceId(), |
| 1108 | operation.failureCode()); |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1109 | if (operation.failureCode() == GroupOperation.GroupMsgErrorCode.GROUP_EXISTS) { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1110 | if (operation.buckets().equals(existing.buckets())) { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1111 | if (existing.state() == GroupState.PENDING_ADD || |
| 1112 | existing.state() == GroupState.PENDING_ADD_RETRY) { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1113 | log.info("GROUP_EXISTS: GroupID and Buckets match for group in pending " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1114 | + "add state - moving to ADDED for group {} in device {}", |
| 1115 | existing.id(), deviceId); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1116 | addOrUpdateGroupEntry(existing); |
| 1117 | return; |
| 1118 | } else { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1119 | log.warn("GROUP_EXISTS: GroupId and Buckets match but existing" |
| 1120 | + "group in state: {}", existing.state()); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1121 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1122 | } else { |
| 1123 | log.warn("GROUP EXISTS: Group ID matched but buckets did not. " |
| 1124 | + "Operation: {} Existing: {}", operation.buckets(), |
| 1125 | existing.buckets()); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1126 | } |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1127 | } |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1128 | if (operation.failureCode() == GroupOperation.GroupMsgErrorCode.INVALID_GROUP) { |
| 1129 | existing.incrFailedRetryCount(); |
| 1130 | if (existing.failedRetryCount() < MAX_FAILED_ATTEMPTS) { |
| 1131 | log.warn("Group {} programming failed {} of {} times in dev {}, " |
| 1132 | + "retrying ..", existing.id(), |
| 1133 | existing.failedRetryCount(), MAX_FAILED_ATTEMPTS, |
| 1134 | deviceId); |
| 1135 | return; |
| 1136 | } |
| 1137 | log.warn("Group {} programming failed {} of {} times in dev {}, " |
| 1138 | + "removing group from store", existing.id(), |
| 1139 | existing.failedRetryCount(), MAX_FAILED_ATTEMPTS, |
| 1140 | deviceId); |
| 1141 | // fall through to case |
| 1142 | } |
| 1143 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1144 | switch (operation.opType()) { |
| 1145 | case ADD: |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1146 | if (existing.state() == GroupState.PENDING_ADD |
| 1147 | || existing.state() == GroupState.PENDING_ADD_RETRY) { |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1148 | notifyDelegate(new GroupEvent(Type.GROUP_ADD_FAILED, existing)); |
| 1149 | log.warn("groupOperationFailed: cleaningup " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1150 | + "group {} from store in device {}....", |
| 1151 | existing.id(), |
| 1152 | existing.deviceId()); |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1153 | //Removal from groupid based map will happen in the |
| 1154 | //map update listener |
| 1155 | getGroupStoreKeyMap().remove(new GroupStoreKeyMapKey(existing.deviceId(), |
| 1156 | existing.appCookie())); |
| 1157 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1158 | break; |
| 1159 | case MODIFY: |
| 1160 | notifyDelegate(new GroupEvent(Type.GROUP_UPDATE_FAILED, existing)); |
| 1161 | break; |
| 1162 | case DELETE: |
| 1163 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVE_FAILED, existing)); |
| 1164 | break; |
| 1165 | default: |
| 1166 | log.warn("Unknown group operation type {}", operation.opType()); |
| 1167 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | @Override |
| 1171 | public void addOrUpdateExtraneousGroupEntry(Group group) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1172 | log.debug("add/update extraneous group entry {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1173 | group.id(), |
| 1174 | group.deviceId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1175 | ConcurrentMap<GroupId, Group> extraneousIdTable = |
| 1176 | getExtraneousGroupIdTable(group.deviceId()); |
| 1177 | extraneousIdTable.put(group.id(), group); |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 1178 | // Don't remove the extraneous groups, instead re-use it when |
| 1179 | // a group request comes with the same set of buckets |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | @Override |
| 1183 | public void removeExtraneousGroupEntry(Group group) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1184 | log.debug("remove extraneous group entry {} of device {} from store", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1185 | group.id(), |
| 1186 | group.deviceId()); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1187 | ConcurrentMap<GroupId, Group> extraneousIdTable = |
| 1188 | getExtraneousGroupIdTable(group.deviceId()); |
| 1189 | extraneousIdTable.remove(group.id()); |
| 1190 | } |
| 1191 | |
| 1192 | @Override |
| 1193 | public Iterable<Group> getExtraneousGroups(DeviceId deviceId) { |
| 1194 | // flatten and make iterator unmodifiable |
| 1195 | return FluentIterable.from( |
| 1196 | getExtraneousGroupIdTable(deviceId).values()); |
| 1197 | } |
| 1198 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1199 | /** |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 1200 | * 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] | 1201 | */ |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 1202 | private class GroupStoreKeyMapListener implements |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1203 | MapEventListener<GroupStoreKeyMapKey, StoredGroupEntry> { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1204 | |
| 1205 | @Override |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1206 | public void event(MapEvent<GroupStoreKeyMapKey, StoredGroupEntry> mapEvent) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1207 | GroupEvent groupEvent = null; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1208 | GroupStoreKeyMapKey key = mapEvent.key(); |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1209 | StoredGroupEntry group = Versioned.valueOrNull(mapEvent.newValue()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1210 | if ((key == null) && (group == null)) { |
| 1211 | log.error("GroupStoreKeyMapListener: Received " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1212 | + "event {} with null entry", mapEvent.type()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1213 | return; |
| 1214 | } else if (group == null) { |
| 1215 | group = getGroupIdTable(key.deviceId()).values() |
| 1216 | .stream() |
| 1217 | .filter((storedGroup) -> (storedGroup.appCookie().equals(key.appCookie))) |
Yuta HIGUCHI | 6e5f470 | 2016-11-21 11:42:11 -0800 | [diff] [blame] | 1218 | .findFirst().orElse(null); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1219 | if (group == null) { |
| 1220 | log.error("GroupStoreKeyMapListener: Received " |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1221 | + "event {} with null entry... can not process", mapEvent.type()); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1222 | return; |
| 1223 | } |
| 1224 | } |
| 1225 | log.trace("received groupid map event {} for id {} in device {}", |
| 1226 | mapEvent.type(), |
| 1227 | group.id(), |
jaegonkim | 68e080c | 2016-12-01 22:31:01 +0900 | [diff] [blame] | 1228 | (key != null ? key.deviceId() : null)); |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1229 | if (mapEvent.type() == MapEvent.Type.INSERT || mapEvent.type() == MapEvent.Type.UPDATE) { |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 1230 | // Update the group ID table |
| 1231 | getGroupIdTable(group.deviceId()).put(group.id(), group); |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1232 | StoredGroupEntry value = Versioned.valueOrNull(mapEvent.newValue()); |
| 1233 | if (value.state() == Group.GroupState.ADDED) { |
| 1234 | if (value.isGroupStateAddedFirstTime()) { |
| 1235 | groupEvent = new GroupEvent(Type.GROUP_ADDED, value); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1236 | log.trace("Received first time GROUP_ADDED state update for id {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1237 | group.id(), |
| 1238 | group.deviceId()); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1239 | } else { |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1240 | groupEvent = new GroupEvent(Type.GROUP_UPDATED, value); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1241 | log.trace("Received following GROUP_ADDED state update for id {} in device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1242 | group.id(), |
| 1243 | group.deviceId()); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1244 | } |
| 1245 | } |
Madan Jampani | 0b84753 | 2016-03-03 13:44:15 -0800 | [diff] [blame] | 1246 | } else if (mapEvent.type() == MapEvent.Type.REMOVE) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1247 | groupEvent = new GroupEvent(Type.GROUP_REMOVED, group); |
Srikanth Vavilapalli | 6a9d4e4 | 2015-03-30 19:41:56 -0700 | [diff] [blame] | 1248 | // Remove the entry from the group ID table |
| 1249 | getGroupIdTable(group.deviceId()).remove(group.id(), group); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | if (groupEvent != null) { |
| 1253 | notifyDelegate(groupEvent); |
| 1254 | } |
| 1255 | } |
| 1256 | } |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 1257 | |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 1258 | private void processGroupMessage(GroupStoreMessage message) { |
| 1259 | if (message.type() == GroupStoreMessage.Type.FAILOVER) { |
| 1260 | // FIXME: groupStoreEntriesByKey inaccessible here |
| 1261 | getGroupIdTable(message.deviceId()).values() |
| 1262 | .stream() |
| 1263 | .filter((storedGroup) -> (storedGroup.appCookie().equals(message.appCookie()))) |
| 1264 | .findFirst().ifPresent(group -> notifyDelegate(new GroupEvent(Type.GROUP_BUCKET_FAILOVER, group))); |
| 1265 | } |
| 1266 | } |
| 1267 | |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 1268 | private void process(GroupStoreMessage groupOp) { |
| 1269 | log.debug("Received remote group operation {} request for device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1270 | groupOp.type(), |
| 1271 | groupOp.deviceId()); |
| 1272 | if (!mastershipService.isLocalMaster(groupOp.deviceId())) { |
| 1273 | log.warn("This node is not MASTER for device {}", groupOp.deviceId()); |
| 1274 | return; |
| 1275 | } |
| 1276 | if (groupOp.type() == GroupStoreMessage.Type.ADD) { |
| 1277 | storeGroupDescriptionInternal(groupOp.groupDesc()); |
| 1278 | } else if (groupOp.type() == GroupStoreMessage.Type.UPDATE) { |
| 1279 | updateGroupDescriptionInternal(groupOp.deviceId(), |
| 1280 | groupOp.appCookie(), |
| 1281 | groupOp.updateType(), |
| 1282 | groupOp.updateBuckets(), |
| 1283 | groupOp.newAppCookie()); |
| 1284 | } else if (groupOp.type() == GroupStoreMessage.Type.DELETE) { |
| 1285 | deleteGroupDescriptionInternal(groupOp.deviceId(), |
| 1286 | groupOp.appCookie()); |
| 1287 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | /** |
| 1291 | * Flattened map key to be used to store group entries. |
| 1292 | */ |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 1293 | protected static class GroupStoreMapKey { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1294 | private final DeviceId deviceId; |
| 1295 | |
| 1296 | public GroupStoreMapKey(DeviceId deviceId) { |
| 1297 | this.deviceId = deviceId; |
| 1298 | } |
| 1299 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1300 | public DeviceId deviceId() { |
| 1301 | return deviceId; |
| 1302 | } |
| 1303 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1304 | @Override |
| 1305 | public boolean equals(Object o) { |
| 1306 | if (this == o) { |
| 1307 | return true; |
| 1308 | } |
| 1309 | if (!(o instanceof GroupStoreMapKey)) { |
| 1310 | return false; |
| 1311 | } |
| 1312 | GroupStoreMapKey that = (GroupStoreMapKey) o; |
| 1313 | return this.deviceId.equals(that.deviceId); |
| 1314 | } |
| 1315 | |
| 1316 | @Override |
| 1317 | public int hashCode() { |
| 1318 | int result = 17; |
| 1319 | |
| 1320 | result = 31 * result + Objects.hash(this.deviceId); |
| 1321 | |
| 1322 | return result; |
| 1323 | } |
| 1324 | } |
| 1325 | |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 1326 | protected static class GroupStoreKeyMapKey extends GroupStoreMapKey { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1327 | private final GroupKey appCookie; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1328 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1329 | public GroupStoreKeyMapKey(DeviceId deviceId, |
| 1330 | GroupKey appCookie) { |
| 1331 | super(deviceId); |
| 1332 | this.appCookie = appCookie; |
| 1333 | } |
| 1334 | |
| 1335 | @Override |
| 1336 | public boolean equals(Object o) { |
| 1337 | if (this == o) { |
| 1338 | return true; |
| 1339 | } |
| 1340 | if (!(o instanceof GroupStoreKeyMapKey)) { |
| 1341 | return false; |
| 1342 | } |
| 1343 | GroupStoreKeyMapKey that = (GroupStoreKeyMapKey) o; |
| 1344 | return (super.equals(that) && |
| 1345 | this.appCookie.equals(that.appCookie)); |
| 1346 | } |
| 1347 | |
| 1348 | @Override |
| 1349 | public int hashCode() { |
| 1350 | int result = 17; |
| 1351 | |
| 1352 | result = 31 * result + super.hashCode() + Objects.hash(this.appCookie); |
| 1353 | |
| 1354 | return result; |
| 1355 | } |
| 1356 | } |
| 1357 | |
Ray Milkey | b3c5ce2 | 2015-08-10 09:07:36 -0700 | [diff] [blame] | 1358 | protected static class GroupStoreIdMapKey extends GroupStoreMapKey { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1359 | private final GroupId groupId; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1360 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1361 | public GroupStoreIdMapKey(DeviceId deviceId, |
| 1362 | GroupId groupId) { |
| 1363 | super(deviceId); |
| 1364 | this.groupId = groupId; |
| 1365 | } |
| 1366 | |
| 1367 | @Override |
| 1368 | public boolean equals(Object o) { |
| 1369 | if (this == o) { |
| 1370 | return true; |
| 1371 | } |
| 1372 | if (!(o instanceof GroupStoreIdMapKey)) { |
| 1373 | return false; |
| 1374 | } |
| 1375 | GroupStoreIdMapKey that = (GroupStoreIdMapKey) o; |
| 1376 | return (super.equals(that) && |
| 1377 | this.groupId.equals(that.groupId)); |
| 1378 | } |
| 1379 | |
| 1380 | @Override |
| 1381 | public int hashCode() { |
| 1382 | int result = 17; |
| 1383 | |
| 1384 | result = 31 * result + super.hashCode() + Objects.hash(this.groupId); |
| 1385 | |
| 1386 | return result; |
| 1387 | } |
| 1388 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1389 | |
| 1390 | @Override |
| 1391 | public void pushGroupMetrics(DeviceId deviceId, |
| 1392 | Collection<Group> groupEntries) { |
| 1393 | boolean deviceInitialAuditStatus = |
| 1394 | deviceInitialAuditStatus(deviceId); |
| 1395 | Set<Group> southboundGroupEntries = |
| 1396 | Sets.newHashSet(groupEntries); |
| 1397 | Set<StoredGroupEntry> storedGroupEntries = |
| 1398 | Sets.newHashSet(getStoredGroups(deviceId)); |
| 1399 | Set<Group> extraneousStoredEntries = |
| 1400 | Sets.newHashSet(getExtraneousGroups(deviceId)); |
pier | 00ac83e | 2019-12-19 16:04:23 +0100 | [diff] [blame] | 1401 | NodeId master; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1402 | |
Sho SHIMIZU | 695bac6 | 2016-08-15 12:41:59 -0700 | [diff] [blame] | 1403 | if (log.isTraceEnabled()) { |
| 1404 | log.trace("pushGroupMetrics: Displaying all ({}) southboundGroupEntries for device {}", |
| 1405 | southboundGroupEntries.size(), |
| 1406 | deviceId); |
| 1407 | for (Group group : southboundGroupEntries) { |
| 1408 | log.trace("Group {} in device {}", group, deviceId); |
| 1409 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1410 | |
Sho SHIMIZU | 695bac6 | 2016-08-15 12:41:59 -0700 | [diff] [blame] | 1411 | log.trace("Displaying all ({}) stored group entries for device {}", |
| 1412 | storedGroupEntries.size(), |
| 1413 | deviceId); |
| 1414 | for (StoredGroupEntry group : storedGroupEntries) { |
| 1415 | log.trace("Stored Group {} for device {}", group, deviceId); |
| 1416 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1419 | garbageCollect(deviceId, southboundGroupEntries, storedGroupEntries); |
| 1420 | |
pier | 00ac83e | 2019-12-19 16:04:23 +0100 | [diff] [blame] | 1421 | // update stats |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1422 | for (Iterator<Group> it2 = southboundGroupEntries.iterator(); it2.hasNext();) { |
pier | 00ac83e | 2019-12-19 16:04:23 +0100 | [diff] [blame] | 1423 | // Mastership change can occur during this iteration |
| 1424 | master = mastershipService.getMasterFor(deviceId); |
| 1425 | if (!Objects.equals(local, master)) { |
| 1426 | log.warn("Tried to update the group stats while the node was not the master"); |
| 1427 | return; |
| 1428 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1429 | Group group = it2.next(); |
| 1430 | if (storedGroupEntries.remove(group)) { |
| 1431 | // we both have the group, let's update some info then. |
| 1432 | log.trace("Group AUDIT: group {} exists in both planes for device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1433 | group.id(), deviceId); |
| 1434 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1435 | groupAdded(group); |
| 1436 | it2.remove(); |
| 1437 | } |
| 1438 | } |
pier | 00ac83e | 2019-12-19 16:04:23 +0100 | [diff] [blame] | 1439 | |
| 1440 | // extraneous groups in the dataplane |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1441 | for (Group group : southboundGroupEntries) { |
| 1442 | if (getGroup(group.deviceId(), group.id()) != null) { |
| 1443 | // There is a group existing with the same id |
| 1444 | // It is possible that group update is |
| 1445 | // in progress while we got a stale info from switch |
| 1446 | if (!storedGroupEntries.remove(getGroup( |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1447 | group.deviceId(), group.id()))) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1448 | log.warn("Group AUDIT: Inconsistent state:" |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1449 | + "Group exists in ID based table while " |
| 1450 | + "not present in key based table"); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1451 | } |
| 1452 | } else { |
pier | 00ac83e | 2019-12-19 16:04:23 +0100 | [diff] [blame] | 1453 | // Mastership change can occur during this iteration |
| 1454 | master = mastershipService.getMasterFor(deviceId); |
| 1455 | if (!Objects.equals(local, master)) { |
| 1456 | log.warn("Tried to process extraneous groups while the node was not the master"); |
| 1457 | return; |
| 1458 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1459 | // there are groups in the switch that aren't in the store |
| 1460 | log.debug("Group AUDIT: extraneous group {} exists in data plane for device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1461 | group.id(), deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1462 | extraneousStoredEntries.remove(group); |
Kavitha Alagesan | c56cded | 2017-01-13 10:48:18 +0530 | [diff] [blame] | 1463 | if (allowExtraneousGroups) { |
| 1464 | extraneousGroup(group); |
| 1465 | } else { |
| 1466 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVE_REQUESTED, group)); |
| 1467 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1468 | } |
| 1469 | } |
pier | 00ac83e | 2019-12-19 16:04:23 +0100 | [diff] [blame] | 1470 | |
| 1471 | // missing groups in the dataplane |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 1472 | for (StoredGroupEntry group : storedGroupEntries) { |
pier | 00ac83e | 2019-12-19 16:04:23 +0100 | [diff] [blame] | 1473 | // Mastership change can occur during this iteration |
| 1474 | master = mastershipService.getMasterFor(deviceId); |
| 1475 | if (!Objects.equals(local, master)) { |
| 1476 | log.warn("Tried to process missing groups while the node was not the master"); |
| 1477 | return; |
| 1478 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1479 | // there are groups in the store that aren't in the switch |
| 1480 | log.debug("Group AUDIT: group {} missing in data plane for device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1481 | group.id(), deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1482 | groupMissing(group); |
| 1483 | } |
pier | 00ac83e | 2019-12-19 16:04:23 +0100 | [diff] [blame] | 1484 | |
| 1485 | // extraneous groups in the store |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1486 | for (Group group : extraneousStoredEntries) { |
pier | 00ac83e | 2019-12-19 16:04:23 +0100 | [diff] [blame] | 1487 | // Mastership change can occur during this iteration |
| 1488 | master = mastershipService.getMasterFor(deviceId); |
| 1489 | if (!Objects.equals(local, master)) { |
| 1490 | log.warn("Tried to process node extraneous groups while the node was not the master"); |
| 1491 | return; |
| 1492 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1493 | // there are groups in the extraneous store that |
| 1494 | // aren't in the switch |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1495 | log.debug("Group AUDIT: clearing extraneous group {} from store for device {}", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1496 | group.id(), deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1497 | removeExtraneousGroupEntry(group); |
| 1498 | } |
| 1499 | |
| 1500 | if (!deviceInitialAuditStatus) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1501 | log.info("Group AUDIT: Setting device {} initial AUDIT completed", |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1502 | deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1503 | deviceInitialAuditCompleted(deviceId, true); |
| 1504 | } |
| 1505 | } |
| 1506 | |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 1507 | @Override |
| 1508 | public void notifyOfFailovers(Collection<Group> failoverGroups) { |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 1509 | failoverGroups.forEach(group -> { |
| 1510 | if (group.type() == Group.Type.FAILOVER) { |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 1511 | groupTopic.publish(GroupStoreMessage.createGroupFailoverMsg( |
| 1512 | group.deviceId(), group)); |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 1513 | } |
| 1514 | }); |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1517 | private void garbageCollect(DeviceId deviceId, |
| 1518 | Set<Group> southboundGroupEntries, |
| 1519 | Set<StoredGroupEntry> storedGroupEntries) { |
| 1520 | if (!garbageCollect) { |
| 1521 | return; |
| 1522 | } |
| 1523 | |
pier | 00ac83e | 2019-12-19 16:04:23 +0100 | [diff] [blame] | 1524 | NodeId master; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1525 | Iterator<StoredGroupEntry> it = storedGroupEntries.iterator(); |
| 1526 | while (it.hasNext()) { |
pier | 00ac83e | 2019-12-19 16:04:23 +0100 | [diff] [blame] | 1527 | // Mastership change can occur during this iteration |
| 1528 | master = mastershipService.getMasterFor(deviceId); |
| 1529 | if (!Objects.equals(local, master)) { |
| 1530 | log.warn("Tried to run garbage collector while the node was not the master"); |
| 1531 | return; |
| 1532 | } |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 1533 | StoredGroupEntry group = it.next(); |
| 1534 | if (group.state() != GroupState.PENDING_DELETE && checkGroupRefCount(group)) { |
| 1535 | log.debug("Garbage collecting group {} on {}", group, deviceId); |
| 1536 | deleteGroupDescription(deviceId, group.appCookie()); |
| 1537 | southboundGroupEntries.remove(group); |
| 1538 | it.remove(); |
| 1539 | } |
| 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | private boolean checkGroupRefCount(Group group) { |
| 1544 | return (group.referenceCount() == 0 && group.age() >= gcThresh); |
| 1545 | } |
| 1546 | |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 1547 | private void groupMissing(StoredGroupEntry group) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1548 | switch (group.state()) { |
| 1549 | case PENDING_DELETE: |
| 1550 | log.debug("Group {} delete confirmation from device {}", |
| 1551 | group, group.deviceId()); |
| 1552 | removeGroupEntry(group); |
| 1553 | break; |
| 1554 | case ADDED: |
| 1555 | case PENDING_ADD: |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1556 | case PENDING_ADD_RETRY: |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1557 | case PENDING_UPDATE: |
Srikanth Vavilapalli | 5428b6c | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1558 | 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] | 1559 | group.id(), |
| 1560 | group.deviceId(), |
| 1561 | group.state()); |
| 1562 | group.setState(Group.GroupState.PENDING_ADD_RETRY); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1563 | //Re-PUT map entries to trigger map update events |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 1564 | getGroupStoreKeyMap().put(new GroupStoreKeyMapKey(group.deviceId(), group.appCookie()), group); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1565 | notifyDelegate(new GroupEvent(GroupEvent.Type.GROUP_ADD_REQUESTED, |
| 1566 | group)); |
| 1567 | break; |
| 1568 | default: |
| 1569 | log.debug("Group {} has not been installed.", group); |
| 1570 | break; |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | private void extraneousGroup(Group group) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 1575 | log.trace("Group {} is on device {} but not in store.", |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1576 | group, group.deviceId()); |
| 1577 | addOrUpdateExtraneousGroupEntry(group); |
| 1578 | } |
| 1579 | |
| 1580 | private void groupAdded(Group group) { |
| 1581 | log.trace("Group {} Added or Updated in device {}", |
| 1582 | group, group.deviceId()); |
| 1583 | addOrUpdateGroupEntry(group); |
| 1584 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 1585 | } |