/** * Generate a UID, globally unique. This method relies on the configured serverId for network uniqueness. * @return the generated UID. */ public static Uid generateUid() { byte[] timestamp = Encoder.longToBytes(MonotonicClock.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(sequenceGenerator.incrementAndGet()); byte[] serverId = TransactionManagerServices.getConfiguration().buildServerIdArray(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); return new Uid(uidArray); }
/** * Generate a UID, globally unique. This method relies on the configured serverId for network uniqueness. * @return the generated UID. */ public static Uid generateUid() { byte[] timestamp = Encoder.longToBytes(MonotonicClock.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(sequenceGenerator.incrementAndGet()); byte[] serverId = TransactionManagerServices.getConfiguration().buildServerIdArray(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); return new Uid(uidArray); }
/** * Generate a UID, globally unique. This method relies on the configured serverId for network uniqueness. * @return the generated UID. */ public static Uid generateUid() { byte[] timestamp = Encoder.longToBytes(MonotonicClock.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(sequenceGenerator.incrementAndGet()); byte[] serverId = TransactionManagerServices.getConfiguration().buildServerIdArray(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); return new Uid(uidArray); }
/** * Generate a UID, globally unique. This method relies on the configured serverId for network uniqueness. * @return the generated UID. */ public static Uid generateUid() { byte[] timestamp = Encoder.longToBytes(MonotonicClock.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(sequenceGenerator.incrementAndGet()); byte[] serverId = TransactionManagerServices.getConfiguration().buildServerIdArray(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); return new Uid(uidArray); }
public void testCrc32Value() throws Exception { Set<String> names = new HashSet<String>(); names.add("ActiveMQ"); names.add("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"); String uidString = "626974726F6E697853657276657249440000011C31FD45510000955B"; byte[] uidArray = new byte[uidString.length()/2]; for (int i=0; i<uidString.length()/2 ;i++) { String substr = uidString.substring(i*2, i*2+2); byte b = (byte)Integer.parseInt(substr, 16); uidArray[i] = b; } Uid uid = new Uid(uidArray); TransactionLogRecord tlr = new TransactionLogRecord(Status.STATUS_COMMITTED, 116, 28, 1220609394845L, 38266, -1380478121, uid, names, TransactionLogAppender.END_RECORD); boolean correct = tlr.isCrc32Correct(); assertTrue("CRC32 values did not match", correct); names = new TreeSet<String>(); names.add("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"); names.add("ActiveMQ"); tlr = new TransactionLogRecord(Status.STATUS_COMMITTED, 116, 28, 1220609394845L, 38266, -1380478121, uid, names, TransactionLogAppender.END_RECORD); assertTrue(tlr.isCrc32Correct()); }
public void testCrc32Value() throws Exception { Set<String> names = new HashSet<String>(); names.add("ActiveMQ"); names.add("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"); String uidString = "626974726F6E697853657276657249440000011C31FD45510000955B"; byte[] uidArray = new byte[uidString.length()/2]; for (int i=0; i<uidString.length()/2 ;i++) { String substr = uidString.substring(i*2, i*2+2); byte b = (byte)Integer.parseInt(substr, 16); uidArray[i] = b; } Uid uid = new Uid(uidArray); TransactionLogRecord tlr = new TransactionLogRecord(Status.STATUS_COMMITTED, 116, 28, 1220609394845L, 38266, -1380478121, uid, names, TransactionLogAppender.END_RECORD); boolean correct = tlr.isCrc32Correct(); assertTrue("CRC32 values did not match", correct); names = new TreeSet<String>(); names.add("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"); names.add("ActiveMQ"); tlr = new TransactionLogRecord(Status.STATUS_COMMITTED, 116, 28, 1220609394845L, 38266, -1380478121, uid, names, TransactionLogAppender.END_RECORD); assertTrue(tlr.isCrc32Correct()); }
public void testHexaStringEncoder() throws Exception { byte[] result = Encoder.intToBytes(0x80); String hexString = new Uid(result).toString(); assertEquals("00000080", hexString); result = Encoder.longToBytes(0x81); hexString = new Uid(result).toString(); assertEquals("0000000000000081", hexString); result = Encoder.shortToBytes((short) 0xff); hexString = new Uid(result).toString(); assertEquals("00FF", hexString); }
public void testHexaStringEncoder() throws Exception { byte[] result = Encoder.intToBytes(0x80); String hexString = new Uid(result).toString(); assertEquals("00000080", hexString); result = Encoder.longToBytes(0x81); hexString = new Uid(result).toString(); assertEquals("0000000000000081", hexString); result = Encoder.shortToBytes((short) 0xff); hexString = new Uid(result).toString(); assertEquals("00FF", hexString); }
public void testExtracts() throws Exception { byte[] timestamp = Encoder.longToBytes(System.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(1); byte[] serverId = "my-server-id".getBytes(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); Uid uid = new Uid(uidArray); assertTrue(Arrays.equals(serverId, uid.extractServerId())); assertEquals(Encoder.bytesToLong(timestamp, 0), uid.extractTimestamp()); assertEquals(Encoder.bytesToInt(sequence, 0), uid.extractSequence()); }
public void testExtracts() throws Exception { byte[] timestamp = Encoder.longToBytes(System.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(1); byte[] serverId = "my-server-id".getBytes(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); Uid uid = new Uid(uidArray); assertTrue(Arrays.equals(serverId, uid.extractServerId())); assertEquals(Encoder.bytesToLong(timestamp, 0), uid.extractTimestamp()); assertEquals(Encoder.bytesToInt(sequence, 0), uid.extractSequence()); }
@Override public JdbcPooledConnection createPooledConnection(Object xaFactory, ResourceBean bean) throws Exception { JdbcPooledConnection pc = super.createPooledConnection(xaFactory, bean); MockXAResource xaResource = (MockXAResource) pc.getXAResource(); xaResource.addInDoubtXid(UidGenerator.generateXid(new Uid(xid0.getGlobalTransactionId()))); return pc; } };
@Override public JdbcPooledConnection createPooledConnection(Object xaFactory, ResourceBean bean) throws Exception { JdbcPooledConnection pc = super.createPooledConnection(xaFactory, bean); MockXAResource xaResource = (MockXAResource) pc.getXAResource(); xaResource.addInDoubtXid(UidGenerator.generateXid(new Uid(xid0.getGlobalTransactionId()))); return pc; } };
/** * Create 3 XIDs on the resource that are in the journal -> recoverer commits them. * @throws Exception */ public void testIncrementalRecoverCommitting() throws Exception { Xid xid0 = new MockXid(0, UidGenerator.generateUid().getArray(), BitronixXid.FORMAT_ID); xaResource.addInDoubtXid(xid0); Xid xid1 = new MockXid(1, UidGenerator.generateUid().getArray(), BitronixXid.FORMAT_ID); xaResource.addInDoubtXid(xid1); Xid xid2 = new MockXid(2, UidGenerator.generateUid().getArray(), BitronixXid.FORMAT_ID); xaResource.addInDoubtXid(xid2); Set<String> names = new HashSet<String>(); names.add(pds.getUniqueName()); journal.log(Status.STATUS_COMMITTING, new Uid(xid0.getGlobalTransactionId()), names); journal.log(Status.STATUS_COMMITTING, new Uid(xid1.getGlobalTransactionId()), names); journal.log(Status.STATUS_COMMITTING, new Uid(xid2.getGlobalTransactionId()), names); IncrementalRecoverer.recover(pds); assertEquals(0, xaResource.recover(XAResource.TMSTARTRSCAN | XAResource.TMENDRSCAN).length); }
/** * Create 3 XIDs on the resource that are in the journal -> recoverer commits them. * @throws Exception */ public void testIncrementalRecoverCommitting() throws Exception { Xid xid0 = new MockXid(0, UidGenerator.generateUid().getArray(), BitronixXid.FORMAT_ID); xaResource.addInDoubtXid(xid0); Xid xid1 = new MockXid(1, UidGenerator.generateUid().getArray(), BitronixXid.FORMAT_ID); xaResource.addInDoubtXid(xid1); Xid xid2 = new MockXid(2, UidGenerator.generateUid().getArray(), BitronixXid.FORMAT_ID); xaResource.addInDoubtXid(xid2); Set<String> names = new HashSet<String>(); names.add(pds.getUniqueName()); journal.log(Status.STATUS_COMMITTING, new Uid(xid0.getGlobalTransactionId()), names); journal.log(Status.STATUS_COMMITTING, new Uid(xid1.getGlobalTransactionId()), names); journal.log(Status.STATUS_COMMITTING, new Uid(xid2.getGlobalTransactionId()), names); IncrementalRecoverer.recover(pds); assertEquals(0, xaResource.recover(XAResource.TMSTARTRSCAN | XAResource.TMENDRSCAN).length); }
/** * Create 3 XIDs on the resource that are in the journal -> recoverer commits them. * @throws Exception */ public void testRecoverCommitting() throws Exception { Xid xid0 = new MockXid(0, UidGenerator.generateUid().getArray(), BitronixXid.FORMAT_ID); xaResource.addInDoubtXid(xid0); Xid xid1 = new MockXid(1, UidGenerator.generateUid().getArray(), BitronixXid.FORMAT_ID); xaResource.addInDoubtXid(xid1); Xid xid2 = new MockXid(2, UidGenerator.generateUid().getArray(), BitronixXid.FORMAT_ID); xaResource.addInDoubtXid(xid2); Set<String> names = new HashSet<String>(); names.add(pds.getUniqueName()); journal.log(Status.STATUS_COMMITTING, new Uid(xid0.getGlobalTransactionId()), names); journal.log(Status.STATUS_COMMITTING, new Uid(xid1.getGlobalTransactionId()), names); journal.log(Status.STATUS_COMMITTING, new Uid(xid2.getGlobalTransactionId()), names); TransactionManagerServices.getRecoverer().run(); assertEquals(3, TransactionManagerServices.getRecoverer().getCommittedCount()); assertEquals(0, TransactionManagerServices.getRecoverer().getRolledbackCount()); assertEquals(0, xaResource.recover(XAResource.TMSTARTRSCAN | XAResource.TMENDRSCAN).length); }
/** * Create 3 XIDs on the resource that are in the journal -> recoverer commits them. * @throws Exception */ public void testRecoverCommitting() throws Exception { Xid xid0 = new MockXid(0, UidGenerator.generateUid().getArray(), BitronixXid.FORMAT_ID); xaResource.addInDoubtXid(xid0); Xid xid1 = new MockXid(1, UidGenerator.generateUid().getArray(), BitronixXid.FORMAT_ID); xaResource.addInDoubtXid(xid1); Xid xid2 = new MockXid(2, UidGenerator.generateUid().getArray(), BitronixXid.FORMAT_ID); xaResource.addInDoubtXid(xid2); Set<String> names = new HashSet<String>(); names.add(pds.getUniqueName()); journal.log(Status.STATUS_COMMITTING, new Uid(xid0.getGlobalTransactionId()), names); journal.log(Status.STATUS_COMMITTING, new Uid(xid1.getGlobalTransactionId()), names); journal.log(Status.STATUS_COMMITTING, new Uid(xid2.getGlobalTransactionId()), names); TransactionManagerServices.getRecoverer().run(); assertEquals(3, TransactionManagerServices.getRecoverer().getCommittedCount()); assertEquals(0, TransactionManagerServices.getRecoverer().getRolledbackCount()); assertEquals(0, xaResource.recover(XAResource.TMSTARTRSCAN | XAResource.TMENDRSCAN).length); }