Search in sources :

Example 81 with Transaction

use of org.apache.ignite.transactions.Transaction in project ignite by apache.

the class EvictionAbstractTest method checkPartitionedMultiThreaded.

/**
 * @throws Exception If failed.
 */
protected void checkPartitionedMultiThreaded() throws Exception {
    try {
        startGridsMultiThreaded(gridCnt);
        final Random rand = new Random();
        final AtomicInteger cntr = new AtomicInteger();
        multithreaded(new Callable() {

            @Nullable
            @Override
            public Object call() throws Exception {
                int cnt = 100;
                for (int i = 0; i < cnt && !Thread.currentThread().isInterrupted(); i++) {
                    IgniteEx grid = grid(rand.nextInt(2));
                    IgniteCache<Integer, String> cache = grid.cache(DEFAULT_CACHE_NAME);
                    int key = rand.nextInt(1000);
                    String val = Integer.toString(key);
                    try (Transaction tx = grid.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                        String v = cache.get(key);
                        assert v == null || v.equals(Integer.toString(key)) : "Invalid value for key [key=" + key + ", val=" + v + ']';
                        cache.put(key, val);
                        tx.commit();
                    }
                    if (cntr.incrementAndGet() % 100 == 0)
                        info("Stored cache object for key [key=" + key + ", idx=" + i + ']');
                }
                return null;
            }
        }, 10);
    } finally {
        stopAllGrids();
    }
}
Also used : Random(java.util.Random) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx) IgniteCache(org.apache.ignite.IgniteCache) Callable(java.util.concurrent.Callable) Nullable(org.jetbrains.annotations.Nullable) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 82 with Transaction

use of org.apache.ignite.transactions.Transaction in project ignite by apache.

the class GridCacheEvictionTouchSelfTest method testPolicyConsistency.

/**
 * @throws Exception If failed.
 */
public void testPolicyConsistency() throws Exception {
    FifoEvictionPolicy<Object, Object> plc = new FifoEvictionPolicy<>();
    plc.setMaxSize(500);
    this.plc = plc;
    try {
        Ignite ignite = startGrid(1);
        final IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);
        final Random rnd = new Random();
        try (Transaction tx = ignite.transactions().txStart()) {
            int iterCnt = 20;
            int keyCnt = 5000;
            for (int i = 0; i < iterCnt; i++) {
                int j = rnd.nextInt(keyCnt);
                // Put or remove?
                if (rnd.nextBoolean())
                    cache.put(j, j);
                else
                    cache.remove(j);
                if (i != 0 && i % 1000 == 0)
                    info("Stats [iterCnt=" + i + ", size=" + cache.size() + ']');
            }
            FifoEvictionPolicy<Integer, Integer> plc0 = (FifoEvictionPolicy<Integer, Integer>) this.plc;
            if (!plc0.queue().isEmpty()) {
                for (Cache.Entry<Integer, Integer> e : plc0.queue()) U.warn(log, "Policy queue item: " + e);
                fail("Test failed, see logs for details.");
            }
            tx.commit();
        }
    } catch (Throwable t) {
        error("Test failed.", t);
        fail("Test failed, see logs for details.");
    } finally {
        stopAllGrids();
    }
}
Also used : Random(java.util.Random) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) FifoEvictionPolicy(org.apache.ignite.cache.eviction.fifo.FifoEvictionPolicy) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 83 with Transaction

use of org.apache.ignite.transactions.Transaction in project ignite by apache.

the class WithKeepBinaryCacheFullApiTest method runInAllTxModes.

/**
 * @param task Task.
 * @throws Exception If failed.
 */
