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