Encapsulate ShardedDOMDataTreeProducer layout
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / ShardedDOMDataTreeWriteTransaction.java
index e1ad2f9795654578b6657e82717d386a9b1eb22d..53feef2dc4aa6a87c69734eaba1ce011aca8acf4 100644 (file)
@@ -8,21 +8,20 @@
 package org.opendaylight.mdsal.dom.broker;
 
 import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
+import java.util.ArrayDeque;
 import java.util.Deque;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Set;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.BiConsumer;
+import java.util.function.Consumer;
 import java.util.stream.Collectors;
 import javax.annotation.Nonnull;
 import javax.annotation.concurrent.GuardedBy;
@@ -30,11 +29,8 @@ import javax.annotation.concurrent.NotThreadSafe;
 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
-import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
-import org.opendaylight.mdsal.dom.store.inmemory.DOMDataTreeShardProducer;
 import org.opendaylight.mdsal.dom.store.inmemory.DOMDataTreeShardWriteTransaction;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.slf4j.Logger;
@@ -43,11 +39,19 @@ import org.slf4j.LoggerFactory;
 @NotThreadSafe
 final class ShardedDOMDataTreeWriteTransaction implements DOMDataTreeCursorAwareTransaction {
     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeWriteTransaction.class);
+    private static final TransactionCommitFailedExceptionMapper SUBMIT_FAILED_MAPPER =
+            TransactionCommitFailedExceptionMapper.create("submit");
     private static final AtomicLong COUNTER = new AtomicLong();
-    private final Map<DOMDataTreeIdentifier, DOMDataTreeShardWriteTransaction> idToTransaction;
+
+    private final Map<DOMDataTreeIdentifier, DOMDataTreeShardWriteTransaction> transactions;
     private final ShardedDOMDataTreeProducer producer;
+    private final ProducerLayout layout;
     private final String identifier;
-    private final Set<YangInstanceIdentifier> childBoundaries = new HashSet<>();
+
+    private final SettableFuture<Void> future = SettableFuture.create();
+    private final CheckedFuture<Void, TransactionCommitFailedException> submitFuture =
+            Futures.makeChecked(future, SUBMIT_FAILED_MAPPER);
+
     @GuardedBy("this")
     private boolean closed =  false;
 
@@ -55,27 +59,24 @@ final class ShardedDOMDataTreeWriteTransaction implements DOMDataTreeCursorAware
     private DOMDataTreeWriteCursor openCursor;
 
     ShardedDOMDataTreeWriteTransaction(final ShardedDOMDataTreeProducer producer,
-                                       final Map<DOMDataTreeIdentifier, DOMDataTreeShardProducer> idToProducer,
-                                       final Map<DOMDataTreeIdentifier, DOMDataTreeProducer> childProducers) {
+        final Map<DOMDataTreeIdentifier, DOMDataTreeShardWriteTransaction> transactions, final ProducerLayout layout) {
         this.producer = Preconditions.checkNotNull(producer);
-        idToTransaction = new HashMap<>();
-        Preconditions.checkNotNull(idToProducer).forEach((id, prod) -> idToTransaction.put(id, prod.createTransaction()));
+        this.transactions = ImmutableMap.copyOf(transactions);
+        this.layout = Preconditions.checkNotNull(layout);
         this.identifier = "SHARDED-DOM-" + COUNTER.getAndIncrement();
-        childProducers.forEach((id, prod) -> childBoundaries.add(id.getRootIdentifier()));
+        LOG.debug("Created new transaction {}", identifier);
     }
 
-    // FIXME: use atomic operations
-    @GuardedBy("this")
     private DOMDataTreeShardWriteTransaction lookup(final DOMDataTreeIdentifier prefix) {
-        for (final Entry<DOMDataTreeIdentifier, DOMDataTreeShardWriteTransaction> e : idToTransaction.entrySet()) {
+        for (final Entry<DOMDataTreeIdentifier, DOMDataTreeShardWriteTransaction> e : transactions.entrySet()) {
             if (e.getKey().contains(prefix)) {
                 Preconditions.checkArgument(!producer.isDelegatedToChild(prefix),
-                        "Path %s is delegated to child producer.",
-                        prefix);
+                    "Path %s is delegated to child producer.", prefix);
                 return e.getValue();
             }
         }
-        throw new IllegalArgumentException(String.format("Path %s is not accessible from transaction %s", prefix, this));
+
+        return null;
     }
 
     @Override
@@ -93,7 +94,7 @@ final class ShardedDOMDataTreeWriteTransaction implements DOMDataTreeCursorAware
         if (openCursor != null) {
             openCursor.close();
         }
