Search in sources :

Example 16 with LongAdder8

use of org.jsr166.LongAdder8 in project ignite by apache.

the class GridUnsafeMemorySelfTest method testGuardedOps.

/**
     * @throws Exception if failed.
     */
public void testGuardedOps() throws Exception {
    final int lineSize = 16;
    final int ptrsCnt = 4;
    final AtomicReferenceArray<CmpMem> ptrs = new AtomicReferenceArray<>(ptrsCnt * lineSize);
    final AtomicBoolean finished = new AtomicBoolean();
    final LongAdder8 cntr = new LongAdder8();
    final GridUnsafeGuard guard = new GridUnsafeGuard();
    GridRandom rnd = new GridRandom();
    for (int a = 0; a < 7; a++) {
        finished.set(false);
        int threads = 2 + rnd.nextInt(37);
        int time = rnd.nextInt(5);
        X.println("__ starting threads: " + threads + " time: " + time + " sec");
        final LongAdder8 locAdder = new LongAdder8();
        IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                Random rnd = new GridRandom();
                while (!finished.get()) {
                    int idx = rnd.nextInt(ptrsCnt) * lineSize;
                    guard.begin();
                    try {
                        final CmpMem old;
                        CmpMem ptr = null;
                        switch(rnd.nextInt(6)) {
                            case 0:
                                ptr = new CmpMem(cntr);
                            //noinspection fallthrough
                            case 1:
                                old = ptrs.getAndSet(idx, ptr);
                                if (old != null) {
                                    guard.finalizeLater(new Runnable() {

                                        @Override
                                        public void run() {
                                            old.deallocate();
                                        }
                                    });
                                }
                                break;
                            case 2:
                                if (rnd.nextBoolean())
                                    ptr = new CmpMem(cntr);
                                old = ptrs.getAndSet(idx, ptr);
                                if (old != null)
                                    guard.releaseLater(old);
                                break;
                            default:
                                old = ptrs.get(idx);
                                if (old != null)
                                    old.touch();
                        }
                    } finally {
                        guard.end();
                        locAdder.increment();
                    }
                }
                return null;
            }
        }, threads);
        Thread.sleep(1000 * time);
        X.println("__ stopping ops...");
        finished.set(true);
        fut.get();
        X.println("__ stopped, performed ops: " + locAdder.sum());
        for (int i = 0; i < ptrs.length(); i++) {
            CmpMem ptr = ptrs.getAndSet(i, null);
            if (ptr != null) {
                ptr.touch();
                ptr.deallocate();
            }
        }
        X.println("__ " + guard);
        assertEquals(0, cntr.sum());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) LongAdder8(org.jsr166.LongAdder8)

Example 17 with LongAdder8

use of org.jsr166.LongAdder8 in project ignite by apache.

the class GridIoManagerBenchmark0 method testThroughput.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings("deprecation")
public void testThroughput() throws Exception {
    final IgniteKernal sndKernal = (IgniteKernal) grid(0);
    final IgniteKernal rcvKernal = (IgniteKernal) grid(1);
    final ClusterNode sndNode = sndKernal.localNode();
    final ClusterNode rcvNode = rcvKernal.localNode();
    final GridIoManager snd = sndKernal.context().io();
    final GridIoManager rcv = rcvKernal.context().io();
    info("Senders: " + THREADS);
    info("Messages: " + CONCUR_MSGS);
    final Semaphore sem = new Semaphore(CONCUR_MSGS);
    final LongAdder8 msgCntr = new LongAdder8();
    final String topic = "test-topic";
    rcv.addMessageListener(topic, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg) {
            try {
                rcv.sendToCustomTopic(sndNode, topic, (Message) msg, PUBLIC_POOL);
            } catch (IgniteCheckedException e) {
                error("Failed to send message.", e);
            }
        }
    });
    snd.addMessageListener(topic, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg) {
            msgCntr.increment();
            sem.release();
        }
    });
    Timer t = new Timer("results-reporter");
    t.schedule(new TimerTask() {

        private long ts = System.currentTimeMillis();

        @Override
        public void run() {
            long newTs = System.currentTimeMillis();
            long qrys = msgCntr.sumThenReset();
            long time = newTs - ts;
            X.println("Communication benchmark [qps=" + qrys * 1000 / time + ", executed=" + qrys + ", time=" + time + ']');
            ts = newTs;
        }
    }, 10000, 10000);
    final AtomicBoolean finish = new AtomicBoolean();
    IgniteInternalFuture<?> f = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                IgniteUuid msgId = IgniteUuid.randomUuid();
                while (!finish.get()) {
                    sem.acquire();
                    snd.sendToCustomTopic(rcvNode, topic, new GridTestMessage(msgId, (String) null), PUBLIC_POOL);
                }
            } catch (IgniteCheckedException e) {
                X.println("Message send failed", e);
            } catch (InterruptedException ignored) {
            // No-op.
            }
            return null;
        }
    }, THREADS, "send-thread");
    Thread.sleep(TEST_TIMEOUT);
    finish.set(true);
    sem.release(CONCUR_MSGS * 2);
    t.cancel();
    f.get();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) Message(org.apache.ignite.plugin.extensions.communication.Message) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) Semaphore(java.util.concurrent.Semaphore) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Timer(java.util.Timer) TimerTask(java.util.TimerTask) GridIoManager(org.apache.ignite.internal.managers.communication.GridIoManager) IgniteUuid(org.apache.ignite.lang.IgniteUuid) UUID(java.util.UUID) LongAdder8(org.jsr166.LongAdder8)

