use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheKeepBinaryTransactionTest method testBinaryContains.
/**
* @throws Exception If failed.
*/
public void testBinaryContains() throws Exception {
IgniteEx ignite = grid(0);
IgniteCache<Object, Object> cache = ignite.cache("tx-cache").withKeepBinary();
try (Transaction tx = ignite.transactions().txStart()) {
BinaryObject key = ignite.binary().builder("test2").setField("id", 1).build();
assertFalse(cache.containsKey(key));
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxConflictGetAndPutIfAbsent.
/**
* @throws Exception If failed.
*/
public void testTxConflictGetAndPutIfAbsent() 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)) {
Object old = cache.getAndPutIfAbsent(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.getAndPutIfAbsent(key, 2);
assertEquals(1, old);
tx.commit();
}
checkValue(key, 1, cache.getName());
cache.remove(key);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Object old = cache.getAndPutIfAbsent(key, 2);
assertNull(old);
tx.commit();
}
checkValue(key, 2, cache.getName());
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Object old = cache.getAndPutIfAbsent(key, 4);
assertEquals(2, old);
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 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 incrementTx.
/**
* @param nearCache If {@code true} near cache is enabled.
* @param store If {@code true} cache store is enabled.
* @param restart If {@code true} restarts one node.
* @throws Exception If failed.
*/
private void incrementTx(boolean nearCache, boolean store, final boolean restart) throws Exception {
final Ignite srv = ignite(1);
CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, store, false);
final List<Ignite> clients = clients();
final String cacheName = srv.createCache(ccfg).getName();
final AtomicBoolean stop = new AtomicBoolean();
try {
final List<IgniteCache<Integer, Integer>> caches = new ArrayList<>();
for (Ignite client : clients) {
if (nearCache)
caches.add(client.createNearCache(cacheName, new NearCacheConfiguration<Integer, Integer>()));
else
caches.add(client.<Integer, Integer>cache(cacheName));
}
IgniteInternalFuture<?> restartFut = restart ? restartFuture(stop, null) : null;
final long stopTime = U.currentTimeMillis() + getTestTimeout() - 30_000;
for (int i = 0; i < 30; i++) {
final AtomicInteger cntr = new AtomicInteger();
final Integer key = i;
final AtomicInteger threadIdx = new AtomicInteger();
final int THREADS = 10;
final CyclicBarrier barrier = new CyclicBarrier(THREADS);
GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
int idx = threadIdx.getAndIncrement() % caches.size();
IgniteCache<Integer, Integer> cache = caches.get(idx);
Ignite ignite = cache.unwrap(Ignite.class);
IgniteTransactions txs = ignite.transactions();
log.info("Started update thread: " + ignite.name());
barrier.await();
for (int i = 0; i < 1000; i++) {
if (i % 100 == 0 && U.currentTimeMillis() > stopTime)
break;
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache.get(key);
cache.put(key, val == null ? 1 : val + 1);
tx.commit();
}
cntr.incrementAndGet();
} catch (TransactionOptimisticException ignore) {
// Retry.
} catch (IgniteException | CacheException e) {
assertTrue("Unexpected exception [err=" + e + ", cause=" + e.getCause() + ']', restart && X.hasCause(e, ClusterTopologyCheckedException.class));
}
}
return null;
}
}, THREADS, "update-thread").get();
log.info("Iteration [iter=" + i + ", val=" + cntr.get() + ']');
assertTrue(cntr.get() > 0);
checkValue(key, cntr.get(), cacheName, restart);
if (U.currentTimeMillis() > stopTime)
break;
}
stop.set(true);
if (restartFut != null)
restartFut.get();
} finally {
stop.set(true);
destroyCache(cacheName);
}
}
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());
}
}
}
Aggregations