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