use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class IgniteWalRecoveryTest method testRecoveryOnTransactionalAndPartitionedCache.
/**
* Test recovery from WAL on 3 nodes in case of transactional cache.
*
* @throws Exception If fail.
*/
public void testRecoveryOnTransactionalAndPartitionedCache() throws Exception {
IgniteEx ignite = (IgniteEx) startGrids(3);
ignite.active(true);
try {
final String cacheName = "transactional";
CacheConfiguration<Object, Object> cacheConfiguration = new CacheConfiguration<>(cacheName).setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL).setAffinity(new RendezvousAffinityFunction(false, 32)).setCacheMode(CacheMode.PARTITIONED).setRebalanceMode(CacheRebalanceMode.SYNC).setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC).setBackups(2);
ignite.createCache(cacheConfiguration);
IgniteCache<Object, Object> cache = ignite.cache(cacheName);
Map<Object, Object> map = new HashMap<>();
final int transactions = 100;
final int operationsPerTransaction = 40;
Random random = new Random();
for (int t = 1; t <= transactions; t++) {
Transaction tx = ignite.transactions().txStart(TransactionConcurrency.OPTIMISTIC, TransactionIsolation.READ_COMMITTED);
Map<Object, Object> changesInTransaction = new HashMap<>();
for (int op = 0; op < operationsPerTransaction; op++) {
int key = random.nextInt(1000) + 1;
Object value;
if (random.nextBoolean())
value = randomString(random) + key;
else
value = new BigObject(key);
changesInTransaction.put(key, value);
cache.put(key, value);
}
if (random.nextBoolean()) {
tx.commit();
map.putAll(changesInTransaction);
} else {
tx.rollback();
}
if (t % 50 == 0)
log.info("Finished transaction " + t);
}
stopAllGrids();
ignite = (IgniteEx) startGrids(3);
ignite.active(true);
cache = ignite.cache(cacheName);
for (Object key : map.keySet()) {
Object expectedValue = map.get(key);
Object actualValue = cache.get(key);
Assert.assertEquals("Unexpected value for key " + key, expectedValue, actualValue);
}
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class IgniteWalReaderTest method createCache2.
/**
* Creates and fills cache with data.
*
* @param ig Ignite instance.
* @param mode Cache Atomicity Mode.
*/
private void createCache2(Ignite ig, CacheAtomicityMode mode) {
if (log.isInfoEnabled())
log.info("Populating the cache...");
final CacheConfiguration<Integer, Organization> cfg = new CacheConfiguration<>("Org" + "11");
cfg.setAtomicityMode(mode);
final IgniteCache<Integer, Organization> cache = ig.getOrCreateCache(cfg).withKeepBinary();
try (Transaction tx = ig.transactions().txStart()) {
for (int i = 0; i < 10; i++) {
cache.put(i, new Organization(i, "Organization-" + i));
if (i % 2 == 0)
cache.put(i, new Organization(i, "Organization-updated-" + i));
if (i % 5 == 0)
cache.remove(i);
}
tx.commit();
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheContinuousQueryAsyncFilterListenerTest method testNonDeadLockInFilter.
/**
* @param ccfg Cache configuration.
* @param asyncFilter Async filter.
* @param asyncLsnr Async listener.
* @param jcacheApi Use JCache api for start update listener.
* @throws Exception If failed.
*/
private void testNonDeadLockInFilter(CacheConfiguration ccfg, final boolean asyncFilter, final boolean asyncLsnr, boolean jcacheApi) throws Exception {
ignite(0).createCache(ccfg);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
try {
for (int i = 0; i < ITERATION_CNT; i++) {
log.info("Start iteration: " + i);
int nodeIdx = i % NODES;
final String cacheName = ccfg.getName();
final IgniteCache cache = grid(nodeIdx).cache(cacheName);
final QueryTestKey key = NODES - 1 != nodeIdx ? affinityKey(cache) : new QueryTestKey(1);
final QueryTestValue val0 = new QueryTestValue(1);
final QueryTestValue newVal = new QueryTestValue(2);
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch evtFromLsnrLatch = new CountDownLatch(1);
IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> fltrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {
@Override
public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
if (asyncFilter) {
assertFalse("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("sys-"));
assertTrue("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("callback-"));
}
IgniteCache<Object, Object> cache0 = ignite.cache(cacheName);
QueryTestValue val = e.getValue();
if (val == null)
return;
else if (val.equals(newVal)) {
evtFromLsnrLatch.countDown();
return;
} else if (!val.equals(val0))
return;
Transaction tx = null;
try {
if (cache0.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL)
tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
assertEquals(val, val0);
cache0.put(key, newVal);
if (tx != null)
tx.commit();
latch.countDown();
} catch (Exception exp) {
log.error("Failed: ", exp);
throw new IgniteException(exp);
} finally {
if (tx != null)
tx.close();
}
}
};
IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> lsnrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {
@Override
public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
if (asyncLsnr) {
assertFalse("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("sys-"));
assertTrue("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("callback-"));
}
QueryTestValue val = e.getValue();
if (val == null || !val.equals(new QueryTestValue(1)))
return;
assertEquals(val, val0);
latch.countDown();
}
};
QueryCursor qry = null;
MutableCacheEntryListenerConfiguration<QueryTestKey, QueryTestValue> lsnrCfg = null;
CacheInvokeListener locLsnr = asyncLsnr ? new CacheInvokeListenerAsync(lsnrClsr) : new CacheInvokeListener(lsnrClsr);
CacheEntryEventSerializableFilter<QueryTestKey, QueryTestValue> rmtFltr = asyncFilter ? new CacheTestRemoteFilterAsync(fltrClsr) : new CacheTestRemoteFilter(fltrClsr);
if (jcacheApi) {
lsnrCfg = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(locLsnr), FactoryBuilder.factoryOf(rmtFltr), true, false);
cache.registerCacheEntryListener(lsnrCfg);
} else {
ContinuousQuery<QueryTestKey, QueryTestValue> conQry = new ContinuousQuery<>();
conQry.setLocalListener(locLsnr);
conQry.setRemoteFilterFactory(FactoryBuilder.factoryOf(rmtFltr));
qry = cache.query(conQry);
}
try {
if (rnd.nextBoolean())
cache.put(key, val0);
else
cache.invoke(key, new CacheEntryProcessor() {
@Override
public Object process(MutableEntry entry, Object... arguments) throws EntryProcessorException {
entry.setValue(val0);
return null;
}
});
assert U.await(latch, 3, SECONDS) : "Failed to waiting event.";
assertEquals(cache.get(key), new QueryTestValue(2));
assertTrue("Failed to waiting event from filter.", U.await(latch, 3, SECONDS));
} finally {
if (qry != null)
qry.close();
if (lsnrCfg != null)
cache.deregisterCacheEntryListener(lsnrCfg);
}
log.info("Iteration finished: " + i);
}
} finally {
ignite(0).destroyCache(ccfg.getName());
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheContinuousQueryAsyncFilterListenerTest method testNonDeadLockInListener.
/**
* @param ccfg Cache configuration.
* @param asyncFltr Async filter.
* @param asyncLsnr Async listener.
* @param jcacheApi Use JCache api for registration entry update listener.
* @throws Exception If failed.
*/
private void testNonDeadLockInListener(CacheConfiguration ccfg, final boolean asyncFltr, boolean asyncLsnr, boolean jcacheApi) throws Exception {
ignite(0).createCache(ccfg);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
try {
for (int i = 0; i < ITERATION_CNT; i++) {
log.info("Start iteration: " + i);
int nodeIdx = i % NODES;
final String cacheName = ccfg.getName();
final IgniteCache cache = grid(nodeIdx).cache(cacheName);
final QueryTestKey key = NODES - 1 != nodeIdx ? affinityKey(cache) : new QueryTestKey(1);
final QueryTestValue val0 = new QueryTestValue(1);
final QueryTestValue newVal = new QueryTestValue(2);
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch evtFromLsnrLatch = new CountDownLatch(1);
IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> fltrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {
@Override
public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
if (asyncFltr) {
assertFalse("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("sys-"));
assertTrue("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("callback-"));
}
}
};
IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> lsnrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {
@Override
public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
IgniteCache<Object, Object> cache0 = ignite.cache(cacheName);
QueryTestValue val = e.getValue();
if (val == null)
return;
else if (val.equals(newVal)) {
evtFromLsnrLatch.countDown();
return;
} else if (!val.equals(val0))
return;
Transaction tx = null;
try {
if (cache0.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL)
tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
assertEquals(val, val0);
cache0.put(key, newVal);
if (tx != null)
tx.commit();
latch.countDown();
} catch (Exception exp) {
log.error("Failed: ", exp);
throw new IgniteException(exp);
} finally {
if (tx != null)
tx.close();
}
}
};
QueryCursor qry = null;
MutableCacheEntryListenerConfiguration<QueryTestKey, QueryTestValue> lsnrCfg = null;
CacheInvokeListener locLsnr = asyncLsnr ? new CacheInvokeListenerAsync(lsnrClsr) : new CacheInvokeListener(lsnrClsr);
CacheEntryEventSerializableFilter<QueryTestKey, QueryTestValue> rmtFltr = asyncFltr ? new CacheTestRemoteFilterAsync(fltrClsr) : new CacheTestRemoteFilter(fltrClsr);
if (jcacheApi) {
lsnrCfg = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(locLsnr), FactoryBuilder.factoryOf(rmtFltr), true, false);
cache.registerCacheEntryListener(lsnrCfg);
} else {
ContinuousQuery<QueryTestKey, QueryTestValue> conQry = new ContinuousQuery<>();
conQry.setLocalListener(locLsnr);
conQry.setRemoteFilterFactory(FactoryBuilder.factoryOf(rmtFltr));
qry = cache.query(conQry);
}
try {
if (rnd.nextBoolean())
cache.put(key, val0);
else {
cache.invoke(key, new CacheEntryProcessor() {
@Override
public Object process(MutableEntry entry, Object... arguments) throws EntryProcessorException {
entry.setValue(val0);
return null;
}
});
}
assertTrue("Failed to waiting event.", U.await(latch, 3, SECONDS));
assertEquals(cache.get(key), new QueryTestValue(2));
assertTrue("Failed to waiting event from listener.", U.await(latch, 3, SECONDS));
} finally {
if (qry != null)
qry.close();
if (lsnrCfg != null)
cache.deregisterCacheEntryListener(lsnrCfg);
}
log.info("Iteration finished: " + i);
}
} finally {
ignite(0).destroyCache(ccfg.getName());
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheContinuousQueryFactoryFilterRandomOperationTest 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(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();
updatePartitionCounter(cache, key, partCntr);
waitAndCheckEvent(evtsQueues, partCntr, 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);
waitAndCheckEvent(evtsQueues, partCntr, affinity(cache), key, newVal, oldVal);
expData.put(key, newVal);
break;
}
case 2:
{
cache.remove(key);
if (tx != null)
tx.commit();
updatePartitionCounter(cache, key, partCntr);
waitAndCheckEvent(evtsQueues, partCntr, affinity(cache), key, null, oldVal);
expData.remove(key);
break;
}
case 3:
{
cache.getAndRemove(key);
if (tx != null)
tx.commit();
updatePartitionCounter(cache, key, partCntr);
waitAndCheckEvent(evtsQueues, partCntr, 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);
waitAndCheckEvent(evtsQueues, partCntr, 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);
waitAndCheckEvent(evtsQueues, partCntr, 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);
waitAndCheckEvent(evtsQueues, partCntr, 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);
waitAndCheckEvent(evtsQueues, partCntr, 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);
waitAndCheckEvent(evtsQueues, partCntr, 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);
waitAndCheckEvent(evtsQueues, partCntr, 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);
waitAndCheckEvent(evtsQueues, partCntr, 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;
}
default:
fail("Op:" + op);
}
} finally {
if (tx != null)
tx.close();
}
}
Aggregations