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