use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxReadInParallerTxWrite.
/**
* Transactional read in parallel with changing the same data.
*
* @throws Exception If failed.
*/
public void testTxReadInParallerTxWrite() throws Exception {
final Ignite ignite = ignite(0);
final Integer key = 1;
final Integer val = 1;
final CountDownLatch readLatch = new CountDownLatch(1);
final CountDownLatch writeLatch = new CountDownLatch(1);
final Exception[] err = { null };
final String cacheName = ignite.createCache(cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false)).getName();
final IgniteCache<Integer, Integer> cache = ignite.cache(cacheName);
try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(key, val);
tx.commit();
}
try {
IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
assertTrue(cache.get(key).equals(val));
readLatch.countDown();
writeLatch.await(10, TimeUnit.SECONDS);
try {
tx.commit();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
err[0] = e;
}
}
return null;
}
}, "read-thread");
GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
readLatch.await(10, TimeUnit.SECONDS);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(key, val);
tx.commit();
}
writeLatch.countDown();
return null;
}
}, "write-thread").get();
fut.get();
assertNotNull("Expected exception was not thrown", err[0]);
} finally {
destroyCache(cacheName);
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class CacheSerializableTransactionsTest method rollbackNearCacheWrite.
/**
* @param near If {@code true} locks entry using the same near cache.
* @throws Exception If failed.
*/
private void rollbackNearCacheWrite(boolean near) throws Exception {
Ignite ignite0 = ignite(0);
IgniteCache<Integer, Integer> cache0 = ignite0.createCache(cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false));
final String cacheName = cache0.getName();
try {
Ignite ignite = ignite(SRVS);
IgniteCache<Integer, Integer> cache = ignite.createNearCache(cacheName, new NearCacheConfiguration<Integer, Integer>());
IgniteTransactions txs = ignite.transactions();
Integer key1 = primaryKey(ignite(0).cache(cacheName));
Integer key2 = primaryKey(ignite(1).cache(cacheName));
Integer key3 = primaryKey(ignite(2).cache(cacheName));
CountDownLatch latch = new CountDownLatch(1);
IgniteInternalFuture<?> fut = null;
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(key1, key1);
cache.put(key2, key2);
cache.put(key3, key3);
fut = lockKey(latch, near ? cache : cache0, key2);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
latch.countDown();
assert fut != null;
fut.get();
checkValue(key1, null, cacheName);
checkValue(key2, 1, cacheName);
checkValue(key3, null, cacheName);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(key1, key1);
cache.put(key2, key2);
cache.put(key3, key3);
tx.commit();
}
checkValue(key1, key1, cacheName);
checkValue(key2, key2, cacheName);
checkValue(key3, key3, cacheName);
} finally {
destroyCache(cacheName);
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxConflictPutIfAbsent.
/**
* @throws Exception If failed.
*/
public void testTxConflictPutIfAbsent() 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);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean put = cache.putIfAbsent(key, 2);
assertTrue(put);
updateKey(cache, key, 1);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, 1, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean put = cache.putIfAbsent(key, 2);
assertFalse(put);
tx.commit();
}
checkValue(key, 1, cache.getName());
cache.remove(key);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean put = cache.putIfAbsent(key, 2);
assertTrue(put);
tx.commit();
}
checkValue(key, 2, cache.getName());
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean put = cache.putIfAbsent(key, 2);
assertFalse(put);
updateKey(cache, key, 3);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, 3, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class CacheSerializableTransactionsTest method checkReadWriteTransactionsNoDeadlock.
/**
* @param multiNode Multi-node test flag.
* @throws Exception If failed.
*/
private void checkReadWriteTransactionsNoDeadlock(final boolean multiNode) throws Exception {
final Ignite ignite0 = ignite(0);
for (final CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
ignite0.createCache(ccfg);
try {
final long stopTime = U.currentTimeMillis() + 10_000;
final AtomicInteger idx = new AtomicInteger();
GridTestUtils.runMultiThreaded(new Callable<Void>() {
@Override
public Void call() throws Exception {
Ignite ignite = multiNode ? ignite(idx.incrementAndGet() % (SRVS + CLIENTS)) : ignite0;
IgniteCache<Integer, Integer> cache = ignite.cache(ccfg.getName());
ThreadLocalRandom rnd = ThreadLocalRandom.current();
while (U.currentTimeMillis() < stopTime) {
try {
try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
for (int i = 0; i < 10; i++) {
Integer key = rnd.nextInt(30);
if (rnd.nextBoolean())
cache.get(key);
else
cache.put(key, key);
}
tx.commit();
}
} catch (TransactionOptimisticException ignore) {
// No-op.
}
}
return null;
}
}, 32, "test-thread");
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxConflictReadWrite3.
/**
* @throws Exception If failed.
*/
public void testTxConflictReadWrite3() throws Exception {
Ignite ignite0 = ignite(0);
final IgniteTransactions txs = ignite0.transactions();
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
List<Integer> readKeys = new ArrayList<>();
List<Integer> writeKeys = new ArrayList<>();
readKeys.add(primaryKey(cache));
writeKeys.add(primaryKeys(cache, 1, 1000_0000).get(0));
if (ccfg.getBackups() > 0) {
readKeys.add(backupKey(cache));
writeKeys.add(backupKeys(cache, 1, 1000_0000).get(0));
}
if (ccfg.getCacheMode() == PARTITIONED) {
readKeys.add(nearKey(cache));
writeKeys.add(nearKeys(cache, 1, 1000_0000).get(0));
}
try {
for (Integer readKey : readKeys) {
for (Integer writeKey : writeKeys) {
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.get(readKey);
cache.put(writeKey, writeKey);
updateKey(cache, readKey, 0);
tx.commit();
}
fail();
} catch (TransactionOptimisticException ignored) {
// Expected exception.
}
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.get(readKey);
cache.put(writeKey, writeKey);
tx.commit();
}
}
}
} finally {
destroyCache(ccfg.getName());
}
}
}
Aggregations