Search in sources :

Example 31 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project ignite by apache.

the class IgnitePdsTransactionsHangTest method testTransactionsDontHang.

/**
 * Copied from customers benchmark.
 *
 * @throws Exception If failed.
 */
public void testTransactionsDontHang() throws Exception {
    try {
        final Ignite g = startGrids(2);
        g.active(true);
        g.getOrCreateCache(getCacheConfiguration());
        ExecutorService threadPool = Executors.newFixedThreadPool(THREADS_CNT);
        final CyclicBarrier cyclicBarrier = new CyclicBarrier(THREADS_CNT);
        final AtomicBoolean interrupt = new AtomicBoolean(false);
        final LongAdder operationCnt = new LongAdder();
        final IgniteCache<Long, TestEntity> cache = g.cache(CACHE_NAME);
        for (int i = 0; i < THREADS_CNT; i++) {
            threadPool.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        ThreadLocalRandom locRandom = ThreadLocalRandom.current();
                        cyclicBarrier.await();
                        while (!interrupt.get()) {
                            long randomKey = locRandom.nextLong(MAX_KEY_COUNT);
                            TestEntity entity = TestEntity.newTestEntity(locRandom);
                            try (Transaction tx = g.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                                cache.put(randomKey, entity);
                                tx.commit();
                            }
                            operationCnt.increment();
                        }
                    } catch (Throwable e) {
                        log.error("Unexpected exception:", e);
                        throw new RuntimeException(e);
                    }
                }
            });
        }
        long stopTime = System.currentTimeMillis() + DURATION * 1000;
        long totalOperations = 0;
        int periods = 0;
        long max = Long.MIN_VALUE, min = Long.MAX_VALUE;
        while (System.currentTimeMillis() < stopTime) {
            U.sleep(1000);
            long sum = operationCnt.sumThenReset();
            periods++;
            if (periods > WARM_UP_PERIOD) {
                totalOperations += sum;
                max = Math.max(max, sum);
                min = Math.min(min, sum);
                log.info("Operation count: " + sum + " min=" + min + " max=" + max + " avg=" + totalOperations / (periods - WARM_UP_PERIOD));
            }
        }
        interrupt.set(true);
        threadPool.shutdown();
        log.info("Test complete");
        threadPool.awaitTermination(getTestTimeout(), TimeUnit.MILLISECONDS);
        IgniteTxManager tm = internalCache(cache).context().tm();
        assertEquals("There are still active transactions", 0, tm.activeTransactions().size());
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteTxManager(org.apache.ignite.internal.processors.cache.transactions.IgniteTxManager) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongAdder(java.util.concurrent.atomic.LongAdder) Transaction(org.apache.ignite.transactions.Transaction) ExecutorService(java.util.concurrent.ExecutorService) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Example 32 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project ignite by apache.

the class SortedEvictionPolicyPerformanceTest method testThroughput.

/**
 * Tests throughput.
 */
public void testThroughput() throws Exception {
    final LongAdder cnt = new LongAdder();
    final AtomicBoolean finished = new AtomicBoolean();
    final int pPut = P_PUT;
    final int pGet = P_PUT + P_GET;
    final IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);
    multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            for (; ; ) {
                U.sleep(1000);
                info("Ops/sec: " + cnt.sumThenReset());
            }
        }
    }, 1);
    multithreaded(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finished.get()) {
                int p = RND.nextInt(100);
                int key = RND.nextInt(KEYS);
                if (p >= 0 && p < pPut)
                    cache.put(key, 0);
                else if (p >= pPut && p < pGet)
                    cache.get(key);
                else
                    cache.remove(key);
                cnt.increment();
            }
            return null;
        }
    }, THREADS);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongAdder(java.util.concurrent.atomic.LongAdder)

Example 33 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project ignite by apache.

the class IgniteDataStreamerPerformanceTest method doTest.

/**
 * @throws Exception If failed.
 */
private void doTest() throws Exception {
    System.gc();
    System.gc();
    System.gc();
    try {
        useCache = true;
        startGridsMultiThreaded(GRID_CNT);
        useCache = false;
        Ignite ignite = startGrid();
        final IgniteDataStreamer<Integer, String> ldr = ignite.dataStreamer(DEFAULT_CACHE_NAME);
        ldr.perNodeBufferSize(8192);
        ldr.receiver(DataStreamerCacheUpdaters.<Integer, String>batchedSorted());
        ldr.autoFlushFrequency(0);
        final LongAdder cnt = new LongAdder();
        long start = U.currentTimeMillis();
        Thread t = new Thread(new Runnable() {

            @SuppressWarnings("BusyWait")
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException ignored) {
                        break;
                    }
                    info(">>> Adds/sec: " + cnt.sumThenReset() / 10);
                }
            }
        });
        t.setDaemon(true);
        t.start();
        // Runtime.getRuntime().availableProcessors();
        int threadNum = 2;
        multithreaded(new Callable<Object>() {

            @SuppressWarnings("InfiniteLoopStatement")
            @Override
            public Object call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                while (true) {
                    int i = rnd.nextInt(ENTRY_CNT);
                    ldr.addData(i, vals[rnd.nextInt(vals.length)]);
                    cnt.increment();
                }
            }
        }, threadNum, "loader");
        info("Closing loader...");
        ldr.close(false);
        long duration = U.currentTimeMillis() - start;
        info("Finished performance test. Duration: " + duration + "ms.");
    } finally {
        stopAllGrids();
    }
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Example 34 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project ignite by apache.

the class TxRollbackOnTimeoutTest method testRandomMixedTxConfigurations.

/**
 * Test timeouts with random values and different tx configurations.
 */
