Search in sources :

Example 16 with QueryException

use of org.apache.druid.query.QueryException in project druid by druid-io.

the class SqlResourceTest method testAssertionErrorThrowsErrorWithFilterResponse.

@Test
public void testAssertionErrorThrowsErrorWithFilterResponse() throws Exception {
    resource = new SqlResource(JSON_MAPPER, CalciteTests.TEST_AUTHORIZER_MAPPER, sqlLifecycleFactory, lifecycleManager, new ServerConfig() {

        @Override
        public boolean isShowDetailedJettyErrors() {
            return true;
        }

        @Override
        public ErrorResponseTransformStrategy getErrorResponseTransformStrategy() {
            return new AllowedRegexErrorResponseTransformStrategy(ImmutableList.of());
        }
    });
    String errorMessage = "could not assert";
    SqlQuery badQuery = EasyMock.createMock(SqlQuery.class);
    EasyMock.expect(badQuery.getQuery()).andReturn("SELECT ANSWER TO LIFE");
    EasyMock.expect(badQuery.getContext()).andReturn(ImmutableMap.of("sqlQueryId", "id"));
    EasyMock.expect(badQuery.getParameterList()).andThrow(new Error(errorMessage));
    EasyMock.replay(badQuery);
    final QueryException exception = doPost(badQuery).lhs;
    Assert.assertNotNull(exception);
    Assert.assertNull(exception.getMessage());
    Assert.assertNull(exception.getHost());
    Assert.assertEquals(exception.getErrorCode(), QueryInterruptedException.UNKNOWN_EXCEPTION);
    Assert.assertNull(exception.getErrorClass());
    Assert.assertTrue(lifecycleManager.getAll("id").isEmpty());
}
Also used : ServerConfig(org.apache.druid.server.initialization.ServerConfig) UnsupportedSQLQueryException(org.apache.druid.sql.calcite.planner.UnsupportedSQLQueryException) QueryException(org.apache.druid.query.QueryException) PlanningError(org.apache.druid.sql.SqlPlanningException.PlanningError) AllowedRegexErrorResponseTransformStrategy(org.apache.druid.common.exception.AllowedRegexErrorResponseTransformStrategy) Test(org.junit.Test)

Example 17 with QueryException

use of org.apache.druid.query.QueryException in project druid by druid-io.

the class SqlResourceTest method testCancelBetweenValidateAndPlan.

@Test
public void testCancelBetweenValidateAndPlan() throws Exception {
    final String sqlQueryId = "toCancel";
    lifecycleAddLatch = new CountDownLatch(1);
    CountDownLatch validateAndAuthorizeLatch = new CountDownLatch(1);
    validateAndAuthorizeLatchSupplier.set(new NonnullPair<>(validateAndAuthorizeLatch, true));
    CountDownLatch planLatch = new CountDownLatch(1);
    planLatchSupplier.set(new NonnullPair<>(planLatch, false));
    Future<Response> future = executorService.submit(() -> resource.doPost(createSimpleQueryWithId(sqlQueryId, "SELECT DISTINCT dim1 FROM foo"), makeRegularUserReq()));
    Assert.assertTrue(validateAndAuthorizeLatch.await(1, TimeUnit.SECONDS));
    Assert.assertTrue(lifecycleAddLatch.await(1, TimeUnit.SECONDS));
    Response response = resource.cancelQuery(sqlQueryId, mockRequestForCancel());
    planLatch.countDown();
    Assert.assertEquals(Status.ACCEPTED.getStatusCode(), response.getStatus());
    Assert.assertTrue(lifecycleManager.getAll(sqlQueryId).isEmpty());
    response = future.get();
    Assert.assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
    QueryException exception = JSON_MAPPER.readValue((byte[]) response.getEntity(), QueryException.class);
    Assert.assertEquals(QueryInterruptedException.QUERY_CANCELLED, exception.getErrorCode());
}
Also used : Response(javax.ws.rs.core.Response) UnsupportedSQLQueryException(org.apache.druid.sql.calcite.planner.UnsupportedSQLQueryException) QueryException(org.apache.druid.query.QueryException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 18 with QueryException

use of org.apache.druid.query.QueryException in project druid by druid-io.

the class SqlResourceTest method testTooManyRequests.

@Test
public void testTooManyRequests() throws Exception {
    sleep = true;
    final int numQueries = 3;
    final String sqlQueryId = "tooManyRequestsTest";
    List<Future<Pair<QueryException, List<Map<String, Object>>>>> futures = new ArrayList<>(numQueries);
    for (int i = 0; i < numQueries; i++) {
        futures.add(executorService.submit(() -> {
            try {
                return doPost(new SqlQuery("SELECT COUNT(*) AS cnt, 'foo' AS TheFoo FROM druid.foo", null, false, false, false, ImmutableMap.of("priority", -5, BaseQuery.SQL_QUERY_ID, sqlQueryId), null), makeRegularUserReq());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }));
    }
    int success = 0;
    int limited = 0;
    for (int i = 0; i < numQueries; i++) {
        Pair<QueryException, List<Map<String, Object>>> result = futures.get(i).get();
        List<Map<String, Object>> rows = result.rhs;
        if (rows != null) {
            Assert.assertEquals(ImmutableList.of(ImmutableMap.of("cnt", 6, "TheFoo", "foo")), rows);
            success++;
        } else {
            QueryException interruped = result.lhs;
            Assert.assertEquals(QueryCapacityExceededException.ERROR_CODE, interruped.getErrorCode());
            Assert.assertEquals(QueryCapacityExceededException.makeLaneErrorMessage(HiLoQueryLaningStrategy.LOW, 2), interruped.getMessage());
            limited++;
        }
    }
    Assert.assertEquals(2, success);
    Assert.assertEquals(1, limited);
    Assert.assertEquals(3, testRequestLogger.getSqlQueryLogs().size());
    Assert.assertTrue(lifecycleManager.getAll(sqlQueryId).isEmpty());
}
Also used : ArrayList(java.util.ArrayList) UnsupportedSQLQueryException(org.apache.druid.sql.calcite.planner.UnsupportedSQLQueryException) ForbiddenException(org.apache.druid.server.security.ForbiddenException) QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) QueryException(org.apache.druid.query.QueryException) QueryCapacityExceededException(org.apache.druid.query.QueryCapacityExceededException) QueryInterruptedException(org.apache.druid.query.QueryInterruptedException) IOException(java.io.IOException) RelConversionException(org.apache.calcite.tools.RelConversionException) ResourceLimitExceededException(org.apache.druid.query.ResourceLimitExceededException) QueryUnsupportedException(org.apache.druid.query.QueryUnsupportedException) UnsupportedSQLQueryException(org.apache.druid.sql.calcite.planner.UnsupportedSQLQueryException) QueryException(org.apache.druid.query.QueryException) Future(java.util.concurrent.Future) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.Test)

