use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.
the class GridDhtTxPrepareFuture method onDone.
/** {@inheritDoc} */
@Override
public boolean onDone(GridNearTxPrepareResponse res0, Throwable err) {
assert err != null || (initialized() && !hasPending()) : "On done called for prepare future that has " + "pending mini futures: " + this;
ERR_UPD.compareAndSet(this, null, err);
// Must clear prepare future before response is sent or listeners are notified.
if (tx.optimistic())
tx.clearPrepareFuture(this);
// Do not commit one-phase commit transaction if originating node has near cache enabled.
if (tx.onePhaseCommit() && tx.commitOnPrepare()) {
assert last;
Throwable prepErr = this.err;
// Must create prepare response before transaction is committed to grab correct return value.
final GridNearTxPrepareResponse res = createPrepareResponse(prepErr);
onComplete(res);
if (tx.commitOnPrepare()) {
if (tx.markFinalizing(IgniteInternalTx.FinalizationStatus.USER_FINISH)) {
IgniteInternalFuture<IgniteInternalTx> fut = null;
CIX1<IgniteInternalFuture<IgniteInternalTx>> resClo = new CIX1<IgniteInternalFuture<IgniteInternalTx>>() {
@Override
public void applyx(IgniteInternalFuture<IgniteInternalTx> fut) {
if (REPLIED_UPD.compareAndSet(GridDhtTxPrepareFuture.this, 0, 1))
sendPrepareResponse(res);
}
};
if (prepErr == null) {
try {
fut = tx.commitAsync();
} catch (RuntimeException | Error e) {
Exception hEx = new IgniteTxHeuristicCheckedException("Commit produced a runtime " + "exception: " + CU.txString(tx), e);
res.error(hEx);
tx.systemInvalidate(true);
fut = tx.rollbackAsync();
fut.listen(resClo);
throw e;
}
} else if (!cctx.kernalContext().isStopping())
fut = tx.rollbackAsync();
if (fut != null)
fut.listen(resClo);
}
} else {
if (REPLIED_UPD.compareAndSet(this, 0, 1))
sendPrepareResponse(res);
}
return true;
} else {
if (REPLIED_UPD.compareAndSet(this, 0, 1)) {
GridNearTxPrepareResponse res = createPrepareResponse(this.err);
try {
sendPrepareResponse(res);
} finally {
// Will call super.onDone().
onComplete(res);
}
return true;
} else {
// Other thread is completing future. Wait for it to complete.
try {
if (err != null)
get();
} catch (IgniteInterruptedException e) {
onError(new IgniteCheckedException("Got interrupted while waiting for replies to be sent.", e));
} catch (IgniteCheckedException ignored) {
// No-op, get() was just synchronization.
}
return false;
}
}
}
use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.
the class GridCacheQueueAdapter method poll.
/** {@inheritDoc} */
@Nullable
@Override
public T poll(long timeout, TimeUnit unit) throws IgniteException {
A.ensure(timeout >= 0, "Timeout cannot be negative: " + timeout);
long end = U.currentTimeMillis() + MILLISECONDS.convert(timeout, unit);
while (U.currentTimeMillis() < end) {
T retVal = null;
try {
if (readSem.tryAcquire(end - U.currentTimeMillis(), MILLISECONDS)) {
checkStopping();
retVal = poll();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IgniteInterruptedException("Queue poll interrupted.", e);
}
if (retVal != null)
return retVal;
}
return null;
}
use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.
the class GridCacheAbstractDataStructuresFailoverSelfTest method doTestSemaphore.
/**
* @throws Exception If failed.
*/
private void doTestSemaphore(ConstantTopologyChangeWorker topWorker, final boolean failoverSafe) throws Exception {
final int permits = topWorker instanceof MultipleTopologyChangeWorker || topWorker instanceof PartitionedMultipleTopologyChangeWorker ? TOP_CHANGE_THREAD_CNT * 3 : TOP_CHANGE_CNT;
try (IgniteSemaphore s = grid(0).semaphore(STRUCTURE_NAME, permits, failoverSafe, true)) {
IgniteInternalFuture<?> fut = topWorker.startChangingTopology(new IgniteClosure<Ignite, Object>() {
@Override
public Object apply(Ignite ignite) {
IgniteSemaphore sem = ignite.semaphore(STRUCTURE_NAME, permits, failoverSafe, false);
while (true) {
try {
sem.acquire(1);
break;
} catch (IgniteInterruptedException e) {
// Exception may happen in non failover safe mode.
if (failoverSafe)
throw e;
else {
// and should always be discarded after exception is caught.
break;
}
}
}
return null;
}
});
while (!fut.isDone()) {
while (true) {
try {
s.acquire(1);
break;
} catch (IgniteInterruptedException e) {
// Exception may happen in non failover safe mode.
if (failoverSafe)
throw e;
else {
// and should always be discarded after exception is caught.
break;
}
}
}
assert s.availablePermits() < permits;
s.release();
assert s.availablePermits() <= permits;
}
fut.get();
// Semaphore is left in proper state only if failoverSafe mode is used.
if (failoverSafe) {
for (Ignite g : G.allGrids()) assertEquals(permits, g.semaphore(STRUCTURE_NAME, permits, false, false).availablePermits());
}
}
}
use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.
the class TcpClientDiscoverySpiSelfTest method testTimeoutWaitingNodeAddedMessage.
/**
* @throws Exception If any error occurs.
*/
public void testTimeoutWaitingNodeAddedMessage() throws Exception {
longSockTimeouts = true;
startServerNodes(2);
final CountDownLatch cnt = new CountDownLatch(1);
((TcpDiscoverySpi) G.ignite("server-1").configuration().getDiscoverySpi()).addSendMessageListener(new IgniteInClosure<TcpDiscoveryAbstractMessage>() {
@Override
public void apply(TcpDiscoveryAbstractMessage msg) {
try {
cnt.await(10, MINUTES);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IgniteInterruptedException(e);
}
}
});
try {
netTimeout = 500;
startGrid("client-0");
assert false;
} catch (IgniteCheckedException e) {
cnt.countDown();
IgniteSpiException spiEx = e.getCause(IgniteSpiException.class);
assert spiEx != null : e;
assert spiEx.getMessage().contains("Join process timed out") : spiEx.getMessage();
}
}
use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.
the class IgniteCacheOffheapEvictQueryTest method testEvictAndRemove.
/**
* @throws Exception If failed.
*/
public void testEvictAndRemove() throws Exception {
final int KEYS_CNT = 3000;
final int THREADS_CNT = 250;
final IgniteCache<Integer, Integer> c = startGrid().cache(DEFAULT_CACHE_NAME);
for (int i = 0; i < KEYS_CNT; i++) {
c.put(i, i);
if ((i & 1) == 0)
c.localEvict(F.asList(i));
}
X.println("___ Cache loaded...");
final CyclicBarrier b = new CyclicBarrier(THREADS_CNT, new Runnable() {
@Override
public void run() {
X.println("___ go!");
}
});
final AtomicInteger keys = new AtomicInteger(KEYS_CNT);
IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {
@Override
public void run() {
Random rnd = new GridRandom();
try {
b.await();
} catch (InterruptedException e) {
throw new IgniteInterruptedException(e);
} catch (BrokenBarrierException e) {
throw new IllegalStateException(e);
}
while (keys.get() > 0) {
int k = rnd.nextInt(KEYS_CNT);
try {
switch(rnd.nextInt(4)) {
case 0:
c.localEvict(F.asList(k));
break;
case 1:
c.get(k);
break;
case 2:
if (c.remove(k))
keys.decrementAndGet();
break;
case 3:
c.query(new SqlFieldsQuery("select _val from Integer where _key between ? and ?").setArgs(k, k + 20).setLocal(true)).getAll();
break;
}
} catch (CacheException e) {
String msgStart = "Failed to get value for key:";
for (Throwable th = e; th != null; th = th.getCause()) {
String msg = th.getMessage();
if (msg != null && msg.startsWith(msgStart)) {
int dot = msg.indexOf('.', msgStart.length());
assertTrue(dot != -1);
final Integer failedKey = Integer.parseInt(msg.substring(msgStart.length(), dot).trim());
X.println("___ failed key: " + failedKey);
break;
}
}
LT.warn(log, e.getMessage());
return;
}
}
}
}, THREADS_CNT);
try {
fut.get(60_000);
if (c.size(CachePeekMode.ALL) != 0)
fail("Not all keys removed.");
X.println("___ all keys removed");
} catch (IgniteFutureTimeoutCheckedException ignored) {
X.println("___ timeout");
X.println("___ keys: " + keys.get());
keys.set(0);
fut.get();
}
}
Aggregations