Search in sources :

Example 96 with IgniteTransactions

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

the class IgniteCacheThreadLocalTxTest method checkNoTx.

/**
 * @param node Node.
 */
private void checkNoTx(Ignite node) {
    IgniteTransactions txs = node.transactions();
    assertNull(txs.tx());
    assertNull(((IgniteKernal) node).context().cache().context().tm().tx());
}
Also used : IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 97 with IgniteTransactions

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

the class IgniteCacheConfigVariationsFullApiTest method checkSkipStoreWithTransaction.

/**
 * @param cache Cache instance.
 * @param cacheSkipStore Cache skip store projection.
 * @param data Data set.
 * @param keys Keys list.
 * @param txConcurrency Concurrency mode.
 * @param txIsolation Isolation mode.
 * @throws Exception If failed.
 */
private void checkSkipStoreWithTransaction(IgniteCache<String, Integer> cache, IgniteCache<String, Integer> cacheSkipStore, Map<String, Integer> data, List<String> keys, TransactionConcurrency txConcurrency, TransactionIsolation txIsolation) throws Exception {
    info("Test tx skip store [concurrency=" + txConcurrency + ", isolation=" + txIsolation + ']');
    cache.removeAll(data.keySet());
    checkEmpty(cache, cacheSkipStore);
    IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
    Integer val = -1;
    // Several put check.
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        for (String key : keys) cacheSkipStore.put(key, val);
        for (String key : keys) {
            assertEquals(val, cacheSkipStore.get(key));
            assertEquals(val, cache.get(key));
            assertFalse(storeStgy.isInStore(key));
        }
        tx.commit();
    }
    for (String key : keys) {
        assertEquals(val, cacheSkipStore.get(key));
        assertEquals(val, cache.get(key));
        assertFalse(storeStgy.isInStore(key));
    }
    assertEquals(0, storeStgy.getStoreSize());
    // cacheSkipStore putAll(..)/removeAll(..) check.
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        cacheSkipStore.putAll(data);
        tx.commit();
    }
    for (String key : keys) {
        val = data.get(key);
        assertEquals(val, cacheSkipStore.get(key));
        assertEquals(val, cache.get(key));
        assertFalse(storeStgy.isInStore(key));
    }
    storeStgy.putAllToStore(data);
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        cacheSkipStore.removeAll(data.keySet());
        tx.commit();
    }
    for (String key : keys) {
        assertNull(cacheSkipStore.get(key));
        assertNotNull(cache.get(key));
        assertTrue(storeStgy.isInStore(key));
        cache.remove(key);
    }
    assertTrue(storeStgy.getStoreSize() == 0);
    // cache putAll(..)/removeAll(..) check.
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        cache.putAll(data);
        for (String key : keys) {
            assertNotNull(cacheSkipStore.get(key));
            assertNotNull(cache.get(key));
            assertFalse(storeStgy.isInStore(key));
        }
        cache.removeAll(data.keySet());
        for (String key : keys) {
            assertNull(cacheSkipStore.get(key));
            assertNull(cache.get(key));
            assertFalse(storeStgy.isInStore(key));
        }
        tx.commit();
    }
    assertTrue(storeStgy.getStoreSize() == 0);
    // putAll(..) from both cacheSkipStore and cache.
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        Map<String, Integer> subMap = new HashMap<>();
        for (int i = 0; i < keys.size() / 2; i++) subMap.put(keys.get(i), i);
        cacheSkipStore.putAll(subMap);
        subMap.clear();
        for (int i = keys.size() / 2; i < keys.size(); i++) subMap.put(keys.get(i), i);
        cache.putAll(subMap);
        for (String key : keys) {
            assertNotNull(cacheSkipStore.get(key));
            assertNotNull(cache.get(key));
            assertFalse(storeStgy.isInStore(key));
        }
        tx.commit();
    }
    for (int i = 0; i < keys.size() / 2; i++) {
        String key = keys.get(i);
        assertNotNull(cacheSkipStore.get(key));
        assertNotNull(cache.get(key));
        assertFalse(storeStgy.isInStore(key));
    }
    for (int i = keys.size() / 2; i < keys.size(); i++) {
        String key = keys.get(i);
        assertNotNull(cacheSkipStore.get(key));
        assertNotNull(cache.get(key));
        assertTrue(storeStgy.isInStore(key));
    }
    cache.removeAll(data.keySet());
    for (String key : keys) {
        assertNull(cacheSkipStore.get(key));
        assertNull(cache.get(key));
        assertFalse(storeStgy.isInStore(key));
    }
    // Check that read-through is disabled when cacheSkipStore is used.
    for (int i = 0; i < keys.size(); i++) putToStore(keys.get(i), i);
    assertTrue(cacheSkipStore.size(ALL) == 0);
    assertTrue(cache.size(ALL) == 0);
    assertTrue(storeStgy.getStoreSize() != 0);
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        assertTrue(cacheSkipStore.getAll(data.keySet()).isEmpty());
        for (String key : keys) {
            assertNull(cacheSkipStore.get(key));
            if (txIsolation == READ_COMMITTED) {
                assertNotNull(cache.get(key));
                assertNotNull(cacheSkipStore.get(key));
            }
        }
        tx.commit();
    }
    cache.removeAll(data.keySet());
    val = -1;
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        for (String key : data.keySet()) {
            storeStgy.putToStore(key, 0);
            assertNull(cacheSkipStore.invoke(key, new SetValueProcessor(val)));
        }
        tx.commit();
    }
    for (String key : data.keySet()) {
        assertEquals(0, storeStgy.getFromStore(key));
        assertEquals(val, cacheSkipStore.get(key));
        assertEquals(val, cache.get(key));
    }
    cache.removeAll(data.keySet());
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        for (String key : data.keySet()) {
            storeStgy.putToStore(key, 0);
            assertTrue(cacheSkipStore.putIfAbsent(key, val));
        }
        tx.commit();
    }
    for (String key : data.keySet()) {
        assertEquals(0, storeStgy.getFromStore(key));
        assertEquals(val, cacheSkipStore.get(key));
        assertEquals(val, cache.get(key));
    }
    cache.removeAll(data.keySet());
    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
        for (String key : data.keySet()) {
            storeStgy.putToStore(key, 0);
            assertNull(cacheSkipStore.getAndPut(key, val));
        }
        tx.commit();
    }
    for (String key : data.keySet()) {
        assertEquals(0, storeStgy.getFromStore(key));
        assertEquals(val, cacheSkipStore.get(key));
        assertEquals(val, cache.get(key));
    }
    cache.removeAll(data.keySet());
    checkEmpty(cache, cacheSkipStore);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 98 with IgniteTransactions

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

