2 * Copyright (c) 2015 Cisco Systems and others. All rights reserved.
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
8 package org.opendaylight.dsbenchmark;
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.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
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();
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
64 private long testsCompleted = 0;
66 public DsbenchmarkProvider(final DOMDataBroker domDataBroker, final DataBroker dataBroker) {
67 this.domDataBroker = domDataBroker;
68 this.dataBroker = dataBroker;
71 @SuppressWarnings("checkstyle:illegalCatch")
73 listenerProvider.setDataBroker(dataBroker);
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);
83 LOG.info("DsbenchmarkProvider initiated");
88 LOG.info("DsbenchmarkProvider closed");
92 public ListenableFuture<RpcResult<CleanupStoreOutput>> cleanupStore(final CleanupStoreInput input) {
94 LOG.debug("Data Store cleaned up");
95 return Futures.immediateFuture(RpcResultBuilder.success(new CleanupStoreOutputBuilder().build()).build());
99 @SuppressWarnings("checkstyle:illegalCatch")
100 public ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
101 LOG.info("Starting the data store benchmark test, input: {}", input);
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();
111 // Cleanup data that may be left over from a previous test run
114 // Get the appropriate writer based on operation type and data format
115 DatastoreAbstractWriter dsWriter = getDatastoreWriter(input);
117 // Create listeners on OPERATIONAL and CONFIG test data subtrees
118 listenerProvider.createAndRegisterListeners(input.getListeners().intValue());
121 long startTime = System.nanoTime();
122 dsWriter.createList();
123 long endTime = System.nanoTime();
124 final long listCreateTime = (endTime - startTime) / 1000;
126 // Run the test and measure the execution time
129 startTime = System.nanoTime();
130 dsWriter.executeList();
131 endTime = System.nanoTime();
132 execTime = (endTime - startTime) / 1000;
134 this.testsCompleted++;
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();
144 LOG.info("Test finished");
145 setTestOperData(ExecStatus.Idle, testsCompleted);
146 execStatus.set(ExecStatus.Idle);
148 // Get the number of data change events and cleanup the data change listeners
149 long numDataChanges = listenerProvider.getDataChangeCount();
150 long numEvents = listenerProvider.getEventCountAndDestroyListeners();
152 StartTestOutput output = new StartTestOutputBuilder()
153 .setStatus(StartTestOutput.Status.OK)
154 .setListBuildTime(listCreateTime)
155 .setExecTime(execTime)
156 .setTxOk((long)dsWriter.getTxOk())
158 .setDataChangeEventsOk(numDataChanges)
159 .setTxError((long)dsWriter.getTxError())
162 return RpcResultBuilder.success(output).buildFuture();
165 private void setTestOperData(final ExecStatus sts, final long tstCompl) {
166 TestStatus status = new TestStatusBuilder()
168 .setTestsCompleted(tstCompl)
171 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
172 tx.put(LogicalDatastoreType.OPERATIONAL, TEST_STATUS_IID, status);
176 } catch (final InterruptedException | ExecutionException e) {
177 throw new IllegalStateException(e);
180 LOG.debug("DataStore test oper status populated: {}", status);
183 private void cleanupTestStore() {
184 TestExec data = new TestExecBuilder()
185 .setOuterList(Collections.<OuterList>emptyList())
188 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
189 tx.put(LogicalDatastoreType.CONFIGURATION, TEST_EXEC_IID, data);
192 LOG.debug("DataStore config test data cleaned up");
193 } catch (final InterruptedException | ExecutionException e) {
194 LOG.info("Failed to cleanup DataStore configtest data");
195 throw new IllegalStateException(e);
198 tx = dataBroker.newWriteOnlyTransaction();
199 tx.put(LogicalDatastoreType.OPERATIONAL, TEST_EXEC_IID, data);
202 LOG.debug("DataStore operational test data cleaned up");
203 } catch (final InterruptedException | ExecutionException e) {
204 LOG.info("Failed to cleanup DataStore operational test data");
205 throw new IllegalStateException(e);
210 private DatastoreAbstractWriter getDatastoreWriter(final StartTestInput input) {
212 final DatastoreAbstractWriter retVal;
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();
223 if (txType == StartTestInput.TransactionType.SIMPLETX) {
224 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
225 if (StartTestInput.Operation.DELETE == oper) {
226 retVal = new SimpletxBaDelete(this.dataBroker, outerListElem,
227 innerListElem,writesPerTx, dataStore);
228 } else if (StartTestInput.Operation.READ == oper) {
229 retVal = new SimpletxBaRead(this.dataBroker, outerListElem,
230 innerListElem, writesPerTx, dataStore);
232 retVal = new SimpletxBaWrite(this.dataBroker, oper, outerListElem,
233 innerListElem, writesPerTx, dataStore);
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);
243 retVal = new SimpletxDomWrite(this.domDataBroker, oper, outerListElem,
244 innerListElem, writesPerTx, dataStore);
248 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
249 if (StartTestInput.Operation.DELETE == oper) {
250 retVal = new TxchainBaDelete(this.dataBroker, outerListElem,
251 innerListElem, writesPerTx, dataStore);
252 } else if (StartTestInput.Operation.READ == oper) {
253 retVal = new TxchainBaRead(this.dataBroker, outerListElem,
254 innerListElem,writesPerTx, dataStore);
256 retVal = new TxchainBaWrite(this.dataBroker, oper, outerListElem,
257 innerListElem, writesPerTx, dataStore);
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);
268 retVal = new TxchainDomWrite(this.domDataBroker, oper, outerListElem,
269 innerListElem,writesPerTx, dataStore);
274 execStatus.set(ExecStatus.Idle);