package org.opendaylight.controller.clustering.it.provider.impl;
-import com.google.common.base.Stopwatch;
-import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
-import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
-import java.util.List;
import java.util.Set;
import java.util.SplittableRandom;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.ProduceTransactionsInput;
import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.ProduceTransactionsOutput;
import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.ProduceTransactionsOutputBuilder;
-import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.RpcError;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class ProduceTransactionsHandler implements Runnable {
-
+public class ProduceTransactionsHandler extends AbstractTransactionHandler {
private static final Logger LOG = LoggerFactory.getLogger(ProduceTransactionsHandler.class);
- private static final int SECOND_AS_NANO = 1000000000;
- //2^20 as in the model
- private static final int MAX_ITEM = 1048576;
-
- static final QName ID_INTS =
- QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target", "2017-02-15", "id-ints").intern();
- public static final QName ID_INT =
- QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target", "2017-02-15", "id-int").intern();
- static final QName ID =
- QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target", "2017-02-15", "id").intern();
- static final QName ITEM =
- QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target", "2017-02-15", "item").intern();
- private static final QName NUMBER =
- QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target", "2017-02-15", "number".intern());
-
- public static final YangInstanceIdentifier ID_INTS_YID = YangInstanceIdentifier.of(ID_INTS);
- public static final YangInstanceIdentifier ID_INT_YID = ID_INTS_YID.node(ID_INT).toOptimized();
- private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
- private final List<ListenableFuture<Void>> futures = new ArrayList<>();
- private final Set<Integer> usedValues = new HashSet<>();
+ private final SettableFuture<RpcResult<ProduceTransactionsOutput>> future = SettableFuture.create();
private final SplittableRandom random = new SplittableRandom();
+ private final Set<Integer> usedValues = new HashSet<>();
+ private final DOMDataTreeIdentifier idListItem;
+ private final DOMDataTreeProducer itemProducer;
- private final DOMDataTreeService domDataTreeService;
- private final long runtimeNanos;
- private final long delayNanos;
- private final String id;
-
- private SettableFuture<RpcResult<ProduceTransactionsOutput>> completionFuture;
- private Stopwatch stopwatch;
-
- private long allTx = 0;
private long insertTx = 0;
private long deleteTx = 0;
- private ScheduledFuture<?> scheduledFuture;
- private DOMDataTreeProducer itemProducer;
- private DOMDataTreeIdentifier idListItem;
-
- public ProduceTransactionsHandler(final DOMDataTreeService domDataTreeService,
- final ProduceTransactionsInput input) {
-
- this.domDataTreeService = domDataTreeService;
- runtimeNanos = TimeUnit.SECONDS.toNanos(input.getSeconds());
- delayNanos = SECOND_AS_NANO / input.getTransactionsPerSecond();
- id = input.getId();
- }
-
- @Override
- public void run() {
- futures.add(execWrite(futures.size()));
- maybeFinish();
+ private ProduceTransactionsHandler(final DOMDataTreeProducer producer, final DOMDataTreeIdentifier idListItem,
+ final ProduceTransactionsInput input) {
+ super(input);
+ this.itemProducer = Preconditions.checkNotNull(producer);
+ this.idListItem = Preconditions.checkNotNull(idListItem);
}
- public void start(final SettableFuture<RpcResult<ProduceTransactionsOutput>> settableFuture) {
- completionFuture = settableFuture;
-
- if (fillInitialList(completionFuture)) {
- stopwatch = Stopwatch.createStarted();
- scheduledFuture = executor.scheduleAtFixedRate(this, 0, delayNanos, TimeUnit.NANOSECONDS);
- } else {
- executor.shutdown();
- }
- }
-
- private boolean fillInitialList(final SettableFuture<RpcResult<ProduceTransactionsOutput>> settableFuture) {
- LOG.debug("Filling the item list with initial values.");
+ public static ListenableFuture<RpcResult<ProduceTransactionsOutput>> start(
+ final DOMDataTreeService domDataTreeService, final ProduceTransactionsInput input) {
+ final String id = input.getId();
+ LOG.debug("Filling the item list {} with initial values.", id);
final YangInstanceIdentifier idListWithKey = ID_INT_YID.node(new NodeIdentifierWithPredicates(ID_INT, ID, id));
- itemProducer = domDataTreeService.createProducer(
- Collections.singleton(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, idListWithKey)));
+ final DOMDataTreeProducer itemProducer = domDataTreeService.createProducer(
+ Collections.singleton(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, idListWithKey)));
final DOMDataTreeCursorAwareTransaction tx = itemProducer.createTransaction(false);
final DOMDataTreeWriteCursor cursor =
cursor.write(list.getIdentifier(), list);
cursor.close();
- idListItem = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION,
- idListWithKey.node(list.getIdentifier()).toOptimized());
-
try {
- tx.submit().checkedGet(125, TimeUnit.SECONDS);
- return true;
+ tx.submit().checkedGet(INIT_TX_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (final Exception e) {
LOG.warn("Unable to fill the initial item list.", e);
- settableFuture.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
- .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
- }
- try {
- itemProducer.close();
- } catch (final DOMDataTreeProducerException exception) {
- LOG.warn("Failure while closing producer.", exception);
+ try {
+ itemProducer.close();
+ } catch (final DOMDataTreeProducerException exception) {
+ LOG.warn("Failure while closing producer.", exception);
+ }
+
+ return Futures.immediateFuture(RpcResultBuilder.<ProduceTransactionsOutput>failed()
+ .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
}
- return false;
+
+ final ProduceTransactionsHandler handler = new ProduceTransactionsHandler(itemProducer,
+ new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, idListWithKey.node(list.getIdentifier())
+ .toOptimized()), input);
+ handler.doStart();
+ return handler.future;
}
- private ListenableFuture<Void> execWrite(final int offset) {
+ @Override
+ ListenableFuture<Void> execWrite(final long txId) {
final int i = random.nextInt(MAX_ITEM + 1);
final DOMDataTreeCursorAwareTransaction tx = itemProducer.createTransaction(false);
final DOMDataTreeWriteCursor cursor = tx.createCursor(idListItem);
- allTx++;
-
final NodeIdentifierWithPredicates entryId = new NodeIdentifierWithPredicates(ITEM, NUMBER, i);
if (usedValues.contains(i)) {
LOG.debug("Deleting item: {}", i);
cursor.close();
- final ListenableFuture<Void> future = tx.submit();
- if (LOG.isDebugEnabled()) {
- Futures.addCallback(future, new FutureCallback<Void>() {
- @Override
- public void onSuccess(final Void result) {
- LOG.debug("Future #{} completed successfully", offset);
- }
-
- @Override
- public void onFailure(final Throwable cause) {
- LOG.debug("Future #{} failed", offset, cause);
- }
- });
- }
-
- return future;
+ return tx.submit();
}
- private void maybeFinish() {
- final long elapsed = stopwatch.elapsed(TimeUnit.NANOSECONDS);
- if (elapsed >= runtimeNanos) {
- LOG.debug("Reached max running time, waiting for futures to complete.");
- scheduledFuture.cancel(false);
-
- final ListenableFuture<List<Void>> allFutures = Futures.allAsList(futures);
-
- try {
- // Timeout from cds should be 2 minutes so leave some leeway.
- allFutures.get(125, TimeUnit.SECONDS);
-
- LOG.debug("All futures completed successfully.");
-
- final ProduceTransactionsOutput output = new ProduceTransactionsOutputBuilder()
- .setAllTx(allTx)
- .setInsertTx(insertTx)
- .setDeleteTx(deleteTx)
- .build();
- completionFuture.set(RpcResultBuilder.<ProduceTransactionsOutput>success()
- .withResult(output).build());
- } catch (ExecutionException e) {
- LOG.error("Write transactions failed.", e.getCause());
- completionFuture.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
- .withError(RpcError.ErrorType.APPLICATION, "Submit failed", e.getCause()).build());
- } catch (InterruptedException | TimeoutException e) {
- LOG.error("Write transactions failed.", e);
- completionFuture.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
- .withError(RpcError.ErrorType.APPLICATION,
- "Final submit was timed out by the test provider or was interrupted", e).build());
-
- for (int i = 0; i < futures.size(); i++) {
- final ListenableFuture<Void> future = futures.get(i);
+ @Override
+ void runFailed(final Throwable cause) {
+ future.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
+ .withError(RpcError.ErrorType.APPLICATION, "Submit failed", cause).build());
+ }
- try {
- future.get(0, TimeUnit.NANOSECONDS);
- } catch (TimeoutException fe) {
- LOG.warn("Future #{}/{} not completed yet", i, futures.size());
- } catch (ExecutionException fe) {
- LOG.warn("Future #{}/{} failed", i, futures.size(), e.getCause());
- } catch (InterruptedException fe) {
- LOG.warn("Interrupted while examining future #{}/{}", i, futures.size(), e);
- }
- }
- } catch (Exception e) {
- LOG.error("Write transactions failed.", e);
- completionFuture.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
- .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
- }
+ @Override
+ void runSuccessful(final long allTx) {
+ final ProduceTransactionsOutput output = new ProduceTransactionsOutputBuilder()
+ .setAllTx(allTx)
+ .setInsertTx(insertTx)
+ .setDeleteTx(deleteTx)
+ .build();
+ future.set(RpcResultBuilder.<ProduceTransactionsOutput>success()
+ .withResult(output).build());
+ }
- executor.shutdown();
- try {
- itemProducer.close();
- } catch (final DOMDataTreeProducerException e) {
- LOG.warn("Failure while closing item producer.", e);
- }
- }
+ @Override
+ void runTimedOut(final Exception cause) {
+ future.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
+ .withError(RpcError.ErrorType.APPLICATION,
+ "Final submit was timed out by the test provider or was interrupted", cause).build());
}
}