Search in sources :

Example 1 with PRQueryTraceInfo

use of org.apache.geode.cache.query.internal.PRQueryTraceInfo in project geode by apache.

the class QueryMessage method operateOnPartitionedRegion.

@Override
protected boolean operateOnPartitionedRegion(DistributionManager dm, PartitionedRegion pr, long startTime) throws CacheException, QueryException, ForceReattemptException, InterruptedException {
    // calculate trace start time if trace is on this is because the start time is only set if
    // enableClock stats is on in this case we still want to see trace time even if clock is not
    // enabled
    long traceStartTime = 0;
    if (this.traceOn) {
        traceStartTime = NanoTimer.getTime();
    }
    if (Thread.interrupted()) {
        throw new InterruptedException();
    }
    if (logger.isTraceEnabled(LogMarker.DM)) {
        logger.trace(LogMarker.DM, "QueryMessage operateOnPartitionedRegion: {} buckets {}", pr.getFullPath(), this.buckets);
    }
    pr.waitOnInitialization();
    if (QueryMonitor.isLowMemory()) {
        String reason = LocalizedStrings.QueryMonitor_LOW_MEMORY_CANCELED_QUERY.toLocalizedString(QueryMonitor.getMemoryUsedDuringLowMemory());
        // throws the same error for low memory
        throw new QueryExecutionLowMemoryException(reason);
    }
    DefaultQuery query = new DefaultQuery(this.queryString, pr.getCache(), false);
    // Remote query, use the PDX types in serialized form.
    DefaultQuery.setPdxReadSerialized(pr.getCache(), true);
    // In case of "select *" queries we can keep the results in serialized form and send
    query.setRemoteQuery(true);
    QueryObserver indexObserver = query.startTrace();
    boolean isQueryTraced = false;
    List queryTraceList = null;
    try {
        query.setIsCqQuery(this.cqQuery);
        PRQueryProcessor qp = new PRQueryProcessor(pr, query, this.parameters, this.buckets);
        if (logger.isDebugEnabled()) {
            logger.debug("Started executing query from remote node: {}", query.getQueryString());
        }
        isQueryTraced = query.isTraced() && this.sender.getVersionObject().compareTo(Version.GFE_81) >= 0;
        // Adds a query trace info object to the results list for remote queries
        PRQueryTraceInfo queryTraceInfo = null;
        if (isQueryTraced) {
            this.isTraceInfoIteration = true;
            if (DefaultQuery.testHook != null) {
                DefaultQuery.testHook.doTestHook("Create PR Query Trace Info for Remote Query");
            }
            queryTraceInfo = new PRQueryTraceInfo();
            queryTraceList = Collections.singletonList(queryTraceInfo);
        }
        this.isStructType = qp.executeQuery(this.resultCollector);
        // from the sorted collection of NWayMergeResults
        if (isQueryTraced) {
            this.resultCollector.add(0, queryTraceList);
        }
        this.currentSelectResultIterator = this.resultCollector.iterator();
        // information here rather than the finally block.
        if (isQueryTraced) {
            if (DefaultQuery.testHook != null) {
                DefaultQuery.testHook.doTestHook("Populating Trace Info for Remote Query");
            }
            // calculate the number of rows being sent
            int traceSize = queryTraceInfo.calculateNumberOfResults(this.resultCollector);
            // subtract the query trace info object
            traceSize -= 1;
            queryTraceInfo.setTimeInMillis((NanoTimer.getTime() - traceStartTime) / 1.0e6f);
            queryTraceInfo.setNumResults(traceSize);
            // created the indexes used string
            if (indexObserver instanceof IndexTrackingQueryObserver) {
                Map indexesUsed = ((IndexTrackingQueryObserver) indexObserver).getUsedIndexes();
                StringBuilder sb = new StringBuilder();
                sb.append(" indexesUsed(").append(indexesUsed.size()).append(")");
                if (indexesUsed.size() > 0) {
                    sb.append(":");
                    for (Iterator itr = indexesUsed.entrySet().iterator(); itr.hasNext(); ) {
                        Map.Entry entry = (Map.Entry) itr.next();
                        sb.append(entry.getKey()).append(entry.getValue());
                        if (itr.hasNext()) {
                            sb.append(",");
                        }
                    }
                }
                queryTraceInfo.setIndexesUsed(sb.toString());
            }
        }
        if (QueryMonitor.isLowMemory()) {
            String reason = LocalizedStrings.QueryMonitor_LOW_MEMORY_CANCELED_QUERY.toLocalizedString(QueryMonitor.getMemoryUsedDuringLowMemory());
            throw new QueryExecutionLowMemoryException(reason);
        }
        super.operateOnPartitionedRegion(dm, pr, startTime);
    } finally {
        // remove trace info so that it is not included in the num results when logged
        if (isQueryTraced) {
            this.resultCollector.remove(queryTraceList);
        }
        DefaultQuery.setPdxReadSerialized(pr.getCache(), false);
        query.setRemoteQuery(false);
        query.endTrace(indexObserver, traceStartTime, this.resultCollector);
    }
    // Unless there was an exception thrown, this message handles sending the response
    return false;
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) PRQueryTraceInfo(org.apache.geode.cache.query.internal.PRQueryTraceInfo) QueryExecutionLowMemoryException(org.apache.geode.cache.query.QueryExecutionLowMemoryException) PRQueryProcessor(org.apache.geode.internal.cache.PRQueryProcessor) IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver) IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 QueryExecutionLowMemoryException (org.apache.geode.cache.query.QueryExecutionLowMemoryException)1 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)1 IndexTrackingQueryObserver (org.apache.geode.cache.query.internal.IndexTrackingQueryObserver)1 PRQueryTraceInfo (org.apache.geode.cache.query.internal.PRQueryTraceInfo)1 QueryObserver (org.apache.geode.cache.query.internal.QueryObserver)1 PRQueryProcessor (org.apache.geode.internal.cache.PRQueryProcessor)1