use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.
the class IgniteLockAbstractSelfTest method testLock.
/**
* @throws Exception If failed.
*/
private void testLock(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;
try {
lock0.lock();
} catch (IgniteInterruptedException ignored) {
isInterrupted = true;
fail("Lock() method is uninterruptible.");
} finally {
// Assert that thread was not interrupted.
assertFalse(isInterrupted);
// Assert that interrupted flag is set and clear it in order to call unlock().
assertTrue(Thread.interrupted());
// Assert that lock is still owned by this thread.
assertTrue(lock0.isLocked());
// Assert that this thread does own the lock.
assertTrue(lock0.isHeldByCurrentThread());
// Release lock.
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();
lock0.unlock();
fut.get();
assertFalse(lock0.isLocked());
for (Thread t : startedThreads) assertFalse(lock0.hasQueuedThread(t));
lock0.close();
}
use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.
the class IgniteLockAbstractSelfTest method testLockInterruptiblyMultinode.
/**
* @throws Exception If failed.
*/
private void testLockInterruptiblyMultinode(final boolean fair) throws Exception {
if (gridCount() == 1)
return;
// Initialize reentrant lock.
final IgniteLock lock0 = grid(0).reentrantLock("lock", true, fair, true);
assertEquals(0, lock0.getHoldCount());
assertFalse(lock0.hasQueuedThreads());
lock0.lock();
// Number of threads, one per node.
final int threadCount = gridCount();
final AtomicLong threadCounter = new AtomicLong(0);
IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
final int localNodeId = (int) threadCounter.getAndIncrement();
final Ignite grid = grid(localNodeId);
IgniteClosure<Ignite, Void> closure = new IgniteClosure<Ignite, Void>() {
@Override
public Void apply(Ignite ignite) {
final IgniteLock l = ignite.reentrantLock("lock", true, true, true);
final AtomicReference<Thread> thread = new AtomicReference<>();
final AtomicBoolean done = new AtomicBoolean(false);
final AtomicBoolean exceptionThrown = new AtomicBoolean(false);
final IgniteCountDownLatch latch = ignite.countDownLatch("latch", threadCount, false, true);
IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
try {
thread.set(Thread.currentThread());
l.lockInterruptibly();
} catch (IgniteInterruptedException ignored) {
exceptionThrown.set(true);
} finally {
done.set(true);
}
return null;
}
});
// Wait until l.lock() has been called.
while (!l.hasQueuedThreads()) {
// No-op.
}
latch.countDown();
latch.await();
thread.get().interrupt();
while (!done.get()) {
// No-op.
}
try {
fut.get();
} catch (IgniteCheckedException e) {
fail(e.getMessage());
throw new RuntimeException(e);
}
assertTrue(exceptionThrown.get());
return null;
}
};
closure.apply(grid);
return null;
}
}, threadCount);
fut.get();
lock0.unlock();
info("Checking if interrupted threads are removed from global waiting queue...");
// Check if interrupted threads are removed from global waiting queue.
boolean locked = lock0.tryLock(1000, MILLISECONDS);
info("Interrupted threads successfully removed from global waiting queue. ");
assertTrue(locked);
lock0.unlock();
assertFalse(lock0.isLocked());
lock0.close();
}
use of org.apache.ignite.IgniteInterruptedException 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.IgniteInterruptedException in project ignite by apache.
the class IgniteLockAbstractSelfTest method testLockInterruptibly.
/**
* @throws Exception If failed.
*/
private void testLockInterruptibly(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;
try {
lock0.lockInterruptibly();
} catch (IgniteInterruptedException ignored) {
assertFalse(Thread.currentThread().isInterrupted());
isInterrupted = true;
} finally {
// Assert that thread was interrupted.
assertTrue(isInterrupted);
// Assert that locked is still owned by main thread.
assertTrue(lock0.isLocked());
// Assert that this thread doesn't own the lock.
assertFalse(lock0.isHeldByCurrentThread());
}
return null;
}
}, totalThreads);
// Wait for all threads to attempt to acquire lock.
while (startedThreads.size() != totalThreads) {
Thread.sleep(1000);
}
for (Thread t : startedThreads) t.interrupt();
fut.get();
lock0.unlock();
assertFalse(lock0.isLocked());
for (Thread t : startedThreads) assertFalse(lock0.hasQueuedThread(t));
lock0.close();
}
use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.
the class GridH2AbstractKeyValueRow method syncValue.
/**
* @param waitTime Time to await for value unswap.
* @return Synchronized value.
*/
protected synchronized Value syncValue(long waitTime) {
Value v = peekValue(VAL_COL);
while (v == null && waitTime > 0) {
// This call must be quite rare, so performance is not a concern.
long start = System.nanoTime();
try {
// Wait for value arrival to allow other threads to make a progress.
wait(waitTime);
} catch (InterruptedException e) {
throw new IgniteInterruptedException(e);
}
long t = System.nanoTime() - start;
if (t > 0)
waitTime -= TimeUnit.NANOSECONDS.toMillis(t);
v = peekValue(VAL_COL);
}
return v;
}
Aggregations