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