Search in sources :

Example 46 with TraceSurroundings

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

the class GridMapQueryExecutor method onDmlRequest.

/**
 * @param node Node.
 * @param req DML request.
 */
public void onDmlRequest(final ClusterNode node, final GridH2DmlRequest req) {
    int[] parts = req.queryPartitions();
    List<Integer> cacheIds = req.caches();
    long reqId = req.requestId();
    AffinityTopologyVersion topVer = req.topologyVersion();
    PartitionReservation reserved = null;
    MapNodeResults nodeResults = resultsForNode(node.id());
    // 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(SpanType.SQL_DML_QRY_EXEC_REQ, MTC.span()).addTag(SQL_QRY_TEXT, req::query));
    try {
        reserved = h2.partitionReservationManager().reservePartitions(cacheIds, topVer, parts, node.id(), reqId);
        if (reserved.failed()) {
            U.error(log, "Failed to reserve partitions for DML request. [localNodeId=" + ctx.localNodeId() + ", nodeId=" + node.id() + ", reqId=" + req.requestId() + ", cacheIds=" + cacheIds + ", topVer=" + topVer + ", parts=" + Arrays.toString(parts) + ']');
            sendUpdateResponse(node, reqId, null, "Failed to reserve partitions for DML request. " + reserved.error());
            return;
        }
        IndexingQueryFilter filter = h2.backupFilter(topVer, parts);
        GridQueryCancel cancel = nodeResults.putUpdate(reqId);
        SqlFieldsQuery fldsQry = new SqlFieldsQuery(req.query());
        if (req.parameters() != null)
            fldsQry.setArgs(req.parameters());
        fldsQry.setEnforceJoinOrder(req.isFlagSet(GridH2QueryRequest.FLAG_ENFORCE_JOIN_ORDER));
        fldsQry.setPageSize(req.pageSize());
        fldsQry.setLocal(true);
        if (req.timeout() > 0 || req.explicitTimeout())
            fldsQry.setTimeout(req.timeout(), TimeUnit.MILLISECONDS);
        boolean local = true;
        final boolean replicated = req.isFlagSet(GridH2QueryRequest.FLAG_REPLICATED);
        if (!replicated && !F.isEmpty(cacheIds) && CU.firstPartitioned(ctx.cache().context(), cacheIds).config().getQueryParallelism() > 1) {
            fldsQry.setDistributedJoins(true);
            local = false;
        }
        UpdateResult updRes = h2.executeUpdateOnDataNode(req.schemaName(), fldsQry, filter, cancel, local);
        GridCacheContext<?, ?> mainCctx = !F.isEmpty(cacheIds) ? ctx.cache().context().cacheContext(cacheIds.get(0)) : null;
        boolean evt = local && mainCctx != null && mainCctx.events().isRecordable(EVT_CACHE_QUERY_EXECUTED);
        if (evt) {
            ctx.event().record(new CacheQueryExecutedEvent<>(node, "SQL query executed.", EVT_CACHE_QUERY_EXECUTED, CacheQueryType.SQL.name(), mainCctx.name(), null, req.query(), null, null, req.parameters(), node.id(), null));
        }
        sendUpdateResponse(node, reqId, updRes, null);
    } catch (Exception e) {
        MTC.span().addTag(ERROR, e::getMessage);
        U.error(log, "Error processing dml request. [localNodeId=" + ctx.localNodeId() + ", nodeId=" + node.id() + ", req=" + req + ']', e);
        sendUpdateResponse(node, reqId, null, e.getMessage());
    } finally {
        if (reserved != null)
            reserved.release();
        nodeResults.removeUpdate(reqId);
        if (trace != null)
            trace.close();
    }
}
Also used : AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IndexingQueryFilter(org.apache.ignite.spi.indexing.IndexingQueryFilter) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) QueryRetryException(org.apache.ignite.cache.query.QueryRetryException) GridH2RetryException(org.apache.ignite.internal.processors.query.h2.opt.GridH2RetryException) SQLException(java.sql.SQLException) CacheException(javax.cache.CacheException) GridQueryCancel(org.apache.ignite.internal.processors.query.GridQueryCancel) UpdateResult(org.apache.ignite.internal.processors.query.h2.UpdateResult)