the class IgniteTxPreloadAbstractTest method testLocalTxPreloading.

/**
 * Tries to execute transaction doing transform when target key is not yet preloaded.
 *
 * @param txConcurrency Transaction concurrency;
 * @throws Exception If failed.
 */
private void testLocalTxPreloading(TransactionConcurrency txConcurrency) throws Exception {
    Map<String, Integer> map = new HashMap<>();
    for (int i = 0; i < 10000; i++) map.put(String.valueOf(i), 0);
    IgniteCache<String, Integer> cache0 = jcache(0);
    cache0.putAll(map);
    final String TX_KEY = "9000";
    int expVal = 0;
    for (int i = 1; i < GRID_CNT; i++) {
        assertEquals((Integer) expVal, cache0.get(TX_KEY));
        startGrid(i);
        IgniteCache<String, Integer> cache = jcache(i);
        IgniteTransactions txs = ignite(i).transactions();
        try (Transaction tx = txs.txStart(txConcurrency, TransactionIsolation.REPEATABLE_READ)) {
            cache.invoke(TX_KEY, new EntryProcessor<String, Integer, Void>() {

                @Override
                public Void process(MutableEntry<String, Integer> e, Object... args) {
                    Integer val = e.getValue();
                    if (val == null) {
                        keyNotLoaded = true;
                        e.setValue(1);
                        return null;
                    }
                    e.setValue(val + 1);
                    return null;
                }
            });
            tx.commit();
        }
        assertFalse(keyNotLoaded);
        expVal++;
        assertEquals((Integer) expVal, cache.get(TX_KEY));
    }
    for (int i = 0; i < GRID_CNT; i++) assertEquals("Unexpected value for cache " + i, (Integer) expVal, jcache(i).get(TX_KEY));
}
Also used : HashMap(java.util.HashMap) IgniteTransactions(org.apache.ignite.IgniteTransactions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction)

