You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ch...@apache.org on 2009/06/29 16:02:45 UTC

svn commit: r789330 - in /activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker: TransactionManager.java XATransaction.java XidImpl.java

Author: chirino
Date: Mon Jun 29 14:02:45 2009
New Revision: 789330

URL: http://svn.apache.org/viewvc?rev=789330&view=rev
Log:
- Making the XidImpl a little simpler and frendiler to the use of Buffer classes.
- Streamlining the TransactionManager class.. Parsing Xids is not really required on the broker side.. so lets not force it.


Modified:
    activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker/TransactionManager.java
    activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker/XATransaction.java
    activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker/XidImpl.java

Modified: activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker/TransactionManager.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker/TransactionManager.java?rev=789330&r1=789329&r2=789330&view=diff
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker/TransactionManager.java (original)
+++ activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker/TransactionManager.java Mon Jun 29 14:02:45 2009
@@ -22,8 +22,6 @@
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicLong;
 
-import javax.transaction.xa.Xid;
-
 import org.apache.activemq.apollo.broker.BrokerDatabase.OperationContext;
 import org.apache.activemq.apollo.broker.Transaction.TxOp;
 import org.apache.activemq.broker.store.Store.MessageRecord;
@@ -157,7 +155,7 @@
      * @param xid
      * @return
      */
