Search in sources :

Example 51 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class IgniteCacheQueryNodeRestartDistributedJoinSelfTest method restarts.

/**
     * @param broadcastQry If {@code true} tests broadcast query.
     * @throws Exception If failed.
     */
private void restarts(final boolean broadcastQry) throws Exception {
    int duration = 90 * 1000;
    int qryThreadNum = 4;
    // 4 + 2 = 6 nodes
    int restartThreadsNum = 2;
    final int nodeLifeTime = 4000;
    final int logFreq = 100;
    final AtomicIntegerArray locks = new AtomicIntegerArray(totalNodes);
    SqlFieldsQuery qry0;
    if (broadcastQry)
        qry0 = new SqlFieldsQuery(QRY_0_BROADCAST).setDistributedJoins(true).setEnforceJoinOrder(true);
    else
        qry0 = new SqlFieldsQuery(QRY_0).setDistributedJoins(true);
    String plan = queryPlan(grid(0).cache("pu"), qry0);
    X.println("Plan1: " + plan);
    assertEquals(broadcastQry, plan.contains("batched:broadcast"));
    final List<List<?>> pRes = grid(0).cache("pu").query(qry0).getAll();
    Thread.sleep(3000);
    assertEquals(pRes, grid(0).cache("pu").query(qry0).getAll());
    final SqlFieldsQuery qry1;
    if (broadcastQry)
        qry1 = new SqlFieldsQuery(QRY_1_BROADCAST).setDistributedJoins(true).setEnforceJoinOrder(true);
    else
        qry1 = new SqlFieldsQuery(QRY_1).setDistributedJoins(true);
    plan = queryPlan(grid(0).cache("co"), qry1);
    X.println("Plan2: " + plan);
    assertEquals(broadcastQry, plan.contains("batched:broadcast"));
    final List<List<?>> rRes = grid(0).cache("co").query(qry1).getAll();
    assertFalse(pRes.isEmpty());
    assertFalse(rRes.isEmpty());
    final AtomicInteger qryCnt = new AtomicInteger();
    final AtomicBoolean qrysDone = new AtomicBoolean();
    final AtomicBoolean fail = new AtomicBoolean();
    IgniteInternalFuture<?> fut1 = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            GridRandom rnd = new GridRandom();
            try {
                while (!qrysDone.get()) {
                    int g;
                    do {
                        g = rnd.nextInt(locks.length());
                        if (fail.get())
                            return;
                    } while (!locks.compareAndSet(g, 0, 1));
                    if (rnd.nextBoolean()) {
                        IgniteCache<?, ?> cache = grid(g).cache("pu");
                        SqlFieldsQuery qry;
                        if (broadcastQry)
                            qry = new SqlFieldsQuery(QRY_0_BROADCAST).setDistributedJoins(true).setEnforceJoinOrder(true);
                        else
                            qry = new SqlFieldsQuery(QRY_0).setDistributedJoins(true);
                        boolean smallPageSize = rnd.nextBoolean();
                        qry.setPageSize(smallPageSize ? 30 : 1000);
                        try {
                            assertEquals(pRes, cache.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.");
                            }
                        }
                    } else {
                        IgniteCache<?, ?> cache = grid(g).cache("co");
                        assertEquals(rRes, cache.query(qry1).getAll());
                    }
                    locks.set(g, 0);
                    int c = qryCnt.incrementAndGet();
                    if (c % logFreq == 0)
                        info("Executed queries: " + c);
                }
            } catch (Throwable e) {
                e.printStackTrace();
                error("Got exception: " + e.getMessage());
                fail.set(true);
            }
        }
    }, 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 {
            try {
                GridRandom rnd = new GridRandom();
                while (!restartsDone.get()) {
                    int g;
                    do {
                        g = rnd.nextInt(locks.length());
                        if (fail.get())
                            return null;
                    } 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;
            } catch (Throwable e) {
                e.printStackTrace();
                return true;
            }
        }
    }, restartThreadsNum, "restart-thread");
    Thread.sleep(duration);
    info("Stopping...");
    restartsDone.set(true);
    qrysDone.set(true);
    fut2.get();
    fut1.get();
    if (fail.get())
        fail("See message above");
    info("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) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) CAX(org.apache.ignite.internal.util.typedef.CAX)

Example 52 with CacheException

use of javax.cache.CacheException 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 53 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class IgniteCacheQueryStopOnCancelOrTimeoutDistributedJoinSelfTest method testQueryCancel.

/** */
private void testQueryCancel(Ignite ignite, String cacheName, String sql, int timeoutUnits, TimeUnit timeUnit, boolean timeout) throws Exception {
    SqlFieldsQuery qry = new SqlFieldsQuery(sql).setDistributedJoins(true);
    IgniteCache<Object, Object> cache = ignite.cache(cacheName);
    final QueryCursor<List<?>> cursor;
    if (timeout) {
        qry.setTimeout(timeoutUnits, timeUnit);
        cursor = cache.query(qry);
    } else {
        cursor = cache.query(qry);
        ignite.scheduler().runLocal(new Runnable() {

            @Override
            public void run() {
                cursor.close();
            }
        }, timeoutUnits, timeUnit);
    }
    try (QueryCursor<List<?>> ignored = cursor) {
        cursor.iterator();
    } catch (CacheException ex) {
        log().error("Got expected exception", ex);
        assertTrue("Must throw correct exception", ex.getCause() instanceof QueryCancelledException);
    }
    // Give some time to clean up.
    Thread.sleep(TimeUnit.MILLISECONDS.convert(timeoutUnits, timeUnit) + 3_000);
    checkCleanState();
}
Also used : CacheException(javax.cache.CacheException) List(java.util.List) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 54 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class IgniteCacheDistributedPartitionQueryAbstractSelfTest method doTestJoinQuery.

/**
     * @param orig Query originator.
     * @param regionIds Region ids.
     */
protected void doTestJoinQuery(Ignite orig, int... regionIds) {
    IgniteCache<ClientKey, Client> cl = orig.cache("cl");
    if (regionIds == null) {
        regionIds = new int[PARTS_PER_REGION.length];
        for (int i = 0; i < regionIds.length; i++) regionIds[i] = i + 1;
    }
    for (int regionId : regionIds) {
        List<Integer> range = REGION_TO_PART_MAP.get(regionId);
        SqlFieldsQuery qry = new SqlFieldsQuery(JOIN_QRY);
        int[] pSet = createRange(range.get(0), 1 + rnd.nextInt(range.get(1) - 1));
        qry.setPartitions(pSet);
        try {
            List<List<?>> rows = cl.query(qry).getAll();
            for (List<?> row : rows) {
                ClientKey key = (ClientKey) row.get(0);
                int p = orig.affinity("cl").partition(key);
                assertTrue(Arrays.binarySearch(pSet, p) >= 0);
            }
            // Query must produce only results from single region.
            for (List<?> row : rows) assertEquals("Region id", regionId, ((Integer) row.get(2)).intValue());
            if (regionId == UNMAPPED_REGION)
                fail();
        } catch (CacheException ignored) {
            if (X.hasCause(ignored, InterruptedException.class, IgniteInterruptedCheckedException.class))
                // Allow interruptions.
                return;
            if (regionId != UNMAPPED_REGION)
                fail();
        }
    }
}
Also used : CacheException(javax.cache.CacheException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 55 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class IgniteCacheDistributedQueryStopOnCancelOrTimeoutSelfTest method testQueryCancel.

/** */
private void testQueryCancel(int keyCnt, int valSize, String sql, int timeoutUnits, TimeUnit timeUnit, boolean timeout) throws Exception {
    try (Ignite client = startGrid("client")) {
        IgniteCache<Object, Object> cache = client.cache(DEFAULT_CACHE_NAME);
        assertEquals(0, cache.localSize());
        int p = 1;
        for (int i = 1; i <= keyCnt; i++) {
            char[] tmp = new char[valSize];
            Arrays.fill(tmp, ' ');
            cache.put(i, new String(tmp));
            if (i / (float) keyCnt >= p / 10f) {
                log().info("Loaded " + i + " of " + keyCnt);
                p++;
            }
        }
        assertEquals(0, cache.localSize());
        SqlFieldsQuery qry = new SqlFieldsQuery(sql);
        final QueryCursor<List<?>> cursor;
        if (timeout) {
            qry.setTimeout(timeoutUnits, timeUnit);
            cursor = cache.query(qry);
        } else {
            cursor = cache.query(qry);
            client.scheduler().runLocal(new Runnable() {

                @Override
                public void run() {
                    cursor.close();
                }
            }, timeoutUnits, timeUnit);
        }
        try (QueryCursor<List<?>> ignored = cursor) {
            cursor.iterator();
        } catch (CacheException ex) {
            log().error("Got expected exception", ex);
            assertTrue("Must throw correct exception", ex.getCause() instanceof QueryCancelledException);
        }
        // Give some time to clean up.
        Thread.sleep(TimeUnit.MILLISECONDS.convert(timeoutUnits, timeUnit) + 3_000);
        checkCleanState();
    }
}
Also used : CacheException(javax.cache.CacheException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Ignite(org.apache.ignite.Ignite) List(java.util.List) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException)

Aggregations

CacheException (javax.cache.CacheException)144 Ignite (org.apache.ignite.Ignite)42 IgniteException (org.apache.ignite.IgniteException)36 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)26 Transaction (org.apache.ignite.transactions.Transaction)25 ArrayList (java.util.ArrayList)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)18 IgniteCache (org.apache.ignite.IgniteCache)18 CountDownLatch (java.util.concurrent.CountDownLatch)17 List (java.util.List)16 Map (java.util.Map)16 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)15 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)13 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)13 HashMap (java.util.HashMap)12 IgniteTransactions (org.apache.ignite.IgniteTransactions)12 CyclicBarrier (java.util.concurrent.CyclicBarrier)11 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)10 Cache (javax.cache.Cache)9