X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsamples%2Fclustering-test-app%2Fprovider%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fclustering%2Fit%2Fprovider%2Fimpl%2FProduceTransactionsHandler.java;h=7ce2b8ded6562b6df6df571ca4c87dd2f14b7348;hp=917f6857694bc780b827f0273ddffd8907f7bbe5;hb=c078e3e33bb956b1b2d59b2410f90df5d03f0a64;hpb=64dbc396e21d73a7323b3e9dbf51b31df295cfb5 diff --git a/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/ProduceTransactionsHandler.java b/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/ProduceTransactionsHandler.java index 917f685769..7ce2b8ded6 100644 --- a/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/ProduceTransactionsHandler.java +++ b/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/ProduceTransactionsHandler.java @@ -9,7 +9,6 @@ package org.opendaylight.controller.clustering.it.provider.impl; 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; @@ -19,11 +18,12 @@ import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.SplittableRandom; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; -import javax.annotation.Nullable; +import java.util.concurrent.TimeoutException; import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.mdsal.common.api.TransactionCommitFailedException; import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction; @@ -40,14 +40,11 @@ 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.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; -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; @@ -58,13 +55,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 +87,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 +112,7 @@ public class ProduceTransactionsHandler implements Runnable { public void start(final SettableFuture> 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 +120,6 @@ public class ProduceTransactionsHandler implements Runnable { } } - private boolean ensureListExists(final SettableFuture> 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.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> settableFuture) { LOG.debug("Filling the item list with initial values."); @@ -179,6 +129,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))); @@ -191,8 +143,8 @@ public class ProduceTransactionsHandler implements Runnable { cursor.close(); try { - tx.submit().checkedGet(); - } catch (final TransactionCommitFailedException e) { + tx.submit().checkedGet(125, TimeUnit.SECONDS); + } catch (final TransactionCommitFailedException | TimeoutException e) { LOG.warn("Unable to fill the initial item list.", e); settableFuture.set(RpcResultBuilder.failed() .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build()); @@ -239,32 +191,36 @@ public class ProduceTransactionsHandler implements Runnable { final ListenableFuture> allFutures = Futures.allAsList(futures); - Futures.addCallback(allFutures, new FutureCallback>() { - @Override - public void onSuccess(@Nullable final List result) { - LOG.debug("All futures completed successfully."); + try { + // Timeout from cds should be 2 minutes so leave some leeway. + allFutures.get(125, TimeUnit.SECONDS); - final ProduceTransactionsOutput output = new ProduceTransactionsOutputBuilder() - .setAllTx(allTx) - .setInsertTx(insertTx) - .setDeleteTx(deleteTx) - .build(); + LOG.debug("All futures completed successfully."); - completionFuture.set(RpcResultBuilder.success() - .withResult(output).build()); + final ProduceTransactionsOutput output = new ProduceTransactionsOutputBuilder() + .setAllTx(allTx) + .setInsertTx(insertTx) + .setDeleteTx(deleteTx) + .build(); - executor.shutdown(); - } - @Override - public void onFailure(final Throwable t) { - LOG.error("Write transactions failed.", t); - completionFuture.set(RpcResultBuilder.failed() - .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", t).build()); + completionFuture.set(RpcResultBuilder.success() + .withResult(output).build()); + + executor.shutdown(); + } catch (InterruptedException | ExecutionException | TimeoutException exception) { + LOG.error("Write transactions failed.", exception); + completionFuture.set(RpcResultBuilder.failed() + .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", exception).build()); - executor.shutdown(); + executor.shutdown(); + } finally { + try { + itemProducer.close(); + } catch (final DOMDataTreeProducerException e) { + LOG.warn("Failure while closing item producer.", e); } - }); + } } } }