Search in sources :

Example 66 with ThreadLocalRandom

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

the class JdbcSqlInsertDeleteBenchmark method test.

/**
 * Benchmarked action that inserts and immediately deletes single row.
 *
 * {@inheritDoc}
 */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    long insertKey = rnd.nextLong(args.range()) + 1 + args.range();
    long insertVal = insertKey + 1;
    PreparedStatement insert = singleInsert.get();
    insert.setLong(1, insertKey);
    insert.setLong(2, insertVal);
    PreparedStatement delete = singleDelete.get();
    delete.setLong(1, insertKey);
    try {
        insert.executeUpdate();
        delete.executeUpdate();
    } catch (Exception ignored) {
    // collision occurred, ignoring
    }
    return true;
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) PreparedStatement(java.sql.PreparedStatement)

Example 67 with ThreadLocalRandom

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

the class IgniteBinaryObjectQueryArgumentsTest method testBigDecimalArgument.

/**
 * @throws Exception If failed.
 */
public void testBigDecimalArgument() throws Exception {
    final ThreadLocalRandom rnd = ThreadLocalRandom.current();
    final BigDecimal bd1 = new BigDecimal(rnd.nextDouble());
    BigDecimal bd2 = new BigDecimal(rnd.nextDouble());
    while (bd1.equals(bd2)) bd2 = new BigDecimal(rnd.nextDouble());
    testKeyValQuery(BIG_DECIMAL_CACHE, bd1, bd2);
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BigDecimal(java.math.BigDecimal)

Example 68 with ThreadLocalRandom

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

the class H2IndexingAbstractGeoSelfTest method checkGeoMultithreaded.

/**
 * Check geo indexing multithreaded with dynamic index creation.
 *
 * @param dynamic Whether index should be created dynamically.
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
private void checkGeoMultithreaded(boolean dynamic) throws Exception {
    final IgniteCache<Integer, EnemyCamp> cache1 = createCache("camp", true, Integer.class, EnemyCamp.class, dynamic);
    final IgniteCache<Integer, EnemyCamp> cache2 = grid(1).cache("camp");
    final IgniteCache<Integer, EnemyCamp> cache3 = grid(2).cache("camp");
    try {
        final String[] points = new String[CNT];
        WKTReader r = new WKTReader();
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
        for (int idx = 0; idx < CNT; idx++) {
            int x = rnd.nextInt(1, 100);
            int y = rnd.nextInt(1, 100);
            cache1.getAndPut(idx, new EnemyCamp(r.read("POINT(" + x + " " + y + ")"), Integer.toString(idx)));
            points[idx] = Integer.toString(idx);
        }
        Thread.sleep(200);
        final AtomicBoolean stop = new AtomicBoolean();
        final AtomicReference<Exception> err = new AtomicReference<>();
        IgniteInternalFuture<?> putFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                WKTReader r = new WKTReader();
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                while (!stop.get()) {
                    int cacheIdx = rnd.nextInt(0, 3);
                    IgniteCache<Integer, EnemyCamp> cache = cacheIdx == 0 ? cache1 : cacheIdx == 1 ? cache2 : cache3;
                    int idx = rnd.nextInt(CNT);
                    int x = rnd.nextInt(1, 100);
                    int y = rnd.nextInt(1, 100);
                    cache.getAndPut(idx, new EnemyCamp(r.read("POINT(" + x + " " + y + ")"), Integer.toString(idx)));
                    U.sleep(50);
                }
                return null;
            }
        }, Runtime.getRuntime().availableProcessors(), "put-thread");
        IgniteInternalFuture<?> qryFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                WKTReader r = new WKTReader();
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                while (!stop.get()) {
                    try {
                        int cacheIdx = rnd.nextInt(0, 3);
                        IgniteCache<Integer, EnemyCamp> cache = cacheIdx == 0 ? cache1 : cacheIdx == 1 ? cache2 : cache3;
                        SqlQuery<Integer, EnemyCamp> qry = new SqlQuery<>(EnemyCamp.class, "coords && ?");
                        Collection<Cache.Entry<Integer, EnemyCamp>> res = cache.query(qry.setArgs(r.read("POLYGON((0 0, 0 100, 100 100, 100 0, 0 0))"))).getAll();
                        checkPoints(res, points);
                        U.sleep(5);
                    } catch (Exception e) {
                        err.set(e);
                        stop.set(true);
                        break;
                    }
                }
                return null;
            }
        }, 4, "qry-thread");
        U.sleep(6000L);
        stop.set(true);
        putFut.get();
        qryFut.get();
        Exception err0 = err.get();
        if (err0 != null)
            throw err0;
    } finally {
        destroy(cache1, grid(0), dynamic);
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) IgniteCache(org.apache.ignite.IgniteCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) WKTReader(com.vividsolutions.jts.io.WKTReader) ParseException(com.vividsolutions.jts.io.ParseException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Collection(java.util.Collection) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 69 with ThreadLocalRandom

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

the class H2IndexingAbstractGeoSelfTest method checkSegmentedGeoIndexJoin.

/**
 * Check segmented geo-index join.
 *
 * @param partitioned Partitioned flag.
 * @param dynamic Whether index should be created dynamically.
 * @throws Exception If failed.
 */
private void checkSegmentedGeoIndexJoin(boolean partitioned, boolean dynamic) throws Exception {
    IgniteCache<Integer, Enemy> c1 = createCache("enemy", true, Integer.class, Enemy.class);
    IgniteCache<Integer, EnemyCamp> c2 = createCache("camp", partitioned, Integer.class, EnemyCamp.class, dynamic);
    try {
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
        WKTReader r = new WKTReader();
        for (int i = 0; i < ENEMYCAMP_SAMPLES_COUNT; i++) {
            final String point = String.format("POINT(%d %d)", rnd.nextInt(100), rnd.nextInt(100));
            c2.put(i, new EnemyCamp(r.read(point), "camp-" + i));
        }
        for (int i = 0; i < ENEMY_SAMPLES_COUNT; i++) {
            int campID = 30 + rnd.nextInt(ENEMYCAMP_SAMPLES_COUNT + 10);
            c1.put(i, new Enemy(campID, "enemy-" + i));
        }
        checkDistributedQuery();
        checkLocalQuery();
    } finally {
        destroy(c1, grid(0), dynamic);
        destroy(c2, grid(0), dynamic);
    }
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) WKTReader(com.vividsolutions.jts.io.WKTReader)

Example 70 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project quasar by puniverse.

the class TransferChannel method awaitMatch.

/**
 * Spins/yields/blocks until node s is matched or caller gives up.
 *
 * @param s     the waiting node
 * @param pred  the predecessor of s, or s itself if it has no
 *              predecessor, or null if unknown (the null case does not occur
 *              in any current calls but may in possible future extensions)
 * @param e     the comparison value for checking match
 * @param timed if true, wait only until timeout elapses
 * @param nanos timeout in nanosecs, used only if timed is true
 * @return matched item, or e if unmatched on interrupt or timeout
 */
private Message awaitMatch(Node s, Node pred, Message e, boolean timed, long nanos) throws SuspendExecution {
    long lastTime = timed ? System.nanoTime() : 0L;
    Strand w = Strand.currentStrand();
    // no spins in fiber; otherwise, initialized after first item and cancel checks
    int spins = (w.isFiber() ? 0 : -1);
    // bound if needed
    ThreadLocalRandom randomYields = null;
    if (spins == 0)
        requestUnpark(s, w);
    for (; ; ) {
        Object item = s.item;
        if (item == CHANNEL_CLOSED)
            setReceiveClosed();
        if (item != e) {
            // matched
            // assert item != s;
            // avoid garbage
            s.forgetContents();
            return this.<Message>cast(item);
        }
        if ((w.isInterrupted() || (timed && nanos <= 0)) && s.casItem(e, s)) {
            // cancel
            unsplice(pred, s);
            return e;
        }
        if (spins < 0) {
            // establish spins at/near front
            if ((spins = spinsFor(pred, s.isData)) > 0)
                randomYields = ThreadLocalRandom.current();
        } else if (spins > 0) {
            // spin
            --spins;
            if (randomYields.nextInt(CHAINED_SPINS) == 0)
                // occasionally yield
                Strand.yield();
        } else if (s.waiter == null) {
            // request unpark then recheck
            requestUnpark(s, w);
        } else if (timed) {
            long now = System.nanoTime();
            if ((nanos -= now - lastTime) > 0)
                Strand.parkNanos(this, nanos);
            lastTime = now;
        } else {
            Strand.park(this);
        }
    }
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Strand(co.paralleluniverse.strands.Strand)

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