Example 19 with QueryException

use of org.apache.druid.query.QueryException in project druid by druid-io.

the class SqlResourceTest method testCancelBetweenPlanAndExecute.

@Test
public void testCancelBetweenPlanAndExecute() throws Exception {
    final String sqlQueryId = "toCancel";
    CountDownLatch planLatch = new CountDownLatch(1);
    planLatchSupplier.set(new NonnullPair<>(planLatch, true));
    CountDownLatch execLatch = new CountDownLatch(1);
    executeLatchSupplier.set(new NonnullPair<>(execLatch, false));
    Future<Response> future = executorService.submit(() -> resource.doPost(createSimpleQueryWithId(sqlQueryId, "SELECT DISTINCT dim1 FROM foo"), makeRegularUserReq()));
    Assert.assertTrue(planLatch.await(1, TimeUnit.SECONDS));
    Response response = resource.cancelQuery(sqlQueryId, mockRequestForCancel());
    execLatch.countDown();
    Assert.assertEquals(Status.ACCEPTED.getStatusCode(), response.getStatus());
    Assert.assertTrue(lifecycleManager.getAll(sqlQueryId).isEmpty());
    response = future.get();
    Assert.assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
    QueryException exception = JSON_MAPPER.readValue((byte[]) response.getEntity(), QueryException.class);
    Assert.assertEquals(QueryInterruptedException.QUERY_CANCELLED, exception.getErrorCode());
}
Also used : Response(javax.ws.rs.core.Response) UnsupportedSQLQueryException(org.apache.druid.sql.calcite.planner.UnsupportedSQLQueryException) QueryException(org.apache.druid.query.QueryException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 20 with QueryException

use of org.apache.druid.query.QueryException in project druid by druid-io.

the class SqlResourceTest method testQueryTimeoutException.

@Test
public void testQueryTimeoutException() throws Exception {
    final String sqlQueryId = "timeoutTest";
    Map<String, Object> queryContext = ImmutableMap.of(QueryContexts.TIMEOUT_KEY, 1, BaseQuery.SQL_QUERY_ID, sqlQueryId);
    final QueryException timeoutException = doPost(new SqlQuery("SELECT CAST(__time AS DATE), dim1, dim2, dim3 FROM druid.foo GROUP by __time, dim1, dim2, dim3 ORDER BY dim2 DESC", ResultFormat.OBJECT, false, false, false, queryContext, null)).lhs;
    Assert.assertNotNull(timeoutException);
    Assert.assertEquals(timeoutException.getErrorCode(), QueryTimeoutException.ERROR_CODE);
    Assert.assertEquals(timeoutException.getErrorClass(), QueryTimeoutException.class.getName());
    Assert.assertTrue(lifecycleManager.getAll(sqlQueryId).isEmpty());
}
Also used : QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) UnsupportedSQLQueryException(org.apache.druid.sql.calcite.planner.UnsupportedSQLQueryException) QueryException(org.apache.druid.query.QueryException) Test(org.junit.Test)

Aggregations

QueryException (org.apache.druid.query.QueryException)25 Test (org.junit.Test)22 UnsupportedSQLQueryException (org.apache.druid.sql.calcite.planner.UnsupportedSQLQueryException)13 IOException (java.io.IOException)9 QueryInterruptedException (org.apache.druid.query.QueryInterruptedException)9 ServerConfig (org.apache.druid.server.initialization.ServerConfig)9 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 Response (javax.ws.rs.core.Response)7 AllowedRegexErrorResponseTransformStrategy (org.apache.druid.common.exception.AllowedRegexErrorResponseTransformStrategy)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 Properties (java.util.Properties)6 ServletOutputStream (javax.servlet.ServletOutputStream)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 DefaultGenericQueryMetricsFactory (org.apache.druid.query.DefaultGenericQueryMetricsFactory)6 MapQueryToolChestWarehouse (org.apache.druid.query.MapQueryToolChestWarehouse)6 QueryUnsupportedException (org.apache.druid.query.QueryUnsupportedException)6 BaseJettyTest (org.apache.druid.server.initialization.BaseJettyTest)6 NoopRequestLogger (org.apache.druid.server.log.NoopRequestLogger)6 NoopServiceEmitter (org.apache.druid.server.metrics.NoopServiceEmitter)6 AuthenticatorMapper (org.apache.druid.server.security.AuthenticatorMapper)6