use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxConflictCasReplace.
/**
* @throws Exception If failed.
*/
public void testTxConflictCasReplace() 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 replace = cache.replace(key, 1, 2);
assertFalse(replace);
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 replace = cache.replace(key, 1, 2);
assertTrue(replace);
tx.commit();
}
checkValue(key, 2, cache.getName());
cache.remove(key);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 1, 2);
assertFalse(replace);
tx.commit();
}
checkValue(key, null, cache.getName());
cache.put(key, 2);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 2, 1);
assertTrue(replace);
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 replace = cache.replace(key, 3, 4);
assertTrue(replace);
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 replace = cache.replace(key, 2, 3);
assertFalse(replace);
tx.commit();
}
checkValue(key, 1, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 1, 3);
assertTrue(replace);
tx.commit();
}
checkValue(key, 3, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class IgniteTxAbstractTest method checkCommit.
/**
* @param concurrency Concurrency.
* @param isolation Isolation.
* @throws Exception If check failed.
*/
protected void checkCommit(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
int gridIdx = RAND.nextInt(gridCount());
Ignite ignite = grid(gridIdx);
if (isTestDebug())
debug("Checking commit on grid: " + ignite.cluster().localNode().id());
for (int i = 0; i < iterations(); i++) {
IgniteCache<Integer, String> cache = jcache(gridIdx);
try (Transaction tx = ignite(gridIdx).transactions().txStart(concurrency, isolation, 0, 0)) {
int prevKey = -1;
for (Integer key : getKeys()) {
// Make sure we have the same locking order for all concurrent transactions.
assert key >= prevKey : "key: " + key + ", prevKey: " + prevKey;
if (isTestDebug()) {
AffinityFunction aff = cache.getConfiguration(CacheConfiguration.class).getAffinity();
int part = aff.partition(key);
debug("Key affinity [key=" + key + ", partition=" + part + ", affinity=" + U.toShortString(ignite(gridIdx).affinity(DEFAULT_CACHE_NAME).mapPartitionToPrimaryAndBackups(part)) + ']');
}
String val = Integer.toString(key);
switch(getOp()) {
case READ:
{
if (isTestDebug())
debug("Reading key [key=" + key + ", i=" + i + ']');
val = cache.get(key);
if (isTestDebug())
debug("Read value for key [key=" + key + ", val=" + val + ']');
break;
}
case WRITE:
{
if (isTestDebug())
debug("Writing key and value [key=" + key + ", val=" + val + ", i=" + i + ']');
cache.put(key, val);
break;
}
case REMOVE:
{
if (isTestDebug())
debug("Removing key [key=" + key + ", i=" + i + ']');
cache.remove(key);
break;
}
default:
{
assert false;
}
}
}
tx.commit();
if (isTestDebug())
debug("Committed transaction [i=" + i + ", tx=" + tx + ']');
} catch (TransactionOptimisticException e) {
if (!(concurrency == OPTIMISTIC && isolation == SERIALIZABLE)) {
log.error("Unexpected error: " + e, e);
throw e;
}
} catch (Throwable e) {
log.error("Unexpected error: " + e, e);
throw e;
}
}
Transaction tx = ignite(gridIdx).transactions().tx();
assertNull("Thread should not have transaction upon completion", tx);
if (printMemoryStats()) {
if (cntr.getAndIncrement() % 100 == 0)
// Print transaction memory stats.
((IgniteKernal) grid(gridIdx)).internalCache(DEFAULT_CACHE_NAME).context().tm().printMemoryStats();
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class CacheSerializableTransactionsTest 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()) {
logCacheInfo(ccfg);
try {
IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
List<Integer> keys = testKeys(cache);
for (Integer key : keys) {
log.info("Test key: " + key);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Object old = cache.getAndPutIfAbsent(key, 2);
assertNull(old);
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)) {
Object old = cache.getAndPutIfAbsent(key, 2);
assertEquals(1, old);
tx.commit();
}
checkValue(key, 1, cache.getName());
cache.remove(key);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Object old = cache.getAndPutIfAbsent(key, 2);
assertNull(old);
tx.commit();
}
checkValue(key, 2, cache.getName());
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Object old = cache.getAndPutIfAbsent(key, 4);
assertEquals(2, old);
updateKey(cache, key, 3);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, 3, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class IgniteAccountSerializableTxBenchmark method test.
/** {@inheritDoc} */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
Set<Integer> accountIds = new HashSet<>();
int accNum = args.batch();
while (accountIds.size() < accNum) accountIds.add(nextRandom(args.range()));
while (true) {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Map<Integer, Account> accounts = (Map) cache.getAll(accountIds);
if (accounts.size() != accNum)
throw new Exception("Failed to find accounts: " + accountIds);
Integer fromId = accountIds.iterator().next();
int fromBalance = accounts.get(fromId).balance();
for (Integer id : accountIds) {
if (id.equals(fromId))
continue;
Account account = accounts.get(id);
if (fromBalance > 0) {
fromBalance--;
cache.put(id, new Account(account.balance() + 1));
}
}
cache.put(fromId, new Account(fromBalance));
tx.commit();
} catch (TransactionOptimisticException e) {
continue;
}
break;
}
return true;
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class IgnitePutAllSerializableTxBenchmark method test.
/** {@inheritDoc} */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
ThreadRange r = threadRange();
Map<Integer, Integer> vals = new HashMap<>();
for (int i = 0; i < args.batch(); i++) {
int key = r.nextRandom();
vals.put(key, key);
}
while (true) {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.putAll(vals);
tx.commit();
} catch (TransactionOptimisticException e) {
continue;
}
break;
}
return true;
}
Aggregations