Search in sources :

Example 31 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 32 with IgniteSemaphore

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

the class IgnitePersistentStoreDataStructuresTest method testSemaphoreVolatility.

/**
 * @throws Exception If failed.
 */
@Test
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) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 33 with IgniteSemaphore

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

the class IgniteClientReconnectAtomicsTest method testSemaphoreReconnect.

/**
 * @throws Exception If failed.
 */
@Test
public void testSemaphoreReconnect() throws Exception {
    Ignite client = grid(serverCount());
    assertTrue(client.cluster().localNode().isClient());
    Ignite srv = ignite(0);
    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) Test(org.junit.Test)

Example 34 with IgniteSemaphore

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

the class IgniteInlineIndexBenchmark method setUp.

/**
 * {@inheritDoc}
 */
@Override
public void setUp(BenchmarkConfiguration cfg) throws Exception {
    super.setUp(cfg);
    range = args.range();
    isJavaObj = args.getBooleanParameter("javaObject", false);
    if (isJavaObj) {
        cacheName = "CACHE_POJO";
        keyCls = TestKey.class;
    } else {
        cacheName = "CACHE_LONG";
        keyCls = Integer.class;
    }
    printParameters();
    IgniteSemaphore sem = ignite().semaphore("setup", 1, true, true);
    try {
        if (sem.tryAcquire()) {
            println(cfg, "Create tables...");
            init();
        } else {
            // Acquire (wait setup by other client) and immediately release/
            println(cfg, "Waits for setup...");
            sem.acquire();
        }
    } finally {
        sem.release();
    }
}
Also used : IgniteSemaphore(org.apache.ignite.IgniteSemaphore)

Example 35 with IgniteSemaphore

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

the class IgniteSqlQueryJoinBenchmark2 method setUp.

/**
 * {@inheritDoc}
 */
@Override
public void setUp(BenchmarkConfiguration cfg) throws Exception {
    super.setUp(cfg);
    String subqueryName = args.getStringParameter(SUBQUERY_PARAM, null);
    if (!F.isEmpty(subqueryName))
        subqry = Subquery.valueOfSafe(subqueryName.trim().toUpperCase());
    if (subqry == null)
        throw new IllegalArgumentException("The \"" + SUBQUERY_PARAM + "\" is mandatory" + " and should be one of the " + Arrays.toString(Subquery.values()));
    if (args.getBooleanParameter(SKIP_INIT_PARAM, false))
        return;
    IgniteSemaphore sem = ignite().semaphore("setup", 1, true, true);
    try {
        if (sem.tryAcquire()) {
            println(cfg, "Create tables...");
            init();
        } else {
            println(cfg, "Waits for setup...");
            sem.acquire();
        }
    } finally {
        sem.release();
    }
    println(cfg, "Done");
}
Also used : IgniteSemaphore(org.apache.ignite.IgniteSemaphore)

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