Search in sources :

Example 11 with GridRandom

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

the class IgniteCacheQueryNodeRestartSelfTest2 method testRestarts.

/**
     * @throws Exception If failed.
     */
public void testRestarts() throws Exception {
    int duration = 90 * 1000;
    int qryThreadNum = 4;
    // 4 + 2 = 6 nodes
    int restartThreadsNum = 2;
    final int nodeLifeTime = 2 * 1000;
    final int logFreq = 10;
    startGridsMultiThreaded(GRID_CNT);
    final AtomicIntegerArray locks = new AtomicIntegerArray(GRID_CNT);
    fillCaches();
    final List<List<?>> pRes = grid(0).cache("pu").query(new SqlFieldsQuery(PARTITIONED_QRY)).getAll();
    Thread.sleep(3000);
    assertEquals(pRes, grid(0).cache("pu").query(new SqlFieldsQuery(PARTITIONED_QRY)).getAll());
    final List<List<?>> rRes = grid(0).cache("co").query(new SqlFieldsQuery(REPLICATED_QRY)).getAll();
    assertFalse(pRes.isEmpty());
    assertFalse(rRes.isEmpty());
    final AtomicInteger qryCnt = new AtomicInteger();
    final AtomicBoolean qrysDone = new AtomicBoolean();
    IgniteInternalFuture<?> fut1 = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            GridRandom rnd = new GridRandom();
            while (!qrysDone.get()) {
                int g;
                do {
                    g = rnd.nextInt(locks.length());
                } while (!locks.compareAndSet(g, 0, 1));
                try {
                    if (rnd.nextBoolean()) {
                        // Partitioned query.
                        IgniteCache<?, ?> cache = grid(g).cache("pu");
                        SqlFieldsQuery qry = new SqlFieldsQuery(PARTITIONED_QRY);
                        boolean smallPageSize = rnd.nextBoolean();
                        if (smallPageSize)
                            qry.setPageSize(3);
                        try {
                            assertEquals(pRes, cache.query(qry).getAll());
                        } catch (CacheException e) {
                            // Interruptions are expected here.
                            if (e.getCause() instanceof IgniteInterruptedCheckedException)
                                continue;
                            if (e.getCause() instanceof QueryCancelledException)
                                fail("Retry is expected");
                            if (!smallPageSize)
                                e.printStackTrace();
                            assertTrue("On large page size must retry.", smallPageSize);
                            boolean failedOnRemoteFetch = false;
                            boolean failedOnInterruption = false;
                            for (Throwable th = e; th != null; th = th.getCause()) {
                                if (th instanceof InterruptedException) {
                                    failedOnInterruption = true;
                                    break;
                                }
                                if (!(th instanceof CacheException))
                                    continue;
                                if (th.getMessage() != null && th.getMessage().startsWith("Failed to fetch data from node:")) {
                                    failedOnRemoteFetch = true;
                                    break;
                                }
                            }
                            // Interruptions are expected here.
                            if (failedOnInterruption)
                                continue;
                            if (!failedOnRemoteFetch) {
                                e.printStackTrace();
                                fail("Must fail inside of GridResultPage.fetchNextPage or subclass.");
                            }
                        }
                    } else {
                        // Replicated query.
                        IgniteCache<?, ?> cache = grid(g).cache("co");
                        assertEquals(rRes, cache.query(new SqlFieldsQuery(REPLICATED_QRY)).getAll());
                    }
                } finally {
                    // Clearing lock in final handler to avoid endless loop if exception is thrown.
                    locks.set(g, 0);
                    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));
                try {
                    log.info("Stop node: " + g);
                    stopGrid(g);
                    Thread.sleep(rnd.nextInt(nodeLifeTime));
                    log.info("Start node: " + g);
                    startGrid(g);
                    Thread.sleep(rnd.nextInt(nodeLifeTime));
                } finally {
                    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);
    // Query thread can stuck in next page waiting loop because all nodes are left.
    try {
        fut1.get(5_000);
    } catch (IgniteFutureTimeoutCheckedException ignored) {
        fut1.cancel();
    }
    info("Queries stopped.");
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) CacheException(javax.cache.CacheException) IgniteCache(org.apache.ignite.IgniteCache) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) CacheException(javax.cache.CacheException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) List(java.util.List) CAX(org.apache.ignite.internal.util.typedef.CAX) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException)

Example 12 with GridRandom

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

the class BPlusTreeSelfTest method doTestMassivePut.

/**
     * @param canGetRow Can get row in inner page.
     * @throws Exception If failed.
     */
private void doTestMassivePut(final boolean canGetRow) throws Exception {
    final int threads = 16;
    // We may fail to insert more on small pages size because of tree height.
    final int keys = 26;
    final TestTree tree = createTestTree(canGetRow);
    info("Put...");
    final AtomicLongArray k = new AtomicLongArray(keys);
    GridTestUtils.runMultiThreaded(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            Random rnd = new GridRandom();
            for (; ; ) {
                int idx = 0;
                boolean found = false;
                for (int i = 0, shift = rnd.nextInt(keys); i < keys; i++) {
                    idx = (i + shift) % keys;
                    if (k.get(idx) == 0 && k.compareAndSet(idx, 0, 1)) {
                        found = true;
                        break;
                    }
                }
                if (!found)
                    break;
                assertNull(tree.put((long) idx));
                assertNoLocks();
            }
            return null;
        }
    }, threads, "put");
    assertEquals(keys, tree.size());
    tree.validateTree();
    GridCursor<Long> c = tree.find(null, null);
    long x = 0;
    while (c.next()) assertEquals(Long.valueOf(x++), c.get());
    assertEquals(keys, x);
    assertNoLocks();
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 13 with GridRandom

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

