use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class IgniteCacheNearOnlyTxTest method txMultithreaded.
/**
* @param optimistic If {@code true} uses optimistic transaction.
* @throws Exception If failed.
*/
private void txMultithreaded(final boolean optimistic) throws Exception {
final Ignite ignite1 = ignite(1);
assertTrue(ignite1.configuration().isClientMode());
ignite1.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<>());
final AtomicInteger idx = new AtomicInteger();
final Integer key = 1;
IgniteCache<Integer, Integer> cache0 = ignite(0).cache(DEFAULT_CACHE_NAME);
IgniteCache<Integer, Integer> cache1 = ignite1.cache(DEFAULT_CACHE_NAME);
for (int i = 0; i < 5; i++) {
log.info("Iteration: " + i);
GridTestUtils.runMultiThreaded(new Callable<Object>() {
@Override
public Object call() throws Exception {
IgniteCache<Integer, Integer> cache = ignite1.cache(DEFAULT_CACHE_NAME);
IgniteTransactions txs = ignite1.transactions();
int val = idx.getAndIncrement();
for (int i = 0; i < 100; i++) {
try (Transaction tx = txs.txStart(optimistic ? OPTIMISTIC : PESSIMISTIC, REPEATABLE_READ)) {
cache.get(key);
cache.put(key, val);
tx.commit();
}
}
return null;
}
}, 5, "put-thread");
assertEquals(cache0.localPeek(key), cache1.localPeek(key));
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class IgniteCachePutGetRestartAbstractTest method testTxPutGetRestart.
/**
* @throws Exception If failed.
*/
public void testTxPutGetRestart() throws Exception {
int clientGrid = gridCount() - 1;
assertTrue(ignite(clientGrid).configuration().isClientMode());
final IgniteTransactions txs = ignite(clientGrid).transactions();
final IgniteCache<Integer, Integer> cache = jcache(clientGrid);
updateCache(cache, txs);
final AtomicBoolean stop = new AtomicBoolean();
IgniteInternalFuture<?> updateFut = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
Thread.currentThread().setName("update-thread");
assertTrue(latch.await(30_000, TimeUnit.MILLISECONDS));
int iter = 0;
while (!stop.get()) {
log.info("Start update: " + iter);
synchronized (mux) {
updateCache(cache, txs);
}
log.info("End update: " + iter++);
}
log.info("Update iterations: " + iter);
return null;
}
});
IgniteInternalFuture<?> restartFut = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
Thread.currentThread().setName("restart-thread");
ThreadLocalRandom rnd = ThreadLocalRandom.current();
while (!stop.get()) {
assertTrue(latch.await(30_000, TimeUnit.MILLISECONDS));
int node = rnd.nextInt(0, gridCount() - 1);
log.info("Stop node: " + node);
stopGrid(node);
U.sleep(100);
log.info("Start node: " + node);
startGrid(node);
latch = new CountDownLatch(1);
U.sleep(100);
}
return null;
}
});
long endTime = System.currentTimeMillis() + 2 * 60_000;
try {
int iter = 0;
while (System.currentTimeMillis() < endTime && !updateFut.isDone() && !restartFut.isDone()) {
try {
log.info("Start get: " + iter);
synchronized (mux) {
readCache(cache, txs);
}
log.info("End get: " + iter++);
} finally {
latch.countDown();
}
}
log.info("Get iterations: " + iter);
} finally {
latch.countDown();
stop.set(true);
}
updateFut.get();
restartFut.get();
readCache(cache, txs);
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CrossCacheTxRandomOperationsTest method txOperations.
/**
* @param concurrency Transaction concurrency.
* @param isolation Transaction isolation.
* @param crossCacheTx If {@code true} uses cross cache transaction.
* @param client If {@code true} uses client node.
* @throws Exception If failed.
*/
private void txOperations(TransactionConcurrency concurrency, TransactionIsolation isolation, boolean crossCacheTx, boolean client) throws Exception {
final Map<TestKey, TestValue> expData1 = new HashMap<>();
final Map<TestKey, TestValue> expData2 = new HashMap<>();
Ignite ignite = client ? ignite(GRID_CNT - 1) : ignite(0);
assertEquals(client, (boolean) ignite.configuration().isClientMode());
final List<IgniteCache<TestKey, TestValue>> caches1 = new ArrayList<>();
final List<IgniteCache<TestKey, TestValue>> caches2 = new ArrayList<>();
for (int i = 0; i < GRID_CNT; i++) {
caches1.add(ignite(i).<TestKey, TestValue>cache(CACHE1));
caches2.add(ignite(i).<TestKey, TestValue>cache(CACHE2));
}
IgniteCache<TestKey, TestValue> cache1 = ignite.cache(CACHE1);
IgniteCache<TestKey, TestValue> cache2 = ignite.cache(CACHE2);
assertNotNull(cache1);
assertNotNull(cache2);
assertNotSame(cache1, cache2);
try {
Random rnd = new Random();
long seed = System.currentTimeMillis();
rnd.setSeed(seed);
log.info("Test tx operations [concurrency=" + concurrency + ", isolation=" + isolation + ", client=" + client + ", seed=" + seed + ']');
IgniteTransactions txs = ignite.transactions();
final List<TestKey> keys = new ArrayList<>();
for (int i = 0; i < KEY_RANGE; i++) keys.add(new TestKey(i));
CacheConfiguration ccfg = cache1.getConfiguration(CacheConfiguration.class);
boolean fullSync = ccfg.getWriteSynchronizationMode() == FULL_SYNC;
boolean optimistic = concurrency == OPTIMISTIC;
boolean checkData = fullSync && !optimistic;
long stopTime = System.currentTimeMillis() + 10_000;
for (int i = 0; i < 10_000; i++) {
if (i % 100 == 0) {
if (System.currentTimeMillis() > stopTime) {
log.info("Stop on timeout, iteration: " + i);
break;
}
log.info("Iteration: " + i);
}
boolean rollback = i % 10 == 0;
try (Transaction tx = txs.txStart(concurrency, isolation)) {
cacheOperation(expData1, rnd, cache1, checkData, rollback);
if (crossCacheTx)
cacheOperation(expData2, rnd, cache2, checkData, rollback);
if (rollback)
tx.rollback();
else
tx.commit();
}
}
if (fullSync) {
checkData(caches1, keys, expData1);
checkData(caches2, keys, expData2);
cache1.removeAll();
cache2.removeAll();
checkData(caches1, keys, new HashMap<TestKey, TestValue>());
checkData(caches2, keys, new HashMap<TestKey, TestValue>());
}
} finally {
cache1.removeAll();
cache2.removeAll();
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class IgniteCacheConfigVariationsFullApiTest method testContainsKeysTx.
/**
* @throws Exception If failed.
*/
public void testContainsKeysTx() throws Exception {
if (!txEnabled())
return;
IgniteCache<String, Integer> cache = jcache();
IgniteTransactions txs = ignite(0).transactions();
Set<String> keys = new HashSet<>();
for (int i = 0; i < 10; i++) {
String key = String.valueOf(i);
keys.add(key);
}
try (Transaction tx = txs.txStart()) {
for (String key : keys) assertNull(key, cache.get(key));
assertFalse(cache.containsKeys(keys));
tx.commit();
}
try (Transaction tx = txs.txStart()) {
for (String key : keys) assertNull(key, cache.get(key));
for (String key : keys) cache.put(key, 0);
assertTrue(cache.containsKeys(keys));
tx.commit();
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class IgniteCacheConfigVariationsFullApiTest method testContainsKeyTx.
/**
* @throws Exception If failed.
*/
public void testContainsKeyTx() throws Exception {
if (!txEnabled())
return;
IgniteCache<String, Integer> cache = jcache();
IgniteTransactions txs = ignite(0).transactions();
for (int i = 0; i < 10; i++) {
String key = String.valueOf(i);
try (Transaction tx = txs.txStart()) {
assertNull(key, cache.get(key));
assertFalse(cache.containsKey(key));
tx.commit();
}
try (Transaction tx = txs.txStart()) {
assertNull(key, cache.get(key));
cache.put(key, i);
assertTrue(cache.containsKey(key));
tx.commit();
}
}
}
Aggregations