Search in sources :

Example 16 with TraceSurroundings

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

the class GridNearOptimisticTxPrepareFutureAdapter method onNearTxLocalTimeout.

/**
 * {@inheritDoc}
 */
@Override
public final void onNearTxLocalTimeout() {
    try (TraceSurroundings ignored = MTC.support(span)) {
        if (keyLockFut != null && !keyLockFut.isDone()) {
            ERR_UPD.compareAndSet(this, null, new IgniteTxTimeoutCheckedException("Failed to acquire lock within provided timeout for transaction [timeout=" + tx.timeout() + ", tx=" + tx + ']'));
            keyLockFut.onDone();
        }
    }
}
Also used : IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings)

Example 17 with TraceSurroundings

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

the class AbstractReducer method pollNextIterator.

/**
 * @param queue Queue to poll.
 * @param iter Current iterator.
 * @return The same or new iterator.
 */
protected final Iterator<Value[]> pollNextIterator(Pollable<ReduceResultPage> queue, Iterator<Value[]> iter) {
    if (!iter.hasNext()) {
        try (TraceSurroundings ignored = MTC.support(ctx.tracing().create(SQL_PAGE_FETCH, MTC.span()))) {
            ReduceResultPage page = takeNextPage(queue);
            if (!page.isLast())
                // Failed will throw an exception here.
                page.fetchNextPage();
            iter = page.rows();
            MTC.span().addTag(SQL_PAGE_ROWS, () -> Integer.toString(page.rowsInPage()));
            // The received iterator must be empty in the dummy last page or on failure.
            assert iter.hasNext() || page.isDummyLast() || page.isFail();
        }
    }
    return iter;
}
Also used : TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings)

Example 18 with TraceSurroundings

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

the class GridMapQueryExecutor method onQueryRequest0.

/**
 * @param node Node authored request.
 * @param qryId Query ID.
 * @param reqId Request ID.
 * @param segmentId index segment ID.
 * @param schemaName Schema name.
 * @param qrys Queries to execute.
 * @param cacheIds Caches which will be affected by these queries.
 * @param topVer Topology version.
 * @param partsMap Partitions map for unstable topology.
 * @param parts Explicit partitions for current node.
 * @param pageSize Page size.
 * @param distributedJoins Query distributed join mode.
 * @param enforceJoinOrder Enforce join order H2 flag.
 * @param replicated Replicated only flag.
 * @param timeout Query timeout.
 * @param params Query parameters.
 * @param lazy Streaming flag.
 * @param mvccSnapshot MVCC snapshot.
 * @param dataPageScanEnabled If data page scan is enabled.
 */
