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