75fb89241866aa42f3a68425252cef3e6ba303ac
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / txchain / TxchainBaDelete.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.concurrent.ExecutionException;
13 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.binding.api.Transaction;
16 import org.opendaylight.mdsal.binding.api.TransactionChain;
17 import org.opendaylight.mdsal.binding.api.TransactionChainListener;
18 import org.opendaylight.mdsal.binding.api.WriteTransaction;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterListKey;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class TxchainBaDelete extends DatastoreAbstractWriter implements TransactionChainListener {
31     private static final Logger LOG = LoggerFactory.getLogger(TxchainBaDelete.class);
32     private final DataBroker bindingDataBroker;
33
34     public TxchainBaDelete(final DataBroker bindingDataBroker, final int outerListElem, final int innerListElem,
35             final long writesPerTx, final DataStore dataStore) {
36         super(StartTestInput.Operation.DELETE, outerListElem, innerListElem, writesPerTx, dataStore);
37         this.bindingDataBroker = bindingDataBroker;
38         LOG.debug("Created TxchainBaDelete");
39     }
40
41     @Override
42     public void createList() {
43         LOG.debug("TxchainBaDelete: creating data in the data store");
44
45         // Dump the whole list into the data store in a single transaction
46         // with <outerListElem> PUTs on the transaction
47         TxchainBaWrite dd = new TxchainBaWrite(bindingDataBroker, StartTestInput.Operation.PUT, outerListElem,
48             innerListElem, outerListElem, dataStore);
49         dd.createList();
50         dd.executeList();
51     }
52
53     @Override
54     public void executeList() {
55         final LogicalDatastoreType dsType = getDataStoreType();
56         final TransactionChain chain = bindingDataBroker.createMergingTransactionChain(this);
57
58         WriteTransaction tx = chain.newWriteOnlyTransaction();
59         int txSubmitted = 0;
60         int writeCnt = 0;
61
62         for (int l = 0; l < outerListElem; l++) {
63             InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
64                                                     .child(OuterList.class, new OuterListKey(l));
65             tx.delete(dsType, iid);
66
67             writeCnt++;
68
69             if (writeCnt == writesPerTx) {
70                 txSubmitted++;
71                 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
72                     @Override
73                     public void onSuccess(final CommitInfo result) {
74                         txOk++;
75                     }
76
77                     @Override
78                     public void onFailure(final Throwable cause) {
79                         LOG.error("Transaction failed", cause);
80                         txError++;
81                     }
82                 }, MoreExecutors.directExecutor());
83                 tx = chain.newWriteOnlyTransaction();
84                 writeCnt = 0;
85             }
86         }
87
88         // Submit the outstanding transaction even if it's empty and wait for it to finish
89         // We need to empty the chain before closing it
90         try {
91             if (writeCnt > 0) {
92                 txSubmitted++;
93             }
94             tx.commit().get();
95         } catch (final InterruptedException | ExecutionException e) {
96             LOG.error("Transaction failed", e);
97         }
98         try {
99             chain.close();
100         } catch (final IllegalStateException e) {
101             LOG.error("Transaction close failed,", e);
102         }
103         LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, txOk + txError);
104     }
105
106     @Override
107     public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
108             final Throwable cause) {
109         LOG.error("Broken chain {} in TxchainBaDelete, transaction {}, cause {}", chain, transaction.getIdentifier(),
110             cause);
111     }
112
113     @Override
114     public void onTransactionChainSuccessful(final TransactionChain chain) {
115         LOG.debug("TxchainBaDelete closed successfully, chain {}", chain);
116     }
117 }