Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import java.util.Collection; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | import java.util.Map.Entry; |
| 24 | import java.util.Set; |
| 25 | import java.util.TreeMap; |
| 26 | import java.util.concurrent.CompletableFuture; |
| 27 | import java.util.concurrent.atomic.AtomicBoolean; |
| 28 | import java.util.concurrent.atomic.AtomicInteger; |
| 29 | import java.util.function.BiFunction; |
| 30 | import java.util.function.Predicate; |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 31 | import java.util.stream.Collectors; |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 32 | |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 33 | import org.onlab.util.Tools; |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 34 | import org.onosproject.cluster.PartitionId; |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 35 | import org.onosproject.store.primitives.MapUpdate; |
| 36 | import org.onosproject.store.primitives.TransactionId; |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 37 | import org.onosproject.store.service.AsyncConsistentMap; |
| 38 | import org.onosproject.store.service.MapEventListener; |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 39 | import org.onosproject.store.service.MapTransaction; |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 40 | import org.onosproject.store.service.Versioned; |
| 41 | |
| 42 | import com.google.common.collect.Lists; |
| 43 | import com.google.common.collect.Maps; |
| 44 | import com.google.common.collect.Sets; |
| 45 | |
| 46 | /** |
| 47 | * {@link AsyncConsistentMap} that has its entries partitioned horizontally across |
| 48 | * several {@link AsyncConsistentMap maps}. |
| 49 | * |
| 50 | * @param <K> key type |
| 51 | * @param <V> value type |
| 52 | */ |
| 53 | public class PartitionedAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V> { |
| 54 | |
| 55 | private final String name; |
| 56 | private final TreeMap<PartitionId, AsyncConsistentMap<K, V>> partitions = Maps.newTreeMap(); |
| 57 | private final Hasher<K> keyHasher; |
| 58 | |
| 59 | public PartitionedAsyncConsistentMap(String name, |
| 60 | Map<PartitionId, AsyncConsistentMap<K, V>> partitions, |
| 61 | Hasher<K> keyHasher) { |
| 62 | this.name = name; |
| 63 | this.partitions.putAll(checkNotNull(partitions)); |
| 64 | this.keyHasher = checkNotNull(keyHasher); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public String name() { |
| 69 | return name; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public CompletableFuture<Integer> size() { |
| 74 | AtomicInteger totalSize = new AtomicInteger(0); |
| 75 | return CompletableFuture.allOf(getMaps() |
| 76 | .stream() |
| 77 | .map(map -> map.size().thenAccept(totalSize::addAndGet)) |
| 78 | .toArray(CompletableFuture[]::new)) |
| 79 | .thenApply(v -> totalSize.get()); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public CompletableFuture<Boolean> isEmpty() { |
| 84 | return size().thenApply(size -> size == 0); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public CompletableFuture<Boolean> containsKey(K key) { |
| 89 | return getMap(key).containsKey(key); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public CompletableFuture<Boolean> containsValue(V value) { |
| 94 | AtomicBoolean contains = new AtomicBoolean(false); |
| 95 | return CompletableFuture.allOf(getMaps().stream() |
| 96 | .map(map -> map.containsValue(value) |
| 97 | .thenAccept(v -> contains.set(contains.get() || v))) |
| 98 | .toArray(CompletableFuture[]::new)) |
| 99 | .thenApply(v -> contains.get()); |
| 100 | } |
| 101 | @Override |
| 102 | public CompletableFuture<Versioned<V>> get(K key) { |
| 103 | return getMap(key).get(key); |
| 104 | } |
| 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 getMap(key).computeIf(key, condition, remappingFunction); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public CompletableFuture<Versioned<V>> put(K key, V value) { |
| 115 | return getMap(key).put(key, value); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public CompletableFuture<Versioned<V>> putAndGet(K key, V value) { |
| 120 | return getMap(key).putAndGet(key, value); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public CompletableFuture<Versioned<V>> remove(K key) { |
| 125 | return getMap(key).remove(key); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public CompletableFuture<Void> clear() { |
| 130 | return CompletableFuture.allOf(getMaps().stream() |
| 131 | .map(map -> map.clear()) |
| 132 | .toArray(CompletableFuture[]::new)); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public CompletableFuture<Set<K>> keySet() { |
| 137 | Set<K> allKeys = Sets.newConcurrentHashSet(); |
| 138 | return CompletableFuture.allOf(getMaps().stream() |
| 139 | .map(map -> map.keySet().thenAccept(allKeys::addAll)) |
| 140 | .toArray(CompletableFuture[]::new)) |
| 141 | .thenApply(v -> allKeys); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public CompletableFuture<Collection<Versioned<V>>> values() { |
| 146 | List<Versioned<V>> allValues = Lists.newCopyOnWriteArrayList(); |
| 147 | return CompletableFuture.allOf(getMaps().stream() |
| 148 | .map(map -> map.values().thenAccept(allValues::addAll)) |
| 149 | .toArray(CompletableFuture[]::new)) |
| 150 | .thenApply(v -> allValues); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet() { |
| 155 | Set<Entry<K, Versioned<V>>> allEntries = Sets.newConcurrentHashSet(); |
| 156 | return CompletableFuture.allOf(getMaps().stream() |
| 157 | .map(map -> map.entrySet().thenAccept(allEntries::addAll)) |
| 158 | .toArray(CompletableFuture[]::new)) |
| 159 | .thenApply(v -> allEntries); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public CompletableFuture<Versioned<V>> putIfAbsent(K key, V value) { |
| 164 | return getMap(key).putIfAbsent(key, value); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public CompletableFuture<Boolean> remove(K key, V value) { |
| 169 | return getMap(key).remove(key, value); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public CompletableFuture<Boolean> remove(K key, long version) { |
| 174 | return getMap(key).remove(key, version); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public CompletableFuture<Versioned<V>> replace(K key, V value) { |
| 179 | return getMap(key).replace(key, value); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) { |
| 184 | return getMap(key).replace(key, oldValue, newValue); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) { |
| 189 | return getMap(key).replace(key, oldVersion, newValue); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public CompletableFuture<Void> addListener(MapEventListener<K, V> listener) { |
| 194 | return CompletableFuture.allOf(getMaps().stream() |
| 195 | .map(map -> map.addListener(listener)) |
| 196 | .toArray(CompletableFuture[]::new)); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public CompletableFuture<Void> removeListener(MapEventListener<K, V> listener) { |
| 201 | return CompletableFuture.allOf(getMaps().stream() |
| 202 | .map(map -> map.removeListener(listener)) |
| 203 | .toArray(CompletableFuture[]::new)); |
| 204 | } |
| 205 | |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 206 | @Override |
| 207 | public CompletableFuture<Boolean> prepare(MapTransaction<K, V> transaction) { |
| 208 | |
| 209 | Map<AsyncConsistentMap<K, V>, List<MapUpdate<K, V>>> updatesGroupedByMap = Maps.newIdentityHashMap(); |
| 210 | transaction.updates().forEach(update -> { |
| 211 | AsyncConsistentMap<K, V> map = getMap(update.key()); |
| 212 | updatesGroupedByMap.computeIfAbsent(map, k -> Lists.newLinkedList()).add(update); |
| 213 | }); |
| 214 | Map<AsyncConsistentMap<K, V>, MapTransaction<K, V>> transactionsByMap = |
| 215 | Maps.transformValues(updatesGroupedByMap, |
| 216 | list -> new MapTransaction<>(transaction.transactionId(), list)); |
| 217 | |
| 218 | return Tools.allOf(transactionsByMap.entrySet() |
| 219 | .stream() |
| 220 | .map(e -> e.getKey().prepare(e.getValue())) |
| 221 | .collect(Collectors.toList())) |
| 222 | .thenApply(list -> list.stream().reduce(Boolean::logicalAnd).orElse(true)); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public CompletableFuture<Void> commit(TransactionId transactionId) { |
| 227 | return CompletableFuture.allOf(getMaps().stream() |
| 228 | .map(e -> e.commit(transactionId)) |
| 229 | .toArray(CompletableFuture[]::new)); |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public CompletableFuture<Void> rollback(TransactionId transactionId) { |
| 234 | return CompletableFuture.allOf(getMaps().stream() |
| 235 | .map(e -> e.rollback(transactionId)) |
| 236 | .toArray(CompletableFuture[]::new)); |
| 237 | } |
| 238 | |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 239 | /** |
| 240 | * Returns the map (partition) to which the specified key maps. |
| 241 | * @param key key |
| 242 | * @return AsyncConsistentMap to which key maps |
| 243 | */ |
| 244 | private AsyncConsistentMap<K, V> getMap(K key) { |
| 245 | return partitions.get(keyHasher.hash(key)); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Returns all the constituent maps. |
| 250 | * @return collection of maps. |
| 251 | */ |
| 252 | private Collection<AsyncConsistentMap<K, V>> getMaps() { |
| 253 | return partitions.values(); |
| 254 | } |
| 255 | } |