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 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 18 | import org.apache.commons.lang3.RandomUtils; |
Jonathan Hart | f910823 | 2015-02-02 16:37:35 -0800 | [diff] [blame] | 19 | import org.apache.commons.lang3.tuple.Pair; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 20 | import org.onlab.util.KryoNamespace; |
| 21 | import org.onosproject.cluster.ClusterService; |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 22 | import org.onosproject.cluster.ControllerNode; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 23 | import org.onosproject.cluster.NodeId; |
| 24 | import org.onosproject.store.Timestamp; |
| 25 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 26 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 27 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 28 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Jonathan Hart | 77bdd26 | 2015-02-03 09:07:48 -0800 | [diff] [blame] | 29 | import org.onosproject.store.impl.ClockService; |
| 30 | import org.onosproject.store.impl.Timestamped; |
| 31 | import org.onosproject.store.impl.WallClockTimestamp; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 32 | import org.onosproject.store.serializers.KryoSerializer; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import java.io.IOException; |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.Collection; |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 39 | import java.util.HashMap; |
| 40 | import java.util.LinkedList; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 41 | import java.util.List; |
| 42 | import java.util.Map; |
| 43 | import java.util.Set; |
| 44 | import java.util.concurrent.ConcurrentHashMap; |
| 45 | import java.util.concurrent.CopyOnWriteArraySet; |
| 46 | import java.util.concurrent.ExecutorService; |
| 47 | import java.util.concurrent.Executors; |
| 48 | import java.util.concurrent.ScheduledExecutorService; |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 49 | import java.util.concurrent.TimeUnit; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 50 | import java.util.stream.Collectors; |
| 51 | |
| 52 | import static com.google.common.base.Preconditions.checkNotNull; |
| 53 | import static com.google.common.base.Preconditions.checkState; |
| 54 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Brian O'Connor | 4b2ba5f | 2015-02-18 20:54:00 -0800 | [diff] [blame] | 55 | import static org.onlab.util.Tools.groupedThreads; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 56 | import static org.onlab.util.Tools.minPriority; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Distributed Map implementation which uses optimistic replication and gossip |
| 60 | * based techniques to provide an eventually consistent data store. |
| 61 | */ |
| 62 | public class EventuallyConsistentMapImpl<K, V> |
| 63 | implements EventuallyConsistentMap<K, V> { |
| 64 | |
| 65 | private static final Logger log = LoggerFactory.getLogger(EventuallyConsistentMapImpl.class); |
| 66 | |
| 67 | private final Map<K, Timestamped<V>> items; |
| 68 | private final Map<K, Timestamp> removedItems; |
| 69 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 70 | private final ClusterService clusterService; |
| 71 | private final ClusterCommunicationService clusterCommunicator; |
| 72 | private final KryoSerializer serializer; |
| 73 | |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 74 | private final ClockService<K, V> clockService; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 75 | |
| 76 | private final MessageSubject updateMessageSubject; |
| 77 | private final MessageSubject removeMessageSubject; |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 78 | private final MessageSubject antiEntropyAdvertisementSubject; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 79 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 80 | private final Set<EventuallyConsistentMapListener<K, V>> listeners |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 81 | = new CopyOnWriteArraySet<>(); |
| 82 | |
| 83 | private final ExecutorService executor; |
| 84 | |
| 85 | private final ScheduledExecutorService backgroundExecutor; |
| 86 | |
Madan Jampani | b28e4ad | 2015-02-19 12:31:37 -0800 | [diff] [blame] | 87 | private ExecutorService broadcastMessageExecutor; |
Madan Jampani | 2872628 | 2015-02-19 11:40:23 -0800 | [diff] [blame] | 88 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 89 | private volatile boolean destroyed = false; |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 90 | private static final String ERROR_DESTROYED = " map is already destroyed"; |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 91 | private final String destroyedMessage; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 92 | |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 93 | private static final String ERROR_NULL_KEY = "Key cannot be null"; |
| 94 | private static final String ERROR_NULL_VALUE = "Null values are not allowed"; |
| 95 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 96 | // TODO: Make these anti-entropy params configurable |
| 97 | private long initialDelaySec = 5; |
| 98 | private long periodSec = 5; |
| 99 | |
| 100 | /** |
| 101 | * Creates a new eventually consistent map shared amongst multiple instances. |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 102 | * <p> |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 103 | * Each map is identified by a string map name. EventuallyConsistentMapImpl |
| 104 | * objects in different JVMs that use the same map name will form a |
| 105 | * distributed map across JVMs (provided the cluster service is aware of |
| 106 | * both nodes). |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 107 | * </p> |
| 108 | * <p> |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 109 | * The client is expected to provide an |
| 110 | * {@link org.onlab.util.KryoNamespace.Builder} with which all classes that |
| 111 | * will be stored in this map have been registered (including referenced |
| 112 | * classes). This serializer will be used to serialize both K and V for |
| 113 | * inter-node notifications. |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 114 | * </p> |
| 115 | * <p> |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 116 | * The client must provide an {@link org.onosproject.store.impl.ClockService} |
| 117 | * which can generate timestamps for a given key. The clock service is free |
| 118 | * to generate timestamps however it wishes, however these timestamps will |
| 119 | * be used to serialize updates to the map so they must be strict enough |
| 120 | * to ensure updates are properly ordered for the use case (i.e. in some |
| 121 | * cases wallclock time will suffice, whereas in other cases logical time |
| 122 | * will be necessary). |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 123 | * </p> |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 124 | * |
| 125 | * @param mapName a String identifier for the map. |
| 126 | * @param clusterService the cluster service |
| 127 | * @param clusterCommunicator the cluster communications service |
| 128 | * @param serializerBuilder a Kryo namespace builder that can serialize |
| 129 | * both K and V |
| 130 | * @param clockService a clock service able to generate timestamps |
| 131 | * for K |
| 132 | */ |
| 133 | public EventuallyConsistentMapImpl(String mapName, |
| 134 | ClusterService clusterService, |
| 135 | ClusterCommunicationService clusterCommunicator, |
| 136 | KryoNamespace.Builder serializerBuilder, |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 137 | ClockService<K, V> clockService) { |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 138 | this.clusterService = checkNotNull(clusterService); |
| 139 | this.clusterCommunicator = checkNotNull(clusterCommunicator); |
| 140 | |
| 141 | serializer = createSerializer(checkNotNull(serializerBuilder)); |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 142 | destroyedMessage = mapName + ERROR_DESTROYED; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 143 | |
| 144 | this.clockService = checkNotNull(clockService); |
| 145 | |
| 146 | items = new ConcurrentHashMap<>(); |
| 147 | removedItems = new ConcurrentHashMap<>(); |
| 148 | |
Brian O'Connor | 4b2ba5f | 2015-02-18 20:54:00 -0800 | [diff] [blame] | 149 | executor = Executors //FIXME |
| 150 | .newFixedThreadPool(4, groupedThreads("onos/ecm", mapName + "-fg-%d")); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 151 | |
Madan Jampani | 2872628 | 2015-02-19 11:40:23 -0800 | [diff] [blame] | 152 | broadcastMessageExecutor = Executors.newSingleThreadExecutor(groupedThreads("onos/ecm", mapName + "-notify")); |
| 153 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 154 | backgroundExecutor = |
| 155 | newSingleThreadScheduledExecutor(minPriority( |
Brian O'Connor | 4b2ba5f | 2015-02-18 20:54:00 -0800 | [diff] [blame] | 156 | groupedThreads("onos/ecm", mapName + "-bg-%d"))); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 157 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 158 | // start anti-entropy thread |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 159 | backgroundExecutor.scheduleAtFixedRate(new SendAdvertisementTask(), |
| 160 | initialDelaySec, periodSec, |
| 161 | TimeUnit.SECONDS); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 162 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 163 | updateMessageSubject = new MessageSubject("ecm-" + mapName + "-update"); |
| 164 | clusterCommunicator.addSubscriber(updateMessageSubject, |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 165 | new InternalPutEventListener(), executor); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 166 | removeMessageSubject = new MessageSubject("ecm-" + mapName + "-remove"); |
| 167 | clusterCommunicator.addSubscriber(removeMessageSubject, |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 168 | new InternalRemoveEventListener(), executor); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 169 | antiEntropyAdvertisementSubject = new MessageSubject("ecm-" + mapName + "-anti-entropy"); |
| 170 | clusterCommunicator.addSubscriber(antiEntropyAdvertisementSubject, |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 171 | new InternalAntiEntropyListener(), backgroundExecutor); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | private KryoSerializer createSerializer(KryoNamespace.Builder builder) { |
| 175 | return new KryoSerializer() { |
| 176 | @Override |
| 177 | protected void setupKryoPool() { |
| 178 | // Add the map's internal helper classes to the user-supplied serializer |
| 179 | serializerPool = builder |
| 180 | .register(WallClockTimestamp.class) |
| 181 | .register(PutEntry.class) |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 182 | .register(RemoveEntry.class) |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 183 | .register(ArrayList.class) |
| 184 | .register(InternalPutEvent.class) |
| 185 | .register(InternalRemoveEvent.class) |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 186 | .register(AntiEntropyAdvertisement.class) |
| 187 | .register(HashMap.class) |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 188 | .build(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 189 | } |
| 190 | }; |
| 191 | } |
| 192 | |
Madan Jampani | b28e4ad | 2015-02-19 12:31:37 -0800 | [diff] [blame] | 193 | /** |
| 194 | * Sets the executor to use for broadcasting messages and returns this |
| 195 | * instance for method chaining. |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 196 | * |
Madan Jampani | b28e4ad | 2015-02-19 12:31:37 -0800 | [diff] [blame] | 197 | * @param executor executor service |
| 198 | * @return this instance |
| 199 | */ |
| 200 | public EventuallyConsistentMapImpl<K, V> withBroadcastMessageExecutor(ExecutorService executor) { |
| 201 | checkNotNull(executor, "Null executor"); |
| 202 | broadcastMessageExecutor = executor; |
| 203 | return this; |
| 204 | } |
| 205 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 206 | @Override |
| 207 | public int size() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 208 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 209 | return items.size(); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public boolean isEmpty() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 214 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 215 | return items.isEmpty(); |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public boolean containsKey(K key) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 220 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 221 | checkNotNull(key, ERROR_NULL_KEY); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 222 | return items.containsKey(key); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public boolean containsValue(V value) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 227 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 228 | checkNotNull(value, ERROR_NULL_VALUE); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 229 | |
| 230 | return items.values().stream() |
| 231 | .anyMatch(timestamped -> timestamped.value().equals(value)); |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public V get(K key) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 236 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 237 | checkNotNull(key, ERROR_NULL_KEY); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 238 | |
| 239 | Timestamped<V> value = items.get(key); |
| 240 | if (value != null) { |
| 241 | return value.value(); |
| 242 | } |
| 243 | return null; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public void put(K key, V value) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 248 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 249 | checkNotNull(key, ERROR_NULL_KEY); |
| 250 | checkNotNull(value, ERROR_NULL_VALUE); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 251 | |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 252 | Timestamp timestamp = clockService.getTimestamp(key, value); |
| 253 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 254 | if (putInternal(key, value, timestamp)) { |
| 255 | notifyPeers(new InternalPutEvent<>(key, value, timestamp)); |
| 256 | EventuallyConsistentMapEvent<K, V> externalEvent |
| 257 | = new EventuallyConsistentMapEvent<>( |
| 258 | EventuallyConsistentMapEvent.Type.PUT, key, value); |
| 259 | notifyListeners(externalEvent); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | private boolean putInternal(K key, V value, Timestamp timestamp) { |
Brian O'Connor | 2952e3b | 2015-02-19 21:47:57 -0800 | [diff] [blame] | 264 | Timestamp removed = removedItems.get(key); |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame^] | 265 | if (removed != null && removed.isNewerThan(timestamp)) { |
Brian O'Connor | 2952e3b | 2015-02-19 21:47:57 -0800 | [diff] [blame] | 266 | log.debug("ecmap - removed was newer {}", value); |
| 267 | return false; |
| 268 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 269 | |
Brian O'Connor | 2952e3b | 2015-02-19 21:47:57 -0800 | [diff] [blame] | 270 | boolean success; |
| 271 | synchronized (this) { |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 272 | Timestamped<V> existing = items.get(key); |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame^] | 273 | if (existing != null && existing.isNewerThan(timestamp)) { |
Jonathan Hart | 07e58be | 2015-02-12 09:57:16 -0800 | [diff] [blame] | 274 | log.debug("ecmap - existing was newer {}", value); |
Brian O'Connor | 2952e3b | 2015-02-19 21:47:57 -0800 | [diff] [blame] | 275 | success = false; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 276 | } else { |
| 277 | items.put(key, new Timestamped<>(value, timestamp)); |
Brian O'Connor | 2952e3b | 2015-02-19 21:47:57 -0800 | [diff] [blame] | 278 | success = true; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 279 | } |
| 280 | } |
Brian O'Connor | 2952e3b | 2015-02-19 21:47:57 -0800 | [diff] [blame] | 281 | |
| 282 | if (success && removed != null) { |
| 283 | removedItems.remove(key, removed); |
| 284 | } |
| 285 | return success; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public void remove(K key) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 290 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 291 | checkNotNull(key, ERROR_NULL_KEY); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 292 | |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 293 | // TODO prevent calls here if value is important for timestamp |
| 294 | Timestamp timestamp = clockService.getTimestamp(key, null); |
| 295 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 296 | if (removeInternal(key, timestamp)) { |
| 297 | notifyPeers(new InternalRemoveEvent<>(key, timestamp)); |
| 298 | EventuallyConsistentMapEvent<K, V> externalEvent |
| 299 | = new EventuallyConsistentMapEvent<>( |
| 300 | EventuallyConsistentMapEvent.Type.REMOVE, key, null); |
| 301 | notifyListeners(externalEvent); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | private boolean removeInternal(K key, Timestamp timestamp) { |
Madan Jampani | 7e6cfe3 | 2015-02-19 17:44:44 -0800 | [diff] [blame] | 306 | Timestamped<V> value = items.get(key); |
| 307 | if (value != null) { |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame^] | 308 | if (value.isNewerThan(timestamp)) { |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 309 | return false; |
Madan Jampani | 7e6cfe3 | 2015-02-19 17:44:44 -0800 | [diff] [blame] | 310 | } else { |
| 311 | items.remove(key, value); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 312 | } |
Madan Jampani | 7e6cfe3 | 2015-02-19 17:44:44 -0800 | [diff] [blame] | 313 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 314 | |
Madan Jampani | 7e6cfe3 | 2015-02-19 17:44:44 -0800 | [diff] [blame] | 315 | Timestamp removedTimestamp = removedItems.get(key); |
| 316 | if (removedTimestamp == null) { |
| 317 | return removedItems.putIfAbsent(key, timestamp) == null; |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame^] | 318 | } else if (timestamp.isNewerThan(removedTimestamp)) { |
Madan Jampani | 7e6cfe3 | 2015-02-19 17:44:44 -0800 | [diff] [blame] | 319 | return removedItems.replace(key, removedTimestamp, timestamp); |
| 320 | } else { |
| 321 | return false; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | @Override |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 326 | public void remove(K key, V value) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 327 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 328 | checkNotNull(key, ERROR_NULL_KEY); |
| 329 | checkNotNull(value, ERROR_NULL_VALUE); |
| 330 | |
| 331 | Timestamp timestamp = clockService.getTimestamp(key, value); |
| 332 | |
| 333 | if (removeInternal(key, timestamp)) { |
| 334 | notifyPeers(new InternalRemoveEvent<>(key, timestamp)); |
| 335 | EventuallyConsistentMapEvent<K, V> externalEvent |
| 336 | = new EventuallyConsistentMapEvent<>( |
| 337 | EventuallyConsistentMapEvent.Type.REMOVE, key, value); |
| 338 | notifyListeners(externalEvent); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | @Override |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 343 | public void putAll(Map<? extends K, ? extends V> m) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 344 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 345 | |
| 346 | List<PutEntry<K, V>> updates = new ArrayList<>(m.size()); |
| 347 | |
| 348 | for (Map.Entry<? extends K, ? extends V> entry : m.entrySet()) { |
| 349 | K key = entry.getKey(); |
| 350 | V value = entry.getValue(); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 351 | |
| 352 | checkNotNull(key, ERROR_NULL_KEY); |
| 353 | checkNotNull(value, ERROR_NULL_VALUE); |
| 354 | |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 355 | Timestamp timestamp = clockService.getTimestamp(key, value); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 356 | |
| 357 | if (putInternal(key, value, timestamp)) { |
| 358 | updates.add(new PutEntry<>(key, value, timestamp)); |
| 359 | } |
| 360 | } |
| 361 | |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 362 | if (!updates.isEmpty()) { |
| 363 | notifyPeers(new InternalPutEvent<>(updates)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 364 | |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 365 | for (PutEntry<K, V> entry : updates) { |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 366 | EventuallyConsistentMapEvent<K, V> externalEvent = |
| 367 | new EventuallyConsistentMapEvent<>( |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 368 | EventuallyConsistentMapEvent.Type.PUT, entry.key(), |
| 369 | entry.value()); |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 370 | notifyListeners(externalEvent); |
| 371 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public void clear() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 377 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 378 | |
| 379 | List<RemoveEntry<K>> removed = new ArrayList<>(items.size()); |
| 380 | |
| 381 | for (K key : items.keySet()) { |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 382 | // TODO also this is not applicable if value is important for timestamp? |
| 383 | Timestamp timestamp = clockService.getTimestamp(key, null); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 384 | |
| 385 | if (removeInternal(key, timestamp)) { |
| 386 | removed.add(new RemoveEntry<>(key, timestamp)); |
| 387 | } |
| 388 | } |
| 389 | |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 390 | if (!removed.isEmpty()) { |
| 391 | notifyPeers(new InternalRemoveEvent<>(removed)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 392 | |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 393 | for (RemoveEntry<K> entry : removed) { |
| 394 | EventuallyConsistentMapEvent<K, V> externalEvent |
| 395 | = new EventuallyConsistentMapEvent<>( |
| 396 | EventuallyConsistentMapEvent.Type.REMOVE, entry.key(), |
| 397 | null); |
| 398 | notifyListeners(externalEvent); |
| 399 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
| 403 | @Override |
| 404 | public Set<K> keySet() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 405 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 406 | |
| 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 | |
| 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(); |
Madan Jampani | 337bb44 | 2015-02-19 14:29:18 -0800 | [diff] [blame] | 448 | broadcastMessageExecutor.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); |
| 453 | clusterCommunicator.removeSubscriber(removeMessageSubject); |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 454 | clusterCommunicator.removeSubscriber(antiEntropyAdvertisementSubject); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 457 | private void notifyListeners(EventuallyConsistentMapEvent<K, V> event) { |
| 458 | for (EventuallyConsistentMapListener<K, V> listener : listeners) { |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 459 | listener.event(event); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | private void notifyPeers(InternalPutEvent event) { |
Brian O'Connor | b289422 | 2015-02-20 22:05:19 -0800 | [diff] [blame] | 464 | // FIXME extremely memory expensive when we are overrun |
| 465 | // broadcastMessageExecutor.execute(() -> broadcastMessage(updateMessageSubject, event)); |
| 466 | broadcastMessage(updateMessageSubject, event); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | private void notifyPeers(InternalRemoveEvent event) { |
Brian O'Connor | b289422 | 2015-02-20 22:05:19 -0800 | [diff] [blame] | 470 | // FIXME extremely memory expensive when we are overrun |
| 471 | // broadcastMessageExecutor.execute(() -> broadcastMessage(removeMessageSubject, event)); |
| 472 | broadcastMessage(removeMessageSubject, event); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Jonathan Hart | 7d656f4 | 2015-01-27 14:07:23 -0800 | [diff] [blame] | 475 | private void broadcastMessage(MessageSubject subject, Object event) { |
Brian O'Connor | b289422 | 2015-02-20 22:05:19 -0800 | [diff] [blame] | 476 | // FIXME can we parallelize the serialization... use the caller??? |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 477 | ClusterMessage message = new ClusterMessage( |
| 478 | clusterService.getLocalNode().id(), |
| 479 | subject, |
| 480 | serializer.encode(event)); |
Brian O'Connor | b289422 | 2015-02-20 22:05:19 -0800 | [diff] [blame] | 481 | //broadcastMessageExecutor.execute(() -> clusterCommunicator.broadcast(message)); |
Madan Jampani | 337bb44 | 2015-02-19 14:29:18 -0800 | [diff] [blame] | 482 | clusterCommunicator.broadcast(message); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | private void unicastMessage(NodeId peer, |
| 486 | MessageSubject subject, |
| 487 | Object event) throws IOException { |
| 488 | ClusterMessage message = new ClusterMessage( |
| 489 | clusterService.getLocalNode().id(), |
| 490 | subject, |
| 491 | serializer.encode(event)); |
| 492 | clusterCommunicator.unicast(message, peer); |
| 493 | } |
| 494 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 495 | private final class SendAdvertisementTask implements Runnable { |
| 496 | @Override |
| 497 | public void run() { |
| 498 | if (Thread.currentThread().isInterrupted()) { |
| 499 | log.info("Interrupted, quitting"); |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | try { |
| 504 | final NodeId self = clusterService.getLocalNode().id(); |
| 505 | Set<ControllerNode> nodes = clusterService.getNodes(); |
| 506 | |
| 507 | List<NodeId> nodeIds = nodes.stream() |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 508 | .map(ControllerNode::id) |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 509 | .collect(Collectors.toList()); |
| 510 | |
| 511 | if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) { |
| 512 | log.trace("No other peers in the cluster."); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | NodeId peer; |
| 517 | do { |
| 518 | int idx = RandomUtils.nextInt(0, nodeIds.size()); |
| 519 | peer = nodeIds.get(idx); |
| 520 | } while (peer.equals(self)); |
| 521 | |
| 522 | if (Thread.currentThread().isInterrupted()) { |
| 523 | log.info("Interrupted, quitting"); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | AntiEntropyAdvertisement<K> ad = createAdvertisement(); |
| 528 | |
| 529 | try { |
| 530 | unicastMessage(peer, antiEntropyAdvertisementSubject, ad); |
| 531 | } catch (IOException e) { |
| 532 | log.debug("Failed to send anti-entropy advertisement to {}", peer); |
| 533 | } |
| 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; |
Jonathan Hart | 3469e60 | 2015-02-19 13:50:27 -0800 | [diff] [blame] | 555 | boolean sync = false; |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 556 | |
| 557 | synchronized (this) { |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 558 | externalEvents = antiEntropyCheckLocalItems(ad); |
| 559 | |
| 560 | antiEntropyCheckLocalRemoved(ad); |
| 561 | |
| 562 | externalEvents.addAll(antiEntropyCheckRemoteRemoved(ad)); |
| 563 | |
| 564 | // if remote ad has something unknown, actively sync |
| 565 | for (K key : ad.timestamps().keySet()) { |
| 566 | if (!items.containsKey(key)) { |
Jonathan Hart | 3469e60 | 2015-02-19 13:50:27 -0800 | [diff] [blame] | 567 | sync = true; |
| 568 | break; |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | } // synchronized (this) |
| 572 | |
Jonathan Hart | 3469e60 | 2015-02-19 13:50:27 -0800 | [diff] [blame] | 573 | // Send the advertisement outside the synchronized block |
| 574 | if (sync) { |
| 575 | final NodeId sender = ad.sender(); |
| 576 | AntiEntropyAdvertisement<K> myAd = createAdvertisement(); |
| 577 | try { |
| 578 | unicastMessage(sender, antiEntropyAdvertisementSubject, myAd); |
| 579 | } catch (IOException e) { |
| 580 | log.debug( |
| 581 | "Failed to send reactive anti-entropy advertisement to {}", |
| 582 | sender); |
| 583 | } |
| 584 | } |
| 585 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 586 | externalEvents.forEach(this::notifyListeners); |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Checks if any of the remote's live items or tombstones are out of date |
| 591 | * according to our local live item list, or if our live items are out of |
| 592 | * date according to the remote's tombstone list. |
| 593 | * If the local copy is more recent, it will be pushed to the remote. If the |
| 594 | * remote has a more recent remove, we apply that to the local state. |
| 595 | * |
| 596 | * @param ad remote anti-entropy advertisement |
| 597 | * @return list of external events relating to local operations performed |
| 598 | */ |
| 599 | // Guarded by synchronized (this) |
| 600 | private List<EventuallyConsistentMapEvent<K, V>> antiEntropyCheckLocalItems( |
| 601 | AntiEntropyAdvertisement<K> ad) { |
| 602 | final List<EventuallyConsistentMapEvent<K, V>> externalEvents |
| 603 | = new LinkedList<>(); |
| 604 | final NodeId sender = ad.sender(); |
| 605 | |
| 606 | final List<PutEntry<K, V>> updatesToSend = new ArrayList<>(); |
| 607 | |
| 608 | for (Map.Entry<K, Timestamped<V>> item : items.entrySet()) { |
| 609 | K key = item.getKey(); |
| 610 | Timestamped<V> localValue = item.getValue(); |
| 611 | |
| 612 | Timestamp remoteTimestamp = ad.timestamps().get(key); |
| 613 | if (remoteTimestamp == null) { |
| 614 | remoteTimestamp = ad.tombstones().get(key); |
| 615 | } |
| 616 | if (remoteTimestamp == null || localValue |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame^] | 617 | .isNewerThan(remoteTimestamp)) { |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 618 | // local value is more recent, push to sender |
| 619 | updatesToSend |
| 620 | .add(new PutEntry<>(key, localValue.value(), |
| 621 | localValue.timestamp())); |
| 622 | } |
| 623 | |
| 624 | Timestamp remoteDeadTimestamp = ad.tombstones().get(key); |
| 625 | if (remoteDeadTimestamp != null && |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame^] | 626 | remoteDeadTimestamp.isNewerThan(localValue.timestamp())) { |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 627 | // sender has a more recent remove |
| 628 | if (removeInternal(key, remoteDeadTimestamp)) { |
| 629 | externalEvents.add(new EventuallyConsistentMapEvent<>( |
| 630 | EventuallyConsistentMapEvent.Type.REMOVE, key, null)); |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | // Send all updates to the peer at once |
| 636 | if (!updatesToSend.isEmpty()) { |
| 637 | try { |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 638 | unicastMessage(sender, updateMessageSubject, |
| 639 | new InternalPutEvent<>(updatesToSend)); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 640 | } catch (IOException e) { |
| 641 | log.warn("Failed to send advertisement response", e); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | return externalEvents; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * Checks if any items in the remote live list are out of date according |
| 650 | * to our tombstone list. If we find we have a more up to date tombstone, |
| 651 | * we'll send it to the remote. |
| 652 | * |
| 653 | * @param ad remote anti-entropy advertisement |
| 654 | */ |
| 655 | // Guarded by synchronized (this) |
| 656 | private void antiEntropyCheckLocalRemoved(AntiEntropyAdvertisement<K> ad) { |
| 657 | final NodeId sender = ad.sender(); |
| 658 | |
| 659 | final List<RemoveEntry<K>> removesToSend = new ArrayList<>(); |
| 660 | |
| 661 | for (Map.Entry<K, Timestamp> dead : removedItems.entrySet()) { |
| 662 | K key = dead.getKey(); |
| 663 | Timestamp localDeadTimestamp = dead.getValue(); |
| 664 | |
| 665 | Timestamp remoteLiveTimestamp = ad.timestamps().get(key); |
| 666 | if (remoteLiveTimestamp != null |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame^] | 667 | && localDeadTimestamp.isNewerThan(remoteLiveTimestamp)) { |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 668 | // sender has zombie, push remove |
| 669 | removesToSend |
| 670 | .add(new RemoveEntry<>(key, localDeadTimestamp)); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | // Send all removes to the peer at once |
| 675 | if (!removesToSend.isEmpty()) { |
| 676 | try { |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 677 | unicastMessage(sender, removeMessageSubject, |
| 678 | new InternalRemoveEvent<>(removesToSend)); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 679 | } catch (IOException e) { |
| 680 | log.warn("Failed to send advertisement response", e); |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * Checks if any of the local live items are out of date according to the |
| 687 | * remote's tombstone advertisements. If we find a local item is out of date, |
| 688 | * we'll apply the remove operation to the local state. |
| 689 | * |
| 690 | * @param ad remote anti-entropy advertisement |
| 691 | * @return list of external events relating to local operations performed |
| 692 | */ |
| 693 | // Guarded by synchronized (this) |
| 694 | private List<EventuallyConsistentMapEvent<K, V>> |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 695 | antiEntropyCheckRemoteRemoved(AntiEntropyAdvertisement<K> ad) { |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 696 | final List<EventuallyConsistentMapEvent<K, V>> externalEvents |
| 697 | = new LinkedList<>(); |
| 698 | |
| 699 | for (Map.Entry<K, Timestamp> remoteDead : ad.tombstones().entrySet()) { |
| 700 | K key = remoteDead.getKey(); |
| 701 | Timestamp remoteDeadTimestamp = remoteDead.getValue(); |
| 702 | |
| 703 | Timestamped<V> local = items.get(key); |
| 704 | Timestamp localDead = removedItems.get(key); |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame^] | 705 | if (local != null && remoteDeadTimestamp.isNewerThan( |
| 706 | local.timestamp())) { |
| 707 | // If the remote has a more recent tombstone than either our local |
| 708 | // value, then do a remove with their timestamp |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 709 | if (removeInternal(key, remoteDeadTimestamp)) { |
| 710 | externalEvents.add(new EventuallyConsistentMapEvent<>( |
| 711 | EventuallyConsistentMapEvent.Type.REMOVE, key, null)); |
| 712 | } |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame^] | 713 | } else if (localDead != null && remoteDeadTimestamp.isNewerThan( |
| 714 | localDead)) { |
| 715 | // If the remote has a more recent tombstone than us, update ours |
| 716 | // to their timestamp |
| 717 | removeInternal(key, remoteDeadTimestamp); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
| 721 | return externalEvents; |
| 722 | } |
| 723 | |
| 724 | private final class InternalAntiEntropyListener |
| 725 | implements ClusterMessageHandler { |
| 726 | |
| 727 | @Override |
| 728 | public void handle(ClusterMessage message) { |
Jonathan Hart | 4fd4ebb | 2015-02-04 17:38:48 -0800 | [diff] [blame] | 729 | log.trace("Received anti-entropy advertisement from peer: {}", |
| 730 | message.sender()); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 731 | AntiEntropyAdvertisement<K> advertisement = serializer.decode(message.payload()); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 732 | try { |
| 733 | handleAntiEntropyAdvertisement(advertisement); |
| 734 | } catch (Exception e) { |
| 735 | log.warn("Exception thrown handling advertisements", e); |
| 736 | } |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 740 | private final class InternalPutEventListener implements |
| 741 | ClusterMessageHandler { |
| 742 | @Override |
| 743 | public void handle(ClusterMessage message) { |
| 744 | log.debug("Received put event from peer: {}", message.sender()); |
| 745 | InternalPutEvent<K, V> event = serializer.decode(message.payload()); |
| 746 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 747 | try { |
| 748 | for (PutEntry<K, V> entry : event.entries()) { |
| 749 | K key = entry.key(); |
| 750 | V value = entry.value(); |
| 751 | Timestamp timestamp = entry.timestamp(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 752 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 753 | if (putInternal(key, value, timestamp)) { |
| 754 | EventuallyConsistentMapEvent<K, V> externalEvent = |
| 755 | new EventuallyConsistentMapEvent<>( |
| 756 | EventuallyConsistentMapEvent.Type.PUT, key, |
| 757 | value); |
| 758 | notifyListeners(externalEvent); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 759 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 760 | } |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 761 | } catch (Exception e) { |
| 762 | log.warn("Exception thrown handling put", e); |
| 763 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
| 767 | private final class InternalRemoveEventListener implements |
| 768 | ClusterMessageHandler { |
| 769 | @Override |
| 770 | public void handle(ClusterMessage message) { |
| 771 | log.debug("Received remove event from peer: {}", message.sender()); |
| 772 | InternalRemoveEvent<K> event = serializer.decode(message.payload()); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 773 | try { |
| 774 | for (RemoveEntry<K> entry : event.entries()) { |
| 775 | K key = entry.key(); |
| 776 | Timestamp timestamp = entry.timestamp(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 777 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 778 | if (removeInternal(key, timestamp)) { |
| 779 | EventuallyConsistentMapEvent<K, V> externalEvent |
| 780 | = new EventuallyConsistentMapEvent<>( |
| 781 | EventuallyConsistentMapEvent.Type.REMOVE, |
| 782 | key, null); |
| 783 | notifyListeners(externalEvent); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 784 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 785 | } |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 786 | } catch (Exception e) { |
| 787 | log.warn("Exception thrown handling remove", e); |
| 788 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 792 | } |