Search in sources :

Example 16 with GridRandom

use of org.apache.ignite.internal.util.GridRandom in project ignite by apache.

the class GridUnsafeMemorySelfTest method testGuardedOps.

/**
     * @throws Exception if failed.
     */
public void testGuardedOps() throws Exception {
    final int lineSize = 16;
    final int ptrsCnt = 4;
    final AtomicReferenceArray<CmpMem> ptrs = new AtomicReferenceArray<>(ptrsCnt * lineSize);
    final AtomicBoolean finished = new AtomicBoolean();
    final LongAdder8 cntr = new LongAdder8();
    final GridUnsafeGuard guard = new GridUnsafeGuard();
    GridRandom rnd = new GridRandom();
    for (int a = 0; a < 7; a++) {
        finished.set(false);
        int threads = 2 + rnd.nextInt(37);
        int time = rnd.nextInt(5);
        X.println("__ starting threads: " + threads + " time: " + time + " sec");
        final LongAdder8 locAdder = new LongAdder8();
        IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                Random rnd = new GridRandom();
                while (!finished.get()) {
                    int idx = rnd.nextInt(ptrsCnt) * lineSize;
                    guard.begin();
                    try {
                        final CmpMem old;
                        CmpMem ptr = null;
                        switch(rnd.nextInt(6)) {
                            case 0:
                                ptr = new CmpMem(cntr);
                            //noinspection fallthrough
                            case 1:
                                old = ptrs.getAndSet(idx, ptr);
                                if (old != null) {
                                    guard.finalizeLater(new Runnable() {

                                        @Override
                                        public void run() {
                                            old.deallocate();
                                        }
                                    });
                                }
                                break;
                            case 2:
                                if (rnd.nextBoolean())
                                    ptr = new CmpMem(cntr);
                                old = ptrs.getAndSet(idx, ptr);
                                if (old != null)
                                    guard.releaseLater(old);
                                break;
                            default:
                                old = ptrs.get(idx);
                                if (old != null)
                                    old.touch();
                        }
                    } finally {
                        guard.end();
                        locAdder.increment();
                    }
                }
                return null;
            }
        }, threads);
        Thread.sleep(1000 * time);
        X.println("__ stopping ops...");
        finished.set(true);
        fut.get();
        X.println("__ stopped, performed ops: " + locAdder.sum());
        for (int i = 0; i < ptrs.length(); i++) {
            CmpMem ptr = ptrs.getAndSet(i, null);
            if (ptr != null) {
                ptr.touch();
                ptr.deallocate();
            }
        }
        X.println("__ " + guard);
        assertEquals(0, cntr.sum());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) LongAdder8(org.jsr166.LongAdder8)

Example 17 with GridRandom

use of org.apache.ignite.internal.util.GridRandom in project ignite by apache.

the class IgniteSqlSplitterSelfTest method testHaving.

/**
     * Test HAVING clause.
     */
public void testHaving() {
    IgniteCache<Integer, Integer> c = ignite(0).getOrCreateCache(cacheConfig("having", true, Integer.class, Integer.class));
    try {
        Random rnd = new GridRandom();
        Map<Integer, AtomicLong> cntMap = new HashMap<>();
        for (int i = 0; i < 1000; i++) {
            int v = (int) (50 * rnd.nextGaussian());
            c.put(i, v);
            AtomicLong cnt = cntMap.get(v);
            if (cnt == null)
                cntMap.put(v, cnt = new AtomicLong());
            cnt.incrementAndGet();
        }
        assertTrue(cntMap.size() > 10);
        String sqlQry = "select _val, count(*) cnt from Integer group by _val having cnt > ?";
        X.println("Plan: " + c.query(new SqlFieldsQuery("explain " + sqlQry).setArgs(0)).getAll());
        for (int i = -1; i <= 1001; i += 10) {
            List<List<?>> res = c.query(new SqlFieldsQuery(sqlQry).setArgs(i)).getAll();
            for (List<?> row : res) {
                int v = (Integer) row.get(0);
                long cnt = (Long) row.get(1);
                assertTrue(cnt + " > " + i, cnt > i);
                assertEquals(cntMap.get(v).longValue(), cnt);
            }
        }
    } finally {
        c.destroy();
    }
}
Also used : HashMap(java.util.HashMap) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AtomicLong(java.util.concurrent.atomic.AtomicLong) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) List(java.util.List)

Example 18 with GridRandom

use of org.apache.ignite.internal.util.GridRandom in project ignite by apache.

the class IgniteSqlSplitterSelfTest method testOffsetLimit.

/**
     * Tests offset and limit clauses for query.
     * @throws Exception If failed.
     */
