Search in sources :

Example 6 with IgniteTxOptimisticCheckedException

use of org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException in project ignite by apache.

the class GridCacheNodeFailureAbstractTest method checkTransaction.

/**
     * @param concurrency Concurrency.
     * @param isolation Isolation.
     * @throws Exception If check failed.
     */
private void checkTransaction(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Throwable {
    int idx = RAND.nextInt(GRID_CNT);
    info("Grid will be stopped: " + idx);
    Ignite g = grid(idx);
    Transaction tx = g.transactions().txStart(concurrency, isolation);
    try {
        g.cache(DEFAULT_CACHE_NAME).put(KEY, VALUE);
        int checkIdx = (idx + 1) % G.allGrids().size();
        info("Check grid index: " + checkIdx);
        IgniteFuture<?> f = waitForLocalEvent(grid(checkIdx).events(), new P1<Event>() {

            @Override
            public boolean apply(Event e) {
                info("Received grid event: " + e);
                return true;
            }
        }, EVT_NODE_LEFT);
        stopGrid(idx);
        f.get();
        U.sleep(getInteger(IGNITE_TX_SALVAGE_TIMEOUT, 3000));
        IgniteCache<Integer, String> checkCache = jcache(checkIdx);
        boolean locked = false;
        Lock lock = checkCache.lock(KEY);
        for (int i = 0; !locked && i < 3; i++) {
            locked = lock.tryLock();
            if (!locked)
                U.sleep(500);
            else
                break;
        }
        assert locked : "Failed to lock key on cache [idx=" + checkIdx + ", key=" + KEY + ']';
        lock.unlock();
    } catch (IgniteTxOptimisticCheckedException e) {
        U.warn(log, "Optimistic transaction failure (will rollback) [msg=" + e.getMessage() + ", tx=" + tx + ']');
        if (G.state(g.name()) == IgniteState.STARTED)
            tx.rollback();
        assert concurrency == OPTIMISTIC && isolation == SERIALIZABLE;
    } catch (Throwable e) {
        error("Transaction failed (will rollback): " + tx, e);
        if (G.state(g.name()) == IgniteState.STARTED)
            tx.rollback();
        throw e;
    }
}
Also used : IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) Lock(java.util.concurrent.locks.Lock) IgniteSystemProperties.getInteger(org.apache.ignite.IgniteSystemProperties.getInteger) Transaction(org.apache.ignite.transactions.Transaction) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite)

Aggregations

IgniteTxOptimisticCheckedException (org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IgniteTxRollbackCheckedException (org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException)3 IgniteTxTimeoutCheckedException (org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException)3 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)2 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)2 IgniteTxEntry (org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry)2 IgniteTxHeuristicCheckedException (org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException)2 Lock (java.util.concurrent.locks.Lock)1 Ignite (org.apache.ignite.Ignite)1 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)1 IgniteSystemProperties.getInteger (org.apache.ignite.IgniteSystemProperties.getInteger)1 Event (org.apache.ignite.events.Event)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)1 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)1 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)1 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)1 GridDhtTxPrepareFuture (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture)1 GridDhtTxPrepareResponse (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareResponse)1