use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheContinuousQueryOperationFromCallbackTest method doTest.
/**
* @param ccfg Cache configuration.
* @throws Exception If failed.
*/
protected void doTest(final CacheConfiguration ccfg, boolean fromLsnr) throws Exception {
ignite(0).createCache(ccfg);
List<QueryCursor<?>> qries = new ArrayList<>();
assertEquals(0, filterCbCntr.get());
try {
List<Set<T2<QueryTestKey, QueryTestValue>>> rcvdEvts = new ArrayList<>(NODES);
List<Set<T2<QueryTestKey, QueryTestValue>>> evtsFromCallbacks = new ArrayList<>(NODES);
final AtomicInteger qryCntr = new AtomicInteger(0);
final AtomicInteger cbCntr = new AtomicInteger(0);
final int threadCnt = SYSTEM_POOL_SIZE * 2;
for (int idx = 0; idx < NODES; idx++) {
Set<T2<QueryTestKey, QueryTestValue>> evts = Collections.newSetFromMap(new ConcurrentHashMap<T2<QueryTestKey, QueryTestValue>, Boolean>());
Set<T2<QueryTestKey, QueryTestValue>> evtsFromCb = Collections.newSetFromMap(new ConcurrentHashMap<T2<QueryTestKey, QueryTestValue>, Boolean>());
IgniteCache<Object, Object> cache = grid(idx).getOrCreateCache(ccfg.getName());
ContinuousQuery qry = new ContinuousQuery();
qry.setLocalListener(new TestCacheAsyncEventListener(evts, evtsFromCb, fromLsnr ? cache : null, qryCntr, cbCntr));
if (!fromLsnr)
qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheTestRemoteFilterAsync(ccfg.getName())));
rcvdEvts.add(evts);
evtsFromCallbacks.add(evtsFromCb);
QueryCursor qryCursor = cache.query(qry);
qries.add(qryCursor);
}
IgniteInternalFuture<Long> f = GridTestUtils.runMultiThreadedAsync(new Runnable() {
@Override
public void run() {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = 0; i < ITERATION_CNT; i++) {
IgniteCache<QueryTestKey, QueryTestValue> cache = grid(rnd.nextInt(NODES)).cache(ccfg.getName());
QueryTestKey key = new QueryTestKey(rnd.nextInt(KEYS));
boolean startTx = cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL && rnd.nextBoolean();
Transaction tx = null;
if (startTx)
tx = cache.unwrap(Ignite.class).transactions().txStart();
try {
if ((cache.get(key) == null) || rnd.nextBoolean())
cache.invoke(key, new IncrementTestEntryProcessor());
else {
QueryTestValue val;
QueryTestValue newVal;
do {
val = cache.get(key);
newVal = val == null ? new QueryTestValue(0) : new QueryTestValue(val.val1 + 1);
} while (!cache.replace(key, val, newVal));
}
} finally {
if (tx != null)
tx.commit();
}
}
}
}, threadCnt, "put-thread");
f.get(30, TimeUnit.SECONDS);
assert GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return qryCntr.get() >= ITERATION_CNT * threadCnt * NODES;
}
}, TimeUnit.MINUTES.toMillis(2));
for (Set<T2<QueryTestKey, QueryTestValue>> set : rcvdEvts) checkEvents(set, ITERATION_CNT * threadCnt, grid(0).cache(ccfg.getName()), false);
if (fromLsnr) {
final int expCnt = qryCntr.get() * NODES * KEYS_FROM_CALLBACK;
boolean res = GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return cbCntr.get() >= expCnt;
}
}, TimeUnit.SECONDS.toMillis(60));
assertTrue("Failed to wait events [exp=" + expCnt + ", act=" + cbCntr.get() + "]", res);
assertEquals(expCnt, cbCntr.get());
for (Set<T2<QueryTestKey, QueryTestValue>> set : evtsFromCallbacks) checkEvents(set, qryCntr.get() * KEYS_FROM_CALLBACK, grid(0).cache(ccfg.getName()), true);
} else {
final int expInvkCnt = ITERATION_CNT * threadCnt * (ccfg.getCacheMode() != REPLICATED ? (ccfg.getBackups() + 1) : NODES - 1) * NODES;
GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
return filterCbCntr.get() >= expInvkCnt;
}
}, TimeUnit.SECONDS.toMillis(60));
assertEquals(expInvkCnt, filterCbCntr.get());
for (Set<T2<QueryTestKey, QueryTestValue>> set : evtsFromCallbacks) checkEvents(set, expInvkCnt * KEYS_FROM_CALLBACK, grid(0).cache(ccfg.getName()), true);
}
} finally {
for (QueryCursor<?> qry : qries) qry.close();
ignite(0).destroyCache(ccfg.getName());
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheContinuousQueryRandomOperationsTest method randomUpdate.
/**
* @param rnd Random generator.
* @param evtsQueues Events queue.
* @param expData Expected cache data.
* @param partCntr Partition counter.
* @param cache Cache.
* @throws Exception If failed.
*/
private void randomUpdate(Random rnd, List<BlockingQueue<CacheEntryEvent<?, ?>>> evtsQueues, ConcurrentMap<Object, Object> expData, Map<Integer, Long> partCntr, IgniteCache<Object, Object> cache) throws Exception {
Object key = new QueryTestKey(rnd.nextInt(KEYS));
Object newVal = value(rnd);
Object oldVal = expData.get(key);
int op = rnd.nextInt(13);
Ignite ignite = cache.unwrap(Ignite.class);
Map<Object, Long> expEvtCntrs = new ConcurrentHashMap<>();
Transaction tx = null;
if (cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL && rnd.nextBoolean())
tx = ignite.transactions().txStart(txRandomConcurrency(rnd), txRandomIsolation(rnd));
try {
switch(op) {
case 0:
{
cache.put(key, newVal);
if (tx != null)
tx.commit();
updatePartitionCounter(cache, key, partCntr, expEvtCntrs);
waitAndCheckEvent(evtsQueues, partCntr, expEvtCntrs, affinity(cache), key, newVal, oldVal);
expData.put(key, newVal);
break;
}
case 1:
{
cache.getAndPut(key, newVal);
if (tx != null)
tx.commit();
updatePartitionCounter(cache, key, partCntr, expEvtCntrs);
waitAndCheckEvent(evtsQueues, partCntr, expEvtCntrs, affinity(cache), key, newVal, oldVal);
expData.put(key, newVal);
break;
}
case 2:
{
cache.remove(key);
if (tx != null)
tx.commit();
updatePartitionCounter(cache, key, partCntr, expEvtCntrs);
waitAndCheckEvent(evtsQueues, partCntr, expEvtCntrs, affinity(cache), key, null, oldVal);
expData.remove(key);
break;
}
case 3:
{
cache.getAndRemove(key);
if (tx != null)
tx.commit();
updatePartitionCounter(cache, key, partCntr, expEvtCntrs);
waitAndCheckEvent(evtsQueues, partCntr, expEvtCntrs, affinity(cache), key, null, oldVal);
expData.remove(key);
break;
}
case 4:
{
cache.invoke(key, new EntrySetValueProcessor(newVal, rnd.nextBoolean()));
if (tx != null)
tx.commit();
updatePartitionCounter(cache, key, partCntr, expEvtCntrs);
waitAndCheckEvent(evtsQueues, partCntr, expEvtCntrs, affinity(cache), key, newVal, oldVal);
expData.put(key, newVal);
break;
}
case 5:
{
cache.invoke(key, new EntrySetValueProcessor(null, rnd.nextBoolean()));
if (tx != null)
tx.commit();
updatePartitionCounter(cache, key, partCntr, expEvtCntrs);
waitAndCheckEvent(evtsQueues, partCntr, expEvtCntrs, affinity(cache), key, null, oldVal);
expData.remove(key);
break;
}
case 6:
{
cache.putIfAbsent(key, newVal);
if (tx != null)
tx.commit();
if (oldVal == null) {
updatePartitionCounter(cache, key, partCntr, expEvtCntrs);
waitAndCheckEvent(evtsQueues, partCntr, expEvtCntrs, affinity(cache), key, newVal, null);
expData.put(key, newVal);
} else
checkNoEvent(evtsQueues);
break;
}
case 7:
{
cache.getAndPutIfAbsent(key, newVal);
if (tx != null)
tx.commit();
if (oldVal == null) {
updatePartitionCounter(cache, key, partCntr, expEvtCntrs);
waitAndCheckEvent(evtsQueues, partCntr, expEvtCntrs, affinity(cache), key, newVal, null);
expData.put(key, newVal);
} else
checkNoEvent(evtsQueues);
break;
}
case 8:
{
cache.replace(key, newVal);
if (tx != null)
tx.commit();
if (oldVal != null) {
updatePartitionCounter(cache, key, partCntr, expEvtCntrs);
waitAndCheckEvent(evtsQueues, partCntr, expEvtCntrs, affinity(cache), key, newVal, oldVal);
expData.put(key, newVal);
} else
checkNoEvent(evtsQueues);
break;
}
case 9:
{
cache.getAndReplace(key, newVal);
if (tx != null)
tx.commit();
if (oldVal != null) {
updatePartitionCounter(cache, key, partCntr, expEvtCntrs);
waitAndCheckEvent(evtsQueues, partCntr, expEvtCntrs, affinity(cache), key, newVal, oldVal);
expData.put(key, newVal);
} else
checkNoEvent(evtsQueues);
break;
}
case 10:
{
if (oldVal != null) {
Object replaceVal = value(rnd);
boolean success = replaceVal.equals(oldVal);
if (success) {
cache.replace(key, replaceVal, newVal);
if (tx != null)
tx.commit();
updatePartitionCounter(cache, key, partCntr, expEvtCntrs);
waitAndCheckEvent(evtsQueues, partCntr, expEvtCntrs, affinity(cache), key, newVal, oldVal);
expData.put(key, newVal);
} else {
cache.replace(key, replaceVal, newVal);
if (tx != null)
tx.commit();
checkNoEvent(evtsQueues);
}
} else {
cache.replace(key, value(rnd), newVal);
if (tx != null)
tx.commit();
checkNoEvent(evtsQueues);
}
break;
}
case 11:
{
SortedMap<Object, Object> vals = new TreeMap<>();
while (vals.size() < KEYS / 5) vals.put(new QueryTestKey(rnd.nextInt(KEYS)), value(rnd));
cache.putAll(vals);
if (tx != null)
tx.commit();
for (Map.Entry<Object, Object> e : vals.entrySet()) updatePartitionCounter(cache, e.getKey(), partCntr, expEvtCntrs);
waitAndCheckEvent(evtsQueues, partCntr, expEvtCntrs, affinity(cache), vals, expData);
expData.putAll(vals);
break;
}
case 12:
{
SortedMap<Object, Object> vals = new TreeMap<>();
while (vals.size() < KEYS / 5) vals.put(new QueryTestKey(rnd.nextInt(KEYS)), newVal);
cache.invokeAll(vals.keySet(), new EntrySetValueProcessor(newVal, rnd.nextBoolean()));
if (tx != null)
tx.commit();
for (Map.Entry<Object, Object> e : vals.entrySet()) updatePartitionCounter(cache, e.getKey(), partCntr, expEvtCntrs);
waitAndCheckEvent(evtsQueues, partCntr, expEvtCntrs, affinity(cache), vals, expData);
for (Object o : vals.keySet()) expData.put(o, newVal);
break;
}
default:
fail("Op:" + op);
}
} finally {
if (tx != null)
tx.close();
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheContinuousQueryVariationsTest method randomUpdate.
/**
* @param rnd Random generator.
* @param evtsQueues Events queue.
* @param expData Expected cache data.
* @param cache Cache.
* @throws Exception If failed.
*/
private void randomUpdate(Random rnd, List<BlockingQueue<CacheEntryEvent<?, ?>>> evtsQueues, ConcurrentMap<Object, Object> expData, IgniteCache<Object, Object> cache, boolean keepBinary, boolean withFilter) throws Exception {
Object key = key(rnd.nextInt(KEYS));
Object newVal = value(rnd.nextInt());
Object oldVal = expData.get(key);
int op = rnd.nextInt(11);
Ignite ignite = cache.unwrap(Ignite.class);
Transaction tx = null;
if (cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL && rnd.nextBoolean())
tx = ignite.transactions().txStart(txRandomConcurrency(rnd), txRandomIsolation(rnd));
try {
switch(op) {
case 0:
{
cache.put(key, newVal);
if (tx != null)
tx.commit();
waitAndCheckEvent(evtsQueues, key, newVal, oldVal, keepBinary, withFilter);
expData.put(key, newVal);
break;
}
case 1:
{
cache.getAndPut(key, newVal);
if (tx != null)
tx.commit();
waitAndCheckEvent(evtsQueues, key, newVal, oldVal, keepBinary, withFilter);
expData.put(key, newVal);
break;
}
case 2:
{
cache.remove(key);
if (tx != null)
tx.commit();
waitAndCheckEvent(evtsQueues, key, null, oldVal, keepBinary, withFilter);
expData.remove(key);
break;
}
case 3:
{
cache.getAndRemove(key);
if (tx != null)
tx.commit();
waitAndCheckEvent(evtsQueues, key, null, oldVal, keepBinary, withFilter);
expData.remove(key);
break;
}
case 4:
{
cache.invoke(key, new EntrySetValueProcessor(newVal, rnd.nextBoolean()));
if (tx != null)
tx.commit();
waitAndCheckEvent(evtsQueues, key, newVal, oldVal, keepBinary, withFilter);
expData.put(key, newVal);
break;
}
case 5:
{
cache.invoke(key, new EntrySetValueProcessor(null, rnd.nextBoolean()));
if (tx != null)
tx.commit();
waitAndCheckEvent(evtsQueues, key, null, oldVal, keepBinary, withFilter);
expData.remove(key);
break;
}
case 6:
{
cache.putIfAbsent(key, newVal);
if (tx != null)
tx.commit();
if (oldVal == null) {
waitAndCheckEvent(evtsQueues, key, newVal, null, keepBinary, withFilter);
expData.put(key, newVal);
} else
checkNoEvent(evtsQueues);
break;
}
case 7:
{
cache.getAndPutIfAbsent(key, newVal);
if (tx != null)
tx.commit();
if (oldVal == null) {
waitAndCheckEvent(evtsQueues, key, newVal, null, keepBinary, withFilter);
expData.put(key, newVal);
} else
checkNoEvent(evtsQueues);
break;
}
case 8:
{
cache.replace(key, newVal);
if (tx != null)
tx.commit();
if (oldVal != null) {
waitAndCheckEvent(evtsQueues, key, newVal, oldVal, keepBinary, withFilter);
expData.put(key, newVal);
} else
checkNoEvent(evtsQueues);
break;
}
case 9:
{
cache.getAndReplace(key, newVal);
if (tx != null)
tx.commit();
if (oldVal != null) {
waitAndCheckEvent(evtsQueues, key, newVal, oldVal, keepBinary, withFilter);
expData.put(key, newVal);
} else
checkNoEvent(evtsQueues);
break;
}
case 10:
{
if (oldVal != null) {
Object replaceVal = value(rnd.nextInt(VALS));
boolean success = replaceVal.equals(oldVal);
if (success) {
cache.replace(key, replaceVal, newVal);
if (tx != null)
tx.commit();
waitAndCheckEvent(evtsQueues, key, newVal, oldVal, keepBinary, withFilter);
expData.put(key, newVal);
} else {
cache.replace(key, replaceVal, newVal);
if (tx != null)
tx.commit();
checkNoEvent(evtsQueues);
}
} else {
cache.replace(key, value(rnd.nextInt(VALS)), newVal);
if (tx != null)
tx.commit();
checkNoEvent(evtsQueues);
}
break;
}
default:
fail("Op:" + op);
}
} finally {
if (tx != null)
tx.close();
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class IgnitePdsTransactionsHangTest method testTransactionsDontHang.
/**
* Copied from customers benchmark.
*
* @throws Exception If failed.
*/
public void testTransactionsDontHang() throws Exception {
try {
final Ignite g = startGrids(2);
g.active(true);
g.getOrCreateCache(getCacheConfiguration());
ExecutorService threadPool = Executors.newFixedThreadPool(THREADS_CNT);
final CyclicBarrier cyclicBarrier = new CyclicBarrier(THREADS_CNT);
final AtomicBoolean interrupt = new AtomicBoolean(false);
final LongAdder operationCnt = new LongAdder();
final IgniteCache<Long, TestEntity> cache = g.cache(CACHE_NAME);
for (int i = 0; i < THREADS_CNT; i++) {
threadPool.submit(new Runnable() {
@Override
public void run() {
try {
ThreadLocalRandom locRandom = ThreadLocalRandom.current();
cyclicBarrier.await();
while (!interrupt.get()) {
long randomKey = locRandom.nextLong(MAX_KEY_COUNT);
TestEntity entity = TestEntity.newTestEntity(locRandom);
try (Transaction tx = g.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache.put(randomKey, entity);
tx.commit();
}
operationCnt.increment();
}
} catch (Throwable e) {
log.error("Unexpected exception:", e);
throw new RuntimeException(e);
}
}
});
}
long stopTime = System.currentTimeMillis() + DURATION * 1000;
long totalOperations = 0;
int periods = 0;
long max = Long.MIN_VALUE, min = Long.MAX_VALUE;
while (System.currentTimeMillis() < stopTime) {
U.sleep(1000);
long sum = operationCnt.sumThenReset();
periods++;
if (periods > WARM_UP_PERIOD) {
totalOperations += sum;
max = Math.max(max, sum);
min = Math.min(min, sum);
log.info("Operation count: " + sum + " min=" + min + " max=" + max + " avg=" + totalOperations / (periods - WARM_UP_PERIOD));
}
}
interrupt.set(true);
threadPool.shutdown();
log.info("Test complete");
threadPool.awaitTermination(getTestTimeout(), TimeUnit.MILLISECONDS);
IgniteTxManager tm = internalCache(cache).context().tm();
assertEquals("There are still active transactions", 0, tm.activeTransactions().size());
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class IgniteCacheNearOnlyTxTest method testConcurrentTx.
/**
* @throws Exception If failed.
*/
public void testConcurrentTx() throws Exception {
final Ignite ignite1 = ignite(1);
assertTrue(ignite1.configuration().isClientMode());
ignite1.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<>());
final Integer key = 1;
IgniteInternalFuture<?> fut1 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
IgniteCache<Integer, Integer> cache = ignite1.cache(DEFAULT_CACHE_NAME);
for (int i = 0; i < 100; i++) cache.put(key, 1);
return null;
}
}, 5, "put1-thread");
IgniteInternalFuture<?> fut2 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
IgniteCache<Integer, Integer> cache = ignite1.cache(DEFAULT_CACHE_NAME);
IgniteTransactions txs = ignite1.transactions();
for (int i = 0; i < 100; i++) {
try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache.get(key);
cache.put(key, 1);
tx.commit();
}
}
return null;
}
}, 5, "put2-thread");
fut1.get();
fut2.get();
}
Aggregations