Search in sources :

Example 6 with IgniteCountDownLatch

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

the class IgniteTransactionalWriteInvokeBenchmark method setUp.

/** {@inheritDoc} */
@Override
public void setUp(BenchmarkConfiguration cfg) throws Exception {
    super.setUp(cfg);
    assert args.keysCount() > 0 : "Count of keys: " + args.keysCount();
    IgniteCountDownLatch latch = ignite().countDownLatch("DATA-POPULATED-LATCH-" + cacheName(), 1, true, true);
    if (cfg.memberId() == 0) {
        println(cfg, "Populating data for cache: " + cacheName());
        long start = System.nanoTime();
        try (IgniteDataStreamer<String, Long> dataLdr = ignite().dataStreamer(cacheName())) {
            for (int k = 0; k < args.range() && !Thread.currentThread().isInterrupted(); k++) {
                dataLdr.addData("key-" + k + "-master", INITIAL_VALUE);
                for (int i = 0; i < args.keysCount(); i++) dataLdr.addData("key-" + k + "-" + i, INITIAL_VALUE);
                if (k % 100000 == 0)
                    println(cfg, "Populated accounts: " + k);
            }
        }
        println(cfg, "Finished populating data in " + ((System.nanoTime() - start) / 1_000_000) + " ms. for cache: " + cacheName());
        latch.countDown();
    } else {
        println(cfg, "Waiting for populating data in cache by driver with id 0: " + cacheName());
        boolean success = latch.await(TIMEOUT_SEC, TimeUnit.SECONDS);
        if (!success)
            throw new IllegalStateException("Failed to wait that data populating finish.");
        println(cfg, "Finished waiting for populating data in cache by driver with id 0: " + cacheName());
    }
}
Also used : IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch)

Example 7 with IgniteCountDownLatch

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

the class IgniteLockAbstractSelfTest method testFairness.

/**
     * Tests if lock is evenly acquired among nodes when fair flag is set on.
     * Since exact ordering of lock acquisitions cannot be guaranteed because it also depends
     * on the OS thread scheduling, certain deviation from uniform distribution is tolerated.
     * @throws Exception If failed.
     */
public void testFairness() throws Exception {
    if (gridCount() == 1)
        return;
    // Total number of ops.
    final long opsCount = 10000;
    // Allowed deviation from uniform distribution.
    final double tolerance = 0.05;
    // Shared counter.
    final String OPS_COUNTER = "ops_counter";
    // Number of threads, one per node.
    final int threadCount = gridCount();
    final AtomicLong threadCounter = new AtomicLong(0);
    Ignite ignite = startGrid(gridCount());
    // Initialize reentrant lock.
    IgniteLock l = ignite.reentrantLock("lock", true, true, true);
    // Initialize OPS_COUNTER.
    ignite.getOrCreateCache(OPS_COUNTER).put(OPS_COUNTER, (long) 0);
    final Map<Integer, Long> counts = new ConcurrentHashMap<>();
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            final int localNodeId = (int) threadCounter.getAndIncrement();
            final Ignite grid = grid(localNodeId);
            IgniteClosure<Ignite, Long> closure = new IgniteClosure<Ignite, Long>() {

                @Override
                public Long apply(Ignite ignite) {
                    IgniteLock l = ignite.reentrantLock("lock", true, true, true);
                    long localCount = 0;
                    IgniteCountDownLatch latch = ignite.countDownLatch("latch", threadCount, false, true);
                    latch.countDown();
                    latch.await();
                    while (true) {
                        l.lock();
                        try {
                            long opsCounter = (long) ignite.getOrCreateCache(OPS_COUNTER).get(OPS_COUNTER);
                            if (opsCounter == opsCount)
                                break;
                            ignite.getOrCreateCache(OPS_COUNTER).put(OPS_COUNTER, ++opsCounter);
                            localCount++;
                            if (localCount > 1000) {
                                assertTrue(localCount < (1 + tolerance) * opsCounter / threadCount);
                                assertTrue(localCount > (1 - tolerance) * opsCounter / threadCount);
                            }
                            if (localCount % 100 == 0) {
                                info("Node [id=" + ignite.cluster().localNode().id() + "] acquired " + localCount + " times. " + "Total ops count: " + opsCounter + "/" + opsCount + "]");
                            }
                        } finally {
                            l.unlock();
                        }
                    }
                    return localCount;
                }
            };
            long localCount = closure.apply(grid);
            counts.put(localNodeId, localCount);
            return null;
        }
    }, threadCount);
    fut.get();
    long totalSum = 0;
    for (int i = 0; i < gridCount(); i++) {
        totalSum += counts.get(i);
        info("Node " + grid(i).localNode().id() + " acquired the lock " + counts.get(i) + " times. ");
    }
    assertEquals(totalSum, opsCount);
    l.close();
    ignite.close();
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) ExpectedException(org.junit.rules.ExpectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) Ignite(org.apache.ignite.Ignite) IgniteLock(org.apache.ignite.IgniteLock) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 8 with IgniteCountDownLatch

