Search in sources :

Example 16 with ResultsPage

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

the class ExtendedRunningQueryTest method testNext_NoResultsAfterCancellationUsingDeprecatedConstructor.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testNext_NoResultsAfterCancellationUsingDeprecatedConstructor() throws Exception {
    // Set local test input
    String userDN = "userDN";
    String userSid = "userSid";
    List<String> dnList = Lists.newArrayList(userDN);
    UUID queryId = UUID.randomUUID();
    String query = "query";
    String queryLogicName = "queryLogicName";
    String queryName = "queryName";
    long currentTime = System.currentTimeMillis();
    Date beginDate = new Date(currentTime - 5000);
    Date endDate = new Date(currentTime - 1000);
    String methodAuths = "AUTH_1";
    String columnVisibility = "AUTH_1";
    DatawaveUser user = new DatawaveUser(SubjectIssuerDNPair.of("userDN", "issuerDN"), UserType.USER, Collections.singleton(methodAuths), null, null, 0L);
    DatawavePrincipal principal = new DatawavePrincipal(Collections.singletonList(user));
    long maxResults = 100L;
    // Set expectations
    expect(this.queryLogic.getCollectQueryMetrics()).andReturn(true);
    expect(this.query.getUncaughtExceptionHandler()).andReturn(exceptionHandler).times(3);
    expect(this.exceptionHandler.getThrowable()).andReturn(null).times(3);
    expect(this.query.getId()).andReturn(queryId).times(3);
    expect(this.query.getUserDN()).andReturn(userDN).times(3);
    expect(this.query.getDnList()).andReturn(dnList);
    expect(this.query.getOwner()).andReturn(userSid);
    expect(this.query.getQuery()).andReturn(query);
    expect(this.query.getQueryLogicName()).andReturn(queryLogicName);
    expect(this.query.getQueryName()).andReturn(queryName);
    expect(this.query.getBeginDate()).andReturn(beginDate);
    expect(this.query.getEndDate()).andReturn(endDate);
    expect(this.query.getParameters()).andReturn(new HashSet<>());
    expect(this.query.getQueryAuthorizations()).andReturn(methodAuths);
    expect(this.query.getColumnVisibility()).andReturn(columnVisibility);
    expect(this.queryLogic.initialize(eq(this.connector), eq(this.query), isA(Set.class))).andReturn(this.genericConfiguration);
    this.queryLogic.setupQuery(this.genericConfiguration);
    this.queryMetrics.updateMetric(isA(QueryMetric.class));
    PowerMock.expectLastCall().times(3);
    expect(this.queryLogic.getTransformIterator(this.query)).andReturn(this.transformIterator);
    expect(this.transformIterator.hasNext()).andReturn(true);
    expect(this.genericConfiguration.getQueryString()).andReturn("query").once();
    expect(this.queryLogic.getResultLimit(eq(dnList))).andReturn(maxResults);
    expect(this.queryLogic.getMaxResults()).andReturn(maxResults);
    // Run the test
    PowerMock.replayAll();
    RunningQuery subject = new RunningQuery(this.queryMetrics, this.connector, Priority.NORMAL, this.queryLogic, this.query, methodAuths, principal, new QueryMetricFactoryImpl());
    subject.cancel();
    boolean result1 = subject.isCanceled();
    ResultsPage result2 = subject.next();
    PowerMock.verifyAll();
    // Verify results
    assertTrue("Expected isCanceled() to return true", result1);
    assertNotNull("Expected a non-null page", result2);
    assertNotNull("Expected a non-null list of results", result2.getResults());
    assertTrue("Expected an empty list of results", result2.getResults().isEmpty());
    assertSame("Expected status to be cancelled", QueryMetric.Lifecycle.CANCELLED, subject.getMetric().getLifecycle());
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) DatawaveUser(datawave.security.authorization.DatawaveUser) ResultsPage(datawave.webservice.query.cache.ResultsPage) Date(java.util.Date) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) QueryMetric(datawave.microservice.querymetric.QueryMetric) UUID(java.util.UUID) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) Test(org.junit.Test)

