Improve segmented journal actor metrics
[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 package org.opendaylight.dsbenchmark.txchain;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.util.List;
15 import java.util.concurrent.ExecutionException;
16 import org.opendaylight.dsbenchmark.BaListBuilder;
17 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
18 import org.opendaylight.mdsal.binding.api.DataBroker;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.Operation;
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.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.common.Empty;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class TxchainBaWrite extends DatastoreAbstractWriter implements FutureCallback<Empty> {
31     private static final Logger LOG = LoggerFactory.getLogger(TxchainBaWrite.class);
32
33     private final DataBroker dataBroker;
34     private List<OuterList> list = null;
35
36     public TxchainBaWrite(final DataBroker dataBroker, final Operation oper, final int outerListElem,
37             final int innerListElem, final long writesPerTx, final DataStore dataStore) {
38         super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
39         this.dataBroker = requireNonNull(dataBroker);
40         LOG.debug("Created TxchainBaWrite");
41     }
42
43     @Override
44     public void createList() {
45         list = BaListBuilder.buildOuterList(outerListElem, innerListElem);
46     }
47
48     @Override
49     public void executeList() {
50         final var chain = dataBroker.createMergingTransactionChain();
51         chain.addCallback(this);
52         final var dsType = getDataStoreType();
53
54         var tx = chain.newWriteOnlyTransaction();
55         int txSubmitted = 0;
56         int writeCnt = 0;
57
58         for (var element : list) {
59             final var iid = InstanceIdentifier.create(TestExec.class).child(OuterList.class, element.key());
60
61             if (oper == StartTestInput.Operation.PUT) {
62                 tx.put(dsType, iid, element);
63             } else {
64                 tx.merge(dsType, iid, element);
65             }
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         // *** Clean up and close the transaction chain ***
89         // Submit the outstanding transaction even if it's empty and wait for it to finish
90         // We need to empty the transaction chain before closing it
91         try {
92             txSubmitted++;
93             tx.commit().get();
94             txOk++;
95         } catch (final InterruptedException | ExecutionException e) {
96             LOG.error("Transaction failed", e);
97             txError++;
98         }
99         try {
100             chain.close();
101         } catch (final IllegalStateException e) {
102             LOG.error("Transaction close failed,", e);
103         }
104         LOG.debug("Transactions: submitted {}, completed {}", txSubmitted, txOk + txError);
105     }
106
107     @Override
108     public void onFailure(final Throwable cause) {
109         LOG.error("Broken chain in DatastoreBaAbstractWrite", cause);
110     }
111
112     @Override
113     public void onSuccess(final Empty result) {
114         LOG.debug("DatastoreBaAbstractWrite closed successfully");
115     }
116 }