use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxRollback.
/**
* @throws Exception If failed.
*/
public void testTxRollback() throws Exception {
Ignite ignite0 = ignite(0);
Ignite ignite1 = ignite(1);
final IgniteTransactions txs0 = ignite0.transactions();
final IgniteTransactions txs1 = ignite1.transactions();
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
try {
IgniteCache<Integer, Integer> cache0 = ignite0.createCache(ccfg);
IgniteCache<Integer, Integer> cache1 = ignite1.cache(ccfg.getName());
List<Integer> keys = testKeys(cache0);
for (Integer key : keys) {
log.info("Test key: " + key);
Integer expVal = null;
for (int i = 0; i < 100; i++) {
try (Transaction tx = txs0.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache0.get(key);
assertEquals(expVal, val);
cache0.put(key, i);
tx.rollback();
}
try (Transaction tx = txs0.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache0.get(key);
assertEquals(expVal, val);
cache0.put(key, i);
tx.commit();
expVal = i;
}
try (Transaction tx = txs1.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache1.get(key);
assertEquals(expVal, val);
cache1.put(key, val);
tx.commit();
}
}
checkValue(key, expVal, cache0.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.transactions.Transaction 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());
}
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheSerializableTransactionsTest method testNoReadLockConflictMultiNode.
/**
* @throws Exception If failed.
*/
public void testNoReadLockConflictMultiNode() throws Exception {
Ignite ignite0 = ignite(0);
for (final CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
final AtomicInteger putKey = new AtomicInteger(1_000_000);
ignite0.createCache(ccfg);
try {
final int THREADS = 64;
IgniteCache<Integer, Integer> cache0 = ignite0.cache(ccfg.getName());
List<Integer> readKeys = testKeys(cache0);
for (final Integer readKey : readKeys) {
final CyclicBarrier barrier = new CyclicBarrier(THREADS);
cache0.put(readKey, Integer.MIN_VALUE);
final AtomicInteger idx = new AtomicInteger();
GridTestUtils.runMultiThreaded(new Callable<Void>() {
@Override
public Void call() throws Exception {
Ignite ignite = ignite(idx.incrementAndGet() % (CLIENTS + SRVS));
IgniteCache<Integer, Integer> cache = ignite.cache(ccfg.getName());
try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.get(readKey);
barrier.await();
cache.put(putKey.incrementAndGet(), 0);
tx.commit();
}
return null;
}
}, THREADS, "test-thread");
assertEquals((Integer) Integer.MIN_VALUE, cache0.get(readKey));
cache0.put(readKey, readKey);
assertEquals(readKey, cache0.get(readKey));
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxConflictReplace.
/**
* @throws Exception If failed.
*/
public void testTxConflictReplace() 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 replace = cache.replace(key, 2);
assertFalse(replace);
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 replace = cache.replace(key, 2);
assertTrue(replace);
tx.commit();
}
checkValue(key, 2, cache.getName());
cache.remove(key);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 2);
assertFalse(replace);
tx.commit();
}
checkValue(key, null, cache.getName());
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 2);
assertFalse(replace);
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 replace = cache.replace(key, 2);
assertTrue(replace);
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 replace = cache.replace(key, 2);
assertTrue(replace);
tx.commit();
}
checkValue(key, 2, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheSerializableTransactionsTest method rollbackIfLockedPartialLock.
/**
* @param locKey If {@code true} gets lock for local key.
* @throws Exception If failed.
*/
private void rollbackIfLockedPartialLock(boolean locKey) 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);
final Integer key1 = primaryKey(ignite(1).cache(cache.getName()));
final Integer key2 = locKey ? primaryKey(cache) : primaryKey(ignite(2).cache(cache.getName()));
CountDownLatch latch = new CountDownLatch(1);
IgniteInternalFuture<?> fut = lockKey(latch, cache, key1);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(key1, 2);
cache.put(key2, 2);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
latch.countDown();
fut.get();
checkValue(key1, 1, cache.getName());
checkValue(key2, null, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(key1, 2);
cache.put(key2, 2);
tx.commit();
}
checkValue(key1, 2, cache.getName());
checkValue(key2, 2, cache.getName());
} finally {
destroyCache(ccfg.getName());
}
}
}
Aggregations