Search in sources :

Example 6 with Span

use of org.apache.ignite.internal.processors.tracing.Span 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 7 with Span

use of org.apache.ignite.internal.processors.tracing.Span in project ignite by apache.

the class IgniteH2Indexing method querySqlFields.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "StringEquality" })
@Override
public List<FieldsQueryCursor<List<?>>> querySqlFields(String schemaName, SqlFieldsQuery qry, @Nullable SqlClientContext cliCtx, boolean keepBinary, boolean failOnMultipleStmts, GridQueryCancel cancel) {
    try {
        List<FieldsQueryCursor<List<?>>> res = new ArrayList<>(1);
        SqlFieldsQuery remainingQry = qry;
        while (remainingQry != null) {
            Span qrySpan = ctx.tracing().create(SQL_QRY, MTC.span()).addTag(SQL_SCHEMA, () -> schemaName);
            try (TraceSurroundings ignored = MTC.supportContinual(qrySpan)) {
                // Parse.
                QueryParserResult parseRes = parser.parse(schemaName, remainingQry, !failOnMultipleStmts);
                qrySpan.addTag(SQL_QRY_TEXT, () -> parseRes.queryDescriptor().sql());
                remainingQry = parseRes.remainingQuery();
                // Get next command.
                QueryDescriptor newQryDesc = parseRes.queryDescriptor();
                QueryParameters newQryParams = parseRes.queryParameters();
                // since they pass parameters differently.
                if (!newQryDesc.batched()) {
                    int qryParamsCnt = F.isEmpty(newQryParams.arguments()) ? 0 : newQryParams.arguments().length;
                    if (qryParamsCnt < parseRes.parametersCount())
                        throw new IgniteSQLException("Invalid number of query parameters [expected=" + parseRes.parametersCount() + ", actual=" + qryParamsCnt + ']');
                }
                // Check if cluster state is valid.
                checkClusterState(parseRes);
                // Execute.
                if (parseRes.isCommand()) {
                    QueryParserResultCommand cmd = parseRes.command();
                    assert cmd != null;
                    FieldsQueryCursor<List<?>> cmdRes = executeCommand(newQryDesc, newQryParams, cliCtx, cmd);
                    res.add(cmdRes);
                } else if (parseRes.isDml()) {
                    QueryParserResultDml dml = parseRes.dml();
                    assert dml != null;
                    List<? extends FieldsQueryCursor<List<?>>> dmlRes = executeDml(newQryDesc, newQryParams, dml, cancel);
                    res.addAll(dmlRes);
                } else {
                    assert parseRes.isSelect();
                    QueryParserResultSelect select = parseRes.select();
                    assert select != null;
                    List<? extends FieldsQueryCursor<List<?>>> qryRes = executeSelect(newQryDesc, newQryParams, select, keepBinary, cancel);
                    res.addAll(qryRes);
                }
            } catch (Throwable th) {
                qrySpan.addTag(ERROR, th::getMessage).end();
                throw th;
            }
        }
        return res;
    } catch (RuntimeException | Error e) {
        GridNearTxLocal tx = ctx.cache().context().tm().tx();
        if (tx != null && tx.mvccSnapshot() != null && (!(e instanceof IgniteSQLException) || /* Parsing errors should not rollback Tx. */
        ((IgniteSQLException) e).sqlState() != SqlStateCode.PARSING_EXCEPTION))
            tx.setRollbackOnly();
        throw e;
    }
}
Also used : FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) ArrayList(java.util.ArrayList) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) Span(org.apache.ignite.internal.processors.tracing.Span) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with Span

use of org.apache.ignite.internal.processors.tracing.Span in project ignite by apache.

the class RegisteredQueryCursor method close.

/**
 * {@inheritDoc}
 */
