use of org.apache.ignite.IgniteLock in project ignite by apache.
the class IgniteLockAbstractSelfTest method testReentrantLockMultinode1.
/**
* @throws Exception If failed.
*/
private void testReentrantLockMultinode1(final boolean fair) throws Exception {
if (gridCount() == 1)
return;
IgniteLock lock = grid(0).reentrantLock("s1", true, fair, true);
List<IgniteInternalFuture<?>> futs = new ArrayList<>();
for (int i = 0; i < gridCount(); i++) {
final Ignite ignite = grid(i);
futs.add(GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
IgniteLock lock = ignite.reentrantLock("s1", true, fair, false);
assertNotNull(lock);
IgniteCondition cond1 = lock.getOrCreateCondition("c1");
IgniteCondition cond2 = lock.getOrCreateCondition("c2");
try {
boolean wait = lock.tryLock(30_000, MILLISECONDS);
assertTrue(wait);
cond2.signal();
cond1.await();
} finally {
lock.unlock();
}
return null;
}
}));
}
boolean done = false;
while (!done) {
done = true;
for (IgniteInternalFuture<?> fut : futs) {
if (!fut.isDone())
done = false;
}
try {
lock.lock();
lock.getOrCreateCondition("c1").signal();
lock.getOrCreateCondition("c2").await(10, MILLISECONDS);
} finally {
lock.unlock();
}
}
for (IgniteInternalFuture<?> fut : futs) fut.get(30_000);
}
use of org.apache.ignite.IgniteLock in project ignite by apache.
the class IgniteLockAbstractSelfTest method testConditionAwaitUninterruptibly.
/**
* @throws Exception If failed.
*/
private void testConditionAwaitUninterruptibly(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.awaitUninterruptibly();
} catch (IgniteInterruptedException ignored) {
isInterrupted = true;
} finally {
// Assert that thread was not interrupted.
assertFalse(isInterrupted);
// Assert that lock is still locked.
assertTrue(lock0.isLocked());
// Assert that this thread does own the lock.
assertTrue(lock0.isHeldByCurrentThread());
// Clear interrupt flag.
assertTrue(Thread.interrupted());
// 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);
}
lock0.lock();
for (Thread t : startedThreads) {
t.interrupt();
lock0.getOrCreateCondition("cond").signal();
}
lock0.unlock();
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 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 checkReentrantLock.
/**
* @throws Exception If failed.
*/
private void checkReentrantLock(final boolean fair) throws Exception {
// Test API.
checkLock(fair);
checkFailoverSafe(fair);
// Test main functionality.
IgniteLock lock1 = grid(0).reentrantLock("lock", true, fair, true);
assertFalse(lock1.isLocked());
lock1.lock();
IgniteFuture<Object> fut = grid(0).compute().callAsync(new IgniteCallable<Object>() {
@IgniteInstanceResource
private Ignite ignite;
@LoggerResource
private IgniteLogger log;
@Nullable
@Override
public Object call() throws Exception {
// Test reentrant lock in multiple threads on each node.
IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Nullable
@Override
public Object call() throws Exception {
IgniteLock lock = ignite.reentrantLock("lock", true, fair, true);
assert lock != null;
log.info("Thread is going to wait on reentrant lock: " + Thread.currentThread().getName());
assert lock.tryLock(1, MINUTES);
log.info("Thread is again runnable: " + Thread.currentThread().getName());
lock.unlock();
return null;
}
}, 5, "test-thread");
fut.get();
return null;
}
});
Thread.sleep(3000);
assert lock1.isHeldByCurrentThread();
assert lock1.getHoldCount() == 1;
lock1.lock();
assert lock1.isHeldByCurrentThread();
assert lock1.getHoldCount() == 2;
lock1.unlock();
lock1.unlock();
// Ensure there are no hangs.
fut.get();
// Test operations on removed lock.
lock1.close();
checkRemovedReentrantLock(lock1);
}
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