Madan Jampani

Fix AtomixConsistentMap::computeIf behavior to throw a ConcurrentModificationExc…

…eption when update races are detected

Change-Id: I6d2ab6b9864649e89156660949160261c511058a
...@@ -35,6 +35,7 @@ import java.util.function.Consumer; ...@@ -35,6 +35,7 @@ import java.util.function.Consumer;
35 import java.util.function.Predicate; 35 import java.util.function.Predicate;
36 36
37 import org.onlab.util.Match; 37 import org.onlab.util.Match;
38 +import org.onlab.util.Tools;
38 import org.onosproject.store.primitives.TransactionId; 39 import org.onosproject.store.primitives.TransactionId;
39 import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Clear; 40 import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Clear;
40 import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.ContainsKey; 41 import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.ContainsKey;
...@@ -53,6 +54,7 @@ import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapComman ...@@ -53,6 +54,7 @@ import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapComman
53 import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.UpdateAndGet; 54 import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.UpdateAndGet;
54 import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Values; 55 import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Values;
55 import org.onosproject.store.service.AsyncConsistentMap; 56 import org.onosproject.store.service.AsyncConsistentMap;
57 +import org.onosproject.store.service.ConsistentMapException;
56 import org.onosproject.store.service.MapEvent; 58 import org.onosproject.store.service.MapEvent;
57 import org.onosproject.store.service.MapEventListener; 59 import org.onosproject.store.service.MapEventListener;
58 import org.onosproject.store.service.MapTransaction; 60 import org.onosproject.store.service.MapTransaction;
...@@ -245,11 +247,18 @@ public class AtomixConsistentMap extends AbstractResource<AtomixConsistentMap> ...@@ -245,11 +247,18 @@ public class AtomixConsistentMap extends AbstractResource<AtomixConsistentMap>
245 Match<byte[]> valueMatch = r1 == null ? Match.NULL : Match.ANY; 247 Match<byte[]> valueMatch = r1 == null ? Match.NULL : Match.ANY;
246 Match<Long> versionMatch = r1 == null ? Match.ANY : Match.ifValue(r1.version()); 248 Match<Long> versionMatch = r1 == null ? Match.ANY : Match.ifValue(r1.version());
247 return client.submit(new UpdateAndGet(key, 249 return client.submit(new UpdateAndGet(key,
248 - computedValue.get(), 250 + computedValue.get(),
249 - valueMatch, 251 + valueMatch,
250 - versionMatch)) 252 + versionMatch))
251 - .whenComplete((r, e) -> throwIfLocked(r.status())) 253 + .whenComplete((r, e) -> throwIfLocked(r.status()))
252 - .thenApply(v -> v.newValue()); 254 + .thenCompose(r -> {
255 + if (r.status() == MapEntryUpdateResult.Status.PRECONDITION_FAILED ||
256 + r.status() == MapEntryUpdateResult.Status.WRITE_LOCK) {
257 + return Tools.exceptionalFuture(new ConsistentMapException.ConcurrentModification());
258 + }
259 + return CompletableFuture.completedFuture(r);
260 + })
261 + .thenApply(v -> v.newValue());
253 }); 262 });
254 } 263 }
255 264
......