use of org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException in project ignite by apache.
the class GridCacheNodeFailureAbstractTest method checkTransaction.
/**
* @param concurrency Concurrency.
* @param isolation Isolation.
* @throws Exception If check failed.
*/
private void checkTransaction(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Throwable {
int idx = RAND.nextInt(GRID_CNT);
info("Grid will be stopped: " + idx);
Ignite g = grid(idx);
Transaction tx = g.transactions().txStart(concurrency, isolation);
try {
g.cache(DEFAULT_CACHE_NAME).put(KEY, VALUE);
int checkIdx = (idx + 1) % G.allGrids().size();
info("Check grid index: " + checkIdx);
IgniteFuture<?> f = waitForLocalEvent(grid(checkIdx).events(), new P1<Event>() {
@Override
public boolean apply(Event e) {
info("Received grid event: " + e);
return true;
}
}, EVT_NODE_LEFT);
stopGrid(idx);
f.get();
U.sleep(getInteger(IGNITE_TX_SALVAGE_TIMEOUT, 3000));
IgniteCache<Integer, String> checkCache = jcache(checkIdx);
boolean locked = false;
Lock lock = checkCache.lock(KEY);
for (int i = 0; !locked && i < 3; i++) {
locked = lock.tryLock();
if (!locked)
U.sleep(500);
else
break;
}
assert locked : "Failed to lock key on cache [idx=" + checkIdx + ", key=" + KEY + ']';
lock.unlock();
} catch (IgniteTxOptimisticCheckedException e) {
U.warn(log, "Optimistic transaction failure (will rollback) [msg=" + e.getMessage() + ", tx=" + tx + ']');
if (G.state(g.name()) == IgniteState.STARTED)
tx.rollback();
assert concurrency == OPTIMISTIC && isolation == SERIALIZABLE;
} catch (Throwable e) {
error("Transaction failed (will rollback): " + tx, e);
if (G.state(g.name()) == IgniteState.STARTED)
tx.rollback();
throw e;
}
}
Aggregations