use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method txConflictGetAndPut.
/**
* @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 txConflictGetAndPut(boolean noVal, boolean rmv) 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)) {
Integer val = rmv ? cache.getAndRemove(key) : cache.getAndPut(key, 2);
assertEquals(expVal, val);
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)) {
Object val = rmv ? cache.getAndRemove(key) : cache.getAndPut(key, 2);
assertEquals(1, val);
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 testTxConflictGetAndReplace.
/**
* @throws Exception If failed.
*/
public void testTxConflictGetAndReplace() 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)) {
Object old = cache.getAndReplace(key, 2);
assertNull(old);
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)) {
Object old = cache.getAndReplace(key, 2);
assertEquals(1, old);
tx.commit();
}
checkValue(key, 2, cache.getName());
cache.remove(key);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Object old = cache.getAndReplace(key, 2);
assertNull(old);
tx.commit();
}
checkValue(key, null, cache.getName());
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Object old = cache.getAndReplace(key, 2);
assertNull(old);
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)) {
Object old = cache.getAndReplace(key, 2);
assertEquals(3, old);
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)) {
Object old = cache.getAndReplace(key, 2);
assertEquals(1, old);
tx.commit();
}
checkValue(key, 2, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method testConflictResolution.
/**
* @throws Exception If failed.
*/
public void testConflictResolution() throws Exception {
final Ignite ignite = ignite(0);
final String cacheName = ignite.createCache(cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false)).getName();
try {
final Map<Integer, Integer> keys = new HashMap<>();
for (int i = 0; i < 500; i++) keys.put(i, i);
final int THREADS = 5;
for (int i = 0; i < 10; i++) {
final CyclicBarrier barrier = new CyclicBarrier(THREADS);
final AtomicInteger commitCntr = new AtomicInteger(0);
GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
IgniteCache<Integer, Integer> cache = ignite.cache(cacheName);
IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.putAll(keys);
barrier.await();
tx.commit();
commitCntr.incrementAndGet();
} catch (TransactionOptimisticException e) {
log.info("Optimistic error: " + e);
}
return null;
}
}, THREADS, "update-thread").get();
int commits = commitCntr.get();
log.info("Iteration [iter=" + i + ", commits=" + commits + ']');
assertTrue(commits > 0);
}
} finally {
destroyCache(cacheName);
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxCommitReadWriteTwoNodes.
/**
* @throws Exception If failed.
*/
public void testTxCommitReadWriteTwoNodes() 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);
Integer key0 = primaryKey(ignite(0).cache(DEFAULT_CACHE_NAME));
Integer key1 = primaryKey(ignite(1).cache(DEFAULT_CACHE_NAME));
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(key0, key0);
cache.get(key1);
tx.commit();
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method txConflictRemoveReturnBoolean.
/**
* @param noVal If {@code true} there is no cache value when do update in tx.
* @throws Exception If failed.
*/
private void txConflictRemoveReturnBoolean(boolean noVal) 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);
if (!noVal)
cache.put(key, -1);
if (noVal) {
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean res = cache.remove(key);
assertFalse(res);
updateKey(cache, key, -1);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, -1, cache.getName());
} else {
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean res = cache.remove(key);
assertTrue(res);
txAsync(cache, PESSIMISTIC, REPEATABLE_READ, 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 res = cache.remove(key);
assertTrue(res);
updateKey(cache, key, 2);
tx.commit();
}
checkValue(key, null, cache.getName());
// Check no conflict for removeAll with single key.
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.removeAll(Collections.singleton(key));
txAsync(cache, PESSIMISTIC, REPEATABLE_READ, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {
@Override
public Void apply(IgniteCache<Integer, Integer> cache) {
cache.remove(key);
return null;
}
});
tx.commit();
}
checkValue(key, null, cache.getName());
cache.put(key, 2);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean res = cache.remove(key);
assertTrue(res);
tx.commit();
}
checkValue(key, null, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean res = cache.remove(key);
assertFalse(res);
tx.commit();
}
checkValue(key, null, cache.getName());
try {
cache.put(key, 1);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Object val = cache.get(key);
assertEquals(1, val);
boolean res = cache.remove(key);
assertTrue(res);
updateKey(cache, key, 2);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
}
} finally {
destroyCache(ccfg.getName());
}
}
}
Aggregations