Search in sources :

Example 6 with TransactionStateChangedEvent

use of org.apache.ignite.events.TransactionStateChangedEvent in project ignite by apache.

the class TxRollbackOnIncorrectParamsTest method testRollbackInsideLocalListenerAfterRemoteFilter.

/**
 */
@Test
public void testRollbackInsideLocalListenerAfterRemoteFilter() throws Exception {
    Ignite ignite = startGrid(0);
    Ignite remote = startGrid(1);
    IgniteCache cacheLocal = ignite.getOrCreateCache(defaultCacheConfiguration());
    IgniteCache cacheRemote = remote.getOrCreateCache(defaultCacheConfiguration());
    AtomicBoolean rollbackFailed = new AtomicBoolean();
    AtomicBoolean alreadyRolledBack = new AtomicBoolean();
    ignite.events().remoteListen((IgniteBiPredicate<UUID, Event>) (uuid, e) -> {
        assert e instanceof TransactionStateChangedEvent;
        TransactionStateChangedEvent evt = (TransactionStateChangedEvent) e;
        Transaction tx = evt.tx();
        try {
            tx.setRollbackOnly();
        } catch (IgniteException ignored) {
            alreadyRolledBack.set(rollbackFailed.getAndSet(true));
        }
        return true;
    }, (IgnitePredicate<Event>) e -> {
        assert e instanceof TransactionStateChangedEvent;
        return true;
    }, EVT_TX_STARTED);
    assertFalse(rollbackFailed.get());
    assertFalse(alreadyRolledBack.get());
    try (Transaction tx = ignite.transactions().txStart()) {
        cacheLocal.put(1, 1);
        tx.commit();
        fail("Should fail prior this line.");
    } catch (CacheException ex) {
        if (MvccFeatureChecker.forcedMvcc())
            assertTrue(ex.toString(), ex.getCause() instanceof TransactionAlreadyCompletedException);
        else
            assertTrue(ex.toString(), ex.getCause() instanceof TransactionRollbackException);
    }
    assertFalse(rollbackFailed.get());
    assertFalse(alreadyRolledBack.get());
    try (Transaction tx = remote.transactions().txStart()) {
        cacheRemote.put(1, 2);
        tx.commit();
    }
    assertTrue(GridTestUtils.waitForCondition(rollbackFailed::get, 5_000));
    assertFalse(alreadyRolledBack.get());
}
Also used : TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) EventType(org.apache.ignite.events.EventType) IgniteException(org.apache.ignite.IgniteException) Event(org.apache.ignite.events.Event) Transaction(org.apache.ignite.transactions.Transaction) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransactionAlreadyCompletedException(org.apache.ignite.transactions.TransactionAlreadyCompletedException) Test(org.junit.Test) EVT_TX_STARTED(org.apache.ignite.events.EventType.EVT_TX_STARTED) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) MvccFeatureChecker(org.apache.ignite.testframework.MvccFeatureChecker) TransactionStateChangedEvent(org.apache.ignite.events.TransactionStateChangedEvent) CacheException(javax.cache.CacheException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) IgniteCache(org.apache.ignite.IgniteCache) Event(org.apache.ignite.events.Event) TransactionStateChangedEvent(org.apache.ignite.events.TransactionStateChangedEvent) Ignite(org.apache.ignite.Ignite) TransactionAlreadyCompletedException(org.apache.ignite.transactions.TransactionAlreadyCompletedException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) UUID(java.util.UUID) TransactionStateChangedEvent(org.apache.ignite.events.TransactionStateChangedEvent) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

Ignite (org.apache.ignite.Ignite)6 TransactionStateChangedEvent (org.apache.ignite.events.TransactionStateChangedEvent)6 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)6 UUID (java.util.UUID)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 CacheException (javax.cache.CacheException)5 IgniteCache (org.apache.ignite.IgniteCache)5 IgniteException (org.apache.ignite.IgniteException)5 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)5 Event (org.apache.ignite.events.Event)5 EventType (org.apache.ignite.events.EventType)5 EVT_TX_STARTED (org.apache.ignite.events.EventType.EVT_TX_STARTED)5 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)5 GridTestUtils (org.apache.ignite.testframework.GridTestUtils)5 MvccFeatureChecker (org.apache.ignite.testframework.MvccFeatureChecker)5 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)5 Transaction (org.apache.ignite.transactions.Transaction)5 TransactionAlreadyCompletedException (org.apache.ignite.transactions.TransactionAlreadyCompletedException)5 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)5 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)5