BUG-7390: fix dsbenchmark
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / txchain / TxchainBaWrite.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.binding.api.BindingTransactionChain;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.dsbenchmark.BaListBuilder;
23 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
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.StartTestInput.Operation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class TxchainBaWrite extends DatastoreAbstractWriter implements TransactionChainListener {
34     private static final Logger LOG = LoggerFactory.getLogger(TxchainBaWrite.class);
35     private final DataBroker bindingDataBroker;
36     private List<OuterList> list;
37
38     public TxchainBaWrite(final DataBroker bindingDataBroker, final Operation oper,
39                           final int outerListElem, final int innerListElem, final long writesPerTx, final DataStore dataStore) {
40         super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
41         this.bindingDataBroker = bindingDataBroker;
42         LOG.info("Created TxchainBaWrite");
43     }
44
45     @Override
46     public void createList() {
47         list = BaListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
48     }
49
50     @Override
51     public void executeList() {
52         final BindingTransactionChain chain = bindingDataBroker.createTransactionChain(this);
53         final LogicalDatastoreType dsType = getDataStoreType();
54
55         WriteTransaction tx = chain.newWriteOnlyTransaction();
56         int txSubmitted = 0;
57         int writeCnt = 0;
58
59         for (OuterList element : this.list) {
60             InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
61                                                     .child(OuterList.class, element.getKey());
62
63             if (oper == StartTestInput.Operation.PUT) {
64                 tx.put(dsType, iid, element);
65             } else {
66                 tx.merge(dsType, iid, element);
67             }
68
69             writeCnt++;
70
71             if (writeCnt == writesPerTx) {
72                 txSubmitted++;
73                 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
74                     @Override
75                     public void onSuccess(final Void result) {
76                         txOk++;
77                     }
78
79                     @Override
80                     public void onFailure(final Throwable t) {
81                         LOG.error("Transaction failed, {}", t);
82                         txError++;
83                     }
84                 });
85                 tx = chain.newWriteOnlyTransaction();
86                 writeCnt = 0;
87             }
88         }
89
90         // *** Clean up and close the transaction chain ***
91         // Submit the outstanding transaction even if it's empty and wait for it to finish
92         // We need to empty the transaction chain before closing it
93         try {
94             txSubmitted++;
95             tx.submit().checkedGet();
96             txOk++;
97         } catch (TransactionCommitFailedException e) {
98             LOG.error("Transaction failed", e);
99             txError++;
100         }
101         try {
102             chain.close();
103         } catch (IllegalStateException e) {
104             LOG.error("Transaction close failed,", e);
105         }
106         LOG.info("Transactions: submitted {}, completed {}", txSubmitted, (txOk + txError));
107     }
108
109     @Override
110     public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
111             final AsyncTransaction<?, ?> transaction, final Throwable cause) {
112         LOG.error("Broken chain {} in DatastoreBaAbstractWrite, transaction {}, cause {}",
113                 chain, transaction.getIdentifier(), cause);
114     }
115
116     @Override
117     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
118         LOG.info("DatastoreBaAbstractWrite closed successfully, chain {}", chain);
119     }
120
121 }

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