@Override
public void close() {
    Span span = MTC.span();
    try (TraceSurroundings ignored = MTC.support(tracing.create(SQL_CURSOR_CLOSE, span != NoopSpan.INSTANCE ? span : qrySpan))) {
        super.close();
        unregisterQuery();
    } catch (Throwable th) {
        qrySpan.addTag(ERROR, th::getMessage);
        throw th;
    }
}
Also used : Span(org.apache.ignite.internal.processors.tracing.Span) NoopSpan(org.apache.ignite.internal.processors.tracing.NoopSpan) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings)

Example 9 with Span

use of org.apache.ignite.internal.processors.tracing.Span in project ignite by apache.

the class GridTracingManager method create.

/**
 * {@inheritDoc}
 */
@Override
public Span create(@NotNull SpanType spanType, @Nullable byte[] serializedParentSpan) {
    // Optimization for noop spi.
    if (noop)
        return NoopSpan.INSTANCE;
    // Optimization for zero sampling rate == 0.
    if ((serializedParentSpan.length == 0 || serializedParentSpan == null) && tracingConfiguration.get(new TracingConfigurationCoordinates.Builder(spanType.scope()).build()).samplingRate() == SAMPLING_RATE_NEVER)
        return NoopSpan.INSTANCE;
    // 1 byte: special flags;
    // 1 bytes: spi type;
    // 2 bytes: major protocol version;
    // 2 bytes: minor protocol version;
    // 4 bytes: spi specific serialized span length;
    // n bytes: spi specific serialized span body;
    // 4 bytes: span type
    // 4 bytes included scopes size;
    // 2 * included scopes size: included scopes items one by one;
    Span span;
    try {
        if (serializedParentSpan == null || serializedParentSpan.length == 0)
            return create(spanType, NoopSpan.INSTANCE);
        // propagate serializedSpan as DeferredSpan.
        if (serializedParentSpan[SPI_TYPE_OFF] != getSpi().type())
            return new DeferredSpan(serializedParentSpan);
        // propagate serializedSpan as DeferredSpan.
        if (serializedParentSpan[MAJOR_PROTOCOL_VERSION_OFF] != MAJOR_PROTOCOL_VERSION)
            return new DeferredSpan(serializedParentSpan);
        // Deserialize and check minor protocol version.
        // within the scope of the same major protocol version, protocol should be backwards compatible
        byte minProtoVer = serializedParentSpan[MINOR_PROTOCOL_VERSION_OFF];
        // Deserialize spi specific span size.
        int spiSpecificSpanSize = bytesToInt(Arrays.copyOfRange(serializedParentSpan, SPI_SPECIFIC_SERIALIZED_SPAN_BYTES_LENGTH_OFF, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF), 0);
        SpanType parentSpanType = null;
        Set<Scope> includedScopes = new HashSet<>();
        // Fall through.
        switch(minProtoVer) {
            case 0:
                {
                    // Deserialize parent span type.
                    parentSpanType = SpanType.fromIndex(bytesToInt(Arrays.copyOfRange(serializedParentSpan, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + spiSpecificSpanSize, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + PARENT_SPAN_TYPE_BYTES_LENGTH + spiSpecificSpanSize), 0));
                    // Deserialize included scopes size.
                    int includedScopesSize = bytesToInt(Arrays.copyOfRange(serializedParentSpan, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + PARENT_SPAN_TYPE_BYTES_LENGTH + spiSpecificSpanSize, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + PARENT_SPAN_TYPE_BYTES_LENGTH + INCLUDED_SCOPES_SIZE_BYTE_LENGTH + spiSpecificSpanSize), 0);
                    // Deserialize included scopes one by one.
                    for (int i = 0; i < includedScopesSize; i++) {
                        includedScopes.add(Scope.fromIndex(bytesToShort(Arrays.copyOfRange(serializedParentSpan, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + PARENT_SPAN_TYPE_BYTES_LENGTH + INCLUDED_SCOPES_SIZE_BYTE_LENGTH + spiSpecificSpanSize + i * SCOPE_INDEX_BYTE_LENGTH, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + PARENT_SPAN_TYPE_BYTES_LENGTH + INCLUDED_SCOPES_SIZE_BYTE_LENGTH + SCOPE_INDEX_BYTE_LENGTH + spiSpecificSpanSize + i * SCOPE_INDEX_BYTE_LENGTH), 0)));
                    }
                }
        }
        assert parentSpanType != null;
        // If there's is parent span and parent span supports given scope then...
        if (parentSpanType.scope() == spanType.scope() || includedScopes.contains(spanType.scope())) {
            // create new span as child span for parent span, using parents span included scopes.
            Set<Scope> mergedIncludedScopes = new HashSet<>(includedScopes);
            mergedIncludedScopes.add(parentSpanType.scope());
            mergedIncludedScopes.remove(spanType.scope());
            span = new SpanImpl(getSpi().create(spanType.spanName(), Arrays.copyOfRange(serializedParentSpan, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF, SPI_SPECIFIC_SERIALIZED_SPAN_BODY_OFF + spiSpecificSpanSize)), spanType, mergedIncludedScopes);
        } else {
            // do nothing;
            return new DeferredSpan(serializedParentSpan);
        // "suppress" parent span for a while, create new span as separate one.
        // return spi.create(trace, null, includedScopes);
        }
    } catch (Exception e) {
        LT.warn(log, "Failed to create span from serialized value " + "[serializedValue=" + Arrays.toString(serializedParentSpan) + "]");
        span = NoopSpan.INSTANCE;
    }
    return enrichWithLocalNodeParameters(span);
}
Also used : DeferredSpan(org.apache.ignite.internal.processors.tracing.DeferredSpan) SpanType(org.apache.ignite.internal.processors.tracing.SpanType) Scope(org.apache.ignite.spi.tracing.Scope) SpanImpl(org.apache.ignite.internal.processors.tracing.SpanImpl) TracingConfigurationCoordinates(org.apache.ignite.spi.tracing.TracingConfigurationCoordinates) Span(org.apache.ignite.internal.processors.tracing.Span) NoopSpan(org.apache.ignite.internal.processors.tracing.NoopSpan) SpiSpecificSpan(org.apache.ignite.spi.tracing.SpiSpecificSpan) DeferredSpan(org.apache.ignite.internal.processors.tracing.DeferredSpan) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) HashSet(java.util.HashSet)

