Search in sources :

Example 6 with REPEATABLE_READ

use of org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ in project ignite by apache.

the class TxRollbackOnTimeoutTest method testRollbackOnNearNodeLeft.

/**
 * Tests that transactions from near node that has left are rolled back on server nodes.
 *
 * @throws Exception If failed.
 */
@Test
public void testRollbackOnNearNodeLeft() throws Exception {
    Ignite client = startClient();
    Integer pk0 = primaryKey(grid(0).cache(CACHE_NAME));
    Integer pk1 = primaryKey(grid(1).cache(CACHE_NAME));
    CountDownLatch locked = new CountDownLatch(1);
    CountDownLatch blocked = new CountDownLatch(1);
    IgniteInternalFuture<Void> fut = runAsync(new Callable<Void>() {

        @Override
        public Void call() {
            try (Transaction tx0 = client.transactions().txStart()) {
                client.cache(CACHE_NAME).put(pk0, 0);
                locked.countDown();
                awaitQuiet(blocked);
                tx0.commit();
            } catch (Exception ignored) {
            // No-op.
            }
            return null;
        }
    });
    IgniteInternalFuture fut2 = runAsync(new Runnable() {

        @Override
        public void run() {
            try (Transaction tx1 = client.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, 1000, 0)) {
                awaitQuiet(locked);
                client.cache(CACHE_NAME).put(pk1, 1);
                spi(client).blockMessages((node, msg) -> msg instanceof GridNearTxFinishRequest);
                spi(grid(0)).blockMessages((node, msg) -> msg instanceof GridNearLockResponse);
                client.cache(CACHE_NAME).put(pk0, 1);
                fail();
            } catch (Exception e) {
                assertTrue(X.hasCause(e, TransactionTimeoutException.class));
            }
        }
    });
    spi(client).waitForBlocked();
    spi(grid(0)).waitForBlocked();
    fut2.get();
    client.close();
    spi(grid(0)).stopBlock();
    blocked.countDown();
    fut.get();
    for (int i = 0; i < GRID_CNT; i++) assertTrue(grid(i).context().cache().context().tm().activeTransactions().isEmpty());
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) TestRecordingCommunicationSpi.spi(org.apache.ignite.internal.TestRecordingCommunicationSpi.spi) IgniteUtils.awaitQuiet(org.apache.ignite.internal.util.IgniteUtils.awaitQuiet) SERIALIZABLE(org.apache.ignite.transactions.TransactionIsolation.SERIALIZABLE) GridNearTxFinishRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxFinishRequest) Transaction(org.apache.ignite.transactions.Transaction) Random(java.util.Random) IgniteEx(org.apache.ignite.internal.IgniteEx) REPEATABLE_READ(org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ) GridTestUtils.runAsync(org.apache.ignite.testframework.GridTestUtils.runAsync) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) X(org.apache.ignite.internal.util.typedef.X) Thread.sleep(java.lang.Thread.sleep) IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) TransactionDeadlockException(org.apache.ignite.transactions.TransactionDeadlockException) OPTIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.OPTIMISTIC) READ_COMMITTED(org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED) IgniteCache(org.apache.ignite.IgniteCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) MvccFeatureChecker(org.apache.ignite.testframework.MvccFeatureChecker) PESSIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) Message(org.apache.ignite.plugin.extensions.communication.Message) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) GridNearLockRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearLockRequest) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) LongAdder(java.util.concurrent.atomic.LongAdder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) U(org.apache.ignite.internal.util.typedef.internal.U) Callable(java.util.concurrent.Callable) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridDhtTxPrepareResponse(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareResponse) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) GridNearTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest) CacheException(javax.cache.CacheException) Assume(org.junit.Assume) G(org.apache.ignite.internal.util.typedef.G) F(org.apache.ignite.internal.util.typedef.F) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) FULL_SYNC(org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC) TRANSACTIONAL(org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL) GridNearLockResponse(org.apache.ignite.internal.processors.cache.distributed.near.GridNearLockResponse) SF(org.apache.ignite.testframework.GridTestUtils.SF) TreeMap(java.util.TreeMap) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridDhtPartitionsFullMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsFullMessage) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) GridNearLockResponse(org.apache.ignite.internal.processors.cache.distributed.near.GridNearLockResponse) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) TransactionDeadlockException(org.apache.ignite.transactions.TransactionDeadlockException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) CacheException(javax.cache.CacheException) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) Ignite(org.apache.ignite.Ignite) GridNearTxFinishRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxFinishRequest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 7 with REPEATABLE_READ

