Search in sources :

Example 11 with PageMetric

use of datawave.microservice.querymetric.BaseQueryMetric.PageMetric in project datawave by NationalSecurityAgency.

the class QueryMetricsReporter method getQueryTime.

private long getQueryTime(BaseQueryMetric metric) {
    long time = metric.getSetupTime();
    List<PageMetric> pageTimes = metric.getPageTimes();
    if (useAllQueryPages) {
        for (PageMetric pageTime : pageTimes) {
            time += pageTime.getReturnTime();
        }
    } else {
        if (pageTimes.size() >= 1) {
            time += pageTimes.get(0).getReturnTime();
        }
    }
    return time;
}
Also used : PageMetric(datawave.microservice.querymetric.BaseQueryMetric.PageMetric)

Example 12 with PageMetric

use of datawave.microservice.querymetric.BaseQueryMetric.PageMetric in project datawave by NationalSecurityAgency.

the class QueryExecutorBean method executeAsync.

@Asynchronous
public Future<?> executeAsync(String logicName, MultivaluedMap<String, String> queryParameters, Long startTime, Long loginTime, AsyncQueryStatusObserver observer) {
    Collection<String> proxyServers = null;
    Principal p = ctx.getCallerPrincipal();
    DatawavePrincipal dp;
    if (p instanceof DatawavePrincipal) {
        dp = (DatawavePrincipal) p;
        proxyServers = dp.getProxyServers();
    }
    long start = (startTime != null) ? startTime : System.nanoTime();
    GenericResponse<String> createResponse;
    try {
        createResponse = createQuery(logicName, queryParameters);
        observer.queryCreated(createResponse);
    } catch (DatawaveWebApplicationException e) {
        observer.queryCreateException(new QueryException(e));
        return new AsyncResult<>(e);
    }
    if (sessionContext.wasCancelCalled())
        return new AsyncResult<>("cancelled");
    long createCallTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
    final String queryId = createResponse.getResult();
    // We created the query and put into cache, get the RunningQuery object
    // and update the query metric for call time.
    final RunningQuery rq = queryCache.get(queryId);
    try {
        rq.getMetric().setCreateCallTime(createCallTime);
        if (loginTime != null) {
            rq.getMetric().setLoginTime(loginTime);
        }
        try {
            metrics.updateMetric(rq.getMetric());
        } catch (Exception e) {
            log.error("Error updating query metric", e);
        }
        boolean done = false;
        Span span = null;
        List<PageMetric> pageMetrics = rq.getMetric().getPageTimes();
        // If we get any exception, then break out of the loop and notify the observer about the problem.
        do {
            long callStart = System.nanoTime();
            rq.setActiveCall(true);
            try {
                BaseQueryResponse page = _next(rq, queryId, proxyServers, span);
                long serializationStart = System.nanoTime();
                observer.queryResultsAvailable(page);
                long serializationTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - serializationStart);
                if (rq.getLogic().getCollectQueryMetrics()) {
                    PageMetric pm = pageMetrics.get(pageMetrics.size() - 1);
                    pm.setSerializationTime(serializationTime);
                    long pageCallTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - callStart);
                    pm.setCallTime(pageCallTime);
                }
            } catch (Exception e) {
                if (e instanceof NoResultsException || e.getCause() instanceof NoResultsException) {
                    // No more results, break out of loop
                    done = true;
                } else if (sessionContext.wasCancelCalled() && e instanceof CancellationException) {
                    // We were cancelled by the originating user, so just break out of the loop.
                    // If we were cancelled due to an admin cancel, we'll report the exception to the user.
                    done = true;
                } else {
                    // We had a real problem. Update the query metric with the error and then notify the observer.
                    if (rq.getLogic().getCollectQueryMetrics()) {
                        rq.getMetric().setError(e);
                    }
                    QueryException qe = (e instanceof QueryException) ? (QueryException) e : new QueryException(e);
                    observer.queryException(qe);
                    done = true;
                    break;
                }
            } finally {
                rq.setActiveCall(false);
                // Update the query metrics for the completion of this page (either successfully or due to error)
                if (rq.getLogic().getCollectQueryMetrics()) {
                    try {
                        metrics.updateMetric(rq.getMetric());
                    } catch (Exception e) {
                        log.error("Error updating query metric", e);
                    }
                }
            }
        } while (!done && !sessionContext.wasCancelCalled());
    } finally {
        // Close the query now that we're done with it.
        try {
            close(rq);
        } catch (Exception e) {
            log.error("Error closing query", e);
        }
    }
    observer.queryFinished(queryId);
    return new AsyncResult<>(queryId);
}
Also used : NoResultsException(datawave.webservice.common.exception.NoResultsException) PageMetric(datawave.microservice.querymetric.BaseQueryMetric.PageMetric) Span(org.apache.accumulo.core.trace.Span) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) CancellationException(java.util.concurrent.CancellationException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) WebApplicationException(javax.ws.rs.WebApplicationException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) IOException(java.io.IOException) QueryException(datawave.webservice.query.exception.QueryException) BadRequestException(datawave.webservice.common.exception.BadRequestException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) JAXBException(javax.xml.bind.JAXBException) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) NoResultsException(datawave.webservice.common.exception.NoResultsException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RollbackException(javax.transaction.RollbackException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryException(datawave.webservice.query.exception.QueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) CancellationException(java.util.concurrent.CancellationException) BaseQueryResponse(datawave.webservice.result.BaseQueryResponse) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) AsyncResult(javax.ejb.AsyncResult) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Asynchronous(javax.ejb.Asynchronous)

Example 13 with PageMetric

use of datawave.microservice.querymetric.BaseQueryMetric.PageMetric in project datawave by NationalSecurityAgency.

the class QueryMetricsEnrichmentInterceptor method aroundWriteTo.

@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
    ResponseMethodStats stats = doWrite(context);
    QueryCall queryCall = (QueryCall) context.getProperty(QueryCall.class.getName());
    if (queryCall != null) {
        if (queryCache != null) {
            RunningQuery rq = queryCache.get(queryCall.queryID);
            if (rq != null) {
                BaseQueryLogic<?> baseLogic = null;
                QueryLogic<?> logic = rq.getLogic();
                if (logic != null && logic instanceof BaseQueryLogic) {
                    baseLogic = (BaseQueryLogic) logic;
                }
                if (baseLogic != null && baseLogic.getCollectQueryMetrics()) {
                    try {
                        BaseQueryMetric metric = rq.getMetric();
                        switch(queryCall.methodType) {
                            case CREATE:
                                metric.setCreateCallTime(stats.getCallTime());
                                metric.setLoginTime(stats.getLoginTime());
                                break;
                            case CREATE_AND_NEXT:
                                metric.setCreateCallTime(stats.getCallTime());
                                metric.setLoginTime(stats.getLoginTime());
                                List<PageMetric> pageTimes = metric.getPageTimes();
                                if (pageTimes != null && !pageTimes.isEmpty()) {
                                    PageMetric pm = pageTimes.get(pageTimes.size() - 1);
                                    pm.setCallTime(stats.getCallTime());
                                    pm.setLoginTime(stats.getLoginTime());
                                    pm.setSerializationTime(stats.getSerializationTime());
                                    pm.setBytesWritten(stats.getBytesWritten());
                                }
                                break;
                            case NEXT:
                                pageTimes = metric.getPageTimes();
                                if (pageTimes != null && !pageTimes.isEmpty()) {
                                    PageMetric pm = pageTimes.get(pageTimes.size() - 1);
                                    pm.setCallTime(stats.getCallTime());
                                    pm.setLoginTime(stats.getLoginTime());
                                    pm.setSerializationTime(stats.getSerializationTime());
                                    pm.setBytesWritten(stats.getBytesWritten());
                                }
                                break;
                        }
                        if (queryMetricsBean != null)
                            queryMetricsBean.updateMetric(metric);
                        else
                            log.error("QueryMetricsBean JNDI lookup returned null");
                    } catch (Exception e) {
                        log.error("Unable to record metrics for " + queryCall.methodType + " method: " + e.getLocalizedMessage(), e);
                    }
                }
            } else {
                log.error("RunningQuery instance not in the cache!, queryId: " + queryCall.queryID);
            }
        } else {
            log.error("Query cache not injected! No metrics will be recorded for serialization times.");
        }
    }
}
Also used : RunningQuery(datawave.webservice.query.runner.RunningQuery) PageMetric(datawave.microservice.querymetric.BaseQueryMetric.PageMetric) BaseQueryMetric(datawave.microservice.querymetric.BaseQueryMetric) BaseQueryLogic(datawave.webservice.query.logic.BaseQueryLogic) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 14 with PageMetric

