Search in sources :

Example 26 with Transaction

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

the class GridCacheFinishPartitionsSelfTest method testMvccFinishKeys.

/**
 * Tests finish future for particular set of keys.
 *
 * @throws Exception If failed.
 */
public void testMvccFinishKeys() throws Exception {
    IgniteCache<String, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    try (Transaction tx = grid(0).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        final String key = "key";
        cache.get(key);
        GridCacheAdapter<String, Integer> internal = grid.internalCache(DEFAULT_CACHE_NAME);
        KeyCacheObject cacheKey = internal.context().toCacheKeyObject(key);
        IgniteInternalFuture<?> nearFut = internal.context().mvcc().finishKeys(Collections.singletonList(cacheKey), internal.context().cacheId(), new AffinityTopologyVersion(2));
        IgniteInternalFuture<?> dhtFut = internal.context().near().dht().context().mvcc().finishKeys(Collections.singletonList(cacheKey), internal.context().cacheId(), new AffinityTopologyVersion(2));
        assert !nearFut.isDone();
        assert !dhtFut.isDone();
        tx.commit();
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)

Example 27 with Transaction

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

the class GridCacheMarshallingNodeJoinSelfTest method testNodeJoin.

/**
 * @throws Exception If failed.
 */
public void testNodeJoin() throws Exception {
    final CountDownLatch allowJoin = new CountDownLatch(1);
    final CountDownLatch joined = new CountDownLatch(2);
    for (int i = 0; i < 2; i++) {
        ignite(i).events().localListen(new PE() {

            @Override
            public boolean apply(Event evt) {
                assert evt.type() == EventType.EVT_NODE_JOINED;
                info(">>> Event: " + evt);
                joined.countDown();
                return true;
            }
        }, EventType.EVT_NODE_JOINED);
    }
    IgniteInternalFuture<?> oneMoreGrid = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            allowJoin.await();
            startGrid("oneMoreGrid");
            return null;
        }
    }, 1);
    IgniteCache<Integer, TestObject> cache = ignite(0).cache(DEFAULT_CACHE_NAME);
    try (Transaction tx = ignite(0).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        cache.get(0);
        allowJoin.countDown();
        joined.await();
        assertNotNull(cache.get(1));
        tx.commit();
    }
    oneMoreGrid.get();
    assertNotNull(cache.get(1));
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheWriterException(javax.cache.integration.CacheWriterException) Transaction(org.apache.ignite.transactions.Transaction) PE(org.apache.ignite.internal.util.typedef.PE) Event(org.apache.ignite.events.Event)

Example 28 with Transaction

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

the class GridCacheMultiUpdateLockSelfTest method checkMultiUpdateLocks.

/**
 * @param nearEnabled Near enabled flag.
 * @throws Exception If failed.
 */
private void checkMultiUpdateLocks(boolean nearEnabled) throws Exception {
    this.nearEnabled = nearEnabled;
    startGrids(3);
    try {
        IgniteKernal g = (IgniteKernal) grid(0);
        GridCacheContext<Object, Object> cctx = g.internalCache(DEFAULT_CACHE_NAME).context();
        GridDhtCacheAdapter cache = nearEnabled ? cctx.near().dht() : cctx.colocated();
        AffinityTopologyVersion topVer = cache.beginMultiUpdate();
        IgniteInternalFuture<?> startFut;
        try {
            assertEquals(3, topVer.topologyVersion());
            final AtomicBoolean started = new AtomicBoolean();
            startFut = multithreadedAsync(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    info(">>>> Starting grid.");
                    Ignite g4 = startGrid(4);
                    started.set(true);
                    IgniteCache<Object, Object> c = g4.cache(DEFAULT_CACHE_NAME);
                    info(">>>> Checking tx in new grid.");
                    try (Transaction tx = g4.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                        assertEquals(2, c.get("a"));
                        assertEquals(4, c.get("b"));
                        assertEquals(6, c.get("c"));
                    }
                    return null;
                }
            }, 1);
            U.sleep(200);
            info(">>>> Checking grid has not started yet.");
            assertFalse(started.get());
            // Check we can proceed with transactions.
            IgniteCache<Object, Object> cache0 = g.cache(DEFAULT_CACHE_NAME);
            info(">>>> Checking tx commit.");
            Transaction tx = g.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
            try {
                cache0.put("a", 1);
                cache0.put("b", 2);
                cache0.put("c", 3);
                tx.commit();
            } finally {
                tx.close();
            }
            info(">>>> Checking grid still is not started");
            assertFalse(started.get());
            tx = g.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
            try {
                cache0.put("a", 2);
                cache0.put("b", 4);
                cache0.put("c", 6);
                tx.commit();
            } finally {
                tx.close();
            }
        } finally {
            info(">>>> Releasing multi update.");
            cache.endMultiUpdate();
        }
        info("Waiting for thread termination.");
        startFut.get();
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridDhtCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter) Transaction(org.apache.ignite.transactions.Transaction) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) Ignite(org.apache.ignite.Ignite) Callable(java.util.concurrent.Callable)

Example 29 with Transaction

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

the class CacheGetEntryAbstractTest method test.

/**
 * @param cfg Cache configuration.
 * @param oneEntry If {@code true} then single entry is tested.
 * @throws Exception If failed.
 */
