From 64d8611a23264d3c4365b9e2e969a6910dfca70e Mon Sep 17 00:00:00 2001 From: nikdivak Date: Fri, 15 Dec 2023 17:06:22 +0530 Subject: [PATCH 1/2] general cleanup, converted tidToTransactionMap_ to concurrent hash map, removed lock for read and write to same --- .../icatch/imp/TransactionServiceImp.java | 42 +++++++------------ 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/public/transactions/src/main/java/com/atomikos/icatch/imp/TransactionServiceImp.java b/public/transactions/src/main/java/com/atomikos/icatch/imp/TransactionServiceImp.java index c1a0ae11..97ac4b2b 100644 --- a/public/transactions/src/main/java/com/atomikos/icatch/imp/TransactionServiceImp.java +++ b/public/transactions/src/main/java/com/atomikos/icatch/imp/TransactionServiceImp.java @@ -10,11 +10,11 @@ import java.util.HashMap; import java.util.HashSet; -import java.util.Hashtable; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.Stack; +import java.util.concurrent.ConcurrentHashMap; import com.atomikos.finitestates.FSMEnterEvent; import com.atomikos.finitestates.FSMEnterListener; @@ -55,7 +55,7 @@ public class TransactionServiceImp implements TransactionServiceProvider, private long maxTimeout_; private Object[] rootLatches_ = null; - private Hashtable tidToTransactionMap_ = null; + private Map tidToTransactionMap_; private Map recreatedCoordinatorsByRootId = new HashMap<>(); private Map allCoordinatorsByCoordinatorId = new HashMap<>(); private boolean shutdownInProgress_ = false; @@ -91,7 +91,7 @@ public class TransactionServiceImp implements TransactionServiceProvider, * configurations that do not support orphan detection. * @param single_threaded_2pc * Whether 2PC commit should happen in the same thread that started the tx. - * @param recoveryLog2 + * @param recoveryLog * */ @@ -105,7 +105,7 @@ public TransactionServiceImp ( String name , initialized_ = false; recoverymanager_ = recoverymanager; tidmgr_ = tidmgr; - tidToTransactionMap_ = new Hashtable(); + tidToTransactionMap_ = new ConcurrentHashMap<>(); rootLatches_ = new Object[NUMLATCHES]; for (int i = 0; i < NUMLATCHES; i++) { rootLatches_[i] = new Object(); @@ -144,12 +144,10 @@ private Object getLatch ( String root ) private void setTidToTx ( String tid , CompositeTransaction ct ) throws IllegalStateException { - synchronized ( tidToTransactionMap_ ) { - if ( tidToTransactionMap_.containsKey ( tid.intern () ) ) - throw new IllegalStateException ( "Already mapped: " + tid ); - tidToTransactionMap_.put ( tid.intern (), ct ); - ct.addSubTxAwareParticipant(this); // for GC purposes - } + if (tidToTransactionMap_.containsKey(tid.intern())) + throw new IllegalStateException("Already mapped: " + tid); + tidToTransactionMap_.put(tid.intern(), ct); + ct.addSubTxAwareParticipant(this); // for GC purposes } /** @@ -215,11 +213,9 @@ private CompositeTransactionImp createCT ( String tid , * * @param recoveryDomainName The recovery domain of the superior of this coordinator. * - * @param RecoveryCoordinator + * @param adaptor * An existing coordinator for the given root. Null if not a * subtx, or an adaptor in other cases. - * @param lineage - * The ancestor information. * @param root * The root id. * @param timeout @@ -232,7 +228,7 @@ private CompositeTransactionImp createCT ( String tid , private CoordinatorImp createCC (String recoveryDomainName, RecoveryCoordinator adaptor , String root, long timeout ) { - CoordinatorImp cc = null; + CoordinatorImp cc; if (maxTimeout_ > 0 && timeout > maxTimeout_ ) { timeout = maxTimeout_; @@ -387,7 +383,7 @@ public Participant getParticipant ( String root ) throws SysException } /** - * @see FSMEnterListener. + * @see FSMEnterListener */ public void entered ( FSMEnterEvent event ) @@ -426,13 +422,7 @@ public void rolledback ( CompositeTransaction tx ) public CompositeTransaction getCompositeTransaction ( String tid ) { - CompositeTransaction ret = null; - - synchronized ( tidToTransactionMap_ ) { - ret = (CompositeTransaction) tidToTransactionMap_.get ( tid.intern () ); - } - - return ret; + return tidToTransactionMap_.get(tid.intern()); } @@ -447,7 +437,7 @@ public CompositeTransaction getCompositeTransaction ( String tid ) CompositeTransaction createSubTransaction ( CompositeTransaction parent ) { if (Configuration.getConfigProperties().getAllowSubTransactions()) { - CompositeTransactionImp ret = null; + CompositeTransactionImp ret; Stack lineage = (Stack) parent.getLineage ().clone (); lineage.push ( parent ); String tid = tidmgr_.get (); @@ -484,8 +474,8 @@ public synchronized CompositeTransaction recreateCompositeTransaction (Propagati throw new IllegalStateException ( "Max number of active transactions reached:" + maxNumberOfActiveTransactions_ ); - CoordinatorImp cc = null; - CompositeTransaction ct = null; + CoordinatorImp cc; + CompositeTransaction ct; try { String tid = tidmgr_.get (); @@ -634,7 +624,7 @@ public CompositeTransaction createCompositeTransaction ( long timeout ) throws S } String tid = tidmgr_.get (); - Stack lineage = new Stack(); + Stack lineage = new Stack<>(); // create a CC with heuristic preference set to false, // since it does not really matter anyway (since we are // creating a root) From a364d258aafa9d0c2d3f507994e7b691c4511820 Mon Sep 17 00:00:00 2001 From: nikhildp Date: Fri, 15 Dec 2023 17:06:22 +0530 Subject: [PATCH 2/2] general cleanup, converted tidToTransactionMap_ to concurrent hash map, removed lock for read and write to same --- .../icatch/imp/TransactionServiceImp.java | 42 +++++++------------ 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/public/transactions/src/main/java/com/atomikos/icatch/imp/TransactionServiceImp.java b/public/transactions/src/main/java/com/atomikos/icatch/imp/TransactionServiceImp.java index c1a0ae11..97ac4b2b 100644 --- a/public/transactions/src/main/java/com/atomikos/icatch/imp/TransactionServiceImp.java +++ b/public/transactions/src/main/java/com/atomikos/icatch/imp/TransactionServiceImp.java @@ -10,11 +10,11 @@ import java.util.HashMap; import java.util.HashSet; -import java.util.Hashtable; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.Stack; +import java.util.concurrent.ConcurrentHashMap; import com.atomikos.finitestates.FSMEnterEvent; import com.atomikos.finitestates.FSMEnterListener; @@ -55,7 +55,7 @@ public class TransactionServiceImp implements TransactionServiceProvider, private long maxTimeout_; private Object[] rootLatches_ = null; - private Hashtable tidToTransactionMap_ = null; + private Map tidToTransactionMap_; private Map recreatedCoordinatorsByRootId = new HashMap<>(); private Map allCoordinatorsByCoordinatorId = new HashMap<>(); private boolean shutdownInProgress_ = false; @@ -91,7 +91,7 @@ public class TransactionServiceImp implements TransactionServiceProvider, * configurations that do not support orphan detection. * @param single_threaded_2pc * Whether 2PC commit should happen in the same thread that started the tx. - * @param recoveryLog2 + * @param recoveryLog * */ @@ -105,7 +105,7 @@ public TransactionServiceImp ( String name , initialized_ = false; recoverymanager_ = recoverymanager; tidmgr_ = tidmgr; - tidToTransactionMap_ = new Hashtable(); + tidToTransactionMap_ = new ConcurrentHashMap<>(); rootLatches_ = new Object[NUMLATCHES]; for (int i = 0; i < NUMLATCHES; i++) { rootLatches_[i] = new Object(); @@ -144,12 +144,10 @@ private Object getLatch ( String root ) private void setTidToTx ( String tid , CompositeTransaction ct ) throws IllegalStateException { - synchronized ( tidToTransactionMap_ ) { - if ( tidToTransactionMap_.containsKey ( tid.intern () ) ) - throw new IllegalStateException ( "Already mapped: " + tid ); - tidToTransactionMap_.put ( tid.intern (), ct ); - ct.addSubTxAwareParticipant(this); // for GC purposes - } + if (tidToTransactionMap_.containsKey(tid.intern())) + throw new IllegalStateException("Already mapped: " + tid); + tidToTransactionMap_.put(tid.intern(), ct); + ct.addSubTxAwareParticipant(this); // for GC purposes } /** @@ -215,11 +213,9 @@ private CompositeTransactionImp createCT ( String tid , * * @param recoveryDomainName The recovery domain of the superior of this coordinator. * - * @param RecoveryCoordinator + * @param adaptor * An existing coordinator for the given root. Null if not a * subtx, or an adaptor in other cases. - * @param lineage - * The ancestor information. * @param root * The root id. * @param timeout @@ -232,7 +228,7 @@ private CompositeTransactionImp createCT ( String tid , private CoordinatorImp createCC (String recoveryDomainName, RecoveryCoordinator adaptor , String root, long timeout ) { - CoordinatorImp cc = null; + CoordinatorImp cc; if (maxTimeout_ > 0 && timeout > maxTimeout_ ) { timeout = maxTimeout_; @@ -387,7 +383,7 @@ public Participant getParticipant ( String root ) throws SysException } /** - * @see FSMEnterListener. + * @see FSMEnterListener */ public void entered ( FSMEnterEvent event ) @@ -426,13 +422,7 @@ public void rolledback ( CompositeTransaction tx ) public CompositeTransaction getCompositeTransaction ( String tid ) { - CompositeTransaction ret = null; - - synchronized ( tidToTransactionMap_ ) { - ret = (CompositeTransaction) tidToTransactionMap_.get ( tid.intern () ); - } - - return ret; + return tidToTransactionMap_.get(tid.intern()); } @@ -447,7 +437,7 @@ public CompositeTransaction getCompositeTransaction ( String tid ) CompositeTransaction createSubTransaction ( CompositeTransaction parent ) { if (Configuration.getConfigProperties().getAllowSubTransactions()) { - CompositeTransactionImp ret = null; + CompositeTransactionImp ret; Stack lineage = (Stack) parent.getLineage ().clone (); lineage.push ( parent ); String tid = tidmgr_.get (); @@ -484,8 +474,8 @@ public synchronized CompositeTransaction recreateCompositeTransaction (Propagati throw new IllegalStateException ( "Max number of active transactions reached:" + maxNumberOfActiveTransactions_ ); - CoordinatorImp cc = null; - CompositeTransaction ct = null; + CoordinatorImp cc; + CompositeTransaction ct; try { String tid = tidmgr_.get (); @@ -634,7 +624,7 @@ public CompositeTransaction createCompositeTransaction ( long timeout ) throws S } String tid = tidmgr_.get (); - Stack lineage = new Stack(); + Stack lineage = new Stack<>(); // create a CC with heuristic preference set to false, // since it does not really matter anyway (since we are // creating a root)