Search in sources :

Example 1 with RecoveryModule

use of com.arjuna.ats.arjuna.recovery.RecoveryModule 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 2 with RecoveryModule

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

the class PeriodicRecovery method doWorkInternal.

/**
 * start performing a scan continuing to completion unless we are terminating
 *
 * <b>Caveats:</b> this must only be called when _currentStatus is SCANNING. on return _currentStatus is always
 * still SCANNING
 */
private void doWorkInternal() {
    if (tsLogger.logger.isDebugEnabled()) {
        tsLogger.logger.debug("Periodic recovery first pass at " + _theTimestamper.format(new Date()));
    }
    // n.b. this works on a copy of the modules list so it is not affected by
    // dynamic updates in the middle of a scan, ensuring first+second pass happen
    // for the same stable set of modules.
    Vector copyOfModules = getModules();
    Enumeration modules = copyOfModules.elements();
    while (modules.hasMoreElements()) {
        RecoveryModule m = (RecoveryModule) modules.nextElement();
        // we need to ensure we use the class loader context of the recovery module while we are executing
        // its methods
        ClassLoader cl = switchClassLoader(m);
        try {
            m.periodicWorkFirstPass();
        } finally {
            restoreClassLoader(cl);
        }
        if (tsLogger.logger.isDebugEnabled()) {
            tsLogger.logger.debug(" ");
        }
    }
    synchronized (_stateLock) {
        // we have to wait for a bit to avoid catching (too many)
        // transactions etc. that are really progressing quite happily
        doBackoffWait();
        if (getMode() == Mode.TERMINATED) {
            if (tsLogger.logger.isDebugEnabled()) {
                tsLogger.logger.debug("PeriodicRecovery: scan TERMINATED at phase 1");
            }
            return;
        }
    }
    if (tsLogger.logger.isDebugEnabled()) {
        tsLogger.logger.debug("Periodic recovery second pass at " + _theTimestamper.format(new Date()));
    }
    modules = copyOfModules.elements();
    while (modules.hasMoreElements()) {
        RecoveryModule m = (RecoveryModule) modules.nextElement();
        ClassLoader cl = switchClassLoader(m);
        try {
            m.periodicWorkSecondPass();
        } finally {
            restoreClassLoader(cl);
        }
        if (tsLogger.logger.isDebugEnabled()) {
            tsLogger.logger.debug(" ");
        }
    }
// n.b. the caller is responsible for clearing the active scan
}
Also used : Enumeration(java.util.Enumeration) RecoveryModule(com.arjuna.ats.arjuna.recovery.RecoveryModule) Vector(java.util.Vector) Date(java.util.Date)

Example 3 with RecoveryModule

use of com.arjuna.ats.arjuna.recovery.RecoveryModule 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)

Example 4 with RecoveryModule

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

the class FailAfterCommitBase method doTest.

protected void doTest(final DataSource dataSource) throws Exception {
    // 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 };
                    }
                });
            }
        }
    }
    // final Object o = new Object();
    // synchronized (o) {
    Thread background = 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 e) {
                e.printStackTrace();
                failed = true;
            } catch (Error e) {
            }
        }
    });
    background.start();
    background.join();
    assertFalse(failed);
    Xid committed = ((JDBCConnectableResource) nonXAResource).getStartedXid();
    assertNotNull(committed);
    // The recovery module has to perform lookups
    new InitialContext().rebind("commitmarkableresource", dataSource);
    // Check if the item is still in the db
    commitMarkableResourceRecoveryModule.periodicWorkFirstPass();
    assertTrue(commitMarkableResourceRecoveryModule.wasCommitted("commitmarkableresource", committed));
    commitMarkableResourceRecoveryModule.periodicWorkSecondPass();
    // Now we need to correctly complete the transaction
    manager.scan();
    // Make sure the item is no longer in the DB, it will need two scans as
    // second phase of the CommitMarkableResourceRecoveryModule will have
    // executed before the AtomicActionRecoveryModule has been able to GC
    // it
    assertTrue(commitMarkableResourceRecoveryModule.wasCommitted("commitmarkableresource", committed));
    manager.scan();
    commitMarkableResourceRecoveryModule.periodicWorkFirstPass();
    commitMarkableResourceRecoveryModule.periodicWorkSecondPass();
    assertTrue(xaResource.wasCommitted());
    assertFalse(xaResource.wasRolledback());
    // Make sure that the resource was GC'd by the CRRRM
    assertFalse(commitMarkableResourceRecoveryModule.wasCommitted("commitmarkableresource", committed));
}
Also used : Enumeration(java.util.Enumeration) Connection(java.sql.Connection) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) ExecuteException(org.jboss.byteman.rule.exception.ExecuteException) InitialContext(javax.naming.InitialContext) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) ExecuteException(org.jboss.byteman.rule.exception.ExecuteException) CommitMarkableResourceRecordRecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule) RecoveryModule(com.arjuna.ats.arjuna.recovery.RecoveryModule) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) CommitMarkableResourceRecordRecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule) Vector(java.util.Vector) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)

Example 5 with RecoveryModule

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

the class RecoveryManagerService method addXAResourceRecovery.

// ////////////////////////////
public void addXAResourceRecovery(XAResourceRecovery xaResourceRecovery) {
    if (_recoveryManager == null) {
        throw new IllegalStateException(jbossatxLogger.i18NLogger.get_jta_RecoveryManagerService_norecoverysystem());
    }
    XARecoveryModule xaRecoveryModule = null;
    for (RecoveryModule recoveryModule : ((Vector<RecoveryModule>) _recoveryManager.getModules())) {
        if (recoveryModule instanceof XARecoveryModule) {
            xaRecoveryModule = (XARecoveryModule) recoveryModule;
            break;
        }
    }
    if (xaRecoveryModule == null) {
        throw new IllegalStateException(jbossatxLogger.i18NLogger.get_jta_RecoveryManagerService_norecoverymodule());
    }
    xaRecoveryModule.addXAResourceRecoveryHelper(new XAResourceRecoveryHelperWrapper(xaResourceRecovery));
}
Also used : XAResourceRecoveryHelperWrapper(com.arjuna.ats.internal.jbossatx.jta.XAResourceRecoveryHelperWrapper) 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)

Aggregations

RecoveryModule (com.arjuna.ats.arjuna.recovery.RecoveryModule)27 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)18 Enumeration (java.util.Enumeration)14 Vector (java.util.Vector)14 XAResource (javax.transaction.xa.XAResource)14 Test (org.junit.Test)12 CommitMarkableResourceRecordRecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule)11 XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)11 Connection (java.sql.Connection)10 Xid (javax.transaction.xa.Xid)10 InitialContext (javax.naming.InitialContext)9 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)7 ExecuteException (org.jboss.byteman.rule.exception.ExecuteException)6 Uid (com.arjuna.ats.arjuna.common.Uid)5 RecoveryManager (com.arjuna.ats.arjuna.recovery.RecoveryManager)5 XAException (javax.transaction.xa.XAException)5 TransactionImple (com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple)4 AtomicAction (com.arjuna.ats.arjuna.AtomicAction)3 InputObjectState (com.arjuna.ats.arjuna.state.InputObjectState)3 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.jts.XARecoveryModule)3