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;
}
}
};
}
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);
}
}
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));
}
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.
}
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;
}
}
Aggregations