use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class GridCacheFinishPartitionsSelfTest method testMvccFinishKeys.
/**
* Tests finish future for particular set of keys.
*
* @throws Exception If failed.
*/
public void testMvccFinishKeys() throws Exception {
IgniteCache<String, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
try (Transaction tx = grid(0).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
final String key = "key";
cache.get(key);
GridCacheAdapter<String, Integer> internal = grid.internalCache(DEFAULT_CACHE_NAME);
KeyCacheObject cacheKey = internal.context().toCacheKeyObject(key);
IgniteInternalFuture<?> nearFut = internal.context().mvcc().finishKeys(Collections.singletonList(cacheKey), internal.context().cacheId(), new AffinityTopologyVersion(2));
IgniteInternalFuture<?> dhtFut = internal.context().near().dht().context().mvcc().finishKeys(Collections.singletonList(cacheKey), internal.context().cacheId(), new AffinityTopologyVersion(2));
assert !nearFut.isDone();
assert !dhtFut.isDone();
tx.commit();
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class GridCacheMarshallingNodeJoinSelfTest method testNodeJoin.
/**
* @throws Exception If failed.
*/
public void testNodeJoin() throws Exception {
final CountDownLatch allowJoin = new CountDownLatch(1);
final CountDownLatch joined = new CountDownLatch(2);
for (int i = 0; i < 2; i++) {
ignite(i).events().localListen(new PE() {
@Override
public boolean apply(Event evt) {
assert evt.type() == EventType.EVT_NODE_JOINED;
info(">>> Event: " + evt);
joined.countDown();
return true;
}
}, EventType.EVT_NODE_JOINED);
}
IgniteInternalFuture<?> oneMoreGrid = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
allowJoin.await();
startGrid("oneMoreGrid");
return null;
}
}, 1);
IgniteCache<Integer, TestObject> cache = ignite(0).cache(DEFAULT_CACHE_NAME);
try (Transaction tx = ignite(0).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache.get(0);
allowJoin.countDown();
joined.await();
assertNotNull(cache.get(1));
tx.commit();
}
oneMoreGrid.get();
assertNotNull(cache.get(1));
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class GridCacheMultiUpdateLockSelfTest method checkMultiUpdateLocks.
/**
* @param nearEnabled Near enabled flag.
* @throws Exception If failed.
*/
private void checkMultiUpdateLocks(boolean nearEnabled) throws Exception {
this.nearEnabled = nearEnabled;
startGrids(3);
try {
IgniteKernal g = (IgniteKernal) grid(0);
GridCacheContext<Object, Object> cctx = g.internalCache(DEFAULT_CACHE_NAME).context();
GridDhtCacheAdapter cache = nearEnabled ? cctx.near().dht() : cctx.colocated();
AffinityTopologyVersion topVer = cache.beginMultiUpdate();
IgniteInternalFuture<?> startFut;
try {
assertEquals(3, topVer.topologyVersion());
final AtomicBoolean started = new AtomicBoolean();
startFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
info(">>>> Starting grid.");
Ignite g4 = startGrid(4);
started.set(true);
IgniteCache<Object, Object> c = g4.cache(DEFAULT_CACHE_NAME);
info(">>>> Checking tx in new grid.");
try (Transaction tx = g4.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
assertEquals(2, c.get("a"));
assertEquals(4, c.get("b"));
assertEquals(6, c.get("c"));
}
return null;
}
}, 1);
U.sleep(200);
info(">>>> Checking grid has not started yet.");
assertFalse(started.get());
// Check we can proceed with transactions.
IgniteCache<Object, Object> cache0 = g.cache(DEFAULT_CACHE_NAME);
info(">>>> Checking tx commit.");
Transaction tx = g.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
try {
cache0.put("a", 1);
cache0.put("b", 2);
cache0.put("c", 3);
tx.commit();
} finally {
tx.close();
}
info(">>>> Checking grid still is not started");
assertFalse(started.get());
tx = g.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
try {
cache0.put("a", 2);
cache0.put("b", 4);
cache0.put("c", 6);
tx.commit();
} finally {
tx.close();
}
} finally {
info(">>>> Releasing multi update.");
cache.endMultiUpdate();
}
info("Waiting for thread termination.");
startFut.get();
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheGetEntryAbstractTest method test.
/**
* @param cfg Cache configuration.
* @param oneEntry If {@code true} then single entry is tested.
* @throws Exception If failed.
*/
private void test(CacheConfiguration cfg, final boolean oneEntry) throws Exception {
final IgniteCache<Integer, TestValue> cache = grid(0).createCache(cfg);
try {
init(cache);
test(cache, null, null, null, oneEntry);
if (cfg.getAtomicityMode() == TRANSACTIONAL) {
TransactionConcurrency txConcurrency = concurrency();
TransactionIsolation txIsolation = isolation();
try (Transaction tx = grid(0).transactions().txStart(txConcurrency, txIsolation)) {
initTx(cache);
test(cache, txConcurrency, txIsolation, tx, oneEntry);
tx.commit();
}
testConcurrentTx(cache, OPTIMISTIC, REPEATABLE_READ, oneEntry);
testConcurrentTx(cache, OPTIMISTIC, READ_COMMITTED, oneEntry);
testConcurrentTx(cache, PESSIMISTIC, REPEATABLE_READ, oneEntry);
testConcurrentTx(cache, PESSIMISTIC, READ_COMMITTED, oneEntry);
testConcurrentOptimisticTxGet(cache, REPEATABLE_READ);
testConcurrentOptimisticTxGet(cache, READ_COMMITTED);
testConcurrentOptimisticTxGet(cache, SERIALIZABLE);
}
} finally {
cache.destroy();
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheInterceptorPartitionCounterLocalSanityTest method randomUpdate.
/**
* @param rnd Random generator.
* @param expData Expected cache data.
* @param cache Cache.
* @throws Exception If failed.
*/
private void randomUpdate(Random rnd, ConcurrentMap<Object, Object> expData, IgniteCache<Object, Object> cache) throws Exception {
Object key = new TestKey(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();
waitAndCheckEvent(key, newVal, oldVal, false);
expData.put(key, newVal);
break;
}
case 1:
{
cache.getAndPut(key, newVal);
if (tx != null)
tx.commit();
waitAndCheckEvent(key, newVal, oldVal, false);
expData.put(key, newVal);
break;
}
case 2:
{
cache.remove(key);
if (tx != null)
tx.commit();
waitAndCheckEvent(key, null, oldVal, true);
expData.remove(key);
break;
}
case 3:
{
cache.getAndRemove(key);
if (tx != null)
tx.commit();
waitAndCheckEvent(key, null, oldVal, true);
expData.remove(key);
break;
}
case 4:
{
cache.invoke(key, new EntrySetValueProcessor(newVal, rnd.nextBoolean()));
if (tx != null)
tx.commit();
waitAndCheckEvent(key, newVal, oldVal, false);
expData.put(key, newVal);
break;
}
case 5:
{
cache.invoke(key, new EntrySetValueProcessor(null, rnd.nextBoolean()));
if (tx != null)
tx.commit();
waitAndCheckEvent(key, null, oldVal, true);
expData.remove(key);
break;
}
case 6:
{
cache.putIfAbsent(key, newVal);
if (tx != null)
tx.commit();
if (oldVal == null) {
waitAndCheckEvent(key, newVal, null, false);
expData.put(key, newVal);
} else
checkNoEvent(afterPutEvts);
break;
}
case 7:
{
cache.getAndPutIfAbsent(key, newVal);
if (tx != null)
tx.commit();
if (oldVal == null) {
waitAndCheckEvent(key, newVal, null, false);
expData.put(key, newVal);
} else
checkNoEvent(afterPutEvts);
break;
}
case 8:
{
cache.replace(key, newVal);
if (tx != null)
tx.commit();
if (oldVal != null) {
waitAndCheckEvent(key, newVal, oldVal, false);
expData.put(key, newVal);
} else
checkNoEvent(afterPutEvts);
break;
}
case 9:
{
cache.getAndReplace(key, newVal);
if (tx != null)
tx.commit();
if (oldVal != null) {
waitAndCheckEvent(key, newVal, oldVal, false);
expData.put(key, newVal);
} else
checkNoEvent(afterPutEvts);
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();
waitAndCheckEvent(key, newVal, oldVal, false);
expData.put(key, newVal);
} else {
cache.replace(key, replaceVal, newVal);
if (tx != null)
tx.commit();
checkNoEvent(afterPutEvts);
}
} else {
cache.replace(key, value(rnd), newVal);
if (tx != null)
tx.commit();
checkNoEvent(afterPutEvts);
}
break;
}
default:
fail("Op:" + op);
}
} finally {
if (tx != null)
tx.close();
}
}
Aggregations