-    public final Transaction createTransaction(Xid xid) {
+    public final Transaction createTransaction(Buffer xid) {
         Transaction ret;
 
         long tid = tidGen.incrementAndGet();
@@ -230,8 +228,9 @@
             tx = new LocalTransaction(this, tid, queue);
             break;
         case Transaction.TYPE_XA:
-            XidImpl xid = new XidImpl();
-            xid.readbody(bais);
+        	int length = bais.readByte();
+        	Buffer xid = new Buffer(new byte[length]);
+        	bais.readFully(xid.data);
             tx = new XATransaction(this, tid, xid, queue);
             break;
         default:
@@ -252,7 +251,10 @@
         baos.writeLong(tx.getTid());
         if(tx.getType() == Transaction.TYPE_XA)
         {
-            ((XATransaction)tx).getXid().writebody(baos);
+        	Buffer xid = ((XATransaction)tx).getXid();
+        	// An XID max size is around 140 bytes
+        	baos.writeByte( xid.length );
+        	baos.write(xid.data, xid.offset, xid.length);
         }
         
         return database.updateMapEntry(TXN_MAP, tx.getBackingQueueName(), new Buffer(baos.getData(), 0, baos.size()));

Modified: activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker/XATransaction.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker/XATransaction.java?rev=789330&r1=789329&r2=789330&view=diff
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker/XATransaction.java (original)
+++ activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker/XATransaction.java Mon Jun 29 14:02:45 2009
@@ -19,10 +19,9 @@
 import java.io.IOException;
 
 import javax.transaction.xa.XAException;
-import javax.transaction.xa.Xid;
 
+import org.apache.activemq.protobuf.Buffer;
 import org.apache.activemq.queue.IQueue;
-import org.apache.activemq.util.DataByteArrayInputStream;
 
 /**
  * XATransaction
@@ -35,18 +34,14 @@
  */
 public class XATransaction extends Transaction {
 
-    private final XidImpl xid;
+    private final Buffer xid;
 
-    XATransaction(TransactionManager manager, long tid, Xid xid, IQueue<Long, TxOp> opQueue) {
+    XATransaction(TransactionManager manager, long tid, Buffer xid, IQueue<Long, TxOp> opQueue) {
         super(manager, tid, opQueue);
-        if (xid instanceof XidImpl) {
-            this.xid = XidImpl.class.cast(xid);
-        } else {
-            this.xid = new XidImpl(xid);
-        }
+        this.xid = xid;
     }
 
-    public XidImpl getXid() {
+    public Buffer getXid() {
         return xid;
     }
 

Modified: activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker/XidImpl.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker/XidImpl.java?rev=789330&r1=789329&r2=789330&view=diff
==============================================================================
--- activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker/XidImpl.java (original)
+++ activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/broker/XidImpl.java Mon Jun 29 14:02:45 2009
@@ -22,26 +22,20 @@
 
 import javax.transaction.xa.Xid;
 
+import org.apache.activemq.protobuf.Buffer;
+
 /**
- * An implementation of JTA transaction idenifier (javax.transaction.xa.Xid).
- * This is SonicMQ internal Xid. Any external Xid object will be converted to
- * this class.
+ * An implementation of JTA transaction identifier (javax.transaction.xa.Xid).
  */
 public class XidImpl implements Xid, Cloneable, java.io.Serializable {
 
-    //fix bug #8334
-    static final long serialVersionUID = -5363901495878210611L;
+    private static final long serialVersionUID = -5363901495878210611L;
+    private static final Buffer EMPTY_BUFFER = new Buffer(new byte[]{});
 
     // The format identifier for the XID. A value of -1 indicates the NULLXID
-    private int m_formatID = -1; // default format
-
-    private byte m_gtrid[];
-    // The number of bytes in the global transaction identfier
-    private int m_gtridLength; // Value from 1 through MAXGTRIDSIZE
-
-    private byte m_bqual[];
-    // The number of bytes in the branch qualifier
-    private int m_bqualLength; // Value from 1 through MAXBQUALSIZE
+    private int formatId = -1; // default format
+    Buffer globalTransactionId = EMPTY_BUFFER;
+    Buffer branchQualifier = EMPTY_BUFFER;
 
     /////////////////////////////// Constructors /////////////////////////////
     /**
@@ -50,15 +44,20 @@
      * After construction the data within the XID should be initialized.
      */
     public XidImpl() {
-        this(-1, null, null);
     }
 
     public XidImpl(int formatID, byte[] globalTxnID, byte[] branchID) {
-        m_formatID = formatID;
+        this.formatId = formatID;
         setGlobalTransactionId(globalTxnID);
         setBranchQualifier(branchID);
     }
 
+    public XidImpl(int formatID, Buffer globalTransactionId,  Buffer branchQualifier) {
+        this.formatId = formatID;
+        this.globalTransactionId = globalTransactionId;
+        this.branchQualifier=branchQualifier;
+    }
+
     /**
      * Initialize an XID using another XID as the source of data.
      * 
@@ -66,13 +65,12 @@
      *            the XID to initialize this XID from
      */
     public XidImpl(Xid from) {
-
         if ((from == null) || (from.getFormatId() == -1)) {
-            m_formatID = -1;
+            formatId = -1;
             setGlobalTransactionId(null);
             setBranchQualifier(null);
         } else {
-            m_formatID = from.getFormatId();
+            formatId = from.getFormatId();
             setGlobalTransactionId(from.getGlobalTransactionId());
             setBranchQualifier(from.getBranchQualifier());
         }
@@ -81,9 +79,7 @@
 
     // used for test purpose
     public XidImpl(String globalTxnId, String branchId) {
-
         this(99, globalTxnId.getBytes(), branchId.getBytes());
-
     }
 
     //////////// Public Methods //////////////
@@ -98,22 +94,16 @@
      *         transaction as this, otherwise returns false.
      */
     public boolean equals(Object o) {
-        Xid other;
-
-        if (!(o instanceof Xid))
+        if (o.getClass() != XidImpl.class)
             return false;
 
-        other = (Xid) o;
-
-        if (m_formatID == -1 && other.getFormatId() == -1)
+        XidImpl other = (XidImpl) o;
+        if (formatId == -1 && other.formatId == -1)
             return true;
 
-        if (m_formatID != other.getFormatId() || m_gtridLength != other.getGlobalTransactionId().length || m_bqualLength != other.getBranchQualifier().length) {
-            return false;
-        }
-
-        return isEqualGtrid(other) && isEqualBranchQualifier(other.getBranchQualifier());
-
+        return formatId == other.formatId 
+        		&& globalTransactionId.equals(other.globalTransactionId)
+        		&& branchQualifier.equals(other.branchQualifier);
     }
 
     /**
@@ -121,14 +111,10 @@
      * 
      * @return the computed hashcode
      */
-
     public int hashCode() {
-
-        if (m_formatID == -1)
+        if (formatId == -1)
             return (-1);
-
-        return m_formatID + m_gtridLength - m_bqualLength;
-
+        return formatId ^ globalTransactionId.hashCode() ^ branchQualifier.hashCode();
     }
 
     /**
@@ -142,7 +128,7 @@
     public String toString() {
         String gtString = new String(getGlobalTransactionId());
         String brString = new String(getBranchQualifier());
-        return new String("{Xid: " + "formatID=" + m_formatID + ", " + "gtrid[" + m_gtridLength + "]=" + gtString + ", " + "brid[" + m_bqualLength + "]=" + brString + "}");
+        return new String("{Xid: " + "formatID=" + formatId + ", " + "gtrid[" + globalTransactionId.length + "]=" + gtString + ", " + "brid[" + branchQualifier.length + "]=" + brString + "}");
 
     }
 
@@ -152,7 +138,7 @@
      * @return Format identifier. -1 indicates a null XID
      */
     public int getFormatId() {
-        return m_formatID;
+        return formatId;
     }
 
     /**
@@ -161,7 +147,8 @@
      * @return the global transaction identifier
      */
     public byte[] getGlobalTransactionId() {
-        return m_gtrid;
+    	// TODO.. may want to compact() first and keep cache that..
+        return globalTransactionId.toByteArray();
     }
 
     /**
@@ -170,7 +157,8 @@
      * @return the branch qualifier
      */
     public byte[] getBranchQualifier() {
-        return m_bqual;
+    	// TODO.. may want to compact() first and keep cache that..
+        return branchQualifier.toByteArray();
     }
 
     ///////////////////////// private methods ////////////////////////////////
@@ -186,76 +174,32 @@
      */
     private void setBranchQualifier(byte[] branchID) {
         if (branchID == null) {
-            m_bqualLength = 0;
-            m_bqual = new byte[m_bqualLength];
+            branchQualifier = EMPTY_BUFFER;
         } else {
-            m_bqualLength = branchID.length > MAXBQUALSIZE ? MAXBQUALSIZE : branchID.length;
-            m_bqual = new byte[m_bqualLength];
-            System.arraycopy(branchID, 0, m_bqual, 0, m_bqualLength);
+            int length = branchID.length > MAXBQUALSIZE ? MAXBQUALSIZE : branchID.length;
+            // TODO: Do we really need to copy the bytes??
+            branchQualifier = new Buffer(new byte[length]);
+            System.arraycopy(branchID, 0, branchQualifier.data, 0, length);
         }
     }
 
     private void setGlobalTransactionId(byte[] globalTxnID) {
         if (globalTxnID == null) {
-            m_gtridLength = 0;
-            m_gtrid = new byte[m_gtridLength];
+            globalTransactionId = EMPTY_BUFFER;
         } else {
-            m_gtridLength = globalTxnID.length > MAXGTRIDSIZE ? MAXGTRIDSIZE : globalTxnID.length;
-            m_gtrid = new byte[m_gtridLength];
-            System.arraycopy(globalTxnID, 0, m_gtrid, 0, m_gtridLength);
+        	int length = globalTxnID.length > MAXGTRIDSIZE ? MAXGTRIDSIZE : globalTxnID.length;
+            // TODO: Do we really need to copy the bytes??
+            globalTransactionId = new Buffer(new byte[length]);
+            System.arraycopy(globalTxnID, 0, globalTransactionId.data, 0, length);
         }
     }
 
-    /**
-     * Return whether the Gtrid of this is equal to the Gtrid of xid
-     */
-    private boolean isEqualGtrid(Xid xid) {
-        byte[] xidGtrid = xid.getGlobalTransactionId();
-
-        if (getGlobalTransactionId() == null && xidGtrid == null)
-            return true;
-        if (getGlobalTransactionId() == null)
-            return false;
-        if (xidGtrid == null)
-            return false;
-
-        if (m_gtridLength != xidGtrid.length) {
-            return false;
-        }
-
-        for (int i = 0; i < m_gtridLength; i++) {
-            if (m_gtrid[i] != xidGtrid[i])
-                return false;
-        }
-        return true;
-    }
-
-    /**
-     * Determine if an array of bytes equals the branch qualifier
-     * 
-     * @return true if equal
-     */
-    private boolean isEqualBranchQualifier(byte[] data) {
-
-        int L = data.length > MAXBQUALSIZE ? MAXBQUALSIZE : data.length;
-
-        if (L != m_bqualLength)
-            return false;
-
-        for (int i = 0; i < m_bqualLength; i++) {
-            if (data[i] != m_bqual[i])
-                return false;
-        }
-
-        return true;
-    }
-
     public int getMemorySize() {
         return 4 // formatId
                 + 4 // length of globalTxnId
-                + m_gtridLength // globalTxnId
+                + globalTransactionId.length // globalTxnId
                 + 4 // length of branchId
-                + m_bqualLength; // branchId
+                + branchQualifier.length; // branchId
     }
 
     /**
@@ -272,12 +216,12 @@
      *                written.
      */
     public void writebody(DataOutput out) throws IOException {
-        out.writeInt(m_formatID); // format ID
+        out.writeInt(formatId); // format ID
 
-        out.writeInt(m_gtridLength); // length of global Txn ID
-        out.write(getGlobalTransactionId(), 0, m_gtridLength); // global transaction ID
-        out.writeInt(m_bqualLength); // length of branch ID
-        out.write(getBranchQualifier(), 0, m_bqualLength); // branch ID
+        out.writeInt(globalTransactionId.length); // length of global Txn ID
+        out.write(globalTransactionId.data, globalTransactionId.offset, globalTransactionId.length); // global transaction ID
+        out.writeInt(branchQualifier.length); // length of branch ID
+        out.write(branchQualifier.data, branchQualifier.offset, branchQualifier.length); // branch ID
     }
 
     /**
@@ -288,16 +232,16 @@
      * @throws IOException
      */
     public void readbody(DataInput in) throws IOException {
-        m_formatID = in.readInt();
-        int gtidLen = in.readInt();
-        byte[] globalTxnId = new byte[gtidLen];
-        in.readFully(globalTxnId, 0, gtidLen);
-
-        int brlen = in.readInt();
-        byte[] branchId = new byte[brlen];
-        in.readFully(branchId, 0, brlen);
+        formatId = in.readInt();
 
-        setGlobalTransactionId(globalTxnId);
-        setBranchQualifier(branchId);
+        int length = in.readInt();
+        byte[] data = new byte[length];
+        in.readFully(data);
+        setGlobalTransactionId(data);
+
+        length = in.readInt();
+        data = new byte[length];
+        in.readFully(data);
+        setBranchQualifier(data);
     }
 } // class XidImpl