Search in sources :

Example 71 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class IgniteCacheCrossCacheTxFailoverTest method crossCacheTxFailover.

/**
 * @param cacheMode Cache mode.
 * @param sameAff If {@code false} uses different number of partitions for caches.
 * @param concurrency Transaction concurrency.
 * @param isolation Transaction isolation.
 * @throws Exception If failed.
 */
private void crossCacheTxFailover(CacheMode cacheMode, boolean sameAff, final TransactionConcurrency concurrency, final TransactionIsolation isolation) throws Exception {
    IgniteKernal ignite0 = (IgniteKernal) ignite(0);
    final AtomicBoolean stop = new AtomicBoolean();
    try {
        ignite0.createCache(cacheConfiguration(CACHE1, cacheMode, 256));
        ignite0.createCache(cacheConfiguration(CACHE2, cacheMode, sameAff ? 256 : 128));
        final AtomicInteger threadIdx = new AtomicInteger();
        IgniteInternalFuture<?> fut = runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                int idx = threadIdx.getAndIncrement();
                Ignite ignite = ignite(idx % GRID_CNT);
                log.info("Started update thread [node=" + ignite.name() + ", client=" + ignite.configuration().isClientMode() + ']');
                IgniteCache<TestKey, TestValue> cache1 = ignite.cache(CACHE1);
                IgniteCache<TestKey, TestValue> cache2 = ignite.cache(CACHE2);
                assertNotSame(cache1, cache2);
                IgniteTransactions txs = ignite.transactions();
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                long iter = 0;
                while (!stop.get()) {
                    boolean sameKey = rnd.nextBoolean();
                    try {
                        try (Transaction tx = txs.txStart(concurrency, isolation)) {
                            if (sameKey) {
                                TestKey key = new TestKey(rnd.nextLong(KEY_RANGE));
                                cacheOperation(rnd, cache1, key);
                                cacheOperation(rnd, cache2, key);
                            } else {
                                TestKey key1 = new TestKey(rnd.nextLong(KEY_RANGE));
                                TestKey key2 = new TestKey(key1.key() + 1);
                                cacheOperation(rnd, cache1, key1);
                                cacheOperation(rnd, cache2, key2);
                            }
                            tx.commit();
                        }
                    } catch (CacheException | IgniteException e) {
                        log.info("Update error: " + e);
                    }
                    if (iter++ % 500 == 0)
                        log.info("Iteration: " + iter);
                }
                return null;
            }

            /**
             * @param rnd Random.
             * @param cache Cache.
             * @param key Key.
             */
            private void cacheOperation(ThreadLocalRandom rnd, IgniteCache<TestKey, TestValue> cache, TestKey key) {
                switch(rnd.nextInt(4)) {
                    case 0:
                        cache.put(key, new TestValue(rnd.nextLong()));
                        break;
                    case 1:
                        cache.remove(key);
                        break;
                    case 2:
                        cache.invoke(key, new TestEntryProcessor(rnd.nextBoolean() ? 1L : null));
                        break;
                    case 3:
                        cache.get(key);
                        break;
                    default:
                        assert false;
                }
            }
        }, 10, "tx-thread");
        long stopTime = System.currentTimeMillis() + 3 * 60_000;
        long topVer = ignite0.cluster().topologyVersion();
        boolean failed = false;
        while (System.currentTimeMillis() < stopTime) {
            log.info("Start node.");
            IgniteKernal ignite = (IgniteKernal) startGrid(GRID_CNT);
            assertFalse(ignite.configuration().isClientMode());
            topVer++;
            IgniteInternalFuture<?> affFut = ignite.context().cache().context().exchange().affinityReadyFuture(new AffinityTopologyVersion(topVer));
            try {
                if (affFut != null)
                    affFut.get(30_000);
            } catch (IgniteFutureTimeoutCheckedException ignored) {
                log.error("Failed to wait for affinity future after start: " + topVer);
                failed = true;
                break;
            }
            Thread.sleep(500);
            log.info("Stop node.");
            stopGrid(GRID_CNT);
            topVer++;
            affFut = ignite0.context().cache().context().exchange().affinityReadyFuture(new AffinityTopologyVersion(topVer));
            try {
                if (affFut != null)
                    affFut.get(30_000);
            } catch (IgniteFutureTimeoutCheckedException ignored) {
                log.error("Failed to wait for affinity future after stop: " + topVer);
                failed = true;
                break;
            }
        }
        stop.set(true);
        fut.get();
        assertFalse("Test failed, see log for details.", failed);
    } finally {
        stop.set(true);
        ignite0.destroyCache(CACHE1);
        ignite0.destroyCache(CACHE2);
        AffinityTopologyVersion topVer = ignite0.context().cache().context().exchange().lastTopologyFuture().get();
        for (Ignite ignite : G.allGrids()) ((IgniteKernal) ignite).context().cache().context().exchange().affinityReadyFuture(topVer).get();
        awaitPartitionMapExchange();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) Ignite(org.apache.ignite.Ignite)

