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 | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 21 | import com.google.common.collect.Sets; |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 22 | |
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; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 32 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 33 | import org.onosproject.store.impl.LogicalTimestamp; |
Jonathan Hart | 63939a3 | 2015-05-08 11:57:03 -0700 | [diff] [blame] | 34 | import org.onosproject.store.service.WallClockTimestamp; |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 35 | import org.onosproject.store.serializers.KryoNamespaces; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 36 | import org.onosproject.store.serializers.KryoSerializer; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 37 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 38 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 39 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 43 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT; |
| 44 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE; |
| 45 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 46 | import java.util.Collection; |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 47 | import java.util.Collections; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 48 | import java.util.List; |
| 49 | import java.util.Map; |
Madan Jampani | 43e9c9c | 2015-06-26 14:16:46 -0700 | [diff] [blame] | 50 | import java.util.Optional; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 51 | import java.util.Set; |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 52 | import java.util.Timer; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 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; |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 57 | import java.util.concurrent.atomic.AtomicBoolean; |
Madan Jampani | 43e9c9c | 2015-06-26 14:16:46 -0700 | [diff] [blame] | 58 | import java.util.concurrent.atomic.AtomicReference; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 59 | import java.util.function.BiFunction; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 60 | import java.util.stream.Collectors; |
| 61 | |
| 62 | import static com.google.common.base.Preconditions.checkNotNull; |
| 63 | import static com.google.common.base.Preconditions.checkState; |
| 64 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Brian O'Connor | c6713a8 | 2015-02-24 11:55:48 -0800 | [diff] [blame] | 65 | import static org.onlab.util.BoundedThreadPool.newFixedThreadPool; |
Brian O'Connor | 4b2ba5f | 2015-02-18 20:54:00 -0800 | [diff] [blame] | 66 | import static org.onlab.util.Tools.groupedThreads; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Distributed Map implementation which uses optimistic replication and gossip |
| 70 | * based techniques to provide an eventually consistent data store. |
| 71 | */ |
| 72 | public class EventuallyConsistentMapImpl<K, V> |
| 73 | implements EventuallyConsistentMap<K, V> { |
| 74 | |
| 75 | private static final Logger log = LoggerFactory.getLogger(EventuallyConsistentMapImpl.class); |
| 76 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 77 | private final Map<K, MapValue<V>> items; |
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; |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 82 | private final NodeId localNodeId; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 83 | |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 84 | private final BiFunction<K, V, Timestamp> timestampProvider; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 85 | |
| 86 | private final MessageSubject updateMessageSubject; |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 87 | private final MessageSubject antiEntropyAdvertisementSubject; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 88 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 89 | private final Set<EventuallyConsistentMapListener<K, V>> listeners |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 90 | = Sets.newCopyOnWriteArraySet(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 91 | |
| 92 | private final ExecutorService executor; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 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 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 96 | private final ExecutorService communicationExecutor; |
| 97 | private final 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 | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 106 | private final long initialDelaySec = 5; |
| 107 | private final boolean lightweightAntiEntropy; |
| 108 | private final boolean tombstonesDisabled; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 109 | |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 110 | private static final int WINDOW_SIZE = 5; |
| 111 | private static final int HIGH_LOAD_THRESHOLD = 0; |
| 112 | private static final int LOAD_WINDOW = 2; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 113 | private SlidingWindowCounter counter = new SlidingWindowCounter(WINDOW_SIZE); |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 114 | |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 115 | private final boolean persistent; |
| 116 | private final PersistentStore<K, V> persistentStore; |
| 117 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 118 | /** |
| 119 | * Creates a new eventually consistent map shared amongst multiple instances. |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 120 | * <p> |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 121 | * See {@link org.onosproject.store.service.EventuallyConsistentMapBuilder} |
| 122 | * for more description of the parameters expected by the map. |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 123 | * </p> |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 124 | * |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 125 | * @param mapName a String identifier for the map. |
| 126 | * @param clusterService the cluster service |
| 127 | * @param clusterCommunicator the cluster communications service |
| 128 | * @param serializerBuilder a Kryo namespace builder that can serialize |
| 129 | * both K and V |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 130 | * @param timestampProvider provider of timestamps for K and V |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 131 | * @param peerUpdateFunction function that provides a set of nodes to immediately |
| 132 | * update to when there writes to the map |
| 133 | * @param eventExecutor executor to use for processing incoming |
| 134 | * events from peers |
| 135 | * @param communicationExecutor executor to use for sending events to peers |
| 136 | * @param backgroundExecutor executor to use for background anti-entropy |
| 137 | * tasks |
| 138 | * @param tombstonesDisabled true if this map should not maintain |
| 139 | * tombstones |
| 140 | * @param antiEntropyPeriod period that the anti-entropy task should run |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 141 | * @param antiEntropyTimeUnit time unit for anti-entropy period |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 142 | * @param convergeFaster make anti-entropy try to converge faster |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 143 | * @param persistent persist data to disk |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 144 | */ |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 145 | EventuallyConsistentMapImpl(String mapName, |
| 146 | ClusterService clusterService, |
| 147 | ClusterCommunicationService clusterCommunicator, |
| 148 | KryoNamespace.Builder serializerBuilder, |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 149 | BiFunction<K, V, Timestamp> timestampProvider, |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 150 | BiFunction<K, V, Collection<NodeId>> peerUpdateFunction, |
| 151 | ExecutorService eventExecutor, |
| 152 | ExecutorService communicationExecutor, |
| 153 | ScheduledExecutorService backgroundExecutor, |
| 154 | boolean tombstonesDisabled, |
| 155 | long antiEntropyPeriod, |
| 156 | TimeUnit antiEntropyTimeUnit, |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 157 | boolean convergeFaster, |
| 158 | boolean persistent) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 159 | items = Maps.newConcurrentMap(); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 160 | senderPending = Maps.newConcurrentMap(); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 161 | destroyedMessage = mapName + ERROR_DESTROYED; |
Madan Jampani | 2872628 | 2015-02-19 11:40:23 -0800 | [diff] [blame] | 162 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 163 | this.clusterService = clusterService; |
| 164 | this.clusterCommunicator = clusterCommunicator; |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 165 | this.localNodeId = clusterService.getLocalNode().id(); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 166 | |
| 167 | this.serializer = createSerializer(serializerBuilder); |
| 168 | |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 169 | this.timestampProvider = timestampProvider; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 170 | |
| 171 | if (peerUpdateFunction != null) { |
| 172 | this.peerUpdateFunction = peerUpdateFunction; |
| 173 | } else { |
| 174 | this.peerUpdateFunction = (key, value) -> clusterService.getNodes().stream() |
| 175 | .map(ControllerNode::id) |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 176 | .filter(nodeId -> !nodeId.equals(localNodeId)) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 177 | .collect(Collectors.toList()); |
| 178 | } |
| 179 | |
| 180 | if (eventExecutor != null) { |
| 181 | this.executor = eventExecutor; |
| 182 | } else { |
| 183 | // should be a normal executor; it's used for receiving messages |
| 184 | this.executor = |
| 185 | Executors.newFixedThreadPool(8, groupedThreads("onos/ecm", mapName + "-fg-%d")); |
| 186 | } |
| 187 | |
| 188 | if (communicationExecutor != null) { |
| 189 | this.communicationExecutor = communicationExecutor; |
| 190 | } else { |
| 191 | // sending executor; should be capped |
| 192 | //TODO this probably doesn't need to be bounded anymore |
| 193 | this.communicationExecutor = |
| 194 | newFixedThreadPool(8, groupedThreads("onos/ecm", mapName + "-publish-%d")); |
| 195 | } |
| 196 | |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 197 | this.persistent = persistent; |
| 198 | |
| 199 | if (this.persistent) { |
| 200 | String dataDirectory = System.getProperty("karaf.data", "./data"); |
| 201 | String filename = dataDirectory + "/" + "mapdb-ecm-" + mapName; |
| 202 | |
| 203 | ExecutorService dbExecutor = |
| 204 | newFixedThreadPool(1, groupedThreads("onos/ecm", mapName + "-dbwriter")); |
| 205 | |
| 206 | persistentStore = new MapDbPersistentStore<>(filename, dbExecutor, serializer); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 207 | persistentStore.readInto(items); |
Jonathan Hart | ca335e9 | 2015-03-05 10:34:32 -0800 | [diff] [blame] | 208 | } else { |
| 209 | this.persistentStore = null; |
| 210 | } |
| 211 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 212 | if (backgroundExecutor != null) { |
| 213 | this.backgroundExecutor = backgroundExecutor; |
| 214 | } else { |
| 215 | this.backgroundExecutor = |
| 216 | newSingleThreadScheduledExecutor(groupedThreads("onos/ecm", mapName + "-bg-%d")); |
| 217 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 218 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 219 | // start anti-entropy thread |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 220 | this.backgroundExecutor.scheduleAtFixedRate(this::sendAdvertisement, |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 221 | initialDelaySec, antiEntropyPeriod, |
| 222 | antiEntropyTimeUnit); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 223 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 224 | updateMessageSubject = new MessageSubject("ecm-" + mapName + "-update"); |
| 225 | clusterCommunicator.addSubscriber(updateMessageSubject, |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 226 | serializer::decode, |
| 227 | this::processUpdates, |
| 228 | this.executor); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 229 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 230 | antiEntropyAdvertisementSubject = new MessageSubject("ecm-" + mapName + "-anti-entropy"); |
| 231 | clusterCommunicator.addSubscriber(antiEntropyAdvertisementSubject, |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 232 | serializer::decode, |
| 233 | this::handleAntiEntropyAdvertisement, |
| 234 | this.backgroundExecutor); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 235 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 236 | this.tombstonesDisabled = tombstonesDisabled; |
| 237 | this.lightweightAntiEntropy = !convergeFaster; |
Madan Jampani | e135628 | 2015-03-10 19:05:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 240 | private KryoSerializer createSerializer(KryoNamespace.Builder builder) { |
| 241 | return new KryoSerializer() { |
| 242 | @Override |
| 243 | protected void setupKryoPool() { |
| 244 | // Add the map's internal helper classes to the user-supplied serializer |
| 245 | serializerPool = builder |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 246 | .register(KryoNamespaces.BASIC) |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 247 | .register(LogicalTimestamp.class) |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 248 | .register(WallClockTimestamp.class) |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 249 | .register(AntiEntropyAdvertisement.class) |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 250 | .register(UpdateEntry.class) |
| 251 | .register(MapValue.class) |
| 252 | .register(MapValue.Digest.class) |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 253 | .build(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 254 | } |
| 255 | }; |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public int size() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 260 | checkState(!destroyed, destroyedMessage); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 261 | // TODO: Maintain a separate counter for tracking live elements in map. |
| 262 | return Maps.filterValues(items, MapValue::isAlive).size(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public boolean isEmpty() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 267 | checkState(!destroyed, destroyedMessage); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 268 | return size() == 0; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public boolean containsKey(K key) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 273 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 274 | checkNotNull(key, ERROR_NULL_KEY); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 275 | return get(key) != null; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public boolean containsValue(V value) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 280 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 281 | checkNotNull(value, ERROR_NULL_VALUE); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 282 | return items.values() |
| 283 | .stream() |
| 284 | .filter(MapValue::isAlive) |
| 285 | .anyMatch(v -> v.get().equals(value)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public V get(K key) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 290 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 291 | checkNotNull(key, ERROR_NULL_KEY); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 292 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 293 | MapValue<V> value = items.get(key); |
| 294 | return (value == null || value.isTombstone()) ? null : value.get(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public void put(K key, V value) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 299 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 300 | checkNotNull(key, ERROR_NULL_KEY); |
| 301 | checkNotNull(value, ERROR_NULL_VALUE); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 302 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 303 | MapValue<V> newValue = new MapValue<>(value, timestampProvider.apply(key, value)); |
| 304 | if (updateInternal(key, newValue)) { |
| 305 | notifyPeers(new UpdateEntry<>(key, newValue), peerUpdateFunction.apply(key, value)); |
| 306 | notifyListeners(new EventuallyConsistentMapEvent<>(PUT, key, value)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 310 | @Override |
Madan Jampani | 43e9c9c | 2015-06-26 14:16:46 -0700 | [diff] [blame] | 311 | public V remove(K key) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 312 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 313 | checkNotNull(key, ERROR_NULL_KEY); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 314 | return removeInternal(key, Optional.empty()); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | @Override |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 318 | public void remove(K key, V value) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 319 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 320 | checkNotNull(key, ERROR_NULL_KEY); |
| 321 | checkNotNull(value, ERROR_NULL_VALUE); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 322 | removeInternal(key, Optional.of(value)); |
| 323 | } |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 324 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 325 | private V removeInternal(K key, Optional<V> value) { |
| 326 | checkState(!destroyed, destroyedMessage); |
| 327 | checkNotNull(key, ERROR_NULL_KEY); |
| 328 | checkNotNull(value, ERROR_NULL_VALUE); |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 329 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 330 | MapValue<V> newValue = new MapValue<>(null, timestampProvider.apply(key, value.orElse(null))); |
| 331 | AtomicBoolean updated = new AtomicBoolean(false); |
| 332 | AtomicReference<V> previousValue = new AtomicReference<>(); |
| 333 | items.compute(key, (k, existing) -> { |
| 334 | if (existing != null && existing.isAlive()) { |
| 335 | updated.set(!value.isPresent() || value.get().equals(existing.get())); |
| 336 | previousValue.set(existing.get()); |
| 337 | } |
| 338 | updated.set(existing == null || newValue.isNewerThan(existing)); |
| 339 | return updated.get() ? newValue : existing; |
| 340 | }); |
| 341 | if (updated.get()) { |
| 342 | notifyPeers(new UpdateEntry<>(key, newValue), peerUpdateFunction.apply(key, previousValue.get())); |
| 343 | notifyListeners(new EventuallyConsistentMapEvent<>(REMOVE, key, previousValue.get())); |
| 344 | if (persistent) { |
| 345 | persistentStore.update(key, newValue); |
| 346 | } |
| 347 | return previousValue.get(); |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 348 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 349 | return null; |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | @Override |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 353 | public void putAll(Map<? extends K, ? extends V> m) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 354 | checkState(!destroyed, destroyedMessage); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 355 | m.forEach(this::put); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public void clear() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 360 | checkState(!destroyed, destroyedMessage); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 361 | Maps.filterValues(items, MapValue::isAlive) |
| 362 | .forEach((k, v) -> remove(k)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public Set<K> keySet() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 367 | checkState(!destroyed, destroyedMessage); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 368 | return Maps.filterValues(items, MapValue::isAlive) |
| 369 | .keySet(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public Collection<V> values() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 374 | checkState(!destroyed, destroyedMessage); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 375 | return Maps.filterValues(items, MapValue::isAlive) |
| 376 | .values() |
| 377 | .stream() |
| 378 | .map(MapValue::get) |
| 379 | .collect(Collectors.toList()); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | @Override |
| 383 | public Set<Map.Entry<K, V>> entrySet() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 384 | checkState(!destroyed, destroyedMessage); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 385 | return Maps.filterValues(items, MapValue::isAlive) |
| 386 | .entrySet() |
| 387 | .stream() |
| 388 | .map(e -> Pair.of(e.getKey(), e.getValue().get())) |
| 389 | .collect(Collectors.toSet()); |
| 390 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 391 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 392 | /** |
| 393 | * Returns true if newValue was accepted i.e. map is updated. |
| 394 | * @param key key |
| 395 | * @param newValue proposed new value |
| 396 | * @return true if update happened; false if map already contains a more recent value for the key |
| 397 | */ |
| 398 | private boolean updateInternal(K key, MapValue<V> newValue) { |
| 399 | AtomicBoolean updated = new AtomicBoolean(false); |
| 400 | items.compute(key, (k, existing) -> { |
| 401 | if (existing == null || newValue.isNewerThan(existing)) { |
| 402 | updated.set(true); |
| 403 | if (newValue.isTombstone()) { |
| 404 | return tombstonesDisabled ? null : newValue; |
| 405 | } |
| 406 | return newValue; |
| 407 | } |
| 408 | return existing; |
| 409 | }); |
| 410 | if (updated.get() && persistent) { |
| 411 | persistentStore.update(key, newValue); |
| 412 | } |
| 413 | return updated.get(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | @Override |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 417 | public void addListener(EventuallyConsistentMapListener<K, V> listener) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 418 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 419 | |
| 420 | listeners.add(checkNotNull(listener)); |
| 421 | } |
| 422 | |
| 423 | @Override |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 424 | public void removeListener(EventuallyConsistentMapListener<K, V> listener) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 425 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 426 | |
| 427 | listeners.remove(checkNotNull(listener)); |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public void destroy() { |
| 432 | destroyed = true; |
| 433 | |
| 434 | executor.shutdown(); |
| 435 | backgroundExecutor.shutdown(); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 436 | communicationExecutor.shutdown(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 437 | |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 438 | listeners.clear(); |
| 439 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 440 | clusterCommunicator.removeSubscriber(updateMessageSubject); |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 441 | clusterCommunicator.removeSubscriber(antiEntropyAdvertisementSubject); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 444 | private void notifyListeners(EventuallyConsistentMapEvent<K, V> event) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 445 | listeners.forEach(listener -> listener.event(event)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 448 | private void notifyPeers(UpdateEntry<K, V> event, Collection<NodeId> peers) { |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 449 | queueUpdate(event, peers); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 452 | private void queueUpdate(UpdateEntry<K, V> event, Collection<NodeId> peers) { |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 453 | if (peers == null) { |
| 454 | // we have no friends :( |
| 455 | return; |
| 456 | } |
| 457 | peers.forEach(node -> |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 458 | senderPending.computeIfAbsent(node, unusedKey -> new EventAccumulator(node)).add(event) |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 459 | ); |
| 460 | } |
| 461 | |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 462 | private boolean underHighLoad() { |
| 463 | return counter.get(LOAD_WINDOW) > HIGH_LOAD_THRESHOLD; |
| 464 | } |
| 465 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 466 | private void sendAdvertisement() { |
| 467 | try { |
Thomas Vachuska | 152f9fd | 2015-04-02 16:28:13 -0700 | [diff] [blame] | 468 | if (underHighLoad() || destroyed) { |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 469 | return; |
| 470 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 471 | pickRandomActivePeer().ifPresent(this::sendAdvertisementToPeer); |
| 472 | } catch (Exception e) { |
| 473 | // Catch all exceptions to avoid scheduled task being suppressed. |
| 474 | log.error("Exception thrown while sending advertisement", e); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 478 | private Optional<NodeId> pickRandomActivePeer() { |
| 479 | List<NodeId> activePeers = clusterService.getNodes() |
| 480 | .stream() |
| 481 | .filter(node -> !localNodeId.equals(node)) |
| 482 | .map(ControllerNode::id) |
| 483 | .filter(id -> clusterService.getState(id) == ControllerNode.State.ACTIVE) |
| 484 | .collect(Collectors.toList()); |
| 485 | Collections.shuffle(activePeers); |
| 486 | return activePeers.isEmpty() ? Optional.empty() : Optional.of(activePeers.get(0)); |
| 487 | } |
| 488 | |
| 489 | private void sendAdvertisementToPeer(NodeId peer) { |
| 490 | clusterCommunicator.unicast(createAdvertisement(), |
| 491 | antiEntropyAdvertisementSubject, |
| 492 | serializer::encode, |
| 493 | peer) |
| 494 | .whenComplete((result, error) -> { |
| 495 | if (error != null) { |
| 496 | log.warn("Failed to send anti-entropy advertisement to {}", peer); |
| 497 | } |
| 498 | }); |
| 499 | } |
| 500 | |
| 501 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 502 | private AntiEntropyAdvertisement<K> createAdvertisement() { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 503 | return new AntiEntropyAdvertisement<K>(localNodeId, Maps.transformValues(items, MapValue::digest)); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | private void handleAntiEntropyAdvertisement(AntiEntropyAdvertisement<K> ad) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 507 | if (destroyed || underHighLoad()) { |
| 508 | return; |
| 509 | } |
| 510 | try { |
| 511 | antiEntropyCheckLocalItems(ad).forEach(this::notifyListeners); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 512 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 513 | if (!lightweightAntiEntropy) { |
| 514 | Set<K> missingKeys = Sets.difference(items.keySet(), ad.digest().keySet()); |
| 515 | // if remote ad has something unknown, actively sync |
| 516 | if (missingKeys.size() > 0) { |
Jonathan Hart | f893be8 | 2015-02-24 17:35:51 -0800 | [diff] [blame] | 517 | // Send the advertisement back if this peer is out-of-sync |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 518 | // TODO: Send ad for missing keys and for entries that are stale |
| 519 | sendAdvertisementToPeer(ad.sender()); |
Jonathan Hart | f893be8 | 2015-02-24 17:35:51 -0800 | [diff] [blame] | 520 | } |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 521 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 522 | } catch (Exception e) { |
| 523 | log.warn("Error handling anti-entropy advertisement", e); |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 524 | } |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /** |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 528 | * Processes anti-entropy ad from peer by taking following actions: |
| 529 | * 1. If peer has an old entry, updates peer. |
| 530 | * 2. If peer indicates an entry is removed and has a more recent |
| 531 | * timestamp than the local entry, update local state. |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 532 | */ |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 533 | private List<EventuallyConsistentMapEvent<K, V>> antiEntropyCheckLocalItems( |
| 534 | AntiEntropyAdvertisement<K> ad) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 535 | final List<EventuallyConsistentMapEvent<K, V>> externalEvents = Lists.newLinkedList(); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 536 | final NodeId sender = ad.sender(); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 537 | items.forEach((key, localValue) -> { |
| 538 | MapValue.Digest remoteValueDigest = ad.digest().get(key); |
| 539 | if (remoteValueDigest == null || localValue.isNewerThan(remoteValueDigest.timestamp())) { |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 540 | // local value is more recent, push to sender |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 541 | queueUpdate(new UpdateEntry<>(key, localValue), ImmutableList.of(sender)); |
| 542 | } else { |
| 543 | if (remoteValueDigest.isTombstone() |
| 544 | && remoteValueDigest.timestamp().isNewerThan(localValue.timestamp())) { |
| 545 | if (updateInternal(key, new MapValue<>(null, remoteValueDigest.timestamp()))) { |
| 546 | externalEvents.add(new EventuallyConsistentMapEvent<>(REMOVE, key, null)); |
| 547 | } |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 548 | } |
| 549 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 550 | }); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 551 | return externalEvents; |
| 552 | } |
| 553 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 554 | private void processUpdates(Collection<UpdateEntry<K, V>> updates) { |
| 555 | if (destroyed) { |
| 556 | return; |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 557 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 558 | updates.forEach(update -> { |
| 559 | final K key = update.key(); |
| 560 | final MapValue<V> value = update.value(); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 561 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 562 | if (updateInternal(key, value)) { |
| 563 | final EventuallyConsistentMapEvent.Type type = value.isTombstone() ? REMOVE : PUT; |
| 564 | notifyListeners(new EventuallyConsistentMapEvent<>(type, key, value.get())); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 565 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 566 | }); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 569 | // TODO pull this into the class if this gets pulled out... |
| 570 | private static final int DEFAULT_MAX_EVENTS = 1000; |
| 571 | private static final int DEFAULT_MAX_IDLE_MS = 10; |
| 572 | private static final int DEFAULT_MAX_BATCH_MS = 50; |
| 573 | private static final Timer TIMER = new Timer("onos-ecm-sender-events"); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 574 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 575 | private final class EventAccumulator extends AbstractAccumulator<UpdateEntry<K, V>> { |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 576 | |
| 577 | private final NodeId peer; |
| 578 | |
| 579 | private EventAccumulator(NodeId peer) { |
| 580 | super(TIMER, DEFAULT_MAX_EVENTS, DEFAULT_MAX_BATCH_MS, DEFAULT_MAX_IDLE_MS); |
| 581 | this.peer = peer; |
| 582 | } |
| 583 | |
| 584 | @Override |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 585 | public void processItems(List<UpdateEntry<K, V>> items) { |
| 586 | Map<K, UpdateEntry<K, V>> map = Maps.newHashMap(); |
| 587 | items.forEach(item -> map.compute(item.key(), (key, existing) -> |
| 588 | existing == null || item.compareTo(existing) > 0 ? item : existing)); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 589 | communicationExecutor.submit(() -> { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 590 | clusterCommunicator.unicast(ImmutableList.copyOf(map.values()), |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 591 | updateMessageSubject, |
| 592 | serializer::encode, |
| 593 | peer) |
| 594 | .whenComplete((result, error) -> { |
| 595 | if (error != null) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 596 | log.debug("Failed to send to {}", peer, error); |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 597 | } |
| 598 | }); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 599 | }); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 600 | } |
| 601 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame^] | 602 | } |