-        for (final DOMDataTreeShardWriteTransaction tx : ImmutableSet.copyOf(idToTransaction.values())) {
+        for (final DOMDataTreeShardWriteTransaction tx : transactions.values()) {
             tx.close();
         }
 
@@ -106,7 +107,10 @@ final class ShardedDOMDataTreeWriteTransaction implements DOMDataTreeCursorAware
     public synchronized DOMDataTreeWriteCursor createCursor(final DOMDataTreeIdentifier prefix) {
         Preconditions.checkState(!closed, "Transaction is closed already");
         Preconditions.checkState(openCursor == null, "There is still a cursor open");
+
         final DOMDataTreeShardWriteTransaction lookup = lookup(prefix);
+        Preconditions.checkArgument(lookup != null, "Path %s is not accessible from transaction %s", prefix, this);
+
         openCursor = new DelegatingCursor(lookup.createCursor(prefix), prefix);
         return openCursor;
     }
@@ -116,28 +120,39 @@ final class ShardedDOMDataTreeWriteTransaction implements DOMDataTreeCursorAware
         Preconditions.checkState(!closed, "Transaction %s is already closed", identifier);
         Preconditions.checkState(openCursor == null, "Cannot submit transaction while there is a cursor open");
 
-        final Set<DOMDataTreeShardWriteTransaction> txns = ImmutableSet.copyOf(idToTransaction.values());
-        final ListenableFuture<List<Void>> listListenableFuture =
-                Futures.allAsList(txns.stream().map(tx -> {
-                    tx.ready();
-                    return tx.submit();
-                }).collect(Collectors.toList()));
+        producer.transactionSubmitted(this);
+        return submitFuture;
+    }
+
+    void doSubmit(final Consumer<ShardedDOMDataTreeWriteTransaction> success,
+            final BiConsumer<ShardedDOMDataTreeWriteTransaction, Throwable> failure) {
+
+        final ListenableFuture<List<Void>> listListenableFuture = Futures.allAsList(
+            transactions.values().stream().map(tx -> {
+                LOG.debug("Readying tx {}", identifier);
+                tx.ready();
+                return tx.submit();
+            }).collect(Collectors.toList()));
 
-        final SettableFuture<Void> ret = SettableFuture.create();
         Futures.addCallback(listListenableFuture, new FutureCallback<List<Void>>() {
             @Override
             public void onSuccess(final List<Void> result) {
-                ret.set(null);
+                success.accept(ShardedDOMDataTreeWriteTransaction.this);
             }
 
             @Override
-            public void onFailure(final Throwable t) {
-                ret.setException(t);
+            public void onFailure(final Throwable exp) {
+                failure.accept(ShardedDOMDataTreeWriteTransaction.this, exp);
             }
         });
+    }
 
-        producer.transactionSubmitted(this);
-        return Futures.makeChecked(ret, TransactionCommitFailedExceptionMapper.create("submit"));
+    void onTransactionSuccess(final Void result) {
+        future.set(result);
+    }
+
+    void onTransactionFailure(final Throwable throwable) {
+        future.setException(throwable);
     }
 
     synchronized void cursorClosed() {
@@ -145,20 +160,21 @@ final class ShardedDOMDataTreeWriteTransaction implements DOMDataTreeCursorAware
     }
 
     private class DelegatingCursor implements DOMDataTreeWriteCursor {
-
+        private final Deque<PathArgument> path = new ArrayDeque<>();
         private final DOMDataTreeWriteCursor delegate;
-        private final Deque<PathArgument> path = new LinkedList<>();
+        private final DOMDataTreeIdentifier rootPosition;
 
-        public DelegatingCursor(final DOMDataTreeWriteCursor delegate, final DOMDataTreeIdentifier rootPosition) {
-            this.delegate = delegate;
+        DelegatingCursor(final DOMDataTreeWriteCursor delegate, final DOMDataTreeIdentifier rootPosition) {
+            this.delegate = Preconditions.checkNotNull(delegate);
+            this.rootPosition = Preconditions.checkNotNull(rootPosition);
             path.addAll(rootPosition.getRootIdentifier().getPathArguments());
         }
 
         @Override
         public void enter(@Nonnull final PathArgument child) {
             checkAvailable(child);
-            path.push(child);
             delegate.enter(child);
+            path.push(child);
         }
 
         @Override
@@ -177,20 +193,26 @@ final class ShardedDOMDataTreeWriteTransaction implements DOMDataTreeCursorAware
 
         @Override
         public void exit() {
-            path.pop();
             delegate.exit();
+            path.pop();
         }
 
         @Override
         public void exit(final int depth) {
+            delegate.exit(depth);
             for (int i = 0; i < depth; i++) {
                 path.pop();
             }
-            delegate.exit(depth);
         }
 
         @Override
         public void close() {
+            int depthEntered = path.size() - rootPosition.getRootIdentifier().getPathArguments().size();
+            if (depthEntered > 0) {
+                // clean up existing modification cursor in case this tx will be reused for batching
+                delegate.exit(depthEntered);
+            }
+
             delegate.close();
             cursorClosed();
         }
@@ -214,15 +236,11 @@ final class ShardedDOMDataTreeWriteTransaction implements DOMDataTreeCursorAware
         }
 
         void checkAvailable(final PathArgument child) {
-            path.add(child);
-            final YangInstanceIdentifier yid = YangInstanceIdentifier.create(path);
-            childBoundaries.forEach(id -> {
-                if (id.contains(yid)) {
-                    path.removeLast();
-                    throw new IllegalArgumentException("Path {" + yid + "} is not available to this cursor since it's already claimed by a child producer");
-                }
-            });
-            path.removeLast();
+            layout.checkAvailable(path, child);
         }
     }
+
+    ProducerLayout getLayout() {
+        return layout;
+    }
 }