Example 99 with IgniteTransactions

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

the class IgniteCacheExpiryPolicyWithStoreAbstractTest method getReadThrough.

/**
 * @throws Exception If failed.
 */
protected void getReadThrough(boolean withExcPlc, TransactionConcurrency txConcurrency, TransactionIsolation txIsolation) throws Exception {
    IgniteCache<Integer, Integer> cache = jcache(0);
    if (withExcPlc)
        cache = cache.withExpiryPolicy(new ExpiryPolicy() {

            @Override
            public Duration getExpiryForCreation() {
                return new Duration(TimeUnit.MILLISECONDS, 501);
            }

            @Override
            public Duration getExpiryForAccess() {
                return new Duration(TimeUnit.MILLISECONDS, 601);
            }

            @Override
            public Duration getExpiryForUpdate() {
                return new Duration(TimeUnit.MILLISECONDS, 701);
            }
        });
    Integer prim = primaryKeys(cache, 1, 1000).get(0);
    Integer back = backupKeys(cache, 1, 1000).get(0);
    Integer near = nearKeys(cache, 1, 1000).get(0);
    Set<Integer> prims = new HashSet<>(primaryKeys(cache, 10, prim + 1));
    Set<Integer> backs = new HashSet<>(backupKeys(cache, 10, back + 1));
    Set<Integer> nears = new HashSet<>(nearKeys(cache, 10, near + 1));
    Set<Integer> keys = new HashSet<>();
    keys.add(prim);
    keys.add(back);
    keys.add(near);
    keys.addAll(prims);
    keys.addAll(backs);
    keys.addAll(nears);
    for (Integer key : keys) storeMap.put(key, key);
    IgniteTransactions transactions = grid(0).transactions();
    Transaction tx = txConcurrency != null ? transactions.txStart(txConcurrency, txIsolation) : null;
    try {
        Collection<Integer> singleKeys = new HashSet<>();
        singleKeys.add(prim);
        singleKeys.add(back);
        singleKeys.add(near);
        assertEquals(3, singleKeys.size());
        for (Integer key : singleKeys) assertEquals(key, cache.get(key));
        Map<Integer, Integer> res = new HashMap<>();
        res.putAll(cache.getAll(prims));
        res.putAll(cache.getAll(backs));
        res.putAll(cache.getAll(nears));
        assertEquals(30, res.size());
        for (Map.Entry<Integer, Integer> e : res.entrySet()) assertEquals(e.getKey(), e.getValue());
    } finally {
        if (tx != null)
            tx.rollback();
    }
    for (Integer key : keys) checkTtl(key, withExcPlc ? 501 : 500, true);
    U.sleep(600);
    for (Integer key : keys) checkExpired(key);
}
Also used : HashMap(java.util.HashMap) Duration(javax.cache.expiry.Duration) IgniteTransactions(org.apache.ignite.IgniteTransactions) Transaction(org.apache.ignite.transactions.Transaction) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 100 with IgniteTransactions

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

the class TxWithKeyContentionSelfTest method runKeyCollisionsMetric.

/**
 * Tests metric correct results while tx collisions occured.
 *
 * @param concurrency Concurrency level.
 * @param isolation Isolation level.
 * @throws Exception If failed.
 */
