Search in sources :

Example 26 with IgniteLock

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

the class IgniteLockAbstractSelfTest method removeReentrantLock.

/**
 * @param lockName Reentrant lock name.
 * @throws Exception If failed.
 */
private void removeReentrantLock(String lockName, final boolean fair) throws Exception {
    IgniteLock lock = grid(RND.nextInt(NODES_CNT)).reentrantLock(lockName, false, fair, true);
    assert lock != null;
    // Remove lock on random node.
    IgniteLock lock0 = grid(RND.nextInt(NODES_CNT)).reentrantLock(lockName, false, fair, true);
    assertNotNull(lock0);
    lock0.close();
    // Ensure reentrant lock is removed on all nodes.
    for (Ignite g : G.allGrids()) assertNull(((IgniteKernal) g).context().dataStructures().reentrantLock(lockName, null, false, fair, false));
    checkRemovedReentrantLock(lock);
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteLock(org.apache.ignite.IgniteLock) Ignite(org.apache.ignite.Ignite)

Example 27 with IgniteLock

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

the class SpringCache method get.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public <T> T get(final Object key, final Callable<T> valLdr) {
    Object val = cache.get(key);
    if (val == null) {
        IgniteLock lock = mgr.getSyncLock(cache.getName(), key);
        lock.lock();
        try {
            val = cache.get(key);
            if (val == null) {
                try {
                    T retVal = valLdr.call();
                    val = wrapNull(retVal);
                    cache.put(key, val);
                } catch (Exception e) {
                    throw new ValueRetrievalException(key, valLdr, e);
                }
            }
        } finally {
            lock.unlock();
        }
    }
    return (T) unwrapNull(val);
}
Also used : IgniteLock(org.apache.ignite.IgniteLock)

Example 28 with IgniteLock

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

the class CacheBasedDatasetTest method testPartitionExchangeDuringComputeCall.

/**
 * Tests that partitions of the upstream cache and the partition {@code context} cache are reserved during
 * computations on dataset. Reservation means that partitions won't be unloaded from the node before computation is
 * completed.
 */
public void testPartitionExchangeDuringComputeCall() {
    int partitions = 4;
    IgniteCache<Integer, String> upstreamCache = generateTestData(4, 0);
    CacheBasedDatasetBuilder<Integer, String> builder = new CacheBasedDatasetBuilder<>(ignite, upstreamCache);
    CacheBasedDataset<Integer, String, Long, AutoCloseable> dataset = builder.build((upstream, upstreamSize) -> upstreamSize, (upstream, upstreamSize, ctx) -> null);
    assertTrue("Before computation all partitions should not be reserved", areAllPartitionsNotReserved(upstreamCache.getName(), dataset.getDatasetCache().getName()));
    UUID numOfStartedComputationsId = UUID.randomUUID();
    IgniteAtomicLong numOfStartedComputations = ignite.atomicLong(numOfStartedComputationsId.toString(), 0, true);
    UUID computationsLockId = UUID.randomUUID();
    IgniteLock computationsLock = ignite.reentrantLock(computationsLockId.toString(), false, true, true);
    // lock computations lock to stop computations in the middle
    computationsLock.lock();
    try {
        new Thread(() -> dataset.compute((data, partIndex) -> {
            // track number of started computations
            ignite.atomicLong(numOfStartedComputationsId.toString(), 0, false).incrementAndGet();
            ignite.reentrantLock(computationsLockId.toString(), false, true, false).lock();
            ignite.reentrantLock(computationsLockId.toString(), false, true, false).unlock();
        })).start();
        while (numOfStartedComputations.get() < partitions) {
        }
        assertTrue("During computation all partitions should be reserved", areAllPartitionsReserved(upstreamCache.getName(), dataset.getDatasetCache().getName()));
    } finally {
        computationsLock.unlock();
    }
    assertTrue("All partitions should be released", areAllPartitionsNotReserved(upstreamCache.getName(), dataset.getDatasetCache().getName()));
}
Also used : IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) IgniteLock(org.apache.ignite.IgniteLock) UUID(java.util.UUID)

Example 29 with IgniteLock

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

the class CacheBasedDatasetTest method testPartitionExchangeDuringComputeWithCtxCall.

/**
 * Tests that partitions of the upstream cache and the partition {@code context} cache are reserved during
 * computations on dataset. Reservation means that partitions won't be unloaded from the node before computation is
 * completed.
 */
public void testPartitionExchangeDuringComputeWithCtxCall() {
    int partitions = 4;
    IgniteCache<Integer, String> upstreamCache = generateTestData(4, 0);
    CacheBasedDatasetBuilder<Integer, String> builder = new CacheBasedDatasetBuilder<>(ignite, upstreamCache);
    CacheBasedDataset<Integer, String, Long, AutoCloseable> dataset = builder.build((upstream, upstreamSize) -> upstreamSize, (upstream, upstreamSize, ctx) -> null);
    assertTrue("Before computation all partitions should not be reserved", areAllPartitionsNotReserved(upstreamCache.getName(), dataset.getDatasetCache().getName()));
    UUID numOfStartedComputationsId = UUID.randomUUID();
    IgniteAtomicLong numOfStartedComputations = ignite.atomicLong(numOfStartedComputationsId.toString(), 0, true);
    UUID computationsLockId = UUID.randomUUID();
    IgniteLock computationsLock = ignite.reentrantLock(computationsLockId.toString(), false, true, true);
    // lock computations lock to stop computations in the middle
    computationsLock.lock();
    try {
        new Thread(() -> dataset.computeWithCtx((ctx, data, partIndex) -> {
            // track number of started computations
            ignite.atomicLong(numOfStartedComputationsId.toString(), 0, false).incrementAndGet();
            ignite.reentrantLock(computationsLockId.toString(), false, true, false).lock();
            ignite.reentrantLock(computationsLockId.toString(), false, true, false).unlock();
        })).start();
        while (numOfStartedComputations.get() < partitions) {
        }
        assertTrue("During computation all partitions should be reserved", areAllPartitionsReserved(upstreamCache.getName(), dataset.getDatasetCache().getName()));
    } finally {
        computationsLock.unlock();
    }
    assertTrue("All partitions should be released", areAllPartitionsNotReserved(upstreamCache.getName(), dataset.getDatasetCache().getName()));
}
Also used : IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) IgniteLock(org.apache.ignite.IgniteLock) UUID(java.util.UUID)

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