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;
}
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);
}
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);
}
}
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);
}
}
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);
}
}
}
Aggregations