Example 72 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class IgniteCachePrimaryNodeFailureRecoveryAbstractTest method primaryAndOriginatingNodeFailure.

/**
 * @param locBackupKey If {@code true} uses one key which is backup for originating node.
 * @param rollback If {@code true} tests rollback after primary node failure.
 * @param optimistic If {@code true} tests optimistic transaction.
 * @throws Exception If failed.
 */
private void primaryAndOriginatingNodeFailure(final boolean locBackupKey, final boolean rollback, boolean optimistic) throws Exception {
    // TODO IGNITE-6174: when exchanges can be merged test fails because of IGNITE-6174.
    System.setProperty(IGNITE_EXCHANGE_COMPATIBILITY_VER_1, "true");
    try {
        IgniteCache<Integer, Integer> cache0 = jcache(0);
        IgniteCache<Integer, Integer> cache2 = jcache(2);
        Affinity<Integer> aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
        Integer key0 = null;
        for (int key = 0; key < 10_000; key++) {
            if (aff.isPrimary(ignite(1).cluster().localNode(), key)) {
                if (locBackupKey == aff.isBackup(ignite(0).cluster().localNode(), key)) {
                    key0 = key;
                    break;
                }
            }
        }
        assertNotNull(key0);
        final Integer key1 = key0;
        final Integer key2 = primaryKey(cache2);
        int backups = cache0.getConfiguration(CacheConfiguration.class).getBackups();
        final Collection<ClusterNode> key1Nodes = (locBackupKey && backups < 2) ? null : aff.mapKeyToPrimaryAndBackups(key1);
        final Collection<ClusterNode> key2Nodes = aff.mapKeyToPrimaryAndBackups(key2);
        TestCommunicationSpi commSpi = (TestCommunicationSpi) ignite(0).configuration().getCommunicationSpi();
        IgniteTransactions txs = ignite(0).transactions();
        Transaction tx = txs.txStart(optimistic ? OPTIMISTIC : PESSIMISTIC, REPEATABLE_READ);
        log.info("Put key1 [key1=" + key1 + ", nodes=" + U.nodeIds(aff.mapKeyToPrimaryAndBackups(key1)) + ']');
        cache0.put(key1, key1);
        log.info("Put key2 [key2=" + key2 + ", nodes=" + U.nodeIds(aff.mapKeyToPrimaryAndBackups(key2)) + ']');
        cache0.put(key2, key2);
        log.info("Start prepare.");
        GridNearTxLocal txEx = ((TransactionProxyImpl) tx).tx();
        // Do not allow to finish prepare for key2.
        commSpi.blockMessages(ignite(2).cluster().localNode().id());
        IgniteInternalFuture<?> prepFut = txEx.prepareNearTxLocal();
        waitPrepared(ignite(1));
        log.info("Stop one primary node.");
        stopGrid(1);
        // Wait some time to catch possible issues in tx recovery.
        U.sleep(1000);
        if (!rollback) {
            commSpi.stopBlock();
            prepFut.get(10_000);
        }
        log.info("Stop originating node.");
        stopGrid(0);
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                try {
                    checkKey(key1, rollback ? null : key1Nodes);
                    checkKey(key2, rollback ? null : key2Nodes);
                    return true;
                } catch (AssertionError e) {
                    log.info("Check failed: " + e);
                    return false;
                }
            }
        }, 5000);
        checkKey(key1, rollback ? null : key1Nodes);
        checkKey(key2, rollback ? null : key2Nodes);
    } finally {
        System.clearProperty(IGNITE_EXCHANGE_COMPATIBILITY_VER_1);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteTransactions(org.apache.ignite.IgniteTransactions) Transaction(org.apache.ignite.transactions.Transaction) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl)

Example 73 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class IgniteCachePrimaryNodeFailureRecoveryAbstractTest method primaryNodeFailure.

