Author: dereed
Date: 2012-08-05 13:20:57 -0400 (Sun, 05 Aug 2012)
New Revision: 8549
Modified:
core/trunk/src/main/java/org/jboss/cache/transaction/GlobalTransaction.java
Log:
[JBCACHE-1621] Make GlobalTransaction unique even across a restart of an instance using
the same IP:port
Modified: core/trunk/src/main/java/org/jboss/cache/transaction/GlobalTransaction.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/transaction/GlobalTransaction.java 2012-08-02
21:22:33 UTC (rev 8548)
+++ core/trunk/src/main/java/org/jboss/cache/transaction/GlobalTransaction.java 2012-08-05
17:20:57 UTC (rev 8549)
@@ -23,11 +23,14 @@
import org.jgroups.Address;
+import org.jgroups.stack.IpAddress;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
+import java.util.Arrays;
+import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
@@ -47,6 +50,13 @@
private static AtomicLong sid = new AtomicLong(0);
+ // addr + instanceId = Unique ID for the cache even after restarting with the same
IP:port
+ private static byte[] instanceId = new byte[4];
+ static
+ {
+ new Random().nextBytes(instanceId);
+ }
+
private Address addr = null;
private long id = -1;
private transient boolean remote = false;
@@ -63,6 +73,18 @@
private GlobalTransaction(Address addr)
{
+ // store instanceId in IpAddress.additionalData (backwards compatible
serialization)
+ if(addr != null && addr instanceof IpAddress)
+ {
+ try
+ {
+ addr = (Address)((IpAddress)addr).clone();
+ ((IpAddress)addr).setAdditionalData(instanceId);
+ }
+ catch ( CloneNotSupportedException e )
+ {
+ }
+ }
this.addr = addr;
id = sid.getAndIncrement();
}
@@ -77,6 +99,7 @@
return addr;
}
+ // only used during externalization
public void setAddress(Address address)
{
addr = address;
@@ -87,12 +110,20 @@
return id;
}
+ public byte [] getInstanceId ()
+ {
+ if(addr == null || ! (addr instanceof IpAddress))
+ return null;
+ return ((IpAddress)addr).getAdditionalData();
+ }
+
@Override
public int hashCode()
{
if (hash_code == -1)
{
- hash_code = (addr != null ? addr.hashCode() : 0) + (int) id;
+ hash_code = (addr != null ? addr.hashCode() : 0) + (int) id
+ + Arrays.hashCode(getInstanceId());
}
return hash_code;
}
@@ -107,7 +138,7 @@
GlobalTransaction otherGtx = (GlobalTransaction) other;
boolean aeq = (addr == null) ? (otherGtx.addr == null) :
addr.equals(otherGtx.addr);
- return aeq && (id == otherGtx.id);
+ return aeq && (id == otherGtx.id) && Arrays.equals(getInstanceId(),
otherGtx.getInstanceId());
}
@Override
Show replies by date