use of com.arjuna.ats.arjuna.recovery.RecoveryModule in project narayana by jbosstm.
the class TestCommitMarkableResource method doTest.
private void doTest(DataSource dataSource) throws Exception {
// Test code
Utils.createTables(dataSource.getConnection());
javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
tm.begin();
Connection localJDBCConnection = dataSource.getConnection();
localJDBCConnection.setAutoCommit(false);
XAResource nonXAResource = new JDBCConnectableResource(localJDBCConnection);
tm.getTransaction().enlistResource(nonXAResource);
tm.getTransaction().enlistResource(new DummyXAResource());
localJDBCConnection.createStatement().execute("INSERT INTO foo (bar) VALUES (1)");
tm.commit();
// This is test code, it allows us to verify that the correct XID was
// removed
Xid committed = ((JDBCConnectableResource) nonXAResource).getStartedXid();
assertNotNull(committed);
// We can't just instantiate one as we need to be using the same one as
// the transaction
// manager would have used to mark the transaction for GC
CommitMarkableResourceRecordRecoveryModule recoveryModule = null;
Vector recoveryModules = manager.getModules();
if (recoveryModules != null) {
Enumeration modules = recoveryModules.elements();
while (modules.hasMoreElements()) {
RecoveryModule m = (RecoveryModule) modules.nextElement();
if (m instanceof CommitMarkableResourceRecordRecoveryModule) {
recoveryModule = (CommitMarkableResourceRecordRecoveryModule) m;
}
}
}
recoveryModule = new CommitMarkableResourceRecordRecoveryModule();
// The recovery module has to perform lookups
new InitialContext().rebind("commitmarkableresource", dataSource);
// Run the first pass it will load the committed Xids into memory
recoveryModule.periodicWorkFirstPass();
assertTrue(recoveryModule.wasCommitted("commitmarkableresource", committed));
recoveryModule.periodicWorkSecondPass();
// Make sure that the resource was GC'd by the CRRRM
assertFalse(recoveryModule.wasCommitted("commitmarkableresource", committed));
}
use of com.arjuna.ats.arjuna.recovery.RecoveryModule in project narayana by jbosstm.
the class TestCommitMarkableResourceFailActivate method testFailAfterPrepare.
@Test
@BMScript("commitMarkableResourceFailAfterCommit")
public void testFailAfterPrepare() throws Exception {
final DataSource dataSource = new JdbcDataSource();
((JdbcDataSource) dataSource).setURL("jdbc:h2:mem:JBTMDB;MVCC=TRUE;DB_CLOSE_DELAY=-1");
// Test code
Utils.createTables(dataSource.getConnection());
// We can't just instantiate one as we need to be using the
// same one as
// the transaction
// manager would have used to mark the transaction for GC
CommitMarkableResourceRecordRecoveryModule recoveryModule = null;
Vector recoveryModules = manager.getModules();
if (recoveryModules != null) {
Enumeration modules = recoveryModules.elements();
while (modules.hasMoreElements()) {
RecoveryModule m = (RecoveryModule) modules.nextElement();
if (m instanceof CommitMarkableResourceRecordRecoveryModule) {
recoveryModule = (CommitMarkableResourceRecordRecoveryModule) m;
} else if (m instanceof XARecoveryModule) {
XARecoveryModule xarm = (XARecoveryModule) m;
xarm.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {
public boolean initialise(String p) throws Exception {
return true;
}
public XAResource[] getXAResources() throws Exception {
return new XAResource[] { xaResource };
}
});
}
}
}
// final Object o = new Object();
// synchronized (o) {
Thread foo = new Thread(new Runnable() {
public void run() {
try {
javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
tm.begin();
Connection localJDBCConnection = dataSource.getConnection();
localJDBCConnection.setAutoCommit(false);
nonXAResource = new JDBCConnectableResource(localJDBCConnection);
tm.getTransaction().enlistResource(nonXAResource);
xaResource = new SimpleXAResource();
tm.getTransaction().enlistResource(xaResource);
localJDBCConnection.createStatement().execute("INSERT INTO foo (bar) VALUES (1)");
tm.commit();
} catch (ExecuteException t) {
} catch (Exception t) {
t.printStackTrace();
failed = true;
} catch (Error t) {
}
}
});
foo.start();
foo.join();
assertFalse(failed);
// Now we need to correctly complete the transaction
manager.scan();
assertFalse(xaResource.wasCommitted());
assertFalse(xaResource.wasRolledback());
// This is test code, it allows us to verify that the
// correct XID was
// removed
Xid committed = ((JDBCConnectableResource) nonXAResource).getStartedXid();
assertNotNull(committed);
// The recovery module has to perform lookups
new InitialContext().rebind("commitmarkableresource", dataSource);
// Run the first pass it will load the committed Xids into so we can
// indepently verify that the item was committed
recoveryModule.periodicWorkFirstPass();
recoveryModule.periodicWorkSecondPass();
assertTrue(recoveryModule.wasCommitted("commitmarkableresource", committed));
// Run the scan to clear the content
manager.scan();
assertTrue(xaResource.wasCommitted());
assertFalse(xaResource.wasRolledback());
// Make sure that the resource was GC'd by the CRRRM
assertTrue(recoveryModule.wasCommitted("commitmarkableresource", committed));
recoveryModule.periodicWorkFirstPass();
assertTrue(recoveryModule.wasCommitted("commitmarkableresource", committed));
// This is the pass that will delete it
recoveryModule.periodicWorkSecondPass();
assertFalse(recoveryModule.wasCommitted("commitmarkableresource", committed));
}
use of com.arjuna.ats.arjuna.recovery.RecoveryModule in project narayana by jbosstm.
the class TestCommitMarkableResourceFailAfterCommitOrphan method test.
@Test
@BMScript("commitMarkableResourceFailAfterCommit")
public void test() throws Exception {
final JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setURL("jdbc:h2:mem:JBTMDB;MVCC=TRUE;DB_CLOSE_DELAY=-1");
// Test code
Utils.createTables(dataSource.getConnection());
// We can't just instantiate one as we need to be using the
// same one as
// the transaction
// manager would have used to mark the transaction for GC
CommitMarkableResourceRecordRecoveryModule commitMarkableResourceRecoveryModule = null;
Vector recoveryModules = manager.getModules();
if (recoveryModules != null) {
Enumeration modules = recoveryModules.elements();
while (modules.hasMoreElements()) {
RecoveryModule m = (RecoveryModule) modules.nextElement();
if (m instanceof CommitMarkableResourceRecordRecoveryModule) {
commitMarkableResourceRecoveryModule = (CommitMarkableResourceRecordRecoveryModule) m;
} else if (m instanceof XARecoveryModule) {
xarm = (XARecoveryModule) m;
xarm.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {
public boolean initialise(String p) throws Exception {
return true;
}
public XAResource[] getXAResources() throws Exception {
return new XAResource[] { xaResource };
}
});
}
}
}
// final Object o = new Object();
// synchronized (o) {
Thread preCrash = new Thread(new Runnable() {
public void run() {
try (Connection localJDBCConnection = dataSource.getConnection()) {
javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
tm.begin();
localJDBCConnection.setAutoCommit(false);
cmrResource = new JDBCConnectableResource(localJDBCConnection);
tm.getTransaction().enlistResource(cmrResource);
tm.getTransaction().enlistResource(xaResource);
localJDBCConnection.createStatement().execute("INSERT INTO foo (bar) VALUES (1)");
tm.commit();
} catch (ExecuteException t) {
} catch (Exception e) {
e.printStackTrace();
failed = true;
} catch (Error e) {
}
}
});
preCrash.start();
preCrash.join();
assertFalse(failed);
assertFalse(wasCommitted);
assertFalse(wasRolledback);
// The recovery module has to perform lookups
new InitialContext().rebind("commitmarkableresource", dataSource);
// Testing that we can find a AAC but that it won't be ignored in orphan detection so need CMRRM run
commitMarkableResourceRecoveryModule.periodicWorkFirstPass();
XAResourceOrphanFilter.Vote vote = new JTATransactionLogXAResourceOrphanFilter().checkXid(preparedXid);
assertTrue(vote == XAResourceOrphanFilter.Vote.LEAVE_ALONE);
manager.scan();
manager.scan();
assertTrue(wasCommitted);
}
use of com.arjuna.ats.arjuna.recovery.RecoveryModule in project narayana by jbosstm.
the class XATerminatorUnitTest method testRecovery.
@Test
public void testRecovery() throws Exception {
Implementations.initialise();
XATerminatorImple xa = new XATerminatorImple();
Xid[] recover = xa.recover(XAResource.TMSTARTRSCAN);
int initialLength = recover == null ? 0 : recover.length;
xa.recover(XAResource.TMENDRSCAN);
XidImple xid = new XidImple(new Uid());
TransactionImporter imp = SubordinationManager.getTransactionImporter();
SubordinateTransaction importTransaction = imp.importTransaction(xid);
importTransaction.enlistResource(new XAResource() {
@Override
public void start(Xid xid, int flags) throws XAException {
}
@Override
public void end(Xid xid, int flags) throws XAException {
}
@Override
public int prepare(Xid xid) throws XAException {
return 0;
}
@Override
public void commit(Xid xid, boolean onePhase) throws XAException {
throw new XAException(XAException.XA_RETRY);
}
@Override
public void rollback(Xid xid) throws XAException {
}
@Override
public void forget(Xid xid) throws XAException {
}
@Override
public Xid[] recover(int flag) throws XAException {
return null;
}
@Override
public boolean isSameRM(XAResource xaRes) throws XAException {
return false;
}
@Override
public int getTransactionTimeout() throws XAException {
return 0;
}
@Override
public boolean setTransactionTimeout(int seconds) throws XAException {
return false;
}
});
assertTrue(xa.beforeCompletion(xid));
assertEquals(xa.prepare(xid), XAResource.XA_OK);
try {
xa.commit(xid, false);
fail();
} catch (XAException e) {
assertTrue(e.errorCode == XAException.XAER_RMFAIL);
}
Xid[] recover2 = xa.recover(XAResource.TMSTARTRSCAN);
assertTrue(recover2.length == initialLength + 1);
try {
xa.commit(xid, false);
fail();
} catch (XAException e) {
assertTrue("Wrong errorcode" + e.errorCode, e.errorCode == XAException.XAER_RMFAIL);
}
xa.recover(XAResource.TMENDRSCAN);
// Feed the recovery manager with something it can recover with
RecoveryModule module = new XARecoveryModule() {
@Override
public XAResource getNewXAResource(final XAResourceRecord xaResourceRecord) {
return new XAResource() {
@Override
public void start(Xid xid, int flags) throws XAException {
}
@Override
public void end(Xid xid, int flags) throws XAException {
}
@Override
public int prepare(Xid xid) throws XAException {
return 0;
}
@Override
public void commit(Xid xid, boolean onePhase) throws XAException {
}
@Override
public void rollback(Xid xid) throws XAException {
}
@Override
public void forget(Xid xid) throws XAException {
}
@Override
public Xid[] recover(int flag) throws XAException {
return null;
}
@Override
public boolean isSameRM(XAResource xaRes) throws XAException {
return false;
}
@Override
public int getTransactionTimeout() throws XAException {
return 0;
}
@Override
public boolean setTransactionTimeout(int seconds) throws XAException {
return false;
}
};
}
};
RecoveryManager.manager().addModule(module);
try {
Xid[] recover3 = xa.recover(XAResource.TMSTARTRSCAN);
assertTrue(recover3.length == recover2.length);
xa.commit(xid, false);
xa.recover(XAResource.TMENDRSCAN);
Xid[] recover4 = xa.recover(XAResource.TMSTARTRSCAN);
assertTrue(recover4 == null || recover4.length == initialLength);
xa.recover(XAResource.TMENDRSCAN);
} finally {
RecoveryManager.manager().removeModule(module, false);
}
}
use of com.arjuna.ats.arjuna.recovery.RecoveryModule in project narayana by jbosstm.
the class CrashRecoveryCommitReturnsXA_RETRYHeuristicRollback method testHeuristicRollback.
@Test
public void testHeuristicRollback() throws Exception {
// this test is supposed to leave a record around in the log store
// during a commit long enough
// that the periodic recovery thread runs and detects it. rather than
// rely on delays to make
// this happen (placing us at the mercy of the scheduler) we use a
// byteman script to enforce
// the thread sequence we need
RecoveryEnvironmentBean recoveryEnvironmentBean = BeanPopulator.getDefaultInstance(RecoveryEnvironmentBean.class);
// JBTM-1354 we need to make sure that a full scan has gone off
recoveryEnvironmentBean.setRecoveryBackoffPeriod(1);
recoveryEnvironmentBean.setPeriodicRecoveryPeriod(Integer.MAX_VALUE);
List<String> recoveryModuleClassNames = new ArrayList<String>();
recoveryModuleClassNames.add("com.arjuna.ats.internal.arjuna.recovery.AtomicActionRecoveryModule");
recoveryModuleClassNames.add("com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule");
recoveryEnvironmentBean.setRecoveryModuleClassNames(recoveryModuleClassNames);
List<String> expiryScannerClassNames = new ArrayList<String>();
expiryScannerClassNames.add("com.arjuna.ats.internal.arjuna.recovery.ExpiredTransactionStatusManagerScanner");
recoveryEnvironmentBean.setExpiryScannerClassNames(expiryScannerClassNames);
recoveryEnvironmentBean.setRecoveryActivators(null);
// start the recovery manager
RecoveryManager.manager().initialize();
XARecoveryModule xaRecoveryModule = null;
for (RecoveryModule recoveryModule : ((Vector<RecoveryModule>) RecoveryManager.manager().getModules())) {
if (recoveryModule instanceof XARecoveryModule) {
xaRecoveryModule = (XARecoveryModule) recoveryModule;
break;
}
}
if (xaRecoveryModule == null) {
throw new Exception("No XARM");
}
// JBTM-1354 Run a scan to make sure that the recovery thread has completed a full run before starting the test
// The important thing is that replayCompletion is allowed to do a scan of the transactions
RecoveryManager.manager().scan();
XAResource firstResource = new SimpleResource();
Object toWakeUp = new Object();
final SimpleResourceXA_RETRYHeuristicRollback secondResource = new SimpleResourceXA_RETRYHeuristicRollback();
xaRecoveryModule.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {
@Override
public boolean initialise(String p) throws Exception {
// TODO Auto-generated method stub
return true;
}
@Override
public XAResource[] getXAResources() throws Exception {
// TODO Auto-generated method stub
return new XAResource[] { secondResource };
}
});
// ok, now drive a TX to completion. the script should ensure that the
// recovery
javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
tm.begin();
javax.transaction.Transaction theTransaction = tm.getTransaction();
Uid txUid = ((TransactionImple) theTransaction).get_uid();
theTransaction.enlistResource(firstResource);
theTransaction.enlistResource(secondResource);
assertFalse(secondResource.wasCommitted());
tm.commit();
InputObjectState uids = new InputObjectState();
String type = new AtomicAction().type();
StoreManager.getRecoveryStore().allObjUids(type, uids);
boolean moreUids = true;
boolean found = false;
while (moreUids) {
Uid theUid = UidHelper.unpackFrom(uids);
if (theUid.equals(txUid)) {
found = true;
Field heuristicListField = BasicAction.class.getDeclaredField("heuristicList");
heuristicListField.setAccessible(true);
ActionStatusService ass = new ActionStatusService();
{
int theStatus = ass.getTransactionStatus(type, theUid.stringForm());
assertTrue(theStatus == ActionStatus.COMMITTED);
RecoverAtomicAction rcvAtomicAction = new RecoverAtomicAction(theUid, theStatus);
theStatus = rcvAtomicAction.status();
rcvAtomicAction.replayPhase2();
assertTrue(theStatus == ActionStatus.COMMITTED);
assertTrue(secondResource.wasCommitted());
RecordList heuristicList = (RecordList) heuristicListField.get(rcvAtomicAction);
assertTrue("Expected 1 heuristics: " + heuristicList.size(), heuristicList.size() == 1);
}
{
int theStatus = ass.getTransactionStatus(type, theUid.stringForm());
assertTrue(theStatus == ActionStatus.COMMITTED);
RecoverAtomicAction rcvAtomicAction = new RecoverAtomicAction(theUid, theStatus);
theStatus = rcvAtomicAction.status();
assertTrue(theStatus == ActionStatus.COMMITTED);
RecordList heuristicList = (RecordList) heuristicListField.get(rcvAtomicAction);
assertTrue("Expected 1 heuristics: " + heuristicList.size(), heuristicList.size() == 1);
assertTrue(secondResource.wasCommitted());
}
} else if (theUid.equals(Uid.nullUid())) {
moreUids = false;
}
}
if (!found) {
throw new Exception("Could not locate the Uid");
}
}
Aggregations