Search in sources :

Example 1 with QueryCancelledException

use of org.apache.ignite.cache.query.QueryCancelledException 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 2 with QueryCancelledException

use of org.apache.ignite.cache.query.QueryCancelledException 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 3 with QueryCancelledException

use of org.apache.ignite.cache.query.QueryCancelledException 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)

Example 4 with QueryCancelledException

use of org.apache.ignite.cache.query.QueryCancelledException in project ignite by apache.

the class GridMapQueryExecutor method onQueryRequest0.

/**
     * @param node Node authored request.
     * @param reqId Request ID.
     * @param segmentId index segment ID.
     * @param schemaName Schema name.
     * @param qrys Queries to execute.
     * @param cacheIds Caches which will be affected by these queries.
     * @param topVer Topology version.
     * @param partsMap Partitions map for unstable topology.
     * @param parts Explicit partitions for current node.
     * @param tbls Tables.
     * @param pageSize Page size.
     * @param distributedJoinMode Query distributed join mode.
     */
private void onQueryRequest0(ClusterNode node, long reqId, int segmentId, String schemaName, Collection<GridCacheSqlQuery> qrys, List<Integer> cacheIds, AffinityTopologyVersion topVer, Map<UUID, int[]> partsMap, int[] parts, Collection<QueryTable> tbls, int pageSize, DistributedJoinMode distributedJoinMode, boolean enforceJoinOrder, boolean replicated, int timeout, Object[] params) {
    // Prepare to run queries.
    GridCacheContext<?, ?> mainCctx = !F.isEmpty(cacheIds) ? ctx.cache().context().cacheContext(cacheIds.get(0)) : null;
    NodeResults nodeRess = resultsForNode(node.id());
    QueryResults qr = null;
    List<GridReservable> reserved = new ArrayList<>();
    try {
        if (topVer != null) {
            // Reserve primary for topology version or explicit partitions.
            if (!reservePartitions(cacheIds, topVer, parts, reserved)) {
                sendRetry(node, reqId, segmentId);
                return;
            }
        }
        qr = new QueryResults(reqId, qrys.size(), mainCctx != null ? mainCctx.name() : null);
        if (nodeRess.put(reqId, segmentId, qr) != null)
            throw new IllegalStateException();
        // Prepare query context.
        GridH2QueryContext qctx = new GridH2QueryContext(ctx.localNodeId(), node.id(), reqId, segmentId, replicated ? REPLICATED : MAP).filter(h2.backupFilter(topVer, parts)).partitionsMap(partsMap).distributedJoinMode(distributedJoinMode).pageSize(pageSize).topologyVersion(topVer).reservations(reserved);
        List<GridH2Table> snapshotedTbls = null;
        if (!F.isEmpty(tbls)) {
            snapshotedTbls = new ArrayList<>(tbls.size());
            for (QueryTable tbl : tbls) {
                GridH2Table h2Tbl = h2.dataTable(tbl);
                Objects.requireNonNull(h2Tbl, tbl.toString());
                h2Tbl.snapshotIndexes(qctx, segmentId);
                snapshotedTbls.add(h2Tbl);
            }
        }
        Connection conn = h2.connectionForSchema(schemaName);
        H2Utils.setupConnection(conn, distributedJoinMode != OFF, enforceJoinOrder);
        GridH2QueryContext.set(qctx);
        // qctx is set, we have to release reservations inside of it.
        reserved = null;
        try {
            if (nodeRess.cancelled(reqId)) {
                GridH2QueryContext.clear(ctx.localNodeId(), node.id(), reqId, qctx.type());
                nodeRess.cancelRequest(reqId);
                throw new QueryCancelledException();
            }
            // Run queries.
            int qryIdx = 0;
            boolean evt = mainCctx != null && ctx.event().isRecordable(EVT_CACHE_QUERY_EXECUTED);
            for (GridCacheSqlQuery qry : qrys) {
                ResultSet rs = null;
                // If we are not the target node for this replicated query, just ignore it.
                if (qry.node() == null || (segmentId == 0 && qry.node().equals(ctx.localNodeId()))) {
                    rs = h2.executeSqlQueryWithTimer(conn, qry.query(), F.asList(qry.parameters(params)), true, timeout, qr.cancels[qryIdx]);
                    if (evt) {
                        assert mainCctx != null;
                        ctx.event().record(new CacheQueryExecutedEvent<>(node, "SQL query executed.", EVT_CACHE_QUERY_EXECUTED, CacheQueryType.SQL.name(), mainCctx.name(), null, qry.query(), null, null, params, node.id(), null));
                    }
                    assert rs instanceof JdbcResultSet : rs.getClass();
                }
                qr.addResult(qryIdx, qry, node.id(), rs, params);
                if (qr.canceled) {
                    qr.result(qryIdx).close();
                    throw new QueryCancelledException();
                }
                // Send the first page.
                sendNextPage(nodeRess, node, qr, qryIdx, segmentId, pageSize);
                qryIdx++;
            }
        } finally {
            GridH2QueryContext.clearThreadLocal();
            if (distributedJoinMode == OFF)
                qctx.clearContext(false);
            if (!F.isEmpty(snapshotedTbls)) {
                for (GridH2Table dataTbl : snapshotedTbls) dataTbl.releaseSnapshots();
            }
        }
    } catch (Throwable e) {
        if (qr != null) {
            nodeRess.remove(reqId, segmentId, qr);
            qr.cancel(false);
        }
        if (X.hasCause(e, GridH2RetryException.class))
            sendRetry(node, reqId, segmentId);
        else {
            U.error(log, "Failed to execute local query.", e);
            sendError(node, reqId, e);
            if (e instanceof Error)
                throw (Error) e;
        }
    } finally {
        if (reserved != null) {
            // Release reserved partitions.
            for (int i = 0; i < reserved.size(); i++) reserved.get(i).release();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Connection(java.sql.Connection) GridH2RetryException(org.apache.ignite.internal.processors.query.h2.opt.GridH2RetryException) GridReservable(org.apache.ignite.internal.processors.cache.distributed.dht.GridReservable) QueryTable(org.apache.ignite.internal.processors.cache.query.QueryTable) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) ResultSet(java.sql.ResultSet) JdbcResultSet(org.h2.jdbc.JdbcResultSet) GridCacheSqlQuery(org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) GridH2QueryContext(org.apache.ignite.internal.processors.query.h2.opt.GridH2QueryContext) JdbcResultSet(org.h2.jdbc.JdbcResultSet)

Example 5 with QueryCancelledException

use of org.apache.ignite.cache.query.QueryCancelledException in project ignite by apache.

the class GridMapQueryExecutor method onNextPageRequest.

/**
     * @param node Node.
     * @param req Request.
     */
private void onNextPageRequest(ClusterNode node, GridQueryNextPageRequest req) {
    NodeResults nodeRess = qryRess.get(node.id());
    if (nodeRess == null) {
        sendError(node, req.queryRequestId(), new CacheException("No node result found for request: " + req));
        return;
    } else if (nodeRess.cancelled(req.queryRequestId())) {
        sendError(node, req.queryRequestId(), new QueryCancelledException());
        return;
    }
    QueryResults qr = nodeRess.get(req.queryRequestId(), req.segmentId());
    if (qr == null)
        sendError(node, req.queryRequestId(), new CacheException("No query result found for request: " + req));
    else if (qr.canceled)
        sendError(node, req.queryRequestId(), new QueryCancelledException());
    else
        sendNextPage(nodeRess, node, qr, req.query(), req.segmentId(), req.pageSize());
}
Also used : CacheException(javax.cache.CacheException) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException)

Aggregations

QueryCancelledException (org.apache.ignite.cache.query.QueryCancelledException)8 CacheException (javax.cache.CacheException)6 List (java.util.List)5 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)4 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 Ignite (org.apache.ignite.Ignite)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)2 GridCacheSqlQuery (org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery)2 GridH2QueryContext (org.apache.ignite.internal.processors.query.h2.opt.GridH2QueryContext)2 Connection (java.sql.Connection)1 Collections.singletonList (java.util.Collections.singletonList)1 UUID (java.util.UUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)1 IgniteCache (org.apache.ignite.IgniteCache)1 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)1