[jboss-svn-commits] JBL Code SVN: r28502 - in labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src: main/java/uk/ac/ncl/sdia/a8905943/persistence/xa and 2 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Jul 27 12:40:32 EDT 2009


Author: whitingjr
Date: 2009-07-27 12:40:31 -0400 (Mon, 27 Jul 2009)
New Revision: 28502

Modified:
   labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/factory/TransactionFactory.java
   labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/persistence/xa/BranchMapping.java
   labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/persistence/xa/ResourceStatus.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/persistence/xa/JUnitTestSTMXAResource.java
Log:
Added test cases and resource implementation.

Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/factory/TransactionFactory.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/factory/TransactionFactory.java	2009-07-27 16:39:26 UTC (rev 28501)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/factory/TransactionFactory.java	2009-07-27 16:40:31 UTC (rev 28502)
@@ -9,7 +9,7 @@
  */
 public class TransactionFactory
 {
-
+   
    public static STMTransaction getCurrentTransaction()
    {
       //TODO: complete implementation

Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/persistence/xa/BranchMapping.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/persistence/xa/BranchMapping.java	2009-07-27 16:39:26 UTC (rev 28501)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/persistence/xa/BranchMapping.java	2009-07-27 16:40:31 UTC (rev 28502)
@@ -42,9 +42,10 @@
    {
       return resourceStatus.get();
    }
-   public void changeStatus(int status)
+   
+   public boolean changeStatus(int current, int next)
    {
-      resourceStatus.set(status  );
+      return resourceStatus.compareAndSet(current, next) ;
    }
    
 }

Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/persistence/xa/ResourceStatus.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/persistence/xa/ResourceStatus.java	2009-07-27 16:39:26 UTC (rev 28501)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/persistence/xa/ResourceStatus.java	2009-07-27 16:40:31 UTC (rev 28502)
@@ -12,5 +12,7 @@
    public static final int ACTIVE = 2;
    public static final int SUSPENDED = 3;
    public static final int COMPLETE = 4;
+   public static final int PREPARING = 5;
+   public static final int PREPARED = 6;
    
 }

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-07-27 16:39:26 UTC (rev 28501)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/persistence/xa/STMXAResource.java	2009-07-27 16:40:31 UTC (rev 28502)
@@ -37,7 +37,8 @@
     * connection, others may be associated. Non active associations allowed are
     * suspended or ended.*/
    private final Map<Xid, BranchMapping> branches = new HashMap<Xid, BranchMapping> ();
-   private AtomicReference<BranchMapping> currentBranch = new AtomicReference<BranchMapping>();
+   private AtomicReference<Xid> currentBranch = new AtomicReference<Xid>();
+   private final Map<Xid, BranchMapping> prepared = new HashMap<Xid, BranchMapping>();
    //TODO: jrw decide what is a valid measure of unique identity in the system, the value below is clumsy
    private final long rmid = System.currentTimeMillis();
    
@@ -48,30 +49,82 @@
       {
          throw new XAException("Cannot commit using an inactive connection.");
       }
-      /*
-      switch (xFlag)
+      if (!isPrepared (xid))
       {
-         case XAResource. :
-            
-            break;
-
-         default :
-            break;
+         throw new XAException(XAException.XAER_NOTA);
       }
-      */
+      /* inform the transactional system the prepared changes are to be committed. There should
+      not be any blocking by the STM. */
+      getXAConnection().getSTM().commit();
+      //TODO: complete the implementation to handle errors and mapping them to XAException types
    }
 
    @Override
    public void end(Xid xid, int xFlag) throws XAException
    {
+      if (logger.isTraceEnabled())
+      {
+         logger.trace("XAResource end called for branch ["+xid.toString()+"] and flag ["+xFlag+"]");
+      }
       if (!this.xaConnection.isActive())
       {
          throw new XAException(XAException.XA_RBPROTO);
       }
-      if (!this.currentBranch.get().getXidentity().equals(xid))
+      if (!isXidRecognised(xid))
       {
-         throw new XAException("Cannot end a branch of work. The resource manager does not recognize this branch.");
+         throw new XAException(XAException.XAER_NOTA);
       }
+      switch (xFlag)
+      {
+         case XAResource.TMSUSPEND :
+            
+            if (!isActive(xid))
+            {
+               throw new XAException("Attempted to XAResource.end suspend an inactive branch rejected.");
+            }
+            synchronized(this)
+            {
+               BranchMapping active = this.branches.get(xid);
+               boolean transitionSuccess = active.changeStatus(ResourceStatus.ACTIVE, ResourceStatus.SUSPENDED); 
+               if (!transitionSuccess)
+               {
+                  throw new XAException("Failed to transition an active branch to suspended status. ");
+               }
+               boolean deactivateSuccess = this.currentBranch.compareAndSet(xid, null);
+               if (!deactivateSuccess)
+               {
+                  throw new XAException("Failed to nullify currentActive branch id, this invariant has been corrupted.");
+               }
+            }
+            
+            break;
+
+         case XAResource.TMSUCCESS:
+            
+            if (!isActive(xid))
+            {
+               throw new XAException("Attempted to XAResource.end successful completion of an inactive branch rejected.");
+            }
+            synchronized(this)
+            {
+               boolean transitionSuccess = this.branches.get(xid).changeStatus(ResourceStatus.ACTIVE, ResourceStatus.COMPLETE);
+               if (!transitionSuccess)
+               {
+                  throw new XAException("Failed to transition an active branch to completed status. ");
+               }
+               boolean deactivateSuccess = this.currentBranch.compareAndSet(xid, null);
+               if (!deactivateSuccess)
+               {
+                  throw new XAException("Failed to nullify currentActive branch id, this invariant has been corrupted.");
+               }
+            }
+            
+            break;
+            
+         default :
+            throw new XAException(XAException.XA_RBOTHER);
+      }
+      
       //TODO:jrw pass through to the STM system 
       
    }