Example 18 with LongAdder8

use of org.jsr166.LongAdder8 in project ignite by apache.

the class GridIoManagerBenchmark0 method testVariableLoad.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings("deprecation")
public void testVariableLoad() throws Exception {
    final IgniteKernal sndKernal = (IgniteKernal) grid(0);
    final IgniteKernal rcvKernal = (IgniteKernal) grid(1);
    final ClusterNode sndNode = sndKernal.localNode();
    final ClusterNode rcvNode = rcvKernal.localNode();
    final GridIoManager snd = sndKernal.context().io();
    final GridIoManager rcv = rcvKernal.context().io();
    info("Senders: " + THREADS);
    info("Messages: " + CONCUR_MSGS);
    final Semaphore sem = new Semaphore(CONCUR_MSGS);
    final LongAdder8 msgCntr = new LongAdder8();
    final String topic = "test-topic";
    final Map<IgniteUuid, CountDownLatch> latches = new ConcurrentHashMap8<>();
    rcv.addMessageListener(topic, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg) {
            try {
                rcv.sendToCustomTopic(sndNode, topic, (Message) msg, PUBLIC_POOL);
            } catch (IgniteCheckedException e) {
                error("Failed to send message.", e);
            }
        }
    });
    snd.addMessageListener(topic, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg) {
            msgCntr.increment();
            sem.release();
            CountDownLatch latch = latches.get(((GridTestMessage) msg).id());
            if (latch != null)
                latch.countDown();
        }
    });
    final AtomicBoolean finish = new AtomicBoolean();
    final AtomicReference<CountDownLatch> latchRef = new AtomicReference<>();
    IgniteInternalFuture<?> f = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finish.get()) {
                CountDownLatch latch = latchRef.get();
                if (latch != null)
                    U.await(latch);
                IgniteUuid msgId = IgniteUuid.randomUuid();
                sem.acquire();
                snd.sendToCustomTopic(rcvNode, topic, new GridTestMessage(msgId, (String) null), PUBLIC_POOL);
            }
            return null;
        }
    }, THREADS, "send-thread");
    IgniteInternalFuture<?> f1 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        private long ts = System.currentTimeMillis();

        @Override
        public Object call() throws Exception {
            try {
                while (!finish.get()) {
                    info(U.nl() + ">>>" + U.nl() + ">>> High load." + U.nl() + ">>>");
                    U.sleep(15 * 1000);
                    reportNumbers();
                    info(U.nl() + ">>>" + U.nl() + ">>> Low load." + U.nl() + ">>>");
                    CountDownLatch latch = new CountDownLatch(1);
                    try {
                        // Here will be a pause.
                        latchRef.set(latch);
                        U.sleep(7 * 1000);
                        reportNumbers();
                    } finally {
                        latch.countDown();
                    }
                }
            } catch (IgniteCheckedException e) {
                X.println("Message send failed", e);
            }
            return null;
        }

        /**
             *
             */
        void reportNumbers() {
            long newTs = System.currentTimeMillis();
            long qrys = msgCntr.sumThenReset();
            long time = newTs - ts;
            X.println("Communication benchmark [qps=" + qrys * 1000 / time + ", executed=" + qrys + ", time=" + time + ']');
            ts = newTs;
        }
    }, 1, "load-dispatcher");
    IgniteInternalFuture<?> f2 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finish.get()) {
                U.sleep(1000);
                IgniteUuid msgId = IgniteUuid.randomUuid();
                CountDownLatch latch = new CountDownLatch(1);
                latches.put(msgId, latch);
                snd.sendToCustomTopic(rcvNode, topic, new GridTestMessage(msgId, (String) null), PUBLIC_POOL);
                long start = System.currentTimeMillis();
                latch.await();
                info("Response time: " + (System.currentTimeMillis() - start));
            }
            return null;
        }
    }, THREADS, "low-loader");
    Thread.sleep(TEST_TIMEOUT);
    finish.set(true);
    sem.release(CONCUR_MSGS * 2);
    f.get();
    f1.get();
    f2.get();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) Message(org.apache.ignite.plugin.extensions.communication.Message) ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridIoManager(org.apache.ignite.internal.managers.communication.GridIoManager) IgniteUuid(org.apache.ignite.lang.IgniteUuid) UUID(java.util.UUID) LongAdder8(org.jsr166.LongAdder8)

