blob: 5c0eaefba153406c1d4485dec0391bbccc0275aa [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 Jampanie88086f2016-06-16 16:48:14 -070054 private final MapEventListener<K, V> cacheUpdater;
Madan Jampani1d3b6172016-04-28 13:22:57 -070055 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));
Madan Jampanie88086f2016-06-16 16:48:14 -070077 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 Jampani1d3b6172016-04-28 13:22:57 -070085 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 Jampanie88086f2016-06-16 16:48:14 -070093 super.addListener(cacheUpdater);
Madan Jampani1d3b6172016-04-28 13:22:57 -070094 super.addStatusChangeListener(statusListener);
sangyun-hancce07c52016-04-06 11:07:59 +090095 }
96
Madan Jampani62f15332016-02-01 13:18:39 -080097 @Override
98 public CompletableFuture<Void> destroy() {
Madan Jampani1d3b6172016-04-28 13:22:57 -070099 super.removeStatusChangeListener(statusListener);
Madan Jampanie88086f2016-06-16 16:48:14 -0700100 return super.destroy().thenCompose(v -> removeListener(cacheUpdater));
Madan Jampani10073672016-01-21 19:13:59 -0800101 }
102
103 @Override
104 public CompletableFuture<Versioned<V>> get(K key) {
Madan Jampani77012442016-06-02 07:47:42 -0700105 return cache.getUnchecked(key)
106 .whenComplete((r, e) -> {
107 if (e != null) {
108 cache.invalidate(key);
109 }
110 });
Madan Jampani10073672016-01-21 19:13:59 -0800111 }
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-hancce07c52016-04-06 11:07:59 +0900118 .whenComplete((r, e) -> cache.invalidate(key));
Madan Jampani10073672016-01-21 19:13:59 -0800119 }
120
121 @Override
122 public CompletableFuture<Versioned<V>> put(K key, V value) {
123 return super.put(key, value)
sangyun-hancce07c52016-04-06 11:07:59 +0900124 .whenComplete((r, e) -> cache.invalidate(key));
Madan Jampani10073672016-01-21 19:13:59 -0800125 }
126
127 @Override
128 public CompletableFuture<Versioned<V>> putAndGet(K key, V value) {
Simon Hunt5829c342016-03-07 17:01:43 -0800129 return super.putAndGet(key, value)
sangyun-hancce07c52016-04-06 11:07:59 +0900130 .whenComplete((r, e) -> cache.invalidate(key));
Madan Jampani10073672016-01-21 19:13:59 -0800131 }
132
133 @Override
134 public CompletableFuture<Versioned<V>> remove(K key) {
135 return super.remove(key)
sangyun-hancce07c52016-04-06 11:07:59 +0900136 .whenComplete((r, e) -> cache.invalidate(key));
Madan Jampani10073672016-01-21 19:13:59 -0800137 }
138
139 @Override
140 public CompletableFuture<Void> clear() {
141 return super.clear()
sangyun-hancce07c52016-04-06 11:07:59 +0900142 .whenComplete((r, e) -> cache.invalidateAll());
Madan Jampani10073672016-01-21 19:13:59 -0800143 }
144
145 @Override
146 public CompletableFuture<Boolean> remove(K key, V value) {
147 return super.remove(key, value)
Madan Jampani77012442016-06-02 07:47:42 -0700148 .whenComplete((r, e) -> {
149 if (r) {
150 cache.invalidate(key);
151 }
152 });
Madan Jampani10073672016-01-21 19:13:59 -0800153 }
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 Jampani77012442016-06-02 07:47:42 -0700168 .whenComplete((r, e) -> cache.invalidate(key));
Madan Jampani10073672016-01-21 19:13:59 -0800169 }
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}