Search in sources :

Example 1 with SegmentKey

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

the class DistributedLookupBatch method addSearchRows.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "ForLoopReplaceableByForEach", "IfMayBeConditional" })
@Override
public boolean addSearchRows(SearchRow firstRow, SearchRow lastRow) {
    if (joinCtx == null || findCalled) {
        if (joinCtx == null) {
            // It is the first call after query begin (may be after reuse),
            // reinitialize query context and result.
            QueryContext qctx = QueryContext.threadLocal();
            res = new ArrayList<>();
            assert qctx != null;
            assert !findCalled;
            joinCtx = qctx.distributedJoinContext();
        } else {
            // Cleanup after the previous lookup phase.
            assert batchLookupId != 0;
            findCalled = false;
            joinCtx.putStreams(batchLookupId, null);
            res.clear();
        }
        // Reinitialize for the next lookup phase.
        batchLookupId = joinCtx.nextBatchLookupId();
        rangeStreams = new HashMap<>();
    }
    Object affKey = getAffinityKey(firstRow, lastRow);
    List<SegmentKey> segmentKeys;
    if (affKey != null) {
        // Affinity key is provided.
        if (// Affinity key is explicit null, we will not find anything.
        affKey == EXPLICIT_NULL)
            return false;
        segmentKeys = F.asList(rangeSegment(affKey));
    } else {
        // Affinity key is not provided or is not the same in upper and lower bounds, we have to broadcast.
        if (broadcastSegments == null)
            broadcastSegments = broadcastSegments();
        segmentKeys = broadcastSegments;
    }
    assert !F.isEmpty(segmentKeys) : segmentKeys;
    final int rangeId = res.size();
    // Create messages.
    GridH2RowMessage first = idx.toSearchRowMessage(firstRow);
    GridH2RowMessage last = idx.toSearchRowMessage(lastRow);
    // Range containing upper and lower bounds.
    GridH2RowRangeBounds rangeBounds = rangeBounds(rangeId, first, last);
    // Add range to every message of every participating node.
    for (int i = 0; i < segmentKeys.size(); i++) {
        SegmentKey segmentKey = segmentKeys.get(i);
        assert segmentKey != null;
        RangeStream stream = rangeStreams.get(segmentKey);
        List<GridH2RowRangeBounds> bounds;
        if (stream == null) {
            stream = new RangeStream(cctx.kernalContext(), idx, joinCtx, segmentKey.node());
            stream.request(createRequest(joinCtx, batchLookupId, segmentKey.segmentId()));
            stream.request().bounds(bounds = new ArrayList<>());
            rangeStreams.put(segmentKey, stream);
        } else
            bounds = stream.request().bounds();
        bounds.add(rangeBounds);
        // If at least one node will have a full batch then we are ok.
        if (bounds.size() >= joinCtx.pageSize())
            batchFull = true;
    }
    Cursor cur;
    if (segmentKeys.size() == 1)
        cur = new UnicastCursor(rangeId, rangeStreams.get(F.first(segmentKeys)));
    else
        cur = new BroadcastCursor(idx, rangeId, segmentKeys, rangeStreams);
    res.add(new DoneFuture<>(cur));
    return true;
}
Also used : GridH2RowRangeBounds(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2RowRangeBounds) ArrayList(java.util.ArrayList) QueryContext(org.apache.ignite.internal.processors.query.h2.opt.QueryContext) Cursor(org.h2.index.Cursor) GridH2RowMessage(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2RowMessage)

Example 2 with SegmentKey

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

the class H2TreeIndex method onIndexRangeResponse.

/**
 * @param node Responded node.
 * @param msg Response message.
 */
private void onIndexRangeResponse(ClusterNode node, GridH2IndexRangeResponse msg) {
    try (TraceSurroundings ignored = MTC.support(ctx.tracing().create(SQL_IDX_RANGE_RESP, MTC.span()))) {
        QueryContext qctx = qryCtxRegistry.getShared(msg.originNodeId(), msg.queryId(), msg.originSegmentId());
        if (qctx == null)
            return;
        DistributedJoinContext joinCtx = qctx.distributedJoinContext();
        assert joinCtx != null;
        Map<SegmentKey, RangeStream> streams = joinCtx.getStreams(msg.batchLookupId());
        if (streams == null)
            return;
        RangeStream stream = streams.get(new SegmentKey(node, msg.segment()));
        assert stream != null;
        stream.onResponse(msg);
    }
}
Also used : SegmentKey(org.apache.ignite.internal.processors.query.h2.opt.join.SegmentKey) DistributedJoinContext(org.apache.ignite.internal.processors.query.h2.opt.join.DistributedJoinContext) RangeStream(org.apache.ignite.internal.processors.query.h2.opt.join.RangeStream) IndexQueryContext(org.apache.ignite.internal.cache.query.index.sorted.inline.IndexQueryContext) QueryContext(org.apache.ignite.internal.processors.query.h2.opt.QueryContext) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings)

Aggregations

QueryContext (org.apache.ignite.internal.processors.query.h2.opt.QueryContext)2 ArrayList (java.util.ArrayList)1 IndexQueryContext (org.apache.ignite.internal.cache.query.index.sorted.inline.IndexQueryContext)1 DistributedJoinContext (org.apache.ignite.internal.processors.query.h2.opt.join.DistributedJoinContext)1 RangeStream (org.apache.ignite.internal.processors.query.h2.opt.join.RangeStream)1 SegmentKey (org.apache.ignite.internal.processors.query.h2.opt.join.SegmentKey)1 GridH2RowMessage (org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2RowMessage)1 GridH2RowRangeBounds (org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2RowRangeBounds)1 TraceSurroundings (org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings)1 Cursor (org.h2.index.Cursor)1