use of org.apache.ignite.transactions.TransactionIsolation in project ignite by apache.
the class IgniteCacheContainsKeyAbstractSelfTest method testContainsInTx.
/**
* @throws Exception If failed.
*/
public void testContainsInTx() throws Exception {
if (atomicityMode() == TRANSACTIONAL) {
String key = "1";
for (int i = 0; i < gridCount(); i++) assertFalse("Invalid result on grid: " + i, jcache(i).containsKey(key));
IgniteCache<String, Integer> cache = jcache(0);
for (TransactionConcurrency conc : TransactionConcurrency.values()) {
for (TransactionIsolation iso : TransactionIsolation.values()) {
try (Transaction tx = grid(0).transactions().txStart(conc, iso)) {
assertFalse("Invalid result on grid inside tx", cache.containsKey(key));
assertFalse("Key was enlisted to transaction: " + tx, txContainsKey(tx, key));
cache.put(key, 1);
assertTrue("Invalid result on grid inside tx", cache.containsKey(key));
// Do not commit.
}
for (int i = 0; i < gridCount(); i++) assertFalse("Invalid result on grid: " + i, jcache(i).containsKey(key));
}
}
}
}
use of org.apache.ignite.transactions.TransactionIsolation in project ignite by apache.
the class IgniteCacheClientNodeChangingTopologyTest method multinode.
/**
* @param atomicityMode Atomicity mode cache.
* @param testType Test type.
* @throws Exception If failed.
*/
private void multinode(CacheAtomicityMode atomicityMode, final TestType testType) throws Exception {
ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
ccfg.setCacheMode(PARTITIONED);
ccfg.setBackups(1);
ccfg.setAtomicityMode(atomicityMode);
ccfg.setWriteSynchronizationMode(FULL_SYNC);
ccfg.setRebalanceMode(SYNC);
final int SRV_CNT = 4;
for (int i = 0; i < SRV_CNT; i++) startGrid(i);
final int CLIENT_CNT = 4;
final List<Ignite> clients = new ArrayList<>();
client = true;
for (int i = 0; i < CLIENT_CNT; i++) {
Ignite ignite = startGrid(SRV_CNT + i);
assertTrue(ignite.configuration().isClientMode());
clients.add(ignite);
}
final AtomicBoolean stop = new AtomicBoolean();
final AtomicInteger threadIdx = new AtomicInteger(0);
final int THREADS = CLIENT_CNT * 3;
final ConcurrentHashSet<Integer> putKeys = new ConcurrentHashSet<>();
IgniteInternalFuture<?> fut;
try {
fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
int clientIdx = threadIdx.getAndIncrement() % CLIENT_CNT;
Ignite ignite = clients.get(clientIdx);
assertTrue(ignite.configuration().isClientMode());
Thread.currentThread().setName("update-thread-" + ignite.name());
IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);
boolean useTx = testType == TestType.OPTIMISTIC_TX || testType == TestType.OPTIMISTIC_SERIALIZABLE_TX || testType == TestType.PESSIMISTIC_TX;
if (useTx || testType == TestType.LOCK) {
assertEquals(TRANSACTIONAL, cache.getConfiguration(CacheConfiguration.class).getAtomicityMode());
}
ThreadLocalRandom rnd = ThreadLocalRandom.current();
int cntr = 0;
while (!stop.get()) {
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int i = 0; i < 100; i++) {
Integer key = rnd.nextInt(0, 1000);
map.put(key, rnd.nextInt());
}
try {
if (testType == TestType.LOCK) {
Lock lock = cache.lockAll(map.keySet());
lock.lock();
lock.unlock();
} else {
if (useTx) {
IgniteTransactions txs = ignite.transactions();
TransactionConcurrency concurrency = testType == TestType.PESSIMISTIC_TX ? PESSIMISTIC : OPTIMISTIC;
TransactionIsolation isolation = testType == TestType.OPTIMISTIC_SERIALIZABLE_TX ? SERIALIZABLE : REPEATABLE_READ;
try (Transaction tx = txs.txStart(concurrency, isolation)) {
cache.putAll(map);
tx.commit();
}
} else
cache.putAll(map);
putKeys.addAll(map.keySet());
}
} catch (CacheException | IgniteException e) {
log.info("Operation failed, ignore: " + e);
}
if (++cntr % 100 == 0)
log.info("Iteration: " + cntr);
if (updateBarrier != null)
updateBarrier.await();
}
return null;
}
}, THREADS, "update-thread");
long stopTime = System.currentTimeMillis() + 60_000;
while (System.currentTimeMillis() < stopTime) {
boolean restartClient = ThreadLocalRandom.current().nextBoolean();
Integer idx = null;
if (restartClient) {
log.info("Start client node.");
client = true;
IgniteEx ignite = startGrid(SRV_CNT + CLIENT_CNT);
IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);
assertNotNull(cache);
} else {
idx = ThreadLocalRandom.current().nextInt(0, SRV_CNT);
log.info("Stop server node: " + idx);
stopGrid(idx);
}
updateBarrier = new CyclicBarrier(THREADS + 1, new Runnable() {
@Override
public void run() {
updateBarrier = null;
}
});
try {
updateBarrier.await(30_000, TimeUnit.MILLISECONDS);
} catch (TimeoutException ignored) {
log.error("Failed to wait for update.");
for (Ignite ignite : G.allGrids()) ((IgniteKernal) ignite).dumpDebugInfo();
U.dumpThreads(log);
CyclicBarrier barrier0 = updateBarrier;
if (barrier0 != null)
barrier0.reset();
fail("Failed to wait for update.");
}
U.sleep(500);
if (restartClient) {
log.info("Stop client node.");
stopGrid(SRV_CNT + CLIENT_CNT);
} else {
log.info("Start server node: " + idx);
client = false;
startGrid(idx);
}
updateBarrier = new CyclicBarrier(THREADS + 1, new Runnable() {
@Override
public void run() {
updateBarrier = null;
}
});
try {
updateBarrier.await(30_000, TimeUnit.MILLISECONDS);
} catch (TimeoutException ignored) {
log.error("Failed to wait for update.");
for (Ignite ignite : G.allGrids()) ((IgniteKernal) ignite).dumpDebugInfo();
U.dumpThreads(log);
CyclicBarrier barrier0 = updateBarrier;
if (barrier0 != null)
barrier0.reset();
fail("Failed to wait for update.");
}
U.sleep(500);
}
} finally {
stop.set(true);
}
fut.get(30_000);
if (testType != TestType.LOCK)
checkData(null, putKeys, grid(SRV_CNT).cache(DEFAULT_CACHE_NAME), SRV_CNT + CLIENT_CNT);
}
use of org.apache.ignite.transactions.TransactionIsolation in project ignite by apache.
the class CachePutIfAbsentTest 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()) {
try {
IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = 0; i < 10; i++) {
Integer key = rnd.nextInt(10_000);
cache.put(key, 2);
for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
for (TransactionIsolation isolation : TransactionIsolation.values()) {
try (Transaction tx = txs.txStart(concurrency, isolation)) {
Object old = cache.getAndPutIfAbsent(key, 3);
assertEquals(2, old);
Object val = cache.get(key);
assertEquals(2, val);
tx.commit();
}
assertEquals((Integer) 2, cache.get(key));
}
}
}
} finally {
ignite0.destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.transactions.TransactionIsolation in project ignite by apache.
the class CacheTxFastFinishTest method fastFinishTx.
/**
* @param ignite Node.
*/
private void fastFinishTx(Ignite ignite) {
IgniteTransactions txs = ignite.transactions();
IgniteCache cache = ignite.cache(DEFAULT_CACHE_NAME);
for (boolean commit : new boolean[] { true, false }) {
for (TransactionConcurrency c : TransactionConcurrency.values()) {
for (TransactionIsolation isolation : TransactionIsolation.values()) {
try (Transaction tx = txs.txStart(c, isolation)) {
checkFastTxFinish(tx, commit);
}
}
}
for (int i = 0; i < 100; i++) {
try (Transaction tx = txs.txStart(OPTIMISTIC, REPEATABLE_READ)) {
cache.get(i);
checkFastTxFinish(tx, commit);
}
try (Transaction tx = txs.txStart(OPTIMISTIC, READ_COMMITTED)) {
cache.get(i);
checkFastTxFinish(tx, commit);
}
}
for (int i = 0; i < 100; i++) {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.get(i);
checkNormalTxFinish(tx, commit);
}
try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache.get(i);
checkNormalTxFinish(tx, commit);
}
}
for (int i = 0; i < 100; i++) {
for (TransactionConcurrency c : TransactionConcurrency.values()) {
for (TransactionIsolation isolation : TransactionIsolation.values()) {
try (Transaction tx = txs.txStart(c, isolation)) {
cache.put(i, i);
checkNormalTxFinish(tx, commit);
}
}
}
}
}
}
use of org.apache.ignite.transactions.TransactionIsolation in project ignite by apache.
the class IgniteOnePhaseCommitNearSelfTest method checkKey.
/**
* @param transactions Transactions instance.
* @param cache Cache instance.
* @param key Key.
*/
private void checkKey(IgniteTransactions transactions, Cache<Object, Object> cache, int key) throws Exception {
cache.put(key, key);
finalCheck(key, true);
TransactionIsolation[] isolations = { READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE };
TransactionConcurrency[] concurrencies = { OPTIMISTIC, PESSIMISTIC };
for (TransactionIsolation isolation : isolations) {
for (TransactionConcurrency concurrency : concurrencies) {
info("Checking transaction [isolation=" + isolation + ", concurrency=" + concurrency + ']');
try (Transaction tx = transactions.txStart(concurrency, isolation)) {
cache.put(key, isolation + "-" + concurrency);
tx.commit();
}
finalCheck(key, true);
}
}
}
Aggregations