use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method txNoConflictContainsKey.
/**
* @param noVal If {@code true} there is no cache value when do update in tx.
* @throws Exception If failed.
*/
private void txNoConflictContainsKey(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 (Integer key : keys) {
log.info("Test key: " + key);
if (!noVal)
cache.put(key, -1);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean res = cache.containsKey(key);
assertEquals(!noVal, res);
updateKey(cache, key, 1);
tx.commit();
}
checkValue(key, 1, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean res = cache.containsKey(key);
assertTrue(res);
updateKey(cache, key, 2);
tx.commit();
}
checkValue(key, 2, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean res = cache.containsKey(key);
assertTrue(res);
tx.commit();
}
cache.remove(key);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean res = cache.containsKey(key);
assertFalse(res);
updateKey(cache, key, 3);
tx.commit();
}
checkValue(key, 3, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method getRemoveTx.
/**
* @param nearCache If {@code true} near cache is enabled.
* @param store If {@code true} cache store is enabled.
* @throws Exception If failed.
*/
private void getRemoveTx(boolean nearCache, boolean store) throws Exception {
long stopTime = U.currentTimeMillis() + getTestTimeout() - 30_000;
final Ignite ignite0 = ignite(0);
CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 0, store, false);
final List<Ignite> clients = clients();
final String cacheName = ignite0.createCache(ccfg).getName();
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));
}
for (int i = 0; i < 100; i++) {
if (U.currentTimeMillis() > stopTime)
break;
final AtomicInteger cntr = new AtomicInteger();
final Integer key = i;
final AtomicInteger threadIdx = new AtomicInteger();
final int THREADS = 10;
final CyclicBarrier barrier = new CyclicBarrier(THREADS);
final IgniteInternalFuture<?> updateFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
int thread = threadIdx.getAndIncrement();
int idx = thread % 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());
Thread.currentThread().setName("update-thread-" + ignite.name() + "-" + thread);
barrier.await();
for (int i = 0; i < 50; i++) {
while (true) {
try {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
boolean rmv = rnd.nextInt(3) == 0;
Integer val;
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
val = cache.get(key);
if (rmv)
cache.remove(key);
else
cache.put(key, val == null ? 1 : val + 1);
tx.commit();
if (rmv) {
if (val != null) {
for (int j = 0; j < val; j++) cntr.decrementAndGet();
}
} else
cntr.incrementAndGet();
}
break;
} catch (TransactionOptimisticException ignore) {
// Retry.
}
}
}
return null;
}
}, THREADS, "update-thread");
updateFut.get();
Integer val = cntr.get();
log.info("Iteration [iter=" + i + ", val=" + val + ']');
checkValue(key, val == 0 ? null : val, cacheName);
}
} finally {
destroyCache(cacheName);
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method testMultipleOptimisticRead.
/**
* Multithreaded transactional reads.
*
* @throws Exception If failed.
*/
public void testMultipleOptimisticRead() throws Exception {
final Ignite ignite = ignite(0);
final Integer key = 1;
final Integer val = 1;
final int THREADS_CNT = 50;
final String cacheName = ignite.createCache(cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false)).getName();
try {
final IgniteCache<Integer, Integer> cache = ignite.cache(cacheName);
try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(key, val);
tx.commit();
}
assertTrue(cache.get(key).equals(val));
for (int i = 0; i < 10; i++) {
GridTestUtils.runMultiThreadedAsync(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));
tx.commit();
}
return null;
}
}, THREADS_CNT, "multiple-reads-thread").get();
}
} finally {
destroyCache(cacheName);
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheLateAffinityAssignmentTest method cacheOperations.
/**
* @param cache Cache
*/
private void cacheOperations(IgniteCache<Object, Object> cache) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
final int KEYS = 10_000;
try {
cache.get(rnd.nextInt(KEYS));
cache.put(rnd.nextInt(KEYS), rnd.nextInt(10));
cache.getAndPut(rnd.nextInt(KEYS), rnd.nextInt(10));
cache.remove(rnd.nextInt(KEYS));
cache.getAndRemove(rnd.nextInt(KEYS));
cache.remove(rnd.nextInt(KEYS), rnd.nextInt(10));
cache.putIfAbsent(rnd.nextInt(KEYS), rnd.nextInt(10));
cache.replace(rnd.nextInt(KEYS), rnd.nextInt(10));
cache.replace(rnd.nextInt(KEYS), rnd.nextInt(10), rnd.nextInt(10));
cache.invoke(rnd.nextInt(KEYS), new TestEntryProcessor(rnd.nextInt(10)));
if (cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL) {
IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
for (TransactionIsolation isolation : TransactionIsolation.values()) {
try (Transaction tx = txs.txStart(concurrency, isolation)) {
Integer key = rnd.nextInt(KEYS);
cache.getAndPut(key, rnd.nextInt(10));
cache.invoke(key + 1, new TestEntryProcessor(rnd.nextInt(10)));
cache.get(key + 2);
tx.commit();
}
}
}
}
} catch (Exception e) {
log.info("Cache operation failed: " + e);
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class GridCacheAbstractNodeRestartSelfTest method checkRestartWithTx.
/**
* @param duration Test duration.
* @param putThreads Put threads count.
* @param restartThreads Restart threads count.
* @param txKeys Keys per transaction.
* @throws Exception If failed.
*/
private void checkRestartWithTx(long duration, int putThreads, int restartThreads, final int txKeys) throws Throwable {
if (atomicityMode() == ATOMIC)
return;
final long endTime = System.currentTimeMillis() + duration;
final AtomicReference<Throwable> err = new AtomicReference<>();
startGrids();
Collection<Thread> threads = new LinkedList<>();
try {
final AtomicInteger txCntr = new AtomicInteger();
final CyclicBarrier barrier = new CyclicBarrier(putThreads + restartThreads);
for (int i = 0; i < putThreads; i++) {
final int gridIdx = i;
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
barrier.await();
info("Starting put thread: " + gridIdx);
Ignite ignite = grid(gridIdx);
Thread.currentThread().setName("put-worker-" + ignite.name());
UUID locNodeId = ignite.cluster().localNode().id();
IgniteCache<Integer, String> cache = ignite.cache(CACHE_NAME);
List<Integer> keys = new ArrayList<>(txKeys);
while (System.currentTimeMillis() < endTime && err.get() == null) {
keys.clear();
for (int i = 0; i < txKeys; i++) keys.add(RAND.nextInt(keyCnt));
// Ensure lock order.
Collections.sort(keys);
int c = 0;
try {
IgniteTransactions txs = ignite.transactions();
try (Transaction tx = txs.txStart(txConcurrency(), REPEATABLE_READ)) {
c = txCntr.incrementAndGet();
if (c % LOG_FREQ == 0) {
info(">>> Tx iteration started [cnt=" + c + ", keys=" + keys + ", locNodeId=" + locNodeId + ']');
}
for (int key : keys) {
int op = cacheOp();
if (op == 1)
cache.put(key, Integer.toString(key));
else if (op == 2)
cache.remove(key);
else
cache.get(key);
}
tx.commit();
}
} catch (IgniteException | CacheException ignored) {
// It is ok if primary node leaves grid.
}
if (c % LOG_FREQ == 0) {
info(">>> Tx iteration finished [cnt=" + c + ", cacheSize=" + cache.localSize() + ", keys=" + keys + ", locNodeId=" + locNodeId + ']');
}
}
info(">>> " + Thread.currentThread().getName() + " finished.");
} catch (Exception e) {
err.compareAndSet(null, e);
error("Unexpected exception in put-worker.", e);
}
}
}, "put-worker-" + i);
t.start();
threads.add(t);
}
for (int i = 0; i < restartThreads; i++) {
final int gridIdx = i + putThreads;
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
barrier.await();
info("Starting restart thread: " + gridIdx);
int cnt = 0;
while (System.currentTimeMillis() < endTime && err.get() == null) {
stopGrid(getTestIgniteInstanceName(gridIdx), false, false);
startGrid(gridIdx);
int c = ++cnt;
if (c % LOG_FREQ == 0)
info(">>> Restart iteration: " + c);
}
info(">>> " + Thread.currentThread().getName() + " finished.");
} catch (Exception e) {
err.compareAndSet(null, e);
error("Unexpected exception in restart-worker.", e);
}
}
}, "restart-worker-" + i);
t.start();
threads.add(t);
}
for (Thread t : threads) t.join();
if (err.get() != null)
throw err.get();
} finally {
stopAllGrids();
}
}
Aggregations