Example 17 with ResultsPage

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

the class ExtendedRunningQueryTest method testNextWithDnResultLimit_HappyPathUsingDeprecatedConstructor.

@SuppressWarnings({ "unchecked" })
@Test
public void testNextWithDnResultLimit_HappyPathUsingDeprecatedConstructor() throws Exception {
    // Set local test input
    String userDN = "userDN";
    List<String> dnList = Lists.newArrayList(userDN);
    String userSid = "userSid";
    UUID queryId = UUID.randomUUID();
    String methodAuths = "AUTH_1";
    String columnVisibility = "AUTH_1";
    DatawaveUser user = new DatawaveUser(SubjectIssuerDNPair.of("userDN", "issuerDN"), UserType.USER, Collections.singleton(methodAuths), null, null, 0L);
    DatawavePrincipal principal = new DatawavePrincipal(Collections.singletonList(user));
    String query = "query";
    String queryLogicName = "queryLogicName";
    String queryName = "queryName";
    long currentTime = System.currentTimeMillis();
    Date beginDate = new Date(currentTime - 5000);
    Date endDate = new Date(currentTime - 1000);
    Date expirationDate = new Date(currentTime + 9999);
    int pageSize = 5;
    int maxPageSize = 5;
    long pageByteTrigger = 4 * 1024L;
    long maxWork = Long.MAX_VALUE;
    long maxResults = 10L;
    long dnResultLimit = 2L;
    List<Object> resultObjects = Arrays.asList(new Object(), "resultObject1", "resultObject2", "resultObject3", "resultObject4", "resultObject5");
    // Set expectations
    expect(this.queryLogic.getCollectQueryMetrics()).andReturn(true);
    expect(this.query.getUncaughtExceptionHandler()).andReturn(exceptionHandler).times(5);
    expect(this.exceptionHandler.getThrowable()).andReturn(null).times(5);
    expect(this.query.getId()).andReturn(queryId).times(4);
    expect(this.query.getOwner()).andReturn(userSid).times(2);
    expect(this.query.getQuery()).andReturn(query).times(2);
    expect(this.query.getQueryLogicName()).andReturn(queryLogicName).times(2);
    expect(this.query.getQueryName()).andReturn(queryName).times(2);
    expect(this.query.getBeginDate()).andReturn(beginDate).times(2);
    expect(this.query.getEndDate()).andReturn(endDate).times(2);
    expect(this.query.isMaxResultsOverridden()).andReturn(false).anyTimes();
    expect(this.query.getExpirationDate()).andReturn(expirationDate);
    expect(this.query.getParameters()).andReturn(new HashSet<>()).times(2);
    expect(this.query.getQueryAuthorizations()).andReturn(methodAuths).times(2);
    expect(this.query.getUserDN()).andReturn(userDN).times(4);
    expect(this.query.getColumnVisibility()).andReturn(columnVisibility);
    expect(this.query.getDnList()).andReturn(dnList);
    expect(this.queryLogic.initialize(eq(this.connector), eq(this.query), isA(Set.class))).andReturn(this.genericConfiguration);
    this.queryLogic.setupQuery(this.genericConfiguration);
    expect(this.queryLogic.getTransformIterator(this.query)).andReturn(this.transformIterator);
    expect(this.queryLogic.getResultLimit(eq(dnList))).andReturn(dnResultLimit);
    Iterator<Object> iterator = resultObjects.iterator();
    int count = 0;
    expect(this.transformIterator.hasNext()).andReturn(iterator.hasNext());
    while (iterator.hasNext() && count < dnResultLimit) {
        expect(this.transformIterator.hasNext()).andReturn(iterator.hasNext());
        expect(this.transformIterator.next()).andReturn(iterator.next());
        count++;
    }
    expect(this.transformIterator.getTransformer()).andReturn(transformer).times(count);
    expect(this.query.getPagesize()).andReturn(pageSize).anyTimes();
    expect(this.queryLogic.getMaxPageSize()).andReturn(maxPageSize).anyTimes();
    expect(this.queryLogic.getPageByteTrigger()).andReturn(pageByteTrigger).anyTimes();
    expect(this.queryLogic.getMaxWork()).andReturn(maxWork).anyTimes();
    expect(this.queryLogic.getMaxResults()).andReturn(maxResults).anyTimes();
    expect(this.genericConfiguration.getQueryString()).andReturn(query).once();
    // Run the test
    PowerMock.replayAll();
    RunningQuery subject = new RunningQuery(this.connector, Priority.NORMAL, this.queryLogic, this.query, methodAuths, principal, new QueryMetricFactoryImpl());
    ResultsPage result1 = subject.next();
    String result2 = subject.toString();
    QueryMetric.Lifecycle status = subject.getMetric().getLifecycle();
    PowerMock.verifyAll();
    // Verify results
    assertNotNull("Expected a non-null page", result1);
    assertNotNull("Expected a non-null list of results", result1.getResults());
    assertTrue("Expected DN max results non-null items in the list of results", resultObjects.size() > dnResultLimit);
    assertSame("Expected status to be MAXRESULTS", status, QueryMetric.Lifecycle.MAXRESULTS);
    assertNotNull("Expected a non-null toString() representation", result2);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) DatawaveUser(datawave.security.authorization.DatawaveUser) ResultsPage(datawave.webservice.query.cache.ResultsPage) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Date(java.util.Date) QueryMetric(datawave.microservice.querymetric.QueryMetric) UUID(java.util.UUID) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with ResultsPage

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

