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=5f587c3e2bfabc09fec49463d04a6fbeba414e9c;hp=de4f3efa03b3b9da5798793494f06c060be4ae28;hpb=41c10e8423c8f0fa88a7f7180dfbb582fabcbb8b;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 de4f3efa03..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,18 +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; @@ -57,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; @@ -119,14 +123,14 @@ abstract class AbstractTransactionHandler { // Not completed yet: create a transaction and hook it up final long txId = txCounter.incrementAndGet(); - final ListenableFuture execFuture = execWrite(txId); + 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); } @@ -152,6 +156,7 @@ abstract class AbstractTransactionHandler { state = State.SUCCESSFUL; completingFuture.cancel(false); runSuccessful(txCounter.get()); + shutdownExecutors(); return true; } @@ -190,6 +195,7 @@ abstract class AbstractTransactionHandler { state = State.FAILED; writingFuture.cancel(false); runFailed(cause, txId); + shutdownExecutors(); break; default: throw new IllegalStateException("Unhandled state " + local); @@ -223,13 +229,29 @@ abstract class AbstractTransactionHandler { state = State.FAILED; 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, long txId); abstract void runSuccessful(long allTx); 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; + } }