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