Search in sources :

Example 56 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project MantaroBot by Mantaro.

the class Utils method randomOrder.

public static Comparator<String> randomOrder() {
    ThreadLocalRandom r = ThreadLocalRandom.current();
    int x = r.nextInt(), y = r.nextInt();
    boolean b = r.nextBoolean();
    return Comparator.comparingInt((String s) -> s.hashCode() ^ x).thenComparingInt(s -> s.length() ^ y).thenComparing(b ? Comparator.naturalOrder() : Comparator.reverseOrder());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) MantaroInfo(net.kodehawa.mantarobot.MantaroInfo) java.util(java.util) SneakyThrows(lombok.SneakyThrows) URL(java.net.URL) RateLimiter(net.kodehawa.mantarobot.utils.commands.RateLimiter) NewRateLimiter(net.kodehawa.mantarobot.utils.commands.NewRateLimiter) MantaroBot(net.kodehawa.mantarobot.MantaroBot) RethinkDB.r(com.rethinkdb.RethinkDB.r) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) JSONObject(org.json.JSONObject) Matcher(java.util.regex.Matcher) CharStreams(com.google.common.io.CharStreams) okhttp3(okhttp3) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) Config(net.kodehawa.mantarobot.data.Config) Connection(com.rethinkdb.net.Connection) Resty(us.monoid.web.Resty) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) IOException(java.io.IOException) Field(java.lang.reflect.Field) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Slf4j(lombok.extern.slf4j.Slf4j) URLEncoder(java.net.URLEncoder) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) FinderUtil(com.jagrosh.jdautilities.utils.FinderUtil) Pattern(java.util.regex.Pattern) InputStream(java.io.InputStream) OptsCmd.optsCmd(net.kodehawa.mantarobot.commands.OptsCmd.optsCmd) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 57 with ThreadLocalRandom

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

the class CacheBinaryKeyConcurrentQueryTest method startUpdate.

/**
 * @param cacheName Cache name.
 * @return Future.
 */
private IgniteInternalFuture<?> startUpdate(final String cacheName) {
    final long stopTime = System.currentTimeMillis() + 30_000;
    final AtomicInteger idx = new AtomicInteger();
    return GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Void call() {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            IgniteCache cache = ignite(idx.getAndIncrement() % NODES).cache(cacheName).withKeepBinary();
            while (System.currentTimeMillis() < stopTime) {
                switch(rnd.nextInt(5)) {
                    case 0:
                        {
                            TestKey key = new TestKey(rnd.nextInt(KEYS));
                            CacheEntry e = cache.getEntry(key);
                            assertNotNull(e);
                            assertTrue(e.getKey() instanceof BinaryObject);
                            cache.put(e.getKey(), new TestValue(rnd.nextInt(KEYS)));
                            break;
                        }
                    case 1:
                        {
                            Iterator<Cache.Entry> it = cache.iterator();
                            for (int i = 0; i < 100 && it.hasNext(); i++) {
                                Cache.Entry e = it.next();
                                assertTrue(e.getKey() instanceof BinaryObject);
                                cache.put(e.getKey(), new TestValue(rnd.nextInt(KEYS)));
                            }
                            break;
                        }
                    case 2:
                        {
                            SqlFieldsQuery qry = new SqlFieldsQuery("select _key " + "from \"" + cache.getName() + "\".TestValue where id=?");
                            qry.setArgs(rnd.nextInt(KEYS));
                            List<List> res = cache.query(qry).getAll();
                            assertEquals(1, res.size());
                            BinaryObject key = (BinaryObject) res.get(0).get(0);
                            cache.put(key, new TestValue(rnd.nextInt(KEYS)));
                            break;
                        }
                    case 3:
                        {
                            SqlQuery qry = new SqlQuery("TestValue", "id=?");
                            qry.setArgs(rnd.nextInt(KEYS));
                            List<Cache.Entry> res = cache.query(qry).getAll();
                            assertEquals(1, res.size());
                            break;
                        }
                    case 4:
                        {
                            SqlQuery qry = new SqlQuery("TestValue", "order by id");
                            int cnt = 0;
                            for (Cache.Entry e : (Iterable<Cache.Entry>) cache.query(qry)) {
                                assertNotNull(cache.get(e.getKey()));
                                cnt++;
                            }
                            assertTrue(cnt > 0);
                            break;
                        }
                    default:
                        fail();
                }
            }
            return null;
        }
    }, NODES * 2, "test-thread");
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) IgniteCache(org.apache.ignite.IgniteCache) CacheEntry(org.apache.ignite.cache.CacheEntry) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) CacheEntry(org.apache.ignite.cache.CacheEntry) BinaryObject(org.apache.ignite.binary.BinaryObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BinaryObject(org.apache.ignite.binary.BinaryObject) List(java.util.List) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 58 with ThreadLocalRandom

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

the class IgfsSizeSelfTest method write.

/**
 * Perform write of the files.
 *
 * @return Collection of written file descriptors.
 * @throws Exception If failed.
 */
private Collection<IgfsFile> write() throws Exception {
    Collection<IgfsFile> res = new HashSet<>(FILES_CNT, 1.0f);
    ThreadLocalRandom rand = ThreadLocalRandom.current();
    for (int i = 0; i < FILES_CNT; i++) {
        // Create empty file locally.
        IgfsPath path = new IgfsPath("/file-" + i);
        igfs(0).create(path, false).close();
        IgfsMetaManager meta = igfs(0).context().meta();
        IgniteUuid fileId = meta.fileId(path);
        // Calculate file blocks.
        int fileSize = rand.nextInt(MAX_FILE_SIZE);
        int fullBlocks = fileSize / BLOCK_SIZE;
        int remainderSize = fileSize % BLOCK_SIZE;
        Collection<IgfsBlock> blocks = new ArrayList<>(fullBlocks + remainderSize > 0 ? 1 : 0);
        for (int j = 0; j < fullBlocks; j++) blocks.add(new IgfsBlock(new IgfsBlockKey(fileId, null, true, j), BLOCK_SIZE));
        if (remainderSize > 0)
            blocks.add(new IgfsBlock(new IgfsBlockKey(fileId, null, true, fullBlocks), remainderSize));
        IgfsFile file = new IgfsFile(path, fileSize, blocks);
        // Actual write.
        for (IgfsBlock block : blocks) {
            IgfsOutputStream os = igfs(0).append(path, false);
            os.write(chunk(block.length()));
            os.close();
        }
        // Add written file to the result set.
        res.add(file);
    }
    return res;
}
Also used : ArrayList(java.util.ArrayList) IgfsOutputStream(org.apache.ignite.igfs.IgfsOutputStream) IgfsPath(org.apache.ignite.igfs.IgfsPath) IgniteUuid(org.apache.ignite.lang.IgniteUuid) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) HashSet(java.util.HashSet)

