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