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