Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [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 | */ |
| 16 | |
| 17 | package org.onosproject.store.consistent.impl; |
| 18 | |
| 19 | import static com.google.common.base.Preconditions.*; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 20 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 21 | |
| 22 | import java.util.Collection; |
| 23 | import java.util.Map; |
| 24 | import java.util.Map.Entry; |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 25 | import java.util.Objects; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 26 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 27 | import java.util.concurrent.CopyOnWriteArraySet; |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 28 | import java.util.concurrent.TimeUnit; |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 29 | import java.util.concurrent.atomic.AtomicReference; |
| 30 | import java.util.function.BiFunction; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 31 | import java.util.function.Consumer; |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 32 | import java.util.function.Function; |
| 33 | import java.util.function.Predicate; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 34 | import java.util.stream.Collectors; |
| 35 | import java.util.Set; |
| 36 | |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 37 | import com.codahale.metrics.Timer; |
| 38 | import org.onlab.metrics.MetricsComponent; |
| 39 | import org.onlab.metrics.MetricsFeature; |
| 40 | import org.onlab.metrics.MetricsService; |
| 41 | import org.onlab.osgi.DefaultServiceDirectory; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 42 | import org.onlab.util.HexString; |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 43 | import org.onlab.util.SharedExecutors; |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 44 | import org.onlab.util.Tools; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 45 | import org.onosproject.core.ApplicationId; |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 46 | |
| 47 | import static org.onosproject.store.consistent.impl.StateMachineUpdate.Target.MAP; |
| 48 | |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 49 | import org.onosproject.store.service.AsyncConsistentMap; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 50 | import org.onosproject.store.service.ConsistentMapException; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 51 | import org.onosproject.store.service.MapEvent; |
| 52 | import org.onosproject.store.service.MapEventListener; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 53 | import org.onosproject.store.service.Serializer; |
| 54 | import org.onosproject.store.service.Versioned; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 55 | import org.slf4j.Logger; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 56 | |
| 57 | import com.google.common.cache.CacheBuilder; |
| 58 | import com.google.common.cache.CacheLoader; |
| 59 | import com.google.common.cache.LoadingCache; |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 60 | import com.google.common.collect.Maps; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * AsyncConsistentMap implementation that is backed by a Raft consensus |
| 64 | * based database. |
| 65 | * |
| 66 | * @param <K> type of key. |
| 67 | * @param <V> type of value. |
| 68 | */ |
| 69 | public class DefaultAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V> { |
| 70 | |
| 71 | private final String name; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 72 | private final ApplicationId applicationId; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 73 | private final Database database; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 74 | private final Serializer serializer; |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 75 | private final boolean readOnly; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 76 | private final boolean purgeOnUninstall; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 77 | private final Consumer<MapEvent<K, V>> eventPublisher; |
| 78 | |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 79 | private final MetricsService metricsService; |
| 80 | private final MetricsComponent metricsComponent; |
| 81 | private final MetricsFeature metricsFeature; |
| 82 | private final Map<String, Timer> perMapOpTimers = Maps.newConcurrentMap(); |
| 83 | private final Map<String, Timer> perOpTimers = Maps.newConcurrentMap(); |
| 84 | private final Timer cMapTimer; |
| 85 | private final Timer perMapTimer; |
| 86 | private final MetricsFeature wildcard; |
| 87 | |
| 88 | private static final String COMPONENT_NAME = "consistentMap"; |
| 89 | private static final String SIZE = "size"; |
| 90 | private static final String IS_EMPTY = "isEmpty"; |
| 91 | private static final String CONTAINS_KEY = "containsKey"; |
| 92 | private static final String CONTAINS_VALUE = "containsValue"; |
| 93 | private static final String GET = "get"; |
| 94 | private static final String COMPUTE_IF = "computeIf"; |
| 95 | private static final String PUT = "put"; |
| 96 | private static final String PUT_AND_GET = "putAndGet"; |
| 97 | private static final String PUT_IF_ABSENT = "putIfAbsent"; |
| 98 | private static final String REMOVE = "remove"; |
| 99 | private static final String CLEAR = "clear"; |
| 100 | private static final String KEY_SET = "keySet"; |
| 101 | private static final String VALUES = "values"; |
| 102 | private static final String ENTRY_SET = "entrySet"; |
| 103 | private static final String REPLACE = "replace"; |
| 104 | private static final String COMPUTE_IF_ABSENT = "computeIfAbsent"; |
| 105 | |
| 106 | |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 107 | private final Set<MapEventListener<K, V>> listeners = new CopyOnWriteArraySet<>(); |
| 108 | |
| 109 | private final Logger log = getLogger(getClass()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 110 | |
| 111 | private static final String ERROR_NULL_KEY = "Key cannot be null"; |
| 112 | private static final String ERROR_NULL_VALUE = "Null values are not allowed"; |
| 113 | |
| 114 | private final LoadingCache<K, String> keyCache = CacheBuilder.newBuilder() |
| 115 | .softValues() |
| 116 | .build(new CacheLoader<K, String>() { |
| 117 | |
| 118 | @Override |
| 119 | public String load(K key) { |
| 120 | return HexString.toHexString(serializer.encode(key)); |
| 121 | } |
| 122 | }); |
| 123 | |
| 124 | protected K dK(String key) { |
| 125 | return serializer.decode(HexString.fromHexString(key)); |
| 126 | } |
| 127 | |
| 128 | public DefaultAsyncConsistentMap(String name, |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 129 | ApplicationId applicationId, |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 130 | Database database, |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 131 | Serializer serializer, |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 132 | boolean readOnly, |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 133 | boolean purgeOnUninstall, |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 134 | Consumer<MapEvent<K, V>> eventPublisher) { |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 135 | this.name = checkNotNull(name, "map name cannot be null"); |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 136 | this.applicationId = applicationId; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 137 | this.database = checkNotNull(database, "database cannot be null"); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 138 | this.serializer = checkNotNull(serializer, "serializer cannot be null"); |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 139 | this.readOnly = readOnly; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 140 | this.purgeOnUninstall = purgeOnUninstall; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 141 | this.eventPublisher = eventPublisher; |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 142 | this.database.registerConsumer(update -> { |
| 143 | SharedExecutors.getSingleThreadExecutor().execute(() -> { |
| 144 | if (update.target() == MAP) { |
| 145 | Result<UpdateResult<String, byte[]>> result = update.output(); |
| 146 | if (result.success() && result.value().mapName().equals(name)) { |
| 147 | MapEvent<K, V> mapEvent = result.value().<K, V>map(this::dK, serializer::decode).toMapEvent(); |
| 148 | notifyLocalListeners(mapEvent); |
| 149 | } |
| 150 | } |
| 151 | }); |
| 152 | }); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 153 | this.metricsService = DefaultServiceDirectory.getService(MetricsService.class); |
| 154 | this.metricsComponent = metricsService.registerComponent(COMPONENT_NAME); |
| 155 | this.metricsFeature = metricsComponent.registerFeature(name); |
| 156 | this.wildcard = metricsComponent.registerFeature("*"); |
| 157 | this.perMapTimer = metricsService.createTimer(metricsComponent, metricsFeature, "*"); |
| 158 | this.cMapTimer = metricsService.createTimer(metricsComponent, wildcard, "*"); |
| 159 | |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Returns this map name. |
| 164 | * @return map name |
| 165 | */ |
| 166 | public String name() { |
| 167 | return name; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns the serializer for map entries. |
| 172 | * @return map entry serializer |
| 173 | */ |
| 174 | public Serializer serializer() { |
| 175 | return serializer; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 178 | /** |
| 179 | * Returns the applicationId owning this map. |
| 180 | * @return application Id |
| 181 | */ |
| 182 | public ApplicationId applicationId() { |
| 183 | return applicationId; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Returns whether the map entries should be purged when the application |
| 188 | * owning it is uninstalled. |
| 189 | * @return true is map needs to cleared on app uninstall; false otherwise |
| 190 | */ |
| 191 | public boolean purgeOnUninstall() { |
| 192 | return purgeOnUninstall; |
| 193 | } |
| 194 | |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 195 | @Override |
| 196 | public CompletableFuture<Integer> size() { |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 197 | final OperationTimer timer = startTimer(SIZE); |
| 198 | return database.mapSize(name) |
| 199 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public CompletableFuture<Boolean> isEmpty() { |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 204 | final OperationTimer timer = startTimer(IS_EMPTY); |
| 205 | return database.mapIsEmpty(name) |
| 206 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public CompletableFuture<Boolean> containsKey(K key) { |
| 211 | checkNotNull(key, ERROR_NULL_KEY); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 212 | final OperationTimer timer = startTimer(CONTAINS_KEY); |
| 213 | return database.mapContainsKey(name, keyCache.getUnchecked(key)) |
| 214 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public CompletableFuture<Boolean> containsValue(V value) { |
| 219 | checkNotNull(value, ERROR_NULL_VALUE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 220 | final OperationTimer timer = startTimer(CONTAINS_VALUE); |
| 221 | return database.mapContainsValue(name, serializer.encode(value)) |
| 222 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public CompletableFuture<Versioned<V>> get(K key) { |
| 227 | checkNotNull(key, ERROR_NULL_KEY); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 228 | final OperationTimer timer = startTimer(GET); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 229 | return database.mapGet(name, keyCache.getUnchecked(key)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 230 | .whenComplete((r, e) -> timer.stop()) |
| 231 | .thenApply(v -> v != null ? v.map(serializer::decode) : null); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 235 | public CompletableFuture<Versioned<V>> computeIfAbsent(K key, |
| 236 | Function<? super K, ? extends V> mappingFunction) { |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 237 | checkNotNull(key, ERROR_NULL_KEY); |
| 238 | checkNotNull(mappingFunction, "Mapping function cannot be null"); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 239 | final OperationTimer timer = startTimer(COMPUTE_IF_ABSENT); |
| 240 | return updateAndGet(key, Match.ifNull(), Match.any(), mappingFunction.apply(key)) |
| 241 | .whenComplete((r, e) -> timer.stop()) |
| 242 | .thenApply(v -> v.newValue()); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public CompletableFuture<Versioned<V>> computeIfPresent(K key, |
| 247 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
| 248 | return computeIf(key, Objects::nonNull, remappingFunction); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public CompletableFuture<Versioned<V>> compute(K key, |
| 253 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
| 254 | return computeIf(key, v -> true, remappingFunction); |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public CompletableFuture<Versioned<V>> computeIf(K key, |
| 259 | Predicate<? super V> condition, |
| 260 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
| 261 | checkNotNull(key, ERROR_NULL_KEY); |
| 262 | checkNotNull(condition, "predicate function cannot be null"); |
| 263 | checkNotNull(remappingFunction, "Remapping function cannot be null"); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 264 | final OperationTimer timer = startTimer(COMPUTE_IF); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 265 | return get(key).thenCompose(r1 -> { |
| 266 | V existingValue = r1 == null ? null : r1.value(); |
| 267 | // if the condition evaluates to false, return existing value. |
| 268 | if (!condition.test(existingValue)) { |
| 269 | return CompletableFuture.completedFuture(r1); |
| 270 | } |
| 271 | |
| 272 | AtomicReference<V> computedValue = new AtomicReference<>(); |
| 273 | // if remappingFunction throws an exception, return the exception. |
| 274 | try { |
| 275 | computedValue.set(remappingFunction.apply(key, existingValue)); |
| 276 | } catch (Exception e) { |
| 277 | return Tools.exceptionalFuture(e); |
| 278 | } |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 279 | if (computedValue.get() == null && r1 == null) { |
| 280 | return CompletableFuture.completedFuture(null); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 281 | } |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 282 | Match<V> valueMatcher = r1 == null ? Match.ifNull() : Match.any(); |
| 283 | Match<Long> versionMatcher = r1 == null ? Match.any() : Match.ifValue(r1.version()); |
| 284 | return updateAndGet(key, valueMatcher, versionMatcher, computedValue.get()) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 285 | .whenComplete((r, e) -> timer.stop()) |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 286 | .thenApply(v -> { |
| 287 | if (v.updated()) { |
| 288 | return v.newValue(); |
| 289 | } else { |
| 290 | throw new ConsistentMapException.ConcurrentModification(); |
| 291 | } |
| 292 | }); |
| 293 | }); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Override |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 297 | public CompletableFuture<Versioned<V>> put(K key, V value) { |
| 298 | checkNotNull(key, ERROR_NULL_KEY); |
| 299 | checkNotNull(value, ERROR_NULL_VALUE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 300 | final OperationTimer timer = startTimer(PUT); |
| 301 | return updateAndGet(key, Match.any(), Match.any(), value).thenApply(v -> v.oldValue()) |
| 302 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public CompletableFuture<Versioned<V>> putAndGet(K key, V value) { |
| 307 | checkNotNull(key, ERROR_NULL_KEY); |
| 308 | checkNotNull(value, ERROR_NULL_VALUE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 309 | final OperationTimer timer = startTimer(PUT_AND_GET); |
| 310 | return updateAndGet(key, Match.any(), Match.any(), value).thenApply(v -> v.newValue()) |
| 311 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | @Override |
| 315 | public CompletableFuture<Versioned<V>> remove(K key) { |
| 316 | checkNotNull(key, ERROR_NULL_KEY); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 317 | final OperationTimer timer = startTimer(REMOVE); |
| 318 | return updateAndGet(key, Match.any(), Match.any(), null).thenApply(v -> v.oldValue()) |
| 319 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | @Override |
| 323 | public CompletableFuture<Void> clear() { |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 324 | checkIfUnmodifiable(); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 325 | final OperationTimer timer = startTimer(CLEAR); |
| 326 | return database.mapClear(name).thenApply(this::unwrapResult) |
| 327 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public CompletableFuture<Set<K>> keySet() { |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 332 | final OperationTimer timer = startTimer(KEY_SET); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 333 | return database.mapKeySet(name) |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 334 | .thenApply(s -> s |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 335 | .stream() |
| 336 | .map(this::dK) |
| 337 | .collect(Collectors.toSet())) |
| 338 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public CompletableFuture<Collection<Versioned<V>>> values() { |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 343 | final OperationTimer timer = startTimer(VALUES); |
| 344 | return database.mapValues(name) |
| 345 | .whenComplete((r, e) -> timer.stop()) |
| 346 | .thenApply(c -> c |
| 347 | .stream() |
| 348 | .map(v -> v.<V>map(serializer::decode)) |
| 349 | .collect(Collectors.toList())); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | @Override |
| 353 | public CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet() { |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 354 | final OperationTimer timer = startTimer(ENTRY_SET); |
| 355 | return database.mapEntrySet(name) |
| 356 | .whenComplete((r, e) -> timer.stop()) |
| 357 | .thenApply(s -> s |
| 358 | .stream() |
| 359 | .map(this::mapRawEntry) |
| 360 | .collect(Collectors.toSet())); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | @Override |
| 364 | public CompletableFuture<Versioned<V>> putIfAbsent(K key, V value) { |
| 365 | checkNotNull(key, ERROR_NULL_KEY); |
| 366 | checkNotNull(value, ERROR_NULL_VALUE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 367 | final OperationTimer timer = startTimer(PUT_IF_ABSENT); |
| 368 | return updateAndGet(key, Match.ifNull(), Match.any(), value) |
| 369 | .whenComplete((r, e) -> timer.stop()) |
| 370 | .thenApply(v -> v.oldValue()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public CompletableFuture<Boolean> remove(K key, V value) { |
| 375 | checkNotNull(key, ERROR_NULL_KEY); |
| 376 | checkNotNull(value, ERROR_NULL_VALUE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 377 | final OperationTimer timer = startTimer(REMOVE); |
| 378 | return updateAndGet(key, Match.ifValue(value), Match.any(), null) |
| 379 | .whenComplete((r, e) -> timer.stop()) |
| 380 | .thenApply(v -> v.updated()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | @Override |
| 384 | public CompletableFuture<Boolean> remove(K key, long version) { |
| 385 | checkNotNull(key, ERROR_NULL_KEY); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 386 | final OperationTimer timer = startTimer(REMOVE); |
| 387 | return updateAndGet(key, Match.any(), Match.ifValue(version), null) |
| 388 | .whenComplete((r, e) -> timer.stop()) |
| 389 | .thenApply(v -> v.updated()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) { |
| 394 | checkNotNull(key, ERROR_NULL_KEY); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 395 | checkNotNull(oldValue, ERROR_NULL_VALUE); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 396 | checkNotNull(newValue, ERROR_NULL_VALUE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 397 | final OperationTimer timer = startTimer(REPLACE); |
| 398 | return updateAndGet(key, Match.ifValue(oldValue), Match.any(), newValue) |
| 399 | .whenComplete((r, e) -> timer.stop()) |
| 400 | .thenApply(v -> v.updated()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | @Override |
| 404 | public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) { |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 405 | final OperationTimer timer = startTimer(REPLACE); |
| 406 | return updateAndGet(key, Match.any(), Match.ifValue(oldVersion), newValue) |
| 407 | .whenComplete((r, e) -> timer.stop()) |
| 408 | .thenApply(v -> v.updated()); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 411 | private Map.Entry<K, Versioned<V>> mapRawEntry(Map.Entry<String, Versioned<byte[]>> e) { |
| 412 | return Maps.immutableEntry(dK(e.getKey()), e.getValue().<V>map(serializer::decode)); |
| 413 | } |
| 414 | |
| 415 | private CompletableFuture<UpdateResult<K, V>> updateAndGet(K key, |
| 416 | Match<V> oldValueMatch, |
| 417 | Match<Long> oldVersionMatch, |
| 418 | V value) { |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 419 | checkIfUnmodifiable(); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 420 | return database.mapUpdate(name, |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 421 | keyCache.getUnchecked(key), |
| 422 | oldValueMatch.map(serializer::encode), |
| 423 | oldVersionMatch, |
| 424 | value == null ? null : serializer.encode(value)) |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 425 | .thenApply(this::unwrapResult) |
| 426 | .thenApply(r -> r.<K, V>map(this::dK, serializer::decode)) |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 427 | .whenComplete((r, e) -> { |
| 428 | if (r != null && e == null && !database.hasChangeNotificationSupport()) { |
| 429 | notifyListeners(r.toMapEvent()); |
| 430 | } |
| 431 | }); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 432 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 433 | |
| 434 | private <T> T unwrapResult(Result<T> result) { |
| 435 | if (result.status() == Result.Status.LOCKED) { |
| 436 | throw new ConsistentMapException.ConcurrentModification(); |
| 437 | } else if (result.success()) { |
| 438 | return result.value(); |
| 439 | } else { |
| 440 | throw new IllegalStateException("Must not be here"); |
| 441 | } |
| 442 | } |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 443 | |
| 444 | private void checkIfUnmodifiable() { |
| 445 | if (readOnly) { |
| 446 | throw new UnsupportedOperationException(); |
| 447 | } |
| 448 | } |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 449 | |
| 450 | @Override |
| 451 | public void addListener(MapEventListener<K, V> listener) { |
| 452 | listeners.add(listener); |
| 453 | } |
| 454 | |
| 455 | @Override |
| 456 | public void removeListener(MapEventListener<K, V> listener) { |
| 457 | listeners.remove(listener); |
| 458 | } |
| 459 | |
| 460 | protected void notifyListeners(MapEvent<K, V> event) { |
| 461 | try { |
| 462 | if (event != null) { |
| 463 | notifyLocalListeners(event); |
| 464 | notifyRemoteListeners(event); |
| 465 | } |
| 466 | } catch (Exception e) { |
| 467 | log.warn("Failure notifying listeners about {}", event, e); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | protected void notifyLocalListeners(MapEvent<K, V> event) { |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 472 | if (event != null) { |
| 473 | listeners.forEach(listener -> listener.event(event)); |
| 474 | } |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | protected void notifyRemoteListeners(MapEvent<K, V> event) { |
| 478 | if (eventPublisher != null) { |
| 479 | eventPublisher.accept(event); |
| 480 | } |
| 481 | } |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 482 | |
| 483 | private OperationTimer startTimer(String op) { |
| 484 | //check if timer exist, if it doesn't creates it |
| 485 | final Timer currTimer = perMapOpTimers.computeIfAbsent(op, timer -> |
| 486 | metricsService.createTimer(metricsComponent, metricsFeature, op)); |
| 487 | perOpTimers.computeIfAbsent(op, timer -> metricsService.createTimer(metricsComponent, wildcard, op)); |
| 488 | //starts timer |
| 489 | return new OperationTimer(currTimer.time(), op); |
| 490 | } |
| 491 | |
| 492 | private class OperationTimer { |
| 493 | private final Timer.Context context; |
| 494 | private final String operation; |
| 495 | |
| 496 | public OperationTimer(Timer.Context context, String operation) { |
| 497 | this.context = context; |
| 498 | this.operation = operation; |
| 499 | } |
| 500 | |
| 501 | public void stop() { |
| 502 | //Stop and updates timer with specific measurements per map, per operation |
| 503 | final long time = context.stop(); |
| 504 | //updates timer with aggregated measurements per map |
| 505 | perOpTimers.get(operation).update(time, TimeUnit.NANOSECONDS); |
| 506 | //updates timer with aggregated measurements per map |
| 507 | perMapTimer.update(time, TimeUnit.NANOSECONDS); |
| 508 | //updates timer with aggregated measurements per all Consistent Maps |
| 509 | cMapTimer.update(time, TimeUnit.NANOSECONDS); |
| 510 | } |
| 511 | } |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 512 | } |