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;
}
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;
}
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;
}
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;
}
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));
}
Aggregations