Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -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.resources.impl; |
| 17 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 18 | import io.atomix.copycat.client.CopycatClient; |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 19 | import io.atomix.resource.AbstractResource; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 20 | import io.atomix.resource.ResourceTypeInfo; |
| 21 | |
| 22 | import java.util.Collection; |
| 23 | import java.util.ConcurrentModificationException; |
Madan Jampani | fc98177 | 2016-02-16 09:46:42 -0800 | [diff] [blame] | 24 | import java.util.List; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 25 | import java.util.Map.Entry; |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 26 | import java.util.Properties; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 27 | import java.util.Set; |
| 28 | import java.util.concurrent.CompletableFuture; |
| 29 | import java.util.concurrent.atomic.AtomicReference; |
| 30 | import java.util.function.BiFunction; |
| 31 | import java.util.function.Predicate; |
| 32 | |
| 33 | import org.onlab.util.Match; |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 34 | import org.onosproject.store.primitives.TransactionId; |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 35 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Clear; |
| 36 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.ContainsKey; |
| 37 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.ContainsValue; |
| 38 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.EntrySet; |
| 39 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Get; |
| 40 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.IsEmpty; |
| 41 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.KeySet; |
| 42 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Listen; |
| 43 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Size; |
| 44 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.TransactionCommit; |
| 45 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.TransactionPrepare; |
| 46 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.TransactionRollback; |
| 47 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Unlisten; |
| 48 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.UpdateAndGet; |
| 49 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Values; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 50 | import org.onosproject.store.service.AsyncConsistentMap; |
| 51 | import org.onosproject.store.service.MapEvent; |
| 52 | import org.onosproject.store.service.MapEventListener; |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 53 | import org.onosproject.store.service.MapTransaction; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 54 | import org.onosproject.store.service.Versioned; |
| 55 | |
| 56 | import com.google.common.collect.Sets; |
| 57 | |
| 58 | /** |
| 59 | * Distributed resource providing the {@link AsyncConsistentMap} primitive. |
| 60 | */ |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 61 | @ResourceTypeInfo(id = -151, factory = AtomixConsistentMapFactory.class) |
| 62 | public class AtomixConsistentMap extends AbstractResource<AtomixConsistentMap> |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 63 | implements AsyncConsistentMap<String, byte[]> { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 64 | |
| 65 | private final Set<MapEventListener<String, byte[]>> mapEventListeners = Sets.newCopyOnWriteArraySet(); |
| 66 | |
Madan Jampani | fc98177 | 2016-02-16 09:46:42 -0800 | [diff] [blame] | 67 | public static final String CHANGE_SUBJECT = "changeEvents"; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 68 | |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 69 | public AtomixConsistentMap(CopycatClient client, Properties properties) { |
| 70 | super(client, properties); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public String name() { |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public CompletableFuture<AtomixConsistentMap> open() { |
| 80 | return super.open().thenApply(result -> { |
Madan Jampani | 3a9911c | 2016-02-21 11:25:45 -0800 | [diff] [blame] | 81 | client.onEvent(CHANGE_SUBJECT, this::handleEvent); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 82 | return result; |
| 83 | }); |
| 84 | } |
| 85 | |
Madan Jampani | fc98177 | 2016-02-16 09:46:42 -0800 | [diff] [blame] | 86 | private void handleEvent(List<MapEvent<String, byte[]>> events) { |
| 87 | events.forEach(event -> mapEventListeners.forEach(listener -> listener.event(event))); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public CompletableFuture<Boolean> isEmpty() { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 92 | return submit(new IsEmpty()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public CompletableFuture<Integer> size() { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 97 | return submit(new Size()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public CompletableFuture<Boolean> containsKey(String key) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 102 | return submit(new ContainsKey(key)); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public CompletableFuture<Boolean> containsValue(byte[] value) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 107 | return submit(new ContainsValue(value)); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public CompletableFuture<Versioned<byte[]>> get(String key) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 112 | return submit(new Get(key)); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public CompletableFuture<Set<String>> keySet() { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 117 | return submit(new KeySet()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public CompletableFuture<Collection<Versioned<byte[]>>> values() { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 122 | return submit(new Values()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public CompletableFuture<Set<Entry<String, Versioned<byte[]>>>> entrySet() { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 127 | return submit(new EntrySet()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | @SuppressWarnings("unchecked") |
| 132 | public CompletableFuture<Versioned<byte[]>> put(String key, byte[] value) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 133 | return submit(new UpdateAndGet(key, value, Match.ANY, Match.ANY)) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 134 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 135 | .thenApply(v -> v.oldValue()); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | @SuppressWarnings("unchecked") |
| 140 | public CompletableFuture<Versioned<byte[]>> putAndGet(String key, byte[] value) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 141 | return submit(new UpdateAndGet(key, value, Match.ANY, Match.ANY)) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 142 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 143 | .thenApply(v -> v.newValue()); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | @SuppressWarnings("unchecked") |
| 148 | public CompletableFuture<Versioned<byte[]>> putIfAbsent(String key, byte[] value) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 149 | return submit(new UpdateAndGet(key, value, Match.NULL, Match.ANY)) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 150 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 151 | .thenApply(v -> v.oldValue()); |
| 152 | } |
| 153 | @Override |
| 154 | @SuppressWarnings("unchecked") |
| 155 | public CompletableFuture<Versioned<byte[]>> remove(String key) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 156 | return submit(new UpdateAndGet(key, null, Match.ANY, Match.ANY)) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 157 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 158 | .thenApply(v -> v.oldValue()); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | @SuppressWarnings("unchecked") |
| 163 | public CompletableFuture<Boolean> remove(String key, byte[] value) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 164 | return submit(new UpdateAndGet(key, null, Match.ifValue(value), Match.ANY)) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 165 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 166 | .thenApply(v -> v.updated()); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | @SuppressWarnings("unchecked") |
| 171 | public CompletableFuture<Boolean> remove(String key, long version) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 172 | return submit(new UpdateAndGet(key, null, Match.ANY, Match.ifValue(version))) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 173 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 174 | .thenApply(v -> v.updated()); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | @SuppressWarnings("unchecked") |
| 179 | public CompletableFuture<Versioned<byte[]>> replace(String key, byte[] value) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 180 | return submit(new UpdateAndGet(key, value, Match.NOT_NULL, Match.ANY)) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 181 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 182 | .thenApply(v -> v.oldValue()); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | @SuppressWarnings("unchecked") |
| 187 | public CompletableFuture<Boolean> replace(String key, byte[] oldValue, byte[] newValue) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 188 | return submit(new UpdateAndGet(key, |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 189 | newValue, |
| 190 | Match.ifValue(oldValue), |
| 191 | Match.ANY)) |
| 192 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 193 | .thenApply(v -> v.updated()); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | @SuppressWarnings("unchecked") |
| 198 | public CompletableFuture<Boolean> replace(String key, long oldVersion, byte[] newValue) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 199 | return submit(new UpdateAndGet(key, |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 200 | newValue, |
| 201 | Match.ANY, |
| 202 | Match.ifValue(oldVersion))) |
| 203 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 204 | .thenApply(v -> v.updated()); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public CompletableFuture<Void> clear() { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 209 | return submit(new Clear()) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 210 | .whenComplete((r, e) -> throwIfLocked(r)) |
| 211 | .thenApply(v -> null); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | @SuppressWarnings("unchecked") |
| 216 | public CompletableFuture<Versioned<byte[]>> computeIf(String key, |
| 217 | Predicate<? super byte[]> condition, |
| 218 | BiFunction<? super String, ? super byte[], ? extends byte[]> remappingFunction) { |
| 219 | return get(key).thenCompose(r1 -> { |
| 220 | byte[] existingValue = r1 == null ? null : r1.value(); |
| 221 | // if the condition evaluates to false, return existing value. |
| 222 | if (!condition.test(existingValue)) { |
| 223 | return CompletableFuture.completedFuture(r1); |
| 224 | } |
| 225 | |
| 226 | AtomicReference<byte[]> computedValue = new AtomicReference<>(); |
| 227 | // if remappingFunction throws an exception, return the exception. |
| 228 | try { |
| 229 | computedValue.set(remappingFunction.apply(key, existingValue)); |
| 230 | } catch (Exception e) { |
| 231 | CompletableFuture<Versioned<byte[]>> future = new CompletableFuture<>(); |
| 232 | future.completeExceptionally(e); |
| 233 | return future; |
| 234 | } |
| 235 | if (computedValue.get() == null && r1 == null) { |
| 236 | return CompletableFuture.completedFuture(null); |
| 237 | } |
| 238 | Match<byte[]> valueMatch = r1 == null ? Match.NULL : Match.ANY; |
| 239 | Match<Long> versionMatch = r1 == null ? Match.ANY : Match.ifValue(r1.version()); |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 240 | return submit(new UpdateAndGet(key, |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 241 | computedValue.get(), |
| 242 | valueMatch, |
| 243 | versionMatch)) |
| 244 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 245 | .thenApply(v -> v.newValue()); |
| 246 | }); |
| 247 | } |
| 248 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 249 | @Override |
| 250 | public synchronized CompletableFuture<Void> addListener(MapEventListener<String, byte[]> listener) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 251 | if (mapEventListeners.isEmpty()) { |
| 252 | return submit(new Listen()).thenRun(() -> mapEventListeners.add(listener)); |
| 253 | } else { |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 254 | mapEventListeners.add(listener); |
| 255 | return CompletableFuture.completedFuture(null); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 256 | } |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public synchronized CompletableFuture<Void> removeListener(MapEventListener<String, byte[]> listener) { |
| 261 | if (mapEventListeners.remove(listener) && mapEventListeners.isEmpty()) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 262 | return submit(new Unlisten()).thenApply(v -> null); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 263 | } |
| 264 | return CompletableFuture.completedFuture(null); |
| 265 | } |
| 266 | |
| 267 | private void throwIfLocked(MapEntryUpdateResult.Status status) { |
| 268 | if (status == MapEntryUpdateResult.Status.WRITE_LOCK) { |
| 269 | throw new ConcurrentModificationException("Cannot update map: Another transaction in progress"); |
| 270 | } |
| 271 | } |
| 272 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 273 | @Override |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 274 | public CompletableFuture<Boolean> prepare(MapTransaction<String, byte[]> transaction) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 275 | return submit(new TransactionPrepare(transaction)).thenApply(v -> v == PrepareResult.OK); |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | @Override |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 279 | public CompletableFuture<Void> commit(TransactionId transactionId) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 280 | return submit(new TransactionCommit(transactionId)).thenApply(v -> null); |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | @Override |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 284 | public CompletableFuture<Void> rollback(TransactionId transactionId) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 285 | return submit(new TransactionRollback(transactionId)) |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 286 | .thenApply(v -> null); |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 287 | } |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 288 | } |