use of org.apache.geode.cache.query.QueryExecutionTimeoutException in project geode by apache.
the class CompiledIteratorDef method getRuntimeIterator.
public RuntimeIterator getRuntimeIterator(ExecutionContext context) throws TypeMismatchException, AmbiguousNameException, NameResolutionException {
RuntimeIterator rIter = null;
// check cached in context
rIter = (RuntimeIterator) context.cacheGet(this);
if (rIter != null) {
return rIter;
}
ObjectType type = this.elementType;
if (type.equals(TypeUtils.OBJECT_TYPE)) {
// check to see if there's a typecast for this collection
ObjectType typc = getCollectionElementTypeCast();
if (typc != null) {
type = typc;
} else {
// Does not determine type of nested query
if (!(this.collectionExpr instanceof CompiledSelect)) {
type = computeElementType(context);
}
}
}
rIter = new RuntimeIterator(this, type);
// generate from clause should take care of bucket region substitution if
// necessary and then set the definition.
String fromClause = genFromClause(context);
rIter.setDefinition(fromClause);
/*
* If the type of RunTimeIterator is still ObjectType & if the RuneTimeIterator is independent
* of any iterator of the scopes less than or equal to its own scope, we can evaluate the
* collection via RuntimeIterator. This will initialize the Collection of RuntimeIterator ,
* which is OK. The code in RuntimeIterator will be rectified such that the ElementType of that
* RuntimeIterator is taken from the collection
*/
if (type.equals(TypeUtils.OBJECT_TYPE) && !this.isDependentOnAnyIteratorOfScopeLessThanItsOwn(context)) {
// the collection
try {
rIter.evaluateCollection(context);
} catch (QueryExecutionTimeoutException qet) {
throw qet;
} catch (RegionNotFoundException re) {
throw re;
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("Exception while getting runtime iterator.", e);
}
throw new TypeMismatchException(LocalizedStrings.CompiledIteratorDef_EXCEPTION_IN_EVALUATING_THE_COLLECTION_EXPRESSION_IN_GETRUNTIMEITERATOR_EVEN_THOUGH_THE_COLLECTION_IS_INDEPENDENT_OF_ANY_RUNTIMEITERATOR.toLocalizedString(), e);
}
}
// cache in context
context.cachePut(this, rIter);
return rIter;
}
use of org.apache.geode.cache.query.QueryExecutionTimeoutException in project geode by apache.
the class QueryMonitor method stopMonitoringQueryThread.
/**
* Stops monitoring the query. Removes the passed thread from QueryMonitor queue.
*/
public void stopMonitoringQueryThread(Thread queryThread, Query query) {
// Re-Set the queryExecution status on the LocalThread.
QueryExecutionTimeoutException testException = null;
DefaultQuery defaultQuery = (DefaultQuery) query;
boolean[] queryCompleted = defaultQuery.getQueryCompletedForMonitoring();
synchronized (queryCompleted) {
queryExecutionStatus.get().getAndSet(Boolean.FALSE);
// START - DUnit Test purpose.
if (GemFireCacheImpl.getInstance() != null && GemFireCacheImpl.getInstance().testMaxQueryExecutionTime > 0) {
long maxTimeSet = GemFireCacheImpl.getInstance().testMaxQueryExecutionTime;
QueryThreadTask queryTask = (QueryThreadTask) queryThreads.peek();
long currentTime = System.currentTimeMillis();
// longer than the specified time.
if (queryTask != null) {
if (currentTime - queryTask.StartTime > maxTimeSet) {
// The sleep() is unpredictable.
testException = new QueryExecutionTimeoutException("The QueryMonitor thread may be sleeping longer than" + " the set sleep time. This will happen as the sleep is based on OS thread scheduling," + " verify the time spent by the executor thread.");
}
}
}
// END - DUnit Test purpose.
defaultQuery.setQueryCompletedForMonitoring(true);
// Remove the query task from the queue.
queryThreads.remove(new QueryThreadTask(queryThread, null, null));
}
if (logger.isDebugEnabled()) {
logger.debug("Removed thread from QueryMonitor. QueryMonitor size is:{}, Thread ID is: {} thread is : {}", queryThreads.size(), queryThread.getId(), queryThread);
}
if (testException != null) {
throw testException;
}
}
use of org.apache.geode.cache.query.QueryExecutionTimeoutException in project geode by apache.
the class QueryAccessController method runNamedQuery.
/**
* Run named parametrized Query with ID
*
* @param queryId id of the OQL string
* @param arguments query bind params required while executing query
* @return query result as a JSON document
*/
@RequestMapping(method = RequestMethod.POST, value = "/{query}", produces = { MediaType.APPLICATION_JSON_VALUE })
@ApiOperation(value = "run parametrized query", notes = "run the specified named query passing in scalar values for query parameters in the GemFire cluster", response = void.class)
@ApiResponses({ @ApiResponse(code = 200, message = "Query successfully executed."), @ApiResponse(code = 401, message = "Invalid Username or Password."), @ApiResponse(code = 403, message = "Insufficient privileges for operation."), @ApiResponse(code = 400, message = "Query bind params specified as JSON document in the request body is invalid"), @ApiResponse(code = 500, message = "GemFire throws an error or exception") })
@ResponseBody
@ResponseStatus(HttpStatus.OK)
@PreAuthorize("@securityService.authorize('DATA', 'READ')")
public ResponseEntity<String> runNamedQuery(@PathVariable("query") String queryId, @RequestBody String arguments) {
logger.debug("Running named Query with ID ({})...", queryId);
queryId = decode(queryId);
if (arguments != null) {
// Its a compiled query.
// Convert arguments into Object[]
Object[] args = jsonToObjectArray(arguments);
Query compiledQuery = compiledQueries.get(queryId);
if (compiledQuery == null) {
// This is first time the query is seen by this server.
final String oql = getValue(PARAMETERIZED_QUERIES_REGION, queryId, false);
ValidationUtils.returnValueThrowOnNull(oql, new ResourceNotFoundException(String.format("No Query with ID (%1$s) was found!", queryId)));
try {
compiledQuery = getQueryService().newQuery(oql);
} catch (QueryInvalidException qie) {
throw new GemfireRestException("Syntax of the OQL queryString is invalid!", qie);
}
compiledQueries.putIfAbsent(queryId, (DefaultQuery) compiledQuery);
}
// and handle the Exceptions appropriately (500 Server Error)!
try {
Object queryResult = compiledQuery.execute(args);
return processQueryResponse(compiledQuery, args, queryResult);
} catch (FunctionDomainException fde) {
throw new GemfireRestException("A function was applied to a parameter that is improper for that function!", fde);
} catch (TypeMismatchException tme) {
throw new GemfireRestException("Bind parameter is not of the expected type!", tme);
} catch (NameResolutionException nre) {
throw new GemfireRestException("Name in the query cannot be resolved!", nre);
} catch (IllegalArgumentException iae) {
throw new GemfireRestException(" The number of bound parameters does not match the number of placeholders!", iae);
} catch (IllegalStateException ise) {
throw new GemfireRestException("Query is not permitted on this type of region!", ise);
} catch (QueryExecutionTimeoutException qete) {
throw new GemfireRestException("Query execution time is exceeded max query execution time (gemfire.Cache.MAX_QUERY_EXECUTION_TIME) configured!", qete);
} catch (QueryInvocationTargetException qite) {
throw new GemfireRestException("Data referenced in from clause is not available for querying!", qite);
} catch (QueryExecutionLowMemoryException qelme) {
throw new GemfireRestException("Query gets canceled due to low memory conditions and the resource manager critical heap percentage has been set!", qelme);
} catch (Exception e) {
throw new GemfireRestException("Error encountered while executing named query!", e);
}
} else {
throw new GemfireRestException(" Bind params either not specified or not processed properly by the server!");
}
}
use of org.apache.geode.cache.query.QueryExecutionTimeoutException in project geode by apache.
the class QueryMonitor method run.
/**
* Starts monitoring the query. If query runs longer than the set MAX_QUERY_EXECUTION_TIME,
* interrupts the thread executing the query.
*/
@Override
public void run() {
// if the query monitor is stopped before run has been called, we should not run
synchronized (this.stopped) {
if (this.stopped.get()) {
queryThreads.clear();
return;
}
this.monitoringThread = Thread.currentThread();
}
try {
QueryThreadTask queryTask;
long sleepTime;
// TODO: while-block cannot complete without throwing
while (true) {
// Get the first query task from the queue. This query will have the shortest
// remaining time that needs to canceled first.
queryTask = (QueryThreadTask) queryThreads.peek();
if (queryTask == null) {
// Empty queue.
synchronized (queryThreads) {
queryThreads.wait();
}
continue;
}
long currentTime = System.currentTimeMillis();
// Check if the sleepTime is greater than the remaining query execution time.
if (currentTime - queryTask.StartTime < this.maxQueryExecutionTime) {
sleepTime = this.maxQueryExecutionTime - (currentTime - queryTask.StartTime);
// Its been noted that the sleep is not guaranteed to wait for the specified
// time (as stated in Suns doc too), it depends on the OSs thread scheduling
// behavior, hence thread may sleep for longer than the specified time.
// Specifying shorter time also hasn't worked.
Thread.sleep(sleepTime);
continue;
}
// Query execution has taken more than the max time, Set queryExecutionStatus flag
// to canceled (TRUE).
boolean[] queryCompleted = ((DefaultQuery) queryTask.query).getQueryCompletedForMonitoring();
synchronized (queryCompleted) {
if (!queryCompleted[0]) {
// Check if the query is already completed.
((DefaultQuery) queryTask.query).setCanceled(true, new QueryExecutionTimeoutException(LocalizedStrings.QueryMonitor_LONG_RUNNING_QUERY_CANCELED.toLocalizedString(GemFireCacheImpl.MAX_QUERY_EXECUTION_TIME)));
queryTask.queryExecutionStatus.set(Boolean.TRUE);
// Remove the task from queue.
queryThreads.poll();
}
}
logger.info(LocalizedMessage.create(LocalizedStrings.GemFireCache_LONG_RUNNING_QUERY_EXECUTION_CANCELED, new Object[] { queryTask.query.getQueryString(), queryTask.queryThread.getId() }));
if (logger.isDebugEnabled()) {
logger.debug("Query Execution for the thread {} got canceled.", queryTask.queryThread);
}
}
} catch (InterruptedException ignore) {
if (logger.isDebugEnabled()) {
logger.debug("Query Monitoring thread got interrupted.");
}
} finally {
queryThreads.clear();
}
}
use of org.apache.geode.cache.query.QueryExecutionTimeoutException in project geode by apache.
the class QueryAccessController method runAdhocQuery.
/**
* Run an adhoc Query specified in a query string
*
* @param oql OQL query string to be executed
* @return query result as a JSON document
*/
@RequestMapping(method = RequestMethod.GET, value = "/adhoc", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
@ApiOperation(value = "run an adhoc query", notes = "Run an unnamed (unidentified), ad-hoc query passed as a URL parameter", response = void.class)
@ApiResponses({ @ApiResponse(code = 200, message = "OK."), @ApiResponse(code = 401, message = "Invalid Username or Password."), @ApiResponse(code = 403, message = "Insufficient privileges for operation."), @ApiResponse(code = 500, message = "GemFire throws an error or exception") })
@ResponseBody
@ResponseStatus(HttpStatus.OK)
@PreAuthorize("@securityService.authorize('DATA', 'READ')")
public ResponseEntity<String> runAdhocQuery(@RequestParam("q") String oql) {
logger.debug("Running an adhoc Query ({})...", oql);
oql = decode(oql);
final Query query = getQueryService().newQuery(oql);
// and handle the Exceptions appropriately (500 Server Error)!
try {
Object queryResult = query.execute();
return processQueryResponse(query, null, queryResult);
} catch (FunctionDomainException fde) {
throw new GemfireRestException("A function was applied to a parameter that is improper for that function!", fde);
} catch (TypeMismatchException tme) {
throw new GemfireRestException("Bind parameter is not of the expected type!", tme);
} catch (NameResolutionException nre) {
throw new GemfireRestException("Name in the query cannot be resolved!", nre);
} catch (IllegalArgumentException iae) {
throw new GemfireRestException(" The number of bound parameters does not match the number of placeholders!", iae);
} catch (IllegalStateException ise) {
throw new GemfireRestException("Query is not permitted on this type of region!", ise);
} catch (QueryExecutionTimeoutException qete) {
throw new GemfireRestException("Query execution time is exceeded max query execution time (gemfire.Cache.MAX_QUERY_EXECUTION_TIME) configured! ", qete);
} catch (QueryInvocationTargetException qite) {
throw new GemfireRestException("Data referenced in from clause is not available for querying!", qite);
} catch (QueryExecutionLowMemoryException qelme) {
throw new GemfireRestException("Query execution gets canceled due to low memory conditions and the resource manager critical heap percentage has been set!", qelme);
} catch (Exception e) {
throw new GemfireRestException("Server has encountered while executing Adhoc query!", e);
}
}
Aggregations