Search in sources :

Example 1 with IndexTrackingQueryObserver

use of org.apache.geode.cache.query.internal.IndexTrackingQueryObserver 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)

Example 2 with IndexTrackingQueryObserver

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

the class DataCommandFunction method getLogMessage.

public static String getLogMessage(QueryObserver observer, long startTime, String query) {
    String usedIndexesString = null;
    float time = 0.0f;
    if (startTime > 0L) {
        time = (NanoTimer.getTime() - startTime) / 1.0e6f;
    }
    if (observer != null && observer instanceof IndexTrackingQueryObserver) {
        IndexTrackingQueryObserver indexObserver = (IndexTrackingQueryObserver) observer;
        Map usedIndexes = indexObserver.getUsedIndexes();
        indexObserver.reset();
        StringBuffer buf = new StringBuffer();
        buf.append(" indexesUsed(");
        buf.append(usedIndexes.size());
        buf.append(")");
        if (usedIndexes.size() > 0) {
            buf.append(":");
            for (Iterator itr = usedIndexes.entrySet().iterator(); itr.hasNext(); ) {
                Map.Entry entry = (Map.Entry) itr.next();
                buf.append(entry.getKey().toString()).append(entry.getValue());
                if (itr.hasNext()) {
                    buf.append(",");
                }
            }
        }
        usedIndexesString = buf.toString();
    } else if (DefaultQuery.QUERY_VERBOSE) {
        usedIndexesString = " indexesUsed(NA due to other observer in the way: " + observer.getClass().getName() + ")";
    }
    return String.format("Query Executed%s%s", startTime > 0L ? " in " + time + " ms;" : ";", usedIndexesString != null ? usedIndexesString : "");
}
Also used : Iterator(java.util.Iterator) IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver) Map(java.util.Map)

Example 3 with IndexTrackingQueryObserver

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

the class IndexTrackingQueryObserverJUnitTest method setUp.

@Before
public void setUp() throws Exception {
    System.setProperty(DistributionConfig.GEMFIRE_PREFIX + "Query.VERBOSE", "true");
    CacheUtils.startCache();
    QueryObserver observer = QueryObserverHolder.setInstance(new IndexTrackingQueryObserver());
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver) IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver) Before(org.junit.Before)

Example 4 with IndexTrackingQueryObserver

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

the class IndexTrackingQueryObserverJUnitTest method testIndexInfoOnPartitionedRegion.

@Test
public void testIndexInfoOnPartitionedRegion() throws Exception {
    // Query VERBOSE has to be true for the test
    assertEquals("true", System.getProperty(DistributionConfig.GEMFIRE_PREFIX + "Query.VERBOSE"));
    // Create Partition Region
    PartitionAttributesFactory paf = new PartitionAttributesFactory();
    paf.setTotalNumBuckets(NUM_BKTS);
    AttributesFactory af = new AttributesFactory();
    af.setPartitionAttributes(paf.create());
    region = CacheUtils.createRegion("portfolio", af.create(), false);
    if (region.size() == 0) {
        for (int i = 1; i <= 100; i++) {
            region.put(Integer.toString(i), new Portfolio(i, i));
        }
    }
    assertEquals(100, region.size());
    qs = CacheUtils.getQueryService();
    keyIndex1 = (IndexProtocol) qs.createIndex(INDEX_NAME, IndexType.FUNCTIONAL, "ID", "/portfolio ");
    assertTrue(keyIndex1 instanceof PartitionedIndex);
    Query query = qs.newQuery(queryStr);
    // Inject TestHook in QueryObserver before running query.
    IndexTrackingTestHook th = new IndexTrackingTestHook(region, NUM_BKTS);
    QueryObserver observer = QueryObserverHolder.getInstance();
    assertTrue(QueryObserverHolder.hasObserver());
    ((IndexTrackingQueryObserver) observer).setTestHook(th);
    SelectResults results = (SelectResults) query.execute();
    // The query should return all elements in region.
    assertEquals(region.size(), results.size());
    // Check results size of Map.
    regionMap = ((IndexTrackingTestHook) th).getRegionMap();
    Collection<Integer> rslts = regionMap.getResults().values();
    int totalResults = 0;
    for (Integer i : rslts) {
        totalResults += i.intValue();
    }
    assertEquals(results.size(), totalResults);
    QueryObserverHolder.reset();
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) IndexTrackingTestHook(org.apache.geode.cache.query.internal.index.IndexTrackingQueryObserverDUnitTest.IndexTrackingTestHook) Portfolio(org.apache.geode.cache.query.data.Portfolio) IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 5 with IndexTrackingQueryObserver

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

the class IndexTrackingQueryObserverJUnitTest method testIndexInfoOnLocalRegion.

@Test
public void testIndexInfoOnLocalRegion() throws Exception {
    // Query VERBOSE has to be true for the test
    assertEquals("true", System.getProperty(DistributionConfig.GEMFIRE_PREFIX + "Query.VERBOSE"));
    // Create Partition Region
    AttributesFactory af = new AttributesFactory();
    af.setScope(Scope.LOCAL);
    region = CacheUtils.createRegion("portfolio", af.create(), false);
    if (region.size() == 0) {
        for (int i = 1; i <= 100; i++) {
            region.put(Integer.toString(i), new Portfolio(i, i));
        }
    }
    assertEquals(100, region.size());
    qs = CacheUtils.getQueryService();
    keyIndex1 = (IndexProtocol) qs.createIndex(INDEX_NAME, IndexType.FUNCTIONAL, "ID", "/portfolio ");
    assertTrue(keyIndex1 instanceof CompactRangeIndex);
    Query query = qs.newQuery(queryStr);
    // Inject TestHook in QueryObserver before running query.
    IndexTrackingTestHook th = new IndexTrackingTestHook(region, 0);
    QueryObserver observer = QueryObserverHolder.getInstance();
    assertTrue(QueryObserverHolder.hasObserver());
    ((IndexTrackingQueryObserver) observer).setTestHook(th);
    SelectResults results = (SelectResults) query.execute();
    // The query should return all elements in region.
    assertEquals(region.size(), results.size());
    regionMap = ((IndexTrackingTestHook) th).getRegionMap();
    Object rslts = regionMap.getResults().get(region.getFullPath());
    assertTrue(rslts instanceof Integer);
    assertEquals(results.size(), ((Integer) rslts).intValue());
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) IndexTrackingTestHook(org.apache.geode.cache.query.internal.index.IndexTrackingQueryObserverDUnitTest.IndexTrackingTestHook) Portfolio(org.apache.geode.cache.query.data.Portfolio) IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

IndexTrackingQueryObserver (org.apache.geode.cache.query.internal.IndexTrackingQueryObserver)8 QueryObserver (org.apache.geode.cache.query.internal.QueryObserver)7 AttributesFactory (org.apache.geode.cache.AttributesFactory)3 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)3 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)3 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Region (org.apache.geode.cache.Region)2 Portfolio (org.apache.geode.cache.query.data.Portfolio)2 IndexTrackingTestHook (org.apache.geode.cache.query.internal.index.IndexTrackingQueryObserverDUnitTest.IndexTrackingTestHook)2 LocalRegion (org.apache.geode.internal.cache.LocalRegion)2 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)2 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Index (org.apache.geode.cache.query.Index)1 QueryExecutionLowMemoryException (org.apache.geode.cache.query.QueryExecutionLowMemoryException)1 QueryService (org.apache.geode.cache.query.QueryService)1 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)1