Search in sources :

Example 1 with TransactionProxyImpl

use of org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl in project ignite by apache.

the class CacheTxFastFinishTest method checkFastTxFinish.

/**
 * @param tx Transaction.
 * @param commit Commit flag.
 */
protected void checkFastTxFinish(Transaction tx, boolean commit) {
    if (commit)
        tx.commit();
    else
        tx.rollback();
    IgniteInternalTx tx0 = ((TransactionProxyImpl) tx).tx();
    assertNull(prepareFuture(tx0));
    assertTrue(finishFuture(tx0) instanceof GridNearTxFastFinishFuture);
}
Also used : GridNearTxFastFinishFuture(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxFastFinishFuture) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl)

Example 2 with TransactionProxyImpl

use of org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl in project ignite by apache.

the class CacheMvccTxNodeMappingTest method checkScenario.

/**
 */
private void checkScenario(IgniteEx ign, int srvCnt, ImmutableMap<UUID, Set<UUID>> txNodes, Runnable r) throws Exception {
    try (Transaction userTx = ign.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        r.run();
        GridNearTxLocal nearTx = ((TransactionProxyImpl) userTx).tx();
        nearTx.prepareNearTxLocal().get();
        List<IgniteInternalTx> txs = IntStream.range(0, srvCnt).mapToObj(i -> txsOnNode(grid(i), nearTx.nearXidVersion())).flatMap(Collection::stream).collect(Collectors.toList());
        assertFalse(txs.isEmpty());
        txs.forEach(tx -> assertEquals(txNodes, repack(tx.transactionNodes())));
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl)

Example 3 with TransactionProxyImpl

use of org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl in project ignite by apache.

the class CacheMvccTxRecoveryTest method testUpdateCountersGapIsClosed.

/**
 * @throws Exception if failed.
 */
@Test
public void testUpdateCountersGapIsClosed() throws Exception {
    int srvCnt = 3;
    startGridsMultiThreaded(srvCnt);
    client = true;
    IgniteEx ign = startGrid(srvCnt);
    IgniteCache<Object, Object> cache = ign.getOrCreateCache(basicCcfg().setBackups(2));
    int vid = 1;
    IgniteEx victim = grid(vid);
    ArrayList<Integer> keys = new ArrayList<>();
    Integer part = null;
    Affinity<Object> aff = ign.affinity(DEFAULT_CACHE_NAME);
    for (int i = 0; i < 2000; i++) {
        int p = aff.partition(i);
        if (aff.isPrimary(victim.localNode(), i)) {
            if (part == null)
                part = p;
            if (p == part)
                keys.add(i);
            if (keys.size() == 2)
                break;
        }
    }
    assert keys.size() == 2;
    Transaction txA = ign.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
    // prevent first transaction prepare on backups
    ((TestRecordingCommunicationSpi) victim.configuration().getCommunicationSpi()).blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

        final AtomicInteger limiter = new AtomicInteger();

        @Override
        public boolean apply(ClusterNode node, Message msg) {
            if (msg instanceof GridDhtTxPrepareRequest)
                return limiter.getAndIncrement() < 2;
            return false;
        }
    });
    cache.query(new SqlFieldsQuery("insert into Integer(_key, _val) values(?, 42)").setArgs(keys.get(0)));
    txA.commitAsync();
    GridCacheVersion aXidVer = ((TransactionProxyImpl) txA).tx().xidVersion();
    assertConditionEventually(() -> txsOnNode(victim, aXidVer).stream().anyMatch(tx -> tx.state() == PREPARING));
    GridTestUtils.runAsync(() -> {
        try (Transaction txB = ign.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
            cache.query(new SqlFieldsQuery("insert into Integer(_key, _val) values(?, 42)").setArgs(keys.get(1)));
            txB.commit();
        }
    }).get();
    long victimUpdCntr = updateCounter(victim.cachex(DEFAULT_CACHE_NAME).context(), keys.get(0));
    List<IgniteEx> backupNodes = grids(srvCnt, i -> i != vid);
    List<IgniteInternalTx> backupTxsA = backupNodes.stream().map(node -> txsOnNode(node, aXidVer)).flatMap(Collection::stream).collect(Collectors.toList());
    // drop primary
    victim.close();
    assertConditionEventually(() -> backupTxsA.stream().allMatch(tx -> tx.state() == ROLLED_BACK));
    backupNodes.stream().map(node -> node.cache(DEFAULT_CACHE_NAME)).forEach(c -> {
        assertEquals(1, c.query(new SqlFieldsQuery("select * from Integer")).getAll().size());
    });
    backupNodes.forEach(node -> {
        for (Integer k : keys) assertEquals(victimUpdCntr, updateCounter(node.cachex(DEFAULT_CACHE_NAME).context(), k));
    });
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Transaction(org.apache.ignite.transactions.Transaction) TRANSACTIONAL_SNAPSHOT(org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT) IgniteEx(org.apache.ignite.internal.IgniteEx) REPEATABLE_READ(org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ) IntPredicate(java.util.function.IntPredicate) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) ROLLBAK(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccTxRecoveryTest.TxEndResult.ROLLBAK) Collection(java.util.Collection) CLIENT(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccTxRecoveryTest.NodeMode.CLIENT) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) IgniteCacheOffheapManager(org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Optional(java.util.Optional) PESSIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC) Message(org.apache.ignite.plugin.extensions.communication.Message) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IntStream(java.util.stream.IntStream) GridDhtTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareRequest) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) Affinity(org.apache.ignite.cache.affinity.Affinity) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) ArrayList(java.util.ArrayList) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) GridNearTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest) StreamSupport(java.util.stream.StreamSupport) SERVER(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccTxRecoveryTest.NodeMode.SERVER) IgniteTxManager(org.apache.ignite.internal.processors.cache.transactions.IgniteTxManager) G(org.apache.ignite.internal.util.typedef.G) PREPARED(org.apache.ignite.transactions.TransactionState.PREPARED) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) GridNearTxFinishResponse(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxFinishResponse) ROLLED_BACK(org.apache.ignite.transactions.TransactionState.ROLLED_BACK) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) COMMITTED(org.apache.ignite.transactions.TransactionState.COMMITTED) PREPARING(org.apache.ignite.transactions.TransactionState.PREPARING) TRANSACTIONAL(org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL) COMMIT(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccTxRecoveryTest.TxEndResult.COMMIT) TimeUnit(java.util.concurrent.TimeUnit) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridDhtPartitionsFullMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsFullMessage) CacheMode(org.apache.ignite.cache.CacheMode) Message(org.apache.ignite.plugin.extensions.communication.Message) GridDhtPartitionsFullMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsFullMessage) ArrayList(java.util.ArrayList) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) IgniteEx(org.apache.ignite.internal.IgniteEx) GridDhtTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareRequest) Test(org.junit.Test)