use of org.apache.ignite.IgniteCountDownLatch 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 = 9;
    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 latch, grid: " + ignite.name());
                                res = ignite.countDownLatch(name, 0, true, true);
                                break;
                            case 4:
                                log.info("Create atomic reference, grid: " + ignite.name());
                                res = ignite.atomicReference(name, null, true);
                                break;
                            case 5:
                                log.info("Create queue, grid: " + ignite.name());
                                res = ignite.queue(name, 0, config(false));
                                break;
                            case 6:
                                log.info("Create set, grid: " + ignite.name());
                                res = ignite.set(name, config(false));
                                break;
                            case 7:
                                log.info("Create atomic semaphore, grid: " + ignite.name());
                                res = ignite.semaphore(name, 0, false, true);
                                break;
                            case 8:
                                log.info("Create atomic reentrant lock, grid: " + ignite.name());
                                res = ignite.reentrantLock(name, true, true, true);
                                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 9 with IgniteCountDownLatch

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

the class IgniteLocalCountDownLatchSelfTest method testLatch.

/** {@inheritDoc} */
@Override
public void testLatch() throws Exception {
    // Test main functionality.
    IgniteCountDownLatch latch = grid(0).countDownLatch("latch", 2, false, true);
    assertNotNull(latch);
    assertEquals(2, latch.count());
    IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

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

Example 10 with IgniteCountDownLatch

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

the class IgniteCountDownLatchAbstractSelfTest method testLatchMultinode2.

/**
     * @throws Exception If failed.
     */
public void testLatchMultinode2() throws Exception {
    if (gridCount() == 1)
        return;
    IgniteCountDownLatch latch = grid(0).countDownLatch("l2", gridCount() * 3, true, true);
    assertNotNull(latch);
    List<IgniteInternalFuture<?>> futs = new ArrayList<>();
    final AtomicInteger cnt = new AtomicInteger();
    for (int i = 0; i < gridCount(); i++) {
        final Ignite ignite = grid(i);
        futs.add(GridTestUtils.runAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                IgniteCountDownLatch latch = ignite.countDownLatch("l2", 10, true, false);
                assertNotNull(latch);
                for (int i = 0; i < 3; i++) {
                    cnt.incrementAndGet();
                    latch.countDown();
                }
                boolean wait = latch.await(30_000);
                assertEquals(gridCount() * 3, cnt.get());
                assertEquals(0, latch.count());
                assertTrue(wait);
                return null;
            }
        }));
    }
    for (IgniteInternalFuture<?> fut : futs) fut.get(30_000);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) Ignite(org.apache.ignite.Ignite) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable)

Aggregations

IgniteCountDownLatch (org.apache.ignite.IgniteCountDownLatch)20 Ignite (org.apache.ignite.Ignite)14 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)5 Callable (java.util.concurrent.Callable)4 IgniteCallable (org.apache.ignite.lang.IgniteCallable)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 IgniteException (org.apache.ignite.IgniteException)3 IgniteLock (org.apache.ignite.IgniteLock)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)2 IgniteAtomicSequence (org.apache.ignite.IgniteAtomicSequence)2 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)2 IgniteClosure (org.apache.ignite.lang.IgniteClosure)2 Nullable (org.jetbrains.annotations.Nullable)2 ExpectedException (org.junit.rules.ExpectedException)2 Closeable (java.io.Closeable)1