Search in sources :

Example 1 with RangeSource

use of org.apache.ignite.internal.processors.query.h2.opt.join.RangeSource in project ignite by apache.

the class H2TreeIndex method onIndexRangeRequest.

/**
 * @param node Requesting node.
 * @param msg Request message.
 */
private void onIndexRangeRequest(final ClusterNode node, final GridH2IndexRangeRequest msg) {
    // We don't use try with resources on purpose - the catch block must also be executed in the context of this span.
    TraceSurroundings trace = MTC.support(ctx.tracing().create(SQL_IDX_RANGE_REQ, MTC.span()));
    Span span = MTC.span();
    try {
        span.addTag(SQL_IDX, () -> idxName);
        span.addTag(SQL_TABLE, () -> tblName);
        GridH2IndexRangeResponse res = new GridH2IndexRangeResponse();
        res.originNodeId(msg.originNodeId());
        res.queryId(msg.queryId());
        res.originSegmentId(msg.originSegmentId());
        res.segment(msg.segment());
        res.batchLookupId(msg.batchLookupId());
        QueryContext qctx = qryCtxRegistry.getShared(msg.originNodeId(), msg.queryId(), msg.originSegmentId());
        if (qctx == null)
            res.status(STATUS_NOT_FOUND);
        else {
            DistributedJoinContext joinCtx = qctx.distributedJoinContext();
            assert joinCtx != null;
            try {
                RangeSource src;
                if (msg.bounds() != null) {
                    // This is the first request containing all the search rows.
                    assert !msg.bounds().isEmpty() : "empty bounds";
                    src = new RangeSource(this, msg.bounds(), msg.segment(), idxQryContext(qctx));
                } else {
                    // This is request to fetch next portion of data.
                    src = joinCtx.getSource(node.id(), msg.segment(), msg.batchLookupId());
                    assert src != null;
                }
                List<GridH2RowRange> ranges = new ArrayList<>();
                int maxRows = joinCtx.pageSize();
                assert maxRows > 0 : maxRows;
                while (maxRows > 0) {
                    GridH2RowRange range = src.next(maxRows);
                    if (range == null)
                        break;
                    ranges.add(range);
                    if (range.rows() != null)
                        maxRows -= range.rows().size();
                }
                assert !ranges.isEmpty();
                if (src.hasMoreRows()) {
                    // Save source for future fetches.
                    if (msg.bounds() != null)
                        joinCtx.putSource(node.id(), msg.segment(), msg.batchLookupId(), src);
                } else if (msg.bounds() == null) {
                    // Drop saved source.
                    joinCtx.putSource(node.id(), msg.segment(), msg.batchLookupId(), null);
                }
                res.ranges(ranges);
                res.status(STATUS_OK);
                span.addTag(SQL_IDX_RANGE_ROWS, () -> Integer.toString(ranges.stream().mapToInt(GridH2RowRange::rowsSize).sum()));
            } catch (Throwable th) {
                span.addTag(ERROR, th::getMessage);
                U.error(log, "Failed to process request: " + msg, th);
                res.error(th.getClass() + ": " + th.getMessage());
                res.status(STATUS_ERROR);
            }
        }
        send(singletonList(node), res);
    } catch (Throwable th) {
        span.addTag(ERROR, th::getMessage);
        throw th;
    } finally {
        if (trace != null)
            trace.close();
    }
}
Also used : GridH2IndexRangeResponse(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2IndexRangeResponse) DistributedJoinContext(org.apache.ignite.internal.processors.query.h2.opt.join.DistributedJoinContext) RangeSource(org.apache.ignite.internal.processors.query.h2.opt.join.RangeSource) ArrayList(java.util.ArrayList) GridH2RowRange(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2RowRange) IndexQueryContext(org.apache.ignite.internal.cache.query.index.sorted.inline.IndexQueryContext) QueryContext(org.apache.ignite.internal.processors.query.h2.opt.QueryContext) Span(org.apache.ignite.internal.processors.tracing.Span) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings)

Example 2 with RangeSource

use of org.apache.ignite.internal.processors.query.h2.opt.join.RangeSource in project ignite by apache.

the class GridH2IndexBase method onIndexRangeRequest.

/**
 * @param node Requesting node.
 * @param msg Request message.
 */
private void onIndexRangeRequest(final ClusterNode node, final GridH2IndexRangeRequest msg) {
    GridH2IndexRangeResponse res = new GridH2IndexRangeResponse();
    res.originNodeId(msg.originNodeId());
    res.queryId(msg.queryId());
    res.originSegmentId(msg.originSegmentId());
    res.segment(msg.segment());
    res.batchLookupId(msg.batchLookupId());
    GridH2QueryContext qctx = GridH2QueryContext.get(kernalContext().localNodeId(), msg.originNodeId(), msg.queryId(), msg.originSegmentId(), MAP);
    if (qctx == null)
        res.status(STATUS_NOT_FOUND);
    else {
        try {
            RangeSource src;
            if (msg.bounds() != null) {
                // This is the first request containing all the search rows.
                assert !msg.bounds().isEmpty() : "empty bounds";
                src = new RangeSource(msg.bounds(), msg.segment(), qctx.filter());
            } else {
                // This is request to fetch next portion of data.
                src = qctx.getSource(node.id(), msg.segment(), msg.batchLookupId());
                assert src != null;
            }
            List<GridH2RowRange> ranges = new ArrayList<>();
            int maxRows = qctx.pageSize();
            assert maxRows > 0 : maxRows;
            while (maxRows > 0) {
                GridH2RowRange range = src.next(maxRows);
                if (range == null)
                    break;
                ranges.add(range);
                if (range.rows() != null)
                    maxRows -= range.rows().size();
            }
            assert !ranges.isEmpty();
            if (src.hasMoreRows()) {
                // Save source for future fetches.
                if (msg.bounds() != null)
                    qctx.putSource(node.id(), msg.segment(), msg.batchLookupId(), src);
            } else if (msg.bounds() == null) {
                // Drop saved source.
                qctx.putSource(node.id(), msg.segment(), msg.batchLookupId(), null);
            }
            res.ranges(ranges);
            res.status(STATUS_OK);
        } catch (Throwable th) {
            U.error(log, "Failed to process request: " + msg, th);
            res.error(th.getClass() + ": " + th.getMessage());
            res.status(STATUS_ERROR);
        }
    }
    send(singletonList(node), res);
}
Also used : GridH2IndexRangeResponse(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2IndexRangeResponse) ArrayList(java.util.ArrayList) GridH2RowRange(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2RowRange)

Aggregations

ArrayList (java.util.ArrayList)2 GridH2IndexRangeResponse (org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2IndexRangeResponse)2 GridH2RowRange (org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2RowRange)2 IndexQueryContext (org.apache.ignite.internal.cache.query.index.sorted.inline.IndexQueryContext)1 QueryContext (org.apache.ignite.internal.processors.query.h2.opt.QueryContext)1 DistributedJoinContext (org.apache.ignite.internal.processors.query.h2.opt.join.DistributedJoinContext)1 RangeSource (org.apache.ignite.internal.processors.query.h2.opt.join.RangeSource)1 TraceSurroundings (org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings)1 Span (org.apache.ignite.internal.processors.tracing.Span)1