Example 4 with TransactionProxyImpl

use of org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl in project ignite by apache.

the class CacheMvccTxRecoveryTest method checkRecoveryPrimaryFailure.

/**
 */
private void checkRecoveryPrimaryFailure(TxEndResult endRes, boolean mvccCrd) throws Exception {
    int gridCnt = 4;
    int baseCnt = gridCnt - 1;
    boolean commit = endRes == COMMIT;
    startGridsMultiThreaded(baseCnt);
    client = true;
    IgniteEx nearNode = startGrid(baseCnt);
    IgniteCache<Object, Object> cache = nearNode.getOrCreateCache(basicCcfg().setBackups(1));
    Affinity<Object> aff = nearNode.affinity(DEFAULT_CACHE_NAME);
    List<Integer> keys = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        if (aff.isPrimary(grid(0).localNode(), i) && aff.isBackup(grid(1).localNode(), i)) {
            keys.add(i);
            break;
        }
    }
    for (int i = 0; i < 100; i++) {
        if (aff.isPrimary(grid(1).localNode(), i) && aff.isBackup(grid(2).localNode(), i)) {
            keys.add(i);
            break;
        }
    }
    assert keys.size() == 2;
    int victim, victimBackup;
    if (mvccCrd) {
        victim = 0;
        victimBackup = 1;
    } else {
        victim = 1;
        victimBackup = 2;
    }
    TestRecordingCommunicationSpi victimComm = (TestRecordingCommunicationSpi) grid(victim).configuration().getCommunicationSpi();
    if (commit)
        victimComm.blockMessages(GridNearTxFinishResponse.class, nearNode.name());
    else
        victimComm.blockMessages(GridDhtTxPrepareRequest.class, grid(victimBackup).name());
    GridNearTxLocal nearTx = ((TransactionProxyImpl) nearNode.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)).tx();
    for (Integer k : keys) cache.query(new SqlFieldsQuery("insert into Integer(_key, _val) values(?, 42)").setArgs(k));
    List<IgniteInternalTx> txs = IntStream.range(0, baseCnt).filter(i -> i != victim).mapToObj(i -> txsOnNode(grid(i), nearTx.xidVersion())).flatMap(Collection::stream).collect(Collectors.toList());
    IgniteInternalFuture<IgniteInternalTx> commitFut = nearTx.commitAsync();
    if (commit)
        assertConditionEventually(() -> txs.stream().allMatch(tx -> tx.state() == COMMITTED));
    else
        assertConditionEventually(() -> txs.stream().anyMatch(tx -> tx.state() == PREPARED));
    // drop victim
    grid(victim).close();
    awaitPartitionMapExchange();
    assertConditionEventually(() -> txs.stream().allMatch(tx -> tx.state() == (commit ? COMMITTED : ROLLED_BACK)));
    assert victimComm.hasBlockedMessages();
    if (commit) {
        assertConditionEventually(() -> {
            int rowsCnt = G.allGrids().get(0).cache(DEFAULT_CACHE_NAME).query(new SqlFieldsQuery("select * from Integer")).getAll().size();
            return rowsCnt == keys.size();
        });
    } else {
        int rowsCnt = G.allGrids().get(0).cache(DEFAULT_CACHE_NAME).query(new SqlFieldsQuery("select * from Integer")).getAll().size();
        assertEquals(0, rowsCnt);
    }
    assertTrue(commitFut.isDone());
    assertPartitionCountersAreConsistent(keys, grids(baseCnt, i -> i != victim));
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Transaction(org.apache.ignite.transactions.Transaction) TRANSACTIONAL_SNAPSHOT(org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT) IgniteEx(org.apache.ignite.internal.IgniteEx) REPEATABLE_READ(org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ) IntPredicate(java.util.function.IntPredicate) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) ROLLBAK(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccTxRecoveryTest.TxEndResult.ROLLBAK) Collection(java.util.Collection) CLIENT(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccTxRecoveryTest.NodeMode.CLIENT) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) IgniteCacheOffheapManager(org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Optional(java.util.Optional) PESSIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC) Message(org.apache.ignite.plugin.extensions.communication.Message) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IntStream(java.util.stream.IntStream) GridDhtTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareRequest) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) Affinity(org.apache.ignite.cache.affinity.Affinity) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) ArrayList(java.util.ArrayList) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) GridNearTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest) StreamSupport(java.util.stream.StreamSupport) SERVER(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccTxRecoveryTest.NodeMode.SERVER) IgniteTxManager(org.apache.ignite.internal.processors.cache.transactions.IgniteTxManager) G(org.apache.ignite.internal.util.typedef.G) PREPARED(org.apache.ignite.transactions.TransactionState.PREPARED) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) GridNearTxFinishResponse(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxFinishResponse) ROLLED_BACK(org.apache.ignite.transactions.TransactionState.ROLLED_BACK) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) COMMITTED(org.apache.ignite.transactions.TransactionState.COMMITTED) PREPARING(org.apache.ignite.transactions.TransactionState.PREPARING) TRANSACTIONAL(org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL) COMMIT(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccTxRecoveryTest.TxEndResult.COMMIT) TimeUnit(java.util.concurrent.TimeUnit) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridDhtPartitionsFullMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsFullMessage) CacheMode(org.apache.ignite.cache.CacheMode) ArrayList(java.util.ArrayList) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) GridNearTxFinishResponse(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxFinishResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) IgniteEx(org.apache.ignite.internal.IgniteEx) GridDhtTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareRequest) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl)

