Search in sources :

Example 26 with GridRandom

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

the class IgniteCacheSqlQueryMultiThreadedSelfTest method testQueryPut.

/**
     * Test put and parallel query.
     * @throws Exception If failed.
     */
public void testQueryPut() throws Exception {
    final IgniteCache<Integer, Person> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    cache.clear();
    final AtomicBoolean stop = new AtomicBoolean();
    IgniteInternalFuture<?> fut1 = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Random rnd = new GridRandom();
            while (!stop.get()) {
                List<List<?>> res = cache.query(new SqlFieldsQuery("select avg(age) from Person where age > 0")).getAll();
                assertEquals(1, res.size());
                if (res.get(0).get(0) == null)
                    continue;
                int avgAge = ((Number) res.get(0).get(0)).intValue();
                if (rnd.nextInt(300) == 0)
                    X.println("__ " + avgAge);
            }
            return null;
        }
    }, 20);
    IgniteInternalFuture<?> fut2 = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Random rnd = new GridRandom();
            Random age = new GridRandom();
            while (!stop.get()) cache.put(rnd.nextInt(2000), new Person(age.nextInt(3000) - 1000));
            return null;
        }
    }, 20);
    Thread.sleep(30 * 1000);
    stop.set(true);
    fut2.get(10 * 1000);
    fut1.get(10 * 1000);
}
Also used : 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) List(java.util.List)

Example 27 with GridRandom

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

the class IgniteCacheClientQueryReplicatedNodeRestartSelfTest method testRestarts.

/**
     * @throws Exception If failed.
     */
public void testRestarts() throws Exception {
    int duration = 90 * 1000;
    int qryThreadNum = 5;
    // 2 of 4 data nodes
    int restartThreadsNum = 2;
    final int nodeLifeTime = 2 * 1000;
    final int logFreq = 10;
    startGridsMultiThreaded(GRID_CNT);
    // The last is client only.
    final AtomicIntegerArray locks = new AtomicIntegerArray(GRID_CNT - 1);
    fillCaches();
    final List<List<?>> pRes = grid(0).cache("pu").query(new SqlFieldsQuery(QRY)).getAll();
    Thread.sleep(3000);
    assertEquals(pRes, grid(0).cache("pu").query(new SqlFieldsQuery(QRY)).getAll());
    assertFalse(pRes.isEmpty());
    final AtomicInteger qryCnt = new AtomicInteger();
    final AtomicBoolean qrysDone = new AtomicBoolean();
    final List<Integer> cacheSize = new ArrayList<>(4);
    for (int i = 0; i < GRID_CNT - 1; i++) {
        int j = 0;
        for (String cacheName : F.asList("co", "pr", "pe", "pu")) {
            IgniteCache<?, ?> cache = grid(i).cache(cacheName);
            assertClient(cache, false);
            if (i == 0)
                cacheSize.add(cache.size());
            else
                assertEquals(cacheSize.get(j++).intValue(), cache.size());
        }
    }
    int j = 0;
    for (String cacheName : F.asList("co", "pr", "pe", "pu")) {
        IgniteCache<?, ?> cache = grid(GRID_CNT - 1).cache(cacheName);
        assertClient(cache, true);
        assertEquals(cacheSize.get(j++).intValue(), cache.size());
    }
    final IgniteCache<?, ?> clientCache = grid(GRID_CNT - 1).cache("pu");
    IgniteInternalFuture<?> fut1 = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            GridRandom rnd = new GridRandom();
            while (!qrysDone.get()) {
                SqlFieldsQuery qry = new SqlFieldsQuery(QRY);
                boolean smallPageSize = rnd.nextBoolean();
                if (smallPageSize)
                    qry.setPageSize(3);
                List<List<?>> res;
                try {
                    res = clientCache.query(qry).getAll();
                } catch (CacheException e) {
                    assertTrue("On large page size must retry.", smallPageSize);
                    boolean failedOnRemoteFetch = false;
                    for (Throwable th = e; th != null; th = th.getCause()) {
                        if (!(th instanceof CacheException))
                            continue;
                        if (th.getMessage() != null && th.getMessage().startsWith("Failed to fetch data from node:")) {
                            failedOnRemoteFetch = true;
                            break;
                        }
                    }
                    if (!failedOnRemoteFetch) {
                        e.printStackTrace();
                        fail("Must fail inside of GridResultPage.fetchNextPage or subclass.");
                    }
                    res = FAKE;
                }
                if (res != FAKE && !res.equals(pRes)) {
                    int j = 0;
                    // Check for data loss.
                    for (String cacheName : F.asList("co", "pr", "pe", "pu")) {
                        assertEquals(cacheName, cacheSize.get(j++).intValue(), grid(GRID_CNT - 1).cache(cacheName).size());
                    }
                    // Fail with nice message.
                    assertEquals(pRes, res);
                }
                int c = qryCnt.incrementAndGet();
                if (c % logFreq == 0)
                    info("Executed queries: " + c);
            }
        }
    }, qryThreadNum, "query-thread");
    final AtomicInteger restartCnt = new AtomicInteger();
    final AtomicBoolean restartsDone = new AtomicBoolean();
    IgniteInternalFuture<?> fut2 = multithreadedAsync(new Callable<Object>() {

        @SuppressWarnings({ "BusyWait" })
        @Override
        public Object call() throws Exception {
            GridRandom rnd = new GridRandom();
            while (!restartsDone.get()) {
                int g;
                do {
                    g = rnd.nextInt(locks.length());
                } while (!locks.compareAndSet(g, 0, -1));
                log.info("Stop node: " + g);
                stopGrid(g);
                Thread.sleep(rnd.nextInt(nodeLifeTime));
                log.info("Start node: " + g);
                startGrid(g);
                Thread.sleep(rnd.nextInt(nodeLifeTime));
                locks.set(g, 0);
                int c = restartCnt.incrementAndGet();
                if (c % logFreq == 0)
                    info("Node restarts: " + c);
            }
            return true;
        }
    }, restartThreadsNum, "restart-thread");
    Thread.sleep(duration);
    info("Stopping..");
    restartsDone.set(true);
    fut2.get();
    info("Restarts stopped.");
    qrysDone.set(true);
    fut1.get();
    info("Queries stopped.");
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) CacheException(javax.cache.CacheException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) CAX(org.apache.ignite.internal.util.typedef.CAX)

