Search in sources :

Example 11 with QueryTimeoutException

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

the class QueryLifecycle method emitLogsAndMetrics.

/**
 * Emit logs and metrics for this query.
 *
 * @param e             exception that occurred while processing this query
 * @param remoteAddress remote address, for logging; or null if unknown
 * @param bytesWritten  number of bytes written; will become a query/bytes metric if >= 0
 */
@SuppressWarnings("unchecked")
public void emitLogsAndMetrics(@Nullable final Throwable e, @Nullable final String remoteAddress, final long bytesWritten) {
    if (baseQuery == null) {
        // Never initialized, don't log or emit anything.
        return;
    }
    if (state == State.DONE) {
        log.warn("Tried to emit logs and metrics twice for query[%s]!", baseQuery.getId());
    }
    state = State.DONE;
    final boolean success = e == null;
    try {
        final long queryTimeNs = System.nanoTime() - startNs;
        QueryMetrics queryMetrics = DruidMetrics.makeRequestMetrics(queryMetricsFactory, toolChest, baseQuery, StringUtils.nullToEmptyNonDruidDataString(remoteAddress));
        queryMetrics.success(success);
        queryMetrics.reportQueryTime(queryTimeNs);
        if (bytesWritten >= 0) {
            queryMetrics.reportQueryBytes(bytesWritten);
        }
        if (authenticationResult != null) {
            queryMetrics.identity(authenticationResult.getIdentity());
        }
        queryMetrics.emit(emitter);
        final Map<String, Object> statsMap = new LinkedHashMap<>();
        statsMap.put("query/time", TimeUnit.NANOSECONDS.toMillis(queryTimeNs));
        statsMap.put("query/bytes", bytesWritten);
        statsMap.put("success", success);
        if (authenticationResult != null) {
            statsMap.put("identity", authenticationResult.getIdentity());
        }
        if (e != null) {
            statsMap.put("exception", e.toString());
            if (QueryContexts.isDebug(baseQuery)) {
                log.warn(e, "Exception while processing queryId [%s]", baseQuery.getId());
            } else {
                log.noStackTrace().warn(e, "Exception while processing queryId [%s]", baseQuery.getId());
            }
            if (e instanceof QueryInterruptedException || e instanceof QueryTimeoutException) {
                // Mimic behavior from QueryResource, where this code was originally taken from.
                statsMap.put("interrupted", true);
                statsMap.put("reason", e.toString());
            }
        }
        requestLogger.logNativeQuery(RequestLogLine.forNative(baseQuery, DateTimes.utc(startMs), StringUtils.nullToEmptyNonDruidDataString(remoteAddress), new QueryStats(statsMap)));
    } catch (Exception ex) {
        log.error(ex, "Unable to log query [%s]!", baseQuery);
    }
}
Also used : QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) QueryMetrics(org.apache.druid.query.QueryMetrics) QueryInterruptedException(org.apache.druid.query.QueryInterruptedException) QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) LinkedHashMap(java.util.LinkedHashMap) QueryInterruptedException(org.apache.druid.query.QueryInterruptedException)

Example 12 with QueryTimeoutException

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

the class JsonParserIterator method init.

private void init() {
    if (jp == null) {
        try {
            long timeLeftMillis = timeoutAt - System.currentTimeMillis();
            if (checkTimeout(timeLeftMillis)) {
                throw timeoutQuery();
            }
            InputStream is = hasTimeout ? future.get(timeLeftMillis, TimeUnit.MILLISECONDS) : future.get();
            if (is != null) {
                jp = objectMapper.getFactory().createParser(is);
            } else if (checkTimeout()) {
                throw timeoutQuery();
            } else {
                // TODO: NettyHttpClient should check the actual cause of the failure and set it in the future properly.
                throw ResourceLimitExceededException.withMessage("Possibly max scatter-gather bytes limit reached while reading from url[%s].", url);
            }
            final JsonToken nextToken = jp.nextToken();
            if (nextToken == JsonToken.START_ARRAY) {
                jp.nextToken();
                objectCodec = jp.getCodec();
            } else if (nextToken == JsonToken.START_OBJECT) {
                throw convertException(jp.getCodec().readValue(jp, QueryException.class));
            } else {
                throw convertException(new IAE("Next token wasn't a START_ARRAY, was[%s] from url[%s]", jp.getCurrentToken(), url));
            }
        } catch (ExecutionException | CancellationException e) {
            throw convertException(e.getCause() == null ? e : e.getCause());
        } catch (IOException | InterruptedException e) {
            throw convertException(e);
        } catch (TimeoutException e) {
            throw new QueryTimeoutException(StringUtils.nonStrictFormat("Query [%s] timed out!", queryId), host);
        }
    }
}
Also used : QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) CancellationException(java.util.concurrent.CancellationException) InputStream(java.io.InputStream) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) IAE(org.apache.druid.java.util.common.IAE) ExecutionException(java.util.concurrent.ExecutionException) QueryInterruptedException(org.apache.druid.query.QueryInterruptedException) TimeoutException(java.util.concurrent.TimeoutException) QueryTimeoutException(org.apache.druid.query.QueryTimeoutException)

