use of datawave.webservice.query.util.NextContentCriteria in project datawave by NationalSecurityAgency.
the class QueryExecutorBean method next.
private BaseQueryResponse next(final String id, boolean checkForContentLookup) {
// in case we don't make it to creating the response from the QueryLogic
BaseQueryResponse response = responseObjectFactory.getEventQueryResponse();
Collection<String> proxyServers = null;
Principal p = ctx.getCallerPrincipal();
String userid = p.getName();
if (p instanceof DatawavePrincipal) {
DatawavePrincipal dp = (DatawavePrincipal) p;
userid = dp.getShortName();
proxyServers = dp.getProxyServers();
}
Span span = null;
RunningQuery query = null;
Query contentLookupSettings = null;
try {
ctx.getUserTransaction().begin();
// Not calling getQueryById() here. We don't want to pull the persisted definition.
query = queryCache.get(id);
// The lock should be released at the end of the method call.
if (!queryCache.lock(id)) {
throw new QueryException(DatawaveErrorCode.QUERY_LOCKED_ERROR);
}
// an error.
if (null == query || null == query.getConnection()) {
// status code.
if (null == query) {
List<Query> queries = persister.findById(id);
if (queries == null || queries.size() != 1) {
throw new NotFoundQueryException(DatawaveErrorCode.NO_QUERY_OBJECT_MATCH, MessageFormat.format("{0}", id));
}
}
throw new PreConditionFailedQueryException(DatawaveErrorCode.QUERY_TIMEOUT_OR_SERVER_ERROR, MessageFormat.format("id = {0}", id));
} else {
// Validate the query belongs to the caller
if (!query.getSettings().getOwner().equals(userid)) {
throw new UnauthorizedQueryException(DatawaveErrorCode.QUERY_OWNER_MISMATCH, MessageFormat.format("{0} != {1}", userid, query.getSettings().getOwner()));
}
// Set the active call and get next
query.setActiveCall(true);
response = _next(query, id, proxyServers, span);
// Conditionally swap the standard response with content
if (checkForContentLookup) {
final Query settings = query.getSettings();
final Parameter contentLookupParam = settings.findParameter(LookupUUIDUtil.PARAM_CONTENT_LOOKUP);
if ((null != contentLookupParam) && Boolean.parseBoolean(contentLookupParam.getParameterValue())) {
contentLookupSettings = settings;
}
}
// Unset the active call and return
query.setActiveCall(false);
}
} catch (NoResultsException e) {
if (query != null) {
query.setActiveCall(false);
if (query.getLogic().getCollectQueryMetrics()) {
try {
// do not set the error message here - zero results is not an error that should be added to metrics
metrics.updateMetric(query.getMetric());
} catch (Exception e1) {
log.error(e1.getMessage());
}
}
}
try {
ctx.getUserTransaction().setRollbackOnly();
} catch (Exception ex) {
log.error("Error marking transaction for roll back", ex);
}
// close the query, as there were no results and we are done here
close(id);
// remember that we auto-closed this query
closedQueryCache.add(id);
throw e;
} catch (DatawaveWebApplicationException e) {
if (query != null) {
query.setActiveCall(false);
if (query.getLogic().getCollectQueryMetrics()) {
query.getMetric().setError(e);
try {
metrics.updateMetric(query.getMetric());
} catch (Exception e1) {
log.error("Error updating query metrics", e1);
}
}
}
try {
ctx.getUserTransaction().setRollbackOnly();
} catch (Exception ex) {
log.error("Error marking transaction for roll back", ex);
}
if (e.getCause() instanceof NoResultsException) {
close(id);
// remember that we auto-closed this query
closedQueryCache.add(id);
}
throw e;
} catch (Exception e) {
log.error("Query Failed", e);
if (query != null) {
query.setActiveCall(false);
if (query.getLogic().getCollectQueryMetrics() == true) {
query.getMetric().setError(e);
try {
metrics.updateMetric(query.getMetric());
} catch (Exception e1) {
log.error("Error updating query metrics", e1);
}
}
}
try {
ctx.getUserTransaction().setRollbackOnly();
} catch (Exception ex) {
log.error("Error marking transaction for roll back", ex);
}
QueryException qe = new QueryException(DatawaveErrorCode.QUERY_NEXT_ERROR, e, MessageFormat.format("query id: {0}", id));
if (e.getCause() instanceof NoResultsException) {
log.debug("Got a nested NoResultsException", e);
close(id);
// remember that we auto-closed this query
closedQueryCache.add(id);
} else {
try {
close(id);
} catch (Exception ce) {
log.error(qe, ce);
}
log.error(qe, e);
response.addException(qe.getBottomQueryException());
}
int statusCode = qe.getBottomQueryException().getStatusCode();
throw new DatawaveWebApplicationException(qe, response, statusCode);
} finally {
queryCache.unlock(id);
try {
if (ctx.getUserTransaction().getStatus() == Status.STATUS_MARKED_ROLLBACK) {
ctx.getUserTransaction().rollback();
} else if (ctx.getUserTransaction().getStatus() != Status.STATUS_NO_TRANSACTION) {
// no reason to commit if transaction not started, ie Query not found exception
ctx.getUserTransaction().commit();
}
} catch (IllegalStateException e) {
log.error("Error committing transaction: thread not associated with transaction", e);
} catch (RollbackException e) {
log.error("Error committing transaction: marked for rollback due to error", e);
} catch (HeuristicMixedException e) {
log.error("Error committing transaction: partial commit of resources", e);
} catch (HeuristicRollbackException e) {
log.error("Error committing transaction: resources rolled back transaction", e);
} catch (Exception e) {
log.error("Error committing transaction: Unknown error", e);
} finally {
// Stop timing on this trace, if any
if (span != null) {
span.stop();
}
}
}
// If applicable, perform a paged content lookup (i.e., not streamed), replacing its results in the returned response
if (null != contentLookupSettings) {
final NextContentCriteria criteria = new NextContentCriteria(id, contentLookupSettings);
response = this.lookupUUIDUtil.lookupContentByNextResponse(criteria, response);
}
return response;
}
Aggregations