Search in sources :

Example 21 with XAResourceRecoveryHelper

use of com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper in project narayana by jbosstm.

the class XATerminatorUnitTest method testImportMultipleTx.

@Test
public void testImportMultipleTx() throws XAException, RollbackException, SystemException {
    Implementations.initialise();
    XidImple xid = new XidImple(new Uid());
    TransactionImporter imp = SubordinationManager.getTransactionImporter();
    SubordinateTransaction subordinateTransaction = imp.importTransaction(xid);
    XATerminatorImple xa = new XATerminatorImple();
    XAResourceImple xar1 = new XAResourceImple(XAResource.XA_OK, XAResource.XA_OK);
    XAResourceImple xar2 = new XAResourceImple(XAResource.XA_OK, XAException.XAER_RMFAIL);
    subordinateTransaction.enlistResource(xar1);
    subordinateTransaction.enlistResource(xar2);
    xa.prepare(xid);
    try {
        xa.commit(xid, false);
        fail("Did not expect to pass");
    } catch (XAException xae) {
        assertTrue(xae.errorCode == XAException.XAER_RMFAIL);
    }
    XARecoveryModule xarm = new XARecoveryModule();
    xarm.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {

        @Override
        public boolean initialise(String p) throws Exception {
            return false;
        }

        @Override
        public XAResource[] getXAResources() throws Exception {
            return new XAResource[] { xar2 };
        }
    });
    RecoveryManager.manager().addModule(xarm);
    Xid[] xids = xa.recover(XAResource.TMSTARTRSCAN);
    assertTrue(Arrays.binarySearch(xids, xid, new Comparator<Xid>() {

        @Override
        public int compare(Xid o1, Xid o2) {
            if (((XidImple) o1).equals(o2)) {
                return 0;
            } else {
                return -1;
            }
        }
    }) != -1);
    xa.rollback(xid);
    assertTrue(xar2.rollbackCalled());
    xa.recover(XAResource.TMENDRSCAN);
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) XAException(javax.transaction.xa.XAException) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) UnexpectedConditionException(com.arjuna.ats.jta.exceptions.UnexpectedConditionException) RollbackException(javax.transaction.RollbackException) SystemException(javax.transaction.SystemException) XAException(javax.transaction.xa.XAException) Uid(com.arjuna.ats.arjuna.common.Uid) Xid(javax.transaction.xa.Xid) TransactionImporter(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TransactionImporter) XATerminatorImple(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.XATerminatorImple) SubordinateTransaction(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) Test(org.junit.Test)

Example 22 with XAResourceRecoveryHelper

use of com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper in project narayana by jbosstm.

the class XATerminatorUnitTest method testCommitMid.

@Test
public void testCommitMid() throws Exception {
    TransactionManagerImple tm = new TransactionManagerImple();
    RecordTypeManager.manager().add(new RecordTypeMap() {

        @SuppressWarnings("unchecked")
        public Class getRecordClass() {
            return XAResourceRecord.class;
        }

        public int getType() {
            return RecordType.JTA_RECORD;
        }
    });
    XATerminatorImple xaTerminator = new XATerminatorImple();
    XidImple xid = new XidImple(new Uid());
    XAResourceImple toCommit = new XAResourceImple(XAResource.XA_OK, XAResource.XA_OK);
    {
        SubordinateTransaction subordinateTransaction = SubordinationManager.getTransactionImporter().importTransaction(xid);
        tm.resume(subordinateTransaction);
        subordinateTransaction.enlistResource(new XAResourceImple(XAResource.XA_RDONLY, XAResource.XA_OK));
        subordinateTransaction.enlistResource(toCommit);
        Transaction suspend = tm.suspend();
    }
    {
        SubordinateTransaction subordinateTransaction = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
        tm.resume(subordinateTransaction);
        subordinateTransaction.doPrepare();
        Transaction suspend = tm.suspend();
    }
    XARecoveryModule xaRecoveryModule = new XARecoveryModule();
    xaRecoveryModule.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {

        @Override
        public boolean initialise(String p) throws Exception {
            return false;
        }

        @Override
        public XAResource[] getXAResources() throws Exception {
            return new XAResource[] { toCommit };
        }
    });
    RecoveryManager.manager().addModule(xaRecoveryModule);
    xaTerminator.doRecover(null, null);
    {
        SubordinateTransaction subordinateTransaction = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
        tm.resume(subordinateTransaction);
        subordinateTransaction.doCommit();
        tm.suspend();
    }
    RecoveryManager.manager().removeModule(xaRecoveryModule, false);
    assertTrue(toCommit.wasCommitted());
    SubordinationManager.getTransactionImporter().removeImportedTransaction(xid);
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) UnexpectedConditionException(com.arjuna.ats.jta.exceptions.UnexpectedConditionException) RollbackException(javax.transaction.RollbackException) SystemException(javax.transaction.SystemException) XAException(javax.transaction.xa.XAException) Uid(com.arjuna.ats.arjuna.common.Uid) TransactionManagerImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple) RecordTypeMap(com.arjuna.ats.arjuna.coordinator.abstractrecord.RecordTypeMap) SubordinateTransaction(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction) Transaction(javax.transaction.Transaction) XATerminatorImple(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.XATerminatorImple) SubordinateTransaction(com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) Test(org.junit.Test)

