use of org.apache.ignite.IgniteTransactions 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.IgniteTransactions 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.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method txConflictReadWrite.
/**
* @param noVal If {@code true} there is no cache value when read in tx.
* @param rmv If {@code true} tests remove, otherwise put.
* @throws Exception If failed.
*/
private void txConflictReadWrite(boolean noVal, boolean rmv, boolean needVer) throws Exception {
Ignite ignite0 = ignite(0);
final IgniteTransactions txs = ignite0.transactions();
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
try {
IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
List<Integer> keys = testKeys(cache);
for (Integer key : keys) {
log.info("Test key: " + key);
Integer expVal = null;
if (!noVal) {
expVal = -1;
cache.put(key, expVal);
}
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
if (needVer) {
CacheEntry<Integer, Integer> val = cache.getEntry(key);
assertEquals(expVal, val == null ? null : val.getValue());
} else {
Integer val = cache.get(key);
assertEquals(expVal, val);
}
updateKey(cache, key, 1);
if (rmv)
cache.remove(key);
else
cache.put(key, 2);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, 1, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
if (needVer) {
CacheEntry<Integer, Integer> val = cache.getEntry(key);
assertEquals(1, (Object) val.getValue());
} else {
Integer val = cache.get(key);
assertEquals(1, (Object) val);
}
if (rmv)
cache.remove(key);
else
cache.put(key, 2);
tx.commit();
}
checkValue(key, rmv ? null : 2, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxLoadFromStore.
/**
* @throws Exception If failed.
*/
public void testTxLoadFromStore() throws Exception {
Ignite ignite0 = ignite(0);
final IgniteTransactions txs = ignite0.transactions();
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
if (ccfg.getCacheStoreFactory() == null)
continue;
logCacheInfo(ccfg);
try {
IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
List<Integer> keys = testKeys(cache);
for (Integer key : keys) {
log.info("Test key: " + key);
Integer storeVal = -1;
storeMap.put(key, storeVal);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache.get(key);
assertEquals(storeVal, val);
tx.commit();
}
checkValue(key, storeVal, cache.getName());
cache.remove(key);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache.get(key);
assertNull(val);
tx.commit();
}
checkValue(key, null, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method testRandomOperations.
/**
* @throws Exception If failed.
*/
public void testRandomOperations() throws Exception {
Ignite ignite0 = ignite(0);
long stopTime = U.currentTimeMillis() + getTestTimeout() - 30_000;
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
try {
IgniteCache<Integer, Integer> cache0 = ignite0.createCache(ccfg);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (Ignite ignite : G.allGrids()) {
log.info("Test node: " + ignite.name());
IgniteCache<Integer, Integer> cache = ignite.cache(ccfg.getName());
IgniteTransactions txs = ignite.transactions();
final int KEYS = 100;
for (int i = 0; i < 1000; i++) {
Integer key1 = rnd.nextInt(KEYS);
Integer key2;
if (rnd.nextBoolean()) {
key2 = rnd.nextInt(KEYS);
while (key2.equals(key1)) key2 = rnd.nextInt(KEYS);
} else
key2 = key1 + 1;
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
randomOperation(rnd, cache, key1);
randomOperation(rnd, cache, key2);
tx.commit();
}
if (i % 100 == 0 && U.currentTimeMillis() > stopTime)
break;
}
for (int key = 0; key < KEYS; key++) {
Integer val = cache0.get(key);
for (int node = 1; node < SRVS + CLIENTS; node++) assertEquals(val, ignite(node).cache(cache.getName()).get(key));
}
if (U.currentTimeMillis() > stopTime)
break;
}
} finally {
destroyCache(ccfg.getName());
}
}
}
Aggregations