blob: 75d46c0d89c2d0477e9f7843c59eaae8cf43a94c [file] [log] [blame]
Madan Jampani10073672016-01-21 19:13:59 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampani10073672016-01-21 19:13:59 -08003 *
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 Jampani10073672016-01-21 19:13:59 -080016package org.onosproject.store.primitives.impl;
17
Madan Jampani1d3b6172016-04-28 13:22:57 -070018import static org.slf4j.LoggerFactory.getLogger;
19
Madan Jampani10073672016-01-21 19:13:59 -080020import java.util.concurrent.CompletableFuture;
21import java.util.function.BiFunction;
Madan Jampani1d3b6172016-04-28 13:22:57 -070022import java.util.function.Consumer;
Madan Jampani10073672016-01-21 19:13:59 -080023import java.util.function.Predicate;
24
25import org.onosproject.store.service.AsyncConsistentMap;
Madan Jampani62f15332016-02-01 13:18:39 -080026import org.onosproject.store.service.MapEventListener;
Madan Jampani10073672016-01-21 19:13:59 -080027import org.onosproject.store.service.Versioned;
Madan Jampani1d3b6172016-04-28 13:22:57 -070028import org.slf4j.Logger;
Madan Jampani10073672016-01-21 19:13:59 -080029
30import com.google.common.cache.CacheBuilder;
31import com.google.common.cache.CacheLoader;
32import com.google.common.cache.LoadingCache;
33
Madan Jampani1d3b6172016-04-28 13:22:57 -070034import static org.onosproject.store.service.DistributedPrimitive.Status.INACTIVE;
35import static org.onosproject.store.service.DistributedPrimitive.Status.SUSPENDED;
36
Madan Jampani10073672016-01-21 19:13:59 -080037/**
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 */
48public class CachingAsyncConsistentMap<K, V> extends DelegatingAsyncConsistentMap<K, V> {
Madan Jampani1d3b6172016-04-28 13:22:57 -070049 private static final int DEFAULT_CACHE_SIZE = 10000;
50 private final Logger log = getLogger(getClass());
Madan Jampani10073672016-01-21 19:13:59 -080051
Madan Jampani1d3b6172016-04-28 13:22:57 -070052 private final LoadingCache<K, CompletableFuture<Versioned<V>>> cache;
Madan Jampani10073672016-01-21 19:13:59 -080053
Madan Jampani1d3b6172016-04-28 13:22:57 -070054 private final MapEventListener<K, V> cacheInvalidator;
55 private final Consumer<Status> statusListener;
Madan Jampani62f15332016-02-01 13:18:39 -080056
sangyun-hancce07c52016-04-06 11:07:59 +090057 /**
58 * Default constructor.
59 *
60 * @param backingMap a distributed, strongly consistent map for backing
61 */
Madan Jampani10073672016-01-21 19:13:59 -080062 public CachingAsyncConsistentMap(AsyncConsistentMap<K, V> backingMap) {
Madan Jampani1d3b6172016-04-28 13:22:57 -070063 this(backingMap, DEFAULT_CACHE_SIZE);
Madan Jampani62f15332016-02-01 13:18:39 -080064 }
65
sangyun-hancce07c52016-04-06 11:07:59 +090066 /**
Madan Jampani1d3b6172016-04-28 13:22:57 -070067 * Constructor to configure cache size.
sangyun-hancce07c52016-04-06 11:07:59 +090068 *
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 Jampani1d3b6172016-04-28 13:22:57 -070074 cache = CacheBuilder.newBuilder()
75 .maximumSize(cacheSize)
76 .build(CacheLoader.from(CachingAsyncConsistentMap.super::get));
77 cacheInvalidator = event -> cache.invalidate(event.key());
78 statusListener = status -> {
79 log.debug("{} status changed to {}", this.name(), status);
80 // If the status of the underlying map is SUSPENDED or INACTIVE
81 // we can no longer guarantee that the cache will be in sync.
82 if (status == SUSPENDED || status == INACTIVE) {
83 cache.invalidateAll();
84 }
85 };
sangyun-hancce07c52016-04-06 11:07:59 +090086 super.addListener(cacheInvalidator);
Madan Jampani1d3b6172016-04-28 13:22:57 -070087 super.addStatusChangeListener(statusListener);
sangyun-hancce07c52016-04-06 11:07:59 +090088 }
89
Madan Jampani62f15332016-02-01 13:18:39 -080090 @Override
91 public CompletableFuture<Void> destroy() {
Madan Jampani1d3b6172016-04-28 13:22:57 -070092 super.removeStatusChangeListener(statusListener);
Madan Jampani62f15332016-02-01 13:18:39 -080093 return super.destroy().thenCompose(v -> removeListener(cacheInvalidator));
Madan Jampani10073672016-01-21 19:13:59 -080094 }
95
96 @Override
97 public CompletableFuture<Versioned<V>> get(K key) {
Madan Jampani77012442016-06-02 07:47:42 -070098 return cache.getUnchecked(key)
99 .whenComplete((r, e) -> {
100 if (e != null) {
101 cache.invalidate(key);
102 }
103 });
Madan Jampani10073672016-01-21 19:13:59 -0800104 }
105
106 @Override
107 public CompletableFuture<Versioned<V>> computeIf(K key,
108 Predicate<? super V> condition,
109 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
110 return super.computeIf(key, condition, remappingFunction)
sangyun-hancce07c52016-04-06 11:07:59 +0900111 .whenComplete((r, e) -> cache.invalidate(key));
Madan Jampani10073672016-01-21 19:13:59 -0800112 }
113
114 @Override
115 public CompletableFuture<Versioned<V>> put(K key, V value) {
116 return super.put(key, value)
sangyun-hancce07c52016-04-06 11:07:59 +0900117 .whenComplete((r, e) -> cache.invalidate(key));
Madan Jampani10073672016-01-21 19:13:59 -0800118 }
119
120 @Override
121 public CompletableFuture<Versioned<V>> putAndGet(K key, V value) {
Simon Hunt5829c342016-03-07 17:01:43 -0800122 return super.putAndGet(key, value)
sangyun-hancce07c52016-04-06 11:07:59 +0900123 .whenComplete((r, e) -> cache.invalidate(key));
Madan Jampani10073672016-01-21 19:13:59 -0800124 }
125
126 @Override
127 public CompletableFuture<Versioned<V>> remove(K key) {
128 return super.remove(key)
sangyun-hancce07c52016-04-06 11:07:59 +0900129 .whenComplete((r, e) -> cache.invalidate(key));
Madan Jampani10073672016-01-21 19:13:59 -0800130 }
131
132 @Override
133 public CompletableFuture<Void> clear() {
134 return super.clear()
sangyun-hancce07c52016-04-06 11:07:59 +0900135 .whenComplete((r, e) -> cache.invalidateAll());
Madan Jampani10073672016-01-21 19:13:59 -0800136 }
137
138 @Override
139 public CompletableFuture<Boolean> remove(K key, V value) {
140 return super.remove(key, value)
Madan Jampani77012442016-06-02 07:47:42 -0700141 .whenComplete((r, e) -> {
142 if (r) {
143 cache.invalidate(key);
144 }
145 });
Madan Jampani10073672016-01-21 19:13:59 -0800146 }
147
148 @Override
149 public CompletableFuture<Boolean> remove(K key, long version) {
150 return super.remove(key, version)
151 .whenComplete((r, e) -> {
152 if (r) {
153 cache.invalidate(key);
154 }
155 });
156 }
157
158 @Override
159 public CompletableFuture<Versioned<V>> replace(K key, V value) {
160 return super.replace(key, value)
Madan Jampani77012442016-06-02 07:47:42 -0700161 .whenComplete((r, e) -> cache.invalidate(key));
Madan Jampani10073672016-01-21 19:13:59 -0800162 }
163
164 @Override
165 public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) {
166 return super.replace(key, oldValue, newValue)
167 .whenComplete((r, e) -> {
168 if (r) {
169 cache.invalidate(key);
170 }
171 });
172 }
173
174 @Override
175 public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) {
176 return super.replace(key, oldVersion, newValue)
177 .whenComplete((r, e) -> {
178 if (r) {
179 cache.invalidate(key);
180 }
181 });
182 }
183}