use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxCommit.
/**
* @throws Exception If failed.
*/
public void testTxCommit() throws Exception {
Ignite ignite0 = ignite(0);
Ignite ignite1 = ignite(1);
final IgniteTransactions txs0 = ignite0.transactions();
final IgniteTransactions txs1 = ignite1.transactions();
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
try {
IgniteCache<Integer, Integer> cache0 = ignite0.createCache(ccfg);
IgniteCache<Integer, Integer> cache1 = ignite1.cache(ccfg.getName());
List<Integer> keys = testKeys(cache0);
for (Integer key : keys) {
log.info("Test key: " + key);
Integer expVal = null;
for (int i = 0; i < 100; i++) {
try (Transaction tx = txs0.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache0.get(key);
assertEquals(expVal, val);
cache0.put(key, i);
tx.commit();
expVal = i;
}
try (Transaction tx = txs1.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache1.get(key);
assertEquals(expVal, val);
cache1.put(key, val);
tx.commit();
}
try (Transaction tx = txs0.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache0.get(key);
assertEquals(expVal, val);
cache0.put(key, val);
tx.commit();
}
}
checkValue(key, expVal, cache0.getName());
cache0.remove(key);
try (Transaction tx = txs0.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache0.get(key);
assertNull(val);
cache0.put(key, expVal + 1);
tx.commit();
}
checkValue(key, expVal + 1, cache0.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method lockKey.
/**
* @param releaseLatch Release lock latch.
* @param cache Cache.
* @param key Key.
* @return Future.
* @throws Exception If failed.
*/
private IgniteInternalFuture<?> lockKey(final CountDownLatch releaseLatch, final IgniteCache<Integer, Integer> cache, final Integer key) throws Exception {
final CountDownLatch lockLatch = new CountDownLatch(1);
IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache.put(key, 1);
log.info("Locked key: " + key);
lockLatch.countDown();
assertTrue(releaseLatch.await(100000, SECONDS));
log.info("Commit tx: " + key);
tx.commit();
}
return null;
}
}, "lock-thread");
assertTrue(lockLatch.await(10, SECONDS));
return fut;
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method txStreamerLoad.
/**
* @param ignite Node.
* @param key Key.
* @param cacheName Cache name.
* @param allowOverwrite Streamer flag.
* @throws Exception If failed.
*/
private void txStreamerLoad(Ignite ignite, Integer key, String cacheName, boolean allowOverwrite) throws Exception {
IgniteCache<Integer, Integer> cache = ignite.cache(cacheName);
log.info("Test key: " + key);
Integer loadVal = -1;
IgniteTransactions txs = ignite.transactions();
try (IgniteDataStreamer<Integer, Integer> streamer = ignite.dataStreamer(cache.getName())) {
streamer.allowOverwrite(allowOverwrite);
streamer.addData(key, loadVal);
}
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache.get(key);
assertEquals(loadVal, val);
tx.commit();
}
checkValue(key, loadVal, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache.get(key);
assertEquals(loadVal, val);
cache.put(key, 0);
tx.commit();
}
checkValue(key, 0, cache.getName());
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxConflictInvokeAll.
/**
* @throws Exception If failed
*/
public void testTxConflictInvokeAll() throws Exception {
Ignite ignite0 = ignite(0);
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
try {
IgniteCache<Integer, Integer> cache0 = ignite0.createCache(ccfg);
final Integer key1 = primaryKey(ignite(0).cache(cache0.getName()));
final Integer key2 = primaryKey(ignite(1).cache(cache0.getName()));
Map<Integer, Integer> vals = new HashMap<>();
int newVal = 0;
for (Ignite ignite : G.allGrids()) {
log.info("Test node: " + ignite.name());
IgniteTransactions txs = ignite.transactions();
IgniteCache<Integer, Integer> cache = ignite.cache(cache0.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Map<Integer, EntryProcessorResult<Integer>> res = cache.invokeAll(F.asSet(key1, key2), new SetValueProcessor(newVal));
if (!vals.isEmpty()) {
EntryProcessorResult<Integer> res1 = res.get(key1);
assertNotNull(res1);
assertEquals(vals.get(key1), res1.get());
EntryProcessorResult<Integer> res2 = res.get(key2);
assertNotNull(res2);
assertEquals(vals.get(key2), res2.get());
} else
assertTrue(res.isEmpty());
tx.commit();
}
checkValue(key1, newVal, cache.getName());
checkValue(key2, newVal, cache.getName());
vals.put(key1, newVal);
vals.put(key2, newVal);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Map<Integer, EntryProcessorResult<Integer>> res = cache.invokeAll(F.asSet(key1, key2), new SetValueProcessor(newVal + 1));
EntryProcessorResult<Integer> res1 = res.get(key1);
assertNotNull(res1);
assertEquals(vals.get(key1), res1.get());
EntryProcessorResult<Integer> res2 = res.get(key2);
assertNotNull(res2);
assertEquals(vals.get(key2), res2.get());
updateKey(cache0, key1, -1);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key1, -1, cache.getName());
checkValue(key2, newVal, cache.getName());
vals.put(key1, -1);
vals.put(key2, newVal);
newVal++;
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class CacheSerializableTransactionsTest method incrementTx.
/**
* @param nearCache If {@code true} near cache is enabled.
* @param store If {@code true} cache store is enabled.
* @param restart If {@code true} restarts one node.
* @throws Exception If failed.
*/
private void incrementTx(boolean nearCache, boolean store, final boolean restart) throws Exception {
final Ignite srv = ignite(1);
CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, store, false);
final List<Ignite> clients = clients();
final String cacheName = srv.createCache(ccfg).getName();
final AtomicBoolean stop = new AtomicBoolean();
try {
final List<IgniteCache<Integer, Integer>> caches = new ArrayList<>();
for (Ignite client : clients) {
if (nearCache)
caches.add(client.createNearCache(cacheName, new NearCacheConfiguration<Integer, Integer>()));
else
caches.add(client.<Integer, Integer>cache(cacheName));
}
IgniteInternalFuture<?> restartFut = restart ? restartFuture(stop, null) : null;
final long stopTime = U.currentTimeMillis() + getTestTimeout() - 30_000;
for (int i = 0; i < 30; i++) {
final AtomicInteger cntr = new AtomicInteger();
final Integer key = i;
final AtomicInteger threadIdx = new AtomicInteger();
final int THREADS = 10;
final CyclicBarrier barrier = new CyclicBarrier(THREADS);
GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
int idx = threadIdx.getAndIncrement() % caches.size();
IgniteCache<Integer, Integer> cache = caches.get(idx);
Ignite ignite = cache.unwrap(Ignite.class);
IgniteTransactions txs = ignite.transactions();
log.info("Started update thread: " + ignite.name());
barrier.await();
for (int i = 0; i < 1000; i++) {
if (i % 100 == 0 && U.currentTimeMillis() > stopTime)
break;
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache.get(key);
cache.put(key, val == null ? 1 : val + 1);
tx.commit();
}
cntr.incrementAndGet();
} catch (TransactionOptimisticException ignore) {
// Retry.
} catch (IgniteException | CacheException e) {
assertTrue("Unexpected exception [err=" + e + ", cause=" + e.getCause() + ']', restart && X.hasCause(e, ClusterTopologyCheckedException.class));
}
}
return null;
}
}, THREADS, "update-thread").get();
log.info("Iteration [iter=" + i + ", val=" + cntr.get() + ']');
assertTrue(cntr.get() > 0);
checkValue(key, cntr.get(), cacheName, restart);
if (U.currentTimeMillis() > stopTime)
break;
}
stop.set(true);
if (restartFut != null)
restartFut.get();
} finally {
stop.set(true);
destroyCache(cacheName);
}
}
Aggregations