Move initial list creation to create-prefix-shard. 42/56542/4
authorTomas Cere <tcere@cisco.com>
Wed, 3 May 2017 13:21:00 +0000 (15:21 +0200)
committerRobert Varga <nite@hq.sk>
Sat, 6 May 2017 10:02:22 +0000 (10:02 +0000)
This move the initial list population of produce-transactions
to create-prefix-shard rpc with 3 hardcoded prefixes(prefix-1,prefix-2,prefix-3)
so that csit suites can populate the id-int list just once when the shard is created
and produce-transactions can now run parallely on multiple entries from
multiple nodes.

Change-Id: If70990c0e217cd68027ae960a7545c69acf52cdb
Signed-off-by: Tomas Cere <tcere@cisco.com>
(cherry picked from commit 74175c48bb2b3ee786108bdda8e665484080b7f5)

opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/MdsalLowLevelTestProvider.java
opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/PrefixShardHandler.java
opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/ProduceTransactionsHandler.java

index 8ebac1c01e88c07da64324e4eb9d2f507a81e299..8f67f136edb13f557f75f173c06ea299559c287f 100644 (file)
@@ -160,7 +160,8 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService
 
         registration = rpcRegistry.addRpcImplementation(OdlMdsalLowlevelControlService.class, this);
 
-        prefixShardHandler = new PrefixShardHandler(distributedShardFactory, bindingNormalizedNodeSerializer);
+        prefixShardHandler = new PrefixShardHandler(distributedShardFactory, domDataTreeService,
+                bindingNormalizedNodeSerializer);
     }
 
     @Override
index 22e7700d5cf6166984facd393398eec83b0fb7f8..8f711b3337c9fbdfc8087aa138df3d5a2712d007 100644 (file)
@@ -8,6 +8,13 @@
 
 package org.opendaylight.controller.clustering.it.provider.impl;
 
+import static org.opendaylight.controller.clustering.it.provider.impl.ProduceTransactionsHandler.ID;
+import static org.opendaylight.controller.clustering.it.provider.impl.ProduceTransactionsHandler.ID_INT;
+import static org.opendaylight.controller.clustering.it.provider.impl.ProduceTransactionsHandler.ID_INTS;
+import static org.opendaylight.controller.clustering.it.provider.impl.ProduceTransactionsHandler.ITEM;
+
+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;
@@ -16,36 +23,55 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.CompletionStage;
 import java.util.stream.Collectors;
+import javax.annotation.Nullable;
 import org.opendaylight.controller.cluster.access.concepts.MemberName;
 import org.opendaylight.controller.cluster.sharding.DistributedShardFactory;
 import org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+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.DOMDataTreeProducerException;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingConflictException;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.CreatePrefixShardInput;
+import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.ProduceTransactionsOutput;
 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RemovePrefixShardInput;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class PrefixShardHandler {
 
     private static final Logger LOG = LoggerFactory.getLogger(PrefixShardHandler.class);
+    private static final int MAX_PREFIX = 4;
+    private static final String PREFIX_TEMPLATE = "prefix-";
 
     private final DistributedShardFactory shardFactory;
+    private final DOMDataTreeService domDataTreeService;
     private final BindingNormalizedNodeSerializer serializer;
 
     private final Map<YangInstanceIdentifier, DistributedShardRegistration> registrations =
             Collections.synchronizedMap(new HashMap<>());
 
     public PrefixShardHandler(final DistributedShardFactory shardFactory,
+                              final DOMDataTreeService domDataTreeService,
                               final BindingNormalizedNodeSerializer serializer) {
 
         this.shardFactory = shardFactory;
+        this.domDataTreeService = domDataTreeService;
         this.serializer = serializer;
     }
 
@@ -64,7 +90,25 @@ public class PrefixShardHandler {
             completionStage.thenAccept(registration -> {
                 LOG.debug("Shard[{}] created successfully.", identifier);
                 registrations.put(identifier, registration);
-                future.set(RpcResultBuilder.<Void>success().build());
+
+                final CheckedFuture<Void, TransactionCommitFailedException> ensureFuture = ensureListExists();
+                Futures.addCallback(ensureFuture, new FutureCallback<Void>() {
+                    @Override
+                    public void onSuccess(@Nullable Void result) {
+                        LOG.debug("Initial list write successful.");
+                        future.set(RpcResultBuilder.<Void>success().build());
+                    }
+
+                    @Override
+                    public void onFailure(Throwable throwable) {
+                        LOG.warn("Shard[{}] creation failed:", identifier, throwable);
+
+                        final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed",
+                                "Shard creation failed", "cluster-test-app", "", throwable);
+                        future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
+                    }
+                });
+
             });
             completionStage.exceptionally(throwable -> {
                 LOG.warn("Shard[{}] creation failed:", identifier, throwable);
@@ -111,4 +155,54 @@ public class PrefixShardHandler {
 
         return future;
     }
+
+    private CheckedFuture<Void, TransactionCommitFailedException> ensureListExists() {
+
+        final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = ImmutableNodes.mapNodeBuilder(ID_INT);
+
+        // hardcoded initial list population for parallel produce-transactions testing on multiple nodes
+        for (int i = 1; i < MAX_PREFIX; i++) {
+            mapBuilder.withChild(
+                    ImmutableNodes.mapEntryBuilder(ID_INT, ID, PREFIX_TEMPLATE + i)
+                            .withChild(ImmutableNodes.mapNodeBuilder(ITEM).build())
+                            .build());
+        }
+        final MapNode mapNode = mapBuilder.build();
+
+        final ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(ID_INTS))
+                .withChild(mapNode)
+                .build();
+
+        final DOMDataTreeProducer producer = domDataTreeService.createProducer(Collections.singleton(
+                new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY)));
+
+        final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
+
+        final DOMDataTreeWriteCursor cursor =
+                tx.createCursor(new DOMDataTreeIdentifier(
+                        LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY));
+
+        cursor.merge(containerNode.getIdentifier(), containerNode);
+        cursor.close();
+
+        final CheckedFuture<Void, TransactionCommitFailedException> future = tx.submit();
+        Futures.addCallback(future, new FutureCallback<Void>() {
+            @Override
+            public void onSuccess(@Nullable Void result) {
+                try {
+                    LOG.debug("Closing producer for initial list.");
+                    producer.close();
+                } catch (DOMDataTreeProducerException e) {
+                    LOG.warn("Error while closing producer.", e);
+                }
+            }
+
+            @Override
+            public void onFailure(Throwable throwable) {
+                //NOOP handled by the caller of this method.
+            }
+        });
+        return future;
+    }
 }