the class ExtendedRunningQueryTest method testNextMaxResults_HappyPathUsingDeprecatedConstructor.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testNextMaxResults_HappyPathUsingDeprecatedConstructor() throws Exception {
    // Set local test input
    String userDN = "userDN";
    List<String> dnList = Lists.newArrayList(userDN);
    String userSid = "userSid";
    UUID queryId = UUID.randomUUID();
    String methodAuths = "AUTH_1";
    String columnVisibility = "AUTH_1";
    DatawaveUser user = new DatawaveUser(SubjectIssuerDNPair.of("userDN", "issuerDN"), UserType.USER, Collections.singleton(methodAuths), null, null, 0L);
    DatawavePrincipal principal = new DatawavePrincipal(Collections.singletonList(user));
    String query = "query";
    String queryLogicName = "queryLogicName";
    String queryName = "queryName";
    long currentTime = System.currentTimeMillis();
    Date beginDate = new Date(currentTime - 5000);
    Date endDate = new Date(currentTime - 1000);
    Date expirationDate = new Date(currentTime + 9999);
    int pageSize = 5;
    int maxPageSize = 5;
    long pageByteTrigger = 4 * 1024L;
    long maxWork = Long.MAX_VALUE;
    long maxResults = 4L;
    List<Object> resultObjects = Arrays.asList(new Object(), "resultObject1", "resultObject2", "resultObject3", "resultObject4", "resultObject5");
    // Set expectations
    expect(this.queryLogic.getCollectQueryMetrics()).andReturn(true);
    expect(this.query.getUncaughtExceptionHandler()).andReturn(exceptionHandler).times(7);
    expect(this.exceptionHandler.getThrowable()).andReturn(null).times(7);
    expect(this.query.getId()).andReturn(queryId).times(4);
    expect(this.query.getOwner()).andReturn(userSid).times(2);
    expect(this.query.getQuery()).andReturn(query).times(2);
    expect(this.query.getQueryLogicName()).andReturn(queryLogicName).times(2);
    expect(this.query.getQueryName()).andReturn(queryName).times(2);
    expect(this.query.getBeginDate()).andReturn(beginDate).times(2);
    expect(this.query.getEndDate()).andReturn(endDate).times(2);
    expect(this.query.isMaxResultsOverridden()).andReturn(false).anyTimes();
    expect(this.query.getExpirationDate()).andReturn(expirationDate);
    expect(this.query.getParameters()).andReturn(new HashSet<>()).times(2);
    expect(this.query.getQueryAuthorizations()).andReturn(methodAuths).times(2);
    expect(this.query.getUserDN()).andReturn(userDN).times(3);
    expect(this.query.getColumnVisibility()).andReturn(columnVisibility);
    expect(this.query.getDnList()).andReturn(dnList);
    expect(this.queryLogic.initialize(eq(this.connector), eq(this.query), isA(Set.class))).andReturn(this.genericConfiguration);
    this.queryLogic.setupQuery(this.genericConfiguration);
    expect(this.queryLogic.getTransformIterator(this.query)).andReturn(this.transformIterator);
    expect(this.queryLogic.getResultLimit(eq(dnList))).andReturn(maxResults);
    Iterator<Object> iterator = resultObjects.iterator();
    int count = 0;
    expect(this.transformIterator.hasNext()).andReturn(iterator.hasNext());
    while (iterator.hasNext() && count < maxResults) {
        expect(this.transformIterator.hasNext()).andReturn(iterator.hasNext());
        expect(this.transformIterator.next()).andReturn(iterator.next());
        count++;
    }
    expect(this.transformIterator.getTransformer()).andReturn(transformer).times(count);
    expect(this.query.getPagesize()).andReturn(pageSize).anyTimes();
    expect(this.queryLogic.getMaxPageSize()).andReturn(maxPageSize).anyTimes();
    expect(this.queryLogic.getPageByteTrigger()).andReturn(pageByteTrigger).anyTimes();
    expect(this.queryLogic.getMaxWork()).andReturn(maxWork).anyTimes();
    expect(this.queryLogic.getMaxResults()).andReturn(maxResults).anyTimes();
    expect(this.genericConfiguration.getQueryString()).andReturn(query).once();
    // Run the test
    PowerMock.replayAll();
    RunningQuery subject = new RunningQuery(this.connector, Priority.NORMAL, this.queryLogic, this.query, methodAuths, principal, new QueryMetricFactoryImpl());
    ResultsPage result1 = subject.next();
    String result2 = subject.toString();
    QueryMetric.Lifecycle status = subject.getMetric().getLifecycle();
    PowerMock.verifyAll();
    // Verify results
    assertNotNull("Expected a non-null page", result1);
    assertNotNull("Expected a non-null list of results", result1.getResults());
    assertTrue("Expected MAXRESULTS non-null items in the list of results", resultObjects.size() > maxResults);
    assertSame("Expected status to be MAXRESULTS", status, QueryMetric.Lifecycle.MAXRESULTS);
    assertNotNull("Expected a non-null toString() representation", result2);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) DatawaveUser(datawave.security.authorization.DatawaveUser) ResultsPage(datawave.webservice.query.cache.ResultsPage) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Date(java.util.Date) QueryMetric(datawave.microservice.querymetric.QueryMetric) UUID(java.util.UUID) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with ResultsPage

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

