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