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