private void runKeyCollisionsMetric(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
    if (MvccFeatureChecker.forcedMvcc())
        // Not supported.
        return;
    Ignite ig = startGridsMultiThreaded(3);
    int contCnt = (int) U.staticField(IgniteTxManager.class, "COLLISIONS_QUEUE_THRESHOLD") * 5;
    CountDownLatch txLatch = new CountDownLatch(contCnt);
    ig.cluster().active(true);
    client = true;
    Ignite cl = startGrid();
    IgniteTransactions txMgr = cl.transactions();
    IgniteCache<Integer, Integer> cache = ig.cache(DEFAULT_CACHE_NAME);
    IgniteCache<Integer, Integer> cache0 = cl.cache(DEFAULT_CACHE_NAME);
    final Integer keyId = primaryKey(cache);
    CountDownLatch blockOnce = new CountDownLatch(1);
    for (Ignite ig0 : G.allGrids()) {
        if (ig0.configuration().isClientMode())
            continue;
        TestRecordingCommunicationSpi commSpi0 = (TestRecordingCommunicationSpi) ig0.configuration().getCommunicationSpi();
        commSpi0.blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

            @Override
            public boolean apply(ClusterNode node, Message msg) {
                if (msg instanceof GridNearTxFinishResponse && blockOnce.getCount() > 0) {
                    blockOnce.countDown();
                    return true;
                }
                return false;
            }
        });
    }
    IgniteInternalFuture f = GridTestUtils.runAsync(() -> {
        try (Transaction tx = txMgr.txStart(concurrency, isolation)) {
            cache0.put(keyId, 0);
            tx.commit();
        }
    });
    blockOnce.await();
    GridCompoundFuture<?, ?> finishFut = new GridCompoundFuture<>();
    for (int i = 0; i < contCnt; ++i) {
        IgniteInternalFuture f0 = GridTestUtils.runAsync(() -> {
            try (Transaction tx = txMgr.txStart(concurrency, isolation)) {
                cache0.put(keyId, 0);
                tx.commit();
                txLatch.countDown();
            }
        });
        finishFut.add(f0);
    }
    finishFut.markInitialized();
    for (Ignite ig0 : G.allGrids()) {
        TestRecordingCommunicationSpi commSpi0 = (TestRecordingCommunicationSpi) ig0.configuration().getCommunicationSpi();
        if (ig0.configuration().isClientMode())
            continue;
        commSpi0.stopBlock();
    }
    IgniteTxManager txManager = ((IgniteEx) ig).context().cache().context().tm();
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            try {
                U.invoke(IgniteTxManager.class, txManager, "collectTxCollisionsInfo");
            } catch (IgniteCheckedException e) {
                fail(e.toString());
            }
            CacheMetrics metrics = ig.cache(DEFAULT_CACHE_NAME).localMetrics();
            String coll1 = metrics.getTxKeyCollisions();
            if (!coll1.isEmpty()) {
                String coll2 = metrics.getTxKeyCollisions();
                // check idempotent
                assertEquals(coll1, coll2);
                assertTrue(coll1.contains("queueSize"));
                return true;
            } else
                return false;
        }
    }, 10_000));
    f.get();
    finishFut.get();
    txLatch.await();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) CacheMetrics(org.apache.ignite.cache.CacheMetrics) Message(org.apache.ignite.plugin.extensions.communication.Message) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteTransactions(org.apache.ignite.IgniteTransactions) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) GridNearTxFinishResponse(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxFinishResponse) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite)

Aggregations

IgniteTransactions (org.apache.ignite.IgniteTransactions)120 Transaction (org.apache.ignite.transactions.Transaction)116 Ignite (org.apache.ignite.Ignite)87 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)68 Test (org.junit.Test)49 IgniteCache (org.apache.ignite.IgniteCache)36 TransactionOptimisticException (org.apache.ignite.transactions.TransactionOptimisticException)29 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)28 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)28 HashMap (java.util.HashMap)26 CacheException (javax.cache.CacheException)26 ArrayList (java.util.ArrayList)23 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)23 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)19 Map (java.util.Map)18 IgniteException (org.apache.ignite.IgniteException)18 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)18 CountDownLatch (java.util.concurrent.CountDownLatch)17 HashSet (java.util.HashSet)16 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)16