Search in sources :

Example 21 with IgniteLock

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

the class IgniteLockAbstractSelfTest method testReentrantLockMultinode1.

/**
 * @throws Exception If failed.
 */
private void testReentrantLockMultinode1(final boolean fair) throws Exception {
    if (gridCount() == 1)
        return;
    IgniteLock lock = grid(0).reentrantLock("s1", true, fair, true);
    List<IgniteInternalFuture<?>> futs = new ArrayList<>();
    for (int i = 0; i < gridCount(); i++) {
        final Ignite ignite = grid(i);
        futs.add(GridTestUtils.runAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                IgniteLock lock = ignite.reentrantLock("s1", true, fair, false);
                assertNotNull(lock);
                IgniteCondition cond1 = lock.getOrCreateCondition("c1");
                IgniteCondition cond2 = lock.getOrCreateCondition("c2");
                try {
                    boolean wait = lock.tryLock(30_000, MILLISECONDS);
                    assertTrue(wait);
                    cond2.signal();
                    cond1.await();
                } finally {
                    lock.unlock();
                }
                return null;
            }
        }));
    }
    boolean done = false;
    while (!done) {
        done = true;
        for (IgniteInternalFuture<?> fut : futs) {
            if (!fut.isDone())
                done = false;
        }
        try {
            lock.lock();
            lock.getOrCreateCondition("c1").signal();
            lock.getOrCreateCondition("c2").await(10, MILLISECONDS);
        } finally {
            lock.unlock();
        }
    }
    for (IgniteInternalFuture<?> fut : futs) fut.get(30_000);
}
Also used : ArrayList(java.util.ArrayList) IgniteLock(org.apache.ignite.IgniteLock) Ignite(org.apache.ignite.Ignite) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCondition(org.apache.ignite.IgniteCondition) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable)

Example 22 with IgniteLock

use of org.apache.ignite.IgniteLock 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 23 with IgniteLock

use of org.apache.ignite.IgniteLock 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 24 with IgniteLock

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

the class IgniteLockAbstractSelfTest method checkReentrantLock.

/**
 * @throws Exception If failed.
 */
private void checkReentrantLock(final boolean fair) throws Exception {
    // Test API.
    checkLock(fair);
    checkFailoverSafe(fair);
    // Test main functionality.
    IgniteLock lock1 = grid(0).reentrantLock("lock", true, fair, true);
    assertFalse(lock1.isLocked());
    lock1.lock();
    IgniteFuture<Object> fut = grid(0).compute().callAsync(new IgniteCallable<Object>() {

        @IgniteInstanceResource
        private Ignite ignite;

        @LoggerResource
        private IgniteLogger log;

        @Nullable
        @Override
        public Object call() throws Exception {
            // Test reentrant lock in multiple threads on each node.
            IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

                @Nullable
                @Override
                public Object call() throws Exception {
                    IgniteLock lock = ignite.reentrantLock("lock", true, fair, true);
                    assert lock != null;
                    log.info("Thread is going to wait on reentrant lock: " + Thread.currentThread().getName());
                    assert lock.tryLock(1, MINUTES);
                    log.info("Thread is again runnable: " + Thread.currentThread().getName());
                    lock.unlock();
                    return null;
                }
            }, 5, "test-thread");
            fut.get();
            return null;
        }
    });
    Thread.sleep(3000);
    assert lock1.isHeldByCurrentThread();
    assert lock1.getHoldCount() == 1;
    lock1.lock();
    assert lock1.isHeldByCurrentThread();
    assert lock1.getHoldCount() == 2;
    lock1.unlock();
    lock1.unlock();
    // Ensure there are no hangs.
    fut.get();
    // Test operations on removed lock.
    lock1.close();
    checkRemovedReentrantLock(lock1);
}
Also used : LoggerResource(org.apache.ignite.resources.LoggerResource) 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) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteLock(org.apache.ignite.IgniteLock) Ignite(org.apache.ignite.Ignite) IgniteLogger(org.apache.ignite.IgniteLogger) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with IgniteLock

use of org.apache.ignite.IgniteLock 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

IgniteLock (org.apache.ignite.IgniteLock)29 IgniteException (org.apache.ignite.IgniteException)18 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)15 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)15 IOException (java.io.IOException)14 ExpectedException (org.junit.rules.ExpectedException)14 Ignite (org.apache.ignite.Ignite)13 GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)8 IgniteCondition (org.apache.ignite.IgniteCondition)5 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)5 Callable (java.util.concurrent.Callable)4 IgniteCallable (org.apache.ignite.lang.IgniteCallable)4 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)3 IgniteCountDownLatch (org.apache.ignite.IgniteCountDownLatch)3 IgniteSemaphore (org.apache.ignite.IgniteSemaphore)3 Nullable (org.jetbrains.annotations.Nullable)3 ArrayList (java.util.ArrayList)2 UUID (java.util.UUID)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2