BUG-7390: fix dsbenchmark
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / txchain / TxchainDomWrite.java
1 /*
2  * Copyright (c) 2015 Cisco Systems and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.dsbenchmark.txchain;
10
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.List;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
21 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
22 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
23 import org.opendaylight.dsbenchmark.DomListBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
30 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class TxchainDomWrite extends DatastoreAbstractWriter implements TransactionChainListener {
35     private static final Logger LOG = LoggerFactory.getLogger(TxchainDomWrite.class);
36     private final DOMDataBroker domDataBroker;
37     private List<MapEntryNode> list;
38
39     public TxchainDomWrite(final DOMDataBroker domDataBroker, final StartTestInput.Operation oper, final int outerListElem,
40             final int innerListElem, final long writesPerTx, final DataStore dataStore) {
41         super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
42         this.domDataBroker = domDataBroker;
43         LOG.info("Created TxchainDomWrite");
44     }
45
46     @Override
47     public void createList() {
48         list = DomListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
49     }
50
51     @Override
52     public void executeList() {
53         final LogicalDatastoreType dsType = getDataStoreType();
54         final YangInstanceIdentifier pid =
55                 YangInstanceIdentifier.builder().node(TestExec.QNAME).node(OuterList.QNAME).build();
56         final DOMTransactionChain chain = domDataBroker.createTransactionChain(this);
57
58         DOMDataWriteTransaction tx = chain.newWriteOnlyTransaction();
59         int txSubmitted = 0;
60         int writeCnt = 0;
61
62         for (MapEntryNode element : this.list) {
63             YangInstanceIdentifier yid =
64                     pid.node(new NodeIdentifierWithPredicates(OuterList.QNAME, element.getIdentifier().getKeyValues()));
65
66             if (oper == StartTestInput.Operation.PUT) {
67                 tx.put(dsType, yid, element);
68             } else {
69                 tx.merge(dsType, yid, element);
70             }
71
72             writeCnt++;
73
74             // Start performing the operation; submit the transaction at every n-th operation
75             if (writeCnt == writesPerTx) {
76                 txSubmitted++;
77                 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
78                     @Override
79                     public void onSuccess(final Void result) {
80                         txOk++;
81                     }
82
83                     @Override
84                     public void onFailure(final Throwable t) {
85                         LOG.error("Transaction failed, {}", t);
86                         txError++;
87                     }
88                 });
89                 tx = chain.newWriteOnlyTransaction();
90                 writeCnt = 0;
91             }
92         }
93
94         // *** Clean up and close the transaction chain ***
95         // Submit the outstanding transaction even if it's empty and wait for it to finish
96         // We need to empty the transaction chain before closing it
97
98         try {
99             txSubmitted++;
100             tx.submit().checkedGet();
101             txOk++;
102         } catch (TransactionCommitFailedException e) {
103             LOG.error("Transaction failed", e);
104             txError++;
105         }
106         try {
107             chain.close();
108         } catch (IllegalStateException e) {
109             LOG.error("Transaction close failed,", e);
110         }
111
112         LOG.info("Transactions: submitted {}, completed {}", txSubmitted, (txOk + txError));
113     }
114
115     @Override
116     public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
117             final AsyncTransaction<?, ?> transaction, final Throwable cause) {
118         LOG.error("Broken chain {} in TxchainDomWrite, transaction {}, cause {}",
119                 chain, transaction.getIdentifier(), cause);
120     }
121
122     @Override
123     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
124         LOG.info("Chain {} closed successfully", chain);
125     }
126
127 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.