Search in sources :

Example 1 with QueryExecutionLowMemoryException

use of org.apache.geode.cache.query.QueryExecutionLowMemoryException in project geode by apache.

the class QueryMonitor method monitorQueryThread.

/**
   * Add query to be monitored.
   * 
   * @param queryThread Thread executing the query.
   * @param query Query.
   */
public void monitorQueryThread(Thread queryThread, Query query) {
    if (LOW_MEMORY) {
        String reason = LocalizedStrings.QueryMonitor_LOW_MEMORY_CANCELED_QUERY.toLocalizedString(LOW_MEMORY_USED_BYTES);
        ((DefaultQuery) query).setCanceled(true, new QueryExecutionLowMemoryException(reason));
        throw new QueryExecutionLowMemoryException(reason);
    }
    QueryThreadTask queryTask = new QueryThreadTask(queryThread, query, queryExecutionStatus.get());
    synchronized (queryThreads) {
        queryThreads.add(queryTask);
        queryThreads.notifyAll();
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Adding thread to QueryMonitor. QueryMonitor size is:{}, Thread (id): {} query: {} thread is : {}", queryThreads.size(), queryThread.getId(), query.getQueryString(), queryThread);
    }
    // For dunit test purpose
    if (GemFireCacheImpl.getInstance() != null && GemFireCacheImpl.getInstance().testMaxQueryExecutionTime > 0) {
        if (this.queryMonitorTasks == null) {
            this.queryMonitorTasks = new ConcurrentHashMap();
        }
        this.queryMonitorTasks.put(queryThread, queryTask);
    }
}
Also used : QueryExecutionLowMemoryException(org.apache.geode.cache.query.QueryExecutionLowMemoryException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with QueryExecutionLowMemoryException

use of org.apache.geode.cache.query.QueryExecutionLowMemoryException in project geode by apache.

the class QueryMonitor method cancelQueryDueToLowMemory.

private void cancelQueryDueToLowMemory(QueryThreadTask queryTask, long memoryThreshold) {
    boolean[] queryCompleted = ((DefaultQuery) queryTask.query).getQueryCompletedForMonitoring();
    synchronized (queryCompleted) {
        if (!queryCompleted[0]) {
            // cancel if query is not completed
            String reason = LocalizedStrings.QueryMonitor_LOW_MEMORY_CANCELED_QUERY.toLocalizedString(memoryThreshold);
            ((DefaultQuery) queryTask.query).setCanceled(true, new QueryExecutionLowMemoryException(reason));
            queryTask.queryExecutionStatus.set(Boolean.TRUE);
        }
    }
}
Also used : QueryExecutionLowMemoryException(org.apache.geode.cache.query.QueryExecutionLowMemoryException)

Example 3 with QueryExecutionLowMemoryException

use of org.apache.geode.cache.query.QueryExecutionLowMemoryException 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 4 with QueryExecutionLowMemoryException

use of org.apache.geode.cache.query.QueryExecutionLowMemoryException 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!");
    }
}
Also used : Query(org.apache.geode.cache.query.Query) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) QueryExecutionLowMemoryException(org.apache.geode.cache.query.QueryExecutionLowMemoryException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) QueryExecutionTimeoutException(org.apache.geode.cache.query.QueryExecutionTimeoutException) ResourceNotFoundException(org.apache.geode.rest.internal.web.exception.ResourceNotFoundException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) QueryExecutionLowMemoryException(org.apache.geode.cache.query.QueryExecutionLowMemoryException) GemfireRestException(org.apache.geode.rest.internal.web.exception.GemfireRestException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) GemfireRestException(org.apache.geode.rest.internal.web.exception.GemfireRestException) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) QueryExecutionTimeoutException(org.apache.geode.cache.query.QueryExecutionTimeoutException) ResourceNotFoundException(org.apache.geode.rest.internal.web.exception.ResourceNotFoundException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 5 with QueryExecutionLowMemoryException

use of org.apache.geode.cache.query.QueryExecutionLowMemoryException in project geode by apache.

the class Query method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException, InterruptedException {
    // Based on MessageType.DESTROY
    // Added by gregp 10/18/05
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    serverConnection.setAsTrue(REQUIRES_CHUNKED_RESPONSE);
    // Retrieve the data from the message parts
    String queryString = clientMessage.getPart(0).getString();
    if (clientMessage.getNumberOfParts() == 3) {
        int timeout = clientMessage.getPart(2).getInt();
        serverConnection.setRequestSpecificTimeout(timeout);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("{}: Received query request from {} queryString: {}", serverConnection.getName(), serverConnection.getSocketString(), queryString);
    }
    try {
        // Create query
        QueryService queryService = serverConnection.getCachedRegionHelper().getCache().getLocalQueryService();
        org.apache.geode.cache.query.Query query = queryService.newQuery(queryString);
        Set regionNames = ((DefaultQuery) query).getRegionsInQuery(null);
        // Authorization check
        QueryOperationContext queryContext = null;
        AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
        if (authzRequest != null) {
            queryContext = authzRequest.queryAuthorize(queryString, regionNames);
            String newQueryString = queryContext.getQuery();
            if (queryString != null && !queryString.equals(newQueryString)) {
                query = queryService.newQuery(newQueryString);
                queryString = newQueryString;
                regionNames = queryContext.getRegionNames();
                if (regionNames == null) {
                    regionNames = ((DefaultQuery) query).getRegionsInQuery(null);
                }
            }
        }
        processQuery(clientMessage, query, queryString, regionNames, start, null, queryContext, serverConnection, true);
    } catch (QueryInvalidException e) {
        throw new QueryInvalidException(e.getMessage() + queryString);
    } catch (QueryExecutionLowMemoryException e) {
        writeQueryResponseException(clientMessage, e, serverConnection);
    }
}
Also used : Set(java.util.Set) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) QueryExecutionLowMemoryException(org.apache.geode.cache.query.QueryExecutionLowMemoryException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) QueryOperationContext(org.apache.geode.cache.operations.QueryOperationContext) QueryService(org.apache.geode.cache.query.QueryService)

Aggregations

QueryExecutionLowMemoryException (org.apache.geode.cache.query.QueryExecutionLowMemoryException)7 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)4 QueryInvalidException (org.apache.geode.cache.query.QueryInvalidException)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 FunctionDomainException (org.apache.geode.cache.query.FunctionDomainException)2 NameResolutionException (org.apache.geode.cache.query.NameResolutionException)2 Query (org.apache.geode.cache.query.Query)2 QueryExecutionTimeoutException (org.apache.geode.cache.query.QueryExecutionTimeoutException)2 QueryInvocationTargetException (org.apache.geode.cache.query.QueryInvocationTargetException)2 TypeMismatchException (org.apache.geode.cache.query.TypeMismatchException)2 GemfireRestException (org.apache.geode.rest.internal.web.exception.GemfireRestException)2 ResourceNotFoundException (org.apache.geode.rest.internal.web.exception.ResourceNotFoundException)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1