Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | */ |
Jonathan Hart | 77bdd26 | 2015-02-03 09:07:48 -0800 | [diff] [blame] | 16 | package org.onosproject.store.ecmap; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 17 | |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Maps; |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 21 | import org.apache.commons.lang3.RandomUtils; |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 22 | import org.apache.commons.lang3.mutable.MutableBoolean; |
Jonathan Hart | f910823 | 2015-02-02 16:37:35 -0800 | [diff] [blame] | 23 | import org.apache.commons.lang3.tuple.Pair; |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 24 | import org.onlab.util.AbstractAccumulator; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 25 | import org.onlab.util.KryoNamespace; |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 26 | import org.onlab.util.SlidingWindowCounter; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 27 | import org.onosproject.cluster.ClusterService; |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 28 | import org.onosproject.cluster.ControllerNode; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 29 | import org.onosproject.cluster.NodeId; |
| 30 | import org.onosproject.store.Timestamp; |
| 31 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 32 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 33 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 34 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Jonathan Hart | 77bdd26 | 2015-02-03 09:07:48 -0800 | [diff] [blame] | 35 | import org.onosproject.store.impl.Timestamped; |
| 36 | import org.onosproject.store.impl.WallClockTimestamp; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 37 | import org.onosproject.store.serializers.KryoSerializer; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 38 | import org.onosproject.store.service.ClockService; |
| 39 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 40 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 41 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 45 | import java.util.ArrayList; |
| 46 | import java.util.Collection; |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 47 | import java.util.HashMap; |
| 48 | import java.util.LinkedList; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 49 | import java.util.List; |
| 50 | import java.util.Map; |
| 51 | import java.util.Set; |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 52 | import java.util.Timer; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 53 | import java.util.concurrent.ConcurrentHashMap; |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 54 | import java.util.concurrent.ConcurrentMap; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 55 | import java.util.concurrent.CopyOnWriteArraySet; |
| 56 | import java.util.concurrent.ExecutorService; |
| 57 | import java.util.concurrent.Executors; |
| 58 | import java.util.concurrent.ScheduledExecutorService; |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 59 | import java.util.concurrent.TimeUnit; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 60 | import java.util.function.BiFunction; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 61 | import java.util.stream.Collectors; |
| 62 | |
| 63 | import static com.google.common.base.Preconditions.checkNotNull; |
| 64 | import static com.google.common.base.Preconditions.checkState; |
| 65 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Brian O'Connor | c6713a8 | 2015-02-24 11:55:48 -0800 | [diff] [blame] | 66 | import static org.onlab.util.BoundedThreadPool.newFixedThreadPool; |
Brian O'Connor | 4b2ba5f | 2015-02-18 20:54:00 -0800 | [diff] [blame] | 67 | import static org.onlab.util.Tools.groupedThreads; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Distributed Map implementation which uses optimistic replication and gossip |
| 71 | * based techniques to provide an eventually consistent data store. |
| 72 | */ |
| 73 | public class EventuallyConsistentMapImpl<K, V> |
| 74 | implements EventuallyConsistentMap<K, V> { |
| 75 | |
| 76 | private static final Logger log = LoggerFactory.getLogger(EventuallyConsistentMapImpl.class); |
| 77 | |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 78 | private final ConcurrentMap<K, Timestamped<V>> items; |
| 79 | private final ConcurrentMap<K, Timestamp> removedItems; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 80 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 81 | private final ClusterService clusterService; |
| 82 | private final ClusterCommunicationService clusterCommunicator; |
| 83 | private final KryoSerializer serializer; |
| 84 | |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 85 | private final ClockService<K, V> clockService; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 86 | |
| 87 | private final MessageSubject updateMessageSubject; |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 88 | private final MessageSubject antiEntropyAdvertisementSubject; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 89 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 90 | private final Set<EventuallyConsistentMapListener<K, V>> listeners |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 91 | = new CopyOnWriteArraySet<>(); |
| 92 | |
| 93 | private final ExecutorService executor; |
| 94 | |
| 95 | private final ScheduledExecutorService backgroundExecutor; |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 96 | private final BiFunction<K, V, Collection<NodeId>> peerUpdateFunction; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 97 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 98 | private final ExecutorService communicationExecutor; |
| 99 | private final Map<NodeId, EventAccumulator> senderPending; |
Madan Jampani | 2872628 | 2015-02-19 11:40:23 -0800 | [diff] [blame] | 100 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 101 | private volatile boolean destroyed = false; |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 102 | private static final String ERROR_DESTROYED = " map is already destroyed"; |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 103 | private final String destroyedMessage; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 104 | |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 105 | private static final String ERROR_NULL_KEY = "Key cannot be null"; |
| 106 | private static final String ERROR_NULL_VALUE = "Null values are not allowed"; |
| 107 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 108 | private final long initialDelaySec = 5; |
| 109 | private final boolean lightweightAntiEntropy; |
| 110 | private final boolean tombstonesDisabled; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 111 | |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 112 | private static final int WINDOW_SIZE = 5; |
| 113 | private static final int HIGH_LOAD_THRESHOLD = 0; |
| 114 | private static final int LOAD_WINDOW = 2; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 115 | private SlidingWindowCounter counter = new SlidingWindowCounter(WINDOW_SIZE); |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 116 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 117 | /** |
| 118 | * Creates a new eventually consistent map shared amongst multiple instances. |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 119 | * <p> |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 120 | * See {@link org.onosproject.store.service.EventuallyConsistentMapBuilder} |
| 121 | * for more description of the parameters expected by the map. |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 122 | * </p> |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 123 | * |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 124 | * @param mapName a String identifier for the map. |
| 125 | * @param clusterService the cluster service |
| 126 | * @param clusterCommunicator the cluster communications service |
| 127 | * @param serializerBuilder a Kryo namespace builder that can serialize |
| 128 | * both K and V |
| 129 | * @param clockService a clock service able to generate timestamps |
| 130 | * for K and V |
| 131 | * @param peerUpdateFunction function that provides a set of nodes to immediately |
| 132 | * update to when there writes to the map |
| 133 | * @param eventExecutor executor to use for processing incoming |
| 134 | * events from peers |
| 135 | * @param communicationExecutor executor to use for sending events to peers |
| 136 | * @param backgroundExecutor executor to use for background anti-entropy |
| 137 | * tasks |
| 138 | * @param tombstonesDisabled true if this map should not maintain |
| 139 | * tombstones |
| 140 | * @param antiEntropyPeriod period that the anti-entropy task should run |
| 141 | * in seconds |
Thomas Vachuska | 00121ed | 2015-04-02 17:07:57 -0700 | [diff] [blame] | 142 | * @param antiEntropyTimeUnit time unit for anti-entropy task scheduling |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 143 | * @param convergeFaster make anti-entropy try to converge faster |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 144 | */ |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 145 | EventuallyConsistentMapImpl(String mapName, |
| 146 | ClusterService clusterService, |
| 147 | ClusterCommunicationService clusterCommunicator, |
| 148 | KryoNamespace.Builder serializerBuilder, |
| 149 | ClockService<K, V> clockService, |
| 150 | BiFunction<K, V, Collection<NodeId>> peerUpdateFunction, |
| 151 | ExecutorService eventExecutor, |
| 152 | ExecutorService communicationExecutor, |
| 153 | ScheduledExecutorService backgroundExecutor, |
| 154 | boolean tombstonesDisabled, |
| 155 | long antiEntropyPeriod, |
| 156 | TimeUnit antiEntropyTimeUnit, |
| 157 | boolean convergeFaster) { |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 158 | items = new ConcurrentHashMap<>(); |
| 159 | removedItems = new ConcurrentHashMap<>(); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 160 | senderPending = Maps.newConcurrentMap(); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 161 | destroyedMessage = mapName + ERROR_DESTROYED; |
Madan Jampani | 2872628 | 2015-02-19 11:40:23 -0800 | [diff] [blame] | 162 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 163 | this.clusterService = clusterService; |
| 164 | this.clusterCommunicator = clusterCommunicator; |
| 165 | |
| 166 | this.serializer = createSerializer(serializerBuilder); |
| 167 | |
| 168 | this.clockService = clockService; |
| 169 | |
| 170 | if (peerUpdateFunction != null) { |
| 171 | this.peerUpdateFunction = peerUpdateFunction; |
| 172 | } else { |
| 173 | this.peerUpdateFunction = (key, value) -> clusterService.getNodes().stream() |
| 174 | .map(ControllerNode::id) |
| 175 | .filter(nodeId -> !nodeId.equals(clusterService.getLocalNode().id())) |
| 176 | .collect(Collectors.toList()); |
| 177 | } |
| 178 | |
| 179 | if (eventExecutor != null) { |
| 180 | this.executor = eventExecutor; |
| 181 | } else { |
| 182 | // should be a normal executor; it's used for receiving messages |
| 183 | this.executor = |
| 184 | Executors.newFixedThreadPool(8, groupedThreads("onos/ecm", mapName + "-fg-%d")); |
| 185 | } |
| 186 | |
| 187 | if (communicationExecutor != null) { |
| 188 | this.communicationExecutor = communicationExecutor; |
| 189 | } else { |
| 190 | // sending executor; should be capped |
| 191 | //TODO this probably doesn't need to be bounded anymore |
| 192 | this.communicationExecutor = |
| 193 | newFixedThreadPool(8, groupedThreads("onos/ecm", mapName + "-publish-%d")); |
| 194 | } |
| 195 | |
| 196 | if (backgroundExecutor != null) { |
| 197 | this.backgroundExecutor = backgroundExecutor; |
| 198 | } else { |
| 199 | this.backgroundExecutor = |
| 200 | newSingleThreadScheduledExecutor(groupedThreads("onos/ecm", mapName + "-bg-%d")); |
| 201 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 202 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 203 | // start anti-entropy thread |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 204 | this.backgroundExecutor.scheduleAtFixedRate(new SendAdvertisementTask(), |
| 205 | initialDelaySec, antiEntropyPeriod, |
| 206 | antiEntropyTimeUnit); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 207 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 208 | updateMessageSubject = new MessageSubject("ecm-" + mapName + "-update"); |
| 209 | clusterCommunicator.addSubscriber(updateMessageSubject, |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 210 | new InternalEventListener(), this.executor); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 211 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 212 | antiEntropyAdvertisementSubject = new MessageSubject("ecm-" + mapName + "-anti-entropy"); |
| 213 | clusterCommunicator.addSubscriber(antiEntropyAdvertisementSubject, |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 214 | new InternalAntiEntropyListener(), this.backgroundExecutor); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 215 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 216 | this.tombstonesDisabled = tombstonesDisabled; |
| 217 | this.lightweightAntiEntropy = !convergeFaster; |
Madan Jampani | e135628 | 2015-03-10 19:05:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 220 | private KryoSerializer createSerializer(KryoNamespace.Builder builder) { |
| 221 | return new KryoSerializer() { |
| 222 | @Override |
| 223 | protected void setupKryoPool() { |
| 224 | // Add the map's internal helper classes to the user-supplied serializer |
| 225 | serializerPool = builder |
| 226 | .register(WallClockTimestamp.class) |
| 227 | .register(PutEntry.class) |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 228 | .register(RemoveEntry.class) |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 229 | .register(ArrayList.class) |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 230 | .register(AntiEntropyAdvertisement.class) |
| 231 | .register(HashMap.class) |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 232 | .build(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 233 | } |
| 234 | }; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public int size() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 239 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 240 | return items.size(); |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public boolean isEmpty() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 245 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 246 | return items.isEmpty(); |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public boolean containsKey(K key) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 251 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 252 | checkNotNull(key, ERROR_NULL_KEY); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 253 | return items.containsKey(key); |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public boolean containsValue(V value) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 258 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 259 | checkNotNull(value, ERROR_NULL_VALUE); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 260 | |
| 261 | return items.values().stream() |
| 262 | .anyMatch(timestamped -> timestamped.value().equals(value)); |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public V get(K key) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 267 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 268 | checkNotNull(key, ERROR_NULL_KEY); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 269 | |
| 270 | Timestamped<V> value = items.get(key); |
| 271 | if (value != null) { |
| 272 | return value.value(); |
| 273 | } |
| 274 | return null; |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public void put(K key, V value) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 279 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 280 | checkNotNull(key, ERROR_NULL_KEY); |
| 281 | checkNotNull(value, ERROR_NULL_VALUE); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 282 | |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 283 | Timestamp timestamp = clockService.getTimestamp(key, value); |
| 284 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 285 | if (putInternal(key, value, timestamp)) { |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 286 | notifyPeers(new PutEntry<>(key, value, timestamp), |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 287 | peerUpdateFunction.apply(key, value)); |
| 288 | notifyListeners(new EventuallyConsistentMapEvent<>( |
| 289 | EventuallyConsistentMapEvent.Type.PUT, key, value)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | private boolean putInternal(K key, V value, Timestamp timestamp) { |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 294 | counter.incrementCount(); |
Brian O'Connor | 2952e3b | 2015-02-19 21:47:57 -0800 | [diff] [blame] | 295 | Timestamp removed = removedItems.get(key); |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame] | 296 | if (removed != null && removed.isNewerThan(timestamp)) { |
Brian O'Connor | 2952e3b | 2015-02-19 21:47:57 -0800 | [diff] [blame] | 297 | log.debug("ecmap - removed was newer {}", value); |
| 298 | return false; |
| 299 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 300 | |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 301 | final MutableBoolean updated = new MutableBoolean(false); |
| 302 | |
| 303 | items.compute(key, (k, existing) -> { |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame] | 304 | if (existing != null && existing.isNewerThan(timestamp)) { |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 305 | updated.setFalse(); |
| 306 | return existing; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 307 | } else { |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 308 | updated.setTrue(); |
| 309 | return new Timestamped<>(value, timestamp); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 310 | } |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 311 | }); |
| 312 | |
| 313 | boolean success = updated.booleanValue(); |
| 314 | if (!success) { |
| 315 | log.debug("ecmap - existing was newer {}", value); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 316 | } |
Brian O'Connor | 2952e3b | 2015-02-19 21:47:57 -0800 | [diff] [blame] | 317 | |
| 318 | if (success && removed != null) { |
| 319 | removedItems.remove(key, removed); |
| 320 | } |
| 321 | return success; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public void remove(K key) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 326 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 327 | checkNotNull(key, ERROR_NULL_KEY); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 328 | |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 329 | // TODO prevent calls here if value is important for timestamp |
| 330 | Timestamp timestamp = clockService.getTimestamp(key, null); |
| 331 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 332 | if (removeInternal(key, timestamp)) { |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 333 | notifyPeers(new RemoveEntry<>(key, timestamp), |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 334 | peerUpdateFunction.apply(key, null)); |
| 335 | notifyListeners(new EventuallyConsistentMapEvent<>( |
| 336 | EventuallyConsistentMapEvent.Type.REMOVE, key, null)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | private boolean removeInternal(K key, Timestamp timestamp) { |
Madan Jampani | 54d3499 | 2015-03-06 17:27:52 -0800 | [diff] [blame] | 341 | if (timestamp == null) { |
| 342 | return false; |
| 343 | } |
| 344 | |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 345 | counter.incrementCount(); |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 346 | final MutableBoolean updated = new MutableBoolean(false); |
| 347 | |
| 348 | items.compute(key, (k, existing) -> { |
| 349 | if (existing != null && existing.isNewerThan(timestamp)) { |
| 350 | updated.setFalse(); |
| 351 | return existing; |
Madan Jampani | 7e6cfe3 | 2015-02-19 17:44:44 -0800 | [diff] [blame] | 352 | } else { |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 353 | updated.setTrue(); |
| 354 | // remove from items map |
| 355 | return null; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 356 | } |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 357 | }); |
| 358 | |
| 359 | if (updated.isFalse()) { |
| 360 | return false; |
Madan Jampani | 7e6cfe3 | 2015-02-19 17:44:44 -0800 | [diff] [blame] | 361 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 362 | |
Madan Jampani | e135628 | 2015-03-10 19:05:36 -0700 | [diff] [blame] | 363 | if (!tombstonesDisabled) { |
| 364 | Timestamp removedTimestamp = removedItems.get(key); |
| 365 | if (removedTimestamp == null) { |
| 366 | return removedItems.putIfAbsent(key, timestamp) == null; |
| 367 | } else if (timestamp.isNewerThan(removedTimestamp)) { |
| 368 | return removedItems.replace(key, removedTimestamp, timestamp); |
| 369 | } else { |
| 370 | return false; |
| 371 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 372 | } |
Madan Jampani | e135628 | 2015-03-10 19:05:36 -0700 | [diff] [blame] | 373 | |
| 374 | return updated.booleanValue(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | @Override |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 378 | public void remove(K key, V value) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 379 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 380 | checkNotNull(key, ERROR_NULL_KEY); |
| 381 | checkNotNull(value, ERROR_NULL_VALUE); |
| 382 | |
| 383 | Timestamp timestamp = clockService.getTimestamp(key, value); |
| 384 | |
| 385 | if (removeInternal(key, timestamp)) { |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 386 | notifyPeers(new RemoveEntry<>(key, timestamp), |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 387 | peerUpdateFunction.apply(key, value)); |
| 388 | notifyListeners(new EventuallyConsistentMapEvent<>( |
| 389 | EventuallyConsistentMapEvent.Type.REMOVE, key, value)); |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | @Override |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 394 | public void putAll(Map<? extends K, ? extends V> m) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 395 | checkState(!destroyed, destroyedMessage); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 396 | m.forEach(this::put); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | @Override |
| 400 | public void clear() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 401 | checkState(!destroyed, destroyedMessage); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 402 | items.forEach((key, value) -> remove(key)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | @Override |
| 406 | public Set<K> keySet() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 407 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 408 | return items.keySet(); |
| 409 | } |
| 410 | |
| 411 | @Override |
| 412 | public Collection<V> values() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 413 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 414 | return items.values().stream() |
| 415 | .map(Timestamped::value) |
| 416 | .collect(Collectors.toList()); |
| 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public Set<Map.Entry<K, V>> entrySet() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 421 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 422 | |
| 423 | return items.entrySet().stream() |
Jonathan Hart | f910823 | 2015-02-02 16:37:35 -0800 | [diff] [blame] | 424 | .map(e -> Pair.of(e.getKey(), e.getValue().value())) |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 425 | .collect(Collectors.toSet()); |
| 426 | } |
| 427 | |
| 428 | @Override |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 429 | public void addListener(EventuallyConsistentMapListener<K, V> listener) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 430 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 431 | |
| 432 | listeners.add(checkNotNull(listener)); |
| 433 | } |
| 434 | |
| 435 | @Override |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 436 | public void removeListener(EventuallyConsistentMapListener<K, V> listener) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 437 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 438 | |
| 439 | listeners.remove(checkNotNull(listener)); |
| 440 | } |
| 441 | |
| 442 | @Override |
| 443 | public void destroy() { |
| 444 | destroyed = true; |
| 445 | |
| 446 | executor.shutdown(); |
| 447 | backgroundExecutor.shutdown(); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 448 | communicationExecutor.shutdown(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 449 | |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 450 | listeners.clear(); |
| 451 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 452 | clusterCommunicator.removeSubscriber(updateMessageSubject); |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 453 | clusterCommunicator.removeSubscriber(antiEntropyAdvertisementSubject); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 456 | private void notifyListeners(EventuallyConsistentMapEvent<K, V> event) { |
| 457 | for (EventuallyConsistentMapListener<K, V> listener : listeners) { |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 458 | listener.event(event); |
| 459 | } |
| 460 | } |
| 461 | |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 462 | private void notifyPeers(PutEntry<K, V> event, Collection<NodeId> peers) { |
| 463 | queueUpdate(event, peers); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 466 | private void notifyPeers(RemoveEntry<K, V> event, Collection<NodeId> peers) { |
| 467 | queueUpdate(event, peers); |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 468 | } |
| 469 | |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 470 | private void queueUpdate(AbstractEntry<K, V> event, Collection<NodeId> peers) { |
| 471 | if (peers == null) { |
| 472 | // we have no friends :( |
| 473 | return; |
| 474 | } |
| 475 | peers.forEach(node -> |
| 476 | senderPending.computeIfAbsent(node, unusedKey -> new EventAccumulator(node)).add(event) |
| 477 | ); |
| 478 | } |
| 479 | |
| 480 | private boolean unicastMessage(NodeId peer, MessageSubject subject, Object event) { |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 481 | ClusterMessage message = new ClusterMessage( |
| 482 | clusterService.getLocalNode().id(), |
| 483 | subject, |
| 484 | serializer.encode(event)); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 485 | return clusterCommunicator.unicast(message, peer); |
| 486 | // Note: we had this flipped before... |
| 487 | // communicationExecutor.execute(() -> clusterCommunicator.unicast(message, peer)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 490 | private boolean underHighLoad() { |
| 491 | return counter.get(LOAD_WINDOW) > HIGH_LOAD_THRESHOLD; |
| 492 | } |
| 493 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 494 | private final class SendAdvertisementTask implements Runnable { |
| 495 | @Override |
| 496 | public void run() { |
| 497 | if (Thread.currentThread().isInterrupted()) { |
| 498 | log.info("Interrupted, quitting"); |
| 499 | return; |
| 500 | } |
| 501 | |
Thomas Vachuska | 152f9fd | 2015-04-02 16:28:13 -0700 | [diff] [blame] | 502 | if (underHighLoad() || destroyed) { |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 503 | return; |
| 504 | } |
| 505 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 506 | try { |
| 507 | final NodeId self = clusterService.getLocalNode().id(); |
| 508 | Set<ControllerNode> nodes = clusterService.getNodes(); |
| 509 | |
| 510 | List<NodeId> nodeIds = nodes.stream() |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 511 | .map(ControllerNode::id) |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 512 | .collect(Collectors.toList()); |
| 513 | |
| 514 | if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) { |
| 515 | log.trace("No other peers in the cluster."); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | NodeId peer; |
| 520 | do { |
| 521 | int idx = RandomUtils.nextInt(0, nodeIds.size()); |
| 522 | peer = nodeIds.get(idx); |
| 523 | } while (peer.equals(self)); |
| 524 | |
| 525 | if (Thread.currentThread().isInterrupted()) { |
| 526 | log.info("Interrupted, quitting"); |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | AntiEntropyAdvertisement<K> ad = createAdvertisement(); |
| 531 | |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 532 | if (!unicastMessage(peer, antiEntropyAdvertisementSubject, ad)) { |
| 533 | log.debug("Failed to send anti-entropy advertisement to {}", peer); |
| 534 | } |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 535 | } catch (Exception e) { |
| 536 | // Catch all exceptions to avoid scheduled task being suppressed. |
| 537 | log.error("Exception thrown while sending advertisement", e); |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | private AntiEntropyAdvertisement<K> createAdvertisement() { |
| 543 | final NodeId self = clusterService.getLocalNode().id(); |
| 544 | |
| 545 | Map<K, Timestamp> timestamps = new HashMap<>(items.size()); |
| 546 | |
| 547 | items.forEach((key, value) -> timestamps.put(key, value.timestamp())); |
| 548 | |
| 549 | Map<K, Timestamp> tombstones = new HashMap<>(removedItems); |
| 550 | |
| 551 | return new AntiEntropyAdvertisement<>(self, timestamps, tombstones); |
| 552 | } |
| 553 | |
| 554 | private void handleAntiEntropyAdvertisement(AntiEntropyAdvertisement<K> ad) { |
| 555 | List<EventuallyConsistentMapEvent<K, V>> externalEvents; |
| 556 | |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 557 | externalEvents = antiEntropyCheckLocalItems(ad); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 558 | |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 559 | antiEntropyCheckLocalRemoved(ad); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 560 | |
Jonathan Hart | f893be8 | 2015-02-24 17:35:51 -0800 | [diff] [blame] | 561 | if (!lightweightAntiEntropy) { |
| 562 | externalEvents.addAll(antiEntropyCheckRemoteRemoved(ad)); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 563 | |
Jonathan Hart | f893be8 | 2015-02-24 17:35:51 -0800 | [diff] [blame] | 564 | // if remote ad has something unknown, actively sync |
| 565 | for (K key : ad.timestamps().keySet()) { |
| 566 | if (!items.containsKey(key)) { |
| 567 | // Send the advertisement back if this peer is out-of-sync |
| 568 | final NodeId sender = ad.sender(); |
| 569 | AntiEntropyAdvertisement<K> myAd = createAdvertisement(); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 570 | if (!unicastMessage(sender, antiEntropyAdvertisementSubject, myAd)) { |
| 571 | log.debug("Failed to send reactive anti-entropy advertisement to {}", sender); |
| 572 | } |
Jonathan Hart | f893be8 | 2015-02-24 17:35:51 -0800 | [diff] [blame] | 573 | break; |
| 574 | } |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 575 | } |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 576 | } |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 577 | externalEvents.forEach(this::notifyListeners); |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Checks if any of the remote's live items or tombstones are out of date |
| 582 | * according to our local live item list, or if our live items are out of |
| 583 | * date according to the remote's tombstone list. |
| 584 | * If the local copy is more recent, it will be pushed to the remote. If the |
| 585 | * remote has a more recent remove, we apply that to the local state. |
| 586 | * |
| 587 | * @param ad remote anti-entropy advertisement |
| 588 | * @return list of external events relating to local operations performed |
| 589 | */ |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 590 | private List<EventuallyConsistentMapEvent<K, V>> antiEntropyCheckLocalItems( |
| 591 | AntiEntropyAdvertisement<K> ad) { |
| 592 | final List<EventuallyConsistentMapEvent<K, V>> externalEvents |
| 593 | = new LinkedList<>(); |
| 594 | final NodeId sender = ad.sender(); |
| 595 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 596 | for (Map.Entry<K, Timestamped<V>> item : items.entrySet()) { |
| 597 | K key = item.getKey(); |
| 598 | Timestamped<V> localValue = item.getValue(); |
| 599 | |
| 600 | Timestamp remoteTimestamp = ad.timestamps().get(key); |
| 601 | if (remoteTimestamp == null) { |
| 602 | remoteTimestamp = ad.tombstones().get(key); |
| 603 | } |
| 604 | if (remoteTimestamp == null || localValue |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame] | 605 | .isNewerThan(remoteTimestamp)) { |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 606 | // local value is more recent, push to sender |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 607 | queueUpdate(new PutEntry<>(key, localValue.value(), |
| 608 | localValue.timestamp()), ImmutableList.of(sender)); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | Timestamp remoteDeadTimestamp = ad.tombstones().get(key); |
| 612 | if (remoteDeadTimestamp != null && |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame] | 613 | remoteDeadTimestamp.isNewerThan(localValue.timestamp())) { |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 614 | // sender has a more recent remove |
| 615 | if (removeInternal(key, remoteDeadTimestamp)) { |
| 616 | externalEvents.add(new EventuallyConsistentMapEvent<>( |
| 617 | EventuallyConsistentMapEvent.Type.REMOVE, key, null)); |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 622 | return externalEvents; |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Checks if any items in the remote live list are out of date according |
| 627 | * to our tombstone list. If we find we have a more up to date tombstone, |
| 628 | * we'll send it to the remote. |
| 629 | * |
| 630 | * @param ad remote anti-entropy advertisement |
| 631 | */ |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 632 | private void antiEntropyCheckLocalRemoved(AntiEntropyAdvertisement<K> ad) { |
| 633 | final NodeId sender = ad.sender(); |
| 634 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 635 | for (Map.Entry<K, Timestamp> dead : removedItems.entrySet()) { |
| 636 | K key = dead.getKey(); |
| 637 | Timestamp localDeadTimestamp = dead.getValue(); |
| 638 | |
| 639 | Timestamp remoteLiveTimestamp = ad.timestamps().get(key); |
| 640 | if (remoteLiveTimestamp != null |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame] | 641 | && localDeadTimestamp.isNewerThan(remoteLiveTimestamp)) { |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 642 | // sender has zombie, push remove |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 643 | queueUpdate(new RemoveEntry<>(key, localDeadTimestamp), ImmutableList.of(sender)); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 644 | } |
| 645 | } |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /** |
| 649 | * Checks if any of the local live items are out of date according to the |
| 650 | * remote's tombstone advertisements. If we find a local item is out of date, |
| 651 | * we'll apply the remove operation to the local state. |
| 652 | * |
| 653 | * @param ad remote anti-entropy advertisement |
| 654 | * @return list of external events relating to local operations performed |
| 655 | */ |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 656 | private List<EventuallyConsistentMapEvent<K, V>> |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 657 | antiEntropyCheckRemoteRemoved(AntiEntropyAdvertisement<K> ad) { |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 658 | final List<EventuallyConsistentMapEvent<K, V>> externalEvents |
| 659 | = new LinkedList<>(); |
| 660 | |
| 661 | for (Map.Entry<K, Timestamp> remoteDead : ad.tombstones().entrySet()) { |
| 662 | K key = remoteDead.getKey(); |
| 663 | Timestamp remoteDeadTimestamp = remoteDead.getValue(); |
| 664 | |
| 665 | Timestamped<V> local = items.get(key); |
| 666 | Timestamp localDead = removedItems.get(key); |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame] | 667 | if (local != null && remoteDeadTimestamp.isNewerThan( |
| 668 | local.timestamp())) { |
| 669 | // If the remote has a more recent tombstone than either our local |
| 670 | // value, then do a remove with their timestamp |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 671 | if (removeInternal(key, remoteDeadTimestamp)) { |
| 672 | externalEvents.add(new EventuallyConsistentMapEvent<>( |
| 673 | EventuallyConsistentMapEvent.Type.REMOVE, key, null)); |
| 674 | } |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame] | 675 | } else if (localDead != null && remoteDeadTimestamp.isNewerThan( |
| 676 | localDead)) { |
| 677 | // If the remote has a more recent tombstone than us, update ours |
| 678 | // to their timestamp |
| 679 | removeInternal(key, remoteDeadTimestamp); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
| 683 | return externalEvents; |
| 684 | } |
| 685 | |
| 686 | private final class InternalAntiEntropyListener |
| 687 | implements ClusterMessageHandler { |
| 688 | |
| 689 | @Override |
| 690 | public void handle(ClusterMessage message) { |
Jonathan Hart | 4fd4ebb | 2015-02-04 17:38:48 -0800 | [diff] [blame] | 691 | log.trace("Received anti-entropy advertisement from peer: {}", |
| 692 | message.sender()); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 693 | AntiEntropyAdvertisement<K> advertisement = serializer.decode(message.payload()); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 694 | try { |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 695 | if (!underHighLoad()) { |
| 696 | handleAntiEntropyAdvertisement(advertisement); |
| 697 | } |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 698 | } catch (Exception e) { |
| 699 | log.warn("Exception thrown handling advertisements", e); |
| 700 | } |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
Thomas Vachuska | 152f9fd | 2015-04-02 16:28:13 -0700 | [diff] [blame] | 704 | private final class InternalEventListener implements ClusterMessageHandler { |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 705 | @Override |
| 706 | public void handle(ClusterMessage message) { |
Thomas Vachuska | 152f9fd | 2015-04-02 16:28:13 -0700 | [diff] [blame] | 707 | if (destroyed) { |
| 708 | return; |
| 709 | } |
| 710 | |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 711 | log.debug("Received update event from peer: {}", message.sender()); |
| 712 | Collection<AbstractEntry<K, V>> events = serializer.decode(message.payload()); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 713 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 714 | try { |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 715 | // TODO clean this for loop up |
| 716 | for (AbstractEntry<K, V> entry : events) { |
| 717 | final K key = entry.key(); |
| 718 | final V value; |
| 719 | final Timestamp timestamp = entry.timestamp(); |
| 720 | final EventuallyConsistentMapEvent.Type type; |
| 721 | if (entry instanceof PutEntry) { |
| 722 | PutEntry<K, V> putEntry = (PutEntry<K, V>) entry; |
| 723 | value = putEntry.value(); |
| 724 | type = EventuallyConsistentMapEvent.Type.PUT; |
| 725 | } else if (entry instanceof RemoveEntry) { |
| 726 | type = EventuallyConsistentMapEvent.Type.REMOVE; |
| 727 | value = null; |
| 728 | } else { |
| 729 | throw new IllegalStateException("Unknown entry type " + entry.getClass()); |
| 730 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 731 | |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 732 | boolean success; |
| 733 | switch (type) { |
| 734 | case PUT: |
| 735 | success = putInternal(key, value, timestamp); |
| 736 | break; |
| 737 | case REMOVE: |
| 738 | success = removeInternal(key, timestamp); |
| 739 | break; |
| 740 | default: |
| 741 | success = false; |
| 742 | } |
| 743 | if (success) { |
| 744 | notifyListeners(new EventuallyConsistentMapEvent<>(type, key, value)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 745 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 746 | } |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 747 | } catch (Exception e) { |
| 748 | log.warn("Exception thrown handling put", e); |
| 749 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 753 | // TODO pull this into the class if this gets pulled out... |
| 754 | private static final int DEFAULT_MAX_EVENTS = 1000; |
| 755 | private static final int DEFAULT_MAX_IDLE_MS = 10; |
| 756 | private static final int DEFAULT_MAX_BATCH_MS = 50; |
| 757 | private static final Timer TIMER = new Timer("onos-ecm-sender-events"); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 758 | |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 759 | private final class EventAccumulator extends AbstractAccumulator<AbstractEntry<K, V>> { |
| 760 | |
| 761 | private final NodeId peer; |
| 762 | |
| 763 | private EventAccumulator(NodeId peer) { |
| 764 | super(TIMER, DEFAULT_MAX_EVENTS, DEFAULT_MAX_BATCH_MS, DEFAULT_MAX_IDLE_MS); |
| 765 | this.peer = peer; |
| 766 | } |
| 767 | |
| 768 | @Override |
| 769 | public void processItems(List<AbstractEntry<K, V>> items) { |
| 770 | Map<K, AbstractEntry<K, V>> map = Maps.newHashMap(); |
| 771 | items.forEach(item -> map.compute(item.key(), (key, oldValue) -> |
| 772 | oldValue == null || item.compareTo(oldValue) > 0 ? item : oldValue |
| 773 | ) |
| 774 | ); |
| 775 | communicationExecutor.submit(() -> { |
| 776 | try { |
| 777 | unicastMessage(peer, updateMessageSubject, Lists.newArrayList(map.values())); |
| 778 | } catch (Exception e) { |
| 779 | log.warn("broadcast error", e); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 780 | } |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 781 | }); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 782 | } |
| 783 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 784 | } |