/**
 * @param locBackupKey If {@code true} uses one key which is backup for originating node.
 * @param rollback If {@code true} tests rollback after primary node failure.
 * @param optimistic If {@code true} tests optimistic transaction.
 * @throws Exception If failed.
 */
private void primaryNodeFailure(boolean locBackupKey, final boolean rollback, boolean optimistic) throws Exception {
    IgniteCache<Integer, Integer> cache0 = jcache(0);
    IgniteCache<Integer, Integer> cache2 = jcache(2);
    Affinity<Integer> aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
    Integer key0 = null;
    for (int key = 0; key < 10_000; key++) {
        if (aff.isPrimary(ignite(1).cluster().localNode(), key)) {
            if (locBackupKey == aff.isBackup(ignite(0).cluster().localNode(), key)) {
                key0 = key;
                break;
            }
        }
    }
    assertNotNull(key0);
    final Integer key1 = key0;
    final Integer key2 = primaryKey(cache2);
    final Collection<ClusterNode> key1Nodes = aff.mapKeyToPrimaryAndBackups(key1);
    final Collection<ClusterNode> key2Nodes = aff.mapKeyToPrimaryAndBackups(key2);
    TestCommunicationSpi commSpi = (TestCommunicationSpi) ignite(0).configuration().getCommunicationSpi();
    IgniteTransactions txs = ignite(0).transactions();
    try (Transaction tx = txs.txStart(optimistic ? OPTIMISTIC : PESSIMISTIC, REPEATABLE_READ)) {
        log.info("Put key1: " + key1);
        cache0.put(key1, key1);
        log.info("Put key2: " + key2);
        cache0.put(key2, key2);
        log.info("Start prepare.");
        GridNearTxLocal txEx = ((TransactionProxyImpl) tx).tx();
        // Do not allow to finish prepare for key2.
        commSpi.blockMessages(ignite(2).cluster().localNode().id());
        IgniteInternalFuture<?> prepFut = txEx.prepareNearTxLocal();
        waitPrepared(ignite(1));
        log.info("Stop one primary node.");
        stopGrid(1);
        // Wait some time to catch possible issues in tx recovery.
        U.sleep(1000);
        commSpi.stopBlock();
        prepFut.get(10_000);
        if (rollback) {
            log.info("Rollback.");
            tx.rollback();
        } else {
            log.info("Commit.");
            tx.commit();
        }
    }
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            try {
                checkKey(key1, rollback ? null : key1Nodes);
                checkKey(key2, rollback ? null : key2Nodes);
                return true;
            } catch (AssertionError e) {
                log.info("Check failed: " + e);
                return false;
            }
        }
    }, 5000);
    checkKey(key1, rollback ? null : key1Nodes);
    checkKey(key2, rollback ? null : key2Nodes);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteTransactions(org.apache.ignite.IgniteTransactions) Transaction(org.apache.ignite.transactions.Transaction) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl)

Example 74 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class IgniteCacheExpireAndUpdateConsistencyTest method updateAndEventConsistencyTest.

/**
 * @param node Node.
 * @param cacheName Cache name.
 * @param keyVal Key counter.
 * @param nodesEvts Events map.
 * @param useTx If {@code true} executes update with explicit transaction.
 * @throws Exception If failed.
 */
