Search in sources :

Example 1 with IgniteInterruptedException

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;
        }
    }
}
Also used : CIX1(org.apache.ignite.internal.util.typedef.CIX1) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteTxHeuristicCheckedException(org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteTxHeuristicCheckedException(org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException) IgniteFutureCancelledException(org.apache.ignite.lang.IgniteFutureCancelledException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) GridNearTxPrepareResponse(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareResponse)

Example 2 with IgniteInterruptedException

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;
}
Also used : IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with IgniteInterruptedException

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());
        }
    }
}
Also used : IgniteSemaphore(org.apache.ignite.IgniteSemaphore) Ignite(org.apache.ignite.Ignite) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException)

Example 4 with IgniteInterruptedException

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();
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) TcpDiscoveryAbstractMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryAbstractMessage) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException)

Example 5 with IgniteInterruptedException

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();
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CacheException(javax.cache.CacheException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException)

Aggregations

IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)25 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)17 IgniteException (org.apache.ignite.IgniteException)9 IOException (java.io.IOException)7 IgniteLock (org.apache.ignite.IgniteLock)7 ExpectedException (org.junit.rules.ExpectedException)7 GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)6 Ignite (org.apache.ignite.Ignite)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)2 IgniteCondition (org.apache.ignite.IgniteCondition)2 IgniteSemaphore (org.apache.ignite.IgniteSemaphore)2 IgniteClientDisconnectedCheckedException (org.apache.ignite.internal.IgniteClientDisconnectedCheckedException)2 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)2 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1