Example 59 with ThreadLocalRandom

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

the class IgniteCacheStarvationOnRebalanceTest method testLoadSystemWithPutAndStartRebalancing.

/**
 * @throws Exception If failed.
 */
public void testLoadSystemWithPutAndStartRebalancing() throws Exception {
    final IgniteCache<Integer, CacheValue> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    final long endTime = System.currentTimeMillis() + TEST_TIMEOUT - 60_000;
    int iter = 0;
    while (System.currentTimeMillis() < endTime) {
        info("Iteration: " + iter++);
        final AtomicBoolean stop = new AtomicBoolean();
        IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                while (!stop.get() && System.currentTimeMillis() < endTime) {
                    int key = rnd.nextInt(100_000);
                    cache.put(key, new CacheValue(key));
                }
                return null;
            }
        }, IGNITE_THREAD_POOL_SIZE * 4, "put-thread");
        try {
            Thread.sleep(500);
            info("Initial set of keys is loaded.");
            info("Starting new node...");
            startGrid(GRID_CNT + 1);
            info("New node is started.");
            Thread.sleep(500);
        } finally {
            stop.set(true);
        }
        // Wait for put tasks. If put() is blocked the test is timed out.
        fut.get();
        stopGrid(GRID_CNT + 1);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 60 with ThreadLocalRandom

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

the class IgniteClientReconnectCacheQueriesFailoverTest method testReconnectScanQuery.

/**
 * @throws Exception If failed.
 */
public void testReconnectScanQuery() throws Exception {
    final Ignite client = grid(serverCount());
    final IgniteCache<Integer, Person> cache = client.cache(DEFAULT_CACHE_NAME);
    assertNotNull(cache);
    final Affinity<Integer> aff = client.affinity(DEFAULT_CACHE_NAME);
    final Map<Integer, Integer> partMap = new HashMap<>();
    for (int i = 0; i < aff.partitions(); i++) partMap.put(i, 0);
    for (int i = 0; i <= 10_000; i++) {
        Integer part = aff.partition(i);
        Integer size = partMap.get(part);
        partMap.put(part, size + 1);
    }
    reconnectFailover(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ScanQuery<Integer, Person> qry = new ScanQuery<>(new IgniteBiPredicate<Integer, Person>() {

                @Override
                public boolean apply(Integer key, Person val) {
                    return val.getId() % 2 == 1;
                }
            });
            assertEquals(5000, cache.query(qry).getAll().size());
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            Integer part = rnd.nextInt(0, aff.partitions());
            qry = new ScanQuery<>(part);
            assertEquals((int) partMap.get(part), cache.query(qry).getAll().size());
            return null;
        }
    });
}
Also used : HashMap(java.util.HashMap) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) CacheException(javax.cache.CacheException) 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