Permalink
Browse files

performance improvements

  • Loading branch information...
1 parent 68f7a7d commit 237fe4d929e923a8780e96a496c87a033f4148bc @nmcl nmcl committed Nov 8, 2012
View
31 ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/StateManager.java
@@ -35,6 +35,7 @@
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Hashtable;
+import java.util.concurrent.locks.ReentrantLock;
import com.arjuna.ats.arjuna.common.Uid;
import com.arjuna.ats.arjuna.common.arjPropertyManager;
@@ -60,7 +61,6 @@
import com.arjuna.ats.internal.arjuna.abstractrecords.DisposeRecord;
import com.arjuna.ats.internal.arjuna.abstractrecords.PersistenceRecord;
import com.arjuna.ats.internal.arjuna.abstractrecords.RecoveryRecord;
-import com.arjuna.ats.internal.arjuna.common.BasicMutex;
import com.arjuna.ats.internal.arjuna.common.UidHelper;
/**
@@ -1314,7 +1314,7 @@ protected final synchronized boolean rememberAction (BasicAction action,
* @since JTS 2.1.
*/
- protected final BasicMutex getMutex ()
+ protected final ReentrantLock getMutex ()
{
return mutex;
}
@@ -1326,10 +1326,16 @@ protected final BasicMutex getMutex ()
protected final boolean lockMutex ()
{
- if (mutex.lock() == BasicMutex.LOCKED)
+ try
+ {
+ mutex.lock();
+
return true;
- else
+ }
+ catch (final Throwable ex)
+ {
return false;
+ }
}
/**
@@ -1339,10 +1345,16 @@ protected final boolean lockMutex ()
protected final boolean unlockMutex ()
{
- if (mutex.unlock() == BasicMutex.UNLOCKED)
+ try
+ {
+ mutex.unlock();
+
return true;
- else
+ }
+ catch (final Throwable ex)
+ {
return false;
+ }
}
/**
@@ -1353,10 +1365,7 @@ protected final boolean unlockMutex ()
protected final boolean tryLockMutex ()
{
- if (mutex.tryLock() == BasicMutex.LOCKED)
- return true;
- else
- return false;
+ return mutex.tryLock();
}
/*
@@ -1395,7 +1404,7 @@ synchronized final void destroyed ()
private String storeRoot;
- private BasicMutex mutex = new BasicMutex();
+ private ReentrantLock mutex = new ReentrantLock();
private static final String marker = "#ARJUNA#";
}
View
62 ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/common/Mutex.java
@@ -1,62 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2006, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags.
- * See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License, v. 2.1.
- * This program is distributed in the hope that it will be useful, but WITHOUT A
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
- * You should have received a copy of the GNU Lesser General Public License,
- * v.2.1 along with this distribution; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- * (C) 2005-2006,
- * @author JBoss Inc.
- */
-/*
- * Copyright (C) 1998, 1999, 2000,
- *
- * Arjuna Solutions Limited,
- * Newcastle upon Tyne,
- * Tyne and Wear,
- * UK.
- *
- * $Id: Semaphore.java 2342 2006-03-30 13:06:17Z $
- */
-
-package com.arjuna.ats.arjuna.common;
-
-/**
- * Implementations of semaphore classes should derive from this
- * class and provide implementations of the relevant methods.
- *
- * @author Mark Little (mark@arjuna.com)
- * @version $Id$
- * @since JTS 1.0.
- */
-
-public interface Mutex
-{
- public static final int LOCKED = 0;
-
- public static final int UNLOCKED = 1;
-
- public static final int WOULD_BLOCK = 2;
-
- public static final int ERROR = 3;
-
- /*
- * Classic semaphore operations.
- */
-
- public int lock ();
-
- public int unlock ();
-
- public int tryLock ();
-}
View
2 ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/coordinator/CheckedAction.java
@@ -55,6 +55,8 @@
* associated with the transaction. The supplied information should be
* sufficient for application specific implementations to do useful work
* (such as synchronizing on the threads).
+ *
+ * The default implementation simply prints a warning.
*/
public void check (boolean isCommit, Uid actUid, Hashtable list)
View
200 ArjunaCore/arjuna/classes/com/arjuna/ats/internal/arjuna/common/BasicMutex.java
@@ -1,200 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2006, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags.
- * See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License, v. 2.1.
- * This program is distributed in the hope that it will be useful, but WITHOUT A
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
- * You should have received a copy of the GNU Lesser General Public License,
- * v.2.1 along with this distribution; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- * (C) 2005-2006,
- * @author JBoss Inc.
- */
-/*
- * Copyright (C) 2001,
- *
- * Arjuna Solutions Limited,
- * Newcastle upon Tyne,
- * Tyne and Wear,
- * UK.
- *
- * $Id: Mutex.java 2342 2006-03-30 13:06:17Z $
- */
-
-package com.arjuna.ats.internal.arjuna.common;
-
-import com.arjuna.ats.arjuna.common.Mutex;
-import com.arjuna.ats.arjuna.logging.tsLogger;
-
-/**
- * This is a reentrant Mutex implementation.
- *
- * @author Mark Little (mark@arjuna.com)
- * @version $Id: Mutex.java 2342 2006-03-30 13:06:17Z $
- * @since 1.0.
- */
-
-public class BasicMutex implements Mutex
-{
- public BasicMutex ()
- {
- // this(true);
- this(false);
- }
-
- public BasicMutex (boolean reentrant)
- {
- _lock = new Object();
- _users = 0;
- _owner = null;
- _reentrant = reentrant;
- _recursiveCalls = 0;
- }
-
- /**
- * Classic mutex operations.
- */
-
- public int lock ()
- {
- if (tsLogger.logger.isTraceEnabled()) {
- tsLogger.logger.trace("Mutex::lock()");
- }
-
- synchronized (_lock)
- {
- if (_users == 0)
- {
- _users = 1;
-
- if (_reentrant)
- {
- _owner = Thread.currentThread();
- _recursiveCalls = 1;
- }
- }
- else
- {
- boolean done = false;
-
- if (_reentrant)
- {
- if (_owner == Thread.currentThread())
- {
- _recursiveCalls++;
-
- done = true;
- }
- }
-
- if (!done)
- {
- _users++;
-
- try
- {
- _lock.wait();
- }
- catch (InterruptedException e)
- {
- }
- }
- }
- }
-
- return BasicMutex.LOCKED;
- }
-
- public int unlock ()
- {
- if (tsLogger.logger.isTraceEnabled()) {
- tsLogger.logger.trace("Mutex::unlock()");
- }
-
- synchronized (_lock)
- {
- if (_users <= 0) {
- tsLogger.i18NLogger.warn_common_Mutex_2();
-
- return BasicMutex.ERROR;
- }
- else
- {
- boolean done = false;
-
- if (_reentrant)
- {
- if (_owner == Thread.currentThread())
- {
- if (--_recursiveCalls == 0)
- _owner = null;
-
- done = true;
- }
- else {
- tsLogger.i18NLogger.warn_common_Mutex_2();
-
- return BasicMutex.LOCKED;
- }
- }
-
- if (!done)
- {
- if (--_users >= 0)
- {
- _lock.notify();
- }
- }
- }
- }
-
- return BasicMutex.UNLOCKED;
- }
-
- public int tryLock ()
- {
- if (tsLogger.logger.isTraceEnabled()) {
- tsLogger.logger.trace("Mutex::tryLock()");
- }
-
- synchronized (_lock)
- {
- if (_users == 0)
- return lock();
- else
- {
- if (_reentrant)
- {
- if (_owner == Thread.currentThread())
- return lock();
- }
-
- return BasicMutex.WOULD_BLOCK;
- }
- }
- }
-
- private Object _lock;
- private int _users;
- private Thread _owner;
- private boolean _reentrant;
- private int _recursiveCalls;
-
-}
-
-
-
-
-
-
-
-
-
View
11 .../arjuna/classes/com/arjuna/ats/internal/arjuna/coordinator/CheckedActionFactoryImple.java
@@ -41,8 +41,17 @@
public class CheckedActionFactoryImple implements CheckedActionFactory
{
+ /**
+ * This implementation returns the same CheckedAction instance for every transaction.
+ * Since the check method of the instance returned is stateless this works. But it
+ * is not guaranteed to be correct for other implementations of CheckedAction so only
+ * follow this pattern if you understand the implications.
+ */
+
public CheckedAction getCheckedAction (final Uid txId, final String actionType)
{
- return new CheckedAction();
+ return _theAction;
}
+
+ private final CheckedAction _theAction = new CheckedAction();
}
View
128 ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/common/MutexUnitTest.java
@@ -1,128 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2007, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags.
- * See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License, v. 2.1.
- * This program is distributed in the hope that it will be useful, but WITHOUT A
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
- * You should have received a copy of the GNU Lesser General Public License,
- * v.2.1 along with this distribution; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- * (C) 2007,
- * @author JBoss, a division of Red Hat.
- */
-package com.hp.mwtests.ts.arjuna.common;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
-import org.junit.Test;
-
-import com.arjuna.ats.internal.arjuna.common.BasicMutex;
-
-class MutexThread extends Thread
-{
- public MutexThread (BasicMutex share)
- {
- _mutex = share;
- }
-
- public void run ()
- {
- if (_mutex.lock() == BasicMutex.LOCKED)
- {
- for (int i = 0; i < 1000; i++)
- {
- // do some work
-
- System.currentTimeMillis();
- }
-
- _mutex.unlock();
- }
-
- _done = true;
- }
-
- public boolean valid ()
- {
- return _done;
- }
-
- private BasicMutex _mutex;
- private boolean _done = false;
-}
-
-public class MutexUnitTest
-{
- @Test
- public void testBasicMutex () throws Exception
- {
- BasicMutex mx = new BasicMutex();
-
- mx.lock();
-
- mx.unlock();
- }
-
- @Test
- public void testThreaded () throws Exception
- {
- BasicMutex mx = new BasicMutex();
- MutexThread mt1 = new MutexThread(mx);
- MutexThread mt2 = new MutexThread(mx);
-
- mt1.run();
- mt2.run();
-
- try
- {
- mt1.join();
- mt2.join();
- }
- catch (final Throwable ex)
- {
- fail();
- }
-
- assertTrue(mt1.valid());
- assertTrue(mt2.valid());
- }
-
- @Test
- public void testInvalid () throws Exception
- {
- BasicMutex mx = new BasicMutex();
-
- assertEquals(mx.unlock(), BasicMutex.ERROR);
- }
-
- @Test
- public void testReentrantMutex () throws Exception
- {
- BasicMutex mx = new BasicMutex();
-
- mx.lock();
-
- assertEquals(mx.tryLock(), BasicMutex.WOULD_BLOCK);
-
- mx.unlock();
-
- mx = new BasicMutex(true);
-
- mx.lock();
-
- assertEquals(mx.tryLock(), BasicMutex.LOCKED);
-
- mx.unlock();
- mx.unlock();
- }
-}
View
149 ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/performance/Performance4.java
@@ -0,0 +1,149 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006,
+ * @author JBoss Inc.
+ */
+/*
+ * Copyright (C) 2004,
+ *
+ * Arjuna Technologies Ltd,
+ * Newcastle upon Tyne,
+ * Tyne and Wear,
+ * UK.
+ *
+ * $Id: Performance2.java 2342 2006-03-30 13:06:17Z $
+ */
+
+package com.hp.mwtests.ts.arjuna.performance;
+
+import java.util.Calendar;
+
+import org.junit.Test;
+
+import com.arjuna.ats.arjuna.AtomicAction;
+import com.arjuna.ats.arjuna.common.arjPropertyManager;
+import com.arjuna.ats.internal.arjuna.objectstore.TwoPhaseVolatileStore;
+import com.hp.mwtests.ts.arjuna.resources.BasicRecord;
+
+class Worker4 extends Thread
+{
+
+ public Worker4(int iters)
+ {
+ _iters = iters;
+ }
+
+ public void run()
+ {
+ for (int i = 0; i < _iters; i++) {
+ try {
+ AtomicAction A = new AtomicAction();
+
+ A.begin();
+
+ A.add(new BasicRecord());
+ A.add(new BasicRecord());
+
+ A.commit();
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ Performance4.doSignal();
+ }
+
+ private int _iters;
+
+}
+
+
+public class Performance4
+{
+ public static void main (String[] args)
+ {
+ Performance4 obj = new Performance4();
+
+ obj.setWorkSize(1000);
+
+ obj.test();
+ }
+
+ public void setWorkSize (int size)
+ {
+ _sizeOfWork = size;
+ }
+
+ @Test
+ public void test()
+ {
+ int threads = 10;
+ int work = _sizeOfWork;
+
+ arjPropertyManager.getCoordinatorEnvironmentBean().setCommitOnePhase(false);
+ arjPropertyManager.getObjectStoreEnvironmentBean().setObjectStoreType(TwoPhaseVolatileStore.class.getName());
+
+ number = threads;
+
+ int numberOfTransactions = threads * work;
+ long stime = Calendar.getInstance().getTime().getTime();
+ Worker4[] workers = new Worker4[threads];
+
+ for (int i = 0; i < threads; i++) {
+ workers[i] = new Worker4(work);
+
+ workers[i].start();
+ }
+
+ Performance4.doWait();
+
+ long ftime = Calendar.getInstance().getTime().getTime();
+ long timeTaken = ftime - stime;
+
+ System.out.println("time for " + numberOfTransactions + " write transactions is " + timeTaken);
+ System.out.println("number of transactions: " + numberOfTransactions);
+ System.out.println("throughput: " + (float) (numberOfTransactions / (timeTaken / 1000.0)));
+ }
+
+ public static void doWait()
+ {
+ try {
+ synchronized (sync) {
+ if (number > 0)
+ sync.wait();
+ }
+ }
+ catch (Exception e) {
+ }
+ }
+
+ public static void doSignal()
+ {
+ synchronized (sync) {
+ if (--number == 0)
+ sync.notify();
+ }
+ }
+
+ int _sizeOfWork = 100;
+
+ private static Object sync = new Object();
+ private static int number = 0;
+
+}
View
60 ArjunaCore/txoj/classes/com/arjuna/ats/txoj/LockManager.java
@@ -34,12 +34,12 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
+import java.util.concurrent.locks.ReentrantLock;
import com.arjuna.ats.arjuna.ObjectModel;
import com.arjuna.ats.arjuna.ObjectStatus;
import com.arjuna.ats.arjuna.ObjectType;
import com.arjuna.ats.arjuna.StateManager;
-import com.arjuna.ats.arjuna.common.Mutex;
import com.arjuna.ats.arjuna.common.ObjectStoreEnvironmentBean;
import com.arjuna.ats.arjuna.common.Uid;
import com.arjuna.ats.arjuna.coordinator.AbstractRecord;
@@ -141,7 +141,7 @@ public void finalize () throws Throwable
if (mutex != null)
{
- if (mutex.lock() == Mutex.LOCKED)
+ if (mutex.isLocked())
doSignal = true;
}
@@ -939,40 +939,38 @@ protected final boolean initialise ()
if (mutex == null)
{
- // TODO add a factory if we ever have more than one implementation
- mutex = new com.arjuna.ats.internal.arjuna.common.BasicMutex();
+ mutex = new ReentrantLock();
}
if (mutex != null)
{
- if (mutex.lock() == Mutex.LOCKED)
+ mutex.lock();
+
+ /*
+ * At some point we may want to add a factory to hide this, but
+ * since we only have two implementations at the moment it is perhaps
+ * overkill.
+ *
+ * TODO add factory.
+ */
+
+ if (lockStore == null)
{
- /*
- * At some point we may want to add a factory to hide this, but
- * since we only have two implementations at the moment it is perhaps
- * overkill.
- *
- * TODO add factory.
- */
-
- if (lockStore == null)
+ try
{
- try
- {
- if (lockStoreType.equals(BasicLockStore.class.getName())) {
- lockStore = new BasicLockStore();
- } else {
- ObjectStoreEnvironmentBean objectStoreEnvironmentBean = new ObjectStoreEnvironmentBean();
- objectStoreEnvironmentBean.setLocalOSRoot(systemKey);
- lockStore = new BasicPersistentLockStore(objectStoreEnvironmentBean);
- }
- }
- catch (final Exception ex)
- {
- lockStore = null;
+ if (lockStoreType.equals(BasicLockStore.class.getName())) {
+ lockStore = new BasicLockStore();
+ } else {
+ ObjectStoreEnvironmentBean objectStoreEnvironmentBean = new ObjectStoreEnvironmentBean();
+ objectStoreEnvironmentBean.setLocalOSRoot(systemKey);
+ lockStore = new BasicPersistentLockStore(objectStoreEnvironmentBean);
}
}
+ catch (final Exception ex)
+ {
+ lockStore = null;
+ }
}
mutex.unlock();
@@ -1030,7 +1028,7 @@ protected final boolean loadState ()
return false; /* init failed */
}
- if ((mutex == null) || (mutex.tryLock() == Mutex.WOULD_BLOCK))
+ if ((mutex == null) || (!mutex.tryLock()))
{
return false;
}
@@ -1297,9 +1295,9 @@ protected final boolean unloadState ()
protected boolean hasBeenLocked;/* Locked at least once */
protected boolean objectLocked;/* Semaphore grabbed */
-
- protected com.arjuna.ats.internal.arjuna.common.BasicMutex mutex; /* Controls access to the lock store */
-
+
+ protected ReentrantLock mutex; /* Controls access to the lock store */
+
protected LockConflictManager conflictManager;
protected static final int DOZE_TIME = 1000000;
View
5 ArjunaJTS/jts/classes/com/arjuna/ats/internal/jts/interposition/FactoryList.java
@@ -32,11 +32,12 @@
package com.arjuna.ats.internal.jts.interposition;
+import java.util.concurrent.locks.ReentrantLock;
+
import org.omg.CORBA.SystemException;
import org.omg.CosTransactions.Control;
import org.omg.CosTransactions.PropagationContext;
-import com.arjuna.ats.internal.arjuna.common.BasicMutex;
import com.arjuna.ats.internal.jts.interposition.resources.arjuna.InterpositionCreator;
import com.arjuna.ats.internal.jts.interposition.resources.osi.OSIInterpositionCreator;
import com.arjuna.ats.internal.jts.interposition.resources.restricted.RestrictedInterpositionCreator;
@@ -256,6 +257,6 @@ protected static FactoryElement find (int formatID)
private static FactoryElement _list = null;
private static FactoryElement _default = null; // used if no formatID
// values match.
- private static BasicMutex _lock = new BasicMutex();
+ private static ReentrantLock _lock = new ReentrantLock();
}

0 comments on commit 237fe4d

Please sign in to comment.