index 917f6857694bc780b827f0273ddffd8907f7bbe5..8aa2dfb03af606f91372b9f3b12745c62b6de908 100644 (file)
@@ -58,13 +58,13 @@ public class ProduceTransactionsHandler implements Runnable {
     //2^20 as in the model
     private static final int MAX_ITEM = 1048576;
 
-    private static final QName ID_INTS =
+    static final QName ID_INTS =
             QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target", "2017-02-15", "id-ints");
-    private static final QName ID_INT =
+    static final QName ID_INT =
             QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target", "2017-02-15", "id-int");
-    private static final QName ID =
+    static final QName ID =
             QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target", "2017-02-15", "id");
-    private static final QName ITEM =
+    static final QName ITEM =
             QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target", "2017-02-15", "item");
     private static final QName NUMBER =
             QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target", "2017-02-15", "number");
@@ -90,8 +90,8 @@ public class ProduceTransactionsHandler implements Runnable {
     private long insertTx = 0;
     private long deleteTx = 0;
     private ScheduledFuture<?> scheduledFuture;
-    private YangInstanceIdentifier idListWithKey;
     private DOMDataTreeProducer itemProducer;
+    private YangInstanceIdentifier idListWithKey;
 
     public ProduceTransactionsHandler(final DOMDataTreeService domDataTreeService,
                                       final ProduceTransactionsInput input) {
@@ -115,7 +115,7 @@ public class ProduceTransactionsHandler implements Runnable {
     public void start(final SettableFuture<RpcResult<ProduceTransactionsOutput>> settableFuture) {
         completionFuture = settableFuture;
 
-        if (ensureListExists(completionFuture) && fillInitialList(completionFuture)) {
+        if (fillInitialList(completionFuture)) {
             startTime = System.nanoTime();
             scheduledFuture = executor.scheduleAtFixedRate(this, 0, delay, TimeUnit.NANOSECONDS);
         } else {
@@ -123,53 +123,6 @@ public class ProduceTransactionsHandler implements Runnable {
         }
     }
 
-    private boolean ensureListExists(final SettableFuture<RpcResult<ProduceTransactionsOutput>> settableFuture) {
-
-        final MapEntryNode entry = ImmutableNodes.mapEntryBuilder(ID_INT, ID, id)
-                .withChild(ImmutableNodes.mapNodeBuilder(ITEM).build())
-                .build();
-        final MapNode mapNode =
-                ImmutableNodes.mapNodeBuilder(ID_INT)
-                        .withChild(entry)
-                        .build();
-
-        final ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
-                .withNodeIdentifier(new NodeIdentifier(ID_INTS))
-                .withChild(mapNode)
-                .build();
-
-        final DOMDataTreeProducer producer = domDataTreeService.createProducer(Collections.singleton(
-                new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY)));
-
-        final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
-
-        final DOMDataTreeWriteCursor cursor =
-                tx.createCursor(new DOMDataTreeIdentifier(
-                        LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY));
-
-        idListWithKey = ID_INT_YID.node(entry.getIdentifier());
-
-        cursor.merge(containerNode.getIdentifier(), containerNode);
-        cursor.close();
-
-        try {
-            tx.submit().checkedGet();
-        } catch (TransactionCommitFailedException e) {
-            LOG.warn("Unable to ensure IdInts list for id: {} exists.", id, e);
-            settableFuture.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
-                    .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
-            return false;
-        } finally {
-            try {
-                producer.close();
-            } catch (DOMDataTreeProducerException e) {
-                LOG.warn("Error while closing producer.", e);
-            }
-        }
-
-        return true;
-    }
-
     private boolean fillInitialList(final SettableFuture<RpcResult<ProduceTransactionsOutput>> settableFuture) {
         LOG.debug("Filling the item list with initial values.");
 
@@ -179,6 +132,8 @@ public class ProduceTransactionsHandler implements Runnable {
             mapBuilder.withChild(ImmutableNodes.mapEntry(ITEM, NUMBER, i));
         }
 
+        idListWithKey = ID_INT_YID.node(new NodeIdentifierWithPredicates(ID_INT, ID, id));
+
         itemProducer = domDataTreeService.createProducer(
                 Collections.singleton(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, idListWithKey)));
 
@@ -250,6 +205,12 @@ public class ProduceTransactionsHandler implements Runnable {
                             .setDeleteTx(deleteTx)
                             .build();
 
+                    try {
+                        itemProducer.close();
+                    } catch (final DOMDataTreeProducerException e) {
+                        LOG.warn("Failure while closing item producer.", e);
+                    }
+
                     completionFuture.set(RpcResultBuilder.<ProduceTransactionsOutput>success()
                             .withResult(output).build());