Search in sources :

Example 1 with XAResourceRecoveryHelper

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

the class XARecoveryModuleUnitTest method testCanRepeatFirstPass.

@Test
public void testCanRepeatFirstPass() throws Exception {
    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[] { new XAResource() {

                @Override
                public void commit(Xid xid, boolean b) throws XAException {
                }

                @Override
                public void end(Xid xid, int i) throws XAException {
                }

                @Override
                public void forget(Xid xid) throws XAException {
                }

                @Override
                public int getTransactionTimeout() throws XAException {
                    return 0;
                }

                @Override
                public boolean isSameRM(XAResource xaResource) throws XAException {
                    return false;
                }

                @Override
                public int prepare(Xid xid) throws XAException {
                    return 0;
                }

                @Override
                public Xid[] recover(int i) throws XAException {
                    recoverCalled++;
                    return new Xid[0];
                }

                @Override
                public void rollback(Xid xid) throws XAException {
                }

                @Override
                public boolean setTransactionTimeout(int i) throws XAException {
                    return false;
                }

                @Override
                public void start(Xid xid, int i) throws XAException {
                }
            } };
        }
    });
    xarm.periodicWorkFirstPass();
    assertEquals(recoverCalled, 1);
    xarm.periodicWorkSecondPass();
    assertEquals(recoverCalled, 2);
    xarm.periodicWorkFirstPass();
    assertEquals(recoverCalled, 3);
    xarm.periodicWorkFirstPass();
    assertEquals(recoverCalled, 5);
    xarm.periodicWorkSecondPass();
    assertEquals(recoverCalled, 6);
}
Also used : XAResource(javax.transaction.xa.XAResource) RecoveryXAResource(com.hp.mwtests.ts.jta.common.RecoveryXAResource) Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) XAException(javax.transaction.xa.XAException) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) Test(org.junit.Test)

Example 2 with XAResourceRecoveryHelper

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

the class XARecoveryModuleUnitTest method testRecoverPassFailure.

@Test
public void testRecoverPassFailure() throws Exception {
    int orphanSafetyInterval = jtaPropertyManager.getJTAEnvironmentBean().getOrphanSafetyInterval();
    List<String> xaRecoveryNodes = jtaPropertyManager.getJTAEnvironmentBean().getXaRecoveryNodes();
    jtaPropertyManager.getJTAEnvironmentBean().setOrphanSafetyInterval(0);
    jtaPropertyManager.getJTAEnvironmentBean().setXaRecoveryNodes(Arrays.asList(new String[] { NodeNameXAResourceOrphanFilter.RECOVER_ALL_NODES }));
    XARecoveryModule xarm = new XARecoveryModule();
    xarm.addXAResourceOrphanFilter(new JTANodeNameXAResourceOrphanFilter());
    xarm.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {

        XAResource[] xares = new XAResource[] { new XAResourceWrapper() {

            @Override
            public XAResource getResource() {
                return null;
            }

            @Override
            public String getProductName() {
                return null;
            }

            @Override
            public String getProductVersion() {
                return null;
            }

            @Override
            public String getJndiName() {
                return "test";
            }

            int count = 0;

            Xid xid = new XidImple(new Uid());

            @Override
            public void commit(Xid xid, boolean b) throws XAException {
            }

            @Override
            public void end(Xid xid, int i) throws XAException {
            }

            @Override
            public void forget(Xid xid) throws XAException {
            }

            @Override
            public int getTransactionTimeout() throws XAException {
                return 0;
            }

            @Override
            public boolean isSameRM(XAResource xaResource) throws XAException {
                return false;
            }

            @Override
            public int prepare(Xid xid) throws XAException {
                return 0;
            }

            @Override
            public Xid[] recover(int i) throws XAException {
                count++;
                if (count == 1 || count == 5) {
                    return new Xid[] { xid };
                } else if (count > 5) {
                    return new Xid[0];
                } else {
                    throw new XAException();
                }
            }

            @Override
            public void rollback(Xid xid) throws XAException {
                if (count == 1) {
                    // This comes from the first end scan
                    throw new XAException(XAException.XA_RETRY);
                }
                rolledback = true;
            }

            @Override
            public boolean setTransactionTimeout(int i) throws XAException {
                return false;
            }

            @Override
            public void start(Xid xid, int i) throws XAException {
            }
        } };

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

        @Override
        public XAResource[] getXAResources() throws Exception {
            return xares;
        }
    });
    // The first two recovery cycles do nothing with the resource (because phase two is getting the exception)
    // When count reaches 6 it sees that the xid has gone and presumes abort so calls rollback and hence assertTrue(rolledback) passes
    // 1st pass: returns one xid (count is 1)
    xarm.periodicWorkFirstPass();
    // 2nd pass: throws an exception (count is 2)
    xarm.periodicWorkSecondPass();
    assertTrue(xarm.getContactedJndiNames().contains("test"));
    assertFalse(rolledback);
    // 1st pass: throws an exception (count is 3)
    xarm.periodicWorkFirstPass();
    // 2nd pass: throws an exception (count is 4)
    xarm.periodicWorkSecondPass();
    assertFalse(xarm.getContactedJndiNames().contains("test"));
    assertFalse(rolledback);
    // 1st pass: returns an empty list of xids (count is 5)
    xarm.periodicWorkFirstPass();
    // 2nd pass: returns an empty list of xids (count is 6)
    xarm.periodicWorkSecondPass();
    assertTrue(xarm.getContactedJndiNames().contains("test"));
    assertTrue(rolledback);
    jtaPropertyManager.getJTAEnvironmentBean().setOrphanSafetyInterval(orphanSafetyInterval);
    jtaPropertyManager.getJTAEnvironmentBean().setXaRecoveryNodes(xaRecoveryNodes);
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) JTANodeNameXAResourceOrphanFilter(com.arjuna.ats.internal.jta.recovery.arjunacore.JTANodeNameXAResourceOrphanFilter) XAException(javax.transaction.xa.XAException) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) XAResourceWrapper(org.jboss.tm.XAResourceWrapper) XAException(javax.transaction.xa.XAException) Uid(com.arjuna.ats.arjuna.common.Uid) XAResource(javax.transaction.xa.XAResource) RecoveryXAResource(com.hp.mwtests.ts.jta.common.RecoveryXAResource) Xid(javax.transaction.xa.Xid) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) Test(org.junit.Test)

