Search in sources :

Example 11 with Transaction

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

the class IgniteAccountSerializableTxBenchmark method test.

/**
 * {@inheritDoc}
 */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
    Set<Integer> accountIds = new HashSet<>();
    int accNum = args.batch();
    while (accountIds.size() < accNum) accountIds.add(nextRandom(args.range()));
    while (true) {
        try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
            Map<Integer, Account> accounts = (Map) cache.getAll(accountIds);
            if (accounts.size() != accNum)
                throw new Exception("Failed to find accounts: " + accountIds);
            Integer fromId = accountIds.iterator().next();
            int fromBalance = accounts.get(fromId).balance();
            for (Integer id : accountIds) {
                if (id.equals(fromId))
                    continue;
                Account account = accounts.get(id);
                if (fromBalance > 0) {
                    fromBalance--;
                    cache.put(id, new Account(account.balance() + 1));
                }
            }
            cache.put(fromId, new Account(fromBalance));
            tx.commit();
        } catch (TransactionOptimisticException e) {
            continue;
        }
        break;
    }
    return true;
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) Account(org.apache.ignite.yardstick.cache.model.Account) Transaction(org.apache.ignite.transactions.Transaction) Map(java.util.Map) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) HashSet(java.util.HashSet)

Example 12 with Transaction

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

the class IgnitePutAllSerializableTxBenchmark method test.

/**
 * {@inheritDoc}
 */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
    ThreadRange r = threadRange();
    Map<Integer, Integer> vals = new HashMap<>();
    for (int i = 0; i < args.batch(); i++) {
        int key = r.nextRandom();
        vals.put(key, key);
    }
    IgniteCache<Integer, Object> cache = cacheForOperation();
    while (true) {
        try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
            cache.putAll(vals);
            tx.commit();
        } catch (TransactionOptimisticException e) {
            continue;
        }
        break;
    }
    return true;
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) Transaction(org.apache.ignite.transactions.Transaction) HashMap(java.util.HashMap)

Example 13 with Transaction

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

the class IgnitePutTxLoadBenchmark method test.

/**
 * {@inheritDoc}
 */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
    IgniteTransactions transactions = ignite().transactions();
    long startTime;
    long endTime;
    try (Transaction tx = transactions.txStart(args.txConcurrency(), args.txIsolation())) {
        ArrayList<Long> keyList = new ArrayList<>(args.scaleFactor());
        for (int i = 0; i < args.scaleFactor(); i++) keyList.add(random.nextLong());
        Collections.sort(keyList);
        for (int i = 0; i < args.scaleFactor(); i++) {
            IgniteCache<Object, Object> curCache = cacheList.get(random.nextInt(cacheList.size()));
            curCache.put(keyList.get(i), val);
        }
        startTime = System.currentTimeMillis();
        tx.commit();
        endTime = System.currentTimeMillis();
    }
    TransactionMetrics tm = transactions.metrics();
    if (endTime - startTime > args.getWarningTime())
        BenchmarkUtils.println("Transaction commit time = " + (endTime - startTime));
    if (tm.txRollbacks() > 0 && args.printRollBacks())
        BenchmarkUtils.println("Transaction rollbacks = " + tm.txRollbacks());
    return true;
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) ArrayList(java.util.ArrayList) IgniteTransactions(org.apache.ignite.IgniteTransactions) TransactionMetrics(org.apache.ignite.transactions.TransactionMetrics)

Example 14 with Transaction

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

the class FitnessJob method execute.

/**
 * Perform fitness operation utilizing IFitnessFunction
 *
 * Update chromosome's fitness value
 *
 * @return Fitness score
 */
public Double execute() throws IgniteException {
    IgniteCache<Long, Chromosome> populationCache = ignite.cache(GAGridConstants.POPULATION_CACHE);
    IgniteCache<Long, Gene> geneCache = ignite.cache(GAGridConstants.GENE_CACHE);
    Chromosome chromosome = populationCache.localPeek(key);
    long[] geneKeys = chromosome.getGenes();
    List<Gene> genes = new ArrayList();
    for (int i = 0; i < geneKeys.length; i++) {
        long aKey = geneKeys[i];
        Gene aGene = geneCache.localPeek(aKey);
        genes.add(aGene);
    }
    Double value = fitnessFuncton.evaluate(genes);
    chromosome.setFitnessScore(value);
    Transaction tx = ignite.transactions().txStart();
    populationCache.put(chromosome.id(), chromosome);
    tx.commit();
    return value;
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) ArrayList(java.util.ArrayList)

Example 15 with Transaction

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

the class IgniteCacheGroupsSqlTest method joinQuery.

/**
 * @param grp1 First cache group.
 * @param grp2 Second cache group.
 * @param cm1 First cache mode.
 * @param cm2 Second cache mode.
 * @param cam1 First cache atomicity mode.
 * @param cam2 Second cache atomicity mode.
 * @throws Exception If failed.
 */
private void joinQuery(String grp1, String grp2, CacheMode cm1, CacheMode cm2, CacheAtomicityMode cam1, CacheAtomicityMode cam2) throws Exception {
    int keys = 1000;
    int accsPerPerson = 4;
    Ignite srv0 = ignite(0);
    IgniteCache pers = srv0.createCache(personCacheConfiguration(grp1, "pers").setAffinity(new RendezvousAffinityFunction().setPartitions(10)).setCacheMode(cm1).setAtomicityMode(cam1));
    IgniteCache acc = srv0.createCache(accountCacheConfiguration(grp2, "acc").setAffinity(new RendezvousAffinityFunction().setPartitions(10)).setCacheMode(cm2).setAtomicityMode(cam2));
    try (Transaction tx = cam1 == TRANSACTIONAL || cam2 == TRANSACTIONAL ? srv0.transactions().txStart() : null) {
        for (int i = 0; i < keys; i++) {
            int pKey = i - (i % accsPerPerson);
            if (i % accsPerPerson == 0)
                pers.put(pKey, new Person("pers-" + pKey));
            acc.put(new AffinityKey(i, pKey), new Account(pKey, "acc-" + i));
        }
        if (tx != null)
            tx.commit();
    }
    Ignite node = ignite(2);
    SqlFieldsQuery qry = new SqlFieldsQuery("select p._key as p_key, p.name, a._key as a_key, a.personId, a.attr \n" + "from \"pers\".Person p inner join \"acc\".Account a \n" + "on (p._key = a.personId)");
    IgniteCache<Object, Object> cache = node.cache("acc");
    List<List<?>> res = cache.query(qry).getAll();
    assertEquals(keys, res.size());
    for (List<?> row : res) assertEquals(row.get(0), row.get(3));
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) List(java.util.List) AffinityKey(org.apache.ignite.internal.binary.AffinityKey)

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