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);
}
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);
}
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()));
}
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()));
}
Aggregations