blob: 20849ed0d108cce5c11154b54a8ed026ec58aa7c [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();
Madan Jampani337bb442015-02-19 14:29:18 -0800432 broadcastMessageExecutor.shutdown();
Jonathan Hartdb3af892015-01-26 13:19:07 -0800433
Jonathan Hart584d2f32015-01-27 19:46:14 -0800434 listeners.clear();
435
Jonathan Hartdb3af892015-01-26 13:19:07 -0800436 clusterCommunicator.removeSubscriber(updateMessageSubject);
437 clusterCommunicator.removeSubscriber(removeMessageSubject);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800438 clusterCommunicator.removeSubscriber(antiEntropyAdvertisementSubject);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800439 }
440
Jonathan Hartaaa56572015-01-28 21:56:35 -0800441 private void notifyListeners(EventuallyConsistentMapEvent<K, V> event) {
442 for (EventuallyConsistentMapListener<K, V> listener : listeners) {
Jonathan Hartdb3af892015-01-26 13:19:07 -0800443 listener.event(event);
444 }
445 }
446
447 private void notifyPeers(InternalPutEvent event) {
Madan Jampani337bb442015-02-19 14:29:18 -0800448 broadcastMessageExecutor.execute(() -> broadcastMessage(updateMessageSubject, event));
Jonathan Hartdb3af892015-01-26 13:19:07 -0800449 }
450
451 private void notifyPeers(InternalRemoveEvent event) {
Madan Jampani337bb442015-02-19 14:29:18 -0800452 broadcastMessageExecutor.execute(() -> broadcastMessage(removeMessageSubject, event));
Jonathan Hartdb3af892015-01-26 13:19:07 -0800453 }
454
Jonathan Hart7d656f42015-01-27 14:07:23 -0800455 private void broadcastMessage(MessageSubject subject, Object event) {
Jonathan Hartdb3af892015-01-26 13:19:07 -0800456 ClusterMessage message = new ClusterMessage(
457 clusterService.getLocalNode().id(),
458 subject,
459 serializer.encode(event));
Madan Jampani337bb442015-02-19 14:29:18 -0800460 clusterCommunicator.broadcast(message);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800461 }
462
463 private void unicastMessage(NodeId peer,
464 MessageSubject subject,
465 Object event) throws IOException {
466 ClusterMessage message = new ClusterMessage(
467 clusterService.getLocalNode().id(),
468 subject,
469 serializer.encode(event));
470 clusterCommunicator.unicast(message, peer);
471 }
472
Jonathan Hartaaa56572015-01-28 21:56:35 -0800473 private final class SendAdvertisementTask implements Runnable {
474 @Override
475 public void run() {
476 if (Thread.currentThread().isInterrupted()) {
477 log.info("Interrupted, quitting");
478 return;
479 }
480
481 try {
482 final NodeId self = clusterService.getLocalNode().id();
483 Set<ControllerNode> nodes = clusterService.getNodes();
484
485 List<NodeId> nodeIds = nodes.stream()
486 .map(node -> node.id())
487 .collect(Collectors.toList());
488
489 if (nodeIds.size() == 1 && nodeIds.get(0).equals(self)) {
490 log.trace("No other peers in the cluster.");
491 return;
492 }
493
494 NodeId peer;
495 do {
496 int idx = RandomUtils.nextInt(0, nodeIds.size());
497 peer = nodeIds.get(idx);
498 } while (peer.equals(self));
499
500 if (Thread.currentThread().isInterrupted()) {
501 log.info("Interrupted, quitting");
502 return;
503 }
504
505 AntiEntropyAdvertisement<K> ad = createAdvertisement();
506
507 try {
508 unicastMessage(peer, antiEntropyAdvertisementSubject, ad);
509 } catch (IOException e) {
510 log.debug("Failed to send anti-entropy advertisement to {}", peer);
511 }
512 } catch (Exception e) {
513 // Catch all exceptions to avoid scheduled task being suppressed.
514 log.error("Exception thrown while sending advertisement", e);
515 }
516 }
517 }
518
519 private AntiEntropyAdvertisement<K> createAdvertisement() {
520 final NodeId self = clusterService.getLocalNode().id();
521
522 Map<K, Timestamp> timestamps = new HashMap<>(items.size());
523
524 items.forEach((key, value) -> timestamps.put(key, value.timestamp()));
525
526 Map<K, Timestamp> tombstones = new HashMap<>(removedItems);
527
528 return new AntiEntropyAdvertisement<>(self, timestamps, tombstones);
529 }
530
531 private void handleAntiEntropyAdvertisement(AntiEntropyAdvertisement<K> ad) {
532 List<EventuallyConsistentMapEvent<K, V>> externalEvents;
533
534 synchronized (this) {
535 final NodeId sender = ad.sender();
536
537 externalEvents = antiEntropyCheckLocalItems(ad);
538
539 antiEntropyCheckLocalRemoved(ad);
540
541 externalEvents.addAll(antiEntropyCheckRemoteRemoved(ad));
542
543 // if remote ad has something unknown, actively sync
544 for (K key : ad.timestamps().keySet()) {
545 if (!items.containsKey(key)) {
546 AntiEntropyAdvertisement<K> myAd = createAdvertisement();
547 try {
548 unicastMessage(sender, antiEntropyAdvertisementSubject,
549 myAd);
550 break;
551 } catch (IOException e) {
552 log.debug(
553 "Failed to send reactive anti-entropy advertisement to {}",
554 sender);
555 }
556 }
557 }
558 } // synchronized (this)
559
560 externalEvents.forEach(this::notifyListeners);
561 }
562
563 /**
564 * Checks if any of the remote's live items or tombstones are out of date
565 * according to our local live item list, or if our live items are out of
566 * date according to the remote's tombstone list.
567 * If the local copy is more recent, it will be pushed to the remote. If the
568 * remote has a more recent remove, we apply that to the local state.
569 *
570 * @param ad remote anti-entropy advertisement
571 * @return list of external events relating to local operations performed
572 */
573 // Guarded by synchronized (this)
574 private List<EventuallyConsistentMapEvent<K, V>> antiEntropyCheckLocalItems(
575 AntiEntropyAdvertisement<K> ad) {
576 final List<EventuallyConsistentMapEvent<K, V>> externalEvents
577 = new LinkedList<>();
578 final NodeId sender = ad.sender();
579
580 final List<PutEntry<K, V>> updatesToSend = new ArrayList<>();
581
582 for (Map.Entry<K, Timestamped<V>> item : items.entrySet()) {
583 K key = item.getKey();
584 Timestamped<V> localValue = item.getValue();
585
586 Timestamp remoteTimestamp = ad.timestamps().get(key);
587 if (remoteTimestamp == null) {
588 remoteTimestamp = ad.tombstones().get(key);
589 }
590 if (remoteTimestamp == null || localValue
591 .isNewer(remoteTimestamp)) {
592 // local value is more recent, push to sender
593 updatesToSend
594 .add(new PutEntry<>(key, localValue.value(),
595 localValue.timestamp()));
596 }
597
598 Timestamp remoteDeadTimestamp = ad.tombstones().get(key);
599 if (remoteDeadTimestamp != null &&
600 remoteDeadTimestamp.compareTo(localValue.timestamp()) > 0) {
601 // sender has a more recent remove
602 if (removeInternal(key, remoteDeadTimestamp)) {
603 externalEvents.add(new EventuallyConsistentMapEvent<>(
604 EventuallyConsistentMapEvent.Type.REMOVE, key, null));
605 }
606 }
607 }
608
609 // Send all updates to the peer at once
610 if (!updatesToSend.isEmpty()) {
611 try {
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800612 unicastMessage(sender, updateMessageSubject,
613 new InternalPutEvent<>(updatesToSend));
Jonathan Hartaaa56572015-01-28 21:56:35 -0800614 } catch (IOException e) {
615 log.warn("Failed to send advertisement response", e);
616 }
617 }
618
619 return externalEvents;
620 }
621
622 /**
623 * Checks if any items in the remote live list are out of date according
624 * to our tombstone list. If we find we have a more up to date tombstone,
625 * we'll send it to the remote.
626 *
627 * @param ad remote anti-entropy advertisement
628 */
629 // Guarded by synchronized (this)
630 private void antiEntropyCheckLocalRemoved(AntiEntropyAdvertisement<K> ad) {
631 final NodeId sender = ad.sender();
632
633 final List<RemoveEntry<K>> removesToSend = new ArrayList<>();
634
635 for (Map.Entry<K, Timestamp> dead : removedItems.entrySet()) {
636 K key = dead.getKey();
637 Timestamp localDeadTimestamp = dead.getValue();
638
639 Timestamp remoteLiveTimestamp = ad.timestamps().get(key);
640 if (remoteLiveTimestamp != null
641 && localDeadTimestamp.compareTo(remoteLiveTimestamp) > 0) {
642 // sender has zombie, push remove
643 removesToSend
644 .add(new RemoveEntry<>(key, localDeadTimestamp));
645 }
646 }
647
648 // Send all removes to the peer at once
649 if (!removesToSend.isEmpty()) {
650 try {
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800651 unicastMessage(sender, removeMessageSubject,
652 new InternalRemoveEvent<>(removesToSend));
Jonathan Hartaaa56572015-01-28 21:56:35 -0800653 } catch (IOException e) {
654 log.warn("Failed to send advertisement response", e);
655 }
656 }
657 }
658
659 /**
660 * Checks if any of the local live items are out of date according to the
661 * remote's tombstone advertisements. If we find a local item is out of date,
662 * we'll apply the remove operation to the local state.
663 *
664 * @param ad remote anti-entropy advertisement
665 * @return list of external events relating to local operations performed
666 */
667 // Guarded by synchronized (this)
668 private List<EventuallyConsistentMapEvent<K, V>>
669 antiEntropyCheckRemoteRemoved(AntiEntropyAdvertisement<K> ad) {
670 final List<EventuallyConsistentMapEvent<K, V>> externalEvents
671 = new LinkedList<>();
672
673 for (Map.Entry<K, Timestamp> remoteDead : ad.tombstones().entrySet()) {
674 K key = remoteDead.getKey();
675 Timestamp remoteDeadTimestamp = remoteDead.getValue();
676
677 Timestamped<V> local = items.get(key);
678 Timestamp localDead = removedItems.get(key);
679 if (local != null
680 && remoteDeadTimestamp.compareTo(local.timestamp()) > 0) {
681 // remove our version
682 if (removeInternal(key, remoteDeadTimestamp)) {
683 externalEvents.add(new EventuallyConsistentMapEvent<>(
684 EventuallyConsistentMapEvent.Type.REMOVE, key, null));
685 }
686 } else if (localDead != null &&
687 remoteDeadTimestamp.compareTo(localDead) > 0) {
688 // If we both had the item as removed, but their timestamp is
689 // newer, update ours to the newer value
690 removedItems.put(key, remoteDeadTimestamp);
691 }
692 }
693
694 return externalEvents;
695 }
696
697 private final class InternalAntiEntropyListener
698 implements ClusterMessageHandler {
699
700 @Override
701 public void handle(ClusterMessage message) {
Jonathan Hart4fd4ebb2015-02-04 17:38:48 -0800702 log.trace("Received anti-entropy advertisement from peer: {}",
703 message.sender());
Jonathan Hartaaa56572015-01-28 21:56:35 -0800704 AntiEntropyAdvertisement<K> advertisement = serializer.decode(message.payload());
705 backgroundExecutor.submit(() -> {
706 try {
707 handleAntiEntropyAdvertisement(advertisement);
708 } catch (Exception e) {
709 log.warn("Exception thrown handling advertisements", e);
710 }
711 });
712 }
713 }
714
Jonathan Hartdb3af892015-01-26 13:19:07 -0800715 private final class InternalPutEventListener implements
716 ClusterMessageHandler {
717 @Override
718 public void handle(ClusterMessage message) {
719 log.debug("Received put event from peer: {}", message.sender());
720 InternalPutEvent<K, V> event = serializer.decode(message.payload());
721
722 executor.submit(() -> {
723 try {
724 for (PutEntry<K, V> entry : event.entries()) {
725 K key = entry.key();
726 V value = entry.value();
727 Timestamp timestamp = entry.timestamp();
728
729 if (putInternal(key, value, timestamp)) {
Jonathan Hartaaa56572015-01-28 21:56:35 -0800730 EventuallyConsistentMapEvent<K, V> externalEvent =
Jonathan Hartdb3af892015-01-26 13:19:07 -0800731 new EventuallyConsistentMapEvent<>(
732 EventuallyConsistentMapEvent.Type.PUT, key,
733 value);
734 notifyListeners(externalEvent);
735 }
736 }
737 } catch (Exception e) {
738 log.warn("Exception thrown handling put", e);
739 }
740 });
741 }
742 }
743
744 private final class InternalRemoveEventListener implements
745 ClusterMessageHandler {
746 @Override
747 public void handle(ClusterMessage message) {
748 log.debug("Received remove event from peer: {}", message.sender());
749 InternalRemoveEvent<K> event = serializer.decode(message.payload());
750
751 executor.submit(() -> {
752 try {
753 for (RemoveEntry<K> entry : event.entries()) {
754 K key = entry.key();
755 Timestamp timestamp = entry.timestamp();
756
757 if (removeInternal(key, timestamp)) {
Jonathan Hartaaa56572015-01-28 21:56:35 -0800758 EventuallyConsistentMapEvent<K, V> externalEvent
759 = new EventuallyConsistentMapEvent<>(
Jonathan Hartdb3af892015-01-26 13:19:07 -0800760 EventuallyConsistentMapEvent.Type.REMOVE,
761 key, null);
762 notifyListeners(externalEvent);
763 }
764 }
765 } catch (Exception e) {
766 log.warn("Exception thrown handling remove", e);
767 }
768 });
769 }
770 }
771
Jonathan Hartdb3af892015-01-26 13:19:07 -0800772}