eea490a3c7180130af0b51d7bde6f575920a43e0
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / DsbenchmarkProvider.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;
9
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.Collections;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.atomic.AtomicReference;
15 import org.opendaylight.dsbenchmark.listener.DsbenchmarkListenerProvider;
16 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaDelete;
17 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaRead;
18 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaWrite;
19 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomDelete;
20 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomRead;
21 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomWrite;
22 import org.opendaylight.dsbenchmark.txchain.TxchainBaDelete;
23 import org.opendaylight.dsbenchmark.txchain.TxchainBaRead;
24 import org.opendaylight.dsbenchmark.txchain.TxchainBaWrite;
25 import org.opendaylight.dsbenchmark.txchain.TxchainDomDelete;
26 import org.opendaylight.dsbenchmark.txchain.TxchainDomRead;
27 import org.opendaylight.dsbenchmark.txchain.TxchainDomWrite;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.WriteTransaction;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.CleanupStoreInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.CleanupStoreOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.CleanupStoreOutputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.DsbenchmarkService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestOutputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExecBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatus;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatus.ExecStatus;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatusBuilder;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
47 import org.opendaylight.yangtools.yang.common.Uint32;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
52
53     private static final Logger LOG = LoggerFactory.getLogger(DsbenchmarkProvider.class);
54     private static final InstanceIdentifier<TestExec> TEST_EXEC_IID =
55             InstanceIdentifier.builder(TestExec.class).build();
56     private static final InstanceIdentifier<TestStatus> TEST_STATUS_IID =
57             InstanceIdentifier.builder(TestStatus.class).build();
58
59     private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
60     private final DsbenchmarkListenerProvider listenerProvider = new DsbenchmarkListenerProvider();
61     private final DOMDataBroker domDataBroker;  // Async DOM Broker for use with all DOM operations
62     private final DataBroker dataBroker; // Async Binding-Aware Broker for use in tx chains
63
64     private long testsCompleted = 0;
65
66     public DsbenchmarkProvider(final DOMDataBroker domDataBroker, final DataBroker dataBroker) {
67         this.domDataBroker = domDataBroker;
68         this.dataBroker = dataBroker;
69     }
70
71     @SuppressWarnings("checkstyle:illegalCatch")
72     public void init() {
73         listenerProvider.setDataBroker(dataBroker);
74
75         try {
76             // We want to set the initial operation status so users can detect we are ready to start test.
77             setTestOperData(this.execStatus.get(), testsCompleted);
78         } catch (final Exception e) {
79             // TODO: Use a singleton service to make sure the initial write is performed only once.
80             LOG.warn("Working around Bugs 8829 and 6793 by ignoring exception from setTestOperData", e);
81         }
82
83         LOG.info("DsbenchmarkProvider initiated");
84     }
85
86     @Override
87     public void close() {
88         LOG.info("DsbenchmarkProvider closed");
89     }
90
91     @Override
92     public ListenableFuture<RpcResult<CleanupStoreOutput>> cleanupStore(final CleanupStoreInput input) {
93         cleanupTestStore();
94         LOG.debug("Data Store cleaned up");
95         return Futures.immediateFuture(RpcResultBuilder.success(new CleanupStoreOutputBuilder().build()).build());
96     }
97
98     @Override
99     @SuppressWarnings("checkstyle:illegalCatch")
100     public ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
101         LOG.info("Starting the data store benchmark test, input: {}", input);
102
103         // Check if there is a test in progress
104         if (execStatus.compareAndSet(ExecStatus.Idle, ExecStatus.Executing) == false) {
105             LOG.info("Test in progress");
106             return RpcResultBuilder.success(new StartTestOutputBuilder()
107                     .setStatus(StartTestOutput.Status.TESTINPROGRESS)
108                     .build()).buildFuture();
109         }
110
111         // Cleanup data that may be left over from a previous test run
112         cleanupTestStore();
113
114         // Get the appropriate writer based on operation type and data format
115         DatastoreAbstractWriter dsWriter = getDatastoreWriter(input);
116
117         // Create listeners on OPERATIONAL and CONFIG test data subtrees
118         listenerProvider.createAndRegisterListeners(input.getListeners().intValue());
119
120
121         long startTime = System.nanoTime();
122         dsWriter.createList();
123         long endTime = System.nanoTime();
124         final long listCreateTime = (endTime - startTime) / 1000;
125
126         // Run the test and measure the execution time
127         long execTime;
128         try {
129             startTime = System.nanoTime();
130             dsWriter.executeList();
131             endTime = System.nanoTime();
132             execTime = (endTime - startTime) / 1000;
133
134             this.testsCompleted++;
135
136         } catch (final Exception e) {
137             LOG.error("Test error: {}", e.toString());
138             execStatus.set(ExecStatus.Idle);
139             return RpcResultBuilder.success(new StartTestOutputBuilder()
140                     .setStatus(StartTestOutput.Status.FAILED)
141                     .build()).buildFuture();
142         }
143
144         LOG.info("Test finished");
145         setTestOperData(ExecStatus.Idle, testsCompleted);
146         execStatus.set(ExecStatus.Idle);
147
148         // Get the number of data change events and cleanup the data change listeners
149         long numDataChanges = listenerProvider.getDataChangeCount();
150         long numEvents = listenerProvider.getEventCountAndDestroyListeners();
151
152         StartTestOutput output = new StartTestOutputBuilder()
153                 .setStatus(StartTestOutput.Status.OK)
154                 .setListBuildTime(listCreateTime)
155                 .setExecTime(execTime)
156                 .setTxOk(Uint32.valueOf(dsWriter.getTxOk()))
157                 .setNtfOk(Uint32.valueOf(numEvents))
158                 .setDataChangeEventsOk(Uint32.valueOf(numDataChanges))
159                 .setTxError(Uint32.valueOf(dsWriter.getTxError()))
160                 .build();
161
162         return RpcResultBuilder.success(output).buildFuture();
163     }
164
165     private void setTestOperData(final ExecStatus sts, final long tstCompl) {
166         TestStatus status = new TestStatusBuilder()
167                 .setExecStatus(sts)
168                 .setTestsCompleted(Uint32.valueOf(tstCompl))
169                 .build();
170
171         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
172         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_STATUS_IID, status);
173
174         try {
175             tx.commit().get();
176         } catch (final InterruptedException | ExecutionException e) {
177             throw new IllegalStateException(e);
178         }
179
180         LOG.debug("DataStore test oper status populated: {}", status);
181     }
182
183     private void cleanupTestStore() {
184         TestExec data = new TestExecBuilder().setOuterList(Collections.emptyMap()).build();
185
186         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
187         tx.put(LogicalDatastoreType.CONFIGURATION, TEST_EXEC_IID, data);
188         try {
189             tx.commit().get();
190             LOG.debug("DataStore config test data cleaned up");
191         } catch (final InterruptedException | ExecutionException e) {
192             LOG.info("Failed to cleanup DataStore configtest data");
193             throw new IllegalStateException(e);
194         }
195
196         tx = dataBroker.newWriteOnlyTransaction();
197         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_EXEC_IID, data);
198         try {
199             tx.commit().get();
200             LOG.debug("DataStore operational test data cleaned up");
201         } catch (final InterruptedException | ExecutionException e) {
202             LOG.info("Failed to cleanup DataStore operational test data");
203             throw new IllegalStateException(e);
204         }
205
206     }
207
208     private DatastoreAbstractWriter getDatastoreWriter(final StartTestInput input) {
209
210         final DatastoreAbstractWriter retVal;
211
212         StartTestInput.TransactionType txType = input.getTransactionType();
213         StartTestInput.Operation oper = input.getOperation();
214         StartTestInput.DataFormat dataFormat = input.getDataFormat();
215         StartTestInput.DataStore dataStore = input.getDataStore();
216         int outerListElem = input.getOuterElements().intValue();
217         int innerListElem = input.getInnerElements().intValue();
218         int writesPerTx = input.getPutsPerTx().intValue();
219
220         try {
221             if (txType == StartTestInput.TransactionType.SIMPLETX) {
222                 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
223                     if (StartTestInput.Operation.DELETE == oper) {
224                         retVal = new SimpletxBaDelete(this.dataBroker, outerListElem,
225                                 innerListElem,writesPerTx, dataStore);
226                     } else if (StartTestInput.Operation.READ == oper) {
227                         retVal = new SimpletxBaRead(this.dataBroker, outerListElem,
228                                 innerListElem, writesPerTx, dataStore);
229                     } else {
230                         retVal = new SimpletxBaWrite(this.dataBroker, oper, outerListElem,
231                                 innerListElem, writesPerTx, dataStore);
232                     }
233                 } else {
234                     if (StartTestInput.Operation.DELETE == oper) {
235                         retVal = new SimpletxDomDelete(this.domDataBroker, outerListElem,
236                                 innerListElem, writesPerTx, dataStore);
237                     } else if (StartTestInput.Operation.READ == oper) {
238                         retVal = new SimpletxDomRead(this.domDataBroker, outerListElem,
239                                 innerListElem, writesPerTx, dataStore);
240                     } else {
241                         retVal = new SimpletxDomWrite(this.domDataBroker, oper, outerListElem,
242                                 innerListElem, writesPerTx, dataStore);
243                     }
244                 }
245             } else {
246                 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
247                     if (StartTestInput.Operation.DELETE == oper) {
248                         retVal = new TxchainBaDelete(this.dataBroker, outerListElem,
249                                 innerListElem, writesPerTx, dataStore);
250                     } else if (StartTestInput.Operation.READ == oper) {
251                         retVal = new TxchainBaRead(this.dataBroker, outerListElem,
252                                 innerListElem,writesPerTx, dataStore);
253                     } else {
254                         retVal = new TxchainBaWrite(this.dataBroker, oper, outerListElem,
255                                 innerListElem, writesPerTx, dataStore);
256                     }
257                 } else {
258                     if (StartTestInput.Operation.DELETE == oper) {
259                         retVal = new TxchainDomDelete(this.domDataBroker, outerListElem,
260                                 innerListElem, writesPerTx, dataStore);
261                     } else if (StartTestInput.Operation.READ == oper) {
262                         retVal = new TxchainDomRead(this.domDataBroker, outerListElem,
263                                 innerListElem, writesPerTx, dataStore);
264
265                     } else {
266                         retVal = new TxchainDomWrite(this.domDataBroker, oper, outerListElem,
267                                 innerListElem,writesPerTx, dataStore);
268                     }
269                 }
270             }
271         } finally {
272             execStatus.set(ExecStatus.Idle);
273         }
274         return retVal;
275     }
276 }