public void testOffsetLimit() throws Exception {
    IgniteCache<Integer, Integer> c = ignite(0).getOrCreateCache(cacheConfig("ints", true, Integer.class, Integer.class));
    try {
        awaitPartitionMapExchange();
        List<Integer> res = new ArrayList<>();
        Random rnd = new GridRandom();
        for (int i = 0; i < 10; i++) {
            int val = rnd.nextInt(100);
            c.put(i, val);
            res.add(val);
        }
        Collections.sort(res);
        String qry = "select _val from Integer order by _val ";
        assertEqualsCollections(res, columnQuery(c, qry));
        assertEqualsCollections(res.subList(0, 0), columnQuery(c, qry + "limit ?", 0));
        assertEqualsCollections(res.subList(0, 3), columnQuery(c, qry + "limit ?", 3));
        assertEqualsCollections(res.subList(0, 9), columnQuery(c, qry + "limit ? offset ?", 9, 0));
        assertEqualsCollections(res.subList(3, 7), columnQuery(c, qry + "limit ? offset ?", 4, 3));
        assertEqualsCollections(res.subList(7, 9), columnQuery(c, qry + "limit ? offset ?", 2, 7));
        assertEqualsCollections(res.subList(8, 10), columnQuery(c, qry + "limit ? offset ?", 2, 8));
        assertEqualsCollections(res.subList(9, 10), columnQuery(c, qry + "limit ? offset ?", 1, 9));
        assertEqualsCollections(res.subList(10, 10), columnQuery(c, qry + "limit ? offset ?", 1, 10));
        assertEqualsCollections(res.subList(9, 10), columnQuery(c, qry + "limit ? offset abs(-(4 + ?))", 1, 5));
    } finally {
        c.destroy();
    }
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) ArrayList(java.util.ArrayList)

Example 19 with GridRandom

use of org.apache.ignite.internal.util.GridRandom in project ignite by apache.

the class IgniteSqlSplitterSelfTest method testSortedMergeIndex.

/**
     * @throws Exception If failed.
     */
public void testSortedMergeIndex() throws Exception {
    IgniteCache<Integer, Value> c = ignite(0).getOrCreateCache(cacheConfig("v", true, Integer.class, Value.class));
    try {
        GridTestUtils.setFieldValue(null, GridMergeIndex.class, "PREFETCH_SIZE", 8);
        Random rnd = new GridRandom();
        int cnt = 1000;
        for (int i = 0; i < cnt; i++) {
            c.put(i, new Value(rnd.nextInt(5) == 0 ? null : rnd.nextInt(100), rnd.nextInt(8) == 0 ? null : rnd.nextInt(2000)));
        }
        List<List<?>> plan = c.query(new SqlFieldsQuery("explain select snd from Value order by fst desc")).getAll();
        String rdcPlan = (String) plan.get(1).get(0);
        assertTrue(rdcPlan.contains("merge_sorted"));
        assertTrue(rdcPlan.contains("/* index sorted */"));
        plan = c.query(new SqlFieldsQuery("explain select snd from Value")).getAll();
        rdcPlan = (String) plan.get(1).get(0);
        assertTrue(rdcPlan.contains("merge_scan"));
        assertFalse(rdcPlan.contains("/* index sorted */"));
        for (int i = 0; i < 10; i++) {
            X.println(" --> " + i);
            List<List<?>> res = c.query(new SqlFieldsQuery("select fst from Value order by fst").setPageSize(5)).getAll();
            assertEquals(cnt, res.size());
            Integer p = null;
            for (List<?> row : res) {
                Integer x = (Integer) row.get(0);
                if (x != null) {
                    if (p != null)
                        assertTrue(x + " >= " + p, x >= p);
                    p = x;
                }
            }
        }
    } finally {
        GridTestUtils.setFieldValue(null, GridMergeIndex.class, "PREFETCH_SIZE", 1024);
        c.destroy();
    }
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) ArrayList(java.util.ArrayList) List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 20 with GridRandom

use of org.apache.ignite.internal.util.GridRandom in project ignite by apache.

the class IgniteCacheQueryAbstractDistributedJoinSelfTest method fillCaches.

/**
     *
     */
private void fillCaches() {
    IgniteCache<Integer, Company> co = grid(0).cache("co");
    for (int i = 0; i < COMPANY_CNT; i++) co.put(i, new Company(i));
    IgniteCache<Integer, Product> pr = grid(0).cache("pr");
    Random rnd = new GridRandom();
    for (int i = 0; i < PRODUCT_CNT; i++) pr.put(i, new Product(i, rnd.nextInt(COMPANY_CNT)));
    IgniteCache<Integer, Person> pe = grid(0).cache("pe");
    for (int i = 0; i < PERS_CNT; i++) pe.put(i, new Person(i));
    IgniteCache<Integer, Purchase> pu = grid(0).cache("pu");
    for (int i = 0; i < PURCHASE_CNT; i++) {
        int persId = rnd.nextInt(PERS_CNT);
        int prodId = rnd.nextInt(PRODUCT_CNT);
        pu.put(i, new Purchase(persId, prodId));
    }
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom)

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