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