Example 13 with QueryTimeoutException

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

the class ServerManagerForQueryErrorTest method buildQueryRunnerForSegment.

@Override
protected <T> QueryRunner<T> buildQueryRunnerForSegment(Query<T> query, SegmentDescriptor descriptor, QueryRunnerFactory<T, Query<T>> factory, QueryToolChest<T, Query<T>> toolChest, VersionedIntervalTimeline<String, ReferenceCountingSegment> timeline, Function<SegmentReference, SegmentReference> segmentMapFn, AtomicLong cpuTimeAccumulator, Optional<byte[]> cacheKeyPrefix) {
    if (query.getContextBoolean(QUERY_RETRY_TEST_CONTEXT_KEY, false)) {
        final MutableBoolean isIgnoreSegment = new MutableBoolean(false);
        queryToIgnoredSegments.compute(query.getMostSpecificId(), (queryId, ignoredSegments) -> {
            if (ignoredSegments == null) {
                ignoredSegments = new HashSet<>();
            }
            if (ignoredSegments.size() < MAX_NUM_FALSE_MISSING_SEGMENTS_REPORTS) {
                ignoredSegments.add(descriptor);
                isIgnoreSegment.setTrue();
            }
            return ignoredSegments;
        });
        if (isIgnoreSegment.isTrue()) {
            LOG.info("Pretending I don't have segment[%s]", descriptor);
            return new ReportTimelineMissingSegmentQueryRunner<>(descriptor);
        }
    } else if (query.getContextBoolean(QUERY_TIMEOUT_TEST_CONTEXT_KEY, false)) {
        return (queryPlus, responseContext) -> new Sequence<T>() {

            @Override
            public <OutType> OutType accumulate(OutType initValue, Accumulator<OutType, T> accumulator) {
                throw new QueryTimeoutException("query timeout test");
            }

            @Override
            public <OutType> Yielder<OutType> toYielder(OutType initValue, YieldingAccumulator<OutType, T> accumulator) {
                throw new QueryTimeoutException("query timeout test");
            }
        };
    } else if (query.getContextBoolean(QUERY_CAPACITY_EXCEEDED_TEST_CONTEXT_KEY, false)) {
        return (queryPlus, responseContext) -> new Sequence<T>() {

            @Override
            public <OutType> OutType accumulate(OutType initValue, Accumulator<OutType, T> accumulator) {
                throw QueryCapacityExceededException.withErrorMessageAndResolvedHost("query capacity exceeded test");
            }

            @Override
            public <OutType> Yielder<OutType> toYielder(OutType initValue, YieldingAccumulator<OutType, T> accumulator) {
                throw QueryCapacityExceededException.withErrorMessageAndResolvedHost("query capacity exceeded test");
            }
        };
    } else if (query.getContextBoolean(QUERY_UNSUPPORTED_TEST_CONTEXT_KEY, false)) {
        return (queryPlus, responseContext) -> new Sequence<T>() {

            @Override
            public <OutType> OutType accumulate(OutType initValue, Accumulator<OutType, T> accumulator) {
                throw new QueryUnsupportedException("query unsupported test");
            }

            @Override
            public <OutType> Yielder<OutType> toYielder(OutType initValue, YieldingAccumulator<OutType, T> accumulator) {
                throw new QueryUnsupportedException("query unsupported test");
            }
        };
    } else if (query.getContextBoolean(RESOURCE_LIMIT_EXCEEDED_TEST_CONTEXT_KEY, false)) {
        return (queryPlus, responseContext) -> new Sequence<T>() {

            @Override
            public <OutType> OutType accumulate(OutType initValue, Accumulator<OutType, T> accumulator) {
                throw new ResourceLimitExceededException("resource limit exceeded test");
            }

            @Override
            public <OutType> Yielder<OutType> toYielder(OutType initValue, YieldingAccumulator<OutType, T> accumulator) {
                throw new ResourceLimitExceededException("resource limit exceeded test");
            }
        };
    } else if (query.getContextBoolean(QUERY_FAILURE_TEST_CONTEXT_KEY, false)) {
        return (queryPlus, responseContext) -> new Sequence<T>() {

            @Override
            public <OutType> OutType accumulate(OutType initValue, Accumulator<OutType, T> accumulator) {
                throw new RuntimeException("query failure test");
            }

            @Override
            public <OutType> Yielder<OutType> toYielder(OutType initValue, YieldingAccumulator<OutType, T> accumulator) {
                throw new RuntimeException("query failure test");
            }
        };
    }
    return super.buildQueryRunnerForSegment(query, descriptor, factory, toolChest, timeline, segmentMapFn, cpuTimeAccumulator, cacheKeyPrefix);
}
Also used : Logger(org.apache.druid.java.util.common.logger.Logger) SegmentManager(org.apache.druid.server.SegmentManager) Inject(com.google.inject.Inject) Smile(org.apache.druid.guice.annotations.Smile) QueryProcessingPool(org.apache.druid.query.QueryProcessingPool) JoinableFactory(org.apache.druid.segment.join.JoinableFactory) Function(java.util.function.Function) QueryCapacityExceededException(org.apache.druid.query.QueryCapacityExceededException) HashSet(java.util.HashSet) SegmentReference(org.apache.druid.segment.SegmentReference) Query(org.apache.druid.query.Query) QueryRunner(org.apache.druid.query.QueryRunner) CachePopulator(org.apache.druid.client.cache.CachePopulator) Yielder(org.apache.druid.java.util.common.guava.Yielder) Sequence(org.apache.druid.java.util.common.guava.Sequence) YieldingAccumulator(org.apache.druid.java.util.common.guava.YieldingAccumulator) VersionedIntervalTimeline(org.apache.druid.timeline.VersionedIntervalTimeline) ServerConfig(org.apache.druid.server.initialization.ServerConfig) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ReportTimelineMissingSegmentQueryRunner(org.apache.druid.query.ReportTimelineMissingSegmentQueryRunner) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CacheConfig(org.apache.druid.client.cache.CacheConfig) QueryRunnerFactoryConglomerate(org.apache.druid.query.QueryRunnerFactoryConglomerate) QueryToolChest(org.apache.druid.query.QueryToolChest) Set(java.util.Set) ReferenceCountingSegment(org.apache.druid.segment.ReferenceCountingSegment) AtomicLong(java.util.concurrent.atomic.AtomicLong) QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) ServiceEmitter(org.apache.druid.java.util.emitter.service.ServiceEmitter) QueryRunnerFactory(org.apache.druid.query.QueryRunnerFactory) ResourceLimitExceededException(org.apache.druid.query.ResourceLimitExceededException) Optional(java.util.Optional) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) SegmentDescriptor(org.apache.druid.query.SegmentDescriptor) Cache(org.apache.druid.client.cache.Cache) Accumulator(org.apache.druid.java.util.common.guava.Accumulator) QueryUnsupportedException(org.apache.druid.query.QueryUnsupportedException) YieldingAccumulator(org.apache.druid.java.util.common.guava.YieldingAccumulator) Accumulator(org.apache.druid.java.util.common.guava.Accumulator) Yielder(org.apache.druid.java.util.common.guava.Yielder) QueryUnsupportedException(org.apache.druid.query.QueryUnsupportedException) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) Sequence(org.apache.druid.java.util.common.guava.Sequence) YieldingAccumulator(org.apache.druid.java.util.common.guava.YieldingAccumulator) QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) ReportTimelineMissingSegmentQueryRunner(org.apache.druid.query.ReportTimelineMissingSegmentQueryRunner) ResourceLimitExceededException(org.apache.druid.query.ResourceLimitExceededException)

