Search in sources :

Example 11 with IgniteLock

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

the class IgniteLockAbstractSelfTest method testIsolation.

/**
     * Implementation of ignite data structures internally uses special system caches, need make sure
     * that transaction on these system caches do not intersect with transactions started by user.
     *
     * @throws Exception If failed.
     */
public void testIsolation() throws Exception {
    Ignite ignite = grid(0);
    CacheConfiguration cfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    cfg.setName("myCache");
    cfg.setAtomicityMode(TRANSACTIONAL);
    cfg.setWriteSynchronizationMode(FULL_SYNC);
    IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(cfg);
    try {
        IgniteLock lock = ignite.reentrantLock("lock", true, true, true);
        try (Transaction tx = ignite.transactions().txStart()) {
            cache.put(1, 1);
            boolean success = lock.tryLock(1, MILLISECONDS);
            assertTrue(success);
            tx.rollback();
        }
        assertEquals(0, cache.size());
        assertTrue(lock.isLocked());
        lock.unlock();
        assertFalse(lock.isLocked());
        lock.close();
        assertTrue(lock.removed());
    } finally {
        ignite.destroyCache(cfg.getName());
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteLock(org.apache.ignite.IgniteLock) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 12 with IgniteLock

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

the class IgniteLocalLockSelfTest method testReentrantLock.

/** {@inheritDoc} */
@Override
public void testReentrantLock() throws Exception {
    // Test main functionality.
    IgniteLock lock = grid(0).reentrantLock("lock", true, false, true);
    assertNotNull(lock);
    assertEquals(0, lock.getHoldCount());
    lock.lock();
    IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            IgniteLock lock = grid(0).reentrantLock("lock", true, false, true);
            assert lock != null;
            info("Thread is going to wait on lock: " + Thread.currentThread().getName());
            assert lock.tryLock(1, MINUTES);
            info("Thread is again runnable: " + Thread.currentThread().getName());
            lock.unlock();
            return null;
        }
    }, THREADS_CNT, "test-thread");
    Thread.sleep(3000);
    assert lock.isLocked();
    assert lock.getHoldCount() == 1;
    lock.lock();
    assert lock.isLocked();
    assert lock.getHoldCount() == 2;
    lock.unlock();
    assert lock.isLocked();
    assert lock.getHoldCount() == 1;
    lock.unlock();
    // Ensure there are no hangs.
    fut.get();
    // Test operations on removed lock.
    IgniteLock lock0 = grid(0).reentrantLock("lock", true, false, false);
    assertNotNull(lock0);
    lock0.close();
    checkRemovedReentrantLock(lock0);
}
Also used : IgniteLock(org.apache.ignite.IgniteLock) Nullable(org.jetbrains.annotations.Nullable)

Example 13 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 14 with IgniteLock

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

the class IgniteLockAbstractSelfTest method testHasQueuedThreads.

/**
     * @throws Exception If failed.
     */
private void testHasQueuedThreads(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 = 5;
    final Set<Thread> startedThreads = new GridConcurrentHashSet<>();
    final Set<Thread> finishedThreads = new GridConcurrentHashSet<>();
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            assertFalse(lock0.isHeldByCurrentThread());
            startedThreads.add(Thread.currentThread());
            lock0.lock();
            // Wait until every thread tries to lock.
            do {
                Thread.sleep(1000);
            } while (startedThreads.size() != totalThreads);
            try {
                info("Acquired in separate thread. ");
                assertTrue(lock0.isHeldByCurrentThread());
                assertFalse(lock0.hasQueuedThread(Thread.currentThread()));
                finishedThreads.add(Thread.currentThread());
                if (startedThreads.size() != finishedThreads.size()) {
                    assertTrue(lock0.hasQueuedThreads());
                }
                for (Thread t : startedThreads) {
                    assertTrue(lock0.hasQueuedThread(t) != finishedThreads.contains(t));
                }
            } finally {
                lock0.unlock();
                assertFalse(lock0.isHeldByCurrentThread());
            }
            return null;
        }
    }, totalThreads);
    fut.get();
    assertFalse(lock0.hasQueuedThreads());
    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) ExpectedException(org.junit.rules.ExpectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException)

Example 15 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)26 IgniteException (org.apache.ignite.IgniteException)17 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)12 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 IgniteCountDownLatch (org.apache.ignite.IgniteCountDownLatch)3 IgniteSemaphore (org.apache.ignite.IgniteSemaphore)3 Nullable (org.jetbrains.annotations.Nullable)3 ArrayList (java.util.ArrayList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 IgniteEx (org.apache.ignite.internal.IgniteEx)2 IgniteClosure (org.apache.ignite.lang.IgniteClosure)2