Example 47 with TraceSurroundings

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

the class GridMapQueryExecutor method onNextPageRequest.

/**
 * @param node Node.
 * @param req Request.
 */
public void onNextPageRequest(final ClusterNode node, final GridQueryNextPageRequest req) {
    try (TraceSurroundings ignored = MTC.support(ctx.tracing().create(SQL_NEXT_PAGE_REQ, MTC.span()))) {
        long reqId = req.queryRequestId();
        final MapNodeResults nodeRess = qryRess.get(node.id());
        if (nodeRess == null) {
            sendError(node, reqId, new CacheException("No node result found for request: " + req));
            return;
        } else if (nodeRess.cancelled(reqId)) {
            sendQueryCancel(node, reqId);
            return;
        }
        final MapQueryResults qryResults = nodeRess.get(reqId, req.segmentId());
        if (qryResults == null)
            sendError(node, reqId, new CacheException("No query result found for request: " + req));
        else if (qryResults.cancelled())
            sendQueryCancel(node, reqId);
        else {
            try {
                MapQueryResult res = qryResults.result(req.query());
                assert res != null;
                try {
                    // Session isn't set for lazy=false queries.
                    // Also session == null when result already closed.
                    res.lock();
                    res.lockTables();
                    res.checkTablesVersions();
                    Boolean dataPageScanEnabled = isDataPageScanEnabled(req.getFlags());
                    GridQueryNextPageResponse msg = prepareNextPage(nodeRess, node, qryResults, req.query(), req.segmentId(), req.pageSize(), dataPageScanEnabled);
                    if (msg != null)
                        sendNextPage(node, msg);
                } finally {
                    try {
                        res.unlockTables();
                    } finally {
                        res.unlock();
                    }
                }
            } catch (Exception e) {
                QueryRetryException retryEx = X.cause(e, QueryRetryException.class);
                if (retryEx != null)
                    sendError(node, reqId, retryEx);
                else {
                    SQLException sqlEx = X.cause(e, SQLException.class);
                    if (sqlEx != null && sqlEx.getErrorCode() == ErrorCode.STATEMENT_WAS_CANCELED)
                        sendQueryCancel(node, reqId);
                    else
                        sendError(node, reqId, e);
                }
                qryResults.cancel();
            }
        }
    }
}
Also used : QueryRetryException(org.apache.ignite.cache.query.QueryRetryException) CacheException(javax.cache.CacheException) SQLException(java.sql.SQLException) GridQueryNextPageResponse(org.apache.ignite.internal.processors.query.h2.twostep.messages.GridQueryNextPageResponse) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) QueryRetryException(org.apache.ignite.cache.query.QueryRetryException) GridH2RetryException(org.apache.ignite.internal.processors.query.h2.opt.GridH2RetryException) SQLException(java.sql.SQLException) CacheException(javax.cache.CacheException)

Example 48 with TraceSurroundings

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

the class GridMapQueryExecutor method onCancel.

/**
 * @param node Node.
 * @param msg Message.
 */
public void onCancel(ClusterNode node, GridQueryCancelRequest msg) {
    try (TraceSurroundings ignored = MTC.support(ctx.tracing().create(SQL_QRY_CANCEL_REQ, MTC.span()))) {
        long qryReqId = msg.queryRequestId();
        MapNodeResults nodeRess = resultsForNode(node.id());
        boolean clear = qryCtxRegistry.clearShared(node.id(), qryReqId);
        if (!clear) {
            nodeRess.onCancel(qryReqId);
            qryCtxRegistry.clearShared(node.id(), qryReqId);
        }
        nodeRess.cancelRequest(qryReqId);
    }
}
Also used : TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings)

Example 49 with TraceSurroundings

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

the class GridMapQueryExecutor method onQueryRequest.

/**
 * @param node Node.
 * @param req Query request.
 * @throws IgniteCheckedException On error.
 */