the class BPlusTreeSelfTest method doTestMassiveRemove.

/**
     * @param canGetRow Can get row in inner page.
     * @throws Exception If failed.
     */
private void doTestMassiveRemove(final boolean canGetRow) throws Exception {
    final int threads = 64;
    final int keys = 3000;
    final AtomicLongArray rmvd = new AtomicLongArray(keys);
    final TestTree tree = createTestTree(canGetRow);
    // Put keys in reverse order to have a better balance in the tree (lower height).
    for (long i = keys - 1; i >= 0; i--) {
        tree.put(i);
    //            X.println(tree.printTree());
    }
    assertEquals(keys, tree.size());
    tree.validateTree();
    info("Remove...");
    try {
        GridTestUtils.runMultiThreaded(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                Random rnd = new GridRandom();
                for (; ; ) {
                    int idx = 0;
                    boolean found = false;
                    for (int i = 0, shift = rnd.nextInt(keys); i < keys; i++) {
                        idx = (i + shift) % keys;
                        if (rmvd.get(idx) == 0 && rmvd.compareAndSet(idx, 0, 1)) {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                        break;
                    assertEquals(Long.valueOf(idx), tree.remove((long) idx));
                    if (canGetRow)
                        rmvdIds.add((long) idx);
                }
                return null;
            }
        }, threads, "remove");
        assertEquals(0, tree.size());
        tree.validateTree();
    } finally {
        rmvdIds.clear();
    }
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 14 with GridRandom

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

the class IgniteSqlSplitterSelfTest method doTestDistributedJoins.

/**
     * @param c1 Persons cache.
     * @param c2 Organizations cache.
     * @param orgs Number of organizations.
     * @param persons Number of persons.
     * @param pageSize Page size.
     * @param enforceJoinOrder Enforce join order.
     */
private void doTestDistributedJoins(IgniteCache<?, ?> qryCache, IgniteCache<Integer, Person2> c1, IgniteCache<Integer, Organization> c2, int orgs, int persons, int pageSize, boolean enforceJoinOrder) {
    assertEquals(0, c1.size(CachePeekMode.ALL));
    assertEquals(0, c2.size(CachePeekMode.ALL));
    int key = 0;
    for (int i = 0; i < orgs; i++) {
        Organization o = new Organization();
        o.name = "Org" + i;
        c2.put(key++, o);
    }
    Random rnd = new GridRandom();
    for (int i = 0; i < persons; i++) {
        Person2 p = new Person2();
        p.name = "Person" + i;
        p.orgId = rnd.nextInt(orgs);
        c1.put(key++, p);
    }
    String select = "select count(*) from \"org\".Organization o, \"pers\".Person2 p where p.orgId = o._key";
    String plan = (String) qryCache.query(new SqlFieldsQuery("explain " + select).setDistributedJoins(true).setEnforceJoinOrder(enforceJoinOrder).setPageSize(pageSize)).getAll().get(0).get(0);
    X.println("Plan : " + plan);
    if (enforceJoinOrder)
        assertTrue(plan, plan.contains("batched:broadcast"));
    else
        assertTrue(plan, plan.contains("batched:unicast"));
    assertEquals((long) persons, qryCache.query(new SqlFieldsQuery(select).setDistributedJoins(true).setEnforceJoinOrder(enforceJoinOrder).setPageSize(pageSize)).getAll().get(0).get(0));
    c1.clear();
    c2.clear();
    assertEquals(0, c1.size(CachePeekMode.ALL));
    assertEquals(0L, c1.query(new SqlFieldsQuery(select).setDistributedJoins(true).setEnforceJoinOrder(enforceJoinOrder).setPageSize(pageSize)).getAll().get(0).get(0));
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 15 with GridRandom

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

the class IgniteSqlSplitterSelfTest method testExists.

@SuppressWarnings("SuspiciousMethodCalls")
public void testExists() {
    IgniteCache<Integer, Person2> x = ignite(0).getOrCreateCache(cacheConfig("x", true, Integer.class, Person2.class));
    IgniteCache<Integer, Person2> y = ignite(0).getOrCreateCache(cacheConfig("y", true, Integer.class, Person2.class));
    try {
        GridRandom rnd = new GridRandom();
        Set<Integer> intersects = new HashSet<>();
        for (int i = 0; i < 3000; i++) {
            int r = rnd.nextInt(3);
            if (r != 0)
                x.put(i, new Person2(i, "pers_x_" + i));
            if (r != 1)
                y.put(i, new Person2(i, "pers_y_" + i));
            if (r == 2)
                intersects.add(i);
        }
        assertFalse(intersects.isEmpty());
        List<List<?>> res = x.query(new SqlFieldsQuery("select _key from \"x\".Person2 px " + "where exists(select 1 from \"y\".Person2 py where px._key = py._key)")).getAll();
        assertEquals(intersects.size(), res.size());
        for (List<?> row : res) assertTrue(intersects.contains(row.get(0)));
    } finally {
        x.destroy();
        y.destroy();
    }
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) ArrayList(java.util.ArrayList) List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) HashSet(java.util.HashSet)

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