Search in sources :

Example 26 with IgniteSemaphore

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

Example 27 with IgniteSemaphore

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));
    }
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) ArrayList(java.util.ArrayList) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) Ignite(org.apache.ignite.Ignite) IgniteException(org.apache.ignite.IgniteException)

Example 28 with IgniteSemaphore

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

Example 29 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 {
    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();
}
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)

Example 30 with IgniteSemaphore

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");
}
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) Test(org.junit.Test)

Aggregations

IgniteSemaphore (org.apache.ignite.IgniteSemaphore)35 Ignite (org.apache.ignite.Ignite)19 Test (org.junit.Test)11 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)8 IOException (java.io.IOException)5 IgniteException (org.apache.ignite.IgniteException)5 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)4 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)4 IgniteCallable (org.apache.ignite.lang.IgniteCallable)4 Nullable (org.jetbrains.annotations.Nullable)4 ExpectedException (org.junit.rules.ExpectedException)4 ArrayList (java.util.ArrayList)3 Callable (java.util.concurrent.Callable)3 IgniteCountDownLatch (org.apache.ignite.IgniteCountDownLatch)3 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)3 IgniteEx (org.apache.ignite.internal.IgniteEx)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)2 IgniteAtomicReference (org.apache.ignite.IgniteAtomicReference)2