use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxConflictRemoveWithOldValue.
/**
* @throws Exception If failed.
*/
public void testTxConflictRemoveWithOldValue() 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 (final Integer key : keys) {
log.info("Test key: " + key);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 2);
assertFalse(rmv);
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 rmv = cache.remove(key, 1);
assertTrue(rmv);
tx.commit();
}
checkValue(key, null, cache.getName());
cache.remove(key);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 2);
assertFalse(rmv);
tx.commit();
}
checkValue(key, null, cache.getName());
cache.put(key, 2);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 2);
assertTrue(rmv);
updateKey(cache, key, 3);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, 3, cache.getName());
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 3);
assertTrue(rmv);
txAsync(cache, OPTIMISTIC, SERIALIZABLE, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {
@Override
public Void apply(IgniteCache<Integer, Integer> cache) {
cache.remove(key);
return null;
}
});
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, null, cache.getName());
cache.put(key, 1);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 2);
assertFalse(rmv);
tx.commit();
}
checkValue(key, 1, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 1);
assertTrue(rmv);
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 testTxCommitReadOnlyGetAll.
/**
* @throws Exception If failed.
*/
private void testTxCommitReadOnlyGetAll(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);
Set<Integer> keys = new HashSet<>();
for (int i = 0; i < 100; i++) keys.add(i);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
if (needVer) {
Collection<CacheEntry<Integer, Integer>> c = cache.getEntries(keys);
assertTrue(c.isEmpty());
} else {
Map<Integer, Integer> map = cache.getAll(keys);
assertTrue(map.isEmpty());
}
tx.commit();
}
for (Integer key : keys) checkValue(key, null, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
if (needVer) {
Collection<CacheEntry<Integer, Integer>> c = cache.getEntries(keys);
assertTrue(c.isEmpty());
} else {
Map<Integer, Integer> map = cache.getAll(keys);
assertTrue(map.isEmpty());
}
tx.rollback();
}
for (Integer key : keys) checkValue(key, null, cache.getName());
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.IgniteTransactions 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.IgniteTransactions 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.IgniteTransactions 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);
}
}
Aggregations