Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -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 | */ |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives.impl; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 17 | |
Jonathan Hart | 9dc5f09 | 2016-06-17 15:15:17 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Collections2; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableMap; |
| 21 | import com.google.common.collect.Lists; |
| 22 | import com.google.common.collect.Maps; |
| 23 | import com.google.common.collect.Sets; |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 24 | import org.apache.commons.lang3.tuple.Pair; |
| 25 | import org.onlab.util.AbstractAccumulator; |
| 26 | import org.onlab.util.KryoNamespace; |
| 27 | import org.onlab.util.SlidingWindowCounter; |
| 28 | import org.onosproject.cluster.ClusterService; |
| 29 | import org.onosproject.cluster.ControllerNode; |
| 30 | import org.onosproject.cluster.NodeId; |
| 31 | import org.onosproject.persistence.PersistenceService; |
| 32 | import org.onosproject.store.LogicalTimestamp; |
| 33 | import org.onosproject.store.Timestamp; |
| 34 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 35 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 36 | import org.onosproject.store.serializers.KryoNamespaces; |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 37 | import org.onosproject.store.serializers.StoreSerializer; |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 38 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 39 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 40 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 41 | import org.onosproject.store.service.WallClockTimestamp; |
| 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
Jonathan Hart | 9dc5f09 | 2016-06-17 15:15:17 -0700 | [diff] [blame] | 45 | import java.util.Collection; |
| 46 | import java.util.Collections; |
| 47 | import java.util.HashSet; |
| 48 | import java.util.List; |
| 49 | import java.util.Map; |
| 50 | import java.util.Objects; |
| 51 | import java.util.Optional; |
| 52 | import java.util.Set; |
| 53 | import java.util.Timer; |
| 54 | import java.util.concurrent.CompletableFuture; |
| 55 | import java.util.concurrent.ExecutorService; |
| 56 | import java.util.concurrent.Executors; |
| 57 | import java.util.concurrent.ScheduledExecutorService; |
| 58 | import java.util.concurrent.TimeUnit; |
| 59 | import java.util.concurrent.atomic.AtomicBoolean; |
| 60 | import java.util.concurrent.atomic.AtomicReference; |
| 61 | import java.util.function.BiFunction; |
| 62 | import java.util.stream.Collectors; |
| 63 | |
| 64 | import static com.google.common.base.Preconditions.checkNotNull; |
| 65 | import static com.google.common.base.Preconditions.checkState; |
| 66 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 67 | import static org.onlab.util.BoundedThreadPool.newFixedThreadPool; |
| 68 | import static org.onlab.util.Tools.groupedThreads; |
| 69 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT; |
| 70 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Distributed Map implementation which uses optimistic replication and gossip |
| 74 | * based techniques to provide an eventually consistent data store. |
| 75 | */ |
| 76 | public class EventuallyConsistentMapImpl<K, V> |
| 77 | implements EventuallyConsistentMap<K, V> { |
| 78 | |
| 79 | private static final Logger log = LoggerFactory.getLogger(EventuallyConsistentMapImpl.class); |
| 80 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 81 | private final Map<K, MapValue<V>> items; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 82 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 83 | private final ClusterService clusterService; |
| 84 | private final ClusterCommunicationService clusterCommunicator; |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 85 | private final StoreSerializer serializer; |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 86 | private final NodeId localNodeId; |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 87 | private final PersistenceService persistenceService; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 88 | |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 89 | private final BiFunction<K, V, Timestamp> timestampProvider; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 90 | |
| 91 | private final MessageSubject updateMessageSubject; |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 92 | private final MessageSubject antiEntropyAdvertisementSubject; |
Jon Hall | d198b88 | 2016-05-18 16:44:40 -0700 | [diff] [blame] | 93 | private final MessageSubject updateRequestSubject; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 94 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 95 | private final Set<EventuallyConsistentMapListener<K, V>> listeners |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 96 | = Sets.newCopyOnWriteArraySet(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 97 | |
| 98 | private final ExecutorService executor; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 99 | private final ScheduledExecutorService backgroundExecutor; |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 100 | private final BiFunction<K, V, Collection<NodeId>> peerUpdateFunction; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 101 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 102 | private final ExecutorService communicationExecutor; |
| 103 | private final Map<NodeId, EventAccumulator> senderPending; |
Madan Jampani | 2872628 | 2015-02-19 11:40:23 -0800 | [diff] [blame] | 104 | |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 105 | private long previousTombstonePurgeTime; |
| 106 | private final Map<NodeId, Long> antiEntropyTimes = Maps.newConcurrentMap(); |
| 107 | |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 108 | private final String mapName; |
| 109 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 110 | private volatile boolean destroyed = false; |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 111 | private static final String ERROR_DESTROYED = " map is already destroyed"; |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 112 | private final String destroyedMessage; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 113 | |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 114 | private static final String ERROR_NULL_KEY = "Key cannot be null"; |
| 115 | private static final String ERROR_NULL_VALUE = "Null values are not allowed"; |
| 116 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 117 | private final long initialDelaySec = 5; |
| 118 | private final boolean lightweightAntiEntropy; |
| 119 | private final boolean tombstonesDisabled; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 120 | |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 121 | private static final int WINDOW_SIZE = 5; |
Jon Hall | dabee68 | 2016-05-17 11:29:51 -0700 | [diff] [blame] | 122 | private static final int HIGH_LOAD_THRESHOLD = 2; |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 123 | private static final int LOAD_WINDOW = 2; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 124 | private SlidingWindowCounter counter = new SlidingWindowCounter(WINDOW_SIZE); |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 125 | |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 126 | private final boolean persistent; |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 127 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 128 | /** |
| 129 | * Creates a new eventually consistent map shared amongst multiple instances. |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 130 | * <p> |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 131 | * See {@link org.onosproject.store.service.EventuallyConsistentMapBuilder} |
| 132 | * for more description of the parameters expected by the map. |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 133 | * </p> |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 134 | * |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 135 | * @param mapName a String identifier for the map. |
| 136 | * @param clusterService the cluster service |
| 137 | * @param clusterCommunicator the cluster communications service |
Thomas Vachuska | f5896be | 2016-05-19 14:30:50 -0700 | [diff] [blame] | 138 | * @param ns a Kryo namespace that can serialize |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 139 | * both K and V |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 140 | * @param timestampProvider provider of timestamps for K and V |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 141 | * @param peerUpdateFunction function that provides a set of nodes to immediately |
| 142 | * update to when there writes to the map |
| 143 | * @param eventExecutor executor to use for processing incoming |
| 144 | * events from peers |
| 145 | * @param communicationExecutor executor to use for sending events to peers |
| 146 | * @param backgroundExecutor executor to use for background anti-entropy |
| 147 | * tasks |
| 148 | * @param tombstonesDisabled true if this map should not maintain |
| 149 | * tombstones |
| 150 | * @param antiEntropyPeriod period that the anti-entropy task should run |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 151 | * @param antiEntropyTimeUnit time unit for anti-entropy period |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 152 | * @param convergeFaster make anti-entropy try to converge faster |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 153 | * @param persistent persist data to disk |
Jian Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 154 | * @param persistenceService persistence service |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 155 | */ |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 156 | EventuallyConsistentMapImpl(String mapName, |
| 157 | ClusterService clusterService, |
| 158 | ClusterCommunicationService clusterCommunicator, |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 159 | KryoNamespace ns, |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 160 | BiFunction<K, V, Timestamp> timestampProvider, |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 161 | BiFunction<K, V, Collection<NodeId>> peerUpdateFunction, |
| 162 | ExecutorService eventExecutor, |
| 163 | ExecutorService communicationExecutor, |
| 164 | ScheduledExecutorService backgroundExecutor, |
| 165 | boolean tombstonesDisabled, |
| 166 | long antiEntropyPeriod, |
| 167 | TimeUnit antiEntropyTimeUnit, |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 168 | boolean convergeFaster, |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 169 | boolean persistent, |
| 170 | PersistenceService persistenceService) { |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 171 | this.mapName = mapName; |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 172 | this.serializer = createSerializer(ns); |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 173 | this.persistenceService = persistenceService; |
| 174 | this.persistent = |
| 175 | persistent; |
| 176 | if (persistent) { |
| 177 | items = this.persistenceService.<K, MapValue<V>>persistentMapBuilder() |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame^] | 178 | .withName(mapName) |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 179 | .withSerializer(this.serializer) |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 180 | .build(); |
| 181 | } else { |
| 182 | items = Maps.newConcurrentMap(); |
| 183 | } |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 184 | senderPending = Maps.newConcurrentMap(); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 185 | destroyedMessage = mapName + ERROR_DESTROYED; |
Madan Jampani | 2872628 | 2015-02-19 11:40:23 -0800 | [diff] [blame] | 186 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 187 | this.clusterService = clusterService; |
| 188 | this.clusterCommunicator = clusterCommunicator; |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 189 | this.localNodeId = clusterService.getLocalNode().id(); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 190 | |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 191 | this.timestampProvider = timestampProvider; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 192 | |
| 193 | if (peerUpdateFunction != null) { |
| 194 | this.peerUpdateFunction = peerUpdateFunction; |
| 195 | } else { |
| 196 | this.peerUpdateFunction = (key, value) -> clusterService.getNodes().stream() |
| 197 | .map(ControllerNode::id) |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 198 | .filter(nodeId -> !nodeId.equals(localNodeId)) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 199 | .collect(Collectors.toList()); |
| 200 | } |
| 201 | |
| 202 | if (eventExecutor != null) { |
| 203 | this.executor = eventExecutor; |
| 204 | } else { |
| 205 | // should be a normal executor; it's used for receiving messages |
| 206 | this.executor = |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 207 | Executors.newFixedThreadPool(8, groupedThreads("onos/ecm", mapName + "-fg-%d", log)); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | if (communicationExecutor != null) { |
| 211 | this.communicationExecutor = communicationExecutor; |
| 212 | } else { |
| 213 | // sending executor; should be capped |
| 214 | //TODO this probably doesn't need to be bounded anymore |
| 215 | this.communicationExecutor = |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 216 | newFixedThreadPool(8, groupedThreads("onos/ecm", mapName + "-publish-%d", log)); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 219 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 220 | if (backgroundExecutor != null) { |
| 221 | this.backgroundExecutor = backgroundExecutor; |
| 222 | } else { |
| 223 | this.backgroundExecutor = |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 224 | newSingleThreadScheduledExecutor(groupedThreads("onos/ecm", mapName + "-bg-%d", log)); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 225 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 226 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 227 | // start anti-entropy thread |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 228 | this.backgroundExecutor.scheduleAtFixedRate(this::sendAdvertisement, |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 229 | initialDelaySec, antiEntropyPeriod, |
| 230 | antiEntropyTimeUnit); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 231 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 232 | updateMessageSubject = new MessageSubject("ecm-" + mapName + "-update"); |
| 233 | clusterCommunicator.addSubscriber(updateMessageSubject, |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 234 | serializer::decode, |
| 235 | this::processUpdates, |
| 236 | this.executor); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 237 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 238 | antiEntropyAdvertisementSubject = new MessageSubject("ecm-" + mapName + "-anti-entropy"); |
| 239 | clusterCommunicator.addSubscriber(antiEntropyAdvertisementSubject, |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 240 | serializer::decode, |
| 241 | this::handleAntiEntropyAdvertisement, |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 242 | serializer::encode, |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 243 | this.backgroundExecutor); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 244 | |
Jon Hall | d198b88 | 2016-05-18 16:44:40 -0700 | [diff] [blame] | 245 | updateRequestSubject = new MessageSubject("ecm-" + mapName + "-update-request"); |
| 246 | clusterCommunicator.addSubscriber(updateRequestSubject, |
| 247 | serializer::decode, |
| 248 | this::handleUpdateRequests, |
| 249 | this.backgroundExecutor); |
| 250 | |
Madan Jampani | a8f919e | 2016-04-18 16:47:35 -0700 | [diff] [blame] | 251 | if (!tombstonesDisabled) { |
| 252 | previousTombstonePurgeTime = 0; |
| 253 | this.backgroundExecutor.scheduleWithFixedDelay(this::purgeTombstones, |
| 254 | initialDelaySec, |
| 255 | antiEntropyPeriod, |
| 256 | TimeUnit.SECONDS); |
| 257 | } |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 258 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 259 | this.tombstonesDisabled = tombstonesDisabled; |
| 260 | this.lightweightAntiEntropy = !convergeFaster; |
Jon Hall | dabee68 | 2016-05-17 11:29:51 -0700 | [diff] [blame] | 261 | |
| 262 | // Initiate first round of Gossip |
| 263 | this.bootstrap(); |
Madan Jampani | e135628 | 2015-03-10 19:05:36 -0700 | [diff] [blame] | 264 | } |
| 265 | |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 266 | private StoreSerializer createSerializer(KryoNamespace ns) { |
| 267 | return StoreSerializer.using(KryoNamespace.newBuilder() |
| 268 | .register(ns) |
| 269 | // not so robust way to avoid collision with other |
| 270 | // user supplied registrations |
| 271 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID + 100) |
| 272 | .register(KryoNamespaces.BASIC) |
| 273 | .register(LogicalTimestamp.class) |
| 274 | .register(WallClockTimestamp.class) |
| 275 | .register(AntiEntropyAdvertisement.class) |
| 276 | .register(AntiEntropyResponse.class) |
| 277 | .register(UpdateEntry.class) |
| 278 | .register(MapValue.class) |
| 279 | .register(MapValue.Digest.class) |
Jonathan Hart | 9dc5f09 | 2016-06-17 15:15:17 -0700 | [diff] [blame] | 280 | .register(UpdateRequest.class) |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 281 | .build(name() + "-ecmap")); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | @Override |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 285 | public String name() { |
| 286 | return mapName; |
| 287 | } |
| 288 | |
| 289 | @Override |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 290 | public int size() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 291 | checkState(!destroyed, destroyedMessage); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 292 | // TODO: Maintain a separate counter for tracking live elements in map. |
| 293 | return Maps.filterValues(items, MapValue::isAlive).size(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public boolean isEmpty() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 298 | checkState(!destroyed, destroyedMessage); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 299 | return size() == 0; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public boolean containsKey(K key) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 304 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 305 | checkNotNull(key, ERROR_NULL_KEY); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 306 | return get(key) != null; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public boolean containsValue(V value) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 311 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 312 | checkNotNull(value, ERROR_NULL_VALUE); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 313 | return items.values() |
| 314 | .stream() |
| 315 | .filter(MapValue::isAlive) |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 316 | .anyMatch(v -> value.equals(v.get())); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public V get(K key) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 321 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 322 | checkNotNull(key, ERROR_NULL_KEY); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 323 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 324 | MapValue<V> value = items.get(key); |
| 325 | return (value == null || value.isTombstone()) ? null : value.get(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public void put(K key, V value) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 330 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 331 | checkNotNull(key, ERROR_NULL_KEY); |
| 332 | checkNotNull(value, ERROR_NULL_VALUE); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 333 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 334 | MapValue<V> newValue = new MapValue<>(value, timestampProvider.apply(key, value)); |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 335 | if (putInternal(key, newValue)) { |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 336 | notifyPeers(new UpdateEntry<>(key, newValue), peerUpdateFunction.apply(key, value)); |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 337 | notifyListeners(new EventuallyConsistentMapEvent<>(mapName, PUT, key, value)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 341 | @Override |
Madan Jampani | 43e9c9c | 2015-06-26 14:16:46 -0700 | [diff] [blame] | 342 | public V remove(K key) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 343 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 344 | checkNotNull(key, ERROR_NULL_KEY); |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 345 | return removeAndNotify(key, null); |
| 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public void remove(K key, V value) { |
| 350 | checkState(!destroyed, destroyedMessage); |
| 351 | checkNotNull(key, ERROR_NULL_KEY); |
| 352 | checkNotNull(value, ERROR_NULL_VALUE); |
| 353 | removeAndNotify(key, value); |
| 354 | } |
| 355 | |
| 356 | private V removeAndNotify(K key, V value) { |
Madan Jampani | 483d0a2 | 2015-08-19 17:33:00 -0700 | [diff] [blame] | 357 | Timestamp timestamp = timestampProvider.apply(key, value); |
| 358 | Optional<MapValue<V>> tombstone = tombstonesDisabled || timestamp == null |
| 359 | ? Optional.empty() : Optional.of(MapValue.tombstone(timestamp)); |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 360 | MapValue<V> previousValue = removeInternal(key, Optional.ofNullable(value), tombstone); |
Madan Jampani | a0ac487 | 2015-07-02 11:23:49 -0700 | [diff] [blame] | 361 | if (previousValue != null) { |
Madan Jampani | 483d0a2 | 2015-08-19 17:33:00 -0700 | [diff] [blame] | 362 | notifyPeers(new UpdateEntry<>(key, tombstone.orElse(null)), |
| 363 | peerUpdateFunction.apply(key, previousValue.get())); |
Madan Jampani | a0ac487 | 2015-07-02 11:23:49 -0700 | [diff] [blame] | 364 | if (previousValue.isAlive()) { |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 365 | notifyListeners(new EventuallyConsistentMapEvent<>(mapName, REMOVE, key, previousValue.get())); |
Madan Jampani | a0ac487 | 2015-07-02 11:23:49 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 368 | return previousValue != null ? previousValue.get() : null; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Madan Jampani | 483d0a2 | 2015-08-19 17:33:00 -0700 | [diff] [blame] | 371 | private MapValue<V> removeInternal(K key, Optional<V> value, Optional<MapValue<V>> tombstone) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 372 | checkState(!destroyed, destroyedMessage); |
| 373 | checkNotNull(key, ERROR_NULL_KEY); |
| 374 | checkNotNull(value, ERROR_NULL_VALUE); |
Madan Jampani | 483d0a2 | 2015-08-19 17:33:00 -0700 | [diff] [blame] | 375 | tombstone.ifPresent(v -> checkState(v.isTombstone())); |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 376 | |
| 377 | counter.incrementCount(); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 378 | AtomicBoolean updated = new AtomicBoolean(false); |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 379 | AtomicReference<MapValue<V>> previousValue = new AtomicReference<>(); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 380 | items.compute(key, (k, existing) -> { |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 381 | boolean valueMatches = true; |
| 382 | if (value.isPresent() && existing != null && existing.isAlive()) { |
| 383 | valueMatches = Objects.equals(value.get(), existing.get()); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 384 | } |
Brian O'Connor | 6325dad | 2015-07-07 15:36:29 -0700 | [diff] [blame] | 385 | if (existing == null) { |
Jonathan Hart | 4a29c59 | 2015-09-23 17:55:07 -0700 | [diff] [blame] | 386 | log.trace("ECMap Remove: Existing value for key {} is already null", k); |
Brian O'Connor | 6325dad | 2015-07-07 15:36:29 -0700 | [diff] [blame] | 387 | } |
Madan Jampani | 483d0a2 | 2015-08-19 17:33:00 -0700 | [diff] [blame] | 388 | if (valueMatches) { |
| 389 | if (existing == null) { |
| 390 | updated.set(tombstone.isPresent()); |
| 391 | } else { |
| 392 | updated.set(!tombstone.isPresent() || tombstone.get().isNewerThan(existing)); |
| 393 | } |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 394 | } |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 395 | if (updated.get()) { |
Madan Jampani | 483d0a2 | 2015-08-19 17:33:00 -0700 | [diff] [blame] | 396 | previousValue.set(existing); |
| 397 | return tombstone.orElse(null); |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 398 | } else { |
| 399 | return existing; |
| 400 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 401 | }); |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 402 | return previousValue.get(); |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | @Override |
Madan Jampani | 4727a11 | 2015-07-16 12:12:58 -0700 | [diff] [blame] | 406 | public V compute(K key, BiFunction<K, V, V> recomputeFunction) { |
| 407 | checkState(!destroyed, destroyedMessage); |
| 408 | checkNotNull(key, ERROR_NULL_KEY); |
| 409 | checkNotNull(recomputeFunction, "Recompute function cannot be null"); |
| 410 | |
| 411 | AtomicBoolean updated = new AtomicBoolean(false); |
| 412 | AtomicReference<MapValue<V>> previousValue = new AtomicReference<>(); |
Madan Jampani | dc01297 | 2016-04-25 11:13:26 -0700 | [diff] [blame] | 413 | MapValue<V> computedValue = items.compute(serializer.copy(key), (k, mv) -> { |
Madan Jampani | 4727a11 | 2015-07-16 12:12:58 -0700 | [diff] [blame] | 414 | previousValue.set(mv); |
| 415 | V newRawValue = recomputeFunction.apply(key, mv == null ? null : mv.get()); |
Brian O'Connor | 05acd64 | 2016-08-03 00:42:35 -0700 | [diff] [blame] | 416 | if (mv != null && Objects.equals(newRawValue, mv.get())) { |
| 417 | // value was not updated |
| 418 | return mv; |
| 419 | } |
Madan Jampani | 4727a11 | 2015-07-16 12:12:58 -0700 | [diff] [blame] | 420 | MapValue<V> newValue = new MapValue<>(newRawValue, timestampProvider.apply(key, newRawValue)); |
| 421 | if (mv == null || newValue.isNewerThan(mv)) { |
| 422 | updated.set(true); |
Madan Jampani | dc01297 | 2016-04-25 11:13:26 -0700 | [diff] [blame] | 423 | // We return a copy to ensure updates to peers can be serialized. |
| 424 | // This prevents replica divergence due to serialization failures. |
| 425 | return serializer.copy(newValue); |
Madan Jampani | 4727a11 | 2015-07-16 12:12:58 -0700 | [diff] [blame] | 426 | } else { |
| 427 | return mv; |
| 428 | } |
| 429 | }); |
| 430 | if (updated.get()) { |
| 431 | notifyPeers(new UpdateEntry<>(key, computedValue), peerUpdateFunction.apply(key, computedValue.get())); |
| 432 | EventuallyConsistentMapEvent.Type updateType = computedValue.isTombstone() ? REMOVE : PUT; |
| 433 | V value = computedValue.isTombstone() |
| 434 | ? previousValue.get() == null ? null : previousValue.get().get() |
| 435 | : computedValue.get(); |
| 436 | if (value != null) { |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 437 | notifyListeners(new EventuallyConsistentMapEvent<>(mapName, updateType, key, value)); |
Madan Jampani | 4727a11 | 2015-07-16 12:12:58 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | return computedValue.get(); |
| 441 | } |
| 442 | |
| 443 | @Override |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 444 | public void putAll(Map<? extends K, ? extends V> m) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 445 | checkState(!destroyed, destroyedMessage); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 446 | m.forEach(this::put); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | @Override |
| 450 | public void clear() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 451 | checkState(!destroyed, destroyedMessage); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 452 | Maps.filterValues(items, MapValue::isAlive) |
| 453 | .forEach((k, v) -> remove(k)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | @Override |
| 457 | public Set<K> keySet() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 458 | checkState(!destroyed, destroyedMessage); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 459 | return Maps.filterValues(items, MapValue::isAlive) |
| 460 | .keySet(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | @Override |
| 464 | public Collection<V> values() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 465 | checkState(!destroyed, destroyedMessage); |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 466 | return Collections2.transform(Maps.filterValues(items, MapValue::isAlive).values(), MapValue::get); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | @Override |
| 470 | public Set<Map.Entry<K, V>> entrySet() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 471 | checkState(!destroyed, destroyedMessage); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 472 | return Maps.filterValues(items, MapValue::isAlive) |
| 473 | .entrySet() |
| 474 | .stream() |
| 475 | .map(e -> Pair.of(e.getKey(), e.getValue().get())) |
| 476 | .collect(Collectors.toSet()); |
| 477 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 478 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 479 | /** |
| 480 | * Returns true if newValue was accepted i.e. map is updated. |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 481 | * |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 482 | * @param key key |
| 483 | * @param newValue proposed new value |
| 484 | * @return true if update happened; false if map already contains a more recent value for the key |
| 485 | */ |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 486 | private boolean putInternal(K key, MapValue<V> newValue) { |
| 487 | checkState(!destroyed, destroyedMessage); |
| 488 | checkNotNull(key, ERROR_NULL_KEY); |
| 489 | checkNotNull(newValue, ERROR_NULL_VALUE); |
| 490 | checkState(newValue.isAlive()); |
| 491 | counter.incrementCount(); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 492 | AtomicBoolean updated = new AtomicBoolean(false); |
| 493 | items.compute(key, (k, existing) -> { |
| 494 | if (existing == null || newValue.isNewerThan(existing)) { |
| 495 | updated.set(true); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 496 | return newValue; |
| 497 | } |
| 498 | return existing; |
| 499 | }); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 500 | return updated.get(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | @Override |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 504 | public void addListener(EventuallyConsistentMapListener<K, V> listener) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 505 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 506 | |
| 507 | listeners.add(checkNotNull(listener)); |
| 508 | } |
| 509 | |
| 510 | @Override |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 511 | public void removeListener(EventuallyConsistentMapListener<K, V> listener) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 512 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 513 | |
| 514 | listeners.remove(checkNotNull(listener)); |
| 515 | } |
| 516 | |
| 517 | @Override |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 518 | public CompletableFuture<Void> destroy() { |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 519 | destroyed = true; |
| 520 | |
| 521 | executor.shutdown(); |
| 522 | backgroundExecutor.shutdown(); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 523 | communicationExecutor.shutdown(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 524 | |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 525 | listeners.clear(); |
| 526 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 527 | clusterCommunicator.removeSubscriber(updateMessageSubject); |
Jon Hall | d198b88 | 2016-05-18 16:44:40 -0700 | [diff] [blame] | 528 | clusterCommunicator.removeSubscriber(updateRequestSubject); |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 529 | clusterCommunicator.removeSubscriber(antiEntropyAdvertisementSubject); |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 530 | return CompletableFuture.completedFuture(null); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 533 | private void notifyListeners(EventuallyConsistentMapEvent<K, V> event) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 534 | listeners.forEach(listener -> listener.event(event)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 535 | } |
| 536 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 537 | private void notifyPeers(UpdateEntry<K, V> event, Collection<NodeId> peers) { |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 538 | queueUpdate(event, peers); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 541 | private void queueUpdate(UpdateEntry<K, V> event, Collection<NodeId> peers) { |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 542 | if (peers == null) { |
| 543 | // we have no friends :( |
| 544 | return; |
| 545 | } |
| 546 | peers.forEach(node -> |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 547 | senderPending.computeIfAbsent(node, unusedKey -> new EventAccumulator(node)).add(event) |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 548 | ); |
| 549 | } |
| 550 | |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 551 | private boolean underHighLoad() { |
| 552 | return counter.get(LOAD_WINDOW) > HIGH_LOAD_THRESHOLD; |
| 553 | } |
| 554 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 555 | private void sendAdvertisement() { |
| 556 | try { |
Thomas Vachuska | 152f9fd | 2015-04-02 16:28:13 -0700 | [diff] [blame] | 557 | if (underHighLoad() || destroyed) { |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 558 | return; |
| 559 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 560 | pickRandomActivePeer().ifPresent(this::sendAdvertisementToPeer); |
| 561 | } catch (Exception e) { |
| 562 | // Catch all exceptions to avoid scheduled task being suppressed. |
| 563 | log.error("Exception thrown while sending advertisement", e); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 567 | private Optional<NodeId> pickRandomActivePeer() { |
| 568 | List<NodeId> activePeers = clusterService.getNodes() |
| 569 | .stream() |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 570 | .map(ControllerNode::id) |
| 571 | .filter(id -> !localNodeId.equals(id)) |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 572 | .filter(id -> clusterService.getState(id).isActive()) |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 573 | .collect(Collectors.toList()); |
| 574 | Collections.shuffle(activePeers); |
| 575 | return activePeers.isEmpty() ? Optional.empty() : Optional.of(activePeers.get(0)); |
| 576 | } |
| 577 | |
| 578 | private void sendAdvertisementToPeer(NodeId peer) { |
Madan Jampani | ee35d55 | 2016-04-26 16:07:40 -0700 | [diff] [blame] | 579 | long adCreationTime = System.currentTimeMillis(); |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 580 | AntiEntropyAdvertisement<K> ad = createAdvertisement(); |
| 581 | clusterCommunicator.sendAndReceive(ad, |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 582 | antiEntropyAdvertisementSubject, |
| 583 | serializer::encode, |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 584 | serializer::decode, |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 585 | peer) |
| 586 | .whenComplete((result, error) -> { |
| 587 | if (error != null) { |
Jonathan Hart | e255cc4 | 2016-09-12 14:50:24 -0700 | [diff] [blame] | 588 | log.debug("Failed to send anti-entropy advertisement to {}: {}", |
| 589 | peer, error.getMessage()); |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 590 | } else if (result == AntiEntropyResponse.PROCESSED) { |
Madan Jampani | ee35d55 | 2016-04-26 16:07:40 -0700 | [diff] [blame] | 591 | antiEntropyTimes.put(peer, adCreationTime); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 592 | } |
| 593 | }); |
| 594 | } |
| 595 | |
Jon Hall | d198b88 | 2016-05-18 16:44:40 -0700 | [diff] [blame] | 596 | private void sendUpdateRequestToPeer(NodeId peer, Set<K> keys) { |
| 597 | UpdateRequest<K> request = new UpdateRequest<>(localNodeId, keys); |
| 598 | clusterCommunicator.unicast(request, |
| 599 | updateRequestSubject, |
| 600 | serializer::encode, |
| 601 | peer) |
| 602 | .whenComplete((result, error) -> { |
| 603 | if (error != null) { |
Jonathan Hart | e255cc4 | 2016-09-12 14:50:24 -0700 | [diff] [blame] | 604 | log.debug("Failed to send update request to {}: {}", |
| 605 | peer, error.getMessage()); |
Jon Hall | d198b88 | 2016-05-18 16:44:40 -0700 | [diff] [blame] | 606 | } |
| 607 | }); |
| 608 | } |
| 609 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 610 | private AntiEntropyAdvertisement<K> createAdvertisement() { |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 611 | return new AntiEntropyAdvertisement<>(localNodeId, |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 612 | ImmutableMap.copyOf(Maps.transformValues(items, MapValue::digest))); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 613 | } |
| 614 | |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 615 | private AntiEntropyResponse handleAntiEntropyAdvertisement(AntiEntropyAdvertisement<K> ad) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 616 | if (destroyed || underHighLoad()) { |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 617 | return AntiEntropyResponse.IGNORED; |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 618 | } |
| 619 | try { |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 620 | if (log.isTraceEnabled()) { |
| 621 | log.trace("Received anti-entropy advertisement from {} for {} with {} entries in it", |
Jon Hall | d198b88 | 2016-05-18 16:44:40 -0700 | [diff] [blame] | 622 | ad.sender(), mapName, ad.digest().size()); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 623 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 624 | antiEntropyCheckLocalItems(ad).forEach(this::notifyListeners); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 625 | } catch (Exception e) { |
| 626 | log.warn("Error handling anti-entropy advertisement", e); |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 627 | return AntiEntropyResponse.FAILED; |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 628 | } |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 629 | return AntiEntropyResponse.PROCESSED; |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | /** |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 633 | * Processes anti-entropy ad from peer by taking following actions: |
| 634 | * 1. If peer has an old entry, updates peer. |
| 635 | * 2. If peer indicates an entry is removed and has a more recent |
| 636 | * timestamp than the local entry, update local state. |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 637 | */ |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 638 | private List<EventuallyConsistentMapEvent<K, V>> antiEntropyCheckLocalItems( |
| 639 | AntiEntropyAdvertisement<K> ad) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 640 | final List<EventuallyConsistentMapEvent<K, V>> externalEvents = Lists.newLinkedList(); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 641 | final NodeId sender = ad.sender(); |
Jon Hall | d198b88 | 2016-05-18 16:44:40 -0700 | [diff] [blame] | 642 | final List<NodeId> peers = ImmutableList.of(sender); |
| 643 | Set<K> staleOrMissing = new HashSet<>(); |
| 644 | Set<K> locallyUnknown = new HashSet<>(ad.digest().keySet()); |
| 645 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 646 | items.forEach((key, localValue) -> { |
Jon Hall | d198b88 | 2016-05-18 16:44:40 -0700 | [diff] [blame] | 647 | locallyUnknown.remove(key); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 648 | MapValue.Digest remoteValueDigest = ad.digest().get(key); |
| 649 | if (remoteValueDigest == null || localValue.isNewerThan(remoteValueDigest.timestamp())) { |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 650 | // local value is more recent, push to sender |
Jon Hall | d198b88 | 2016-05-18 16:44:40 -0700 | [diff] [blame] | 651 | queueUpdate(new UpdateEntry<>(key, localValue), peers); |
| 652 | } else if (remoteValueDigest != null |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 653 | && remoteValueDigest.isNewerThan(localValue.digest()) |
| 654 | && remoteValueDigest.isTombstone()) { |
Jon Hall | d198b88 | 2016-05-18 16:44:40 -0700 | [diff] [blame] | 655 | // remote value is more recent and a tombstone: update local value |
Madan Jampani | 483d0a2 | 2015-08-19 17:33:00 -0700 | [diff] [blame] | 656 | MapValue<V> tombstone = MapValue.tombstone(remoteValueDigest.timestamp()); |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 657 | MapValue<V> previousValue = removeInternal(key, |
| 658 | Optional.empty(), |
Madan Jampani | 483d0a2 | 2015-08-19 17:33:00 -0700 | [diff] [blame] | 659 | Optional.of(tombstone)); |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 660 | if (previousValue != null && previousValue.isAlive()) { |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 661 | externalEvents.add(new EventuallyConsistentMapEvent<>(mapName, REMOVE, key, previousValue.get())); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 662 | } |
Jon Hall | d198b88 | 2016-05-18 16:44:40 -0700 | [diff] [blame] | 663 | } else if (remoteValueDigest.isNewerThan(localValue.digest())) { |
| 664 | // Not a tombstone and remote is newer |
| 665 | staleOrMissing.add(key); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 666 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 667 | }); |
Jon Hall | d198b88 | 2016-05-18 16:44:40 -0700 | [diff] [blame] | 668 | // Keys missing in local map |
| 669 | staleOrMissing.addAll(locallyUnknown); |
| 670 | // Request updates that we missed out on |
| 671 | sendUpdateRequestToPeer(sender, staleOrMissing); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 672 | return externalEvents; |
| 673 | } |
| 674 | |
Jon Hall | d198b88 | 2016-05-18 16:44:40 -0700 | [diff] [blame] | 675 | private void handleUpdateRequests(UpdateRequest<K> request) { |
| 676 | final Set<K> keys = request.keys(); |
| 677 | final NodeId sender = request.sender(); |
| 678 | final List<NodeId> peers = ImmutableList.of(sender); |
| 679 | |
| 680 | keys.forEach(key -> |
| 681 | queueUpdate(new UpdateEntry<>(key, items.get(key)), peers) |
| 682 | ); |
| 683 | } |
| 684 | |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 685 | private void purgeTombstones() { |
| 686 | /* |
Jon Hall | d198b88 | 2016-05-18 16:44:40 -0700 | [diff] [blame] | 687 | * In order to mitigate the resource exhaustion that can ensue due to an ever-growing set |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 688 | * of tombstones we employ the following heuristic to purge old tombstones periodically. |
| 689 | * First, we keep track of the time (local system time) when we were able to have a successful |
| 690 | * AE exchange with each peer. The smallest (or oldest) such time across *all* peers is regarded |
| 691 | * as the time before which all tombstones are considered safe to purge. |
| 692 | */ |
Madan Jampani | a8f919e | 2016-04-18 16:47:35 -0700 | [diff] [blame] | 693 | long currentSafeTombstonePurgeTime = clusterService.getNodes() |
| 694 | .stream() |
| 695 | .map(ControllerNode::id) |
| 696 | .filter(id -> !id.equals(localNodeId)) |
| 697 | .map(id -> antiEntropyTimes.getOrDefault(id, 0L)) |
| 698 | .reduce(Math::min) |
| 699 | .orElse(0L); |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 700 | if (currentSafeTombstonePurgeTime == previousTombstonePurgeTime) { |
| 701 | return; |
| 702 | } |
| 703 | List<Map.Entry<K, MapValue<V>>> tombStonesToDelete = items.entrySet() |
| 704 | .stream() |
| 705 | .filter(e -> e.getValue().isTombstone()) |
| 706 | .filter(e -> e.getValue().creationTime() <= currentSafeTombstonePurgeTime) |
| 707 | .collect(Collectors.toList()); |
| 708 | previousTombstonePurgeTime = currentSafeTombstonePurgeTime; |
| 709 | tombStonesToDelete.forEach(entry -> items.remove(entry.getKey(), entry.getValue())); |
| 710 | } |
| 711 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 712 | private void processUpdates(Collection<UpdateEntry<K, V>> updates) { |
| 713 | if (destroyed) { |
| 714 | return; |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 715 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 716 | updates.forEach(update -> { |
| 717 | final K key = update.key(); |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 718 | final MapValue<V> value = update.value() == null ? null : update.value().copy(); |
Madan Jampani | 483d0a2 | 2015-08-19 17:33:00 -0700 | [diff] [blame] | 719 | if (value == null || value.isTombstone()) { |
| 720 | MapValue<V> previousValue = removeInternal(key, Optional.empty(), Optional.ofNullable(value)); |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 721 | if (previousValue != null && previousValue.isAlive()) { |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 722 | notifyListeners(new EventuallyConsistentMapEvent<>(mapName, REMOVE, key, previousValue.get())); |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 723 | } |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 724 | } else if (putInternal(key, value)) { |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 725 | notifyListeners(new EventuallyConsistentMapEvent<>(mapName, PUT, key, value.get())); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 726 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 727 | }); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 728 | } |
| 729 | |
Jon Hall | dabee68 | 2016-05-17 11:29:51 -0700 | [diff] [blame] | 730 | private void bootstrap() { |
| 731 | /* |
| 732 | * Attempt to get in sync with the cluster when a map is created. This is to help avoid a new node |
| 733 | * writing to an ECM until it has a view of the map. Depending on how lightweight the map instance |
| 734 | * is, this will attempt to advertise to all or some of the peers. |
| 735 | */ |
| 736 | int n = 0; |
| 737 | List<NodeId> activePeers = clusterService.getNodes() |
| 738 | .stream() |
| 739 | .map(ControllerNode::id) |
| 740 | .filter(id -> !localNodeId.equals(id)) |
| 741 | .filter(id -> clusterService.getState(id).isActive()) |
| 742 | .collect(Collectors.toList()); |
| 743 | |
| 744 | if (activePeers.isEmpty()) { |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | if (lightweightAntiEntropy) { |
| 749 | n = activePeers.size() / 2; |
| 750 | } else { |
| 751 | n = activePeers.size(); |
| 752 | } |
| 753 | |
| 754 | for (int i = 0; i < n; i++) { |
| 755 | sendAdvertisementToPeer(activePeers.get(i)); |
| 756 | } |
| 757 | } |
| 758 | |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 759 | // TODO pull this into the class if this gets pulled out... |
| 760 | private static final int DEFAULT_MAX_EVENTS = 1000; |
| 761 | private static final int DEFAULT_MAX_IDLE_MS = 10; |
| 762 | private static final int DEFAULT_MAX_BATCH_MS = 50; |
| 763 | private static final Timer TIMER = new Timer("onos-ecm-sender-events"); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 764 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 765 | private final class EventAccumulator extends AbstractAccumulator<UpdateEntry<K, V>> { |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 766 | |
| 767 | private final NodeId peer; |
| 768 | |
| 769 | private EventAccumulator(NodeId peer) { |
| 770 | super(TIMER, DEFAULT_MAX_EVENTS, DEFAULT_MAX_BATCH_MS, DEFAULT_MAX_IDLE_MS); |
| 771 | this.peer = peer; |
| 772 | } |
| 773 | |
| 774 | @Override |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 775 | public void processItems(List<UpdateEntry<K, V>> items) { |
| 776 | Map<K, UpdateEntry<K, V>> map = Maps.newHashMap(); |
| 777 | items.forEach(item -> map.compute(item.key(), (key, existing) -> |
Madan Jampani | 92c64eb | 2015-07-23 15:37:07 -0700 | [diff] [blame] | 778 | item.isNewerThan(existing) ? item : existing)); |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 779 | communicationExecutor.execute(() -> { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 780 | clusterCommunicator.unicast(ImmutableList.copyOf(map.values()), |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 781 | updateMessageSubject, |
| 782 | serializer::encode, |
| 783 | peer) |
| 784 | .whenComplete((result, error) -> { |
| 785 | if (error != null) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 786 | log.debug("Failed to send to {}", peer, error); |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 787 | } |
| 788 | }); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 789 | }); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 790 | } |
| 791 | } |
Jonathan Hart | 4a29c59 | 2015-09-23 17:55:07 -0700 | [diff] [blame] | 792 | } |