Example 14 with QueryTimeoutException

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

the class ConcurrentGrouperTest method testGrouperTimeout.

@Test
public void testGrouperTimeout() throws Exception {
    final ConcurrentGrouper<Long> grouper = new ConcurrentGrouper<>(bufferSupplier, TEST_RESOURCE_HOLDER, KEY_SERDE_FACTORY, KEY_SERDE_FACTORY, NULL_FACTORY, new AggregatorFactory[] { new CountAggregatorFactory("cnt") }, 1024, 0.7f, 1, new LimitedTemporaryStorage(temporaryFolder.newFolder(), 1024 * 1024), new DefaultObjectMapper(), 8, null, false, MoreExecutors.listeningDecorator(SERVICE), 0, true, 1, 4, 8);
    grouper.init();
    final int numRows = 1000;
    Future<?>[] futures = new Future[8];
    for (int i = 0; i < 8; i++) {
        futures[i] = SERVICE.submit(new Runnable() {

            @Override
            public void run() {
                for (long i = 0; i < numRows; i++) {
                    grouper.aggregate(i);
                }
            }
        });
    }
    for (Future eachFuture : futures) {
        eachFuture.get();
    }
    try {
        grouper.iterator(true);
    } catch (RuntimeException e) {
        Assert.assertTrue(e instanceof QueryTimeoutException);
        Assert.assertEquals("Query timeout", ((QueryTimeoutException) e).getErrorCode());
    }
    grouper.close();
}
Also used : QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) Future(java.util.concurrent.Future) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) Test(org.junit.Test)

