use of org.apache.ignite.transactions.Transaction 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.Transaction in project ignite by apache.
the class CacheSerializableTransactionsTest method txNoConflictUpdate.
/**
* @throws Exception If failed.
* @param noVal If {@code true} there is no cache value when do update in tx.
* @param rmv If {@code true} tests remove, otherwise put.
* @param getAfterUpdate If {@code true} tries to get value in tx after update.
*/
private void txNoConflictUpdate(boolean noVal, boolean rmv, boolean getAfterUpdate) 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);
if (!noVal)
cache.put(key, -1);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
if (rmv)
cache.remove(key);
else
cache.put(key, 2);
if (getAfterUpdate) {
Object val = cache.get(key);
if (rmv)
assertNull(val);
else
assertEquals(2, val);
}
if (!rmv)
updateKey(cache, key, 1);
tx.commit();
}
checkValue(key, rmv ? null : 2, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(key, 3);
tx.commit();
}
checkValue(key, 3, cache.getName());
}
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < 100; i++) map.put(i, i);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
if (rmv)
cache.removeAll(map.keySet());
else
cache.putAll(map);
if (getAfterUpdate) {
Map<Integer, Integer> res = cache.getAll(map.keySet());
if (rmv) {
for (Integer key : map.keySet()) assertNull(res.get(key));
} else {
for (Integer key : map.keySet()) assertEquals(map.get(key), res.get(key));
}
}
txAsync(cache, PESSIMISTIC, REPEATABLE_READ, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {
@Override
public Void apply(IgniteCache<Integer, Integer> cache) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < 100; i++) map.put(i, -1);
cache.putAll(map);
return null;
}
});
tx.commit();
}
for (int i = 0; i < 100; i++) checkValue(i, rmv ? null : i, cache.getName());
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.transactions.Transaction 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.Transaction 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.Transaction 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());
}
}
}
Aggregations