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;
}
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 : "");
}
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());
}
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();
}
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());
}
Aggregations