private void updateAndEventConsistencyTest(final Ignite node, String cacheName, final AtomicInteger keyVal, List<ConcurrentMap<TestKey, List<T2<TestValue, TestValue>>>> nodesEvts, final boolean useTx) throws Exception {
    final ConcurrentMap<TestKey, List<T2<TestValue, TestValue>>> updates = new ConcurrentHashMap<>();
    final int THREADS = 5;
    final int KEYS_PER_THREAD = 100;
    final IgniteCache<TestKey, TestValue> cache = node.cache(cacheName);
    final IgniteCache<TestKey, TestValue> expPlcCache = cache.withExpiryPolicy(new CreatedExpiryPolicy(new Duration(SECONDS, 2)));
    GridTestUtils.runMultiThreaded(new IgniteInClosure<Integer>() {

        @Override
        public void apply(Integer idx) {
            List<TestKey> keys = new ArrayList<>();
            for (int i = 0; i < KEYS_PER_THREAD; i++) keys.add(new TestKey(keyVal.incrementAndGet()));
            for (TestKey key : keys) {
                expPlcCache.put(key, new TestValue(0));
                List<T2<TestValue, TestValue>> keyUpdates = new ArrayList<>();
                keyUpdates.add(new T2<>(new TestValue(0), (TestValue) null));
                updates.put(key, keyUpdates);
            }
            long stopTime = U.currentTimeMillis() + 10_000;
            int val = 0;
            Set<TestKey> expired = new HashSet<>();
            IgniteTransactions txs = node.transactions();
            while (U.currentTimeMillis() < stopTime) {
                val++;
                TestValue newVal = new TestValue(val);
                for (TestKey key : keys) {
                    Transaction tx = useTx ? txs.txStart(PESSIMISTIC, REPEATABLE_READ) : null;
                    TestValue oldVal = cache.getAndPut(key, newVal);
                    if (tx != null)
                        tx.commit();
                    List<T2<TestValue, TestValue>> keyUpdates = updates.get(key);
                    keyUpdates.add(new T2<>(newVal, oldVal));
                    if (oldVal == null)
                        expired.add(key);
                }
                if (expired.size() == keys.size())
                    break;
            }
            assertEquals(keys.size(), expired.size());
        }
    }, THREADS, "update-thread");
    for (ConcurrentMap<TestKey, List<T2<TestValue, TestValue>>> evts : nodesEvts) checkEvents(updates, evts);
    nodesEvts.clear();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) IgniteTransactions(org.apache.ignite.IgniteTransactions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) T2(org.apache.ignite.internal.util.typedef.T2)

Example 75 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class IgniteTxCachePrimarySyncTest method checkTxSyncMode.

/**
 * @param ignite Node.
 * @param commit If {@code true} commits transaction.
 */
private void checkTxSyncMode(Ignite ignite, boolean commit) {
    IgniteTransactions txs = ignite.transactions();
    IgniteCache<Object, Object> fullSync1 = ignite.cache("fullSync1");
    IgniteCache<Object, Object> fullSync2 = ignite.cache("fullSync2");
    IgniteCache<Object, Object> fullAsync1 = ignite.cache("fullAsync1");
    IgniteCache<Object, Object> fullAsync2 = ignite.cache("fullAsync2");
    IgniteCache<Object, Object> primarySync1 = ignite.cache("primarySync1");
    IgniteCache<Object, Object> primarySync2 = ignite.cache("primarySync2");
    for (int i = 0; i < 3; i++) {
        int key = 0;
        for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
            for (TransactionIsolation isolation : TransactionIsolation.values()) {
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullSync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    checkSyncMode(tx, FULL_ASYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    primarySync1.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    for (int j = 0; j < 100; j++) fullSync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    for (int j = 0; j < 100; j++) fullAsync1.put(key++, 1);
                    checkSyncMode(tx, FULL_ASYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    for (int j = 0; j < 100; j++) primarySync1.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullSync1.put(key++, 1);
                    fullSync2.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    fullAsync2.put(key++, 1);
                    checkSyncMode(tx, FULL_ASYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    primarySync1.put(key++, 1);
                    primarySync2.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullSync1.put(key++, 1);
                    primarySync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    primarySync1.put(key++, 1);
                    fullSync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullSync1.put(key++, 1);
                    fullAsync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    fullSync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    primarySync1.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    primarySync1.put(key++, 1);
                    fullAsync2.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    primarySync1.put(key++, 1);
                    fullAsync1.put(key++, 1);
                    checkSyncMode(tx, PRIMARY_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullSync1.put(key++, 1);
                    fullAsync1.put(key++, 1);
                    primarySync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
                try (Transaction tx = txs.txStart(concurrency, isolation)) {
                    fullAsync1.put(key++, 1);
                    primarySync1.put(key++, 1);
                    fullSync1.put(key++, 1);
                    checkSyncMode(tx, FULL_SYNC);
                    if (commit)
                        tx.commit();
                }
            }
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Aggregations

IgniteTransactions (org.apache.ignite.IgniteTransactions)81 Transaction (org.apache.ignite.transactions.Transaction)78 Ignite (org.apache.ignite.Ignite)56 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)54 TransactionOptimisticException (org.apache.ignite.transactions.TransactionOptimisticException)28 IgniteCache (org.apache.ignite.IgniteCache)22 IgniteException (org.apache.ignite.IgniteException)18 CacheException (javax.cache.CacheException)17 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)13 HashMap (java.util.HashMap)12 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)10 ArrayList (java.util.ArrayList)9 Callable (java.util.concurrent.Callable)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)9 LinkedHashMap (java.util.LinkedHashMap)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 CyclicBarrier (java.util.concurrent.CyclicBarrier)8