Search in sources :

Example 31 with IgniteTransactions

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

the class IgnitePessimisticTxSuspendResumeTest method testSuspendPessimisticTx.

/**
 * Test for suspension on pessimistic transaction.
 *
 * @throws Exception If failed.
 */
public void testSuspendPessimisticTx() throws Exception {
    try (Ignite g = startGrid()) {
        IgniteCache<Integer, String> cache = jcache();
        IgniteTransactions txs = g.transactions();
        for (TransactionIsolation isolation : TransactionIsolation.values()) {
            final Transaction tx = txs.txStart(TransactionConcurrency.PESSIMISTIC, isolation);
            cache.put(1, "1");
            GridTestUtils.assertThrowsWithCause(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    tx.suspend();
                    return null;
                }
            }, UnsupportedOperationException.class);
            tx.close();
            assertNull(cache.get(1));
        }
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 32 with IgniteTransactions

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

the class IgniteClientReconnectCacheTest method testReconnectTransactions.

/**
 * @throws Exception If failed.
 */
public void testReconnectTransactions() throws Exception {
    clientMode = true;
    IgniteEx client = startGrid(SRV_CNT);
    Ignite srv = clientRouter(client);
    CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setAtomicityMode(TRANSACTIONAL);
    ccfg.setCacheMode(PARTITIONED);
    ccfg.setBackups(1);
    IgniteCache<Object, Object> cache = client.getOrCreateCache(ccfg);
    final IgniteTransactions txs = client.transactions();
    final Transaction tx = txs.txStart(OPTIMISTIC, REPEATABLE_READ);
    cache.put(1, 1);
    reconnectClientNode(client, srv, new Runnable() {

        @Override
        public void run() {
            try {
                tx.commit();
                fail();
            } catch (IgniteClientDisconnectedException e) {
                log.info("Expected error: " + e);
                assertNotNull(e.reconnectFuture());
            }
            try {
                txs.txStart();
                fail();
            } catch (IgniteClientDisconnectedException e) {
                log.info("Expected error: " + e);
                assertNotNull(e.reconnectFuture());
            }
        }
    });
    assertNull(txs.tx());
    try (Transaction tx0 = txs.txStart(OPTIMISTIC, REPEATABLE_READ)) {
        cache.put(1, 1);
        assertEquals(1, cache.get(1));
        tx0.commit();
    }
    try (Transaction tx0 = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
        cache.put(2, 2);
        assertEquals(2, cache.get(2));
        tx0.commit();
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 33 with IgniteTransactions

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

the class IgniteClientReconnectFailoverTest method testReconnectTxCache.

/**
 * @throws Exception If failed.
 */
public void testReconnectTxCache() throws Exception {
    final Ignite client = grid(serverCount());
    final IgniteCache<Integer, Integer> cache = client.cache(TX_CACHE);
    assertNotNull(cache);
    assertEquals(TRANSACTIONAL, cache.getConfiguration(CacheConfiguration.class).getAtomicityMode());
    final IgniteTransactions txs = client.transactions();
    reconnectFailover(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            try {
                TreeMap<Integer, Integer> map = new TreeMap<>();
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < 5; i++) {
                    Integer key = rnd.nextInt(0, 100_000);
                    cache.put(key, key);
                    assertEquals(key, cache.get(key));
                    map.put(key, key);
                }
                for (TransactionConcurrency txConcurrency : TransactionConcurrency.values()) {
                    try (Transaction tx = txs.txStart(txConcurrency, REPEATABLE_READ)) {
                        for (Map.Entry<Integer, Integer> e : map.entrySet()) {
                            cache.put(e.getKey(), e.getValue());
                            assertNotNull(cache.get(e.getKey()));
                        }
                        tx.commit();
                    }
                }
                cache.putAll(map);
                Map<Integer, Integer> res = cache.getAll(map.keySet());
                assertEquals(map, res);
            } catch (IgniteClientDisconnectedException e) {
                throw e;
            } catch (IgniteException e) {
                log.info("Ignore error: " + e);
            } catch (CacheException e) {
                if (e.getCause() instanceof IgniteClientDisconnectedException)
                    throw e;
                else
                    log.info("Ignore error: " + e);
            }
            return null;
        }
    });
}
Also used : CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteTransactions(org.apache.ignite.IgniteTransactions) TreeMap(java.util.TreeMap) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) IgniteException(org.apache.ignite.IgniteException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 34 with IgniteTransactions

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

the class IgniteCachePutRetryAtomicSelfTest method testPutInsideTransaction.

/**
 * @throws Exception If failed.
 */
public void testPutInsideTransaction() throws Exception {
    ignite(0).createCache(cacheConfiguration(false, false));
    CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setName("tx-cache");
    ccfg.setAtomicityMode(TRANSACTIONAL);
    try (IgniteCache<Integer, Integer> txCache = ignite(0).getOrCreateCache(ccfg)) {
        final AtomicBoolean finished = new AtomicBoolean();
        IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                while (!finished.get()) {
                    stopGrid(3);
                    U.sleep(300);
                    startGrid(3);
                }
                return null;
            }
        });
        try {
            IgniteTransactions txs = ignite(0).transactions();
            IgniteCache<Object, Object> cache = ignite(0).cache(DEFAULT_CACHE_NAME);
            long stopTime = System.currentTimeMillis() + 60_000;
            while (System.currentTimeMillis() < stopTime) {
                for (int i = 0; i < 10_000; i++) {
                    try {
                        try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                            txCache.put(0, 0);
                            cache.put(i, i);
                            tx.commit();
                        }
                    } catch (IgniteException | CacheException e) {
                        log.info("Ignore exception: " + e);
                    }
                }
            }
            finished.set(true);
            fut.get();
        } finally {
            finished.set(true);
        }
    }
}
Also used : CacheException(javax.cache.CacheException) IgniteTransactions(org.apache.ignite.IgniteTransactions) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) IgniteException(org.apache.ignite.IgniteException) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 35 with IgniteTransactions

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

