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