Search in sources :

Example 6 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 7 with IgniteLock

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

the class IgniteLockAbstractSelfTest method testLockSerialization.

/**
 * @throws Exception If failed.
 */
public void testLockSerialization() throws Exception {
    final IgniteLock lock = grid(0).reentrantLock("lock", true, true, true);
    info("Lock created: " + lock);
    lock.isFailoverSafe();
    lock.isFair();
    grid(ThreadLocalRandom.current().nextInt(G.allGrids().size())).compute().broadcast(new IgniteCallable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            Thread.sleep(1000);
            lock.lock();
            try {
                info("Inside lock: " + lock.getHoldCount());
            } finally {
                lock.unlock();
            }
            return null;
        }
    });
}
Also used : IgniteLock(org.apache.ignite.IgniteLock) Nullable(org.jetbrains.annotations.Nullable) 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 8 with IgniteLock

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

the class IgniteLockAbstractSelfTest method checkFailoverSafe.

/**
 * This method only checks if parameter of new reentrant lock is initialized properly.
 * For tests considering failure recovery see @GridCachePartitionedNodeFailureSelfTest.
 *
 * @throws Exception Exception.
 */
private void checkFailoverSafe(final boolean fair) throws Exception {
    // Checks only if reentrant lock is initialized properly
    IgniteLock lock = createReentrantLock("rmv", true, fair);
    assert lock.isFailoverSafe();
    removeReentrantLock("rmv", fair);
    IgniteLock lock1 = createReentrantLock("rmv1", false, fair);
    assert !lock1.isFailoverSafe();
    removeReentrantLock("rmv1", fair);
}
Also used : IgniteLock(org.apache.ignite.IgniteLock)

Example 9 with IgniteLock

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

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