protected void runInAllTxModes(TestRunnable task) throws Exception {
    info("Executing implicite tx");
    task.run();
    if (txShouldBeUsed()) {
        for (TransactionConcurrency conc : TransactionConcurrency.values()) {
            for (TransactionIsolation isolation : TransactionIsolation.values()) {
                try (Transaction tx = testedGrid().transactions().txStart(conc, isolation)) {
                    info("Executing explicite tx [isolation" + isolation + ", concurrency=" + conc + "]");
                    task.run();
                    tx.commit();
                }
            }
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation)

Example 84 with Transaction

use of org.apache.ignite.transactions.Transaction in project ignite by apache.

the class IgniteCacheNoWriteThroughAbstractTest method testNoWriteThrough.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("UnnecessaryLocalVariable")
public void testNoWriteThrough() throws Exception {
    IgniteCache<Integer, Integer> cache = jcache(0);
    for (Integer key : keys()) {
        log.info("Test [key=" + key + ']');
        final Integer storeVal = key;
        storeMap.put(key, storeVal);
        assertEquals(key, cache.get(key));
        cache.remove(key);
        assertEquals(storeVal, storeMap.get(key));
        storeMap.remove(key);
        assertNull(cache.get(key));
        assertTrue(cache.putIfAbsent(key, key));
        assertNull(storeMap.get(key));
        assertEquals(key, cache.get(key));
        cache.remove(key);
        storeMap.put(key, storeVal);
        Integer val = key + 1;
        assertFalse(cache.putIfAbsent(key, val));
        assertEquals(storeVal, storeMap.get(key));
        cache.put(key, val);
        assertEquals(val, cache.get(key));
        assertEquals(storeVal, storeMap.get(key));
        val = val + 1;
        assertTrue(cache.replace(key, val));
        assertEquals(val, cache.get(key));
        assertEquals(storeVal, storeMap.get(key));
        cache.remove(key);
        assertEquals(storeVal, storeMap.get(key));
        storeMap.remove(key);
        assertNull(cache.get(key));
        storeMap.put(key, storeVal);
        val = val + 1;
        assertEquals(storeVal, cache.getAndPut(key, val));
        assertEquals(storeVal, storeMap.get(key));
        assertEquals(val, cache.get(key));
        cache.remove(key);
        assertEquals(storeVal, storeMap.get(key));
        assertEquals(storeVal, cache.getAndRemove(key));
        cache.remove(key);
        assertEquals(storeVal, storeMap.get(key));
        Object ret = cache.invoke(key, new EntryProcessor<Integer, Integer, Object>() {

            @Override
            public Object process(MutableEntry<Integer, Integer> entry, Object... args) {
                Integer val = entry.getValue();
                entry.setValue(val + 1);
                return String.valueOf(val);
            }
        });
        assertEquals(String.valueOf(storeVal), ret);
        assertEquals(storeVal + 1, (int) cache.get(key));
        assertEquals(storeVal, storeMap.get(key));
        assertTrue(cache.replace(key, storeVal + 1, storeVal + 2));
        assertEquals(storeVal, storeMap.get(key));
        assertEquals(storeVal + 2, (int) cache.get(key));
    }
    Map<Integer, Integer> expData = new HashMap<>();
    for (int i = 1000_0000; i < 1000_0000 + 1000; i++) {
        storeMap.put(i, i);
        expData.put(i, i);
    }
    assertEquals(expData, cache.getAll(expData.keySet()));
    storeMap.clear();
    cache.putAll(expData);
    assertTrue(storeMap.isEmpty());
    assertEquals(expData, cache.getAll(expData.keySet()));
    Map<Integer, Integer> expData0 = new HashMap<>();
    for (int i = 1000_0000; i < 1000_0000 + 1000; i++) expData0.put(i, 1);
    cache.invokeAll(expData.keySet(), new EntryProcessor<Integer, Integer, Object>() {

        @Override
        public Object process(MutableEntry<Integer, Integer> entry, Object... args) {
            entry.setValue(1);
            return null;
        }
    });
    assertEquals(expData0, cache.getAll(expData0.keySet()));
    assertTrue(storeMap.isEmpty());
    storeMap.putAll(expData);
    cache.removeAll(expData.keySet());
    assertEquals(1000, storeMap.size());
    storeMap.clear();
    assertTrue(cache.getAll(expData.keySet()).isEmpty());
    if (atomicityMode() == TRANSACTIONAL) {
        for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
            for (TransactionIsolation isolation : TransactionIsolation.values()) {
                for (Integer key : keys()) {
                    log.info("Test tx [key=" + key + ", concurrency=" + concurrency + ", isolation=" + isolation + ']');
                    storeMap.put(key, key);
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        assertEquals("For concurrency=" + concurrency + ", isolation=" + isolation + ']', key, cache.getAndPut(key, -1));
                        tx.commit();
                    }
                    assertEquals(-1, (int) cache.get(key));
                    assertEquals(key, storeMap.get(key));
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        cache.put(key, -2);
                        tx.commit();
                    }
                    assertEquals(-2, (int) cache.get(key));
                    assertEquals(key, storeMap.get(key));
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        assertEquals(-2, (int) cache.getAndRemove(key));
                        tx.commit();
                    }
                    assertEquals(key, storeMap.get(key));
                    storeMap.remove(key);
                    assertNull(cache.get(key));
                    storeMap.put(key, key);
                    cache.put(key, key);
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        assertTrue(cache.replace(key, -1));
                        tx.commit();
                    }
                    assertEquals(-1, (int) cache.get(key));
                    assertEquals(key, storeMap.get(key));
                    cache.remove(key);
                    storeMap.clear();
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        cache.putAll(expData);
                        tx.commit();
                    }
                    assertTrue(storeMap.isEmpty());
                    assertEquals(expData, cache.getAll(expData.keySet()));
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        cache.invokeAll(expData.keySet(), new EntryProcessor<Integer, Integer, Object>() {

                            @Override
                            public Object process(MutableEntry<Integer, Integer> entry, Object... args) {
                                entry.setValue(1);
                                return null;
                            }
                        });
                        tx.commit();
                    }
                    assertEquals(expData0, cache.getAll(expData.keySet()));
                    assertTrue(storeMap.isEmpty());
                    storeMap.putAll(expData);
                    try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
                        cache.removeAll(expData.keySet());
                        tx.commit();
                    }
                    assertEquals(1000, storeMap.size());
                    storeMap.clear();
                    assertTrue(cache.getAll(expData.keySet()).isEmpty());
                }
            }
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) HashMap(java.util.HashMap) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation)

