0543e382dfbdbb951c7b879f6ddb96f7da46728f
[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 package org.opendaylight.dsbenchmark.txchain;
9
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.MoreExecutors;
12 import java.util.List;
13 import java.util.concurrent.ExecutionException;
14 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
15 import org.opendaylight.dsbenchmark.DomListBuilder;
16 import org.opendaylight.mdsal.common.api.CommitInfo;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeTransaction;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
21 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
22 import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class TxchainDomWrite extends DatastoreAbstractWriter implements DOMTransactionChainListener {
34     private static final Logger LOG = LoggerFactory.getLogger(TxchainDomWrite.class);
35     private final DOMDataBroker domDataBroker;
36     private List<MapEntryNode> list;
37
38     public TxchainDomWrite(final DOMDataBroker domDataBroker, final StartTestInput.Operation oper,
39             final int outerListElem, final int innerListElem, final long writesPerTx, final DataStore dataStore) {
40         super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
41         this.domDataBroker = domDataBroker;
42         LOG.debug("Created TxchainDomWrite");
43     }
44
45     @Override
46     public void createList() {
47         list = DomListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
48     }
49
50     @Override
51     public void executeList() {
52         final LogicalDatastoreType dsType = getDataStoreType();
53         final YangInstanceIdentifier pid =
54                 YangInstanceIdentifier.builder().node(TestExec.QNAME).node(OuterList.QNAME).build();
55         final DOMTransactionChain chain = domDataBroker.createMergingTransactionChain(this);
56
57         DOMDataTreeWriteTransaction tx = chain.newWriteOnlyTransaction();
58         int txSubmitted = 0;
59         int writeCnt = 0;
60
61         for (MapEntryNode element : this.list) {
62             YangInstanceIdentifier yid =
63                     pid.node(NodeIdentifierWithPredicates.of(OuterList.QNAME, element.getIdentifier().getKeyValues()));
64
65             if (oper == StartTestInput.Operation.PUT) {
66                 tx.put(dsType, yid, element);
67             } else {
68                 tx.merge(dsType, yid, element);
69             }
70
71             writeCnt++;
72
73             // Start performing the operation; submit the transaction at every n-th operation
74             if (writeCnt == writesPerTx) {
75                 txSubmitted++;
76                 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
77                     @Override
78                     public void onSuccess(final CommitInfo result) {
79                         txOk++;
80                     }
81
82                     @Override
83                     public void onFailure(final Throwable cause) {
84                         LOG.error("Transaction failed", cause);
85                         txError++;
86                     }
87                 }, MoreExecutors.directExecutor());
88                 tx = chain.newWriteOnlyTransaction();
89                 writeCnt = 0;
90             }
91         }
92
93         // *** Clean up and close the transaction chain ***
94         // Submit the outstanding transaction even if it's empty and wait for it to finish
95         // We need to empty the transaction chain before closing it
96
97         try {
98             txSubmitted++;
99             tx.commit().get();
100             txOk++;
101         } catch (final InterruptedException | ExecutionException e) {
102             LOG.error("Transaction failed", e);
103             txError++;
104         }
105         try {
106             chain.close();
107         } catch (final IllegalStateException e) {
108             LOG.error("Transaction close failed,", e);
109         }
110
111         LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, txOk + txError);
112     }
113
114     @Override
115     public void onTransactionChainFailed(final DOMTransactionChain chain, final DOMDataTreeTransaction transaction,
116             final Throwable cause) {
117         LOG.error("Broken chain {} in TxchainDomWrite, transaction {}, cause {}", chain, transaction.getIdentifier(),
118             cause);
119     }
120
121     @Override
122     public void onTransactionChainSuccessful(final DOMTransactionChain chain) {
123         LOG.debug("Chain {} closed successfully", chain);
124     }
125 }

©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.