private void test(CacheConfiguration cfg, final boolean oneEntry) throws Exception {
    final IgniteCache<Integer, TestValue> cache = grid(0).createCache(cfg);
    try {
        init(cache);
        test(cache, null, null, null, oneEntry);
        if (cfg.getAtomicityMode() == TRANSACTIONAL) {
            TransactionConcurrency txConcurrency = concurrency();
            TransactionIsolation txIsolation = isolation();
            try (Transaction tx = grid(0).transactions().txStart(txConcurrency, txIsolation)) {
                initTx(cache);
                test(cache, txConcurrency, txIsolation, tx, oneEntry);
                tx.commit();
            }
            testConcurrentTx(cache, OPTIMISTIC, REPEATABLE_READ, oneEntry);
            testConcurrentTx(cache, OPTIMISTIC, READ_COMMITTED, oneEntry);
            testConcurrentTx(cache, PESSIMISTIC, REPEATABLE_READ, oneEntry);
            testConcurrentTx(cache, PESSIMISTIC, READ_COMMITTED, oneEntry);
            testConcurrentOptimisticTxGet(cache, REPEATABLE_READ);
            testConcurrentOptimisticTxGet(cache, READ_COMMITTED);
            testConcurrentOptimisticTxGet(cache, SERIALIZABLE);
        }
    } finally {
        cache.destroy();
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation)

Example 30 with Transaction

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

the class CacheInterceptorPartitionCounterLocalSanityTest method randomUpdate.

/**
 * @param rnd Random generator.
 * @param expData Expected cache data.
 * @param cache Cache.
 * @throws Exception If failed.
 */
private void randomUpdate(Random rnd, ConcurrentMap<Object, Object> expData, IgniteCache<Object, Object> cache) throws Exception {
    Object key = new TestKey(rnd.nextInt(KEYS));
    Object newVal = value(rnd);
    Object oldVal = expData.get(key);
    int op = rnd.nextInt(11);
    Ignite ignite = cache.unwrap(Ignite.class);
    Transaction tx = null;
    if (cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL && rnd.nextBoolean())
        tx = ignite.transactions().txStart(txRandomConcurrency(rnd), txRandomIsolation(rnd));
    try {
        switch(op) {
            case 0:
                {
                    cache.put(key, newVal);
                    if (tx != null)
                        tx.commit();
                    waitAndCheckEvent(key, newVal, oldVal, false);
                    expData.put(key, newVal);
                    break;
                }
            case 1:
                {
                    cache.getAndPut(key, newVal);
                    if (tx != null)
                        tx.commit();
                    waitAndCheckEvent(key, newVal, oldVal, false);
                    expData.put(key, newVal);
                    break;
                }
            case 2:
                {
                    cache.remove(key);
                    if (tx != null)
                        tx.commit();
                    waitAndCheckEvent(key, null, oldVal, true);
                    expData.remove(key);
                    break;
                }
            case 3:
                {
                    cache.getAndRemove(key);
                    if (tx != null)
                        tx.commit();
                    waitAndCheckEvent(key, null, oldVal, true);
                    expData.remove(key);
                    break;
                }
            case 4:
                {
                    cache.invoke(key, new EntrySetValueProcessor(newVal, rnd.nextBoolean()));
                    if (tx != null)
                        tx.commit();
                    waitAndCheckEvent(key, newVal, oldVal, false);
                    expData.put(key, newVal);
                    break;
                }
            case 5:
                {
                    cache.invoke(key, new EntrySetValueProcessor(null, rnd.nextBoolean()));
                    if (tx != null)
                        tx.commit();
                    waitAndCheckEvent(key, null, oldVal, true);
                    expData.remove(key);
                    break;
                }
            case 6:
                {
                    cache.putIfAbsent(key, newVal);
                    if (tx != null)
                        tx.commit();
                    if (oldVal == null) {
                        waitAndCheckEvent(key, newVal, null, false);
                        expData.put(key, newVal);
                    } else
                        checkNoEvent(afterPutEvts);
                    break;
                }
            case 7:
                {
                    cache.getAndPutIfAbsent(key, newVal);
                    if (tx != null)
                        tx.commit();
                    if (oldVal == null) {
                        waitAndCheckEvent(key, newVal, null, false);
                        expData.put(key, newVal);
                    } else
                        checkNoEvent(afterPutEvts);
                    break;
                }
            case 8:
                {
                    cache.replace(key, newVal);
                    if (tx != null)
                        tx.commit();
                    if (oldVal != null) {
                        waitAndCheckEvent(key, newVal, oldVal, false);
                        expData.put(key, newVal);
                    } else
                        checkNoEvent(afterPutEvts);
                    break;
                }
            case 9:
                {
                    cache.getAndReplace(key, newVal);
                    if (tx != null)
                        tx.commit();
                    if (oldVal != null) {
                        waitAndCheckEvent(key, newVal, oldVal, false);
                        expData.put(key, newVal);
                    } else
                        checkNoEvent(afterPutEvts);
                    break;
                }
            case 10:
                {
                    if (oldVal != null) {
                        Object replaceVal = value(rnd);
                        boolean success = replaceVal.equals(oldVal);
                        if (success) {
                            cache.replace(key, replaceVal, newVal);
                            if (tx != null)
                                tx.commit();
                            waitAndCheckEvent(key, newVal, oldVal, false);
                            expData.put(key, newVal);
                        } else {
                            cache.replace(key, replaceVal, newVal);
                            if (tx != null)
                                tx.commit();
                            checkNoEvent(afterPutEvts);
                        }
                    } else {
                        cache.replace(key, value(rnd), newVal);
                        if (tx != null)
                            tx.commit();
                        checkNoEvent(afterPutEvts);
                    }
                    break;
                }
            default:
                fail("Op:" + op);
        }
    } finally {
        if (tx != null)
            tx.close();
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

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