Search in sources :

Example 6 with IgniteSemaphore

use of org.apache.ignite.IgniteSemaphore in project ignite by apache.

the class IgniteLocalSemaphoreSelfTest method testSemaphore.

/**
 * {@inheritDoc}
 */
@Override
public void testSemaphore() throws Exception {
    // Test main functionality.
    IgniteSemaphore semaphore = grid(0).semaphore("semaphore", -2, false, true);
    assertNotNull(semaphore);
    assertEquals(-2, semaphore.availablePermits());
    IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            IgniteSemaphore semaphore = grid(0).semaphore("semaphore", -2, false, true);
            assert semaphore != null && semaphore.availablePermits() == -2;
            info("Thread is going to wait on semaphore: " + Thread.currentThread().getName());
            assert semaphore.tryAcquire(1, 1, MINUTES);
            info("Thread is again runnable: " + Thread.currentThread().getName());
            semaphore.release();
            return null;
        }
    }, THREADS_CNT, "test-thread");
    Thread.sleep(3000);
    assert semaphore.availablePermits() == -2;
    semaphore.release(2);
    assert semaphore.availablePermits() == 0;
    semaphore.release();
    // Ensure there are no hangs.
    fut.get();
    // Test operations on removed latch.
    IgniteSemaphore semaphore0 = grid(0).semaphore("semaphore", 0, false, false);
    assertNotNull(semaphore0);
    semaphore0.close();
    checkRemovedSemaphore(semaphore0);
}
Also used : IgniteSemaphore(org.apache.ignite.IgniteSemaphore) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with IgniteSemaphore

use of org.apache.ignite.IgniteSemaphore in project ignite by apache.

the class IgniteSemaphoreExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        int nodeCount = ignite.cluster().forServers().nodes().size();
        // Number of producers should be equal to number of consumers.
        // This value should not exceed overall number of public thread pools in a cluster,
        // otherwise blocking consumer jobs can occupy all the threads leading to starvation.
        int jobCount = ignite.configuration().getPublicThreadPoolSize() * nodeCount / 2;
        System.out.println();
        System.out.println(">>> Cache atomic semaphore example started.");
        // Initialize semaphore.
        IgniteSemaphore syncSemaphore = ignite.semaphore(SEM_NAME, 0, false, true);
        // Make name of semaphore.
        final String semaphoreName = UUID.randomUUID().toString();
        // Initialize semaphore.
        IgniteSemaphore semaphore = ignite.semaphore(semaphoreName, 0, false, true);
        // Start consumers on all cluster nodes.
        for (int i = 0; i < jobCount; i++) ignite.compute().runAsync(new Consumer(semaphoreName));
        // Start producers on all cluster nodes.
        for (int i = 0; i < jobCount; i++) ignite.compute().runAsync(new Producer(semaphoreName));
        System.out.println("Master node is waiting for all other nodes to finish...");
        // Wait for everyone to finish.
        syncSemaphore.acquire(2 * jobCount);
    }
    System.out.flush();
    System.out.println();
    System.out.println("Finished semaphore example...");
    System.out.println("Check all nodes for output (this node is also part of the cluster).");
}
Also used : Ignite(org.apache.ignite.Ignite) IgniteSemaphore(org.apache.ignite.IgniteSemaphore)

Example 8 with IgniteSemaphore

use of org.apache.ignite.IgniteSemaphore in project ignite by apache.

the class SemaphoreFailoverSafeReleasePermitsTest 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);
        // Initialize second semaphore before the first one is broken.
        IgniteSemaphore sem2 = grid(1).semaphore("sem", 1, true, true);
        assertEquals(1, sem.availablePermits());
        sem.acquire(1);
        assertEquals(0, sem.availablePermits());
        ignite.close();
        awaitPartitionMapExchange();
        assertTrue(sem2.tryAcquire(1, 5000, TimeUnit.MILLISECONDS));
    } finally {
        stopAllGrids();
    }
}
Also used : Ignite(org.apache.ignite.Ignite) IgniteSemaphore(org.apache.ignite.IgniteSemaphore)

Example 9 with IgniteSemaphore

use of org.apache.ignite.IgniteSemaphore in project ignite by apache.

the class GridCacheAbstractDataStructuresFailoverSelfTest method testSemaphoreSingleNodeFailure.

/**
 * @throws Exception If failed.
 */
public void testSemaphoreSingleNodeFailure() throws Exception {
    fail("https://issues.apache.org/jira/browse/IGNITE-5975");
    final Ignite i1 = grid(0);
    IgniteSemaphore sem1 = i1.semaphore(STRUCTURE_NAME, 1, false, true);
    sem1.acquire();
    IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            boolean failed = true;
            IgniteSemaphore sem2 = i1.semaphore(STRUCTURE_NAME, 1, false, true);
            try {
                sem2.acquire();
            } catch (Exception ignored) {
                failed = false;
            } finally {
                assertFalse(failed);
                sem2.release();
            }
            return null;
        }
    });
    while (!sem1.hasQueuedThreads()) {
        try {
            Thread.sleep(1);
        } catch (InterruptedException ignored) {
            fail();
        }
    }
    i1.close();
    fut.get();
}
Also used : Ignite(org.apache.ignite.Ignite) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException)

Example 10 with IgniteSemaphore

use of org.apache.ignite.IgniteSemaphore in project ignite by apache.

the class GridCacheAbstractDataStructuresFailoverSelfTest method doTestSemaphore.

/**
 * @throws Exception If failed.
 */
private void doTestSemaphore(ConstantTopologyChangeWorker topWorker, final boolean failoverSafe) throws Exception {
    final int permits = topWorker instanceof MultipleTopologyChangeWorker || topWorker instanceof PartitionedMultipleTopologyChangeWorker ? TOP_CHANGE_THREAD_CNT * 3 : TOP_CHANGE_CNT;
    try (IgniteSemaphore s = grid(0).semaphore(STRUCTURE_NAME, permits, failoverSafe, true)) {
        IgniteInternalFuture<?> fut = topWorker.startChangingTopology(new IgniteClosure<Ignite, Object>() {

            @Override
            public Object apply(Ignite ignite) {
                IgniteSemaphore sem = ignite.semaphore(STRUCTURE_NAME, permits, failoverSafe, false);
                while (true) {
                    try {
                        sem.acquire(1);
                        break;
                    } catch (IgniteInterruptedException e) {
                        // Exception may happen in non failover safe mode.
                        if (failoverSafe)
                            throw e;
                        else {
                            // and should always be discarded after exception is caught.
                            break;
                        }
                    }
                }
                return null;
            }
        });
        while (!fut.isDone()) {
            while (true) {
                try {
                    s.acquire(1);
                    break;
                } catch (IgniteInterruptedException e) {
                    // Exception may happen in non failover safe mode.
                    if (failoverSafe)
                        throw e;
                    else {
                        // and should always be discarded after exception is caught.
                        break;
                    }
                }
            }
            assert s.availablePermits() < permits;
            s.release();
            assert s.availablePermits() <= permits;
        }
        fut.get();
        // Semaphore is left in proper state only if failoverSafe mode is used.
        if (failoverSafe) {
            for (Ignite g : G.allGrids()) assertEquals(permits, g.semaphore(STRUCTURE_NAME, permits, false, false).availablePermits());
        }
    }
}
Also used : IgniteSemaphore(org.apache.ignite.IgniteSemaphore) Ignite(org.apache.ignite.Ignite) 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