use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class IgniteUtils method exceptionConverters.
/**
* Gets map with converters to convert internal checked exceptions to public API unchecked exceptions.
*
* @return Exception converters.
*/
private static Map<Class<? extends IgniteCheckedException>, C1<IgniteCheckedException, IgniteException>> exceptionConverters() {
Map<Class<? extends IgniteCheckedException>, C1<IgniteCheckedException, IgniteException>> m = new HashMap<>();
m.put(IgniteInterruptedCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {
@Override
public IgniteException apply(IgniteCheckedException e) {
return new IgniteInterruptedException(e.getMessage(), (InterruptedException) e.getCause());
}
});
m.put(IgniteFutureCancelledCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {
@Override
public IgniteException apply(IgniteCheckedException e) {
return new IgniteFutureCancelledException(e.getMessage(), e);
}
});
m.put(IgniteFutureTimeoutCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {
@Override
public IgniteException apply(IgniteCheckedException e) {
return new IgniteFutureTimeoutException(e.getMessage(), e);
}
});
m.put(ClusterGroupEmptyCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {
@Override
public IgniteException apply(IgniteCheckedException e) {
return new ClusterGroupEmptyException(e.getMessage(), e);
}
});
m.put(ClusterTopologyCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {
@Override
public IgniteException apply(IgniteCheckedException e) {
ClusterTopologyException topEx = new ClusterTopologyException(e.getMessage(), e);
ClusterTopologyCheckedException checked = (ClusterTopologyCheckedException) e;
if (checked.retryReadyFuture() != null)
topEx.retryReadyFuture(new IgniteFutureImpl<>(checked.retryReadyFuture()));
return topEx;
}
});
m.put(IgniteDeploymentCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {
@Override
public IgniteException apply(IgniteCheckedException e) {
return new IgniteDeploymentException(e.getMessage(), e);
}
});
m.put(ComputeTaskTimeoutCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {
@Override
public IgniteException apply(IgniteCheckedException e) {
return new ComputeTaskTimeoutException(e.getMessage(), e);
}
});
m.put(ComputeTaskCancelledCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {
@Override
public IgniteException apply(IgniteCheckedException e) {
return new ComputeTaskCancelledException(e.getMessage(), e);
}
});
m.put(IgniteTxRollbackCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {
@Override
public IgniteException apply(IgniteCheckedException e) {
return new TransactionRollbackException(e.getMessage(), e);
}
});
m.put(IgniteTxHeuristicCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {
@Override
public IgniteException apply(IgniteCheckedException e) {
return new TransactionHeuristicException(e.getMessage(), e);
}
});
m.put(IgniteTxTimeoutCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {
@Override
public IgniteException apply(IgniteCheckedException e) {
if (e.getCause() instanceof TransactionDeadlockException)
return new TransactionTimeoutException(e.getMessage(), e.getCause());
return new TransactionTimeoutException(e.getMessage(), e);
}
});
m.put(IgniteTxOptimisticCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {
@Override
public IgniteException apply(IgniteCheckedException e) {
return new TransactionOptimisticException(e.getMessage(), e);
}
});
m.put(IgniteClientDisconnectedCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {
@Override
public IgniteException apply(IgniteCheckedException e) {
return new IgniteClientDisconnectedException(((IgniteClientDisconnectedCheckedException) e).reconnectFuture(), e.getMessage(), e);
}
});
return m;
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class IgniteTxCacheWriteSynchronizationModesMultithreadedTest method multithreaded.
/**
* @param syncMode Write synchronization mode.
* @param backups Number of backups.
* @param store If {@code true} sets store in cache configuration.
* @param nearCache If {@code true} creates near cache on one of client nodes.
* @param restart If {@code true} restarts one node during test.
* @throws Exception If failed.
*/
private void multithreaded(CacheWriteSynchronizationMode syncMode, int backups, boolean store, boolean nearCache, boolean restart) throws Exception {
final Ignite ignite = ignite(0);
createCache(ignite, cacheConfiguration(DEFAULT_CACHE_NAME, syncMode, backups, store), nearCache);
final AtomicBoolean stop = new AtomicBoolean();
IgniteInternalFuture<?> restartFut = null;
try {
if (restart) {
restartFut = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
while (!stop.get()) {
startGrid(NODES);
U.sleep(100);
stopGrid(NODES);
}
return null;
}
}, "restart-thread");
}
commitMultithreaded(new IgniteBiInClosure<Ignite, IgniteCache<Integer, Integer>>() {
@Override
public void apply(Ignite ignite, IgniteCache<Integer, Integer> cache) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
Integer key = rnd.nextInt(MULTITHREADED_TEST_KEYS);
cache.put(key, rnd.nextInt());
}
});
commitMultithreaded(new IgniteBiInClosure<Ignite, IgniteCache<Integer, Integer>>() {
@Override
public void apply(Ignite ignite, IgniteCache<Integer, Integer> cache) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
Map<Integer, Integer> map = new TreeMap<>();
for (int i = 0; i < 100; i++) {
Integer key = rnd.nextInt(MULTITHREADED_TEST_KEYS);
map.put(key, rnd.nextInt());
}
cache.putAll(map);
}
});
commitMultithreaded(new IgniteBiInClosure<Ignite, IgniteCache<Integer, Integer>>() {
@Override
public void apply(Ignite ignite, IgniteCache<Integer, Integer> cache) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
Map<Integer, Integer> map = new TreeMap<>();
for (int i = 0; i < 100; i++) {
Integer key = rnd.nextInt(MULTITHREADED_TEST_KEYS);
map.put(key, rnd.nextInt());
}
try {
try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
for (Map.Entry<Integer, Integer> e : map.entrySet()) cache.put(e.getKey(), e.getValue());
tx.commit();
}
} catch (CacheException | IgniteException ignored) {
// No-op.
}
}
});
commitMultithreaded(new IgniteBiInClosure<Ignite, IgniteCache<Integer, Integer>>() {
@Override
public void apply(Ignite ignite, IgniteCache<Integer, Integer> cache) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
Map<Integer, Integer> map = new LinkedHashMap<>();
for (int i = 0; i < 10; i++) {
Integer key = rnd.nextInt(MULTITHREADED_TEST_KEYS);
map.put(key, rnd.nextInt());
}
while (true) {
try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
for (Map.Entry<Integer, Integer> e : map.entrySet()) cache.put(e.getKey(), e.getValue());
tx.commit();
break;
} catch (TransactionOptimisticException ignored) {
// Retry.
} catch (CacheException | IgniteException ignored) {
break;
}
}
}
});
} finally {
stop.set(true);
ignite.destroyCache(DEFAULT_CACHE_NAME);
if (restartFut != null)
restartFut.get();
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class GridCacheNearMultiGetSelfTest method checkDoubleGet.
/**
* @param concurrency Concurrency.
* @param isolation Isolation.
* @param put If {@code true}, then value will be pre-stored in cache.
* @throws Exception If failed.
*/
private void checkDoubleGet(TransactionConcurrency concurrency, TransactionIsolation isolation, boolean put) throws Exception {
IgniteEx ignite = grid(0);
IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
Integer key = 1;
String val = null;
if (put)
cache.put(key, val = Integer.toString(key));
Transaction tx = ignite.transactions().txStart(concurrency, isolation, 0, 0);
try {
if (isTestDebug()) {
info("Started transaction.");
Affinity<Integer> aff = affinity(cache);
int part = aff.partition(key);
if (isTestDebug())
info("Key affinity [key=" + key + ", partition=" + part + ", affinity=" + U.toShortString(aff.mapKeyToPrimaryAndBackups(key)) + ']');
}
for (int i = 0; i < GET_CNT; i++) {
if (isTestDebug())
info("Reading key [key=" + key + ", i=" + i + ']');
String v = cache.get(key);
assertEquals("Value mismatch for put [val=" + val + ", v=" + v + ", put=" + put + ']', val, v);
if (isTestDebug())
info("Read value for key (will read again) [key=" + key + ", val=" + v + ", i=" + i + ']');
}
if (isTestDebug())
info("Committing transaction.");
tx.commit();
if (isTestDebug())
info("Committed transaction: " + tx);
} catch (TransactionOptimisticException e) {
if (concurrency != OPTIMISTIC || isolation != SERIALIZABLE) {
error("Received invalid optimistic failure.", e);
throw e;
}
if (isTestDebug())
info("Optimistic transaction failure (will rollback) [msg=" + e.getMessage() + ", tx=" + tx.xid() + ']');
try {
tx.rollback();
} catch (IgniteException ex) {
error("Failed to rollback optimistic failure: " + tx, ex);
throw ex;
}
} catch (Exception e) {
error("Transaction failed (will rollback): " + tx, e);
tx.rollback();
throw e;
} catch (Error e) {
error("Error when executing transaction (will rollback): " + tx, e);
tx.rollback();
throw e;
} finally {
Transaction t = ignite.transactions().tx();
assert t == null : "Thread should not have transaction upon completion ['t==tx'=" + (t == tx) + ", t=" + t + (t != tx ? "tx=" + tx : "tx=''") + ']';
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class IgniteBenchmarkUtils method doInTransaction.
/**
* @param igniteTx Ignite transaction.
* @param txConcurrency Transaction concurrency.
* @param clo Closure.
* @return Result of closure execution.
* @throws Exception If failed.
*/
public static <T> T doInTransaction(IgniteTransactions igniteTx, TransactionConcurrency txConcurrency, TransactionIsolation txIsolation, Callable<T> clo) throws Exception {
while (true) {
try (Transaction tx = igniteTx.txStart(txConcurrency, txIsolation)) {
T res = clo.call();
tx.commit();
return res;
} catch (CacheException e) {
if (e.getCause() instanceof ClusterTopologyException) {
ClusterTopologyException topEx = (ClusterTopologyException) e.getCause();
topEx.retryReadyFuture().get();
} else
throw e;
} catch (ClusterTopologyException e) {
e.retryReadyFuture().get();
} catch (TransactionRollbackException | TransactionOptimisticException ignore) {
// Safe to retry right away.
}
}
}
Aggregations