Search in sources :

Example 1 with GridRandom

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());
    }
}
Also used : HadoopJobInfo(org.apache.ignite.internal.processors.hadoop.HadoopJobInfo) HadoopTaskInput(org.apache.ignite.internal.processors.hadoop.HadoopTaskInput) HadoopTaskContext(org.apache.ignite.internal.processors.hadoop.HadoopTaskContext) IOException(java.io.IOException) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) HadoopJobInfo(org.apache.ignite.internal.processors.hadoop.HadoopJobInfo) HadoopTaskContext(org.apache.ignite.internal.processors.hadoop.HadoopTaskContext) Collection(java.util.Collection) HadoopConcurrentHashMultimap(org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopConcurrentHashMultimap) GridUnsafeMemory(org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) IntWritable(org.apache.hadoop.io.IntWritable)

Example 2 with GridRandom

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();
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) Statement(java.sql.Statement) Ignite(org.apache.ignite.Ignite)

Example 3 with GridRandom

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());
}
Also used : AffinityUuid(org.apache.ignite.cache.affinity.AffinityUuid) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) List(java.util.List)

Example 4 with GridRandom

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);
    }
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) ArrayDeque(java.util.ArrayDeque)

Example 5 with GridRandom

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();
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CacheException(javax.cache.CacheException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException)

Aggregations

GridRandom (org.apache.ignite.internal.util.GridRandom)30 Random (java.util.Random)24 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)10 List (java.util.List)9 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 IgniteEx (org.apache.ignite.internal.IgniteEx)6 ArrayList (java.util.ArrayList)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 HashMap (java.util.HashMap)4 CacheException (javax.cache.CacheException)4 IOException (java.io.IOException)3 LinkedHashMap (java.util.LinkedHashMap)3 AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)3 CAX (org.apache.ignite.internal.util.typedef.CAX)3 Collection (java.util.Collection)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2