use of datawave.microservice.querymetric.BaseQueryMetric.PageMetric in project datawave-query-metric-service by NationalSecurityAgency.

the class QueryMetricTest method testPageMetricParsing2.

@Test
public void testPageMetricParsing2() {
    PageMetric pmRef1 = new PageMetric(null, "aa-bb-cc-dd", 2500, 2000, 3500, 3600, 1000, 2200, 3000, 10000);
    // /pageUuid/pageSize/returnTime/callTime/serializationTime/bytesWritten/pageRequested/pageReturned/loginTime
    String pmText1 = "/aa-bb-cc-dd/2500/2000/2200/3000/10000/3500/3600/1000";
    PageMetric pm1 = PageMetric.parse(pmText1);
    assertEquals("page metrics not equal", pmRef1, pm1);
}
Also used : PageMetric(datawave.microservice.querymetric.BaseQueryMetric.PageMetric) Test(org.junit.Test)

Example 15 with PageMetric

use of datawave.microservice.querymetric.BaseQueryMetric.PageMetric in project datawave-query-metric-service by NationalSecurityAgency.

the class QueryMetricTest method setup.

@BeforeClass
public static void setup() {
    queryMetric = new QueryMetric();
    markings = new HashMap<>();
    markings.put(MarkingFunctions.Default.COLUMN_VISIBILITY, "PUBLIC");
    queryMetric.setMarkings(markings);
    negativeSelectors = new ArrayList<>();
    negativeSelectors.add("negativeSelector1");
    positiveSelectors = new ArrayList<>();
    positiveSelectors.add("positiveSelector1");
    pageTimes = new ArrayList<>();
    PageMetric pageMetric = new PageMetric();
    pageMetric.setCallTime(0);
    pageTimes.add(pageMetric);
    proxyServers = new ArrayList<>();
    proxyServers.add("proxyServer1");
}
Also used : PageMetric(datawave.microservice.querymetric.BaseQueryMetric.PageMetric) BeforeClass(org.junit.BeforeClass)

Aggregations

PageMetric (datawave.microservice.querymetric.BaseQueryMetric.PageMetric)17 Date (java.util.Date)4 Test (org.junit.Test)4 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)3 SimpleDateFormat (java.text.SimpleDateFormat)3 ArrayList (java.util.ArrayList)3 TreeMap (java.util.TreeMap)3 BaseQueryMetric (datawave.microservice.querymetric.BaseQueryMetric)2 Prediction (datawave.microservice.querymetric.BaseQueryMetric.Prediction)2 IOException (java.io.IOException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 BadRequestException (datawave.webservice.common.exception.BadRequestException)1 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)1 NoResultsException (datawave.webservice.common.exception.NoResultsException)1 UnauthorizedException (datawave.webservice.common.exception.UnauthorizedException)1 Query (datawave.webservice.query.Query)1 Parameter (datawave.webservice.query.QueryImpl.Parameter)1 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)1 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)1 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)1