the class GridCachePartitionedEvictionSelfTest method doTestEviction.

/**
 * @throws Exception If failed.
 * @param concurrency Tx concurrency.
 * @param isolation Tx isolation.
 */
private void doTestEviction(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
    assert concurrency != null;
    assert isolation != null;
    // This condition should be "true", otherwise the test doesn't make sense.
    assert KEY_CNT >= EVICT_CACHE_SIZE;
    GridDhtCacheAdapter<String, Integer> dht0 = dht(jcache(0));
    GridDhtCacheAdapter<String, Integer> dht1 = dht(jcache(1));
    Affinity<String> aff = dht0.affinity();
    TouchedExpiryPolicy plc = new TouchedExpiryPolicy(new Duration(MILLISECONDS, 10));
    for (int kv = 0; kv < KEY_CNT; kv++) {
        String key = String.valueOf(kv);
        ClusterNode node = aff.mapKeyToNode(key);
        IgniteCache<String, Integer> c = cache(node);
        IgniteTransactions txs = G.ignite(node.id()).transactions();
        try (Transaction tx = txs.txStart(concurrency, isolation)) {
            assert c.get(key) == null;
            c.withExpiryPolicy(plc).put(key, 1);
            assertEquals(Integer.valueOf(kv), c.get(key));
            tx.commit();
        }
    }
    if (TEST_INFO) {
        info("Printing keys in dht0...");
        for (String key : dht0.keySet()) info("[key=" + key + ", primary=" + F.eqNodes(grid(0).localNode(), aff.mapKeyToNode(key)) + ']');
        info("Printing keys in dht1...");
        for (String key : dht1.keySet()) info("[key=" + key + ", primary=" + F.eqNodes(grid(1).localNode(), aff.mapKeyToNode(key)) + ']');
    }
    assertEquals(EVICT_CACHE_SIZE, dht0.size());
    assertEquals(EVICT_CACHE_SIZE, dht1.size());
    assertEquals(0, near(jcache(0)).nearSize());
    assertEquals(0, near(jcache(1)).nearSize());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) Transaction(org.apache.ignite.transactions.Transaction) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) 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