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