Search in sources :

Example 71 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class IgniteCommunicationBalanceTest method testBalance2.

/**
 * @throws Exception If failed.
 */
public void testBalance2() throws Exception {
    System.setProperty(IgniteSystemProperties.IGNITE_IO_BALANCE_PERIOD, "1000");
    try {
        startGridsMultiThreaded(5);
        client = true;
        startGridsMultiThreaded(5, 5);
        for (int i = 0; i < 5; i++) {
            log.info("Iteration: " + i);
            final AtomicInteger idx = new AtomicInteger();
            GridTestUtils.runMultiThreaded(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    Ignite node = ignite(idx.incrementAndGet() % 10);
                    ThreadLocalRandom rnd = ThreadLocalRandom.current();
                    int msgs = rnd.nextInt(500, 600);
                    for (int i = 0; i < msgs; i++) {
                        int sndTo = rnd.nextInt(10);
                        ClusterNode sntToNode = node.cluster().node(ignite(sndTo).cluster().localNode().id());
                        IgniteCompute compute = node.compute(node.cluster().forNode(sntToNode));
                        compute.call(new DummyCallable(new byte[rnd.nextInt(rnd.nextInt(256, 1024))]));
                    }
                    return null;
                }
            }, 30, "test-thread");
            waitNioBalanceStop(G.allGrids(), 10_000);
        }
    } finally {
        System.setProperty(IgniteSystemProperties.IGNITE_IO_BALANCE_PERIOD, "");
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteCompute(org.apache.ignite.IgniteCompute)

Example 72 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class IgniteCommunicationBalanceTest method testBalance1.

/**
 * @throws Exception If failed.
 */
public void testBalance1() throws Exception {
    if (sslEnabled())
        return;
    System.setProperty(IgniteSystemProperties.IGNITE_IO_BALANCE_PERIOD, "5000");
    try {
        selectors = 4;
        final int SRVS = 6;
        startGridsMultiThreaded(SRVS);
        client = true;
        final Ignite client = startGrid(SRVS);
        for (int i = 0; i < SRVS; i++) {
            ClusterNode node = client.cluster().node(ignite(i).cluster().localNode().id());
            client.compute(client.cluster().forNode(node)).call(new DummyCallable(null));
        }
        waitNioBalanceStop(Collections.singletonList(client), 10_000);
        final GridNioServer srv = GridTestUtils.getFieldValue(client.configuration().getCommunicationSpi(), "nioSrvr");
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
        long readMoveCnt1 = srv.readerMoveCount();
        long writeMoveCnt1 = srv.writerMoveCount();
        int prevNodeIdx = -1;
        for (int iter = 0; iter < 10; iter++) {
            int nodeIdx = rnd.nextInt(SRVS);
            while (prevNodeIdx == nodeIdx) nodeIdx = rnd.nextInt(SRVS);
            prevNodeIdx = nodeIdx;
            log.info("Iteration [iter=" + iter + ", node=" + nodeIdx + ']');
            final long readMoveCnt = readMoveCnt1;
            final long writeMoveCnt = writeMoveCnt1;
            final int nodeIdx0 = nodeIdx;
            GridTestUtils.waitForCondition(new GridAbsPredicate() {

                @Override
                public boolean apply() {
                    byte[] data = new byte[100_000];
                    for (int j = 0; j < 10; j++) {
                        for (int i = 0; i < SRVS; i++) {
                            ClusterNode node = client.cluster().node(ignite(i).cluster().localNode().id());
                            IgniteCompute compute = client.compute(client.cluster().forNode(node));
                            compute.call(new DummyCallable(i == nodeIdx0 ? data : null));
                        }
                    }
                    if (usePairedConnections())
                        return srv.readerMoveCount() > readMoveCnt && srv.writerMoveCount() > writeMoveCnt;
                    else
                        return srv.readerMoveCount() > readMoveCnt || srv.writerMoveCount() > writeMoveCnt;
                }
            }, 30_000);
            waitNioBalanceStop(Collections.singletonList(client), 30_000);
            long readMoveCnt2 = srv.readerMoveCount();
            long writeMoveCnt2 = srv.writerMoveCount();
            log.info("Move counts [rc1=" + readMoveCnt1 + ", wc1=" + writeMoveCnt1 + ", rc2=" + readMoveCnt2 + ", wc2=" + writeMoveCnt2 + ']');
            if (usePairedConnections()) {
                assertTrue(readMoveCnt2 > readMoveCnt1);
                assertTrue(writeMoveCnt2 > writeMoveCnt1);
            } else
                assertTrue(readMoveCnt2 > readMoveCnt1 || writeMoveCnt2 > writeMoveCnt1);
            readMoveCnt1 = readMoveCnt2;
            writeMoveCnt1 = writeMoveCnt2;
        }
        waitNioBalanceStop(G.allGrids(), 10_000);
    } finally {
        System.setProperty(IgniteSystemProperties.IGNITE_IO_BALANCE_PERIOD, "");
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) GridNioServer(org.apache.ignite.internal.util.nio.GridNioServer) IgniteCompute(org.apache.ignite.IgniteCompute)

Example 73 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class IgniteVariousConnectionNumberTest method runOperations.

/**
 * @param time Execution time.
 * @throws Exception If failed.
 */
private void runOperations(final long time) throws Exception {
    final AtomicInteger idx = new AtomicInteger();
    GridTestUtils.runMultiThreaded(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Ignite node = ignite(idx.getAndIncrement() % NODES);
            IgniteCache cache = node.cache(DEFAULT_CACHE_NAME);
            long stopTime = U.currentTimeMillis() + time;
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            while (U.currentTimeMillis() < stopTime) {
                cache.put(rnd.nextInt(10_000), 0);
                node.compute().broadcast(new DummyJob());
            }
            return null;
        }
    }, NODES * 10, "test-thread");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteCache(org.apache.ignite.IgniteCache) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Example 74 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class CacheSerializableTransactionsTest method testNoOptimisticExceptionOnChangingTopology.

/**
 * @throws Exception If failed.
 */
public void testNoOptimisticExceptionOnChangingTopology() throws Exception {
    if (FAST)
        return;
    final AtomicBoolean finished = new AtomicBoolean();
    final List<String> cacheNames = new ArrayList<>();
    Ignite srv = ignite(1);
    try {
        {
            CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false);
            ccfg.setName("cache1");
            ccfg.setRebalanceMode(SYNC);
            srv.createCache(ccfg);
            cacheNames.add(ccfg.getName());
        }
        {
            // Store enabled.
            CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, true, false);
            ccfg.setName("cache2");
            ccfg.setRebalanceMode(SYNC);
            srv.createCache(ccfg);
            cacheNames.add(ccfg.getName());
        }
        {
            // Eviction.
            CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false);
            ccfg.setName("cache3");
            ccfg.setRebalanceMode(SYNC);
            LruEvictionPolicy plc = new LruEvictionPolicy();
            plc.setMaxSize(100);
            ccfg.setEvictionPolicy(plc);
            ccfg.setOnheapCacheEnabled(true);
            srv.createCache(ccfg);
            cacheNames.add(ccfg.getName());
        }
        IgniteInternalFuture<?> restartFut = restartFuture(finished, null);
        List<IgniteInternalFuture<?>> futs = new ArrayList<>();
        final int KEYS_PER_THREAD = 100;
        for (int i = 1; i < SRVS + CLIENTS; i++) {
            final Ignite node = ignite(i);
            final int minKey = i * KEYS_PER_THREAD;
            final int maxKey = minKey + KEYS_PER_THREAD;
            // Threads update non-intersecting keys, optimistic exception should not be thrown.
            futs.add(GridTestUtils.runAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    try {
                        log.info("Started update thread [node=" + node.name() + ", minKey=" + minKey + ", maxKey=" + maxKey + ']');
                        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
                        List<IgniteCache<Integer, Integer>> caches = new ArrayList<>();
                        for (String cacheName : cacheNames) caches.add(node.<Integer, Integer>cache(cacheName));
                        assertEquals(3, caches.size());
                        int iter = 0;
                        while (!finished.get()) {
                            int keyCnt = rnd.nextInt(1, 10);
                            final Set<Integer> keys = new LinkedHashSet<>();
                            while (keys.size() < keyCnt) keys.add(rnd.nextInt(minKey, maxKey));
                            for (final IgniteCache<Integer, Integer> cache : caches) {
                                doInTransaction(node, OPTIMISTIC, SERIALIZABLE, new Callable<Void>() {

                                    @Override
                                    public Void call() throws Exception {
                                        for (Integer key : keys) randomOperation(rnd, cache, key);
                                        return null;
                                    }
                                });
                            }
                            if (iter % 100 == 0)
                                log.info("Iteration: " + iter);
                            iter++;
                        }
                        return null;
                    } catch (Throwable e) {
                        log.error("Unexpected error: " + e, e);
                        throw e;
                    }
                }
            }, "update-thread-" + i));
        }
        U.sleep(60_000);
        finished.set(true);
        restartFut.get();
        for (IgniteInternalFuture<?> fut : futs) fut.get();
    } finally {
        finished.set(true);
        for (String cacheName : cacheNames) destroyCache(cacheName);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) IgniteCache(org.apache.ignite.IgniteCache) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Callable(java.util.concurrent.Callable) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteException(org.apache.ignite.IgniteException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheException(javax.cache.CacheException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) LruEvictionPolicy(org.apache.ignite.cache.eviction.lru.LruEvictionPolicy)

Example 75 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class CacheSerializableTransactionsTest method checkReadWriteTransactionsNoDeadlock.

/**
 * @param multiNode Multi-node test flag.
 * @throws Exception If failed.
 */
private void checkReadWriteTransactionsNoDeadlock(final boolean multiNode) throws Exception {
    final Ignite ignite0 = ignite(0);
    for (final CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        ignite0.createCache(ccfg);
        try {
            final long stopTime = U.currentTimeMillis() + 10_000;
            final AtomicInteger idx = new AtomicInteger();
            GridTestUtils.runMultiThreaded(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    Ignite ignite = multiNode ? ignite(idx.incrementAndGet() % (SRVS + CLIENTS)) : ignite0;
                    IgniteCache<Integer, Integer> cache = ignite.cache(ccfg.getName());
                    ThreadLocalRandom rnd = ThreadLocalRandom.current();
                    while (U.currentTimeMillis() < stopTime) {
                        try {
                            try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
                                for (int i = 0; i < 10; i++) {
                                    Integer key = rnd.nextInt(30);
                                    if (rnd.nextBoolean())
                                        cache.get(key);
                                    else
                                        cache.put(key, key);
                                }
                                tx.commit();
                            }
                        } catch (TransactionOptimisticException ignore) {
                        // No-op.
                        }
                    }
                    return null;
                }
            }, 32, "test-thread");
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteCache(org.apache.ignite.IgniteCache) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteException(org.apache.ignite.IgniteException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheException(javax.cache.CacheException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Aggregations

ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)236 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)73 Ignite (org.apache.ignite.Ignite)65 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)53 IgniteCache (org.apache.ignite.IgniteCache)44 ArrayList (java.util.ArrayList)34 Test (org.junit.Test)30 IgniteException (org.apache.ignite.IgniteException)27 Transaction (org.apache.ignite.transactions.Transaction)26 CacheException (javax.cache.CacheException)24 HashMap (java.util.HashMap)22 Map (java.util.Map)20 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)17 LongAdder (java.util.concurrent.atomic.LongAdder)15 TreeMap (java.util.TreeMap)14 IgniteTransactions (org.apache.ignite.IgniteTransactions)13 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)13 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)12 Callable (java.util.concurrent.Callable)11 CountDownLatch (java.util.concurrent.CountDownLatch)11