Example 10 with Span

use of org.apache.ignite.internal.processors.tracing.Span in project ignite by apache.

the class JmhTracingContextBenchmark method spanThreadLocal.

/**
 */
@Benchmark
public void spanThreadLocal() {
    for (int i = 0; i < 100; i++) {
        Span span = tracing.create(SpanType.TX);
        MTC.supportInitial(span);
        MTC.span().addTag("isolation", () -> "isolation");
        MTC.span().addTag("concurrency", () -> "concurrency");
        MTC.span().addTag("timeout", () -> "timeout");
        MTC.span().addTag("label", () -> "label");
    }
}
Also used : Span(org.apache.ignite.internal.processors.tracing.Span) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Aggregations

Span (org.apache.ignite.internal.processors.tracing.Span)14 TraceSurroundings (org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 CustomMessageWrapper (org.apache.ignite.internal.managers.discovery.CustomMessageWrapper)2 DiscoveryServerOnlyCustomMessage (org.apache.ignite.internal.managers.discovery.DiscoveryServerOnlyCustomMessage)2 GridNearTxLocal (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)2 NoopSpan (org.apache.ignite.internal.processors.tracing.NoopSpan)2 TcpDiscoveryCustomEventMessage (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryCustomEventMessage)2 TcpDiscoveryServerOnlyCustomEventMessage (org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryServerOnlyCustomEventMessage)2 Benchmark (org.openjdk.jmh.annotations.Benchmark)2 Channel (java.nio.channels.Channel)1 BitSet (java.util.BitSet)1 Collection (java.util.Collection)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 UUID (java.util.UUID)1