Search in sources :

Example 1 with GridSpiCloseableIteratorWrapper

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

the class GridCacheQueryManager method runFieldsQuery.

/**
 * Processes fields query request.
 *
 * @param qryInfo Query info.
 */
protected void runFieldsQuery(GridCacheQueryInfo qryInfo) {
    assert qryInfo != null;
    if (!enterBusy()) {
        if (cctx.localNodeId().equals(qryInfo.senderId()))
            throw new IllegalStateException("Failed to process query request (grid is stopping).");
        // Ignore remote requests when when node is stopping.
        return;
    }
    try {
        if (log.isDebugEnabled())
            log.debug("Running query: " + qryInfo);
        boolean rmvRes = true;
        FieldsResult res = null;
        final boolean statsEnabled = cctx.statisticsEnabled();
        final boolean readEvt = cctx.events().isRecordable(EVT_CACHE_QUERY_OBJECT_READ);
        try {
            // Preparing query closures.
            IgniteReducer<Object, Object> rdc = (IgniteReducer<Object, Object>) qryInfo.reducer();
            injectResources(rdc);
            GridCacheQueryAdapter<?> qry = qryInfo.query();
            int pageSize = qry.pageSize();
            Collection<Object> data = null;
            Collection<Object> entities = null;
            if (qryInfo.local() || rdc != null || cctx.isLocalNode(qryInfo.senderId()))
                data = new ArrayList<>(pageSize);
            else
                entities = new ArrayList<>(pageSize);
            String taskName = cctx.kernalContext().task().resolveTaskName(qry.taskHash());
            res = qryInfo.local() ? executeFieldsQuery(qry, qryInfo.arguments(), qryInfo.local(), qry.subjectId(), taskName, recipient(qryInfo.senderId(), qryInfo.requestId())) : fieldsQueryResult(qryInfo, taskName);
            // If metadata needs to be returned to user and cleaned from internal fields - copy it.
            List<GridQueryFieldMetadata> meta = qryInfo.includeMetaData() ? (res.metaData() != null ? new ArrayList<>(res.metaData()) : null) : res.metaData();
            if (!qryInfo.includeMetaData())
                meta = null;
            GridCloseableIterator<?> it = new GridSpiCloseableIteratorWrapper<Object>(res.iterator(recipient(qryInfo.senderId(), qryInfo.requestId())));
            if (log.isDebugEnabled())
                log.debug("Received fields iterator [iterHasNext=" + it.hasNext() + ']');
            if (!it.hasNext()) {
                if (rdc != null)
                    data = Collections.singletonList(rdc.reduce());
                onFieldsPageReady(qryInfo.local(), qryInfo, meta, entities, data, true, null);
                return;
            }
            int cnt = 0;
            boolean metaSent = false;
            while (!Thread.currentThread().isInterrupted() && it.hasNext()) {
                long start = statsEnabled ? System.nanoTime() : 0L;
                Object row = it.next();
                // Query is cancelled.
                if (row == null) {
                    onPageReady(qryInfo.local(), qryInfo, null, true, null);
                    break;
                }
                if (statsEnabled) {
                    CacheMetricsImpl metrics = cctx.cache().metrics0();
                    metrics.onRead(true);
                    metrics.addGetTimeNanos(System.nanoTime() - start);
                }
                if (readEvt && cctx.gridEvents().hasListener(EVT_CACHE_QUERY_OBJECT_READ)) {
                    cctx.gridEvents().record(new CacheQueryReadEvent<K, V>(cctx.localNode(), "SQL fields query result set row read.", EVT_CACHE_QUERY_OBJECT_READ, CacheQueryType.SQL_FIELDS.name(), cctx.name(), null, qry.clause(), null, null, qryInfo.arguments(), qry.subjectId(), taskName, null, null, null, row));
                }
                if ((qryInfo.local() || rdc != null || cctx.isLocalNode(qryInfo.senderId()))) {
                    // Reduce.
                    if (rdc != null) {
                        if (!rdc.collect(row))
                            break;
                    } else
                        data.add(row);
                } else
                    entities.add(row);
                if (rdc == null && ((!qryInfo.allPages() && ++cnt == pageSize) || !it.hasNext())) {
                    onFieldsPageReady(qryInfo.local(), qryInfo, !metaSent ? meta : null, entities, data, !it.hasNext(), null);
                    if (it.hasNext())
                        rmvRes = false;
                    if (!qryInfo.allPages())
                        return;
                }
            }
            if (rdc != null) {
                onFieldsPageReady(qryInfo.local(), qryInfo, meta, null, Collections.singletonList(rdc.reduce()), true, null);
            }
        } catch (IgniteCheckedException e) {
            if (log.isDebugEnabled() || !e.hasCause(SQLException.class))
                U.error(log, "Failed to run fields query [qry=" + qryInfo + ", node=" + cctx.nodeId() + ']', e);
            else {
                if (e.hasCause(SQLException.class))
                    U.error(log, "Failed to run fields query [node=" + cctx.nodeId() + ", msg=" + e.getCause(SQLException.class).getMessage() + ']');
                else
                    U.error(log, "Failed to run fields query [node=" + cctx.nodeId() + ", msg=" + e.getMessage() + ']');
            }
            onFieldsPageReady(qryInfo.local(), qryInfo, null, null, null, true, e);
        } catch (Throwable e) {
            U.error(log, "Failed to run fields query [qry=" + qryInfo + ", node=" + cctx.nodeId() + "]", e);
            onFieldsPageReady(qryInfo.local(), qryInfo, null, null, null, true, e);
            if (e instanceof Error)
                throw (Error) e;
        } finally {
            if (qryInfo.local()) {
                // Don't we need to always remove local iterators?
                if (rmvRes && res != null) {
                    try {
                        res.closeIfNotShared(recipient(qryInfo.senderId(), qryInfo.requestId()));
                    } catch (IgniteCheckedException e) {
                        U.error(log, "Failed to close local iterator [qry=" + qryInfo + ", node=" + cctx.nodeId() + "]", e);
                    }
                }
            } else if (rmvRes)
                removeFieldsQueryResult(qryInfo.senderId(), qryInfo.requestId());
        }
    } finally {
        leaveBusy();
    }
}
Also used : GridSpiCloseableIteratorWrapper(org.apache.ignite.internal.util.GridSpiCloseableIteratorWrapper) SQLException(java.sql.SQLException) CacheMetricsImpl(org.apache.ignite.internal.processors.cache.CacheMetricsImpl) ArrayList(java.util.ArrayList) GridQueryFieldMetadata(org.apache.ignite.internal.processors.query.GridQueryFieldMetadata) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteReducer(org.apache.ignite.lang.IgniteReducer) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Aggregations

SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 CacheMetricsImpl (org.apache.ignite.internal.processors.cache.CacheMetricsImpl)1 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)1 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)1 GridQueryFieldMetadata (org.apache.ignite.internal.processors.query.GridQueryFieldMetadata)1 GridSpiCloseableIteratorWrapper (org.apache.ignite.internal.util.GridSpiCloseableIteratorWrapper)1 IgniteReducer (org.apache.ignite.lang.IgniteReducer)1