use of org.apache.ignite.transactions.TransactionConcurrency in project ignite by apache.
the class GridCacheTxLoadFromStoreOnLockSelfTest method checkLoadedValue.
/**
* @throws Exception If failed.
*/
private void checkLoadedValue(int backups) throws Exception {
CacheConfiguration<Integer, Integer> cacheCfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
cacheCfg.setCacheMode(CacheMode.PARTITIONED);
cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
cacheCfg.setRebalanceMode(CacheRebalanceMode.SYNC);
cacheCfg.setCacheStoreFactory(new StoreFactory());
cacheCfg.setReadThrough(true);
cacheCfg.setBackups(backups);
cacheCfg.setLoadPreviousValue(true);
IgniteCache<Integer, Integer> cache = ignite(0).createCache(cacheCfg);
for (int i = 0; i < 10; i++) assertEquals((Integer) i, cache.get(i));
cache.removeAll();
assertEquals(0, cache.size());
for (TransactionConcurrency conc : TransactionConcurrency.values()) {
for (TransactionIsolation iso : TransactionIsolation.values()) {
info("Checking transaction [conc=" + conc + ", iso=" + iso + ']');
try (Transaction tx = ignite(0).transactions().txStart(conc, iso)) {
for (int i = 0; i < 10; i++) assertEquals("Invalid value for transaction [conc=" + conc + ", iso=" + iso + ']', (Integer) i, cache.get(i));
tx.commit();
}
cache.removeAll();
assertEquals(0, cache.size());
}
}
cache.destroy();
}
use of org.apache.ignite.transactions.TransactionConcurrency in project ignite by apache.
the class GridCacheAbstractFullApiSelfTest method doTransformResourceInjectionInTx.
/**
* @param ignite Node.
* @param cache Cache.
* @param async Use async API.
* @param oldAsync Use old async API.
* @throws Exception If failed.
*/
private void doTransformResourceInjectionInTx(Ignite ignite, IgniteCache<String, Integer> cache, boolean async, boolean oldAsync) throws Exception {
for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
for (TransactionIsolation isolation : TransactionIsolation.values()) {
IgniteTransactions txs = ignite.transactions();
try (Transaction tx = txs.txStart(concurrency, isolation)) {
doTransformResourceInjection(ignite, cache, async, oldAsync);
tx.commit();
}
}
}
}
use of org.apache.ignite.transactions.TransactionConcurrency in project ignite by apache.
the class IgniteTxCachePrimarySyncTest method singleKeyPrimaryNodeLeft.
/**
* @param ccfg Cache configuration.
* @throws Exception If failed.
*/
private void singleKeyPrimaryNodeLeft(CacheConfiguration<Object, Object> ccfg) throws Exception {
Ignite ignite = ignite(0);
IgniteCache<Object, Object> cache = ignite.createCache(ccfg);
try {
ignite(NODES - 1).createNearCache(ccfg.getName(), new NearCacheConfiguration<>());
for (int i = 0; i < NODES; i++) {
Ignite node = ignite(i);
singleKeyPrimaryNodeLeft(node, ccfg, new IgniteBiInClosure<Integer, IgniteCache<Object, Object>>() {
@Override
public void apply(Integer key, IgniteCache<Object, Object> cache) {
cache.put(key, key);
}
});
for (final TransactionConcurrency concurrency : TransactionConcurrency.values()) {
for (final TransactionIsolation isolation : TransactionIsolation.values()) {
singleKeyPrimaryNodeLeft(node, ccfg, new IgniteBiInClosure<Integer, IgniteCache<Object, Object>>() {
@Override
public void apply(Integer key, IgniteCache<Object, Object> cache) {
Ignite ignite = cache.unwrap(Ignite.class);
try (Transaction tx = ignite.transactions().txStart(concurrency, isolation)) {
cache.put(key, key);
tx.commit();
}
}
});
}
}
}
} finally {
ignite.destroyCache(cache.getName());
}
}
use of org.apache.ignite.transactions.TransactionConcurrency 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.TransactionConcurrency 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);
}
Aggregations