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