@@ -91,16 +144,21 @@
    }
 
    @Override
-   public boolean isSameRM(XAResource arg0) throws XAException
+   public boolean isSameRM(XAResource stranger) throws XAException
    {
-      // FIXME isSameRM
-      return false;
+      return this.equals(stranger);
    }
 
    @Override
-   public int prepare(Xid arg0) throws XAException
+   public int prepare(Xid xId) throws XAException
    {
-      // FIXME prepare
+      if (logger.isTraceEnabled())
+      {
+         logger.trace("XAResource.prepare called for branch ["+xId+"].");
+      }
+      isXidRecognised(xId);
+      
+      
       return 0;
    }
 
@@ -154,7 +212,11 @@
                   else if (ResourceStatus.SUSPENDED ==  mapping.getStatus())
                   {
                      logger.debug("Resuming transaction branch.");
-                     mapping.changeStatus(ResourceStatus.ACTIVE);  
+                     boolean transitionSuccess = mapping.changeStatus(ResourceStatus.SUSPENDED,  ResourceStatus.ACTIVE); 
+                     if (!transitionSuccess)
+                     {
+                        throw new XAException(XAException.XAER_RMERR);
+                     }
                   }
                   else if (ResourceStatus.ACTIVE == mapping.getStatus())
                   {
@@ -177,7 +239,7 @@
                         synchronized(this)
                         {
                            mapping  = new BranchMapping(xId, this.xaConnection, ResourceStatus.ACTIVE);
-                           this.currentBranch.set(mapping);
+                           this.currentBranch.set(xId);
                            this.branches.put(xId, mapping);
                         }
                      }
@@ -246,4 +308,25 @@
    {
       return this.xaConnection;
    }
+   
+   //
+   private boolean isActive(Xid xaIdentity)
+   {
+      boolean returnValue = false;
+      if (null != this.currentBranch.get() && ResourceStatus.ACTIVE == this.branches.get(xaIdentity).getStatus())
+      {
+         returnValue = true;
+      }
+      return  returnValue;
+   }
+   
+   private boolean isXidRecognised(Xid xid)
+   {
+      return this.branches.containsKey(xid);
+   }
+   
+   private boolean isPrepared(Xid xid)
+   {
+      return this.prepared.containsKey(xid);
+   }
 }

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-07-27 16:39:26 UTC (rev 28501)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/main/java/uk/ac/ncl/sdia/a8905943/stm/STM.java	2009-07-27 16:40:31 UTC (rev 28502)
@@ -5,6 +5,13 @@
 
 import org.apache.commons.lang.builder.EqualsBuilder;
 
+/**
+ * This object is a proxy to the transactional memory system. It provides a
+ * convenient interface to the world for any systems that interact with it.
+ * 
+ * @author <a href="whitingjr at hotmail.com">Jeremy Whiting</a>
+ * @version $Revision: 1.1 $
+ */
 public class STM
 {
    private final String databaseName;
@@ -47,6 +54,21 @@
       }
       return returnValue;
    }
+   
+   public void commit()
+   {
+      
+   }
+   
+   public void abort()
+   {
+      
+   }
+   
+   public void prepare()
+   {
+      
+   }
 
    public String getDatabaseName()
    {

Modified: labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/persistence/xa/JUnitTestSTMXAResource.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/persistence/xa/JUnitTestSTMXAResource.java	2009-07-27 16:39:26 UTC (rev 28501)
+++ labs/jbosstm/workspace/whitingjr/trunk/MVCCSampleSTM/src/test/java/uk/ac/ncl/sdia/a8905943/persistence/xa/JUnitTestSTMXAResource.java	2009-07-27 16:40:31 UTC (rev 28502)
@@ -95,4 +95,51 @@
       
    }
    
+   /**
+    * This test checks a running resouce can be suspended.
+    */
+   @Test
+   public void testCheckSuspendWorks()
+   {
+      Assert.assertNotNull(resourceManager);
+      Xid xid = new XidImple();
+      try
+      {
+         resourceManager.getXAConnection().getConnection();
+         resourceManager.start(xid, XAResource.TMNOFLAGS);
+         resourceManager.end(xid, XAResource.TMSUSPEND); // no exception expected
+      }
+      catch (XAException xae)
+      {// not expecting an exception here
+         Assert.fail("Not expecting an exception when suspending an active resource. message["+xae.getMessage()+"] code ["+xae.errorCode+"].");
+      }
+      catch (SQLException sqle)
+      {
+         Assert.fail("Failed to create a Connection object to get the resource manager to accept work.");
+      }
+   }
+   
+   /* This test checks the working scenario when a suspended  */
+   @Test
+   public void testCheckJoinAfterSuspendWorks()
+   {
+      Assert.assertNotNull(resourceManager);
+      Xid xid = new XidImple();
+      try
+      {
+         resourceManager.getXAConnection().getConnection();
+         resourceManager.start(xid, XAResource.TMNOFLAGS);
+         resourceManager.end(xid, XAResource.TMSUSPEND); 
+         resourceManager.start(xid, XAResource.TMJOIN);// no exception expected
+      }
+      catch (XAException xae)
+      {// not expecting an exception here
+         Assert.fail("Not expecting an exception when suspending an active resource. message["+xae.getMessage()+"] code ["+xae.errorCode+"].");
+      }
+      catch (SQLException sqle)
+      {
+         Assert.fail("Failed to create a Connection object to get the resource manager to accept work.");
+      }
+   }
+   
 }



More information about the jboss-svn-commits mailing list