Search in sources :

Example 21 with GridRandom

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

the class IgniteCacheQueryNodeRestartSelfTest2 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<AffinityKey<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(new AffinityKey<>(i, persId), new Purchase(persId, prodId));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey)

Example 22 with GridRandom

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

the class GridRandomSelfTest method testRandom.

/**
     */
public void testRandom() {
    for (int i = 0; i < 100; i++) {
        long seed = ThreadLocalRandom.current().nextLong();
        Random rnd1 = new Random(seed);
        Random rnd2 = new GridRandom(seed);
        for (int j = 1; j < 100000; j++) {
            assertEquals(rnd1.nextInt(), rnd2.nextInt());
            assertEquals(rnd1.nextInt(j), rnd2.nextInt(j));
            assertEquals(rnd1.nextLong(), rnd2.nextLong());
            assertEquals(rnd1.nextBoolean(), rnd2.nextBoolean());
            if (j % 1000 == 0) {
                seed = ThreadLocalRandom.current().nextLong();
                rnd1.setSeed(seed);
                rnd2.setSeed(seed);
            }
        }
    }
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) GridRandom(org.apache.ignite.internal.util.GridRandom) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Random(java.util.Random)

Example 23 with GridRandom

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

the class HadoopSkipListSelfTest 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 HadoopMultimap m = new HadoopSkipList(job, mem);
        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));
        HadoopTaskInput in = m.input(taskCtx);
        int prevKey = Integer.MIN_VALUE;
        while (in.next()) {
            IntWritable key = (IntWritable) in.key();
            assertTrue(key.get() > prevKey);
            prevKey = key.get();
            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) HadoopMultimap(org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopMultimap) 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) HadoopSkipList(org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopSkipList) HadoopTaskContext(org.apache.ignite.internal.processors.hadoop.HadoopTaskContext) Collection(java.util.Collection) 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 24 with GridRandom

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

the class GridCacheQuerySimpleBenchmark method testPerformance.

/**
     * @throws Exception If failed.
     */
public void testPerformance() throws Exception {
    Random rnd = new GridRandom();
    final IgniteCache<Long, Person> c = ignite.cache("offheap-cache");
    X.println("___ PUT start");
    final int cnt = 100_000;
    final int maxSalary = cnt / 10;
    for (long i = 0; i < cnt; i++) c.put(i, new Person(rnd.nextInt(maxSalary), "Vasya " + i));
    X.println("___ PUT end");
    final AtomicBoolean end = new AtomicBoolean();
    final LongAdder8 puts = new LongAdder8();
    IgniteInternalFuture<?> fut0 = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Random rnd = new GridRandom();
            while (!end.get()) {
                long i = rnd.nextInt(cnt);
                c.put(i, new Person(rnd.nextInt(maxSalary), "Vasya " + i));
                puts.increment();
            }
            return null;
        }
    }, 10);
    final LongAdder8 qrys = new LongAdder8();
    IgniteInternalFuture<?> fut1 = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Random rnd = new GridRandom();
            while (!end.get()) {
                int salary = rnd.nextInt(maxSalary);
                c.query(new SqlFieldsQuery("select name from Person where salary = ?").setArgs(salary)).getAll();
                qrys.increment();
            }
            return null;
        }
    }, 10);
    int runTimeSec = 600;
    for (int s = 0; s < runTimeSec; s++) {
        Thread.sleep(1000);
        long puts0 = puts.sum();
        long qrys0 = qrys.sum();
        puts.add(-puts0);
        qrys.add(-qrys0);
        X.println("___ puts: " + puts0 + " qrys: " + qrys0);
    }
    end.set(true);
    fut0.get();
    fut1.get();
    X.println("___ STOP");
}
Also used : IOException(java.io.IOException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) LongAdder8(org.jsr166.LongAdder8)

Example 25 with GridRandom

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

the class IgniteCacheCollocatedQuerySelfTest method testColocatedQueryWrong.

/**
     * Correct affinity.
     */
public void testColocatedQueryWrong() {
    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);
        // Random affinity.
        c.put(new AffinityUuid(rnd.nextInt(PRODUCTS)), p);
    }
    List<List<?>> res1 = query(c, false);
    List<List<?>> res2 = query(c, true);
    X.println("res1: " + res1);
    X.println("res2: " + res2);
    assertFalse(res1.isEmpty());
    assertFalse(res1.equals(res2));
}
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)

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