Example 15 with QueryTimeoutException

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

the class DirectDruidClientTest method testQueryTimeoutFromFuture.

@Test
public void testQueryTimeoutFromFuture() {
    SettableFuture<Object> noFuture = SettableFuture.create();
    Capture<Request> capturedRequest = EasyMock.newCapture();
    final String queryId = "never-ending-future";
    EasyMock.expect(httpClient.go(EasyMock.capture(capturedRequest), EasyMock.<HttpResponseHandler>anyObject(), EasyMock.anyObject(Duration.class))).andReturn(noFuture).anyTimes();
    EasyMock.replay(httpClient);
    TimeBoundaryQuery query = Druids.newTimeBoundaryQueryBuilder().dataSource("test").build();
    query = query.withOverriddenContext(ImmutableMap.of(DirectDruidClient.QUERY_FAIL_TIME, System.currentTimeMillis() + 500, "queryId", queryId));
    Sequence results = client.run(QueryPlus.wrap(query));
    QueryTimeoutException actualException = null;
    try {
        results.toList();
    } catch (QueryTimeoutException e) {
        actualException = e;
    }
    Assert.assertNotNull(actualException);
    Assert.assertEquals("Query timeout", actualException.getErrorCode());
    Assert.assertEquals(StringUtils.format("Query [%s] timed out!", queryId), actualException.getMessage());
    Assert.assertEquals(hostName, actualException.getHost());
    EasyMock.verify(httpClient);
}
Also used : QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) Request(org.apache.druid.java.util.http.client.Request) TimeBoundaryQuery(org.apache.druid.query.timeboundary.TimeBoundaryQuery) Sequence(org.apache.druid.java.util.common.guava.Sequence) HttpResponseHandler(org.apache.druid.java.util.http.client.response.HttpResponseHandler) Test(org.junit.Test)

Aggregations

QueryTimeoutException (org.apache.druid.query.QueryTimeoutException)15 QueryInterruptedException (org.apache.druid.query.QueryInterruptedException)7 IOException (java.io.IOException)5 Test (org.junit.Test)5 CancellationException (java.util.concurrent.CancellationException)4 ExecutionException (java.util.concurrent.ExecutionException)4 TimeoutException (java.util.concurrent.TimeoutException)4 Sequence (org.apache.druid.java.util.common.guava.Sequence)4 ResourceLimitExceededException (org.apache.druid.query.ResourceLimitExceededException)4 ResponseContext (org.apache.druid.query.context.ResponseContext)4 List (java.util.List)3 Response (javax.ws.rs.core.Response)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Preconditions (com.google.common.base.Preconditions)2 ImmutableList (com.google.common.collect.ImmutableList)2 CountingOutputStream (com.google.common.io.CountingOutputStream)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 ByteBuffer (java.nio.ByteBuffer)2 LinkedHashMap (java.util.LinkedHashMap)2