Search in sources :

Example 1 with QueryCancelledException

use of org.apache.jena.query.QueryCancelledException in project jena by apache.

the class QueryIterTopN method sortTopN.

private Iterator<Binding> sortTopN(final QueryIterator qIter, final Comparator<Binding> comparator) {
    return new IteratorDelayedInitialization<Binding>() {

        @Override
        protected Iterator<Binding> initializeIterator() {
            try {
                while (qIter.hasNext()) {
                    Binding binding = qIter.next();
                    if (heap.size() < limit)
                        add(binding);
                    else {
                        Binding currentMaxLeastN = heap.peek();
                        if (comparator.compare(binding, currentMaxLeastN) < 0)
                            add(binding);
                    }
                }
                qIter.close();
                Binding[] y = heap.toArray(new Binding[] {});
                heap = null;
                Arrays.sort(y, comparator);
                return asList(y).iterator();
            } catch (QueryCancelledException e) {
                QueryIterTopN.this.close();
                this.close();
                throw e;
            }
        }
    };
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) IteratorDelayedInitialization(org.apache.jena.atlas.iterator.IteratorDelayedInitialization) QueryCancelledException(org.apache.jena.query.QueryCancelledException)

Example 2 with QueryCancelledException

use of org.apache.jena.query.QueryCancelledException in project jena by apache.

the class SPARQL_ServletBase method doCommon.

// Common framework for handling HTTP requests
/**
     * Handles GET and POST requests.
     * @param request HTTP request
     * @param response HTTP response
     */
protected void doCommon(HttpServletRequest request, HttpServletResponse response) //throws ServletException, IOException
{
    try {
        long id = allocRequestId(request, response);
        // Lifecycle
        HttpAction action = allocHttpAction(id, request, response);
        // then add to doCommonWorker
        // work with HttpServletResponseTracker
        printRequest(action);
        action.setStartTime();
        response = action.response;
        initResponse(request, response);
        Context cxt = ARQ.getContext();
        try {
            execCommonWorker(action);
        } catch (QueryCancelledException ex) {
            // Also need the per query info ...
            String message = String.format("The query timed out (restricted to %s ms)", cxt.get(ARQ.queryTimeout));
            // Possibility :: response.setHeader("Retry-after", "600") ;    // 5 minutes
            responseSendError(response, HttpSC.SERVICE_UNAVAILABLE_503, message);
        } catch (ActionErrorException ex) {
            if (ex.exception != null)
                ex.exception.printStackTrace(System.err);
            // Log message done by printResponse in a moment.
            if (ex.message != null)
                responseSendError(response, ex.rc, ex.message);
            else
                responseSendError(response, ex.rc);
        } catch (RuntimeIOException ex) {
            log.warn(format("[%d] Runtime IO Exception (client left?) RC = %d", id, HttpSC.INTERNAL_SERVER_ERROR_500));
            responseSendError(response, HttpSC.INTERNAL_SERVER_ERROR_500, ex.getMessage());
        } catch (Throwable ex) {
            // This should not happen.
            //ex.printStackTrace(System.err) ;
            log.warn(format("[%d] RC = %d : %s", id, HttpSC.INTERNAL_SERVER_ERROR_500, ex.getMessage()), ex);
            responseSendError(response, HttpSC.INTERNAL_SERVER_ERROR_500, ex.getMessage());
        }
        action.setFinishTime();
        printResponse(action);
        archiveHttpAction(action);
    } catch (Throwable th) {
        log.error("Internal error", th);
    }
}
Also used : Context(org.apache.jena.sparql.util.Context) RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) QueryCancelledException(org.apache.jena.query.QueryCancelledException)

Example 3 with QueryCancelledException

use of org.apache.jena.query.QueryCancelledException in project jena by apache.

the class TestQueryIterSort method testCancelInterruptsExternalSortAfterStartingIteration.