Example 23 with XAResourceRecoveryHelper

use of com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper 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");
    }
}
Also used : TransactionImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple) ArrayList(java.util.ArrayList) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) AtomicAction(com.arjuna.ats.arjuna.AtomicAction) RecoverAtomicAction(com.arjuna.ats.arjuna.recovery.RecoverAtomicAction) Field(java.lang.reflect.Field) ActionStatusService(com.arjuna.ats.arjuna.recovery.ActionStatusService) RecoverAtomicAction(com.arjuna.ats.arjuna.recovery.RecoverAtomicAction) InputObjectState(com.arjuna.ats.arjuna.state.InputObjectState) Uid(com.arjuna.ats.arjuna.common.Uid) RecoveryEnvironmentBean(com.arjuna.ats.arjuna.common.RecoveryEnvironmentBean) XAResource(javax.transaction.xa.XAResource) RecordList(com.arjuna.ats.arjuna.coordinator.RecordList) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) RecoveryModule(com.arjuna.ats.arjuna.recovery.RecoveryModule) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) Test(org.junit.Test)

Example 24 with XAResourceRecoveryHelper

use of com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper in project narayana by jbosstm.

the class RecoveryShutdownTest method test.

/**
 * test that the call sequence
 *
 * XARecoveryModule#getNewXAResource()
 * RecoveryManager#terminate();
 * XARecoveryModule#removeXAResourceRecoveryHelper
 *
 * does not hang
 *
 * @throws InterruptedException if the test is interrupted
 */
@Test
public void test() throws InterruptedException {
    recoveryPropertyManager.getRecoveryEnvironmentBean().setRecoveryBackoffPeriod(1);
    RecoveryManager manager = RecoveryManager.manager(RecoveryManager.DIRECT_MANAGEMENT);
    XARecoveryModule xarm = new XARecoveryModule();
    final SimpleResource testXAResource = new SimpleResource() {

        @Override
        public Xid[] recover(int i) throws XAException {
            return new Xid[] { new Xid() {

                @Override
                public int getFormatId() {
                    return 0;
                }

                @Override
                public byte[] getGlobalTransactionId() {
                    return new byte[0];
                }

                @Override
                public byte[] getBranchQualifier() {
                    return new byte[0];
                }
            } };
        }
    };
    final XAResourceRecoveryHelper xaResourceRecoveryHelper = new XAResourceRecoveryHelper() {

        @Override
        public boolean initialise(String p) throws Exception {
            return true;
        }

        @Override
        public XAResource[] getXAResources() throws Exception {
            return new XAResource[] { testXAResource };
        }
    };
    xarm.addXAResourceRecoveryHelper(xaResourceRecoveryHelper);
    manager.removeAllModules(false);
    manager.addModule(xarm);
    manager.scan();
    manager.terminate();
    xarm.getNewXAResource(new XAResourceRecord(null, null, new XidImple(), null));
    final boolean[] removedHelper = { false };
    Runnable r = () -> {
        // the next call will hang unless JBTM-2837 is fixed
        xarm.removeXAResourceRecoveryHelper(xaResourceRecoveryHelper);
        removedHelper[0] = true;
    };
    Thread t = new Thread(r);
    t.start();
    Thread.sleep(100);
    assertTrue("removal of an XAResourceRecoveryHelper hung", removedHelper[0]);
}
Also used : RecoveryManager(com.arjuna.ats.arjuna.recovery.RecoveryManager) XidImple(com.arjuna.ats.jta.xa.XidImple) Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) XAResourceRecord(com.arjuna.ats.internal.jta.resources.arjunacore.XAResourceRecord) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) Test(org.junit.Test)

Aggregations

XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)24 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)19 XAResource (javax.transaction.xa.XAResource)17 Test (org.junit.Test)17 Xid (javax.transaction.xa.Xid)13 RecoveryModule (com.arjuna.ats.arjuna.recovery.RecoveryModule)11 XAException (javax.transaction.xa.XAException)11 Uid (com.arjuna.ats.arjuna.common.Uid)8 CommitMarkableResourceRecordRecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule)8 Connection (java.sql.Connection)8 Enumeration (java.util.Enumeration)8 Vector (java.util.Vector)8 InitialContext (javax.naming.InitialContext)7 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)7 ExecuteException (org.jboss.byteman.rule.exception.ExecuteException)6 XidImple (com.arjuna.ats.jta.xa.XidImple)5 TransactionImple (com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple)4 BMScript (org.jboss.byteman.contrib.bmunit.BMScript)4 AtomicAction (com.arjuna.ats.arjuna.AtomicAction)3 InputObjectState (com.arjuna.ats.arjuna.state.InputObjectState)3