Search in sources :

Example 16 with IgniteSemaphore

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

the class IgnitePersistentStoreDataStructuresTest method testSemaphoreVolatility.

/**
 * @throws Exception If failed.
 */
public void testSemaphoreVolatility() throws Exception {
    Ignite ignite = startGrids(4);
    ignite.active(true);
    IgniteSemaphore sem = ignite.semaphore("test", 10, false, true);
    assert sem != null;
    stopAllGrids();
    ignite = startGrids(4);
    ignite.active(true);
    sem = ignite.semaphore("test", 10, false, false);
    assert sem == null;
}
Also used : Ignite(org.apache.ignite.Ignite) IgniteSemaphore(org.apache.ignite.IgniteSemaphore)

Example 17 with IgniteSemaphore

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

the class IgniteDataStructureUniqueNameTest method testUniqueName.

/**
 * @param singleGrid If {@code true} uses single grid.
 * @throws Exception If failed.
 */
private void testUniqueName(final boolean singleGrid) throws Exception {
    final String name = IgniteUuid.randomUuid().toString();
    final int DS_TYPES = 6;
    final int THREADS = DS_TYPES * 3;
    for (int iter = 0; iter < 20; iter++) {
        log.info("Iteration: " + iter);
        List<IgniteInternalFuture<Object>> futs = new ArrayList<>(THREADS);
        final CyclicBarrier barrier = new CyclicBarrier(THREADS);
        for (int i = 0; i < THREADS; i++) {
            final int idx = i;
            IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    try {
                        Thread.currentThread().setName("test thread-" + idx);
                        barrier.await();
                        Ignite ignite = singleGrid ? ignite(0) : ignite(idx % gridCount());
                        Object res;
                        switch(idx % DS_TYPES) {
                            case 0:
                                log.info("Create atomic long, grid: " + ignite.name());
                                res = ignite.atomicLong(name, 0, true);
                                break;
                            case 1:
                                log.info("Create atomic sequence, grid: " + ignite.name());
                                res = ignite.atomicSequence(name, 0, true);
                                break;
                            case 2:
                                log.info("Create atomic stamped, grid: " + ignite.name());
                                res = ignite.atomicStamped(name, 0, true, true);
                                break;
                            case 3:
                                log.info("Create atomic reference, grid: " + ignite.name());
                                res = ignite.atomicReference(name, null, true);
                                break;
                            case 4:
                                log.info("Create queue, grid: " + ignite.name());
                                res = ignite.queue(name, 0, config(false));
                                break;
                            case 5:
                                log.info("Create set, grid: " + ignite.name());
                                res = ignite.set(name, config(false));
                                break;
                            default:
                                fail();
                                return null;
                        }
                        log.info("Thread created: " + res);
                        return res;
                    } catch (IgniteException e) {
                        log.info("Failed: " + e);
                        return e;
                    }
                }
            });
            futs.add(fut);
        }
        Closeable dataStructure = null;
        int createdCnt = 0;
        for (IgniteInternalFuture<Object> fut : futs) {
            Object res = fut.get();
            if (res instanceof IgniteException || res instanceof IgniteCheckedException)
                continue;
            assertTrue("Unexpected object: " + res, res instanceof IgniteAtomicLong || res instanceof IgniteAtomicSequence || res instanceof IgniteAtomicReference || res instanceof IgniteAtomicStamped || res instanceof IgniteCountDownLatch || res instanceof IgniteQueue || res instanceof IgniteSet || res instanceof IgniteSemaphore || res instanceof IgniteLock);
            log.info("Data structure created: " + dataStructure);
            createdCnt++;
            if (dataStructure != null)
                assertEquals(dataStructure.getClass(), res.getClass());
            else
                dataStructure = (Closeable) res;
        }
        assertNotNull(dataStructure);
        assertEquals(3, createdCnt);
        dataStructure.close();
    }
}
Also used : IgniteAtomicReference(org.apache.ignite.IgniteAtomicReference) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CyclicBarrier(java.util.concurrent.CyclicBarrier) IgniteSet(org.apache.ignite.IgniteSet) IgniteAtomicStamped(org.apache.ignite.IgniteAtomicStamped) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteQueue(org.apache.ignite.IgniteQueue) IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) Ignite(org.apache.ignite.Ignite) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) IgniteLock(org.apache.ignite.IgniteLock)

Example 18 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));
    try (IgniteSemaphore semaphore = creator.semaphore("semaphore1", -1, true, true)) {
        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);
                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());
    }
    assertNull(creator.semaphore("semaphore1", 1, true, false));
    assertNull(other.semaphore("semaphore1", 1, true, false));
}
Also used : IgniteSemaphore(org.apache.ignite.IgniteSemaphore) IgniteException(org.apache.ignite.IgniteException)

Example 19 with IgniteSemaphore

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

the class IgniteSemaphoreAbstractSelfTest method checkAcquire.

/**
 * @throws Exception Exception.
 */
private void checkAcquire() throws Exception {
    // Check only 'false' cases here. Successful await is tested over the grid.
    IgniteSemaphore semaphore = createSemaphore("acquire", 5, false);
    assert !semaphore.tryAcquire(10);
    assert !semaphore.tryAcquire(10, 10, MICROSECONDS);
    removeSemaphore("acquire");
}
Also used : IgniteSemaphore(org.apache.ignite.IgniteSemaphore)

Example 20 with IgniteSemaphore

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

the class IgniteSemaphoreAbstractSelfTest method checkSemaphoreSerialization.

/**
 * @throws Exception If failed.
 */
private void checkSemaphoreSerialization() throws Exception {
    final IgniteSemaphore sem = grid(0).semaphore("semaphore", -gridCount() + 1, true, true);
    assertEquals(-gridCount() + 1, sem.availablePermits());
    grid(0).compute().broadcast(new IgniteCallable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            sem.release();
            return null;
        }
    });
    assert sem.availablePermits() == 1;
    sem.acquire();
    assert sem.availablePermits() == 0;
    sem.release();
    // Test operations on removed semaphore.
    sem.close();
    checkRemovedSemaphore(sem);
}
Also used : IgniteSemaphore(org.apache.ignite.IgniteSemaphore) Nullable(org.jetbrains.annotations.Nullable) ExpectedException(org.junit.rules.ExpectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException)

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