Search in sources :

Example 11 with IgniteSemaphore

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();
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridKernalContext(org.apache.ignite.internal.GridKernalContext) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) InvalidObjectException(java.io.InvalidObjectException)

Example 12 with IgniteSemaphore

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());
}
Also used : Ignite(org.apache.ignite.Ignite) IgniteSemaphore(org.apache.ignite.IgniteSemaphore)

Example 13 with IgniteSemaphore

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);
    }
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) Ignite(org.apache.ignite.Ignite)

Example 14 with IgniteSemaphore

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");
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) Ignite(org.apache.ignite.Ignite) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException)

Example 15 with IgniteSemaphore

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();
}
Also used : IgniteException(org.apache.ignite.IgniteException) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) IgniteLock(org.apache.ignite.IgniteLock) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException)

Aggregations

IgniteSemaphore (org.apache.ignite.IgniteSemaphore)22 Ignite (org.apache.ignite.Ignite)15 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 IOException (java.io.IOException)4 IgniteException (org.apache.ignite.IgniteException)4 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)4 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)3 Nullable (org.jetbrains.annotations.Nullable)3 ExpectedException (org.junit.rules.ExpectedException)3 ArrayList (java.util.ArrayList)2 Callable (java.util.concurrent.Callable)2 IgniteLock (org.apache.ignite.IgniteLock)2 IgniteCallable (org.apache.ignite.lang.IgniteCallable)2 Closeable (java.io.Closeable)1 InvalidObjectException (java.io.InvalidObjectException)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)1