Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 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 | */ |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 20 | import java.util.concurrent.CompletableFuture; |
| 21 | import java.util.function.BiFunction; |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 22 | import java.util.function.Consumer; |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 23 | import java.util.function.Predicate; |
| 24 | |
| 25 | import org.onosproject.store.service.AsyncConsistentMap; |
Madan Jampani | 62f1533 | 2016-02-01 13:18:39 -0800 | [diff] [blame] | 26 | import org.onosproject.store.service.MapEventListener; |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 27 | import org.onosproject.store.service.Versioned; |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 28 | import org.slf4j.Logger; |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 29 | |
| 30 | import com.google.common.cache.CacheBuilder; |
| 31 | import com.google.common.cache.CacheLoader; |
| 32 | import com.google.common.cache.LoadingCache; |
| 33 | |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 34 | import static org.onosproject.store.service.DistributedPrimitive.Status.INACTIVE; |
| 35 | import static org.onosproject.store.service.DistributedPrimitive.Status.SUSPENDED; |
| 36 | |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 37 | /** |
| 38 | * {@code AsyncConsistentMap} that caches entries on read. |
| 39 | * <p> |
| 40 | * The cache entries are automatically invalidated when updates are detected either locally or |
| 41 | * remotely. |
| 42 | * <p> This implementation only attempts to serve cached entries for {@link AsyncConsistentMap#get get} |
| 43 | * calls. All other calls skip the cache and directly go the backing map. |
| 44 | * |
| 45 | * @param <K> key type |
| 46 | * @param <V> value type |
| 47 | */ |
| 48 | public class CachingAsyncConsistentMap<K, V> extends DelegatingAsyncConsistentMap<K, V> { |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 49 | private static final int DEFAULT_CACHE_SIZE = 10000; |
| 50 | private final Logger log = getLogger(getClass()); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 51 | |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 52 | private final LoadingCache<K, CompletableFuture<Versioned<V>>> cache; |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 53 | |
Madan Jampani | e88086f | 2016-06-16 16:48:14 -0700 | [diff] [blame] | 54 | private final MapEventListener<K, V> cacheUpdater; |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 55 | private final Consumer<Status> statusListener; |
Madan Jampani | 62f1533 | 2016-02-01 13:18:39 -0800 | [diff] [blame] | 56 | |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 57 | /** |
| 58 | * Default constructor. |
| 59 | * |
| 60 | * @param backingMap a distributed, strongly consistent map for backing |
| 61 | */ |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 62 | public CachingAsyncConsistentMap(AsyncConsistentMap<K, V> backingMap) { |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 63 | this(backingMap, DEFAULT_CACHE_SIZE); |
Madan Jampani | 62f1533 | 2016-02-01 13:18:39 -0800 | [diff] [blame] | 64 | } |
| 65 | |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 66 | /** |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 67 | * Constructor to configure cache size. |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 68 | * |
| 69 | * @param backingMap a distributed, strongly consistent map for backing |
| 70 | * @param cacheSize the maximum size of the cache |
| 71 | */ |
| 72 | public CachingAsyncConsistentMap(AsyncConsistentMap<K, V> backingMap, int cacheSize) { |
| 73 | super(backingMap); |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 74 | cache = CacheBuilder.newBuilder() |
| 75 | .maximumSize(cacheSize) |
| 76 | .build(CacheLoader.from(CachingAsyncConsistentMap.super::get)); |
Madan Jampani | e88086f | 2016-06-16 16:48:14 -0700 | [diff] [blame] | 77 | cacheUpdater = event -> { |
| 78 | Versioned<V> newValue = event.newValue(); |
| 79 | if (newValue == null) { |
| 80 | cache.invalidate(event.key()); |
| 81 | } else { |
| 82 | cache.put(event.key(), CompletableFuture.completedFuture(newValue)); |
| 83 | } |
| 84 | }; |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 85 | statusListener = status -> { |
| 86 | log.debug("{} status changed to {}", this.name(), status); |
| 87 | // If the status of the underlying map is SUSPENDED or INACTIVE |
| 88 | // we can no longer guarantee that the cache will be in sync. |
| 89 | if (status == SUSPENDED || status == INACTIVE) { |
| 90 | cache.invalidateAll(); |
| 91 | } |
| 92 | }; |
Madan Jampani | e88086f | 2016-06-16 16:48:14 -0700 | [diff] [blame] | 93 | super.addListener(cacheUpdater); |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 94 | super.addStatusChangeListener(statusListener); |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 95 | } |
| 96 | |
Madan Jampani | 62f1533 | 2016-02-01 13:18:39 -0800 | [diff] [blame] | 97 | @Override |
| 98 | public CompletableFuture<Void> destroy() { |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 99 | super.removeStatusChangeListener(statusListener); |
Madan Jampani | e88086f | 2016-06-16 16:48:14 -0700 | [diff] [blame] | 100 | return super.destroy().thenCompose(v -> removeListener(cacheUpdater)); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public CompletableFuture<Versioned<V>> get(K key) { |
Madan Jampani | 7701244 | 2016-06-02 07:47:42 -0700 | [diff] [blame] | 105 | return cache.getUnchecked(key) |
| 106 | .whenComplete((r, e) -> { |
| 107 | if (e != null) { |
| 108 | cache.invalidate(key); |
| 109 | } |
| 110 | }); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public CompletableFuture<Versioned<V>> computeIf(K key, |
| 115 | Predicate<? super V> condition, |
| 116 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
| 117 | return super.computeIf(key, condition, remappingFunction) |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 118 | .whenComplete((r, e) -> cache.invalidate(key)); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public CompletableFuture<Versioned<V>> put(K key, V value) { |
| 123 | return super.put(key, value) |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 124 | .whenComplete((r, e) -> cache.invalidate(key)); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public CompletableFuture<Versioned<V>> putAndGet(K key, V value) { |
Simon Hunt | 5829c34 | 2016-03-07 17:01:43 -0800 | [diff] [blame] | 129 | return super.putAndGet(key, value) |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 130 | .whenComplete((r, e) -> cache.invalidate(key)); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public CompletableFuture<Versioned<V>> remove(K key) { |
| 135 | return super.remove(key) |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 136 | .whenComplete((r, e) -> cache.invalidate(key)); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public CompletableFuture<Void> clear() { |
| 141 | return super.clear() |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 142 | .whenComplete((r, e) -> cache.invalidateAll()); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public CompletableFuture<Boolean> remove(K key, V value) { |
| 147 | return super.remove(key, value) |
Madan Jampani | 7701244 | 2016-06-02 07:47:42 -0700 | [diff] [blame] | 148 | .whenComplete((r, e) -> { |
| 149 | if (r) { |
| 150 | cache.invalidate(key); |
| 151 | } |
| 152 | }); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public CompletableFuture<Boolean> remove(K key, long version) { |
| 157 | return super.remove(key, version) |
| 158 | .whenComplete((r, e) -> { |
| 159 | if (r) { |
| 160 | cache.invalidate(key); |
| 161 | } |
| 162 | }); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public CompletableFuture<Versioned<V>> replace(K key, V value) { |
| 167 | return super.replace(key, value) |
Madan Jampani | 7701244 | 2016-06-02 07:47:42 -0700 | [diff] [blame] | 168 | .whenComplete((r, e) -> cache.invalidate(key)); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) { |
| 173 | return super.replace(key, oldValue, newValue) |
| 174 | .whenComplete((r, e) -> { |
| 175 | if (r) { |
| 176 | cache.invalidate(key); |
| 177 | } |
| 178 | }); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) { |
| 183 | return super.replace(key, oldVersion, newValue) |
| 184 | .whenComplete((r, e) -> { |
| 185 | if (r) { |
| 186 | cache.invalidate(key); |
| 187 | } |
| 188 | }); |
| 189 | } |
| 190 | } |