public void onQueryRequest(final ClusterNode node, final GridH2QueryRequest req) throws IgniteCheckedException {
    int[] qryParts = req.queryPartitions();
    final Map<UUID, int[]> partsMap = req.partitions();
    final int[] parts = qryParts == null ? (partsMap == null ? null : partsMap.get(ctx.localNodeId())) : qryParts;
    boolean distributedJoins = req.isFlagSet(GridH2QueryRequest.FLAG_DISTRIBUTED_JOINS);
    boolean enforceJoinOrder = req.isFlagSet(GridH2QueryRequest.FLAG_ENFORCE_JOIN_ORDER);
    boolean explain = req.isFlagSet(GridH2QueryRequest.FLAG_EXPLAIN);
    boolean replicated = req.isFlagSet(GridH2QueryRequest.FLAG_REPLICATED);
    final boolean lazy = req.isFlagSet(GridH2QueryRequest.FLAG_LAZY);
    boolean treatReplicatedAsPartitioned = req.isFlagSet(GridH2QueryRequest.FLAG_REPLICATED_AS_PARTITIONED);
    try {
        Boolean dataPageScanEnabled = req.isDataPageScanEnabled();
        final List<Integer> cacheIds = req.caches();
        final int parallelism = explain || replicated || F.isEmpty(cacheIds) ? 1 : CU.firstPartitioned(ctx.cache().context(), cacheIds).config().getQueryParallelism();
        BitSet segments = new BitSet(parallelism);
        if (parts != null) {
            for (int i = 0; i < parts.length; i++) segments.set(calculateSegment(parallelism, parts[i]));
        } else
            segments.set(0, parallelism);
        final Object[] params = req.parameters();
        final int timeout = req.timeout() > 0 || req.explicitTimeout() ? req.timeout() : (int) h2.distributedConfiguration().defaultQueryTimeout();
        int firstSegment = segments.nextSetBit(0);
        int segment = firstSegment;
        while ((segment = segments.nextSetBit(segment + 1)) != -1) {
            assert !F.isEmpty(cacheIds);
            final int segment0 = segment;
            Span span = MTC.span();
            ctx.closure().runLocal(() -> {
                try (TraceSurroundings ignored = MTC.supportContinual(span)) {
                    onQueryRequest0(node, req.queryId(), req.requestId(), segment0, req.schemaName(), req.queries(), cacheIds, req.topologyVersion(), partsMap, parts, req.pageSize(), distributedJoins, enforceJoinOrder, false, timeout, params, lazy, req.mvccSnapshot(), dataPageScanEnabled, treatReplicatedAsPartitioned);
                } catch (Throwable e) {
                    sendError(node, req.requestId(), e);
                }
            }, QUERY_POOL);
        }
        onQueryRequest0(node, req.queryId(), req.requestId(), firstSegment, req.schemaName(), req.queries(), cacheIds, req.topologyVersion(), partsMap, parts, req.pageSize(), distributedJoins, enforceJoinOrder, replicated, timeout, params, lazy, req.mvccSnapshot(), dataPageScanEnabled, treatReplicatedAsPartitioned);
    } catch (Throwable e) {
        sendError(node, req.requestId(), e);
    }
}
Also used : BitSet(java.util.BitSet) Span(org.apache.ignite.internal.processors.tracing.Span) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) UUID(java.util.UUID)

Aggregations

TraceSurroundings (org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings)49 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)18 QueryCancelledException (org.apache.ignite.cache.query.QueryCancelledException)11 SQLException (java.sql.SQLException)10 IgniteException (org.apache.ignite.IgniteException)10 ArrayList (java.util.ArrayList)9 CacheException (javax.cache.CacheException)8 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)7 List (java.util.List)6 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)6 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)6 Collections.singletonList (java.util.Collections.singletonList)5 ClusterTopologyServerNotFoundException (org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException)5 BatchUpdateException (java.sql.BatchUpdateException)4 CacheServerNotFoundException (org.apache.ignite.cache.CacheServerNotFoundException)4 QueryRetryException (org.apache.ignite.cache.query.QueryRetryException)4 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)4 IgniteClusterReadOnlyException (org.apache.ignite.internal.processors.cache.distributed.dht.IgniteClusterReadOnlyException)4 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)4 QueryContext (org.apache.ignite.internal.processors.query.h2.opt.QueryContext)4