Search in sources :

Example 1 with ResultsPage

use of datawave.webservice.query.cache.ResultsPage in project datawave by NationalSecurityAgency.

the class CachedResultsBean method getRows.

/**
 * Returns a set of rows based on the given starting and end positions. The response object type is dynamic, see the listQueryLogic operation to determine
 * what the response type object will be.
 *
 * @param queryId
 *            CachedResults queryId
 * @param rowBegin
 *            first row to be returned
 * @param rowEnd
 *            last row to be returned
 * @return datawave.webservice.result.BaseQueryResponse
 * @RequestHeader X-ProxiedEntitiesChain use when proxying request for user by specifying a chain of DNs of the identities to proxy
 * @RequestHeader X-ProxiedIssuersChain required when using X-ProxiedEntitiesChain, specify one issuer DN per subject DN listed in X-ProxiedEntitiesChain
 * @RequestHeader query-session-id session id value used for load balancing purposes. query-session-id can be placed in the request in a Cookie header or as
 *                a query parameter
 * @ResponseHeader X-OperationTimeInMS time spent on the server performing the operation, does not account for network or result serialization
 * @ResponseHeader X-Partial-Results true if the page contains less than the requested number of results
 *
 * @HTTP 200 success
 * @HTTP 401 caller is not authorized to run the query
 * @HTTP 412 if the query is not active
 * @HTTP 500 internal server error
 */
