Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.util.Collection; |
Jordan Halterman | 7cdb87f | 2018-06-26 23:41:22 -0700 | [diff] [blame] | 19 | import java.util.Map; |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | import java.util.concurrent.CompletableFuture; |
Jordan Halterman | 7cdb87f | 2018-06-26 23:41:22 -0700 | [diff] [blame] | 22 | import java.util.concurrent.ConcurrentHashMap; |
| 23 | import java.util.concurrent.Executor; |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 24 | import java.util.function.Consumer; |
| 25 | import java.util.stream.Collectors; |
| 26 | |
| 27 | import com.google.common.cache.CacheBuilder; |
| 28 | import com.google.common.cache.CacheLoader; |
| 29 | import com.google.common.cache.LoadingCache; |
| 30 | import com.google.common.collect.ImmutableSet; |
Jordan Halterman | 7cdb87f | 2018-06-26 23:41:22 -0700 | [diff] [blame] | 31 | import com.google.common.util.concurrent.MoreExecutors; |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 32 | import org.onosproject.store.service.AsyncConsistentMultimap; |
| 33 | import org.onosproject.store.service.MultimapEventListener; |
| 34 | import org.onosproject.store.service.Versioned; |
| 35 | import org.slf4j.Logger; |
| 36 | |
| 37 | import static org.onosproject.store.service.DistributedPrimitive.Status.INACTIVE; |
| 38 | import static org.onosproject.store.service.DistributedPrimitive.Status.SUSPENDED; |
| 39 | import static org.slf4j.LoggerFactory.getLogger; |
| 40 | |
| 41 | /** |
| 42 | * Caching {@link AsyncConsistentMultimap} implementation. |
| 43 | */ |
| 44 | public class CachingAsyncConsistentMultimap<K, V> extends DelegatingAsyncConsistentMultimap<K, V> { |
| 45 | private static final int DEFAULT_CACHE_SIZE = 10000; |
| 46 | private final Logger log = getLogger(getClass()); |
| 47 | |
Jordan Halterman | 7cdb87f | 2018-06-26 23:41:22 -0700 | [diff] [blame] | 48 | private final Map<MultimapEventListener<K, V>, Executor> mapEventListeners = new ConcurrentHashMap<>(); |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 49 | private final LoadingCache<K, CompletableFuture<Versioned<Collection<? extends V>>>> cache; |
| 50 | private final MultimapEventListener<K, V> cacheUpdater; |
| 51 | private final Consumer<Status> statusListener; |
| 52 | |
| 53 | /** |
| 54 | * Default constructor. |
| 55 | * |
| 56 | * @param backingMap a distributed, strongly consistent map for backing |
| 57 | */ |
| 58 | public CachingAsyncConsistentMultimap(AsyncConsistentMultimap<K, V> backingMap) { |
| 59 | this(backingMap, DEFAULT_CACHE_SIZE); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Constructor to configure cache size. |
| 64 | * |
| 65 | * @param backingMap a distributed, strongly consistent map for backing |
| 66 | * @param cacheSize the maximum size of the cache |
| 67 | */ |
| 68 | public CachingAsyncConsistentMultimap(AsyncConsistentMultimap<K, V> backingMap, int cacheSize) { |
| 69 | super(backingMap); |
| 70 | cache = CacheBuilder.newBuilder() |
| 71 | .maximumSize(cacheSize) |
| 72 | .build(CacheLoader.from(CachingAsyncConsistentMultimap.super::get)); |
| 73 | cacheUpdater = event -> { |
| 74 | V oldValue = event.oldValue(); |
| 75 | V newValue = event.newValue(); |
| 76 | CompletableFuture<Versioned<Collection<? extends V>>> future = cache.getUnchecked(event.key()); |
| 77 | switch (event.type()) { |
| 78 | case INSERT: |
| 79 | if (future.isDone()) { |
| 80 | Versioned<Collection<? extends V>> oldVersioned = future.join(); |
| 81 | Versioned<Collection<? extends V>> newVersioned = new Versioned<>( |
| 82 | ImmutableSet.<V>builder().addAll(oldVersioned.value()).add(newValue).build(), |
| 83 | oldVersioned.version(), |
| 84 | oldVersioned.creationTime()); |
| 85 | cache.put(event.key(), CompletableFuture.completedFuture(newVersioned)); |
| 86 | } else { |
| 87 | cache.put(event.key(), future.thenApply(versioned -> new Versioned<>( |
| 88 | ImmutableSet.<V>builder().addAll(versioned.value()).add(newValue).build(), |
| 89 | versioned.version(), |
| 90 | versioned.creationTime()))); |
| 91 | } |
| 92 | break; |
| 93 | case REMOVE: |
| 94 | if (future.isDone()) { |
| 95 | Versioned<Collection<? extends V>> oldVersioned = future.join(); |
| 96 | cache.put(event.key(), CompletableFuture.completedFuture(new Versioned<>(oldVersioned.value() |
| 97 | .stream() |
| 98 | .filter(value -> !Objects.equals(value, oldValue)) |
| 99 | .collect(Collectors.toSet()), oldVersioned.version(), oldVersioned.creationTime()))); |
| 100 | } else { |
| 101 | cache.put(event.key(), future.thenApply(versioned -> new Versioned<>(versioned.value() |
| 102 | .stream() |
| 103 | .filter(value -> !Objects.equals(value, oldValue)) |
| 104 | .collect(Collectors.toSet()), versioned.version(), versioned.creationTime()))); |
| 105 | } |
| 106 | break; |
| 107 | default: |
| 108 | break; |
| 109 | } |
Jordan Halterman | 7cdb87f | 2018-06-26 23:41:22 -0700 | [diff] [blame] | 110 | mapEventListeners.forEach((listener, executor) -> executor.execute(() -> listener.event(event))); |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 111 | }; |
| 112 | statusListener = status -> { |
| 113 | log.debug("{} status changed to {}", this.name(), status); |
| 114 | // If the status of the underlying map is SUSPENDED or INACTIVE |
| 115 | // we can no longer guarantee that the cache will be in sync. |
| 116 | if (status == SUSPENDED || status == INACTIVE) { |
| 117 | cache.invalidateAll(); |
| 118 | } |
| 119 | }; |
Jordan Halterman | 7cdb87f | 2018-06-26 23:41:22 -0700 | [diff] [blame] | 120 | super.addListener(cacheUpdater, MoreExecutors.directExecutor()); |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 121 | super.addStatusChangeListener(statusListener); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public CompletableFuture<Boolean> containsKey(K key) { |
| 126 | return get(key).thenApply(value -> value != null && !value.value().isEmpty()); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public CompletableFuture<Boolean> put(K key, V value) { |
| 131 | return super.put(key, value) |
| 132 | .whenComplete((r, e) -> cache.invalidate(key)); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public CompletableFuture<Boolean> remove(K key, V value) { |
| 137 | return super.remove(key, value) |
| 138 | .whenComplete((r, e) -> cache.invalidate(key)); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public CompletableFuture<Boolean> removeAll(K key, Collection<? extends V> values) { |
| 143 | return super.removeAll(key, values) |
| 144 | .whenComplete((r, e) -> cache.invalidate(key)); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public CompletableFuture<Versioned<Collection<? extends V>>> removeAll(K key) { |
| 149 | return super.removeAll(key) |
| 150 | .whenComplete((r, e) -> cache.invalidate(key)); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public CompletableFuture<Boolean> putAll(K key, Collection<? extends V> values) { |
| 155 | return super.putAll(key, values) |
| 156 | .whenComplete((r, e) -> cache.invalidate(key)); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public CompletableFuture<Versioned<Collection<? extends V>>> replaceValues(K key, Collection<V> values) { |
| 161 | return super.replaceValues(key, values) |
| 162 | .whenComplete((r, e) -> cache.invalidate(key)); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public CompletableFuture<Void> clear() { |
| 167 | return super.clear() |
| 168 | .whenComplete((r, e) -> cache.invalidateAll()); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public CompletableFuture<Versioned<Collection<? extends V>>> get(K key) { |
| 173 | return cache.getUnchecked(key) |
| 174 | .whenComplete((r, e) -> { |
| 175 | if (e != null) { |
| 176 | cache.invalidate(key); |
| 177 | } |
| 178 | }); |
| 179 | } |
| 180 | |
| 181 | @Override |
Jordan Halterman | 7cdb87f | 2018-06-26 23:41:22 -0700 | [diff] [blame] | 182 | public CompletableFuture<Void> addListener(MultimapEventListener<K, V> listener, Executor executor) { |
| 183 | mapEventListeners.put(listener, executor); |
| 184 | return CompletableFuture.completedFuture(null); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public CompletableFuture<Void> removeListener(MultimapEventListener<K, V> listener) { |
| 189 | mapEventListeners.remove(listener); |
| 190 | return CompletableFuture.completedFuture(null); |
| 191 | } |
| 192 | |
| 193 | @Override |
Jordan Halterman | 3b13737 | 2018-04-30 14:42:41 -0700 | [diff] [blame] | 194 | public CompletableFuture<Void> destroy() { |
| 195 | super.removeStatusChangeListener(statusListener); |
| 196 | return super.destroy().thenCompose(v -> removeListener(cacheUpdater)); |
| 197 | } |
| 198 | } |