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