private void onQueryRequest0(final ClusterNode node, final long qryId, final long reqId, final int segmentId, final String schemaName, final Collection<GridCacheSqlQuery> qrys, final List<Integer> cacheIds, final AffinityTopologyVersion topVer, final Map<UUID, int[]> partsMap, final int[] parts, final int pageSize, final boolean distributedJoins, final boolean enforceJoinOrder, final boolean replicated, final int timeout, final Object[] params, boolean lazy, @Nullable final MvccSnapshot mvccSnapshot, Boolean dataPageScanEnabled, boolean treatReplicatedAsPartitioned) {
    boolean performanceStatsEnabled = ctx.performanceStatistics().enabled();
    if (performanceStatsEnabled)
        IoStatisticsQueryHelper.startGatheringQueryStatistics();
    // Prepare to run queries.
    GridCacheContext<?, ?> mainCctx = mainCacheContext(cacheIds);
    MapNodeResults nodeRess = resultsForNode(node.id());
    MapQueryResults qryResults = null;
    PartitionReservation reserved = null;
    QueryContext qctx = null;
    // 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_QRY_EXEC_REQ, MTC.span()).addTag(SQL_QRY_TEXT, () -> qrys.stream().map(GridCacheSqlQuery::query).collect(Collectors.joining("; "))));
    try {
        if (topVer != null) {
            // Reserve primary for topology version or explicit partitions.
            reserved = h2.partitionReservationManager().reservePartitions(cacheIds, topVer, parts, node.id(), reqId);
            if (reserved.failed()) {
                sendRetry(node, reqId, segmentId, reserved.error());
                return;
            }
        }
        // Prepare query context.
        DistributedJoinContext distributedJoinCtx = null;
        if (distributedJoins && !replicated) {
            distributedJoinCtx = new DistributedJoinContext(topVer, partsMap, node.id(), reqId, segmentId, pageSize);
        }
        qctx = new QueryContext(segmentId, h2.backupFilter(topVer, parts, treatReplicatedAsPartitioned), distributedJoinCtx, mvccSnapshot, reserved, true);
        qryResults = new MapQueryResults(h2, reqId, qrys.size(), mainCctx, lazy, qctx);
        // qctx is set, we have to release reservations inside of it.
        reserved = null;
        if (distributedJoinCtx != null)
            qryCtxRegistry.setShared(node.id(), reqId, qctx);
        if (nodeRess.put(reqId, segmentId, qryResults) != null)
            throw new IllegalStateException();
        if (nodeRess.cancelled(reqId)) {
            qryCtxRegistry.clearShared(node.id(), reqId);
            nodeRess.cancelRequest(reqId);
            throw new QueryCancelledException();
        }
        // Run queries.
        int qryIdx = 0;
        boolean evt = mainCctx != null && mainCctx.events().isRecordable(EVT_CACHE_QUERY_EXECUTED);
        for (GridCacheSqlQuery qry : qrys) {
            H2PooledConnection conn = h2.connections().connection(schemaName);
            H2Utils.setupConnection(conn, qctx, distributedJoins, enforceJoinOrder, lazy);
            MapQueryResult res = new MapQueryResult(h2, mainCctx, node.id(), qry, params, conn, log);
            qryResults.addResult(qryIdx, res);
            try {
                res.lock();
                // Ensure we are on the target node for this replicated query.
                if (qry.node() == null || (segmentId == 0 && qry.node().equals(ctx.localNodeId()))) {
                    String sql = qry.query();
                    Collection<Object> params0 = F.asList(qry.parameters(params));
                    PreparedStatement stmt = conn.prepareStatement(sql, H2StatementCache.queryFlags(distributedJoins, enforceJoinOrder));
                    H2Utils.bindParameters(stmt, params0);
                    MapH2QueryInfo qryInfo = new MapH2QueryInfo(stmt, qry.query(), node.id(), qryId, reqId, segmentId);
                    ResultSet rs = h2.executeSqlQueryWithTimer(stmt, conn, sql, timeout, qryResults.queryCancel(qryIdx), dataPageScanEnabled, qryInfo);
                    if (evt) {
                        ctx.event().record(new CacheQueryExecutedEvent<>(node, "SQL query executed.", EVT_CACHE_QUERY_EXECUTED, CacheQueryType.SQL.name(), mainCctx.name(), null, qry.query(), null, null, params, node.id(), null));
                    }
                    assert rs instanceof JdbcResultSet : rs.getClass();
                    if (qryResults.cancelled()) {
                        rs.close();
                        throw new QueryCancelledException();
                    }
                    res.openResult(rs, qryInfo);
                    final GridQueryNextPageResponse msg = prepareNextPage(nodeRess, node, qryResults, qryIdx, segmentId, pageSize, dataPageScanEnabled);
                    if (msg != null)
                        sendNextPage(node, msg);
                } else {
                    assert !qry.isPartitioned();
                    qryResults.closeResult(qryIdx);
                }
                qryIdx++;
            } finally {
                try {
                    res.unlockTables();
                } finally {
                    res.unlock();
                }
            }
        }
        if (!lazy)
            qryResults.releaseQueryContext();
    } catch (Throwable e) {
        if (qryResults != null) {
            nodeRess.remove(reqId, segmentId, qryResults);
            qryResults.close();
            // If a query is cancelled before execution is started partitions have to be released.
            if (!lazy || !qryResults.isAllClosed())
                qryResults.releaseQueryContext();
        } else
            releaseReservations(qctx);
        if (e instanceof QueryCancelledException)
            sendError(node, reqId, e);
        else {
            SQLException sqlEx = X.cause(e, SQLException.class);
            if (sqlEx != null && sqlEx.getErrorCode() == ErrorCode.STATEMENT_WAS_CANCELED)
                sendQueryCancel(node, reqId);
            else {
                GridH2RetryException retryErr = X.cause(e, GridH2RetryException.class);
                if (retryErr != null) {
                    final String retryCause = String.format("Failed to execute non-collocated query (will retry) [localNodeId=%s, rmtNodeId=%s, reqId=%s, " + "errMsg=%s]", ctx.localNodeId(), node.id(), reqId, retryErr.getMessage());
                    sendRetry(node, reqId, segmentId, retryCause);
                } else {
                    QueryRetryException qryRetryErr = X.cause(e, QueryRetryException.class);
                    if (qryRetryErr != null)
                        sendError(node, reqId, qryRetryErr);
                    else {
                        if (e instanceof Error) {
                            U.error(log, "Failed to execute local query.", e);
                            throw (Error) e;
                        }
                        U.warn(log, "Failed to execute local query.", e);
                        sendError(node, reqId, e);
                    }
                }
            }
        }
    } finally {
        if (reserved != null)
            reserved.release();
        if (trace != null)
            trace.close();
        if (performanceStatsEnabled) {
            IoStatisticsHolder stat = IoStatisticsQueryHelper.finishGatheringQueryStatistics();
            if (stat.logicalReads() > 0 || stat.physicalReads() > 0) {
                ctx.performanceStatistics().queryReads(GridCacheQueryType.SQL_FIELDS, node.id(), reqId, stat.logicalReads(), stat.physicalReads());
            }
        }
    }
}
Also used : QueryRetryException(org.apache.ignite.cache.query.QueryRetryException) SQLException(java.sql.SQLException) GridQueryNextPageResponse(org.apache.ignite.internal.processors.query.h2.twostep.messages.GridQueryNextPageResponse) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) DistributedJoinContext(org.apache.ignite.internal.processors.query.h2.opt.join.DistributedJoinContext) ResultSet(java.sql.ResultSet) JdbcResultSet(org.h2.jdbc.JdbcResultSet) GridCacheSqlQuery(org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery) JdbcResultSet(org.h2.jdbc.JdbcResultSet) H2PooledConnection(org.apache.ignite.internal.processors.query.h2.H2PooledConnection) MapH2QueryInfo(org.apache.ignite.internal.processors.query.h2.MapH2QueryInfo) GridH2RetryException(org.apache.ignite.internal.processors.query.h2.opt.GridH2RetryException) PreparedStatement(java.sql.PreparedStatement) QueryContext(org.apache.ignite.internal.processors.query.h2.opt.QueryContext) IoStatisticsHolder(org.apache.ignite.internal.metric.IoStatisticsHolder) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException)

