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) { |
| 333 | MapValue<V> tombstone = MapValue.tombstone(timestampProvider.apply(key, value)); |
| 334 | MapValue<V> previousValue = removeInternal(key, Optional.ofNullable(value), tombstone); |
Madan Jampani | a0ac487 | 2015-07-02 11:23:49 -0700 | [diff] [blame] | 335 | if (previousValue != null) { |
| 336 | notifyPeers(new UpdateEntry<>(key, tombstone), peerUpdateFunction.apply(key, previousValue.get())); |
| 337 | if (previousValue.isAlive()) { |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 338 | notifyListeners(new EventuallyConsistentMapEvent<>(mapName, REMOVE, key, previousValue.get())); |
Madan Jampani | a0ac487 | 2015-07-02 11:23:49 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 341 | return previousValue != null ? previousValue.get() : null; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 344 | private MapValue<V> removeInternal(K key, Optional<V> value, MapValue<V> tombstone) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 345 | checkState(!destroyed, destroyedMessage); |
| 346 | checkNotNull(key, ERROR_NULL_KEY); |
| 347 | checkNotNull(value, ERROR_NULL_VALUE); |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 348 | checkState(tombstone.isTombstone()); |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 349 | |
| 350 | counter.incrementCount(); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 351 | AtomicBoolean updated = new AtomicBoolean(false); |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 352 | AtomicReference<MapValue<V>> previousValue = new AtomicReference<>(); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 353 | items.compute(key, (k, existing) -> { |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 354 | boolean valueMatches = true; |
| 355 | if (value.isPresent() && existing != null && existing.isAlive()) { |
| 356 | valueMatches = Objects.equals(value.get(), existing.get()); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 357 | } |
Brian O'Connor | 6325dad | 2015-07-07 15:36:29 -0700 | [diff] [blame] | 358 | if (existing == null) { |
| 359 | log.debug("ECMap Remove: Existing value for key {} is already null", k); |
| 360 | } |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 361 | updated.set(valueMatches && (existing == null || tombstone.isNewerThan(existing))); |
| 362 | if (updated.get()) { |
| 363 | previousValue.set(existing); |
| 364 | } |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 365 | if (updated.get()) { |
| 366 | return tombstonesDisabled ? null : tombstone; |
| 367 | } else { |
| 368 | return existing; |
| 369 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 370 | }); |
| 371 | if (updated.get()) { |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 372 | if (persistent) { |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 373 | if (tombstonesDisabled) { |
| 374 | persistentStore.remove(key); |
| 375 | } else { |
| 376 | persistentStore.update(key, tombstone); |
| 377 | } |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 378 | } |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 379 | } |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 380 | return previousValue.get(); |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | @Override |
Madan Jampani | 4727a11 | 2015-07-16 12:12:58 -0700 | [diff] [blame] | 384 | public V compute(K key, BiFunction<K, V, V> recomputeFunction) { |
| 385 | checkState(!destroyed, destroyedMessage); |
| 386 | checkNotNull(key, ERROR_NULL_KEY); |
| 387 | checkNotNull(recomputeFunction, "Recompute function cannot be null"); |
| 388 | |
| 389 | AtomicBoolean updated = new AtomicBoolean(false); |
| 390 | AtomicReference<MapValue<V>> previousValue = new AtomicReference<>(); |
| 391 | MapValue<V> computedValue = items.compute(key, (k, mv) -> { |
| 392 | previousValue.set(mv); |
| 393 | V newRawValue = recomputeFunction.apply(key, mv == null ? null : mv.get()); |
| 394 | MapValue<V> newValue = new MapValue<>(newRawValue, timestampProvider.apply(key, newRawValue)); |
| 395 | if (mv == null || newValue.isNewerThan(mv)) { |
| 396 | updated.set(true); |
| 397 | return newValue; |
| 398 | } else { |
| 399 | return mv; |
| 400 | } |
| 401 | }); |
| 402 | if (updated.get()) { |
| 403 | notifyPeers(new UpdateEntry<>(key, computedValue), peerUpdateFunction.apply(key, computedValue.get())); |
| 404 | EventuallyConsistentMapEvent.Type updateType = computedValue.isTombstone() ? REMOVE : PUT; |
| 405 | V value = computedValue.isTombstone() |
| 406 | ? previousValue.get() == null ? null : previousValue.get().get() |
| 407 | : computedValue.get(); |
| 408 | if (value != null) { |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 409 | notifyListeners(new EventuallyConsistentMapEvent<>(mapName, updateType, key, value)); |
Madan Jampani | 4727a11 | 2015-07-16 12:12:58 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | return computedValue.get(); |
| 413 | } |
| 414 | |
| 415 | @Override |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 416 | public void putAll(Map<? extends K, ? extends V> m) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 417 | checkState(!destroyed, destroyedMessage); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 418 | m.forEach(this::put); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public void clear() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 423 | checkState(!destroyed, destroyedMessage); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 424 | Maps.filterValues(items, MapValue::isAlive) |
| 425 | .forEach((k, v) -> remove(k)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | @Override |
| 429 | public Set<K> keySet() { |
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 | return Maps.filterValues(items, MapValue::isAlive) |
| 432 | .keySet(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public Collection<V> values() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 437 | checkState(!destroyed, destroyedMessage); |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 438 | return Collections2.transform(Maps.filterValues(items, MapValue::isAlive).values(), MapValue::get); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | @Override |
| 442 | public Set<Map.Entry<K, V>> entrySet() { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 443 | checkState(!destroyed, destroyedMessage); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 444 | return Maps.filterValues(items, MapValue::isAlive) |
| 445 | .entrySet() |
| 446 | .stream() |
| 447 | .map(e -> Pair.of(e.getKey(), e.getValue().get())) |
| 448 | .collect(Collectors.toSet()); |
| 449 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 450 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 451 | /** |
| 452 | * Returns true if newValue was accepted i.e. map is updated. |
| 453 | * @param key key |
| 454 | * @param newValue proposed new value |
| 455 | * @return true if update happened; false if map already contains a more recent value for the key |
| 456 | */ |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 457 | private boolean putInternal(K key, MapValue<V> newValue) { |
| 458 | checkState(!destroyed, destroyedMessage); |
| 459 | checkNotNull(key, ERROR_NULL_KEY); |
| 460 | checkNotNull(newValue, ERROR_NULL_VALUE); |
| 461 | checkState(newValue.isAlive()); |
| 462 | counter.incrementCount(); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 463 | AtomicBoolean updated = new AtomicBoolean(false); |
| 464 | items.compute(key, (k, existing) -> { |
| 465 | if (existing == null || newValue.isNewerThan(existing)) { |
| 466 | updated.set(true); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 467 | return newValue; |
| 468 | } |
| 469 | return existing; |
| 470 | }); |
| 471 | if (updated.get() && persistent) { |
| 472 | persistentStore.update(key, newValue); |
| 473 | } |
| 474 | return updated.get(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | @Override |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 478 | public void addListener(EventuallyConsistentMapListener<K, V> listener) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 479 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 480 | |
| 481 | listeners.add(checkNotNull(listener)); |
| 482 | } |
| 483 | |
| 484 | @Override |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 485 | public void removeListener(EventuallyConsistentMapListener<K, V> listener) { |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 486 | checkState(!destroyed, destroyedMessage); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 487 | |
| 488 | listeners.remove(checkNotNull(listener)); |
| 489 | } |
| 490 | |
| 491 | @Override |
| 492 | public void destroy() { |
| 493 | destroyed = true; |
| 494 | |
| 495 | executor.shutdown(); |
| 496 | backgroundExecutor.shutdown(); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 497 | communicationExecutor.shutdown(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 498 | |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 499 | listeners.clear(); |
| 500 | |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 501 | clusterCommunicator.removeSubscriber(updateMessageSubject); |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 502 | clusterCommunicator.removeSubscriber(antiEntropyAdvertisementSubject); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 503 | } |
| 504 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 505 | private void notifyListeners(EventuallyConsistentMapEvent<K, V> event) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 506 | listeners.forEach(listener -> listener.event(event)); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 509 | private void notifyPeers(UpdateEntry<K, V> event, Collection<NodeId> peers) { |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 510 | queueUpdate(event, peers); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 513 | private void queueUpdate(UpdateEntry<K, V> event, Collection<NodeId> peers) { |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 514 | if (peers == null) { |
| 515 | // we have no friends :( |
| 516 | return; |
| 517 | } |
| 518 | peers.forEach(node -> |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 519 | senderPending.computeIfAbsent(node, unusedKey -> new EventAccumulator(node)).add(event) |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 520 | ); |
| 521 | } |
| 522 | |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 523 | private boolean underHighLoad() { |
| 524 | return counter.get(LOAD_WINDOW) > HIGH_LOAD_THRESHOLD; |
| 525 | } |
| 526 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 527 | private void sendAdvertisement() { |
| 528 | try { |
Thomas Vachuska | 152f9fd | 2015-04-02 16:28:13 -0700 | [diff] [blame] | 529 | if (underHighLoad() || destroyed) { |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 530 | return; |
| 531 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 532 | pickRandomActivePeer().ifPresent(this::sendAdvertisementToPeer); |
| 533 | } catch (Exception e) { |
| 534 | // Catch all exceptions to avoid scheduled task being suppressed. |
| 535 | log.error("Exception thrown while sending advertisement", e); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 539 | private Optional<NodeId> pickRandomActivePeer() { |
| 540 | List<NodeId> activePeers = clusterService.getNodes() |
| 541 | .stream() |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 542 | .map(ControllerNode::id) |
| 543 | .filter(id -> !localNodeId.equals(id)) |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 544 | .filter(id -> clusterService.getState(id) == ControllerNode.State.ACTIVE) |
| 545 | .collect(Collectors.toList()); |
| 546 | Collections.shuffle(activePeers); |
| 547 | return activePeers.isEmpty() ? Optional.empty() : Optional.of(activePeers.get(0)); |
| 548 | } |
| 549 | |
| 550 | private void sendAdvertisementToPeer(NodeId peer) { |
| 551 | clusterCommunicator.unicast(createAdvertisement(), |
| 552 | antiEntropyAdvertisementSubject, |
| 553 | serializer::encode, |
| 554 | peer) |
| 555 | .whenComplete((result, error) -> { |
| 556 | if (error != null) { |
Madan Jampani | a0ac487 | 2015-07-02 11:23:49 -0700 | [diff] [blame] | 557 | log.debug("Failed to send anti-entropy advertisement to {}", peer, error); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 558 | } |
| 559 | }); |
| 560 | } |
| 561 | |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 562 | private AntiEntropyAdvertisement<K> createAdvertisement() { |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 563 | return new AntiEntropyAdvertisement<K>(localNodeId, |
| 564 | ImmutableMap.copyOf(Maps.transformValues(items, MapValue::digest))); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | private void handleAntiEntropyAdvertisement(AntiEntropyAdvertisement<K> ad) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 568 | if (destroyed || underHighLoad()) { |
| 569 | return; |
| 570 | } |
| 571 | try { |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 572 | log.debug("Received anti-entropy advertisement from {} for {} with {} entries in it", |
| 573 | mapName, ad.sender(), ad.digest().size()); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 574 | antiEntropyCheckLocalItems(ad).forEach(this::notifyListeners); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 575 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 576 | if (!lightweightAntiEntropy) { |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 577 | // if remote ad has any entries that the local copy is missing, actively sync |
| 578 | // TODO: Missing keys is not the way local copy can be behind. |
| 579 | if (Sets.difference(ad.digest().keySet(), items.keySet()).size() > 0) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 580 | // TODO: Send ad for missing keys and for entries that are stale |
| 581 | sendAdvertisementToPeer(ad.sender()); |
Jonathan Hart | f893be8 | 2015-02-24 17:35:51 -0800 | [diff] [blame] | 582 | } |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 583 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 584 | } catch (Exception e) { |
| 585 | log.warn("Error handling anti-entropy advertisement", e); |
HIGUCHI Yuta | 00c3f57 | 2015-02-25 07:33:50 -0800 | [diff] [blame] | 586 | } |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /** |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 590 | * Processes anti-entropy ad from peer by taking following actions: |
| 591 | * 1. If peer has an old entry, updates peer. |
| 592 | * 2. If peer indicates an entry is removed and has a more recent |
| 593 | * timestamp than the local entry, update local state. |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 594 | */ |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 595 | private List<EventuallyConsistentMapEvent<K, V>> antiEntropyCheckLocalItems( |
| 596 | AntiEntropyAdvertisement<K> ad) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 597 | final List<EventuallyConsistentMapEvent<K, V>> externalEvents = Lists.newLinkedList(); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 598 | final NodeId sender = ad.sender(); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 599 | items.forEach((key, localValue) -> { |
| 600 | MapValue.Digest remoteValueDigest = ad.digest().get(key); |
| 601 | if (remoteValueDigest == null || localValue.isNewerThan(remoteValueDigest.timestamp())) { |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 602 | // local value is more recent, push to sender |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 603 | queueUpdate(new UpdateEntry<>(key, localValue), ImmutableList.of(sender)); |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 604 | } |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 605 | if (remoteValueDigest != null |
| 606 | && remoteValueDigest.isNewerThan(localValue.digest()) |
| 607 | && remoteValueDigest.isTombstone()) { |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 608 | MapValue<V> previousValue = removeInternal(key, |
| 609 | Optional.empty(), |
Madan Jampani | 43f3795 | 2015-07-02 12:54:08 -0700 | [diff] [blame] | 610 | MapValue.tombstone(remoteValueDigest.timestamp())); |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 611 | if (previousValue != null && previousValue.isAlive()) { |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 612 | externalEvents.add(new EventuallyConsistentMapEvent<>(mapName, REMOVE, key, previousValue.get())); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 613 | } |
| 614 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 615 | }); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 616 | return externalEvents; |
| 617 | } |
| 618 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 619 | private void processUpdates(Collection<UpdateEntry<K, V>> updates) { |
| 620 | if (destroyed) { |
| 621 | return; |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 622 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 623 | updates.forEach(update -> { |
| 624 | final K key = update.key(); |
| 625 | final MapValue<V> value = update.value(); |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 626 | if (value.isTombstone()) { |
| 627 | MapValue<V> previousValue = removeInternal(key, Optional.empty(), value); |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 628 | if (previousValue != null && previousValue.isAlive()) { |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 629 | notifyListeners(new EventuallyConsistentMapEvent<>(mapName, REMOVE, key, previousValue.get())); |
Madan Jampani | d13f3b8 | 2015-07-01 17:37:50 -0700 | [diff] [blame] | 630 | } |
Madan Jampani | 4f1f4cd | 2015-07-08 23:05:35 -0700 | [diff] [blame] | 631 | } else if (putInternal(key, value)) { |
Madan Jampani | cab114c | 2015-07-23 00:14:19 -0700 | [diff] [blame] | 632 | notifyListeners(new EventuallyConsistentMapEvent<>(mapName, PUT, key, value.get())); |
Jonathan Hart | aaa5657 | 2015-01-28 21:56:35 -0800 | [diff] [blame] | 633 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 634 | }); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 637 | // TODO pull this into the class if this gets pulled out... |
| 638 | private static final int DEFAULT_MAX_EVENTS = 1000; |
| 639 | private static final int DEFAULT_MAX_IDLE_MS = 10; |
| 640 | private static final int DEFAULT_MAX_BATCH_MS = 50; |
| 641 | private static final Timer TIMER = new Timer("onos-ecm-sender-events"); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 642 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 643 | private final class EventAccumulator extends AbstractAccumulator<UpdateEntry<K, V>> { |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 644 | |
| 645 | private final NodeId peer; |
| 646 | |
| 647 | private EventAccumulator(NodeId peer) { |
| 648 | super(TIMER, DEFAULT_MAX_EVENTS, DEFAULT_MAX_BATCH_MS, DEFAULT_MAX_IDLE_MS); |
| 649 | this.peer = peer; |
| 650 | } |
| 651 | |
| 652 | @Override |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 653 | public void processItems(List<UpdateEntry<K, V>> items) { |
| 654 | Map<K, UpdateEntry<K, V>> map = Maps.newHashMap(); |
| 655 | items.forEach(item -> map.compute(item.key(), (key, existing) -> |
Madan Jampani | 92c64eb | 2015-07-23 15:37:07 -0700 | [diff] [blame] | 656 | item.isNewerThan(existing) ? item : existing)); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 657 | communicationExecutor.submit(() -> { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 658 | clusterCommunicator.unicast(ImmutableList.copyOf(map.values()), |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 659 | updateMessageSubject, |
| 660 | serializer::encode, |
| 661 | peer) |
| 662 | .whenComplete((result, error) -> { |
| 663 | if (error != null) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 664 | log.debug("Failed to send to {}", peer, error); |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 665 | } |
| 666 | }); |
Brian O'Connor | eeaea2c | 2015-03-05 16:24:34 -0800 | [diff] [blame] | 667 | }); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 668 | } |
| 669 | } |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 670 | } |