use of org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ in project ignite by apache.

the class CacheMvccTxRecoveryTest method checkRecoveryNearFailure.

/**
 */
private void checkRecoveryNearFailure(TxEndResult endRes, NodeMode nearNodeMode) throws Exception {
    int gridCnt = 4;
    int baseCnt = gridCnt - 1;
    boolean commit = endRes == COMMIT;
    startGridsMultiThreaded(baseCnt);
    // tweak client/server near
    client = nearNodeMode == CLIENT;
    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;
    TestRecordingCommunicationSpi nearComm = (TestRecordingCommunicationSpi) nearNode.configuration().getCommunicationSpi();
    if (!commit)
        nearComm.blockMessages(GridNearTxPrepareRequest.class, grid(1).name());
    GridTestUtils.runAsync(() -> {
        // run in separate thread to exclude tx from thread-local map
        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).mapToObj(i -> txsOnNode(grid(i), nearTx.xidVersion())).flatMap(Collection::stream).collect(Collectors.toList());
        IgniteInternalFuture<?> prepareFut = nearTx.prepareNearTxLocal();
        if (commit)
            prepareFut.get();
        else
            assertConditionEventually(() -> txs.stream().anyMatch(tx -> tx.state() == PREPARED));
        // drop near
        nearNode.close();
        assertConditionEventually(() -> txs.stream().allMatch(tx -> tx.state() == (commit ? COMMITTED : ROLLED_BACK)));
        return null;
    }).get();
    if (commit) {
        assertConditionEventually(() -> {
            int rowsCnt = grid(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);
    }
    assertPartitionCountersAreConsistent(keys, grids(baseCnt, i -> true));
}
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) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) IgniteEx(org.apache.ignite.internal.IgniteEx) Collection(java.util.Collection) GridNearTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest) List(java.util.List) ArrayList(java.util.ArrayList)

Example 8 with REPEATABLE_READ

use of org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ in project ignite by apache.

the class CacheMvccTxRecoveryTest method testCountersNeighborcastServerFailed.

/**
 * @throws Exception if failed.
 */
@Test
public void testCountersNeighborcastServerFailed() throws Exception {
    // Reopen https://issues.apache.org/jira/browse/IGNITE-10766 if starts failing
    int srvCnt = 4;
    startGridsMultiThreaded(srvCnt);
    client = true;
    IgniteEx ign = startGrid(srvCnt);
    IgniteCache<Object, Object> cache = ign.getOrCreateCache(basicCcfg().setBackups(2));
    ArrayList<Integer> keys = new ArrayList<>();
    int vid = 3;
    IgniteEx victim = grid(vid);
    Affinity<Object> aff = ign.affinity(DEFAULT_CACHE_NAME);
    for (int i = 0; i < 100; i++) {
        if (aff.isPrimary(victim.localNode(), i) && !aff.isBackup(grid(0).localNode(), i)) {
            keys.add(i);
            break;
        }
    }
    for (int i = 0; i < 100; i++) {
        if (aff.isPrimary(victim.localNode(), i) && !aff.isBackup(grid(1).localNode(), i)) {
            keys.add(i);
            break;
        }
    }
    assert keys.size() == 2 && !keys.contains(99);
    // prevent prepare on one backup
    ((TestRecordingCommunicationSpi) victim.configuration().getCommunicationSpi()).blockMessages(GridDhtTxPrepareRequest.class, grid(0).name());
    GridNearTxLocal nearTx = ((TransactionProxyImpl) ign.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, srvCnt).mapToObj(this::grid).filter(g -> g != victim).map(g -> txsOnNode(g, nearTx.xidVersion())).flatMap(Collection::stream).collect(Collectors.toList());
    nearTx.commitAsync();
    // await tx partially prepared
    assertConditionEventually(() -> txs.stream().anyMatch(tx -> tx.state() == PREPARED));
    CountDownLatch latch1 = new CountDownLatch(1);
    CountDownLatch latch2 = new CountDownLatch(1);
    IgniteInternalFuture<Object> backgroundTxFut = GridTestUtils.runAsync(() -> {
        try (Transaction ignored = ign.transactions().txStart()) {
            boolean upd = false;
            for (int i = 100; i < 200; i++) {
                if (!aff.isPrimary(victim.localNode(), i)) {
                    cache.put(i, 11);
                    upd = true;
                    break;
                }
            }
            assert upd;
            latch1.countDown();
            latch2.await(getTestTimeout(), TimeUnit.MILLISECONDS);
        }
        return null;
    });
    latch1.await(getTestTimeout(), TimeUnit.MILLISECONDS);
    // drop primary
    victim.close();
    // do all assertions before rebalance
    assertConditionEventually(() -> txs.stream().allMatch(tx -> tx.state() == ROLLED_BACK));
    List<IgniteEx> liveNodes = grids(srvCnt, i -> i != vid);
    assertPartitionCountersAreConsistent(keys, liveNodes);
    latch2.countDown();
    backgroundTxFut.get(getTestTimeout());
    assertTrue(liveNodes.stream().map(node -> node.cache(DEFAULT_CACHE_NAME).query(new SqlFieldsQuery("select * from Integer")).getAll()).allMatch(Collection::isEmpty));
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) Transaction(org.apache.ignite.transactions.Transaction) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) IgniteEx(org.apache.ignite.internal.IgniteEx) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl) Test(org.junit.Test)