Example 19 with TraceSurroundings

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

the class GridMapQueryExecutor method prepareNextPage.

/**
 * @param nodeRess Results.
 * @param node Node.
 * @param qr Query results.
 * @param qry Query.
 * @param segmentId Index segment ID.
 * @param pageSize Page size.
 * @param dataPageScanEnabled If data page scan is enabled.
 * @return Next page.
 * @throws IgniteCheckedException If failed.
 */
private GridQueryNextPageResponse prepareNextPage(MapNodeResults nodeRess, ClusterNode node, MapQueryResults qr, int qry, int segmentId, int pageSize, Boolean dataPageScanEnabled) throws IgniteCheckedException {
    try (TraceSurroundings ignored = MTC.support(ctx.tracing().create(SQL_PAGE_PREPARE, MTC.span()))) {
        MapQueryResult res = qr.result(qry);
        assert res != null;
        if (res.closed())
            return null;
        int page = res.page();
        List<Value[]> rows = new ArrayList<>(Math.min(64, pageSize));
        boolean last = res.fetchNextPage(rows, pageSize, dataPageScanEnabled);
        if (last) {
            qr.closeResult(qry);
            if (qr.isAllClosed()) {
                nodeRess.remove(qr.queryRequestId(), segmentId, qr);
                // Clear context, release reservations
                if (qr.isLazy())
                    qr.releaseQueryContext();
            }
        }
        boolean loc = node.isLocal();
        GridQueryNextPageResponse msg = new GridQueryNextPageResponse(qr.queryRequestId(), segmentId, qry, page, page == 0 ? res.rowCount() : -1, res.columnCount(), loc ? null : toMessages(rows, new ArrayList<>(res.columnCount()), res.columnCount()), loc ? rows : null, last);
        MTC.span().addTag(SQL_PAGE_ROWS, () -> String.valueOf(rows.size()));
        return msg;
    }
}
Also used : GridQueryNextPageResponse(org.apache.ignite.internal.processors.query.h2.twostep.messages.GridQueryNextPageResponse) ArrayList(java.util.ArrayList) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings)

