Search in sources :

Example 21 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 22 with IgniteInterruptedException

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

the class GridCacheSemaphoreImpl method tryAcquire.

/**
 * {@inheritDoc}
 */
@Override
public boolean tryAcquire(int permits, long timeout, TimeUnit unit) throws IgniteInterruptedException {
    ctx.kernalContext().gateway().readLock();
    A.ensure(permits >= 0, "Number of permits must be non-negative.");
    try {
        initializeSemaphore();
        boolean res = sync.tryAcquireSharedNanos(permits, unit.toNanos(timeout));
        if (isBroken()) {
            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 23 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 24 with IgniteInterruptedException

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

the class IgniteLockAbstractSelfTest method testConditionInterruptAwait.

/**
 * @throws Exception If failed.
 */
private void testConditionInterruptAwait(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.await();
            } catch (IgniteInterruptedException ignored) {
                isInterrupted = true;
            } finally {
                // Assert that thread was interrupted.
                assertTrue(isInterrupted);
                // Assert that lock is still locked.
                assertTrue(lock0.isLocked());
                // Assert that this thread does own the lock.
                assertTrue(lock0.isHeldByCurrentThread());
                // 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);
    }
    for (Thread t : startedThreads) t.interrupt();
    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 25 with IgniteInterruptedException

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

the class IgniteLockAbstractSelfTest method testTryLock.

/**
 * @throws Exception If failed.
 */
private void testTryLock(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<>();
    lock0.lock();
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            assertFalse(lock0.isHeldByCurrentThread());
            startedThreads.add(Thread.currentThread());
            boolean isInterrupted = false;
            boolean locked = false;
            try {
                locked = lock0.tryLock();
            } catch (IgniteInterruptedException ignored) {
                isInterrupted = true;
                fail("tryLock() method is uninterruptible.");
            } finally {
                // Assert that thread was not interrupted.
                assertFalse(isInterrupted);
                // Assert that lock is locked.
                assertTrue(lock0.isLocked());
                // Assert that this thread does own the lock.
                assertEquals(locked, lock0.isHeldByCurrentThread());
                // Release lock.
                if (locked)
                    lock0.unlock();
            }
            return null;
        }
    }, totalThreads);
    // Wait for all threads to attempt to acquire lock.
    while (startedThreads.size() != totalThreads) {
        Thread.sleep(500);
    }
    for (Thread t : startedThreads) t.interrupt();
    fut.get();
    lock0.unlock();
    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) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) ExpectedException(org.junit.rules.ExpectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException)

Aggregations

IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)26 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)18 IgniteException (org.apache.ignite.IgniteException)10 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 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)3 IgniteClientDisconnectedCheckedException (org.apache.ignite.internal.IgniteClientDisconnectedCheckedException)3 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 IgniteCondition (org.apache.ignite.IgniteCondition)2 IgniteSemaphore (org.apache.ignite.IgniteSemaphore)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2