use of org.apache.ignite.IgniteSemaphore in project ignite by apache.
the class GridCacheSemaphoreImpl method readResolve.
/**
* Reconstructs object on unmarshalling.
*
* @return Reconstructed object.
* @throws ObjectStreamException Thrown in case of unmarshalling error.
*/
private Object readResolve() throws ObjectStreamException {
try {
IgniteBiTuple<GridKernalContext, String> t = stash.get();
IgniteSemaphore sem = IgnitionEx.localIgnite().context().dataStructures().semaphore(t.get2(), null, 0, false, false);
if (sem == null)
throw new IllegalStateException("Semaphore was not found on deserialization: " + t.get2());
return sem;
} catch (IgniteCheckedException e) {
throw U.withCause(new InvalidObjectException(e.getMessage()), e);
} finally {
stash.remove();
}
}
use of org.apache.ignite.IgniteSemaphore in project ignite by apache.
the class IgniteClientReconnectAtomicsTest method testSemaphoreReconnect.
/**
* @throws Exception If failed.
*/
public void testSemaphoreReconnect() throws Exception {
Ignite client = grid(serverCount());
assertTrue(client.cluster().localNode().isClient());
Ignite srv = clientRouter(client);
IgniteSemaphore clientSemaphore = client.semaphore("semaphore1", 3, false, true);
assertEquals(3, clientSemaphore.availablePermits());
final IgniteSemaphore srvSemaphore = srv.semaphore("semaphore1", 3, false, false);
assertEquals(3, srvSemaphore.availablePermits());
reconnectClientNode(client, srv, new Runnable() {
@Override
public void run() {
srvSemaphore.acquire();
}
});
assertEquals(2, srvSemaphore.availablePermits());
assertEquals(2, clientSemaphore.availablePermits());
srvSemaphore.acquire();
assertEquals(1, srvSemaphore.availablePermits());
assertEquals(1, clientSemaphore.availablePermits());
clientSemaphore.acquire();
assertEquals(0, srvSemaphore.availablePermits());
assertEquals(0, clientSemaphore.availablePermits());
assertFalse(srvSemaphore.tryAcquire());
assertFalse(srvSemaphore.tryAcquire());
}
use of org.apache.ignite.IgniteSemaphore in project ignite by apache.
the class GridCacheAbstractDataStructuresFailoverSelfTest method testSemaphoreFailoverSafe.
/**
* @throws Exception If failed.
*/
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 testSemaphoreNonFailoverSafe.
/**
* @throws Exception If failed.
*/
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");
}
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 {
client = true;
Ignite client = startGrid(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();
}
Aggregations