[jboss-svn-commits] JBL Code SVN: r28969 - in labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src: main/java/uk/ac/ncl/sdia/a8905943/handle and 9 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Aug 18 04:58:36 EDT 2009
Author: whitingjr
Date: 2009-08-18 04:58:35 -0400 (Tue, 18 Aug 2009)
New Revision: 28969
Added:
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/transaction/
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/transaction/TransactionManager.java
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/stm/
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/stm/TestUnitSTM.java
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/stm/transaction/
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/stm/transaction/TestUnitTransactionManager.java
Modified:
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/entitymanager/STMEntityManagerImpl.java
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/handle/TransactedObjectReference.java
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/impl/TransactionImpl.java
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/isolation/Isolation.java
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/persistence/xa/STMXAResource.java
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/STM.java
labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/isolation/TestUnitRepeatableReadIsolationImpl.java
Log:
Added test cases.
Fixed usage problem with the configuring of isolation level.
Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/entitymanager/STMEntityManagerImpl.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/entitymanager/STMEntityManagerImpl.java 2009-08-18 03:31:50 UTC (rev 28968)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/entitymanager/STMEntityManagerImpl.java 2009-08-18 08:58:35 UTC (rev 28969)
@@ -39,7 +39,6 @@
@Override
public void clear()
{
- //TODO: detatch the entities managed by this connection.
STMFactory.getFactoryInstance().deallocate();
}
Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/handle/TransactedObjectReference.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/handle/TransactedObjectReference.java 2009-08-18 03:31:50 UTC (rev 28968)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/handle/TransactedObjectReference.java 2009-08-18 08:58:35 UTC (rev 28969)
@@ -18,7 +18,7 @@
final Long lookupIdentity;
private Object value;
private Long nextIdentity;
- private long version;
+ private Long version;
transient final ReentrantReadWriteLock prepared = new ReentrantReadWriteLock();
public TransactedObjectReference(Long identity, Object value, long version)
Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/impl/TransactionImpl.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/impl/TransactionImpl.java 2009-08-18 03:31:50 UTC (rev 28968)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/impl/TransactionImpl.java 2009-08-18 08:58:35 UTC (rev 28969)
@@ -27,7 +27,6 @@
*/
public class TransactionImpl implements STMTransaction
{
-
// status available for inspection
private volatile int status = Status.STATUS_NO_TRANSACTION;
@@ -39,7 +38,7 @@
* The version number of this transaction
*/
- private final long version;
+ private final Long version;
private static final Logger logger = Logger.getLogger(TransactionImpl.class);
@@ -95,4 +94,13 @@
this.isolation = isolation;
}
+ public Long getVersion()
+ {
+ return this.version;
+ }
+
+ public Map<Long, TransactedObjectReference> getDeferredReads()
+ {
+ return deferredReads;
+ }
}
Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/isolation/Isolation.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/isolation/Isolation.java 2009-08-18 03:31:50 UTC (rev 28968)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/isolation/Isolation.java 2009-08-18 08:58:35 UTC (rev 28969)
@@ -14,7 +14,7 @@
* Use this method to write a field object to the transaction.
* This system is a deferred update system so a reference to
* the
- * @param fieldWrite TODO
+ * @param fieldWrite Changes and resources required to make a write
* @param currentWriteSet
*
* @return
Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/persistence/xa/STMXAResource.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/persistence/xa/STMXAResource.java 2009-08-18 03:31:50 UTC (rev 28968)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/persistence/xa/STMXAResource.java 2009-08-18 08:58:35 UTC (rev 28969)
@@ -19,6 +19,8 @@
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.log4j.Logger;
+import uk.ac.ncl.sdia.a8905943.stm.exception.IsolationLevelNotConfiguredException;
+
/**
* This object delegates work and then maps application specific exceptions
* to XA Spec specific exceptions.
@@ -305,6 +307,14 @@
this.currentBranch.set(xId);
this.branches.put(xId, mapping);
}
+ try
+ {
+ this.xaConnection.getSTM().begin();
+ }
+ catch (IsolationLevelNotConfiguredException ilnce)
+ {// the STM was used before the isolation level was set
+ throw new XAException(XAException.XAER_RMERR);
+ }
}
else
{// existing branch association, should this be resumed not started?
Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/STM.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/STM.java 2009-08-18 03:31:50 UTC (rev 28968)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/STM.java 2009-08-18 08:58:35 UTC (rev 28969)
@@ -3,7 +3,6 @@
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.log4j.Logger;
import org.infinispan.Cache;
-import org.jboss.security.xacml.sunxacml.UnknownIdentifierException;
import uk.ac.ncl.sdia.a8905943.factory.TransactionFactory;
import uk.ac.ncl.sdia.a8905943.handle.TransactedObjectReference;
@@ -12,6 +11,7 @@
import uk.ac.ncl.sdia.a8905943.isolation.factory.IsolationFactory;
import uk.ac.ncl.sdia.a8905943.spec.STMTransaction;
import uk.ac.ncl.sdia.a8905943.stm.exception.IsolationLevelNotConfiguredException;
+import uk.ac.ncl.sdia.a8905943.stm.transaction.TransactionManager;
/**
* This object is a proxy to the transactional memory system. It provides a
@@ -24,7 +24,9 @@
{
private final String databaseName;
private static final Logger logger = Logger.getLogger(STM.class);
- private final Cache<Long, TransactedObjectReference> replicatedMemory ;
+ private final Cache<Long, TransactedObjectReference> replicatedFieldMemory ;
+ private final TransactionManager transactionManager = new TransactionManager();
+ private ThreadLocal<Integer> isolationLevel = new ThreadLocal<Integer>();
/**
* This method is where transactional updates are made..
@@ -35,23 +37,23 @@
*/
public Boolean write(FieldWriteParameter fieldWrite)
{
- fieldWrite.setObjectStore(this.replicatedMemory);
- TransactionFactory.getFactory().getCurrentTransaction(false).write(fieldWrite);
- return Boolean.FALSE;
+ Boolean returnValue = null;
+ fieldWrite.setObjectStore(this.replicatedFieldMemory);
+ returnValue = TransactionFactory.getFactory().getCurrentTransaction(false).write(fieldWrite);
+ return returnValue;
}
public Object read( FieldReadParameter fieldRead)
{
- fieldRead.setObjectStore(this.replicatedMemory);
+ fieldRead.setObjectStore(this.replicatedFieldMemory);
STMTransaction transaction = TransactionFactory.getFactory().getCurrentTransaction(false);
return transaction.read(fieldRead);
-
}
public STM(String name, Cache replicatedMemory)
{
this.databaseName = name;
- this.replicatedMemory = replicatedMemory;
+ this.replicatedFieldMemory = replicatedMemory;
}
@Override
@@ -89,19 +91,22 @@
*/
public void abort()
{
-
+ //TODO: jrw complete, complete implementation to release (unlock) prepared resources.
+ this.isolationLevel.remove();
}
/**
* Notify the transactional system the changes should be locked for future
- * updating in share transactional memory.
+ * updating in shared transactional memory.
*/
- public void prepare()
+ public boolean prepare()
{
-
+ //TODO:jrw complete implementation to return the appropriate response from the STM system
+ return false; //returning false will cause the transaction to be aborted
}
/**
* Inform the transactional system that transactional activity will start
- * to happen for this thread. Make changes in transactional memory.
+ * to happen for this thread. Make changes in transactional memory.
+ * @deprecated
*/
public void begin(int isolationLevel)
{
@@ -109,12 +114,37 @@
STMTransaction transaction = TransactionFactory.getFactory().getCurrentTransaction(true);
setCurrentTransactionIsolation(isolationLevel);
}
+
/**
+ * This method tells the STM to create a transaction. The transaction is
+ * allocated to the current thread.
+ * Requires the isolation level to have been set beforehand.
+ * This method clears the set isolation level to prevent mis-configured
+ * isolation levels from previous threads.
+ *
+ * @throws IsolationLevelNotConfiguredException
+ */
+ public void begin()
+ throws IsolationLevelNotConfiguredException
+ {
+ logger.debug("begin invoked on stm system.");
+ STMTransaction transaction = TransactionFactory.getFactory().getCurrentTransaction(true);
+ if (null != this.isolationLevel.get())
+ {
+ transaction.setIsolationLevel(IsolationFactory.createIsolationLevel(this.isolationLevel.get()));
+ this.isolationLevel.remove();
+ }
+ else
+ {
+ throw new IsolationLevelNotConfiguredException("Attempt made to begin an STM transaction when required isolation level has not been set for current thread.");
+ }
+ }
+ /**
* A problem occured in the system and transactional changes should be discarded.
*/
public void discard()
{
-
+ this.isolationLevel.remove();
}
public String getDatabaseName()
@@ -122,9 +152,16 @@
return databaseName;
}
+ /**
+ * Call this before the begin method.
+ *
+ * @param isolationLevel
+ */
public void setCurrentTransactionIsolation(int isolationLevel)
{
+ this.isolationLevel.set(isolationLevel);
STMTransaction transaction = TransactionFactory.getFactory().getCurrentTransaction(false);
+ /*
if (null != transaction)
{
transaction.setIsolationLevel(IsolationFactory.createIsolationLevel(isolationLevel));
@@ -133,5 +170,15 @@
{
logger.warn("Attempt to set the transaction isolation level when a transaction has not been created.");
}
+ */
}
+ /**
+ * This method is used to indicate no more changes are going to be issued to the
+ * STM for this thread-STMTransaction.
+ */
+ public void end()
+ {
+ //TODO:jrw, errm, do something......
+
+ }
}
Added: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/transaction/TransactionManager.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/transaction/TransactionManager.java (rev 0)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/transaction/TransactionManager.java 2009-08-18 08:58:35 UTC (rev 28969)
@@ -0,0 +1,79 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package uk.ac.ncl.sdia.a8905943.stm.transaction;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.log4j.Logger;
+import org.infinispan.Cache;
+
+import uk.ac.ncl.sdia.a8905943.handle.TransactedObjectReference;
+import uk.ac.ncl.sdia.a8905943.impl.TransactionImpl;
+import uk.ac.ncl.sdia.a8905943.spec.STMTransaction;
+
+public class TransactionManager
+{
+ private final Map<Long, TransactionImpl> transactionsKeyedByVersion = new ConcurrentHashMap<Long, TransactionImpl>();
+ private static final Logger logger = Logger.getLogger(TransactionManager.class);
+
+ public TransactionImpl findTransaction(Long version)
+ {
+ return this.transactionsKeyedByVersion.get(version);
+ }
+ public boolean addTransaction(TransactionImpl transaction)
+ {
+ return (null == this.transactionsKeyedByVersion.put(transaction.getVersion() , transaction) ? Boolean.TRUE.booleanValue(): Boolean.FALSE.booleanValue());
+ }
+
+ /**
+ * The purpose of this method is to lock all resources needed by the transaction. Need to keep a list of the
+ * fields locked. On a detected conflict backout. This means unlocking resources in reverse order.
+ *
+ * Also, between the validation and this prepare a concurrent transaction may have updated a
+ * field. Need to validate again after aquiring the lock. Yes this is not a great performance but
+ * at least it will work.
+ * Doing the "lock then validate" in reverse closes any window for a concurrent transaction to update the
+ * field. Again, upon any validation failure unlock the resources in reverse order.
+ *
+ * @param transaction the transaction to prepare
+ * @return did the prepare of resources succeed without conflict
+ */
+ public boolean prepare(TransactionImpl transaction)
+ {
+ boolean returnValue = false;
+
+ //TODO: jrw complete implemenation
+ return returnValue;
+ }
+ /**
+ * The purpose of this validate is to check all the fields in the transaction object
+ * for conflicting concurrent updates.
+ * This method does not change the data in the {@link STMTransaction} object.
+ * @param transaction
+ * @return
+ */
+ public boolean validate(TransactionImpl transaction, Cache<Long, TransactedObjectReference> replicatedFieldMemory)
+ {
+ boolean returnValue = true;
+ /* get the reads, check the version of all fields read during the transaction against all shared versions.*/
+ for (TransactedObjectReference read : transaction.getDeferredReads().values())
+ {
+ boolean fail = read.getVersion() == replicatedFieldMemory.get(read.getLookupIdentity()).getVersion();
+ if (fail)
+ {// validation failure occured
+ logger.info("Validation phase for STM Transaction found a serialization conflict. The validation failed for transaction.");
+ break; // drop out of further validation. Validating is over and nothing can recover the situation
+ }
+ }
+ return returnValue;
+ }
+
+ public TransactionManager()
+ {
+ }
+}
Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/isolation/TestUnitRepeatableReadIsolationImpl.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/isolation/TestUnitRepeatableReadIsolationImpl.java 2009-08-18 03:31:50 UTC (rev 28968)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/isolation/TestUnitRepeatableReadIsolationImpl.java 2009-08-18 08:58:35 UTC (rev 28969)
@@ -18,16 +18,13 @@
public class TestUnitRepeatableReadIsolationImpl
{
-/*
- @Ignore
+
+ @Test
public void testCheckReadIsolationFieldValueNotFound()
{
- TransactedObjectReference reference = new TransactedObjectReference(1l, "Larry", 1l);
- STMTransaction transaction = TransactionFactory.getFactory().getCurrentTransaction(true);
- transaction.setIsolationLevel(IsolationFactory.createIsolationLevel(Connection.TRANSACTION_REPEATABLE_READ)) ;
- Object found = transaction.read(new FieldReadParameter(1l));
+
}
- */
+
@Test
public void check()
{
Added: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/stm/TestUnitSTM.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/stm/TestUnitSTM.java (rev 0)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/stm/TestUnitSTM.java 2009-08-18 08:58:35 UTC (rev 28969)
@@ -0,0 +1,27 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package uk.ac.ncl.sdia.a8905943.stm;
+
+import org.infinispan.manager.CacheManager;
+import org.infinispan.manager.DefaultCacheManager;
+import org.junit.Test;
+
+import uk.ac.ncl.sdia.a8905943.factory.STMFactory;
+import uk.ac.ncl.sdia.a8905943.stm.exception.IsolationLevelNotConfiguredException;
+
+public class TestUnitSTM
+{
+ @Test (expected=IsolationLevelNotConfiguredException.class)
+ public void checkIsolationSetBeforeBeginningTransaction()
+ throws IsolationLevelNotConfiguredException
+ { // expected to throw an exception when stm instructred to begin before configured
+ CacheManager cacheManager = new DefaultCacheManager( );
+ STM stm = new STM("retail", cacheManager.getCache());
+ STMFactory.getFactoryInstance().allocate(stm);
+ stm.begin();
+ }
+}
Added: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/stm/transaction/TestUnitTransactionManager.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/stm/transaction/TestUnitTransactionManager.java (rev 0)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/stm/transaction/TestUnitTransactionManager.java 2009-08-18 08:58:35 UTC (rev 28969)
@@ -0,0 +1,57 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package uk.ac.ncl.sdia.a8905943.stm.transaction;
+
+import java.sql.Connection;
+
+import junit.framework.Assert;
+
+import org.infinispan.manager.CacheManager;
+import org.infinispan.manager.DefaultCacheManager;
+import org.junit.Before;
+import org.junit.Test;
+
+import uk.ac.ncl.sdia.a8905943.factory.STMFactory;
+import uk.ac.ncl.sdia.a8905943.stm.STM;
+import uk.ac.ncl.sdia.a8905943.stm.exception.IsolationLevelNotConfiguredException;
+
+public class TestUnitTransactionManager
+{
+
+ /**
+ * This test constructs a valid number of reads and validates them against the shared version.
+ */
+ @Test
+ public void checkValidationPasses()
+ {
+// Car car = new uk.ac.ncl.sdia.a8905943.model.Car
+
+
+ }
+ @Test
+ public void checkValidationConflictDetected()
+ {
+
+ }
+
+ @Before
+ public void setUp()
+ {
+ CacheManager cacheManager = new DefaultCacheManager( );
+ STM stm = new STM("retail", cacheManager.getCache());
+ STMFactory.getFactoryInstance().allocate(stm);
+ stm.setCurrentTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
+ try
+ {
+ stm.begin();
+ }
+ catch (IsolationLevelNotConfiguredException ilnce)
+ {
+ Assert.fail(ilnce.getMessage());
+ }
+ }
+}
More information about the jboss-svn-commits
mailing list