Example 20 with TraceSurroundings

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

the class PartitionReservationManager method reservePartitions.

/**
 * @param cacheIds Cache IDs.
 * @param reqTopVer Topology version from request.
 * @param explicitParts Explicit partitions list.
 * @param nodeId Node ID.
 * @param reqId Request ID.
 * @return String which is null in case of success or with causeMessage if failed
 * @throws IgniteCheckedException If failed.
 */
public PartitionReservation reservePartitions(@Nullable List<Integer> cacheIds, AffinityTopologyVersion reqTopVer, final int[] explicitParts, UUID nodeId, long reqId) throws IgniteCheckedException {
    try (TraceSurroundings ignored = MTC.support(ctx.tracing().create(SQL_PARTITIONS_RESERVE, MTC.span()))) {
        assert reqTopVer != null;
        AffinityTopologyVersion topVer = ctx.cache().context().exchange().lastAffinityChangedTopologyVersion(reqTopVer);
        if (F.isEmpty(cacheIds))
            return new PartitionReservation(Collections.emptyList());
        Collection<Integer> partIds;
        if (explicitParts == null)
            partIds = null;
        else if (explicitParts.length == 0)
            partIds = Collections.emptyList();
        else {
            partIds = new ArrayList<>(explicitParts.length);
            for (int explicitPart : explicitParts) partIds.add(explicitPart);
        }
        List<GridReservable> reserved = new ArrayList<>();
        for (int i = 0; i < cacheIds.size(); i++) {
            GridCacheContext<?, ?> cctx = ctx.cache().context().cacheContext(cacheIds.get(i));
            // Cache was not found, probably was not deployed yet.
            if (cctx == null) {
                return new PartitionReservation(reserved, String.format("Failed to reserve partitions for query (cache is not " + "found on local node) [localNodeId=%s, rmtNodeId=%s, reqId=%s, affTopVer=%s, cacheId=%s]", ctx.localNodeId(), nodeId, reqId, topVer, cacheIds.get(i)));
            }
            if (cctx.isLocal() || !cctx.rebalanceEnabled())
                continue;
            // For replicated cache topology version does not make sense.
            final PartitionReservationKey grpKey = new PartitionReservationKey(cctx.name(), cctx.isReplicated() ? null : topVer);
            GridReservable r = reservations.get(grpKey);
            if (explicitParts == null && r != null) {
                // Try to reserve group partition if any and no explicits.
                if (r != REPLICATED_RESERVABLE) {
                    if (!r.reserve())
                        return new PartitionReservation(reserved, String.format("Failed to reserve partitions for query (group " + "reservation failed) [localNodeId=%s, rmtNodeId=%s, reqId=%s, affTopVer=%s, cacheId=%s, " + "cacheName=%s]", ctx.localNodeId(), nodeId, reqId, topVer, cacheIds.get(i), cctx.name()));
                    reserved.add(r);
                    MTC.span().addLog(() -> "Cache partitions were reserved " + r);
                }
            } else {
                // Try to reserve partitions one by one.
                int partsCnt = cctx.affinity().partitions();
                if (cctx.isReplicated()) {
                    // Check all the partitions are in owning state for replicated cache.
                    if (r == null) {
                        // Check only once.
                        for (int p = 0; p < partsCnt; p++) {
                            GridDhtLocalPartition part = partition(cctx, p);
                            // We don't need to reserve partitions because they will not be evicted in replicated caches.
                            GridDhtPartitionState partState = part != null ? part.state() : null;
                            if (partState != OWNING)
                                return new PartitionReservation(reserved, String.format("Failed to reserve partitions for " + "query (partition of REPLICATED cache is not in OWNING state) [" + "localNodeId=%s, rmtNodeId=%s, reqId=%s, affTopVer=%s, cacheId=%s, " + "cacheName=%s, part=%s, partFound=%s, partState=%s]", ctx.localNodeId(), nodeId, reqId, topVer, cacheIds.get(i), cctx.name(), p, (part != null), partState));
                        }
                        // Mark that we checked this replicated cache.
                        reservations.putIfAbsent(grpKey, REPLICATED_RESERVABLE);
                        MTC.span().addLog(() -> "Cache partitions were reserved [cache=" + cctx.name() + ", partitions=[0.." + partsCnt + ']');
                    }
                } else {
                    // Reserve primary partitions for partitioned cache (if no explicit given).
                    if (explicitParts == null)
                        partIds = cctx.affinity().primaryPartitions(ctx.localNodeId(), topVer);
                    int reservedCnt = 0;
                    for (int partId : partIds) {
                        GridDhtLocalPartition part = partition(cctx, partId);
                        GridDhtPartitionState partState = part != null ? part.state() : null;
                        if (partState != OWNING) {
                            if (partState == LOST)
                                failQueryOnLostData(cctx, part);
                            else {
                                return new PartitionReservation(reserved, String.format("Failed to reserve partitions " + "for query (partition of PARTITIONED cache is not found or not in OWNING " + "state) [localNodeId=%s, rmtNodeId=%s, reqId=%s, affTopVer=%s, cacheId=%s, " + "cacheName=%s, part=%s, partFound=%s, partState=%s]", ctx.localNodeId(), nodeId, reqId, topVer, cacheIds.get(i), cctx.name(), partId, (part != null), partState));
                            }
                        }
                        if (!part.reserve()) {
                            return new PartitionReservation(reserved, String.format("Failed to reserve partitions for query " + "(partition of PARTITIONED cache cannot be reserved) [" + "localNodeId=%s, rmtNodeId=%s, reqId=%s, affTopVer=%s, cacheId=%s, " + "cacheName=%s, part=%s, partFound=%s, partState=%s]", ctx.localNodeId(), nodeId, reqId, topVer, cacheIds.get(i), cctx.name(), partId, true, partState));
                        }
                        reserved.add(part);
                        reservedCnt++;
                        // Double check that we are still in owning state and partition contents are not cleared.
                        partState = part.state();
                        if (partState != OWNING) {
                            if (partState == LOST)
                                failQueryOnLostData(cctx, part);
                            else {
                                return new PartitionReservation(reserved, String.format("Failed to reserve partitions for " + "query (partition of PARTITIONED cache is not in OWNING state after " + "reservation) [localNodeId=%s, rmtNodeId=%s, reqId=%s, affTopVer=%s, " + "cacheId=%s, cacheName=%s, part=%s, partState=%s]", ctx.localNodeId(), nodeId, reqId, topVer, cacheIds.get(i), cctx.name(), partId, partState));
                            }
                        }
                    }
                    final Collection<Integer> finalPartIds = partIds;
                    MTC.span().addLog(() -> "Cache partitions were reserved [cache=" + cctx.name() + ", partitions=" + finalPartIds + ", topology=" + topVer + ']');
                    if (explicitParts == null && reservedCnt > 0) {
                        // We reserved all the primary partitions for cache, attempt to add group reservation.
                        GridDhtPartitionsReservation grp = new GridDhtPartitionsReservation(topVer, cctx, "SQL");
                        if (grp.register(reserved.subList(reserved.size() - reservedCnt, reserved.size()))) {
                            if (reservations.putIfAbsent(grpKey, grp) != null)
                                throw new IllegalStateException("Reservation already exists.");
                            grp.onPublish(new CI1<GridDhtPartitionsReservation>() {

                                @Override
                                public void apply(GridDhtPartitionsReservation r) {
                                    reservations.remove(grpKey, r);
                                }
                            });
                        }
                    }
                }
            }
        }
        return new PartitionReservation(reserved);
    }
}
Also used : GridDhtPartitionsReservation(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionsReservation) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ArrayList(java.util.ArrayList) GridReservable(org.apache.ignite.internal.processors.cache.distributed.dht.GridReservable) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) GridDhtPartitionState(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition)

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