Example 3 with XAResourceRecoveryHelper

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

the class XARecoveryModuleUnitTest method testXAResourceRecoveryHelperRegistration.

@Test
public void testXAResourceRecoveryHelperRegistration() {
    XARecoveryModule xaRecoveryModule = new XARecoveryModule();
    XAResourceRecoveryHelper xaResourceRecoveryHelper = new DummyXAResourceRecoveryHelper();
    xaRecoveryModule.addXAResourceRecoveryHelper(xaResourceRecoveryHelper);
    xaRecoveryModule.removeXAResourceRecoveryHelper(xaResourceRecoveryHelper);
}
Also used : XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) Test(org.junit.Test)

Example 4 with XAResourceRecoveryHelper

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

the class CrashRecoveryCommitReturnsXA_RETRY method test.

@Test
public void test() 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);
    recoveryEnvironmentBean.setRecoveryBackoffPeriod(1);
    recoveryEnvironmentBean.setPeriodicRecoveryPeriod(1);
    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");
    }
    XAResource firstResource = new SimpleResource();
    final SimpleResourceXA_RETRY secondResource = new SimpleResourceXA_RETRY(this);
    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();
    theTransaction.enlistResource(firstResource);
    theTransaction.enlistResource(secondResource);
    assertFalse(secondResource.wasCommitted());
    tm.commit();
    synchronized (this) {
        while (!committed) {
            wait();
        }
    }
    assertTrue(secondResource.wasCommitted());
}
Also used : ArrayList(java.util.ArrayList) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) RecoveryEnvironmentBean(com.arjuna.ats.arjuna.common.RecoveryEnvironmentBean) XAResource(javax.transaction.xa.XAResource) RecoveryModule(com.arjuna.ats.arjuna.recovery.RecoveryModule) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) Test(org.junit.Test)

Example 5 with XAResourceRecoveryHelper

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

the class TestCommitMarkableResourceReturnUnknownOutcomeFrom1PCCommit method testRMFAILAfterCommit.

@Test
public void testRMFAILAfterCommit() throws Exception {
    jtaPropertyManager.getJTAEnvironmentBean().setNotifyCommitMarkableResourceRecoveryModuleOfCompleteBranches(false);
    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) {
                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 };
                    }
                });
            }
        }
    }
    javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
    tm.begin();
    Uid get_uid = ((TransactionImple) tm.getTransaction()).get_uid();
    Connection localJDBCConnection = dataSource.getConnection();
    localJDBCConnection.setAutoCommit(false);
    nonXAResource = new JDBCConnectableResource(localJDBCConnection) {

        @Override
        public void commit(Xid arg0, boolean arg1) throws XAException {
            super.commit(arg0, arg1);
            throw new XAException(XAException.XAER_RMFAIL);
        }
    };
    tm.getTransaction().enlistResource(nonXAResource);
    xaResource = new SimpleXAResource();
    tm.getTransaction().enlistResource(xaResource);
    localJDBCConnection.createStatement().execute("INSERT INTO foo (bar) VALUES (1)");
    tm.commit();
    assertTrue(xaResource.wasCommitted());
    Xid committed = ((JDBCConnectableResource) nonXAResource).getStartedXid();
    assertNotNull(committed);
    InputObjectState uids = new InputObjectState();
    StoreManager.getRecoveryStore().allObjUids(new AtomicAction().type(), uids);
    Uid uid = UidHelper.unpackFrom(uids);
    assertTrue(uid.equals(get_uid));
    // Belt and braces but we don't expect the CMR to be removed anyway as
    // the RM is "offline"
    manager.scan();
    manager.scan();
    // The recovery module has to perform lookups
    new InitialContext().rebind("commitmarkableresource", dataSource);
    assertTrue(commitMarkableResourceRecoveryModule.wasCommitted("commitmarkableresource", committed));
    // This will complete the atomicaction
    manager.scan();
    StoreManager.getRecoveryStore().allObjUids(new AtomicAction().type(), uids);
    uid = UidHelper.unpackFrom(uids);
    assertTrue(uid.equals(Uid.nullUid()));
    // This is when the CMR deletes are done due to ordering
    manager.scan();
    // of the recovery modules
    assertFalse(commitMarkableResourceRecoveryModule.wasCommitted("commitmarkableresource", committed));
}
Also used : InputObjectState(com.arjuna.ats.arjuna.state.InputObjectState) Enumeration(java.util.Enumeration) XAException(javax.transaction.xa.XAException) TransactionImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) InitialContext(javax.naming.InitialContext) Uid(com.arjuna.ats.arjuna.common.Uid) AtomicAction(com.arjuna.ats.arjuna.AtomicAction) EditableAtomicAction(com.arjuna.ats.internal.arjuna.tools.log.EditableAtomicAction) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) CommitMarkableResourceRecordRecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) RecoveryModule(com.arjuna.ats.arjuna.recovery.RecoveryModule) CommitMarkableResourceRecordRecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule) Vector(java.util.Vector) 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