@GET
@javax.ws.rs.Path("/{queryId}/getRows")
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf" })
@GZIP
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
@Timed(name = "dw.cachedr.getRows", absolute = true)
public BaseQueryResponse getRows(@PathParam("queryId") @Required("queryId") String queryId, @QueryParam("rowBegin") @DefaultValue("1") Integer rowBegin, @QueryParam("rowEnd") Integer rowEnd) {
    BaseQueryResponse response = responseObjectFactory.getEventQueryResponse();
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String owner = getOwnerFromPrincipal(p);
    try {
        if (rowBegin < 1) {
            throw new BadRequestQueryException(DatawaveErrorCode.ROW_BEGIN_LESS_THAN_1);
        }
        if (rowEnd != null && rowEnd < rowBegin) {
            throw new BadRequestQueryException(DatawaveErrorCode.ROW_END_LESS_THAN_ROW_BEGIN);
        }
        // If there is a this.maxPageSize set, then we should honor it here. Otherwise, we use Integer.MAX_VALUE
        int maxPageSize = cachedResultsConfiguration.getMaxPageSize();
        if (rowEnd == null) {
            if (maxPageSize > 0) {
                rowEnd = (rowBegin + maxPageSize) - 1;
            } else {
                rowEnd = Integer.MAX_VALUE;
            }
        }
        int pagesize = (rowEnd - rowBegin) + 1;
        if (maxPageSize > 0 && pagesize > maxPageSize) {
            throw new QueryException(DatawaveErrorCode.TOO_MANY_ROWS_REQUESTED, MessageFormat.format("Size must be less than or equal to: {0}", maxPageSize));
        }
        CachedRunningQuery crq = null;
        try {
            // Get the CachedRunningQuery object from the cache
            try {
                crq = retrieve(queryId, owner);
            } catch (IOException e) {
                throw new PreConditionFailedQueryException(DatawaveErrorCode.CACHED_RESULTS_IMPORT_ERROR, e);
            }
            if (null == crq)
                throw new PreConditionFailedQueryException(DatawaveErrorCode.QUERY_NOT_CACHED);
            response = crq.getQueryLogic().getResponseObjectFactory().getEventQueryResponse();
            if (!crq.getUser().equals(owner)) {
                throw new UnauthorizedQueryException(DatawaveErrorCode.QUERY_OWNER_MISMATCH, MessageFormat.format("{0} != {1}", crq.getUser(), owner));
            }
            synchronized (crq) {
                if (crq.isActivated() == false) {
                    Connection connection = ds.getConnection();
                    String logicName = crq.getQueryLogicName();
                    QueryLogic<?> queryLogic = queryFactory.getQueryLogic(logicName, p);
                    crq.activate(connection, queryLogic);
                }
                try {
                    ResultsPage resultList = crq.getRows(rowBegin, rowEnd, cachedResultsConfiguration.getPageByteTrigger());
                    response = crq.getTransformer().createResponse(resultList);
                    Status status;
                    if (!resultList.getResults().isEmpty()) {
                        response.setHasResults(true);
                        status = Status.OK;
                    } else {
                        response.setHasResults(false);
                        status = Status.NO_CONTENT;
                    }
                    response.setLogicName(crq.getQueryLogic().getLogicName());
                    response.setQueryId(crq.getQueryId());
                    if (response instanceof TotalResultsAware) {
                        ((TotalResultsAware) response).setTotalResults(crq.getTotalRows());
                    }
                    if (status == Status.NO_CONTENT) {
                        throw new NoResultsQueryException(DatawaveErrorCode.NO_CONTENT_STATUS);
                    }
                    crq.getMetric().setLifecycle(QueryMetric.Lifecycle.RESULTS);
                    return response;
                } catch (SQLException e) {
                    throw new QueryException();
                } catch (NoResultsQueryException e) {
                    throw e;
                } catch (RuntimeException e) {
                    log.error(e.getMessage(), e);
                    throw e;
                } finally {
                    crq.closeConnection(log);
                }
            }
        } finally {
            // Push metrics
            try {
                if (null != crq && crq.getQueryLogic().getCollectQueryMetrics() == true) {
                    metrics.updateMetric(crq.getMetric());
                }
            } catch (Exception e1) {
                log.error("Error updating metrics", e1);
            }
        }
    } catch (Exception e) {
        DatawaveErrorCode dec;
        if (e instanceof NoResultsQueryException) {
            dec = DatawaveErrorCode.NO_CONTENT_STATUS;
        } else {
            dec = DatawaveErrorCode.CACHED_QUERY_TRANSACTION_ERROR;
        }
        QueryException qe = new QueryException(dec, e);
        log.error(qe);
        response.addException(qe.getBottomQueryException());
        int statusCode = qe.getBottomQueryException().getStatusCode();
        throw new DatawaveWebApplicationException(qe, response, statusCode);
    }
}
Also used : FileStatus(org.apache.hadoop.fs.FileStatus) Status(javax.ws.rs.core.Response.Status) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultsPage(datawave.webservice.query.cache.ResultsPage) IOException(java.io.IOException) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) EJBException(javax.ejb.EJBException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) BatchUpdateException(java.sql.BatchUpdateException) SQLException(java.sql.SQLException) IOException(java.io.IOException) QueryCanceledQueryException(datawave.webservice.query.exception.QueryCanceledQueryException) QueryException(datawave.webservice.query.exception.QueryException) QueryCanceledException(datawave.webservice.common.exception.QueryCanceledException) PreConditionFailedException(datawave.webservice.common.exception.PreConditionFailedException) NotFoundException(datawave.webservice.common.exception.NotFoundException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) NoResultsException(datawave.webservice.common.exception.NoResultsException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) MalformedURLException(java.net.MalformedURLException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryCanceledQueryException(datawave.webservice.query.exception.QueryCanceledQueryException) QueryException(datawave.webservice.query.exception.QueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) TotalResultsAware(datawave.webservice.result.TotalResultsAware) DatawaveErrorCode(datawave.webservice.query.exception.DatawaveErrorCode) BaseQueryResponse(datawave.webservice.result.BaseQueryResponse) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Interceptors(javax.interceptor.Interceptors) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 2 with ResultsPage

use of datawave.webservice.query.cache.ResultsPage in project datawave by NationalSecurityAgency.

the class CachedResultsBean method previous.

/**
 * Returns the previous page of results to the caller. The response object type is dynamic, see the listQueryLogic operation to determine what the response
 * type object will be.
 *
 * @param queryId
 *            user defined id for this query
 * @return previous page of results
 *
 * @return datawave.webservice.result.BaseQueryResponse
 * @RequestHeader X-ProxiedEntitiesChain use when proxying request for user by specifying a chain of DNs of the identities to proxy
 * @RequestHeader X-ProxiedIssuersChain required when using X-ProxiedEntitiesChain, specify one issuer DN per subject DN listed in X-ProxiedEntitiesChain
 * @RequestHeader query-session-id session id value used for load balancing purposes. query-session-id can be placed in the request in a Cookie header or as
 *                a query parameter
 * @ResponseHeader X-OperationTimeInMS time spent on the server performing the operation, does not account for network or result serialization
 * @ResponseHeader X-query-page-number page number returned by this call
 * @ResponseHeader X-query-last-page if true then there are no more pages for this query, caller should call close()
 * @ResponseHeader X-Partial-Results true if the page contains less than the requested number of results
 *
 * @HTTP 200 success
 * @HTTP 401 caller is not authorized to run the query
 * @HTTP 412 if the query is not active
 * @HTTP 500 internal server error
 */
@GET
@javax.ws.rs.Path("/{queryId}/previous")
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf" })
@GZIP
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
@Timed(name = "dw.cachedr.previous", absolute = true)
public BaseQueryResponse previous(@PathParam("queryId") @Required("queryId") String queryId) {
    BaseQueryResponse response = responseObjectFactory.getEventQueryResponse();
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String owner = getOwnerFromPrincipal(p);
    try {
        CachedRunningQuery crq = null;
        try {
            // Get the CachedRunningQuery object from the cache
            try {
                crq = retrieve(queryId, owner);
            } catch (IOException e) {
                throw new PreConditionFailedQueryException(DatawaveErrorCode.CACHED_RESULTS_IMPORT_ERROR, e);
            }
            if (null == crq)
                throw new PreConditionFailedQueryException(DatawaveErrorCode.QUERY_NOT_CACHED);
            if (!crq.getUser().equals(owner)) {
                throw new UnauthorizedQueryException(DatawaveErrorCode.QUERY_OWNER_MISMATCH, MessageFormat.format("{0} != {1}", crq.getUser(), owner));
            }
            synchronized (crq) {
                if (crq.isActivated() == false) {
                    if (crq.getShouldAutoActivate()) {
                        Connection connection = ds.getConnection();
                        String logicName = crq.getQueryLogicName();
                        QueryLogic<?> queryLogic = queryFactory.getQueryLogic(logicName, p);
                        crq.activate(connection, queryLogic);
                    } else {
                        throw new PreConditionFailedQueryException(DatawaveErrorCode.QUERY_TIMEOUT_FOR_RESOURCES);
                    }
                }
                try {
                    ResultsPage resultList = crq.previous(cachedResultsConfiguration.getPageByteTrigger());
                    long pageNum = crq.getLastPageNumber();
                    response = crq.getTransformer().createResponse(resultList);
                    Status status = null;
                    if (!resultList.getResults().isEmpty()) {
                        response.setHasResults(true);
                        status = Status.OK;
                    } else {
                        response.setHasResults(false);
                        status = Status.NO_CONTENT;
                    }
                    response.setPageNumber(pageNum);
                    response.setLogicName(crq.getQueryLogic().getLogicName());
                    response.setQueryId(crq.getQueryId());
                    if (response instanceof TotalResultsAware) {
                        ((TotalResultsAware) response).setTotalResults(crq.getTotalRows());
                    }
                    if (status == Status.NO_CONTENT) {
                        throw new NoResultsException(null);
                    }
                    crq.getMetric().setLifecycle(QueryMetric.Lifecycle.RESULTS);
                    return response;
                } catch (SQLException e) {
                    throw new QueryException(DatawaveErrorCode.CACHED_QUERY_SQL_ERROR, e);
                }
            }
        } finally {
            // Push metrics
            if (null != crq && crq.getQueryLogic().getCollectQueryMetrics() == true) {
                try {
                    metrics.updateMetric(crq.getMetric());
                } catch (Exception e1) {
                    log.error("Error updating metrics", e1);
                }
            }
        }
    } catch (Exception e) {
        QueryException qe = null;
        if (e instanceof NoResultsException) {
            qe = new QueryException(DatawaveErrorCode.NO_CONTENT_STATUS, e);
        } else {
            qe = new QueryException("Error calling previous.", e, "500-42");
            log.error(qe);
        }
        response.addException(qe.getBottomQueryException());
        int statusCode = qe.getBottomQueryException().getStatusCode();
        throw new DatawaveWebApplicationException(qe, response, statusCode);
    }
}
Also used : FileStatus(org.apache.hadoop.fs.FileStatus) Status(javax.ws.rs.core.Response.Status) NoResultsException(datawave.webservice.common.exception.NoResultsException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultsPage(datawave.webservice.query.cache.ResultsPage) IOException(java.io.IOException) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) EJBException(javax.ejb.EJBException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) BatchUpdateException(java.sql.BatchUpdateException) SQLException(java.sql.SQLException) IOException(java.io.IOException) QueryCanceledQueryException(datawave.webservice.query.exception.QueryCanceledQueryException) QueryException(datawave.webservice.query.exception.QueryException) QueryCanceledException(datawave.webservice.common.exception.QueryCanceledException) PreConditionFailedException(datawave.webservice.common.exception.PreConditionFailedException) NotFoundException(datawave.webservice.common.exception.NotFoundException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) NoResultsException(datawave.webservice.common.exception.NoResultsException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) MalformedURLException(java.net.MalformedURLException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryCanceledQueryException(datawave.webservice.query.exception.QueryCanceledQueryException) QueryException(datawave.webservice.query.exception.QueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) TotalResultsAware(datawave.webservice.result.TotalResultsAware) BaseQueryResponse(datawave.webservice.result.BaseQueryResponse) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Interceptors(javax.interceptor.Interceptors) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 3 with ResultsPage

use of datawave.webservice.query.cache.ResultsPage in project datawave by NationalSecurityAgency.

the class CachedRunningQuery method convert.

/**
 * Convert the cached row set into a result list.
 *
 * @param cachedRowSet
 * @param pageByteTrigger
 * @return
 */
private ResultsPage convert(CachedRowSet cachedRowSet, long pageByteTrigger) {
    boolean hitPageByteTrigger = false;
    List<CacheableQueryRow> cacheableQueryRowList = new ArrayList<>();
    try {
        cachedRowSet.beforeFirst();
        long resultBytes = 0;
        while (cachedRowSet.next() && !hitPageByteTrigger) {
            CacheableQueryRow row = CacheableQueryRowReader.createRow(cachedRowSet, this.fixedFieldsInEvent);
            cacheableQueryRowList.add(row);
            if (pageByteTrigger != 0) {
                resultBytes += ObjectSizeOf.Sizer.getObjectSize(row);
                if (resultBytes >= pageByteTrigger) {
                    hitPageByteTrigger = true;
                }
            }
        }
    } catch (SQLException | RuntimeException e) {
        log.error(e.getMessage(), e);
    }
    if (this.cacheableLogic == null) {
        return new ResultsPage();
    } else {
        return new ResultsPage(this.cacheableLogic.readFromCache(cacheableQueryRowList), (hitPageByteTrigger ? ResultsPage.Status.PARTIAL : ResultsPage.Status.COMPLETE));
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) CacheableQueryRow(datawave.webservice.query.cachedresults.CacheableQueryRow) ResultsPage(datawave.webservice.query.cache.ResultsPage)

Example 4 with ResultsPage

use of datawave.webservice.query.cache.ResultsPage in project datawave by NationalSecurityAgency.

the class CachedRunningQuery method next.

/**
 * Return the next page of results
 *
 * @return next page of results
 * @throws SQLException
 */
public ResultsPage next(long pageByteTrigger) throws SQLException {
    // update timestamp in case this operation takes a long time.
    updateTimestamp();
    // only auto-activate before the first call to next/previous
    this.shouldAutoActivate = false;
    long pageStartTime = System.currentTimeMillis();
    if (currentRow == position.BEFORE_FIRST) {
        this.lastPageNumber = 0;
    }
    ResultsPage resultList = new ResultsPage();
    if (nextPageOfResults()) {
        resultList = convert(this.crs, pageByteTrigger);
    }
    if (!resultList.getResults().isEmpty()) {
        currentRow = position.MIDDLE;
        this.lastPageNumber++;
    } else {
        currentRow = position.AFTER_LAST;
        this.lastPageNumber = ((int) Math.ceil((float) this.totalRows / (float) this.pagesize)) + 1;
    }
    // Update the metric
    long now = System.currentTimeMillis();
    this.getMetric().addPageTime(resultList.getResults().size(), (now - pageStartTime), pageStartTime, now);
    updateTimestamp();
    return resultList;
}
Also used : ResultsPage(datawave.webservice.query.cache.ResultsPage)

Example 5 with ResultsPage

use of datawave.webservice.query.cache.ResultsPage in project datawave by NationalSecurityAgency.

the class BulkResultsFileOutputMapper method map.

@Override
protected void map(Key key, Value value, org.apache.hadoop.mapreduce.Mapper<Key, Value, Key, Value>.Context context) throws IOException, InterruptedException {
    entries.clear();
    entries.put(key, value);
    for (Entry<Key, Value> entry : entries.entrySet()) {
        try {
            Object o = t.transform(entry);
            BaseQueryResponse response = t.createResponse(new ResultsPage(Collections.singletonList(o)));
            Class<? extends BaseQueryResponse> responseClass = null;
            try {
                responseClass = getResponseClass(response.getClass().getName());
            } catch (ClassNotFoundException e) {
                throw new RuntimeException("Unable to find response class: " + response.getClass().getName(), e);
            }
            try {
                Value val = serializeResponse(responseClass, response, this.format);
                // Write out the original key and the new value.
                if (context.getOutputKeyClass() == null || context.getOutputKeyClass().equals(NullWritable.class)) {
                    // don't write the key in this case, write only the value
                    key = null;
                } else {
                    // to preserve whatever the reason was for this wrapping of the key in the original code
                    key = new Key(key);
                }
                context.write(key, val);
            } catch (Exception e) {
                throw new RuntimeException("Unable to serialize response of class: " + response.getClass().getName(), e);
            }
            context.progress();
        } catch (EmptyObjectException e) {
        // not yet done, so continue fetching next
        }
    }
}
Also used : EmptyObjectException(datawave.webservice.query.exception.EmptyObjectException) Value(org.apache.accumulo.core.data.Value) BaseQueryResponse(datawave.webservice.result.BaseQueryResponse) ResultsPage(datawave.webservice.query.cache.ResultsPage) NullWritable(org.apache.hadoop.io.NullWritable) Key(org.apache.accumulo.core.data.Key) EmptyObjectException(datawave.webservice.query.exception.EmptyObjectException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException)

Aggregations

ResultsPage (datawave.webservice.query.cache.ResultsPage)23 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)9 Test (org.junit.Test)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 NoResultsException (datawave.webservice.common.exception.NoResultsException)6 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)6 PreConditionFailedQueryException (datawave.webservice.query.exception.PreConditionFailedQueryException)6 QueryException (datawave.webservice.query.exception.QueryException)6 BaseQueryResponse (datawave.webservice.result.BaseQueryResponse)6 SQLException (java.sql.SQLException)6 Date (java.util.Date)6 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)5 UnauthorizedException (datawave.webservice.common.exception.UnauthorizedException)5 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)5 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)5 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)5 Principal (java.security.Principal)5 Timed (com.codahale.metrics.annotation.Timed)4 NotFoundException (datawave.webservice.common.exception.NotFoundException)4