public void testRandomMixedTxConfigurations() throws Exception {
    final Ignite client = startClient();
    final AtomicBoolean stop = new AtomicBoolean();
    final long seed = System.currentTimeMillis();
    final Random r = new Random(seed);
    log.info("Using seed: " + seed);
    final int threadsCnt = Runtime.getRuntime().availableProcessors() * 2;
    for (int k = 0; k < threadsCnt; k++) grid(0).cache(CACHE_NAME).put(k, (long) 0);
    final TransactionConcurrency[] TC_VALS = TransactionConcurrency.values();
    final TransactionIsolation[] TI_VALS = TransactionIsolation.values();
    final LongAdder cntr0 = new LongAdder();
    final LongAdder cntr1 = new LongAdder();
    final LongAdder cntr2 = new LongAdder();
    final LongAdder cntr3 = new LongAdder();
    final IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {

        @Override
        public void run() {
            while (!stop.get()) {
                int nodeId = r.nextInt(GRID_CNT + 1);
                Ignite node = nodeId == GRID_CNT || nearCacheEnabled() ? client : grid(nodeId);
                TransactionConcurrency conc = TC_VALS[r.nextInt(TC_VALS.length)];
                TransactionIsolation isolation = TI_VALS[r.nextInt(TI_VALS.length)];
                int k = r.nextInt(threadsCnt);
                long timeout = r.nextInt(200) + 50;
                // Roughly 50% of transactions should time out.
                try (Transaction tx = node.transactions().txStart(conc, isolation, timeout, 1)) {
                    cntr0.add(1);
                    final Long v = (Long) node.cache(CACHE_NAME).get(k);
                    final int delay = r.nextInt(400);
                    if (delay > 0)
                        sleep(delay);
                    node.cache(CACHE_NAME).put(k, v + 1);
                    tx.commit();
                    cntr1.add(1);
                } catch (TransactionOptimisticException | InterruptedException e) {
                    // Expected.
                    cntr3.add(1);
                } catch (TransactionTimeoutException e) {
                    cntr2.add(1);
                } catch (CacheException e) {
                    assertEquals(TransactionTimeoutException.class, X.getCause(e).getClass());
                    cntr2.add(1);
                }
            }
        }
    }, threadsCnt, "tx-async-thread");
    sleep(DURATION);
    stop.set(true);
    fut.get(10_000);
    log.info("Tx test stats: started=" + cntr0.sum() + ", completed=" + cntr1.sum() + ", failed=" + cntr3.sum() + ", timedOut=" + cntr2.sum());
    assertEquals("Expected finished count same as started count", cntr0.sum(), cntr1.sum() + cntr2.sum() + cntr3.sum());
}
Also used : CacheException(javax.cache.CacheException) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) LongAdder(java.util.concurrent.atomic.LongAdder) Transaction(org.apache.ignite.transactions.Transaction) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) Ignite(org.apache.ignite.Ignite)

Example 35 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project ignite by apache.

the class GridCacheBinaryObjectsAbstractMultiThreadedSelfTest method testGetPut.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("BusyWait")
public void testGetPut() throws Exception {
    final AtomicBoolean flag = new AtomicBoolean();
    final LongAdder cnt = new LongAdder();
    IgniteInternalFuture<?> f = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            int threadId = idxGen.getAndIncrement() % 2;
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            while (!flag.get()) {
                IgniteCache<Object, Object> c = jcache(rnd.nextInt(gridCount()));
                switch(threadId) {
                    case 0:
                        // Put/get/remove binary -> binary.
                        c.put(new TestObject(rnd.nextInt(10000)), new TestObject(rnd.nextInt(10000)));
                        IgniteCache<Object, Object> p2 = ((IgniteCacheProxy<Object, Object>) c).keepBinary();
                        BinaryObject v = (BinaryObject) p2.get(new TestObject(rnd.nextInt(10000)));
                        if (v != null)
                            v.deserialize();
                        c.remove(new TestObject(rnd.nextInt(10000)));
                        break;
                    case 1:
                        // Put/get int -> binary.
                        c.put(rnd.nextInt(10000), new TestObject(rnd.nextInt(10000)));
                        IgniteCache<Integer, BinaryObject> p4 = ((IgniteCacheProxy<Object, Object>) c).keepBinary();
                        BinaryObject v1 = p4.get(rnd.nextInt(10000));
                        if (v1 != null)
                            v1.deserialize();
                        p4.remove(rnd.nextInt(10000));
                        break;
                    default:
                        assert false;
                }
                cnt.add(3);
            }
            return null;
        }
    }, THREAD_CNT);
    for (int i = 0; i < 30 && !f.isDone(); i++) Thread.sleep(1000);
    flag.set(true);
    f.get();
    info("Operations in 30 sec: " + cnt.sum());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongAdder(java.util.concurrent.atomic.LongAdder) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteCache(org.apache.ignite.IgniteCache) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Aggregations

LongAdder (java.util.concurrent.atomic.LongAdder)92 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 Random (java.util.Random)13 ExecutorService (java.util.concurrent.ExecutorService)10 SplittableRandom (java.util.SplittableRandom)9 HashMap (java.util.HashMap)7 Map (java.util.Map)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 Theory (org.junit.experimental.theories.Theory)6 UUID (java.util.UUID)5 ArrayList (java.util.ArrayList)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Ignite (org.apache.ignite.Ignite)4 IgniteUuid (org.apache.ignite.lang.IgniteUuid)4 Metric (org.springframework.boot.actuate.metrics.Metric)4 Date (java.util.Date)3 HashSet (java.util.HashSet)3 Callable (java.util.concurrent.Callable)3 CountDownLatch (java.util.concurrent.CountDownLatch)3