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