the class CompositeQueryLogicTest method testQueryLogicNoDataLogic2.

@Test
public void testQueryLogicNoDataLogic2() throws Exception {
    List<BaseQueryLogic<?>> logics = new ArrayList<>();
    TestQueryLogic logic1 = new TestQueryLogic();
    TestQueryLogic2 logic2 = new TestQueryLogic2();
    logics.add(logic1);
    logics.add(logic2);
    logic1.getData().put(key1, value1);
    logic1.getData().put(key2, value2);
    logic1.getData().put(key3, value3);
    logic1.getData().put(key4, value4);
    logic1.getData().put(key5, value5);
    logic1.getData().put(key6, value6);
    logic1.getData().put(key7, value7);
    logic1.getData().put(key8, value8);
    QueryImpl settings = new QueryImpl();
    settings.setPagesize(100);
    settings.setQueryAuthorizations(auths.serialize());
    settings.setQuery("FOO == 'BAR'");
    settings.setParameters(new HashSet<>());
    settings.setId(UUID.randomUUID());
    CompositeQueryLogic c = new CompositeQueryLogic();
    // max.results.override is set to -1 when it is not passed in as it is an optional paramter
    logic2.setMaxResults(-1);
    logic1.setMaxResults(-1);
    /**
     * RunningQuery.setupConnection()
     */
    c.setQueryLogics(logics);
    c.initialize((Connector) null, (Query) settings, Collections.singleton(auths));
    c.setupQuery(null);
    TransformIterator iter = c.getTransformIterator((Query) settings);
    /**
     * RunningQuery.next() - iterate over results coming from tablet server through the TransformIterator to turn them into the objects.
     */
    List<Object> results = new ArrayList<>();
    while (iter.hasNext()) {
        Object o = iter.next();
        if (null == o)
            break;
        Assert.assertTrue(o instanceof TestQueryResponse);
        results.add((TestQueryResponse) o);
    }
    Assert.assertEquals(8, results.size());
    ResultsPage page = new ResultsPage(results, Status.COMPLETE);
    /**
     * QueryExecutorBean.next() - transform list of objects into JAXB response
     */
    TestQueryResponseList response = (TestQueryResponseList) c.getTransformer((Query) settings).createResponse(page);
    Assert.assertEquals(8, response.getResponses().size());
    for (TestQueryResponse r : response.getResponses()) {
        Assert.assertNotNull(r);
    }
    c.close();
}
Also used : TransformIterator(org.apache.commons.collections4.iterators.TransformIterator) ArrayList(java.util.ArrayList) ResultsPage(datawave.webservice.query.cache.ResultsPage) QueryImpl(datawave.webservice.query.QueryImpl) BaseQueryLogic(datawave.webservice.query.logic.BaseQueryLogic) Test(org.junit.Test)

