use of org.apache.ignite.IgniteSemaphore in project ignite by apache.
the class SemaphoreFailoverNoWaitingAcquirerTest method doTest.
/**
* @throws Exception If failed.
*/
private void doTest() throws Exception {
try {
startGrids(GRID_CNT);
Ignite ignite = grid(0);
IgniteSemaphore sem = ignite.semaphore("sem", 1, true, true);
assertEquals(1, sem.availablePermits());
sem.acquire(1);
assertEquals(0, sem.availablePermits());
ignite.close();
awaitPartitionMapExchange();
IgniteSemaphore sem2 = grid(1).semaphore("sem", 1, true, true);
assertTrue("Could not aquire after 'restart'", sem2.tryAcquire(1, 5000, TimeUnit.MILLISECONDS));
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.IgniteSemaphore in project ignite by apache.
the class IgniteClientDataStructuresAbstractTest method testSemaphore.
/**
* @param creator Creator node.
* @param other Other node.
* @throws Exception If failed.
*/
private void testSemaphore(Ignite creator, final Ignite other) throws Exception {
assertNull(creator.semaphore("semaphore1", 1, true, false));
assertNull(other.semaphore("semaphore1", 1, true, false));
final List<IgniteSemaphore> semaphores = new ArrayList(2);
try (IgniteSemaphore semaphore = creator.semaphore("semaphore1", -1, true, true)) {
semaphores.add(semaphore);
assertNotNull(semaphore);
assertEquals(-1, semaphore.availablePermits());
IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
U.sleep(1000);
IgniteSemaphore semaphore0 = other.semaphore("semaphore1", -1, true, false);
semaphores.add(semaphore0);
assertEquals(-1, semaphore0.availablePermits());
log.info("Release semaphore.");
semaphore0.release(2);
return null;
}
});
log.info("Acquire semaphore.");
assertTrue(semaphore.tryAcquire(1, 5000, TimeUnit.MILLISECONDS));
log.info("Finished wait.");
fut.get();
assertEquals(0, semaphore.availablePermits());
}
for (IgniteSemaphore semaphore : semaphores) {
try {
semaphore.release();
fail("Operations with closed semaphore must fail");
} catch (Throwable ignore) {
// No-op.
}
}
for (Ignite ignite : F.asList(creator, other)) {
assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return ignite.semaphore("semaphore1", 1, true, false) == null;
}
}, 3_000L));
}
}
use of org.apache.ignite.IgniteSemaphore in project ignite by apache.
the class GridCacheAbstractDataStructuresFailoverSelfTest method testSemaphoreFailoverSafe.
/**
* @throws Exception If failed.
*/
@Test
public void testSemaphoreFailoverSafe() throws Exception {
try (final IgniteSemaphore semaphore = grid(0).semaphore(STRUCTURE_NAME, 20, true, true)) {
Ignite g = startGrid(NEW_IGNITE_INSTANCE_NAME);
IgniteSemaphore semaphore2 = g.semaphore(STRUCTURE_NAME, 20, true, false);
assertEquals(20, semaphore2.availablePermits());
semaphore2.acquire(10);
stopGrid(NEW_IGNITE_INSTANCE_NAME);
waitForCondition(new PA() {
@Override
public boolean apply() {
return semaphore.availablePermits() == 20;
}
}, 2000);
}
}
use of org.apache.ignite.IgniteSemaphore in project ignite by apache.
the class GridCacheAbstractDataStructuresFailoverSelfTest method testReentrantLockFailsWhenServersLeft.
/**
* @throws Exception If failed.
*/
public void testReentrantLockFailsWhenServersLeft(final boolean fair) throws Exception {
Ignite client = startClientGrid(gridCount());
Ignite server = grid(0);
// Initialize lock.
IgniteLock srvLock = server.reentrantLock("lock", true, fair, true);
IgniteSemaphore semaphore = server.semaphore("sync", 0, true, true);
IgniteFuture fut = client.compute().applyAsync(new IgniteClosure<Ignite, Object>() {
@Override
public Object apply(Ignite ignite) {
final IgniteLock l = ignite.reentrantLock("lock", true, fair, true);
l.lock();
assertTrue(l.isHeldByCurrentThread());
l.unlock();
assertFalse(l.isHeldByCurrentThread());
// Signal the server to go down.
ignite.semaphore("sync", 0, true, true).release();
boolean isExceptionThrown = false;
try {
// Wait for the server to go down.
Thread.sleep(1000);
l.lock();
fail("Exception must be thrown.");
} catch (InterruptedException ignored) {
fail("Interrupted exception not expected here.");
} catch (IgniteException ignored) {
isExceptionThrown = true;
} finally {
assertTrue(isExceptionThrown);
assertFalse(l.isHeldByCurrentThread());
}
return null;
}
}, client);
// Wait for the lock on client to be acquired then released.
semaphore.acquire();
for (int i = 0; i < gridCount(); i++) stopGrid(i);
fut.get();
client.close();
}
use of org.apache.ignite.IgniteSemaphore in project ignite by apache.
the class GridCacheAbstractDataStructuresFailoverSelfTest method testSemaphoreNonFailoverSafe.
/**
* @throws Exception If failed.
*/
@Test
public void testSemaphoreNonFailoverSafe() throws Exception {
try (IgniteSemaphore sem = grid(0).semaphore(STRUCTURE_NAME, 20, false, true)) {
Ignite g = startGrid(NEW_IGNITE_INSTANCE_NAME);
IgniteSemaphore sem2 = g.semaphore(STRUCTURE_NAME, 20, false, false);
sem2.acquire(20);
assertEquals(0, sem.availablePermits());
new Timer().schedule(new TimerTask() {
@Override
public void run() {
stopGrid(NEW_IGNITE_INSTANCE_NAME);
}
}, 2000);
try {
sem.acquire(1);
} catch (IgniteInterruptedException ignored) {
// Expected exception.
return;
}
}
fail("Thread hasn't been interrupted");
}
Aggregations