Specialize CommitCoordinationTask 18/103118/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 7 Nov 2022 01:11:11 +0000 (02:11 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 7 Nov 2022 01:21:15 +0000 (02:21 +0100)
Tracker is optional, so let's subclass the tracking into a wrapper
class.

Change-Id: I4271d2453181bbbf2310c4737ef90b87d154b046
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
dom/mdsal-dom-broker/src/main/java/org/opendaylight/mdsal/dom/broker/CommitCoordinationTask.java
dom/mdsal-dom-broker/src/main/java/org/opendaylight/mdsal/dom/broker/SerializedDOMDataBroker.java
dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/CommitCoordinationTaskTest.java

index 64ebada7946b89176670df67bb8eb6f8a53640fc..49af6d65ff14c0461ff161081249bab1c626994e 100644 (file)
@@ -27,7 +27,29 @@ import org.slf4j.LoggerFactory;
 /**
  * Implementation of blocking three-phase commit-coordination tasks without support of cancellation.
  */
-final class CommitCoordinationTask implements Callable<CommitInfo> {
+sealed class CommitCoordinationTask implements Callable<CommitInfo> {
+    static final class WithTracker extends CommitCoordinationTask {
+        private final DurationStatisticsTracker commitStatTracker;
+
+        WithTracker(final DOMDataTreeWriteTransaction transaction,
+                final Collection<DOMStoreThreePhaseCommitCohort> cohorts,
+                final DurationStatisticsTracker commitStatTracker) {
+            super(transaction, cohorts);
+            this.commitStatTracker = requireNonNull(commitStatTracker);
+        }
+
+        @Override
+        public CommitInfo call() throws TransactionCommitFailedException {
+            final long startTime = System.nanoTime();
+
+            try {
+                return super.call();
+            } finally {
+                commitStatTracker.addDuration(System.nanoTime() - startTime);
+            }
+        }
+    }
+
     private enum Phase {
         CAN_COMMIT,
         PRE_COMMIT,
@@ -36,23 +58,17 @@ final class CommitCoordinationTask implements Callable<CommitInfo> {
 
     private static final Logger LOG = LoggerFactory.getLogger(CommitCoordinationTask.class);
     private final Collection<DOMStoreThreePhaseCommitCohort> cohorts;
-    private final DurationStatisticsTracker commitStatTracker;
     private final DOMDataTreeWriteTransaction tx;
 
     CommitCoordinationTask(final DOMDataTreeWriteTransaction transaction,
-            final Collection<DOMStoreThreePhaseCommitCohort> cohorts,
-            final DurationStatisticsTracker commitStatTracker) {
-        this.tx = requireNonNull(transaction, "transaction must not be null");
+            final Collection<DOMStoreThreePhaseCommitCohort> cohorts) {
+        tx = requireNonNull(transaction, "transaction must not be null");
         this.cohorts = requireNonNull(cohorts, "cohorts must not be null");
-        this.commitStatTracker = commitStatTracker;
     }
 
     @Override
     public CommitInfo call() throws TransactionCommitFailedException {
-        final long startTime = commitStatTracker != null ? System.nanoTime() : 0;
-
-        Phase phase = Phase.CAN_COMMIT;
-
+        var phase = Phase.CAN_COMMIT;
         try {
             LOG.debug("Transaction {}: canCommit Started", tx.getIdentifier());
             canCommitBlocking();
@@ -71,10 +87,6 @@ final class CommitCoordinationTask implements Callable<CommitInfo> {
             LOG.warn("Tx: {} Error during phase {}, starting Abort", tx.getIdentifier(), phase, e);
             abortBlocking(e);
             throw e;
-        } finally {
-            if (commitStatTracker != null) {
-                commitStatTracker.addDuration(System.nanoTime() - startTime);
-            }
         }
     }
 
index 4053475ccbb4066d509634df7989cf35779296ce..d100e8f230d4e32c7c38f5e54d1bebc2700405e8 100644 (file)
@@ -71,7 +71,7 @@ public class SerializedDOMDataBroker extends AbstractDOMDataBroker {
 
         try {
             return FluentFuture.from(executor.submit(
-                new CommitCoordinationTask(transaction, cohorts, commitStatsTracker)));
+                new CommitCoordinationTask.WithTracker(transaction, cohorts, commitStatsTracker)));
         } catch (RejectedExecutionException e) {
             LOG.error("The commit executor's queue is full - submit task was rejected. \n{}", executor, e);
             return FluentFutures.immediateFailedFluentFuture(new TransactionCommitFailedException(
index 3cf8b485b24f87be7e52287ac980fd2348343ca0..d93072dd74b0cb4dbdb3000789117b3a436a0a76 100644 (file)
@@ -20,14 +20,14 @@ import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
 
 public class CommitCoordinationTaskTest {
-
     private final DOMStoreThreePhaseCommitCohort cohort = mock(DOMStoreThreePhaseCommitCohort.class);
+
     private CommitCoordinationTask task;
 
     @Before
     public void setUp() throws Exception {
         final DOMDataTreeWriteTransaction tx = mock(DOMDataTreeWriteTransaction.class);
-        task = new CommitCoordinationTask(tx, ImmutableList.of(cohort), null);
+        task = new CommitCoordinationTask(tx, ImmutableList.of(cohort));
         doReturn("test").when(tx).getIdentifier();
     }