Search in sources :

Example 76 with Transaction

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

the class ReplicatedAtomicCacheGetsDistributionTest method runTestSameHostDistribution.

/**
 * Tests that the 'get' and 'getAll' requests are routed to node with same MAC address as at requester.
 *
 * @param destId Destination Ignite instance id for requests distribution.
 * @param batchMode Test mode.
 * @throws Exception In case of an error.
 */
protected void runTestSameHostDistribution(final UUID destId, final boolean batchMode) throws Exception {
    Map<UUID, String> macs = getClusterMacs();
    String clientMac = macs.get(grid(CLIENT_NAME).localNode().id());
    macs.put(destId, clientMac);
    replaceMacAddresses(G.allGrids(), macs);
    IgniteCache<Integer, String> cache = grid(0).createCache(cacheConfiguration());
    List<Integer> keys = primaryKeys(cache, PRIMARY_KEYS_NUMBER);
    for (Integer key : keys) cache.put(key, VAL_PREFIX + key);
    IgniteCache<Integer, String> clientCache = grid(CLIENT_NAME).getOrCreateCache(CACHE_NAME);
    try (Transaction tx = grid(CLIENT_NAME).transactions().txStart()) {
        if (batchMode) {
            Map<Integer, String> results = clientCache.getAll(new TreeSet<>(keys));
            for (Map.Entry<Integer, String> entry : results.entrySet()) assertEquals(VAL_PREFIX + entry.getKey(), entry.getValue());
        } else {
            for (Integer key : keys) assertEquals(VAL_PREFIX + key, clientCache.get(key));
        }
        tx.commit();
    }
    for (int i = 0; i < gridCount(); i++) {
        IgniteEx ignite = grid(i);
        long getsCnt = ignite.cache(CACHE_NAME).localMetrics().getCacheGets();
        if (destId.equals(ignite.localNode().id()))
            assertEquals(PRIMARY_KEYS_NUMBER, getsCnt);
        else
            assertEquals(0L, getsCnt);
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map)

Example 77 with Transaction

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

the class IndexingSpiQuerySelfTest method testIndexingSpiFailure.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public void testIndexingSpiFailure() throws Exception {
    IgniteConfiguration cfg = configuration();
    cfg.setIndexingSpi(new MyBrokenIndexingSpi());
    Ignite ignite = Ignition.start(cfg);
    CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(CACHE_NAME);
    ccfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
    final IgniteCache<Integer, Integer> cache = ignite.createCache(ccfg);
    final IgniteTransactions txs = ignite.transactions();
    for (final TransactionConcurrency concurrency : TransactionConcurrency.values()) {
        for (final TransactionIsolation isolation : TransactionIsolation.values()) {
            System.out.println("Run in transaction: " + concurrency + " " + isolation);
            GridTestUtils.assertThrowsWithCause(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    Transaction tx;
                    try (Transaction tx0 = tx = txs.txStart(concurrency, isolation)) {
                        cache.put(1, 1);
                        tx0.commit();
                    }
                    assertEquals(TransactionState.ROLLED_BACK, tx.state());
                    return null;
                }
            }, IgniteTxHeuristicCheckedException.class);
        }
    }
}
Also used : TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteTransactions(org.apache.ignite.IgniteTransactions) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteTxHeuristicCheckedException(org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite)

Example 78 with Transaction

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

the class IndexingSpiQueryTxSelfTest method testIndexingSpiWithTx.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public void testIndexingSpiWithTx() throws Exception {
    IgniteEx ignite = grid(0);
    final IgniteCache<Integer, Integer> cache = ignite.cache("test-cache");
    final IgniteTransactions txs = ignite.transactions();
    for (final TransactionConcurrency concurrency : TransactionConcurrency.values()) {
        for (final TransactionIsolation isolation : TransactionIsolation.values()) {
            System.out.println("Run in transaction: " + concurrency + " " + isolation);
            GridTestUtils.assertThrowsWithCause(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    Transaction tx;
                    try (Transaction tx0 = tx = txs.txStart(concurrency, isolation)) {
                        cache.put(1, 1);
                        tx0.commit();
                    }
                    assertEquals(TransactionState.ROLLED_BACK, tx.state());
                    return null;
                }
            }, IgniteTxHeuristicCheckedException.class);
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteTransactions(org.apache.ignite.IgniteTransactions) IgniteTxHeuristicCheckedException(org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException)

Example 79 with Transaction

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

the class IgniteCacheTxStoreSessionTest method testTxPut.

/**
 * @param cache Cache.
 * @param concurrency Concurrency mode.
 * @param isolation Isolation mode.
 * @throws Exception If failed.
 */
private void testTxPut(IgniteCache<Object, Object> cache, TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
    log.info("Test tx put [concurrency=" + concurrency + ", isolation=" + isolation + ']');
    List<Integer> keys = testKeys(cache, 3);
    Integer key1 = keys.get(0);
    try (Transaction tx = startTx(concurrency, isolation)) {
        log.info("Do tx get.");
        expData.add(new ExpectedData(false, "load", new HashMap(), cache.getName()));
        expData.add(new ExpectedData(true, "sessionEnd", F.<Object, Object>asMap(0, "load"), cache.getName()));
        cache.get(key1);
        expData.clear();
        log.info("Do tx put.");
        cache.put(key1, key1);
        expData.add(new ExpectedData(true, "write", new HashMap<>(), cache.getName()));
        expData.add(new ExpectedData(true, "sessionEnd", F.<Object, Object>asMap(0, "write"), cache.getName()));
        log.info("Do tx commit.");
        tx.commit();
    }
    assertEquals(0, expData.size());
    Integer key2 = keys.get(1);
    Integer key3 = keys.get(2);
    try (Transaction tx = startTx(concurrency, isolation)) {
        log.info("Do tx put1.");
        cache.put(key2, key2);
        log.info("Do tx put2.");
        cache.put(key3, key3);
        expData.add(new ExpectedData(true, "writeAll", new HashMap<>(), cache.getName()));
        expData.add(new ExpectedData(true, "sessionEnd", F.<Object, Object>asMap(0, "writeAll"), cache.getName()));
        log.info("Do tx commit.");
        tx.commit();
    }
    assertEquals(0, expData.size());
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) HashMap(java.util.HashMap)

Example 80 with Transaction

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

the class GridCacheLocalByteArrayValuesSelfTest method testTransactionMixed.

/**
 * Test transaction behavior.
 *
 * @param cache Cache.
 * @param concurrency Concurrency.
 * @param key1 Key 1.
 * @param val1 Value 1.
 * @param key2 Key 2.
 * @param val2 Value 2.
 * @throws Exception If failed.
 */
private void testTransactionMixed(IgniteCache<Integer, Object> cache, TransactionConcurrency concurrency, Integer key1, byte[] val1, @Nullable Integer key2, @Nullable Object val2) throws Exception {
    Transaction tx = ignite.transactions().txStart(concurrency, REPEATABLE_READ);
    try {
        cache.put(key1, val1);
        if (key2 != null)
            cache.put(key2, val2);
        tx.commit();
    } finally {
        tx.close();
    }
    tx = ignite.transactions().txStart(concurrency, REPEATABLE_READ);
    try {
        assert Arrays.equals(val1, (byte[]) cache.get(key1));
        if (key2 != null)
            assert F.eq(val2, cache.get(key2));
        tx.commit();
    } finally {
        tx.close();
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction)

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