229eddfa0c75d64cad49e3b443b93627c1b7a6c4
[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 java.util.Collections;
12 import java.util.concurrent.Future;
13 import java.util.concurrent.atomic.AtomicReference;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
19 import org.opendaylight.dsbenchmark.listener.DsbenchmarkListenerProvider;
20 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaDelete;
21 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaRead;
22 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaWrite;
23 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomDelete;
24 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomRead;
25 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomWrite;
26 import org.opendaylight.dsbenchmark.txchain.TxchainBaDelete;
27 import org.opendaylight.dsbenchmark.txchain.TxchainBaRead;
28 import org.opendaylight.dsbenchmark.txchain.TxchainBaWrite;
29 import org.opendaylight.dsbenchmark.txchain.TxchainDomDelete;
30 import org.opendaylight.dsbenchmark.txchain.TxchainDomRead;
31 import org.opendaylight.dsbenchmark.txchain.TxchainDomWrite;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.DsbenchmarkService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestOutputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExecBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatus;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatus.ExecStatus;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatusBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
49
50     private static final Logger LOG = LoggerFactory.getLogger(DsbenchmarkProvider.class);
51     private static final InstanceIdentifier<TestExec> TEST_EXEC_IID =
52             InstanceIdentifier.builder(TestExec.class).build();
53     private static final InstanceIdentifier<TestStatus> TEST_STATUS_IID =
54             InstanceIdentifier.builder(TestStatus.class).build();
55
56     private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
57     private final DsbenchmarkListenerProvider listenerProvider = new DsbenchmarkListenerProvider();
58     private final DOMDataBroker domDataBroker;  // Async DOM Broker for use with all DOM operations
59     private final DataBroker txChainDataBroker; // Async Binding-Aware Broker for use in tx chains; initialized to
60                                                 // ping-pong broker in default config (see default-config.xml and
61                                                 // dsbenchmark-impl.yang)
62     private final DataBroker simpleTxDataBroker;      // "Legacy" OSGI Data Broker for use in simple transactions
63
64     private long testsCompleted = 0;
65
66     public DsbenchmarkProvider(final DOMDataBroker domDataBroker, final DataBroker txChainDataBroker,
67             final DataBroker simpleTxDataBroker) {
68         this.domDataBroker = domDataBroker;
69         this.txChainDataBroker = txChainDataBroker;
70         this.simpleTxDataBroker = simpleTxDataBroker;
71     }
72
73     public void init() {
74         listenerProvider.setDataBroker(simpleTxDataBroker);
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 Future<RpcResult<Void>> cleanupStore() {
94         cleanupTestStore();
95         LOG.debug("Data Store cleaned up");
96         return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
97     }
98
99     @Override
100     public Future<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         long startTime, endTime, listCreateTime, execTime;
121
122         startTime = System.nanoTime();
123         dsWriter.createList();
124         endTime = System.nanoTime();
125         listCreateTime = (endTime - startTime) / 1000;
126
127         // Run the test and measure the execution time
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((long)dsWriter.getTxOk())
157                 .setNtfOk(numEvents)
158                 .setDataChangeEventsOk(numDataChanges)
159                 .setTxError((long)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(tstCompl)
169                 .build();
170
171         WriteTransaction tx = simpleTxDataBroker.newWriteOnlyTransaction();
172         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_STATUS_IID, status);
173
174         try {
175             tx.submit().checkedGet();
176         } catch (final TransactionCommitFailedException 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()
185                 .setOuterList(Collections.<OuterList>emptyList())
186                 .build();
187
188         WriteTransaction tx = simpleTxDataBroker.newWriteOnlyTransaction();
189         tx.put(LogicalDatastoreType.CONFIGURATION, TEST_EXEC_IID, data);
190         try {
191             tx.submit().checkedGet();
192             LOG.debug("DataStore config test data cleaned up");
193         } catch (final TransactionCommitFailedException e) {
194             LOG.info("Failed to cleanup DataStore configtest data");
195             throw new IllegalStateException(e);
196         }
197
198         tx = simpleTxDataBroker.newWriteOnlyTransaction();
199         tx.put(LogicalDatastoreType.OPERATIONAL, TEST_EXEC_IID, data);
200         try {
201             tx.submit().checkedGet();
202             LOG.debug("DataStore operational test data cleaned up");
203         } catch (final TransactionCommitFailedException e) {
204             LOG.info("Failed to cleanup DataStore operational test data");
205             throw new IllegalStateException(e);
206         }
207
208     }
209
210     private DatastoreAbstractWriter getDatastoreWriter(final StartTestInput input) {
211
212         final DatastoreAbstractWriter retVal;
213
214         StartTestInput.TransactionType txType = input.getTransactionType();
215         StartTestInput.Operation oper = input.getOperation();
216         StartTestInput.DataFormat dataFormat = input.getDataFormat();
217         StartTestInput.DataStore dataStore = input.getDataStore();
218         int outerListElem = input.getOuterElements().intValue();
219         int innerListElem = input.getInnerElements().intValue();
220         int writesPerTx = input.getPutsPerTx().intValue();
221
222         try {
223             if (txType == StartTestInput.TransactionType.SIMPLETX) {
224                 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
225                     if (StartTestInput.Operation.DELETE == oper) {
226                         retVal = new SimpletxBaDelete(this.simpleTxDataBroker, outerListElem,
227                                 innerListElem,writesPerTx, dataStore);
228                     } else if (StartTestInput.Operation.READ == oper) {
229                         retVal = new SimpletxBaRead(this.simpleTxDataBroker, outerListElem,
230                                 innerListElem, writesPerTx, dataStore);
231                     } else {
232                         retVal = new SimpletxBaWrite(this.simpleTxDataBroker, oper, outerListElem,
233                                 innerListElem, writesPerTx, dataStore);
234                     }
235                 } else {
236                     if (StartTestInput.Operation.DELETE == oper) {
237                         retVal = new SimpletxDomDelete(this.domDataBroker, outerListElem,
238                                 innerListElem, writesPerTx, dataStore);
239                     } else if (StartTestInput.Operation.READ == oper) {
240                         retVal = new SimpletxDomRead(this.domDataBroker, outerListElem,
241                                 innerListElem, writesPerTx, dataStore);
242                     } else {
243                         retVal = new SimpletxDomWrite(this.domDataBroker, oper, outerListElem,
244                                 innerListElem, writesPerTx, dataStore);
245                     }
246                 }
247             } else {
248                 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
249                     if (StartTestInput.Operation.DELETE == oper) {
250                         retVal = new TxchainBaDelete(this.txChainDataBroker, outerListElem,
251                                 innerListElem, writesPerTx, dataStore);
252                     } else if (StartTestInput.Operation.READ == oper) {
253                         retVal = new TxchainBaRead(this.txChainDataBroker,outerListElem,
254                                 innerListElem,writesPerTx, dataStore);
255                     } else {
256                         retVal = new TxchainBaWrite(this.txChainDataBroker, oper, outerListElem,
257                                 innerListElem, writesPerTx, dataStore);
258                     }
259                 } else {
260                     if (StartTestInput.Operation.DELETE == oper) {
261                         retVal = new TxchainDomDelete(this.domDataBroker, outerListElem,
262                                 innerListElem, writesPerTx, dataStore);
263                     } else if (StartTestInput.Operation.READ == oper) {
264                         retVal = new TxchainDomRead(this.domDataBroker, outerListElem,
265                                 innerListElem, writesPerTx, dataStore);
266
267                     } else {
268                         retVal = new TxchainDomWrite(this.domDataBroker, oper, outerListElem,
269                                 innerListElem,writesPerTx, dataStore);
270                     }
271                 }
272             }
273         } finally {
274             execStatus.set(ExecStatus.Idle);
275         }
276         return retVal;
277     }
278 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.