Example 28 with GridRandom

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

the class IgniteCacheClientQueryReplicatedNodeRestartSelfTest 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 29 with GridRandom

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

the class IgniteDbPutGetAbstractTest method testPutGetRandomNonUniqueMultipleObjects.

/**
     * @throws Exception if failed.
     */
public void testPutGetRandomNonUniqueMultipleObjects() throws Exception {
    IgniteEx ig = grid(0);
    final IgniteCache<Integer, DbValue> cache = ig.cache(DEFAULT_CACHE_NAME);
    GridCacheAdapter<Object, Object> internalCache = ig.context().cache().internalCache(DEFAULT_CACHE_NAME);
    int cnt = 100_000;
    Random rnd = new GridRandom();
    Map<Integer, DbValue> map = new HashMap<>();
    X.println("Put start");
    for (int a = 0; a < cnt; a++) {
        int i = rnd.nextInt();
        int k = rnd.nextInt(cnt);
        DbValue v0 = new DbValue(k, "test-value", i);
        //            if (a % 100 == 0)
        //                X.println(" --> " + k + " = " + i);
        map.put(k, v0);
        cache.put(k, v0);
        checkEmpty(internalCache, k);
        assertEquals(v0, cache.get(k));
    //            for (Map.Entry<Integer,DbValue> entry : map.entrySet())
    //                assertEquals(entry.getValue(), cache.get(entry.getKey()));
    }
    X.println("Get start: " + map.size());
    for (int i : map.keySet()) {
        checkEmpty(internalCache, i);
        //            X.println(" <-- " + i);
        assertEquals(map.get(i), cache.get(i));
    }
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IgniteEx(org.apache.ignite.internal.IgniteEx)

Example 30 with GridRandom

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

the class IgniteDbPutGetAbstractTest method testRandomRemove.

/**
     *
     */
public void testRandomRemove() {
    IgniteEx ig = grid(0);
    IgniteCache<Integer, DbValue> cache = ig.cache(DEFAULT_CACHE_NAME);
    final int cnt = 50_000;
    long seed = System.nanoTime();
    X.println("Seed: " + seed);
    Random rnd = new GridRandom(seed);
    int[] keys = generateUniqueRandomKeys(cnt, rnd);
    X.println("Put start");
    for (int i : keys) {
        DbValue v0 = new DbValue(i, "test-value", i);
        //            if (i % 1000 == 0)
        //                X.println(" --> " + i);
        cache.put(i, v0);
        assertEquals(v0, cache.get(i));
    }
    keys = generateUniqueRandomKeys(cnt, rnd);
    X.println("Rmv start");
    for (int i : keys) {
        //            X.println(" --> " + i);
        assertTrue(cache.remove(i));
    }
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IgniteEx(org.apache.ignite.internal.IgniteEx)

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