use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class IgniteTxMultiThreadedAbstractTest method testOptimisticSerializableConsistency.
/**
* @throws Exception If failed.
*/
public void testOptimisticSerializableConsistency() throws Exception {
final IgniteCache<Integer, Long> cache = grid(0).cache(DEFAULT_CACHE_NAME);
final int THREADS = 3;
final int ITERATIONS = 100;
for (int key0 = 100_000; key0 < 100_000 + 20; key0++) {
final int key = key0;
cache.put(key, 0L);
List<IgniteInternalFuture<Collection<Long>>> futs = new ArrayList<>(THREADS);
for (int i = 0; i < THREADS; i++) {
futs.add(GridTestUtils.runAsync(new Callable<Collection<Long>>() {
@Override
public Collection<Long> call() throws Exception {
Collection<Long> res = new ArrayList<>();
for (int i = 0; i < ITERATIONS; i++) {
while (true) {
try (Transaction tx = grid(0).transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
long val = cache.get(key);
cache.put(key, val + 1);
tx.commit();
assertTrue(res.add(val + 1));
break;
} catch (TransactionOptimisticException ignored) {
// Retry.
}
}
}
return res;
}
}));
}
long total = 0;
List<Collection<Long>> cols = new ArrayList<>(THREADS);
for (IgniteInternalFuture<Collection<Long>> fut : futs) {
Collection<Long> col = fut.get();
assertEquals(ITERATIONS, col.size());
total += col.size();
cols.add(col);
}
log.info("Cache value: " + cache.get(key));
Set<Long> duplicates = new HashSet<>();
for (Collection<Long> col1 : cols) {
for (Long val1 : col1) {
for (Collection<Long> col2 : cols) {
if (col1 == col2)
continue;
for (Long val2 : col2) {
if (val1.equals(val2)) {
duplicates.add(val2);
break;
}
}
}
}
}
assertTrue("Found duplicated values: " + duplicates, duplicates.isEmpty());
assertEquals((long) THREADS * ITERATIONS, total);
// Try to update one more time to make sure cache is in consistent state.
try (Transaction tx = grid(0).transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
long val = cache.get(key);
cache.put(key, val);
tx.commit();
}
for (int i = 0; i < gridCount(); i++) assertEquals(total, grid(i).cache(DEFAULT_CACHE_NAME).get(key));
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class CacheSerializableTransactionsTest method txConflictReadWrite.
/**
* @param noVal If {@code true} there is no cache value when read in tx.
* @param rmv If {@code true} tests remove, otherwise put.
* @throws Exception If failed.
*/
private void txConflictReadWrite(boolean noVal, boolean rmv, boolean needVer) throws Exception {
Ignite ignite0 = ignite(0);
final IgniteTransactions txs = ignite0.transactions();
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
try {
IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
List<Integer> keys = testKeys(cache);
for (Integer key : keys) {
log.info("Test key: " + key);
Integer expVal = null;
if (!noVal) {
expVal = -1;
cache.put(key, expVal);
}
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
if (needVer) {
CacheEntry<Integer, Integer> val = cache.getEntry(key);
assertEquals(expVal, val == null ? null : val.getValue());
} else {
Integer val = cache.get(key);
assertEquals(expVal, val);
}
updateKey(cache, key, 1);
if (rmv)
cache.remove(key);
else
cache.put(key, 2);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, 1, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
if (needVer) {
CacheEntry<Integer, Integer> val = cache.getEntry(key);
assertEquals(1, (Object) val.getValue());
} else {
Integer val = cache.get(key);
assertEquals(1, (Object) val);
}
if (rmv)
cache.remove(key);
else
cache.put(key, 2);
tx.commit();
}
checkValue(key, rmv ? null : 2, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException 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.transactions.TransactionOptimisticException 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);
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxConflictRemoveWithOldValue.
/**
* @throws Exception If failed.
*/
public void testTxConflictRemoveWithOldValue() throws Exception {
Ignite ignite0 = ignite(0);
final IgniteTransactions txs = ignite0.transactions();
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
try {
IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
List<Integer> keys = testKeys(cache);
for (final Integer key : keys) {
log.info("Test key: " + key);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 2);
assertFalse(rmv);
updateKey(cache, key, 1);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, 1, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 1);
assertTrue(rmv);
tx.commit();
}
checkValue(key, null, cache.getName());
cache.remove(key);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 2);
assertFalse(rmv);
tx.commit();
}
checkValue(key, null, cache.getName());
cache.put(key, 2);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 2);
assertTrue(rmv);
updateKey(cache, key, 3);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, 3, cache.getName());
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 3);
assertTrue(rmv);
txAsync(cache, OPTIMISTIC, SERIALIZABLE, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {
@Override
public Void apply(IgniteCache<Integer, Integer> cache) {
cache.remove(key);
return null;
}
});
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, null, cache.getName());
cache.put(key, 1);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 2);
assertFalse(rmv);
tx.commit();
}
checkValue(key, 1, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 1);
assertTrue(rmv);
tx.commit();
}
checkValue(key, null, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
Aggregations