Example 5 with TransactionProxyImpl

use of org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl in project ignite by apache.

the class GridExchangeFreeCellularSwitchComplexOperationsTest method testComplexOperationsRecoveryOnCellularSwitch.

/**
 * Test checks that txs will be recovered on Cellular switch if prepared, regardless of their content,
 * as well as upcoming txs will be committed.
 */
@Test
public void testComplexOperationsRecoveryOnCellularSwitch() throws Exception {
    int nodes = 6;
    startGridsMultiThreaded(nodes);
    blockRecoveryMessages();
    CellularCluster cluster = resolveCellularCluster(nodes, startFrom);
    Ignite orig = cluster.orig;
    Ignite failed = cluster.failed;
    List<Ignite> brokenCellNodes = cluster.brokenCellNodes;
    List<Ignite> aliveCellNodes = cluster.aliveCellNodes;
    int recFutsCnt = 7;
    CountDownLatch prepLatch = new CountDownLatch(recFutsCnt);
    CountDownLatch commitLatch = new CountDownLatch(1);
    Set<Integer> partSet = new GridConcurrentHashSet<>();
    Set<Integer> replSet = new GridConcurrentHashSet<>();
    List<IgniteInternalFuture<?>> futs = new ArrayList<>();
    AtomicInteger cnt = new AtomicInteger();
    BiFunction<Ignite, String, Integer> nextPrimaryKey = (ignite, cacheName) -> {
        int idx = cnt.getAndIncrement();
        return primaryKeys(ignite.getOrCreateCache(cacheName), idx + 1).get(idx);
    };
    BiConsumer<String, Set<Integer>> singlePutEverywhere = (cacheName, globSet) -> {
        try {
            Transaction tx = orig.transactions().txStart(concurrency, isolation);
            Set<Integer> set = new HashSet<>();
            for (Ignite ignite : G.allGrids()) {
                if (ignite.configuration().isClientMode())
                    continue;
                set.add(nextPrimaryKey.apply(ignite, cacheName));
            }
            globSet.addAll(set);
            IgniteCache<Integer, Integer> cache = orig.getOrCreateCache(cacheName);
            for (Integer key : set) cache.put(key, key);
            ((TransactionProxyImpl<?, ?>) tx).tx().prepare(true);
            prepLatch.countDown();
            commitLatch.await();
            if (orig != failed)
                ((TransactionProxyImpl<?, ?>) tx).commit();
        } catch (Exception e) {
            fail("Should not happen [exception=" + e + "]");
        }
    };
    futs.add(multithreadedAsync(() -> singlePutEverywhere.accept(PART_CACHE_NAME, partSet), 1));
    futs.add(multithreadedAsync(() -> singlePutEverywhere.accept(REPL_CACHE_NAME, replSet), 1));
    Consumer<Integer> putEverywhereToBoth = (putPerTx) -> {
        try {
            Transaction tx = orig.transactions().txStart(concurrency, isolation);
            Set<Integer> pSet = new HashSet<>();
            Set<Integer> rSet = new HashSet<>();
            for (int i = 0; i < putPerTx; i++) for (Ignite ignite : G.allGrids()) {
                if (ignite.configuration().isClientMode())
                    continue;
                pSet.add(nextPrimaryKey.apply(ignite, PART_CACHE_NAME));
                rSet.add(nextPrimaryKey.apply(ignite, REPL_CACHE_NAME));
            }
            partSet.addAll(pSet);
            replSet.addAll(rSet);
            IgniteCache<Integer, Integer> pCache = orig.getOrCreateCache(PART_CACHE_NAME);
            IgniteCache<Integer, Integer> rCache = orig.getOrCreateCache(REPL_CACHE_NAME);
            for (Integer key : pSet) pCache.put(key, key);
            for (Integer key : rSet) rCache.put(key, key);
            ((TransactionProxyImpl<?, ?>) tx).tx().prepare(true);
            prepLatch.countDown();
            commitLatch.await();
            if (orig != failed)
                ((TransactionProxyImpl<?, ?>) tx).commit();
        } catch (Exception e) {
            fail("Should not happen [exception=" + e + "]");
        }
    };
    futs.add(multithreadedAsync(() -> putEverywhereToBoth.accept(1), 1));
    futs.add(multithreadedAsync(() -> putEverywhereToBoth.accept(2), 1));
    futs.add(multithreadedAsync(() -> putEverywhereToBoth.accept(10), 1));
    Consumer<Boolean> singleTxPerCell = (partAtBrokenCell) -> {
        try {
            Transaction tx = orig.transactions().txStart(concurrency, isolation);
            Integer pKey = partAtBrokenCell ? nextPrimaryKey.apply(failed, PART_CACHE_NAME) : nextPrimaryKey.apply(aliveCellNodes.get(0), PART_CACHE_NAME);
            Integer rKey = partAtBrokenCell ? nextPrimaryKey.apply(aliveCellNodes.get(0), REPL_CACHE_NAME) : nextPrimaryKey.apply(failed, REPL_CACHE_NAME);
            IgniteCache<Integer, Integer> pCache = orig.getOrCreateCache(PART_CACHE_NAME);
            IgniteCache<Integer, Integer> rCache = orig.getOrCreateCache(REPL_CACHE_NAME);
            pCache.put(pKey, pKey);
            rCache.put(rKey, rKey);
            partSet.add(pKey);
            replSet.add((rKey));
            ((TransactionProxyImpl<?, ?>) tx).tx().prepare(true);
            prepLatch.countDown();
            commitLatch.await();
            if (orig != failed)
                ((TransactionProxyImpl<?, ?>) tx).commit();
        } catch (Exception e) {
            fail("Should not happen [exception=" + e + "]");
        }
    };
    futs.add(multithreadedAsync(() -> singleTxPerCell.accept(true), 1));
    futs.add(multithreadedAsync(() -> singleTxPerCell.accept(false), 1));
    prepLatch.await();
    assertEquals(futs.size(), recFutsCnt);
    // Stopping node.
    failed.close();
    awaitForSwitchOnNodeLeft(failed);
    Consumer<Ignite> partTxRun = (ignite) -> {
        try {
            IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(PART_CACHE_NAME);
            try (Transaction tx = ignite.transactions().txStart(concurrency, isolation)) {
                Integer key = nextPrimaryKey.apply(ignite, PART_CACHE_NAME);
                partSet.add(key);
                cache.put(key, key);
                tx.commit();
            }
        } catch (Exception e) {
            fail("Should not happen [exception=" + e + "]");
        }
    };
    Consumer<Ignite> replTxRun = (ignite) -> {
        try {
            IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(REPL_CACHE_NAME);
            try (Transaction tx = ignite.transactions().txStart(concurrency, isolation)) {
                Integer key = nextPrimaryKey.apply(ignite, REPL_CACHE_NAME);
                replSet.add(key);
                cache.put(key, key);
                tx.commit();
            }
        } catch (Exception e) {
            fail("Should not happen [exception=" + e + "]");
        }
    };
    for (Ignite brokenCellNode : brokenCellNodes) {
        futs.add(multithreadedAsync(() -> partTxRun.accept(brokenCellNode), 1));
        futs.add(multithreadedAsync(() -> replTxRun.accept(brokenCellNode), 1));
    }
    for (Ignite aliveCellNode : aliveCellNodes) {
        futs.add(multithreadedAsync(() -> partTxRun.accept(aliveCellNode), 1));
        futs.add(multithreadedAsync(() -> replTxRun.accept(aliveCellNode), 1));
    }
    // Allowing recovery.
    for (Ignite ignite : G.allGrids()) {
        TestRecordingCommunicationSpi spi = (TestRecordingCommunicationSpi) ignite.configuration().getCommunicationSpi();
        spi.stopBlock(true, blockedMsg -> true);
    }
    commitLatch.countDown();
    for (IgniteInternalFuture<?> fut : futs) fut.get();
    for (Ignite node : G.allGrids()) {
        IgniteCache<Integer, Integer> partCache = node.getOrCreateCache(PART_CACHE_NAME);
        IgniteCache<Integer, Integer> replCache = node.getOrCreateCache(REPL_CACHE_NAME);
        for (Integer key : partSet) assertEquals(key, partCache.get(key));
        for (Integer key : replSet) assertEquals(key, replCache.get(key));
    }
    // Final check that any transactions are absent.
    checkTransactionsCount(null, 0, brokenCellNodes, 0, aliveCellNodes, 0, null);
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) G(org.apache.ignite.internal.util.typedef.G) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) Collection(java.util.Collection) BiFunction(java.util.function.BiFunction) Transaction(org.apache.ignite.transactions.Transaction) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) RunWith(org.junit.runner.RunWith) Set(java.util.Set) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl) IgniteCache(org.apache.ignite.IgniteCache) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) Parameterized(org.junit.runners.Parameterized) Set(java.util.Set) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl) Test(org.junit.Test)

Aggregations

TransactionProxyImpl (org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl)19 Transaction (org.apache.ignite.transactions.Transaction)13 GridNearTxLocal (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)12 ArrayList (java.util.ArrayList)9 Ignite (org.apache.ignite.Ignite)9 TestRecordingCommunicationSpi (org.apache.ignite.internal.TestRecordingCommunicationSpi)9 IgniteInternalTx (org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 IgniteCache (org.apache.ignite.IgniteCache)8 ClusterNode (org.apache.ignite.cluster.ClusterNode)8 Test (org.junit.Test)8 Collection (java.util.Collection)7 IgniteEx (org.apache.ignite.internal.IgniteEx)6 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)6 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)6 List (java.util.List)5 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)5 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)5 Optional (java.util.Optional)4