use of org.apache.ignite.transactions.Transaction 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.transactions.Transaction in project ignite by apache.
the class CacheSerializableTransactionsTest method testReadLockPessimisticTxConflict.
/**
* @throws Exception If failed.
*/
@SuppressWarnings("UnnecessaryLocalVariable")
public void testReadLockPessimisticTxConflict() throws Exception {
Ignite ignite0 = ignite(0);
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
ignite0.createCache(ccfg);
try {
Ignite ignite = ignite0;
IgniteCache<Integer, Integer> cache = ignite.cache(ccfg.getName());
Integer writeKey = Integer.MAX_VALUE;
List<Integer> readKeys = testKeys(cache);
for (Integer readKey : readKeys) {
CountDownLatch latch = new CountDownLatch(1);
IgniteInternalFuture<?> fut = lockKey(latch, cache, readKey);
try {
// No conflict for write, conflict with pessimistic tx for read.
try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(writeKey, writeKey);
cache.get(readKey);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
} finally {
latch.countDown();
}
fut.get();
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class GridCacheAtomicStampedApiSelfAbstractTest method testIsolation.
/**
* @throws Exception If failed.
*/
public void testIsolation() throws Exception {
Ignite ignite = grid(0);
CacheConfiguration cfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
cfg.setName("MyCache");
cfg.setAtomicityMode(TRANSACTIONAL);
cfg.setWriteSynchronizationMode(FULL_SYNC);
IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(cfg);
try {
String atomicName = UUID.randomUUID().toString();
String initVal = "qwerty";
String initStamp = "asdf";
IgniteAtomicStamped<String, String> atomicStamped = ignite.atomicStamped(atomicName, initVal, initStamp, true);
try (Transaction tx = ignite.transactions().txStart()) {
cache.put(1, 1);
assertEquals(initVal, atomicStamped.value());
assertEquals(initStamp, atomicStamped.stamp());
assertEquals(initVal, atomicStamped.get().get1());
assertEquals(initStamp, atomicStamped.get().get2());
assertTrue(atomicStamped.compareAndSet(initVal, "b", initStamp, "d"));
tx.rollback();
}
assertEquals(0, cache.size());
assertEquals("b", atomicStamped.value());
assertEquals("d", atomicStamped.stamp());
atomicStamped.close();
assertTrue(atomicStamped.removed());
} finally {
ignite.destroyCache(cfg.getName());
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class IgniteWalFlushFailoverTest method flushingErrorTest.
/**
* @throws Exception if failed.
*/
private void flushingErrorTest() throws Exception {
final IgniteEx grid = startGrid(0);
FileWriteAheadLogManager wal = (FileWriteAheadLogManager) grid.context().cache().context().wal();
boolean mmap = GridTestUtils.getFieldValue(wal, "mmap");
if (mmap)
return;
wal.setFileIOFactory(new FailingFileIOFactory(canFail));
try {
grid.active(true);
IgniteCache<Object, Object> cache = grid.cache(TEST_CACHE);
final int iterations = 100;
canFail.set(true);
for (int i = 0; i < iterations; i++) {
Transaction tx = grid.transactions().txStart(TransactionConcurrency.PESSIMISTIC, TransactionIsolation.READ_COMMITTED);
cache.put(i, "testValue" + i);
Thread.sleep(100L);
tx.commitAsync().get();
}
} catch (Exception expected) {
// There can be any exception. Do nothing.
}
// We should await successful stop of node.
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return grid.context().gateway().getState() == GridKernalState.STOPPED;
}
}, getTestTimeout());
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class IgniteWalFlushMultiNodeFailoverAbstractSelfTest method failWhilePut.
/**
* @throws Exception if failed.
*/
public void failWhilePut(boolean failWhileStart) throws Exception {
final Ignite grid = startGridsMultiThreaded(gridCount());
IgniteWriteAheadLogManager wal = ((IgniteKernal) grid).context().cache().context().wal();
boolean mmap = GridTestUtils.getFieldValue(wal, "mmap");
if (mmap)
return;
grid.active(true);
IgniteCache<Object, Object> cache = grid.cache(TEST_CACHE);
for (int i = 0; i < ITRS; i++) {
while (true) {
try (Transaction tx = grid.transactions().txStart(TransactionConcurrency.PESSIMISTIC, TransactionIsolation.READ_COMMITTED)) {
cache.put(i, "testValue" + i);
tx.commit();
break;
} catch (Exception expected) {
// Expected exception.
}
}
if (i == ITRS / 4) {
try {
if (failWhileStart)
canFail.set(true);
startGrid(gridCount());
FileWriteAheadLogManager wal0 = (FileWriteAheadLogManager) grid(gridCount()).context().cache().context().wal();
wal0.setFileIOFactory(new FailingFileIOFactory(canFail));
grid.cluster().setBaselineTopology(grid.cluster().topologyVersion());
waitForRebalancing();
} catch (Exception expected) {
// There can be any exception. Do nothing.
}
}
if (i == ITRS / 2)
canFail.set(true);
}
// We should await successful stop of node.
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return grid.cluster().nodes().size() == gridCount();
}
}, getTestTimeout());
stopAllGrids();
canFail.set(false);
Ignite grid0 = startGrids(gridCount() + 1);
FileWriteAheadLogManager wal0 = (FileWriteAheadLogManager) grid(gridCount()).context().cache().context().wal();
wal0.setFileIOFactory(new FailingFileIOFactory(canFail));
grid0.active(true);
cache = grid0.cache(TEST_CACHE);
for (int i = 0; i < ITRS; i++) assertEquals(cache.get(i), "testValue" + i);
}
Aggregations