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