use of org.apache.ignite.internal.util.GridRandom in project ignite by apache.
the class HadoopConcurrentHashMultimapSelftest method testMultiThreaded.
/**
* @throws Exception if failed.
*/
public void testMultiThreaded() throws Exception {
GridUnsafeMemory mem = new GridUnsafeMemory(0);
X.println("___ Started");
Random rnd = new GridRandom();
for (int i = 0; i < 20; i++) {
HadoopJobInfo job = new JobInfo();
final HadoopTaskContext taskCtx = new TaskContext();
final HadoopConcurrentHashMultimap m = new HadoopConcurrentHashMultimap(job, mem, 16);
final ConcurrentMap<Integer, Collection<Integer>> mm = new ConcurrentHashMap<>();
X.println("___ MT");
multithreaded(new Callable<Object>() {
@Override
public Object call() throws Exception {
X.println("___ TH in");
Random rnd = new GridRandom();
IntWritable key = new IntWritable();
IntWritable val = new IntWritable();
HadoopMultimap.Adder a = m.startAdding(taskCtx);
for (int i = 0; i < 50000; i++) {
int k = rnd.nextInt(32000);
int v = rnd.nextInt();
key.set(k);
val.set(v);
a.write(key, val);
Collection<Integer> list = mm.get(k);
if (list == null) {
list = new ConcurrentLinkedQueue<>();
Collection<Integer> old = mm.putIfAbsent(k, list);
if (old != null)
list = old;
}
list.add(v);
}
a.close();
X.println("___ TH out");
return null;
}
}, 3 + rnd.nextInt(27));
X.println("___ Check: " + m.capacity());
assertEquals(mm.size(), m.keys());
assertTrue(m.capacity() > 32000);
HadoopTaskInput in = m.input(taskCtx);
while (in.next()) {
IntWritable key = (IntWritable) in.key();
Iterator<?> valsIter = in.values();
Collection<Integer> vals = mm.remove(key.get());
assertNotNull(vals);
while (valsIter.hasNext()) {
IntWritable val = (IntWritable) valsIter.next();
assertTrue(vals.remove(val.get()));
}
assertTrue(vals.isEmpty());
}
in.close();
m.close();
assertEquals(0, mem.allocatedSize());
}
}
use of org.apache.ignite.internal.util.GridRandom in project ignite by apache.
the class IgniteCacheDistributedJoinTest method beforeTestsStarted.
/** {@inheritDoc} */
@Override
protected void beforeTestsStarted() throws Exception {
startGridsMultiThreaded(4);
awaitPartitionMapExchange();
conn = DriverManager.getConnection("jdbc:h2:mem:");
Statement s = conn.createStatement();
s.execute("create schema a");
s.execute("create schema b");
s.execute("create schema c");
s.execute("create table a.a(a bigint, b bigint, c bigint)");
s.execute("create table b.b(a bigint, b bigint, c bigint)");
s.execute("create table c.c(a bigint, b bigint, c bigint)");
s.execute("create index on a.a(a)");
s.execute("create index on a.a(b)");
s.execute("create index on a.a(c)");
s.execute("create index on b.b(a)");
s.execute("create index on b.b(b)");
s.execute("create index on b.b(c)");
s.execute("create index on c.c(a)");
s.execute("create index on c.c(b)");
s.execute("create index on c.c(c)");
GridRandom rnd = new GridRandom();
Ignite ignite = ignite(0);
IgniteCache<Integer, A> a = ignite.cache("a");
IgniteCache<Integer, B> b = ignite.cache("b");
IgniteCache<Integer, C> c = ignite.cache("c");
for (int i = 0; i < 100; i++) {
a.put(i, insert(s, new A(rnd.nextInt(50), rnd.nextInt(100), rnd.nextInt(150))));
b.put(i, insert(s, new B(rnd.nextInt(100), rnd.nextInt(50), rnd.nextInt(150))));
c.put(i, insert(s, new C(rnd.nextInt(150), rnd.nextInt(100), rnd.nextInt(50))));
}
checkSameResult(s, a, "select a, count(*) from a group by a order by a");
checkSameResult(s, a, "select b, count(*) from a group by b order by b");
checkSameResult(s, a, "select c, count(*) from a group by c order by c");
checkSameResult(s, b, "select a, count(*) from b group by a order by a");
checkSameResult(s, b, "select b, count(*) from b group by b order by b");
checkSameResult(s, b, "select c, count(*) from b group by c order by c");
checkSameResult(s, c, "select a, count(*) from c group by a order by a");
checkSameResult(s, c, "select b, count(*) from c group by b order by b");
checkSameResult(s, c, "select c, count(*) from c group by c order by c");
s.close();
}
use of org.apache.ignite.internal.util.GridRandom in project ignite by apache.
the class IgniteCacheCollocatedQuerySelfTest method testColocatedQueryRight.
/**
* Correct affinity.
*/
public void testColocatedQueryRight() {
IgniteCache<AffinityUuid, Purchase> c = ignite(0).cache(DEFAULT_CACHE_NAME);
Random rnd = new GridRandom(SEED);
for (int i = 0; i < PURCHASES; i++) {
Purchase p = new Purchase();
p.productId = rnd.nextInt(PRODUCTS);
p.price = rnd.nextInt(MAX_PRICE);
// Correct affinity.
c.put(new AffinityUuid(p.productId), p);
}
List<List<?>> res1 = query(c, false);
List<List<?>> res2 = query(c, true);
X.println("res1: " + res1);
X.println("res2: " + res2);
assertFalse(res1.isEmpty());
// TODO fix type conversion issue
assertEquals(res1.toString(), res2.toString());
}
use of org.apache.ignite.internal.util.GridRandom in project ignite by apache.
the class GridCircularQueueTest method testQueue.
/**
*
*/
public void testQueue() {
GridCacheQueryManager.CircularQueue<Integer> q = new GridCacheQueryManager.CircularQueue<>(4);
ArrayDeque<Integer> d = new ArrayDeque<>();
for (int i = 0; i < 10; i++) {
q.add(i);
d.add(i);
}
check(q, d);
q.remove(4);
remove(d, 4);
check(q, d);
for (int i = 100; i < 110; i++) {
q.add(i);
d.add(i);
}
check(q, d);
int size = q.size();
q.remove(size);
remove(d, size);
check(q, d);
assertEquals(0, q.size());
GridRandom rnd = new GridRandom();
for (int i = 0; i < 15000; i++) {
switch(rnd.nextInt(2)) {
case 1:
if (q.size() > 0) {
int cnt = 1;
if (q.size() > 1)
cnt += rnd.nextInt(q.size() - 1);
q.remove(cnt);
remove(d, cnt);
break;
}
case 0:
int cnt = rnd.nextInt(50);
for (int j = 0; j < cnt; j++) {
int x = rnd.nextInt();
q.add(x);
d.add(x);
}
break;
}
check(q, d);
}
}
use of org.apache.ignite.internal.util.GridRandom in project ignite by apache.
the class IgniteCacheOffheapEvictQueryTest method testEvictAndRemove.
/**
* @throws Exception If failed.
*/
public void testEvictAndRemove() throws Exception {
final int KEYS_CNT = 3000;
final int THREADS_CNT = 250;
final IgniteCache<Integer, Integer> c = startGrid().cache(DEFAULT_CACHE_NAME);
for (int i = 0; i < KEYS_CNT; i++) {
c.put(i, i);
if ((i & 1) == 0)
c.localEvict(F.asList(i));
}
X.println("___ Cache loaded...");
final CyclicBarrier b = new CyclicBarrier(THREADS_CNT, new Runnable() {
@Override
public void run() {
X.println("___ go!");
}
});
final AtomicInteger keys = new AtomicInteger(KEYS_CNT);
IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {
@Override
public void run() {
Random rnd = new GridRandom();
try {
b.await();
} catch (InterruptedException e) {
throw new IgniteInterruptedException(e);
} catch (BrokenBarrierException e) {
throw new IllegalStateException(e);
}
while (keys.get() > 0) {
int k = rnd.nextInt(KEYS_CNT);
try {
switch(rnd.nextInt(4)) {
case 0:
c.localEvict(F.asList(k));
break;
case 1:
c.get(k);
break;
case 2:
if (c.remove(k))
keys.decrementAndGet();
break;
case 3:
c.query(new SqlFieldsQuery("select _val from Integer where _key between ? and ?").setArgs(k, k + 20).setLocal(true)).getAll();
break;
}
} catch (CacheException e) {
String msgStart = "Failed to get value for key:";
for (Throwable th = e; th != null; th = th.getCause()) {
String msg = th.getMessage();
if (msg != null && msg.startsWith(msgStart)) {
int dot = msg.indexOf('.', msgStart.length());
assertTrue(dot != -1);
final Integer failedKey = Integer.parseInt(msg.substring(msgStart.length(), dot).trim());
X.println("___ failed key: " + failedKey);
break;
}
}
LT.warn(log, e.getMessage());
return;
}
}
}
}, THREADS_CNT);
try {
fut.get(60_000);
if (c.size(CachePeekMode.ALL) != 0)
fail("Not all keys removed.");
X.println("___ all keys removed");
} catch (IgniteFutureTimeoutCheckedException ignored) {
X.println("___ timeout");
X.println("___ keys: " + keys.get());
keys.set(0);
fut.get();
}
}
Aggregations