Improve segmented journal actor metrics
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / txchain / TxchainDomDelete.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.common.api.CommitInfo;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
18 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
23 import org.opendaylight.yangtools.yang.common.Empty;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class TxchainDomDelete extends DatastoreAbstractWriter implements FutureCallback<Empty> {
31     private static final Logger LOG = LoggerFactory.getLogger(TxchainDomDelete.class);
32     private final DOMDataBroker domDataBroker;
33
34     public TxchainDomDelete(final DOMDataBroker domDataBroker, final int outerListElem, final int innerListElem,
35             final long writesPerTx, final DataStore dataStore) {
36         super(StartTestInput.Operation.DELETE, outerListElem, innerListElem, writesPerTx, dataStore);
37         this.domDataBroker = domDataBroker;
38         LOG.debug("Created TxchainDomDelete");
39     }
40
41     @Override
42     public void createList() {
43         LOG.debug("TxchainDomDelete: 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         TxchainDomWrite dd = new TxchainDomWrite(domDataBroker, 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 org.opendaylight.yangtools.yang.common.QName olId = QName.create(OuterList.QNAME, "id");
57         final YangInstanceIdentifier pid =
58                 YangInstanceIdentifier.builder().node(TestExec.QNAME).node(OuterList.QNAME).build();
59         final DOMTransactionChain chain = domDataBroker.createMergingTransactionChain();
60         chain.addCallback(this);
61
62         DOMDataTreeWriteTransaction tx = chain.newWriteOnlyTransaction();
63         int txSubmitted = 0;
64         int writeCnt = 0;
65
66         for (int l = 0; l < outerListElem; l++) {
67             YangInstanceIdentifier yid = pid.node(NodeIdentifierWithPredicates.of(OuterList.QNAME, olId, l));
68             tx.delete(dsType, yid);
69
70             writeCnt++;
71
72             if (writeCnt == writesPerTx) {
73                 txSubmitted++;
74                 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
75                     @Override
76                     public void onSuccess(final CommitInfo result) {
77                         txOk++;
78                     }
79
80                     @Override
81                     public void onFailure(final Throwable cause) {
82                         LOG.error("Transaction failed", cause);
83                         txError++;
84                     }
85                 }, MoreExecutors.directExecutor());
86                 tx = chain.newWriteOnlyTransaction();
87                 writeCnt = 0;
88             }
89         }
90
91         // *** Clean up and close the transaction chain ***
92         // Submit the outstanding transaction even if it's empty and wait for it to finish
93         // We need to empty the transaction chain before closing it
94         try {
95             txSubmitted++;
96             tx.commit().get();
97             txOk++;
98         } catch (final InterruptedException | ExecutionException e) {
99             LOG.error("Transaction failed", e);
100             txError++;
101         }
102         try {
103             chain.close();
104         } catch (final IllegalStateException e) {
105             LOG.error("Transaction close failed,", e);
106         }
107         LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, txOk + txError);
108     }
109
110     @Override
111     public void onFailure(final Throwable cause) {
112         LOG.error("Broken chain in TxchainDomDelete", cause);
113     }
114
115     @Override
116     public void onSuccess(final Empty result) {
117         LOG.debug("TxchainDomDelete closed successfully");
118     }
119 }