Search in sources :

Example 6 with IgniteInterruptedException

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

the class IgniteLockAbstractSelfTest method testLock.

/**
     * @throws Exception If failed.
     */
private void testLock(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;
            try {
                lock0.lock();
            } catch (IgniteInterruptedException ignored) {
                isInterrupted = true;
                fail("Lock() method is uninterruptible.");
            } finally {
                // Assert that thread was not interrupted.
                assertFalse(isInterrupted);
                // Assert that interrupted flag is set and clear it in order to call unlock().
                assertTrue(Thread.interrupted());
                // Assert that lock is still owned by this thread.
                assertTrue(lock0.isLocked());
                // Assert that this thread does own the lock.
                assertTrue(lock0.isHeldByCurrentThread());
                // Release lock.
                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();
    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) 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 7 with IgniteInterruptedException

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

the class IgniteLockAbstractSelfTest method testLockInterruptiblyMultinode.

/**
     * @throws Exception If failed.
     */
private void testLockInterruptiblyMultinode(final boolean fair) throws Exception {
    if (gridCount() == 1)
        return;
    // Initialize reentrant lock.
    final IgniteLock lock0 = grid(0).reentrantLock("lock", true, fair, true);
    assertEquals(0, lock0.getHoldCount());
    assertFalse(lock0.hasQueuedThreads());
    lock0.lock();
    // Number of threads, one per node.
    final int threadCount = gridCount();
    final AtomicLong threadCounter = new AtomicLong(0);
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            final int localNodeId = (int) threadCounter.getAndIncrement();
            final Ignite grid = grid(localNodeId);
            IgniteClosure<Ignite, Void> closure = new IgniteClosure<Ignite, Void>() {

                @Override
                public Void apply(Ignite ignite) {
                    final IgniteLock l = ignite.reentrantLock("lock", true, true, true);
                    final AtomicReference<Thread> thread = new AtomicReference<>();
                    final AtomicBoolean done = new AtomicBoolean(false);
                    final AtomicBoolean exceptionThrown = new AtomicBoolean(false);
                    final IgniteCountDownLatch latch = ignite.countDownLatch("latch", threadCount, false, true);
                    IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {

                        @Override
                        public Void call() throws Exception {
                            try {
                                thread.set(Thread.currentThread());
                                l.lockInterruptibly();
                            } catch (IgniteInterruptedException ignored) {
                                exceptionThrown.set(true);
                            } finally {
                                done.set(true);
                            }
                            return null;
                        }
                    });
                    // Wait until l.lock() has been called.
                    while (!l.hasQueuedThreads()) {
                    // No-op.
                    }
                    latch.countDown();
                    latch.await();
                    thread.get().interrupt();
                    while (!done.get()) {
                    // No-op.
                    }
                    try {
                        fut.get();
                    } catch (IgniteCheckedException e) {
                        fail(e.getMessage());
                        throw new RuntimeException(e);
                    }
                    assertTrue(exceptionThrown.get());
                    return null;
                }
            };
            closure.apply(grid);
            return null;
        }
    }, threadCount);
    fut.get();
    lock0.unlock();
    info("Checking if interrupted threads are removed from global waiting queue...");
    // Check if interrupted threads are removed from global waiting queue.
    boolean locked = lock0.tryLock(1000, MILLISECONDS);
    info("Interrupted threads successfully removed from global waiting queue. ");
    assertTrue(locked);
    lock0.unlock();
    assertFalse(lock0.isLocked());
    lock0.close();
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) AtomicReference(java.util.concurrent.atomic.AtomicReference) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) ExpectedException(org.junit.rules.ExpectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteLock(org.apache.ignite.IgniteLock) Ignite(org.apache.ignite.Ignite)

Example 8 with IgniteInterruptedException

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

the class IgniteLockAbstractSelfTest method testTryLockTimed.

/**
     * @throws Exception If failed.
     */
private void testTryLockTimed(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(100, TimeUnit.MILLISECONDS);
            } catch (IgniteInterruptedException ignored) {
                isInterrupted = true;
            } finally {
                // Assert that thread was not interrupted.
                assertFalse(isInterrupted);
                // Assert that tryLock returned false.
                assertFalse(locked);
                // Assert that lock is still owned by main thread.
                assertTrue(lock0.isLocked());
                // Assert that this thread doesn't own the lock.
                assertFalse(lock0.isHeldByCurrentThread());
                // Release lock.
                if (locked)
                    lock0.unlock();
            }
            return null;
        }
    }, totalThreads);
    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)

Example 9 with IgniteInterruptedException

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

the class IgniteLockAbstractSelfTest method testLockInterruptibly.

/**
     * @throws Exception If failed.
     */
private void testLockInterruptibly(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;
            try {
                lock0.lockInterruptibly();
            } catch (IgniteInterruptedException ignored) {
                assertFalse(Thread.currentThread().isInterrupted());
                isInterrupted = true;
            } finally {
                // Assert that thread was interrupted.
                assertTrue(isInterrupted);
                // Assert that locked is still owned by main thread.
                assertTrue(lock0.isLocked());
                // Assert that this thread doesn't own the lock.
                assertFalse(lock0.isHeldByCurrentThread());
            }
            return null;
        }
    }, totalThreads);
    // Wait for all threads to attempt to acquire lock.
    while (startedThreads.size() != totalThreads) {
        Thread.sleep(1000);
    }
    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)

Example 10 with IgniteInterruptedException

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

the class GridH2AbstractKeyValueRow method syncValue.

/**
     * @param waitTime Time to await for value unswap.
     * @return Synchronized value.
     */
protected synchronized Value syncValue(long waitTime) {
    Value v = peekValue(VAL_COL);
    while (v == null && waitTime > 0) {
        // This call must be quite rare, so performance is not a concern.
        long start = System.nanoTime();
        try {
            // Wait for value arrival to allow other threads to make a progress.
            wait(waitTime);
        } catch (InterruptedException e) {
            throw new IgniteInterruptedException(e);
        }
        long t = System.nanoTime() - start;
        if (t > 0)
            waitTime -= TimeUnit.NANOSECONDS.toMillis(t);
        v = peekValue(VAL_COL);
    }
    return v;
}
Also used : Value(org.h2.value.Value) 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