Example 19 with LongAdder8

use of org.jsr166.LongAdder8 in project ignite by apache.

the class GridTestCacheStore method loadCache.

/**
     * Preload data from store. In this case we just auto-generate random values.
     *
     * @param clo Callback for every key.
     * @param args Optional arguments.
     */
@Override
public void loadCache(final IgniteBiInClosure<GridTestKey, Long> clo, Object... args) {
    // Number of threads is passed in as argument by caller.
    final int numThreads = (Integer) args[0];
    int entryCnt = (Integer) args[1];
    log.info("Number of load threads: " + numThreads);
    log.info("Number of cache entries to load: " + entryCnt);
    ExecutorService execSvc = Executors.newFixedThreadPool(numThreads);
    try {
        ExecutorCompletionService<Object> completeSvc = new ExecutorCompletionService<>(execSvc);
        final IgniteCache<GridTestKey, Long> cache = ignite.cache("partitioned");
        assert cache != null;
        final LongAdder8 adder = new LongAdder8();
        for (int i = 0; i < numThreads; i++) {
            final int threadId = i;
            final int perThreadKeys = entryCnt / numThreads;
            final int mod = entryCnt % numThreads;
            completeSvc.submit(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    int start = threadId * perThreadKeys;
                    int end = start + perThreadKeys;
                    if (threadId + 1 == numThreads)
                        end += mod;
                    for (long i = start; i < end; i++) {
                        if (ignite.affinity(cache.getName()).mapKeyToNode(GridTestKey.affinityKey(i)).isLocal()) {
                            // Only add if key is local.
                            clo.apply(new GridTestKey(i), i);
                            adder.increment();
                        }
                        if (i % 10000 == 0)
                            log.info("Loaded " + adder.intValue() + " keys.");
                    }
                    return null;
                }
            });
        }
        // Wait for threads to complete.
        for (int i = 0; i < numThreads; i++) {
            try {
                completeSvc.take().get();
            } catch (InterruptedException | ExecutionException e) {
                throw new CacheLoaderException(e);
            }
        }
        // Final print out.
        log.info("Loaded " + adder.intValue() + " keys.");
    } finally {
        execSvc.shutdown();
    }
}
Also used : ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) CacheLoaderException(javax.cache.integration.CacheLoaderException) ExecutionException(java.util.concurrent.ExecutionException) CacheLoaderException(javax.cache.integration.CacheLoaderException) ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException) LongAdder8(org.jsr166.LongAdder8)

Example 20 with LongAdder8

use of org.jsr166.LongAdder8 in project ignite by apache.

the class GridCircularBufferPerformanceTest method testThroughput.

/**
     * @throws Exception If failed.
     */
public void testThroughput() throws Exception {
    int size = 256 * 1024;
    final GridCircularBuffer<Integer> buf = new GridCircularBuffer<>(size);
    final LongAdder8 cnt = new LongAdder8();
    final AtomicBoolean finished = new AtomicBoolean();
    multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finished.get()) {
                U.sleep(5000);
                info("Ops/sec: " + cnt.sumThenReset() / 5);
            }
            return null;
        }
    }, 1);
    multithreaded(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finished.get()) {
                buf.add(1);
                cnt.increment();
            }
            return null;
        }
    }, 8);
    info("Buffer: " + buf);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridCircularBuffer(org.apache.ignite.internal.util.GridCircularBuffer) LongAdder8(org.jsr166.LongAdder8)

Aggregations

LongAdder8 (org.jsr166.LongAdder8)24 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 UUID (java.util.UUID)5 ExecutorService (java.util.concurrent.ExecutorService)5 Random (java.util.Random)4 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)4 ClusterNode (org.apache.ignite.cluster.ClusterNode)4 IgniteUuid (org.apache.ignite.lang.IgniteUuid)4 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 Callable (java.util.concurrent.Callable)3 Ignite (org.apache.ignite.Ignite)3 IgniteKernal (org.apache.ignite.internal.IgniteKernal)3 GridIoManager (org.apache.ignite.internal.managers.communication.GridIoManager)3 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)3 Message (org.apache.ignite.plugin.extensions.communication.Message)3 ConcurrentLinkedDeque8 (org.jsr166.ConcurrentLinkedDeque8)3 IOException (java.io.IOException)2 Timer (java.util.Timer)2