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