Example 85 with Transaction

use of org.apache.ignite.transactions.Transaction in project ignite by apache.

the class CacheEntryProcessorNonSerializableTest method doTestInvokeTest.

/**
 * @param ccfg Cache configuration.
 * @throws Exception If failed.
 */
private void doTestInvokeTest(CacheConfiguration ccfg, TransactionConcurrency txConcurrency, TransactionIsolation txIsolation) throws Exception {
    IgniteEx cln = grid(getServerNodeCount());
    grid(0).createCache(ccfg);
    IgniteCache clnCache;
    if (ccfg.getNearConfiguration() != null)
        clnCache = cln.createNearCache(ccfg.getName(), ccfg.getNearConfiguration());
    else
        clnCache = cln.cache(ccfg.getName());
    putKeys(clnCache, EXPECTED_VALUE);
    try {
        // Explicit tx.
        for (int i = 0; i < ITERATION_CNT; i++) {
            try (final Transaction tx = cln.transactions().txStart(txConcurrency, txIsolation)) {
                putKeys(clnCache, WRONG_VALUE);
                clnCache.invoke(KEYS, new NonSerialazibleEntryProcessor());
                GridTestUtils.assertThrowsWithCause(new Callable<Object>() {

                    @Override
                    public Object call() throws Exception {
                        tx.commit();
                        return null;
                    }
                }, NotSerializableException.class);
            }
            checkKeys(clnCache, EXPECTED_VALUE);
        }
        // From affinity node.
        Ignite grid = grid(ThreadLocalRandom.current().nextInt(NODES));
        final IgniteCache cache = grid.cache(ccfg.getName());
        // Explicit tx.
        for (int i = 0; i < ITERATION_CNT; i++) {
            try (final Transaction tx = grid.transactions().txStart(txConcurrency, txIsolation)) {
                putKeys(cache, WRONG_VALUE);
                cache.invoke(KEYS, new NonSerialazibleEntryProcessor());
                GridTestUtils.assertThrowsWithCause(new Callable<Object>() {

                    @Override
                    public Object call() throws Exception {
                        tx.commit();
                        return null;
                    }
                }, NotSerializableException.class);
            }
            checkKeys(cache, EXPECTED_VALUE);
        }
        final IgniteCache clnCache0 = clnCache;
        // Implicit tx.
        for (int i = 0; i < ITERATION_CNT; i++) {
            GridTestUtils.assertThrowsWithCause(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    clnCache0.invoke(KEYS, new NonSerialazibleEntryProcessor());
                    return null;
                }
            }, NotSerializableException.class);
        }
        checkKeys(clnCache, EXPECTED_VALUE);
    } finally {
        grid(0).destroyCache(ccfg.getName());
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite) EntryProcessorException(javax.cache.processor.EntryProcessorException) NotSerializableException(java.io.NotSerializableException)

Aggregations

Transaction (org.apache.ignite.transactions.Transaction)493 Ignite (org.apache.ignite.Ignite)204 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)183 IgniteCache (org.apache.ignite.IgniteCache)88 IgniteTransactions (org.apache.ignite.IgniteTransactions)78 IgniteException (org.apache.ignite.IgniteException)74 CacheException (javax.cache.CacheException)60 HashMap (java.util.HashMap)54 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)45 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)44 TransactionOptimisticException (org.apache.ignite.transactions.TransactionOptimisticException)42 ArrayList (java.util.ArrayList)41 Callable (java.util.concurrent.Callable)41 Map (java.util.Map)36 IgniteEx (org.apache.ignite.internal.IgniteEx)34 CountDownLatch (java.util.concurrent.CountDownLatch)32 IgniteKernal (org.apache.ignite.internal.IgniteKernal)30 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)30 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)29 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)29