Search in sources :

Example 16 with IgniteInterruptedException

use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.

the class IgniteLockAbstractSelfTest method testConditionAwaitUninterruptibly.

/**
     * @throws Exception If failed.
     */
private void testConditionAwaitUninterruptibly(final boolean fair) throws Exception {
    final IgniteLock lock0 = grid(0).reentrantLock("lock", true, fair, true);
    assertEquals(0, lock0.getHoldCount());
    assertFalse(lock0.hasQueuedThreads());
    final int totalThreads = 2;
    final Set<Thread> startedThreads = new GridConcurrentHashSet<>();
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            assertFalse(lock0.isHeldByCurrentThread());
            startedThreads.add(Thread.currentThread());
            boolean isInterrupted = false;
            lock0.lock();
            IgniteCondition cond = lock0.getOrCreateCondition("cond");
            try {
                cond.awaitUninterruptibly();
            } catch (IgniteInterruptedException ignored) {
                isInterrupted = true;
            } finally {
                // Assert that thread was not interrupted.
                assertFalse(isInterrupted);
                // Assert that lock is still locked.
                assertTrue(lock0.isLocked());
                // Assert that this thread does own the lock.
                assertTrue(lock0.isHeldByCurrentThread());
                // Clear interrupt flag.
                assertTrue(Thread.interrupted());
                // Release lock.
                if (lock0.isHeldByCurrentThread())
                    lock0.unlock();
            }
            return null;
        }
    }, totalThreads);
    // Wait for all threads to attempt to acquire lock.
    while (startedThreads.size() != totalThreads) {
        Thread.sleep(500);
    }
    lock0.lock();
    for (Thread t : startedThreads) {
        t.interrupt();
        lock0.getOrCreateCondition("cond").signal();
    }
    lock0.unlock();
    fut.get();
    assertFalse(lock0.isLocked());
    for (Thread t : startedThreads) assertFalse(lock0.hasQueuedThread(t));
    lock0.close();
}
Also used : GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) IgniteLock(org.apache.ignite.IgniteLock) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteCondition(org.apache.ignite.IgniteCondition) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) ExpectedException(org.junit.rules.ExpectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException)

Example 17 with IgniteInterruptedException

use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.

the class GridCacheQueueAdapter method offer.

/** {@inheritDoc} */
@Override
public boolean offer(T item, long timeout, TimeUnit unit) throws IgniteException {
    A.notNull(item, "item");
    A.ensure(timeout >= 0, "Timeout cannot be negative: " + timeout);
    if (!bounded()) {
        boolean offer = offer(item);
        assert offer;
        return true;
    }
    long end = U.currentTimeMillis() + MILLISECONDS.convert(timeout, unit);
    while (U.currentTimeMillis() < end) {
        boolean retVal = false;
        try {
            if (writeSem.tryAcquire(end - U.currentTimeMillis(), MILLISECONDS)) {
                checkStopping();
                retVal = offer(item);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new IgniteInterruptedException("Queue put interrupted.", e);
        }
        if (retVal)
            return true;
    }
    return false;
}
Also used : IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException)

Example 18 with IgniteInterruptedException

use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.

the class GridCacheQueueAdapter method take.

/** {@inheritDoc} */
@Nullable
@Override
public T take() throws IgniteException {
    while (true) {
        try {
            readSem.acquire();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new IgniteInterruptedException("Queue take interrupted.", e);
        }
        checkStopping();
        T e = poll();
        if (e != null)
            return e;
    }
}
Also used : IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with IgniteInterruptedException

use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.

the class GridCacheSemaphoreImpl method tryAcquire.

/** {@inheritDoc} */
@Override
public boolean tryAcquire() {
    ctx.kernalContext().gateway().readLock();
    try {
        initializeSemaphore();
        boolean res = sync.nonfairTryAcquireShared(1) >= 0;
        if (isBroken()) {
            // Clear interrupt flag.
            Thread.interrupted();
            throw new InterruptedException();
        }
        return res;
    } catch (IgniteCheckedException e) {
        throw U.convertException(e);
    } catch (InterruptedException e) {
        throw new IgniteInterruptedException(e);
    } finally {
        ctx.kernalContext().gateway().readUnlock();
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException)

Example 20 with IgniteInterruptedException

use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.

the class GridCacheSemaphoreImpl method acquire.

/** {@inheritDoc} */
@Override
public void acquire(int permits) throws IgniteInterruptedException {
    ctx.kernalContext().gateway().readLock();
    A.ensure(permits >= 0, "Number of permits must be non-negative.");
    try {
        initializeSemaphore();
        sync.acquireSharedInterruptibly(permits);
        if (isBroken()) {
            // Clear interrupt flag.
            Thread.interrupted();
            throw new InterruptedException();
        }
    } catch (IgniteCheckedException e) {
        throw U.convertException(e);
    } catch (InterruptedException e) {
        throw new IgniteInterruptedException(e);
    } finally {
        ctx.kernalContext().gateway().readUnlock();
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException)

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