Search in sources :

Example 6 with BaseSequence

use of org.apache.druid.java.util.common.guava.BaseSequence in project druid by druid-io.

the class GroupByQueryEngine method process.

public Sequence<Row> process(final GroupByQuery query, final StorageAdapter storageAdapter) {
    if (storageAdapter == null) {
        throw new ISE("Null storage adapter found. Probably trying to issue a query against a segment being memory unmapped.");
    }
    if (!query.getContextValue(GroupByQueryConfig.CTX_KEY_ENABLE_MULTI_VALUE_UNNESTING, true)) {
        throw new UOE("GroupBy v1 does not support %s as false. Set %s to true or use groupBy v2", GroupByQueryConfig.CTX_KEY_ENABLE_MULTI_VALUE_UNNESTING, GroupByQueryConfig.CTX_KEY_ENABLE_MULTI_VALUE_UNNESTING);
    }
    final List<Interval> intervals = query.getQuerySegmentSpec().getIntervals();
    if (intervals.size() != 1) {
        throw new IAE("Should only have one interval, got[%s]", intervals);
    }
    Filter filter = Filters.convertToCNFFromQueryContext(query, Filters.toFilter(query.getDimFilter()));
    final Sequence<Cursor> cursors = storageAdapter.makeCursors(filter, intervals.get(0), query.getVirtualColumns(), query.getGranularity(), false, null);
    final ResourceHolder<ByteBuffer> bufferHolder = intermediateResultsBufferPool.take();
    return Sequences.concat(Sequences.withBaggage(Sequences.map(cursors, new Function<Cursor, Sequence<Row>>() {

        @Override
        public Sequence<Row> apply(final Cursor cursor) {
            return new BaseSequence<>(new BaseSequence.IteratorMaker<Row, RowIterator>() {

                @Override
                public RowIterator make() {
                    return new RowIterator(query, cursor, bufferHolder.get(), config.get());
                }

                @Override
                public void cleanup(RowIterator iterFromMake) {
                    CloseableUtils.closeAndWrapExceptions(iterFromMake);
                }
            });
        }
    }), bufferHolder));
}
Also used : UOE(org.apache.druid.java.util.common.UOE) Sequence(org.apache.druid.java.util.common.guava.Sequence) BaseSequence(org.apache.druid.java.util.common.guava.BaseSequence) IAE(org.apache.druid.java.util.common.IAE) Cursor(org.apache.druid.segment.Cursor) ByteBuffer(java.nio.ByteBuffer) Filter(org.apache.druid.query.filter.Filter) ISE(org.apache.druid.java.util.common.ISE) Interval(org.joda.time.Interval)

Example 7 with BaseSequence

use of org.apache.druid.java.util.common.guava.BaseSequence in project druid by druid-io.

the class ChainedExecutionQueryRunner method run.

@Override
public Sequence<T> run(final QueryPlus<T> queryPlus, final ResponseContext responseContext) {
    Query<T> query = queryPlus.getQuery();
    final int priority = QueryContexts.getPriority(query);
    final Ordering ordering = query.getResultOrdering();
    final QueryPlus<T> threadSafeQueryPlus = queryPlus.withoutThreadUnsafeState();
    return new BaseSequence<T, Iterator<T>>(new BaseSequence.IteratorMaker<T, Iterator<T>>() {

        @Override
        public Iterator<T> make() {
            // Make it a List<> to materialize all of the values (so that it will submit everything to the executor)
            List<ListenableFuture<Iterable<T>>> futures = Lists.newArrayList(Iterables.transform(queryables, input -> {
                if (input == null) {
                    throw new ISE("Null queryRunner! Looks to be some segment unmapping action happening");
                }
                return queryProcessingPool.submitRunnerTask(new AbstractPrioritizedQueryRunnerCallable<Iterable<T>, T>(priority, input) {

                    @Override
                    public Iterable<T> call() {
                        try {
                            Sequence<T> result = input.run(threadSafeQueryPlus, responseContext);
                            if (result == null) {
                                throw new ISE("Got a null result! Segments are missing!");
                            }
                            List<T> retVal = result.toList();
                            if (retVal == null) {
                                throw new ISE("Got a null list of results");
                            }
                            return retVal;
                        } catch (QueryInterruptedException e) {
                            throw new RuntimeException(e);
                        } catch (QueryTimeoutException e) {
                            throw e;
                        } catch (Exception e) {
                            log.noStackTrace().error(e, "Exception with one of the sequences!");
                            Throwables.propagateIfPossible(e);
                            throw new RuntimeException(e);
                        }
                    }
                });
            }));
            ListenableFuture<List<Iterable<T>>> future = Futures.allAsList(futures);
            queryWatcher.registerQueryFuture(query, future);
            try {
                return new MergeIterable<>(ordering.nullsFirst(), QueryContexts.hasTimeout(query) ? future.get(QueryContexts.getTimeout(query), TimeUnit.MILLISECONDS) : future.get()).iterator();
            } catch (InterruptedException e) {
                log.noStackTrace().warn(e, "Query interrupted, cancelling pending results, query id [%s]", query.getId());
                // Note: canceling combinedFuture first so that it can complete with INTERRUPTED as its final state. See ChainedExecutionQueryRunnerTest.testQueryTimeout()
                GuavaUtils.cancelAll(true, future, futures);
                throw new QueryInterruptedException(e);
            } catch (CancellationException e) {
                throw new QueryInterruptedException(e);
            } catch (TimeoutException e) {
                log.warn("Query timeout, cancelling pending results for query id [%s]", query.getId());
                GuavaUtils.cancelAll(true, future, futures);
                throw new QueryTimeoutException(StringUtils.nonStrictFormat("Query [%s] timed out", query.getId()));
            } catch (ExecutionException e) {
                GuavaUtils.cancelAll(true, future, futures);
                Throwables.propagateIfPossible(e.getCause());
                throw new RuntimeException(e.getCause());
            }
        }

        @Override
        public void cleanup(Iterator<T> tIterator) {
        }
    });
}
Also used : MergeIterable(org.apache.druid.java.util.common.guava.MergeIterable) MergeIterable(org.apache.druid.java.util.common.guava.MergeIterable) Ordering(com.google.common.collect.Ordering) Iterator(java.util.Iterator) List(java.util.List) ISE(org.apache.druid.java.util.common.ISE) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Sequence(org.apache.druid.java.util.common.guava.Sequence) BaseSequence(org.apache.druid.java.util.common.guava.BaseSequence) BaseSequence(org.apache.druid.java.util.common.guava.BaseSequence) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) CancellationException(java.util.concurrent.CancellationException) ListenableFuture(com.google.common.util.concurrent.ListenableFuture)

Aggregations

BaseSequence (org.apache.druid.java.util.common.guava.BaseSequence)7 Iterator (java.util.Iterator)4 ISE (org.apache.druid.java.util.common.ISE)4 List (java.util.List)3 Sequence (org.apache.druid.java.util.common.guava.Sequence)3 QueryTimeoutException (org.apache.druid.query.QueryTimeoutException)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 CancellationException (java.util.concurrent.CancellationException)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 UOE (org.apache.druid.java.util.common.UOE)2 ResourceLimitExceededException (org.apache.druid.query.ResourceLimitExceededException)2 Filter (org.apache.druid.query.filter.Filter)2 Interval (org.joda.time.Interval)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1