Example 20 with ResultsPage

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

the class QueryExecutorBean method _next.

private BaseQueryResponse _next(RunningQuery query, String queryId, Collection<String> proxyServers, Span span) throws Exception {
    // If we're tracing this query, then continue the trace for the next call.
    TInfo traceInfo = query.getTraceInfo();
    if (traceInfo != null) {
        span = Trace.trace(traceInfo, "query:next");
    }
    ResultsPage resultList;
    try {
        resultList = query.next();
    } catch (RejectedExecutionException e) {
        // - race condition, query expired while user called next
        throw new PreConditionFailedQueryException(DatawaveErrorCode.QUERY_TIMEOUT_OR_SERVER_ERROR, e, MessageFormat.format("id = {0}", queryId));
    }
    long pageNum = query.getLastPageNumber();
    BaseQueryResponse response = query.getLogic().getTransformer(query.getSettings()).createResponse(resultList);
    if (!resultList.getResults().isEmpty()) {
        response.setHasResults(true);
    } else {
        response.setHasResults(false);
    }
    response.setPageNumber(pageNum);
    response.setLogicName(query.getLogic().getLogicName());
    response.setQueryId(queryId);
    if (span != null) {
        span.data("pageNumber", Long.toString(pageNum));
    }
    query.getMetric().setProxyServers(proxyServers);
    testForUncaughtException(query.getSettings(), resultList);
    if (resultList.getResults().isEmpty()) {
        NoResultsQueryException qe = new NoResultsQueryException(DatawaveErrorCode.NO_QUERY_RESULTS_FOUND, MessageFormat.format("{0}", queryId));
        response.addException(qe);
        throw new NoResultsException(qe);
    } else {
        return response;
    }
}
Also used : TInfo(org.apache.accumulo.core.trace.thrift.TInfo) NoResultsException(datawave.webservice.common.exception.NoResultsException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) BaseQueryResponse(datawave.webservice.result.BaseQueryResponse) ResultsPage(datawave.webservice.query.cache.ResultsPage) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

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