Example 9 with REPEATABLE_READ

use of org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ in project ignite by apache.

the class CacheMvccBasicContinuousQueryTest method checkUpdateCountersGapIsProcessedSimple.

/**
 * @throws Exception if failed.
 */
private void checkUpdateCountersGapIsProcessedSimple(CacheMode cacheMode) throws Exception {
    testSpi = true;
    final int srvCnt = 4;
    final int backups = srvCnt - 1;
    startGridsMultiThreaded(srvCnt);
    client = true;
    IgniteEx nearNode = startGrid(srvCnt);
    IgniteCache<Object, Object> cache = nearNode.createCache(cacheConfiguration(cacheMode, FULL_SYNC, backups, srvCnt).setIndexedTypes(Integer.class, Integer.class));
    IgniteEx primary = grid(0);
    List<Integer> keys = primaryKeys(primary.cache(DEFAULT_CACHE_NAME), 3);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    List<CacheEntryEvent> arrivedEvts = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(2);
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent e : evts) {
                arrivedEvts.add(e);
                latch.countDown();
            }
        }
    });
    QueryCursor<Cache.Entry<Integer, Integer>> cur = nearNode.cache(DEFAULT_CACHE_NAME).query(qry);
    // Initial value.
    cache.query(new SqlFieldsQuery("insert into Integer(_key, _val) values(?, 42)").setArgs(keys.get(0))).getAll();
    // prevent first transaction prepare on backups
    TestRecordingCommunicationSpi spi = TestRecordingCommunicationSpi.spi(primary);
    final AtomicInteger dhtPrepMsgLimiter = new AtomicInteger();
    spi.blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

        @Override
        public boolean apply(ClusterNode node, Message msg) {
            if (msg instanceof GridDhtTxPrepareRequest)
                return dhtPrepMsgLimiter.getAndIncrement() < backups;
            if (msg instanceof GridContinuousMessage)
                return true;
            return false;
        }
    });
    // First tx. Expect it will be prepared only on the primary node and GridDhtTxPrepareRequests to remotes
    // will be swallowed.
    Transaction txA = nearNode.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
    cache.query(new SqlFieldsQuery("insert into Integer(_key, _val) values(?, 42)").setArgs(keys.get(1))).getAll();
    txA.commitAsync();
    // Wait until first tx changes it's status to PREPARING.
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            boolean preparing = nearNode.context().cache().context().tm().activeTransactions().stream().allMatch(tx -> tx.state() == PREPARING);
            boolean allPrepsSwallowed = dhtPrepMsgLimiter.get() == backups;
            return preparing && allPrepsSwallowed;
        }
    }, 3_000);
    // Second tx.
    GridTestUtils.runAsync(() -> {
        try (Transaction txB = nearNode.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
            cache.query(new SqlFieldsQuery("insert into Integer(_key, _val) values(?, 42)").setArgs(keys.get(2)));
            txB.commit();
        }
    }).get();
    long primaryUpdCntr = getUpdateCounter(primary, keys.get(0));
    // There were three updates: init, first and second.
    assertEquals(3, primaryUpdCntr);
    // drop primary
    stopGrid(primary.name());
    // Wait all txs are rolled back.
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            boolean allRolledBack = true;
            for (int i = 1; i < srvCnt; i++) {
                boolean rolledBack = grid(i).context().cache().context().tm().activeTransactions().stream().allMatch(tx -> tx.state() == ROLLED_BACK);
                allRolledBack &= rolledBack;
            }
            return allRolledBack;
        }
    }, 3_000);
    for (int i = 1; i < srvCnt; i++) {
        IgniteCache backupCache = grid(i).cache(DEFAULT_CACHE_NAME);
        int size = backupCache.query(new SqlFieldsQuery("select * from Integer")).getAll().size();
        long backupCntr = getUpdateCounter(grid(i), keys.get(0));
        assertEquals(2, size);
        assertEquals(primaryUpdCntr, backupCntr);
    }
    assertTrue(latch.await(3, SECONDS));
    assertEquals(2, arrivedEvts.size());
    assertEquals(keys.get(0), arrivedEvts.get(0).getKey());
    assertEquals(keys.get(2), arrivedEvts.get(1).getKey());
    cur.close();
    nearNode.close();
}
Also used : GridDhtTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareRequest) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Affinity(org.apache.ignite.cache.affinity.Affinity) CacheContinuousQueryManager(org.apache.ignite.internal.processors.cache.query.continuous.CacheContinuousQueryManager) Transaction(org.apache.ignite.transactions.Transaction) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) IgniteEx(org.apache.ignite.internal.IgniteEx) REPEATABLE_READ(org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClusterNode(org.apache.ignite.cluster.ClusterNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) Cache(javax.cache.Cache) CacheException(javax.cache.CacheException) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) G(org.apache.ignite.internal.util.typedef.G) Collection(java.util.Collection) ROLLED_BACK(org.apache.ignite.transactions.TransactionState.ROLLED_BACK) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) PREPARING(org.apache.ignite.transactions.TransactionState.PREPARING) GridContinuousMessage(org.apache.ignite.internal.processors.continuous.GridContinuousMessage) FULL_SYNC(org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC) IgniteCache(org.apache.ignite.IgniteCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) CountDownLatch(java.util.concurrent.CountDownLatch) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) List(java.util.List) CacheEntryEvent(javax.cache.event.CacheEntryEvent) QueryCursor(org.apache.ignite.cache.query.QueryCursor) TX_SIZE_THRESHOLD(org.apache.ignite.internal.processors.cache.mvcc.MvccCachingManager.TX_SIZE_THRESHOLD) PA(org.apache.ignite.internal.util.typedef.PA) PESSIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC) Message(org.apache.ignite.plugin.extensions.communication.Message) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) GridContinuousProcessor(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor) SECONDS(java.util.concurrent.TimeUnit.SECONDS) CacheMode(org.apache.ignite.cache.CacheMode) GridContinuousMessage(org.apache.ignite.internal.processors.continuous.GridContinuousMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) ArrayList(java.util.ArrayList) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) GridDhtTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareRequest) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) GridContinuousMessage(org.apache.ignite.internal.processors.continuous.GridContinuousMessage) CountDownLatch(java.util.concurrent.CountDownLatch) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx)

Aggregations

IgniteCache (org.apache.ignite.IgniteCache)9 IgniteEx (org.apache.ignite.internal.IgniteEx)9 GridTestUtils (org.apache.ignite.testframework.GridTestUtils)9 Transaction (org.apache.ignite.transactions.Transaction)9 PESSIMISTIC (org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC)9 REPEATABLE_READ (org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ)9 Test (org.junit.Test)9 List (java.util.List)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Ignite (org.apache.ignite.Ignite)8 ClusterNode (org.apache.ignite.cluster.ClusterNode)8 TestRecordingCommunicationSpi (org.apache.ignite.internal.TestRecordingCommunicationSpi)8 G (org.apache.ignite.internal.util.typedef.G)8 Message (org.apache.ignite.plugin.extensions.communication.Message)8 ArrayList (java.util.ArrayList)7 Collection (java.util.Collection)7 Affinity (org.apache.ignite.cache.affinity.Affinity)7 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)7 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)7