X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsamples%2Fclustering-test-app%2Fprovider%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fclustering%2Fit%2Fprovider%2Fimpl%2FAbstractTransactionHandler.java;h=e821a2b5769211d53ad5cbcc2a43d58ba1f15e31;hb=da174be7e22b16d4ac80cccefdc52b209b700745;hp=90cdce32dd136737a2d9d230a0f324394d279528;hpb=aea716f562ae010af4687812fab27af03f8a2a26;p=controller.git diff --git a/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/AbstractTransactionHandler.java b/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/AbstractTransactionHandler.java index 90cdce32dd..e821a2b576 100644 --- a/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/AbstractTransactionHandler.java +++ b/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/AbstractTransactionHandler.java @@ -8,17 +8,21 @@ package org.opendaylight.controller.clustering.it.provider.impl; import com.google.common.base.Stopwatch; +import com.google.common.util.concurrent.FluentFuture; import com.google.common.util.concurrent.FutureCallback; -import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ThreadFactoryBuilder; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.concurrent.ExecutionException; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicLong; +import org.opendaylight.mdsal.common.api.CommitInfo; import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.TransactionsParams; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; @@ -56,19 +60,20 @@ abstract class AbstractTransactionHandler { static final long INIT_TX_TIMEOUT_SECONDS = 125; private static final long DEAD_TIMEOUT_SECONDS = TimeUnit.MINUTES.toSeconds(15); + private static final AtomicLong COUNTER = new AtomicLong(); /* * writingExecutor is a single thread executor. Only this thread will write to datastore, * incurring sleep penalties if backend is not responsive. This thread never changes, but reads State. * This thread only adds to futures set. */ - private final ScheduledExecutorService writingExecutor = FinalizableScheduledExecutorService.newSingleThread(); + private final ScheduledExecutorService writingExecutor = newExecutorService("writing"); /* * completingExecutor is a single thread executor. Only this thread writes to State. * This thread should never incur any sleep penalty, so RPC response should always come on time. * This thread only removes from futures set. */ - private final ScheduledExecutorService completingExecutor = FinalizableScheduledExecutorService.newSingleThread(); + private final ScheduledExecutorService completingExecutor = newExecutorService("completing"); private final Collection> futures = Collections.synchronizedSet(new HashSet<>()); private final Stopwatch stopwatch = Stopwatch.createUnstarted(); private final long runtimeNanos; @@ -76,12 +81,12 @@ abstract class AbstractTransactionHandler { private ScheduledFuture writingFuture; private ScheduledFuture completingFuture; - private long txCounter; + private final AtomicLong txCounter = new AtomicLong(); private volatile State state; AbstractTransactionHandler(final TransactionsParams params) { - runtimeNanos = TimeUnit.SECONDS.toNanos(params.getSeconds()); - delayNanos = SECOND_AS_NANO / params.getTransactionsPerSecond(); + runtimeNanos = TimeUnit.SECONDS.toNanos(params.getSeconds().toJava()); + delayNanos = SECOND_AS_NANO / params.getTransactionsPerSecond().toJava(); } final synchronized void doStart() { @@ -117,15 +122,15 @@ abstract class AbstractTransactionHandler { } // Not completed yet: create a transaction and hook it up - final long txId = txCounter++; - final ListenableFuture execFuture = execWrite(txId); + final long txId = txCounter.incrementAndGet(); + final FluentFuture execFuture = execWrite(txId); LOG.debug("New future #{} allocated", txId); // Ordering is important: we need to add the future before hooking the callback futures.add(execFuture); - Futures.addCallback(execFuture, new FutureCallback() { + execFuture.addCallback(new FutureCallback() { @Override - public void onSuccess(final Object result) { + public void onSuccess(final CommitInfo result) { txSuccess(execFuture, txId); } @@ -150,7 +155,8 @@ abstract class AbstractTransactionHandler { LOG.debug("Completed waiting for all futures"); state = State.SUCCESSFUL; completingFuture.cancel(false); - runSuccessful(txCounter); + runSuccessful(txCounter.get()); + shutdownExecutors(); return true; } @@ -176,7 +182,7 @@ abstract class AbstractTransactionHandler { } final void txFailure(final ListenableFuture execFuture, final long txId, final Throwable cause) { - LOG.debug("Future #{} failed", txId, cause); + LOG.error("Commit future failed for tx # {}", txId, cause); futures.remove(execFuture); final State local = state; @@ -188,7 +194,8 @@ abstract class AbstractTransactionHandler { case WAITING: state = State.FAILED; writingFuture.cancel(false); - runFailed(cause); + runFailed(cause, txId); + shutdownExecutors(); break; default: throw new IllegalStateException("Unhandled state " + local); @@ -221,14 +228,30 @@ abstract class AbstractTransactionHandler { } state = State.FAILED; - runTimedOut(new TimeoutException("Collection did not finish in " + DEAD_TIMEOUT_SECONDS + " seconds")); + runTimedOut("Transactions did not finish in " + DEAD_TIMEOUT_SECONDS + " seconds"); + shutdownExecutors(); } - abstract ListenableFuture execWrite(long txId); + private void shutdownExecutors() { + writingExecutor.shutdown(); + completingExecutor.shutdown(); + } + + abstract FluentFuture execWrite(long txId); - abstract void runFailed(Throwable cause); + abstract void runFailed(Throwable cause, long txId); abstract void runSuccessful(long allTx); - abstract void runTimedOut(Exception cause); + abstract void runTimedOut(String cause); + + private ScheduledExecutorService newExecutorService(final String kind) { + final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, new ThreadFactoryBuilder() + .setDaemon(true) + .setNameFormat(getClass().getSimpleName() + "-" + kind + "-" + COUNTER.getAndIncrement() + "%d") + .build()); + executor.setKeepAliveTime(15, TimeUnit.SECONDS); + executor.allowCoreThreadTimeOut(true); + return executor; + } }