blob: fd05d7f44924a3c8f161bbfb0ca42c437d907cb1 [file] [log] [blame]
Jonathan Hartdb3af892015-01-26 13:19:07 -08001/*
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 Hart77bdd262015-02-03 09:07:48 -080016package org.onosproject.store.ecmap;
Jonathan Hartdb3af892015-01-26 13:19:07 -080017
Jonathan Hartaaa56572015-01-28 21:56:35 -080018import org.apache.commons.lang3.RandomUtils;
Jonathan Hartf9108232015-02-02 16:37:35 -080019import org.apache.commons.lang3.tuple.Pair;
Jonathan Hartdb3af892015-01-26 13:19:07 -080020import org.onlab.util.KryoNamespace;
21import org.onosproject.cluster.ClusterService;
Jonathan Hartaaa56572015-01-28 21:56:35 -080022import org.onosproject.cluster.ControllerNode;
Jonathan Hartdb3af892015-01-26 13:19:07 -080023import org.onosproject.cluster.NodeId;
24import org.onosproject.store.Timestamp;
25import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
26import org.onosproject.store.cluster.messaging.ClusterMessage;
27import org.onosproject.store.cluster.messaging.ClusterMessageHandler;
28import org.onosproject.store.cluster.messaging.MessageSubject;
Jonathan Hart77bdd262015-02-03 09:07:48 -080029import org.onosproject.store.impl.ClockService;
30import org.onosproject.store.impl.Timestamped;
31import org.onosproject.store.impl.WallClockTimestamp;
Jonathan Hartdb3af892015-01-26 13:19:07 -080032import org.onosproject.store.serializers.KryoSerializer;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import java.io.IOException;
37import java.util.ArrayList;
38import java.util.Collection;
Jonathan Hartaaa56572015-01-28 21:56:35 -080039import java.util.HashMap;
40import java.util.LinkedList;
Jonathan Hartdb3af892015-01-26 13:19:07 -080041import java.util.List;
42import java.util.Map;
43import java.util.Set;
44import java.util.concurrent.ConcurrentHashMap;
45import java.util.concurrent.CopyOnWriteArraySet;
46import java.util.concurrent.ExecutorService;
47import java.util.concurrent.Executors;
48import java.util.concurrent.ScheduledExecutorService;
Jonathan Hartaaa56572015-01-28 21:56:35 -080049import java.util.concurrent.TimeUnit;
Jonathan Hartdb3af892015-01-26 13:19:07 -080050import java.util.stream.Collectors;
51
52import static com.google.common.base.Preconditions.checkNotNull;
53import static com.google.common.base.Preconditions.checkState;
54import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Brian O'Connor4b2ba5f2015-02-18 20:54:00 -080055import static org.onlab.util.Tools.groupedThreads;
Jonathan Hartdb3af892015-01-26 13:19:07 -080056import static org.onlab.util.Tools.minPriority;
Jonathan Hartdb3af892015-01-26 13:19:07 -080057
58/**
59 * Distributed Map implementation which uses optimistic replication and gossip
60 * based techniques to provide an eventually consistent data store.
61 */
62public 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
70 private final String mapName;
71 private final ClusterService clusterService;
72 private final ClusterCommunicationService clusterCommunicator;
73 private final KryoSerializer serializer;
74
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080075 private final ClockService<K, V> clockService;
Jonathan Hartdb3af892015-01-26 13:19:07 -080076
77 private final MessageSubject updateMessageSubject;
78 private final MessageSubject removeMessageSubject;
Jonathan Hartaaa56572015-01-28 21:56:35 -080079 private final MessageSubject antiEntropyAdvertisementSubject;
Jonathan Hartdb3af892015-01-26 13:19:07 -080080
Jonathan Hartaaa56572015-01-28 21:56:35 -080081 private final Set<EventuallyConsistentMapListener<K, V>> listeners
Jonathan Hartdb3af892015-01-26 13:19:07 -080082 = new CopyOnWriteArraySet<>();
83
84 private final ExecutorService executor;
85
86 private final ScheduledExecutorService backgroundExecutor;
87
Madan Jampanib28e4ad2015-02-19 12:31:37 -080088 private ExecutorService broadcastMessageExecutor;
Madan Jampani28726282015-02-19 11:40:23 -080089
Jonathan Hartdb3af892015-01-26 13:19:07 -080090 private volatile boolean destroyed = false;
Jonathan Hart539a6462015-01-27 17:05:43 -080091 private static final String ERROR_DESTROYED = " map is already destroyed";
Jonathan Hartdb3af892015-01-26 13:19:07 -080092
Jonathan Hart4f397e82015-02-04 09:10:41 -080093 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 Hartdb3af892015-01-26 13:19:07 -080096 // 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.
102 *
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).
107 *
108 * The client is expected to provide an
109 * {@link org.onlab.util.KryoNamespace.Builder} with which all classes that
110 * will be stored in this map have been registered (including referenced
111 * classes). This serializer will be used to serialize both K and V for
112 * inter-node notifications.
113 *
114 * The client must provide an {@link org.onosproject.store.impl.ClockService}
115 * which can generate timestamps for a given key. The clock service is free
116 * to generate timestamps however it wishes, however these timestamps will
117 * be used to serialize updates to the map so they must be strict enough
118 * to ensure updates are properly ordered for the use case (i.e. in some
119 * cases wallclock time will suffice, whereas in other cases logical time
120 * will be necessary).
121 *
122 * @param mapName a String identifier for the map.
123 * @param clusterService the cluster service
124 * @param clusterCommunicator the cluster communications service
125 * @param serializerBuilder a Kryo namespace builder that can serialize
126 * both K and V
127 * @param clockService a clock service able to generate timestamps
128 * for K
129 */
130 public EventuallyConsistentMapImpl(String mapName,
131 ClusterService clusterService,
132 ClusterCommunicationService clusterCommunicator,
133 KryoNamespace.Builder serializerBuilder,
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800134 ClockService<K, V> clockService) {
Jonathan Hartdb3af892015-01-26 13:19:07 -0800135
136 this.mapName = checkNotNull(mapName);
137 this.clusterService = checkNotNull(clusterService);
138 this.clusterCommunicator = checkNotNull(clusterCommunicator);
139
140 serializer = createSerializer(checkNotNull(serializerBuilder));
141
142 this.clockService = checkNotNull(clockService);
143
144 items = new ConcurrentHashMap<>();
145 removedItems = new ConcurrentHashMap<>();
146
Brian O'Connor4b2ba5f2015-02-18 20:54:00 -0800147 executor = Executors //FIXME
148 .newFixedThreadPool(4, groupedThreads("onos/ecm", mapName + "-fg-%d"));
Jonathan Hartdb3af892015-01-26 13:19:07 -0800149
Madan Jampani28726282015-02-19 11:40:23 -0800150 broadcastMessageExecutor = Executors.newSingleThreadExecutor(groupedThreads("onos/ecm", mapName + "-notify"));
151
Jonathan Hartdb3af892015-01-26 13:19:07 -0800152 backgroundExecutor =
153 newSingleThreadScheduledExecutor(minPriority(
Brian O'Connor4b2ba5f2015-02-18 20:54:00 -0800154 groupedThreads("onos/ecm", mapName + "-bg-%d")));
Jonathan Hartdb3af892015-01-26 13:19:07 -0800155
Jonathan Hartaaa56572015-01-28 21:56:35 -0800156 // start anti-entropy thread
157 backgroundExecutor.scheduleAtFixedRate(new SendAdvertisementTask(),
158 initialDelaySec, periodSec,
159 TimeUnit.SECONDS);
160
Jonathan Hartdb3af892015-01-26 13:19:07 -0800161 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 Hartaaa56572015-01-28 21:56:35 -0800167 antiEntropyAdvertisementSubject = new MessageSubject("ecm-" + mapName + "-anti-entropy");
168 clusterCommunicator.addSubscriber(antiEntropyAdvertisementSubject,
169 new InternalAntiEntropyListener());
Jonathan Hartdb3af892015-01-26 13:19:07 -0800170 }
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 Hart539a6462015-01-27 17:05:43 -0800180 .register(RemoveEntry.class)
Jonathan Hartdb3af892015-01-26 13:19:07 -0800181 .register(ArrayList.class)
182 .register(InternalPutEvent.class)
183 .register(InternalRemoveEvent.class)
Jonathan Hartaaa56572015-01-28 21:56:35 -0800184 .register(AntiEntropyAdvertisement.class)
185 .register(HashMap.class)
Jonathan Hartdb3af892015-01-26 13:19:07 -0800186 .build();
Jonathan Hartdb3af892015-01-26 13:19:07 -0800187 }
188 };
189 }
190
Madan Jampanib28e4ad2015-02-19 12:31:37 -0800191 /**
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 Hartdb3af892015-01-26 13:19:07 -0800203 @Override
204 public int size() {
Jonathan Hart539a6462015-01-27 17:05:43 -0800205 checkState(!destroyed, mapName + ERROR_DESTROYED);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800206 return items.size();
207 }
208
209 @Override
210 public boolean isEmpty() {
Jonathan Hart539a6462015-01-27 17:05:43 -0800211 checkState(!destroyed, mapName + ERROR_DESTROYED);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800212 return items.isEmpty();
213 }
214
215 @Override
216 public boolean containsKey(K key) {
Jonathan Hart539a6462015-01-27 17:05:43 -0800217 checkState(!destroyed, mapName + ERROR_DESTROYED);
Jonathan Hart4f397e82015-02-04 09:10:41 -0800218 checkNotNull(key, ERROR_NULL_KEY);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800219 return items.containsKey(key);
220 }
221
222 @Override
223 public boolean containsValue(V value) {
Jonathan Hart539a6462015-01-27 17:05:43 -0800224 checkState(!destroyed, mapName + ERROR_DESTROYED);
Jonathan Hart4f397e82015-02-04 09:10:41 -0800225 checkNotNull(value, ERROR_NULL_VALUE);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800226
227 return items.values().stream()
228 .anyMatch(timestamped -> timestamped.value().equals(value));
229 }
230
231 @Override
232 public V get(K key) {
Jonathan Hart539a6462015-01-27 17:05:43 -0800233 checkState(!destroyed, mapName + ERROR_DESTROYED);
Jonathan Hart4f397e82015-02-04 09:10:41 -0800234 checkNotNull(key, ERROR_NULL_KEY);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800235
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 Hart539a6462015-01-27 17:05:43 -0800245 checkState(!destroyed, mapName + ERROR_DESTROYED);
Jonathan Hart4f397e82015-02-04 09:10:41 -0800246 checkNotNull(key, ERROR_NULL_KEY);
247 checkNotNull(value, ERROR_NULL_VALUE);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800248
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800249 Timestamp timestamp = clockService.getTimestamp(key, value);
250
Jonathan Hartdb3af892015-01-26 13:19:07 -0800251 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) {
261 synchronized (this) {
262 Timestamp removed = removedItems.get(key);
263 if (removed != null && removed.compareTo(timestamp) > 0) {
Jonathan Hart07e58be2015-02-12 09:57:16 -0800264 log.debug("ecmap - removed was newer {}", value);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800265 return false;
266 }
267
268 Timestamped<V> existing = items.get(key);
269 if (existing != null && existing.isNewer(timestamp)) {
Jonathan Hart07e58be2015-02-12 09:57:16 -0800270 log.debug("ecmap - existing was newer {}", value);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800271 return false;
272 } else {
273 items.put(key, new Timestamped<>(value, timestamp));
274 removedItems.remove(key);
275 return true;
276 }
277 }
278 }
279
280 @Override
281 public void remove(K key) {
Jonathan Hart539a6462015-01-27 17:05:43 -0800282 checkState(!destroyed, mapName + ERROR_DESTROYED);
Jonathan Hart4f397e82015-02-04 09:10:41 -0800283 checkNotNull(key, ERROR_NULL_KEY);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800284
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800285 // TODO prevent calls here if value is important for timestamp
286 Timestamp timestamp = clockService.getTimestamp(key, null);
287
Jonathan Hartdb3af892015-01-26 13:19:07 -0800288 if (removeInternal(key, timestamp)) {
289 notifyPeers(new InternalRemoveEvent<>(key, timestamp));
290 EventuallyConsistentMapEvent<K, V> externalEvent
291 = new EventuallyConsistentMapEvent<>(
292 EventuallyConsistentMapEvent.Type.REMOVE, key, null);
293 notifyListeners(externalEvent);
294 }
295 }
296
297 private boolean removeInternal(K key, Timestamp timestamp) {
298 synchronized (this) {
299 if (items.get(key) != null && items.get(key).isNewer(timestamp)) {
300 return false;
301 }
302
303 items.remove(key);
304 removedItems.put(key, timestamp);
305 return true;
306 }
307 }
308
309 @Override
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800310 public void remove(K key, V value) {
311 checkState(!destroyed, mapName + ERROR_DESTROYED);
312 checkNotNull(key, ERROR_NULL_KEY);
313 checkNotNull(value, ERROR_NULL_VALUE);
314
315 Timestamp timestamp = clockService.getTimestamp(key, value);
316
317 if (removeInternal(key, timestamp)) {
318 notifyPeers(new InternalRemoveEvent<>(key, timestamp));
319 EventuallyConsistentMapEvent<K, V> externalEvent
320 = new EventuallyConsistentMapEvent<>(
321 EventuallyConsistentMapEvent.Type.REMOVE, key, value);
322 notifyListeners(externalEvent);
323 }
324 }
325
326 @Override
Jonathan Hartdb3af892015-01-26 13:19:07 -0800327 public void putAll(Map<? extends K, ? extends V> m) {
Jonathan Hart539a6462015-01-27 17:05:43 -0800328 checkState(!destroyed, mapName + ERROR_DESTROYED);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800329
330 List<PutEntry<K, V>> updates = new ArrayList<>(m.size());
331
332 for (Map.Entry<? extends K, ? extends V> entry : m.entrySet()) {
333 K key = entry.getKey();
334 V value = entry.getValue();
Jonathan Hart4f397e82015-02-04 09:10:41 -0800335
336 checkNotNull(key, ERROR_NULL_KEY);
337 checkNotNull(value, ERROR_NULL_VALUE);
338
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800339 Timestamp timestamp = clockService.getTimestamp(key, value);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800340
341 if (putInternal(key, value, timestamp)) {
342 updates.add(new PutEntry<>(key, value, timestamp));
343 }
344 }
345
Jonathan Hart584d2f32015-01-27 19:46:14 -0800346 if (!updates.isEmpty()) {
347 notifyPeers(new InternalPutEvent<>(updates));
Jonathan Hartdb3af892015-01-26 13:19:07 -0800348
Jonathan Hart584d2f32015-01-27 19:46:14 -0800349 for (PutEntry<K, V> entry : updates) {
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800350 EventuallyConsistentMapEvent<K, V> externalEvent =
351 new EventuallyConsistentMapEvent<>(
Jonathan Hart584d2f32015-01-27 19:46:14 -0800352 EventuallyConsistentMapEvent.Type.PUT, entry.key(),
353 entry.value());
354 notifyListeners(externalEvent);
355 }
Jonathan Hartdb3af892015-01-26 13:19:07 -0800356 }
357 }
358
359 @Override
360 public void clear() {
Jonathan Hart539a6462015-01-27 17:05:43 -0800361 checkState(!destroyed, mapName + ERROR_DESTROYED);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800362
363 List<RemoveEntry<K>> removed = new ArrayList<>(items.size());
364
365 for (K key : items.keySet()) {
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800366 // TODO also this is not applicable if value is important for timestamp?
367 Timestamp timestamp = clockService.getTimestamp(key, null);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800368
369 if (removeInternal(key, timestamp)) {
370 removed.add(new RemoveEntry<>(key, timestamp));
371 }
372 }
373
Jonathan Hart584d2f32015-01-27 19:46:14 -0800374 if (!removed.isEmpty()) {
375 notifyPeers(new InternalRemoveEvent<>(removed));
Jonathan Hartdb3af892015-01-26 13:19:07 -0800376
Jonathan Hart584d2f32015-01-27 19:46:14 -0800377 for (RemoveEntry<K> entry : removed) {
378 EventuallyConsistentMapEvent<K, V> externalEvent
379 = new EventuallyConsistentMapEvent<>(
380 EventuallyConsistentMapEvent.Type.REMOVE, entry.key(),
381 null);
382 notifyListeners(externalEvent);
383 }
Jonathan Hartdb3af892015-01-26 13:19:07 -0800384 }
385 }
386
387 @Override
388 public Set<K> keySet() {
Jonathan Hart539a6462015-01-27 17:05:43 -0800389 checkState(!destroyed, mapName + ERROR_DESTROYED);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800390
391 return items.keySet();
392 }
393
394 @Override
395 public Collection<V> values() {
Jonathan Hart539a6462015-01-27 17:05:43 -0800396 checkState(!destroyed, mapName + ERROR_DESTROYED);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800397
398 return items.values().stream()
399 .map(Timestamped::value)
400 .collect(Collectors.toList());
401 }
402
403 @Override
404 public Set<Map.Entry<K, V>> entrySet() {
Jonathan Hart539a6462015-01-27 17:05:43 -0800405 checkState(!destroyed, mapName + ERROR_DESTROYED);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800406
407 return items.entrySet().stream()
Jonathan Hartf9108232015-02-02 16:37:35 -0800408 .map(e -> Pair.of(e.getKey(), e.getValue().value()))
Jonathan Hartdb3af892015-01-26 13:19:07 -0800409 .collect(Collectors.toSet());
410 }
411
412 @Override
Jonathan Hart539a6462015-01-27 17:05:43 -0800413 public void addListener(EventuallyConsistentMapListener<K, V> listener) {
414 checkState(!destroyed, mapName + ERROR_DESTROYED);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800415
416 listeners.add(checkNotNull(listener));
417 }
418
419 @Override
Jonathan Hart539a6462015-01-27 17:05:43 -0800420 public void removeListener(EventuallyConsistentMapListener<K, V> listener) {
421 checkState(!destroyed, mapName + ERROR_DESTROYED);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800422
423 listeners.remove(checkNotNull(listener));
424 }
425
426 @Override
427 public void destroy() {
428 destroyed = true;
429
430 executor.shutdown();
431 backgroundExecutor.shutdown();
432
Jonathan Hart584d2f32015-01-27 19:46:14 -0800433 listeners.clear();
434
Jonathan Hartdb3af892015-01-26 13:19:07 -0800435 clusterCommunicator.removeSubscriber(updateMessageSubject);
436 clusterCommunicator.removeSubscriber(removeMessageSubject);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800437 clusterCommunicator.removeSubscriber(antiEntropyAdvertisementSubject);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800438 }
439
Jonathan Hartaaa56572015-01-28 21:56:35 -0800440 private void notifyListeners(EventuallyConsistentMapEvent<K, V> event) {
441 for (EventuallyConsistentMapListener<K, V> listener : listeners) {
Jonathan Hartdb3af892015-01-26 13:19:07 -0800442 listener.event(event);
443 }
444 }
445
446 private void notifyPeers(InternalPutEvent event) {
Jonathan Hart7d656f42015-01-27 14:07:23 -0800447 broadcastMessage(updateMessageSubject, event);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800448 }
449
450 private void notifyPeers(InternalRemoveEvent event) {
Jonathan Hart7d656f42015-01-27 14:07:23 -0800451 broadcastMessage(removeMessageSubject, event);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800452 }
453
Jonathan Hart7d656f42015-01-27 14:07:23 -0800454 private void broadcastMessage(MessageSubject subject, Object event) {
Jonathan Hartdb3af892015-01-26 13:19:07 -0800455 ClusterMessage message = new ClusterMessage(
456 clusterService.getLocalNode().id(),
457 subject,
458 serializer.encode(event));
Madan Jampani28726282015-02-19 11:40:23 -0800459 broadcastMessageExecutor.execute(() -> clusterCommunicator.broadcast(message));
Jonathan Hartdb3af892015-01-26 13:19:07 -0800460 }
461
462 private void unicastMessage(NodeId peer,
463 MessageSubject subject,
464 Object event) throws IOException {
465 ClusterMessage message = new ClusterMessage(
466 clusterService.getLocalNode().id(),
467 subject,
468 serializer.encode(event));
469 clusterCommunicator.unicast(message, peer);
470 }
471
Jonathan Hartaaa56572015-01-28 21:56:35 -0800472 private final class SendAdvertisementTask implements Runnable {
473 @Override
474 public void run() {
475 if (Thread.currentThread().isInterrupted()) {
476 log.info("Interrupted, quitting");
477 return;
478 }
479
480 try {
481 final NodeId self = clusterService.getLocalNode().id();
482 Set<ControllerNode> nodes = clusterService.getNodes();
483
484 List<NodeId> nodeIds = nodes.stream()
485 .map(node -> node.id())
486 .collect(Collectors.toList());
487
488 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
489 log.trace("No other peers in the cluster.");
490 return;
491 }
492
493 NodeId peer;
494 do {
495 int idx = RandomUtils.nextInt(0, nodeIds.size());
496 peer = nodeIds.get(idx);
497 } while (peer.equals(self));
498
499 if (Thread.currentThread().isInterrupted()) {
500 log.info("Interrupted, quitting");
501 return;
502 }
503
504 AntiEntropyAdvertisement<K> ad = createAdvertisement();
505
506 try {
507 unicastMessage(peer, antiEntropyAdvertisementSubject, ad);
508 } catch (IOException e) {
509 log.debug("Failed to send anti-entropy advertisement to {}", peer);
510 }
511 } catch (Exception e) {
512 // Catch all exceptions to avoid scheduled task being suppressed.
513 log.error("Exception thrown while sending advertisement", e);
514 }
515 }
516 }
517
518 private AntiEntropyAdvertisement<K> createAdvertisement() {
519 final NodeId self = clusterService.getLocalNode().id();
520
521 Map<K, Timestamp> timestamps = new HashMap<>(items.size());
522
523 items.forEach((key, value) -> timestamps.put(key, value.timestamp()));
524
525 Map<K, Timestamp> tombstones = new HashMap<>(removedItems);
526
527 return new AntiEntropyAdvertisement<>(self, timestamps, tombstones);
528 }
529
530 private void handleAntiEntropyAdvertisement(AntiEntropyAdvertisement<K> ad) {
531 List<EventuallyConsistentMapEvent<K, V>> externalEvents;
532
533 synchronized (this) {
534 final NodeId sender = ad.sender();
535
536 externalEvents = antiEntropyCheckLocalItems(ad);
537
538 antiEntropyCheckLocalRemoved(ad);
539
540 externalEvents.addAll(antiEntropyCheckRemoteRemoved(ad));
541
542 // if remote ad has something unknown, actively sync
543 for (K key : ad.timestamps().keySet()) {
544 if (!items.containsKey(key)) {
545 AntiEntropyAdvertisement<K> myAd = createAdvertisement();
546 try {
547 unicastMessage(sender, antiEntropyAdvertisementSubject,
548 myAd);
549 break;
550 } catch (IOException e) {
551 log.debug(
552 "Failed to send reactive anti-entropy advertisement to {}",
553 sender);
554 }
555 }
556 }
557 } // synchronized (this)
558
559 externalEvents.forEach(this::notifyListeners);
560 }
561
562 /**
563 * Checks if any of the remote's live items or tombstones are out of date
564 * according to our local live item list, or if our live items are out of
565 * date according to the remote's tombstone list.
566 * If the local copy is more recent, it will be pushed to the remote. If the
567 * remote has a more recent remove, we apply that to the local state.
568 *
569 * @param ad remote anti-entropy advertisement
570 * @return list of external events relating to local operations performed
571 */
572 // Guarded by synchronized (this)
573 private List<EventuallyConsistentMapEvent<K, V>> antiEntropyCheckLocalItems(
574 AntiEntropyAdvertisement<K> ad) {
575 final List<EventuallyConsistentMapEvent<K, V>> externalEvents
576 = new LinkedList<>();
577 final NodeId sender = ad.sender();
578
579 final List<PutEntry<K, V>> updatesToSend = new ArrayList<>();
580
581 for (Map.Entry<K, Timestamped<V>> item : items.entrySet()) {
582 K key = item.getKey();
583 Timestamped<V> localValue = item.getValue();
584
585 Timestamp remoteTimestamp = ad.timestamps().get(key);
586 if (remoteTimestamp == null) {
587 remoteTimestamp = ad.tombstones().get(key);
588 }
589 if (remoteTimestamp == null || localValue
590 .isNewer(remoteTimestamp)) {
591 // local value is more recent, push to sender
592 updatesToSend
593 .add(new PutEntry<>(key, localValue.value(),
594 localValue.timestamp()));
595 }
596
597 Timestamp remoteDeadTimestamp = ad.tombstones().get(key);
598 if (remoteDeadTimestamp != null &&
599 remoteDeadTimestamp.compareTo(localValue.timestamp()) > 0) {
600 // sender has a more recent remove
601 if (removeInternal(key, remoteDeadTimestamp)) {
602 externalEvents.add(new EventuallyConsistentMapEvent<>(
603 EventuallyConsistentMapEvent.Type.REMOVE, key, null));
604 }
605 }
606 }
607
608 // Send all updates to the peer at once
609 if (!updatesToSend.isEmpty()) {
610 try {
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800611 unicastMessage(sender, updateMessageSubject,
612 new InternalPutEvent<>(updatesToSend));
Jonathan Hartaaa56572015-01-28 21:56:35 -0800613 } catch (IOException e) {
614 log.warn("Failed to send advertisement response", e);
615 }
616 }
617
618 return externalEvents;
619 }
620
621 /**
622 * Checks if any items in the remote live list are out of date according
623 * to our tombstone list. If we find we have a more up to date tombstone,
624 * we'll send it to the remote.
625 *
626 * @param ad remote anti-entropy advertisement
627 */
628 // Guarded by synchronized (this)
629 private void antiEntropyCheckLocalRemoved(AntiEntropyAdvertisement<K> ad) {
630 final NodeId sender = ad.sender();
631
632 final List<RemoveEntry<K>> removesToSend = new ArrayList<>();
633
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
640 && localDeadTimestamp.compareTo(remoteLiveTimestamp) > 0) {
641 // sender has zombie, push remove
642 removesToSend
643 .add(new RemoveEntry<>(key, localDeadTimestamp));
644 }
645 }
646
647 // Send all removes to the peer at once
648 if (!removesToSend.isEmpty()) {
649 try {
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800650 unicastMessage(sender, removeMessageSubject,
651 new InternalRemoveEvent<>(removesToSend));
Jonathan Hartaaa56572015-01-28 21:56:35 -0800652 } catch (IOException e) {
653 log.warn("Failed to send advertisement response", e);
654 }
655 }
656 }
657
658 /**
659 * Checks if any of the local live items are out of date according to the
660 * remote's tombstone advertisements. If we find a local item is out of date,
661 * we'll apply the remove operation to the local state.
662 *
663 * @param ad remote anti-entropy advertisement
664 * @return list of external events relating to local operations performed
665 */
666 // Guarded by synchronized (this)
667 private List<EventuallyConsistentMapEvent<K, V>>
668 antiEntropyCheckRemoteRemoved(AntiEntropyAdvertisement<K> ad) {
669 final List<EventuallyConsistentMapEvent<K, V>> externalEvents
670 = new LinkedList<>();
671
672 for (Map.Entry<K, Timestamp> remoteDead : ad.tombstones().entrySet()) {
673 K key = remoteDead.getKey();
674 Timestamp remoteDeadTimestamp = remoteDead.getValue();
675
676 Timestamped<V> local = items.get(key);
677 Timestamp localDead = removedItems.get(key);
678 if (local != null
679 && remoteDeadTimestamp.compareTo(local.timestamp()) > 0) {
680 // remove our version
681 if (removeInternal(key, remoteDeadTimestamp)) {
682 externalEvents.add(new EventuallyConsistentMapEvent<>(
683 EventuallyConsistentMapEvent.Type.REMOVE, key, null));
684 }
685 } else if (localDead != null &&
686 remoteDeadTimestamp.compareTo(localDead) > 0) {
687 // If we both had the item as removed, but their timestamp is
688 // newer, update ours to the newer value
689 removedItems.put(key, remoteDeadTimestamp);
690 }
691 }
692
693 return externalEvents;
694 }
695
696 private final class InternalAntiEntropyListener
697 implements ClusterMessageHandler {
698
699 @Override
700 public void handle(ClusterMessage message) {
Jonathan Hart4fd4ebb2015-02-04 17:38:48 -0800701 log.trace("Received anti-entropy advertisement from peer: {}",
702 message.sender());
Jonathan Hartaaa56572015-01-28 21:56:35 -0800703 AntiEntropyAdvertisement<K> advertisement = serializer.decode(message.payload());
704 backgroundExecutor.submit(() -> {
705 try {
706 handleAntiEntropyAdvertisement(advertisement);
707 } catch (Exception e) {
708 log.warn("Exception thrown handling advertisements", e);
709 }
710 });
711 }
712 }
713
Jonathan Hartdb3af892015-01-26 13:19:07 -0800714 private final class InternalPutEventListener implements
715 ClusterMessageHandler {
716 @Override
717 public void handle(ClusterMessage message) {
718 log.debug("Received put event from peer: {}", message.sender());
719 InternalPutEvent<K, V> event = serializer.decode(message.payload());
720
721 executor.submit(() -> {
722 try {
723 for (PutEntry<K, V> entry : event.entries()) {
724 K key = entry.key();
725 V value = entry.value();
726 Timestamp timestamp = entry.timestamp();
727
728 if (putInternal(key, value, timestamp)) {
Jonathan Hartaaa56572015-01-28 21:56:35 -0800729 EventuallyConsistentMapEvent<K, V> externalEvent =
Jonathan Hartdb3af892015-01-26 13:19:07 -0800730 new EventuallyConsistentMapEvent<>(
731 EventuallyConsistentMapEvent.Type.PUT, key,
732 value);
733 notifyListeners(externalEvent);
734 }
735 }
736 } catch (Exception e) {
737 log.warn("Exception thrown handling put", e);
738 }
739 });
740 }
741 }
742
743 private final class InternalRemoveEventListener implements
744 ClusterMessageHandler {
745 @Override
746 public void handle(ClusterMessage message) {
747 log.debug("Received remove event from peer: {}", message.sender());
748 InternalRemoveEvent<K> event = serializer.decode(message.payload());
749
750 executor.submit(() -> {
751 try {
752 for (RemoveEntry<K> entry : event.entries()) {
753 K key = entry.key();
754 Timestamp timestamp = entry.timestamp();
755
756 if (removeInternal(key, timestamp)) {
Jonathan Hartaaa56572015-01-28 21:56:35 -0800757 EventuallyConsistentMapEvent<K, V> externalEvent
758 = new EventuallyConsistentMapEvent<>(
Jonathan Hartdb3af892015-01-26 13:19:07 -0800759 EventuallyConsistentMapEvent.Type.REMOVE,
760 key, null);
761 notifyListeners(externalEvent);
762 }
763 }
764 } catch (Exception e) {
765 log.warn("Exception thrown handling remove", e);
766 }
767 });
768 }
769 }
770
Jonathan Hartdb3af892015-01-26 13:19:07 -0800771}