@Test(expected = QueryCancelledException.class)
public void testCancelInterruptsExternalSortAfterStartingIteration() {
    assertEquals(0, iterator.getReturnedElementCount());
    Context context = new Context();
    context.set(ARQ.spillToDiskThreshold, 10L);
    ExecutionContext executionContext = new ExecutionContext(context, (Graph) null, (DatasetGraph) null, (OpExecutorFactory) null);
    QueryIterSort qIter = new QueryIterSort(iterator, comparator, executionContext);
    try {
        assertEquals(0, iterator.getReturnedElementCount());
        assertEquals(0, DataBagExaminer.countTemporaryFiles(qIter.db));
        // throws a QueryCancelledException
        qIter.hasNext();
    } catch (QueryCancelledException e) {
        // expected
        assertEquals(26, iterator.getReturnedElementCount());
        // This is zero because QueryIteratorBase will call close() before throwing the QueryCancelledException.
        // It does this as a failsafe in case the user doesn't close the QueryIterator themselves.
        assertEquals(0, DataBagExaminer.countTemporaryFiles(qIter.db));
        throw e;
    } finally {
        qIter.close();
    }
    assertEquals(0, DataBagExaminer.countTemporaryFiles(qIter.db));
}
Also used : Context(org.apache.jena.sparql.util.Context) SerializationContext(org.apache.jena.sparql.serializer.SerializationContext) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) QueryIterSort(org.apache.jena.sparql.engine.iterator.QueryIterSort) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) QueryCancelledException(org.apache.jena.query.QueryCancelledException) Test(org.junit.Test)

Example 4 with QueryCancelledException

use of org.apache.jena.query.QueryCancelledException in project jena by apache.

the class ResponseResultSet method output.

private static void output(HttpAction action, String contentType, String charset, OutputContent proc) {
    try {
        setHttpResponse(action, contentType, charset);
        action.response.setStatus(HttpSC.OK_200);
        ServletOutputStream out = action.response.getOutputStream();
        try {
            proc.output(out);
            out.flush();
        } catch (QueryCancelledException ex) {
            // Bother.  Status code 200 already sent.
            action.log.info(format("[%d] Query Cancelled - results truncated (but 200 already sent)", action.id));
            out.println();
            out.println("##  Query cancelled due to timeout during execution   ##");
            out.println("##  ****          Incomplete results           ****   ##");
            out.flush();
        // No point raising an exception - 200 was sent already.
        //errorOccurred(ex) ;
        }
    // Includes client gone.
    } catch (IOException ex) {
        ServletOps.errorOccurred(ex);
    }
// Do not call httpResponse.flushBuffer() at this point. JSON callback closing details haven't been added.
// Jetty closes the stream if it is a gzip stream.
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) IOException(java.io.IOException) QueryCancelledException(org.apache.jena.query.QueryCancelledException)

Example 5 with QueryCancelledException

use of org.apache.jena.query.QueryCancelledException in project jena by apache.

the class QueryIteratorBase method nextBinding.

/** final - subclasses implement moveToNextBinding() */
@Override
public final Binding nextBinding() {
    try {
        // Need to make sure to only read this once per iteration
        boolean shouldCancel = requestingCancel;
        if (shouldCancel && abortIterator) {
            // Try to close first to release resources (in case the user
            // doesn't have a close() call in a finally block)
            close();
            throw new QueryCancelledException();
        }
        if (finished)
            throw new NoSuchElementException(Lib.className(this));
        if (!hasNextBinding())
            throw new NoSuchElementException(Lib.className(this));
        Binding obj = moveToNextBinding();
        if (obj == null)
            throw new NoSuchElementException(Lib.className(this));
        if (shouldCancel && !finished) {
            // But .cancel sets both requestingCancel and abortIterator
            // This only happens with a continuing iterator.
            close();
        }
        return obj;
    } catch (QueryFatalException ex) {
        Log.error(this, "QueryFatalException", ex);
        throw ex;
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryFatalException(org.apache.jena.query.QueryFatalException) QueryCancelledException(org.apache.jena.query.QueryCancelledException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

QueryCancelledException (org.apache.jena.query.QueryCancelledException)8 Context (org.apache.jena.sparql.util.Context)4 RuntimeIOException (org.apache.jena.atlas.RuntimeIOException)2 ExecutionContext (org.apache.jena.sparql.engine.ExecutionContext)2 Binding (org.apache.jena.sparql.engine.binding.Binding)2 QueryIterSort (org.apache.jena.sparql.engine.iterator.QueryIterSort)2 SerializationContext (org.apache.jena.sparql.serializer.SerializationContext)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 NoSuchElementException (java.util.NoSuchElementException)1 Random (java.util.Random)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 IteratorDelayedInitialization (org.apache.jena.atlas.iterator.IteratorDelayedInitialization)1 QueryFatalException (org.apache.jena.query.QueryFatalException)1 SortCondition (org.apache.jena.query.SortCondition)1 Var (org.apache.jena.sparql.core.Var)1 BindingComparator (org.apache.jena.sparql.engine.binding.BindingComparator)1 ExprVar (org.apache.jena.sparql.expr.ExprVar)1 Before (org.junit.Before)1