Search in sources :

Example 21 with Sequence

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

the class BackgroundCachePopulatorTest method testWrap.

/**
 * Method: wrap(final Sequence<T> sequence, final Function<T, CacheType> cacheFn, final Cache cache, final Cache.NamedKey cacheKey)
 */
@Test
public void testWrap() {
    String cacheId = "segment";
    SegmentDescriptor segmentDescriptor = new SegmentDescriptor(Intervals.of("2011/2012"), "version", 0);
    CacheStrategy cacheStrategy = toolchest.getCacheStrategy(query);
    Cache.NamedKey cacheKey = CacheUtil.computeSegmentCacheKey(cacheId, segmentDescriptor, cacheStrategy.computeCacheKey(query));
    Sequence res = this.backgroundCachePopulator.wrap(this.baseRunner.run(QueryPlus.wrap(query), ResponseContext.createEmpty()), (value) -> cacheStrategy.prepareForSegmentLevelCache().apply(value), cache, cacheKey);
    Assert.assertFalse("sequence must not be closed", closable.isClosed());
    Assert.assertNull("cache must be empty", cache.get(cacheKey));
    List results = res.toList();
    Assert.assertTrue(closable.isClosed());
    List<Result> expectedRes = makeTopNResults(false, OBJECTS);
    Assert.assertEquals(expectedRes.toString(), results.toString());
    Assert.assertEquals(5, results.size());
}
Also used : SegmentDescriptor(org.apache.druid.query.SegmentDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) Sequence(org.apache.druid.java.util.common.guava.Sequence) CacheStrategy(org.apache.druid.query.CacheStrategy) Result(org.apache.druid.query.Result) Test(org.junit.Test)

Example 22 with Sequence

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

the class BackgroundCachePopulatorTest method testWrapOnFailure.

@Test
public void testWrapOnFailure() {
    String cacheId = "segment";
    SegmentDescriptor segmentDescriptor = new SegmentDescriptor(Intervals.of("2011/2012"), "version", 0);
    CacheStrategy cacheStrategy = toolchest.getCacheStrategy(query);
    Cache.NamedKey cacheKey = CacheUtil.computeSegmentCacheKey(cacheId, segmentDescriptor, cacheStrategy.computeCacheKey(query));
    Sequence res = this.backgroundCachePopulator.wrap(this.baseRunner.run(QueryPlus.wrap(query), ResponseContext.createEmpty()), (value) -> {
        throw new RuntimeException("Error");
    }, cache, cacheKey);
    Assert.assertFalse("sequence must not be closed", closable.isClosed());
    Assert.assertNull("cache must be empty", cache.get(cacheKey));
    List results = res.toList();
    Assert.assertTrue(closable.isClosed());
    List<Result> expectedRes = makeTopNResults(false, OBJECTS);
    Assert.assertEquals(expectedRes.toString(), results.toString());
    Assert.assertEquals(5, results.size());
}
Also used : SegmentDescriptor(org.apache.druid.query.SegmentDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) Sequence(org.apache.druid.java.util.common.guava.Sequence) CacheStrategy(org.apache.druid.query.CacheStrategy) Result(org.apache.druid.query.Result) Test(org.junit.Test)

Example 23 with Sequence

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

the class TestHttpClient method go.

@Override
public <Intermediate, Final> ListenableFuture<Final> go(Request request, HttpResponseHandler<Intermediate, Final> handler, Duration readTimeout) {
    try {
        final Query query = objectMapper.readValue(request.getContent().array(), Query.class);
        final QueryRunner queryRunner = servers.get(request.getUrl()).getQueryRunner();
        if (queryRunner == null) {
            throw new ISE("Can't find queryRunner for url[%s]", request.getUrl());
        }
        final ResponseContext responseContext = ResponseContext.createEmpty();
        final Sequence sequence = queryRunner.run(QueryPlus.wrap(query), responseContext);
        final byte[] serializedContent;
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            objectMapper.writeValue(baos, sequence);
            serializedContent = baos.toByteArray();
        }
        final ResponseContext.SerializationResult serializationResult = responseContext.serializeWith(objectMapper, RESPONSE_CTX_HEADER_LEN_LIMIT);
        final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
        response.headers().add(QueryResource.HEADER_RESPONSE_CONTEXT, serializationResult.getResult());
        response.setContent(HeapChannelBufferFactory.getInstance().getBuffer(serializedContent, 0, serializedContent.length));
        final ClientResponse<Intermediate> intermClientResponse = handler.handleResponse(response, NOOP_TRAFFIC_COP);
        final ClientResponse<Final> finalClientResponse = handler.done(intermClientResponse);
        return Futures.immediateFuture(finalClientResponse.getObj());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Query(org.apache.druid.query.Query) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Sequence(org.apache.druid.java.util.common.guava.Sequence) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) QueryRunner(org.apache.druid.query.QueryRunner) ReportTimelineMissingSegmentQueryRunner(org.apache.druid.query.ReportTimelineMissingSegmentQueryRunner) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) ResponseContext(org.apache.druid.query.context.ResponseContext) ISE(org.apache.druid.java.util.common.ISE)

Example 24 with Sequence

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

the class DruidSchema method refreshSegmentsForDataSource.

/**
 * Attempt to refresh "segmentSignatures" for a set of segments for a particular dataSource. Returns the set of
 * segments actually refreshed, which may be a subset of the asked-for set.
 */
private Set<SegmentId> refreshSegmentsForDataSource(final String dataSource, final Set<SegmentId> segments) throws IOException {
    if (!segments.stream().allMatch(segmentId -> segmentId.getDataSource().equals(dataSource))) {
        // Sanity check. We definitely expect this to pass.
        throw new ISE("'segments' must all match 'dataSource'!");
    }
    log.debug("Refreshing metadata for dataSource[%s].", dataSource);
    final long startTime = System.currentTimeMillis();
    // Segment id string -> SegmentId object.
    final Map<String, SegmentId> segmentIdMap = Maps.uniqueIndex(segments, SegmentId::toString);
    final Set<SegmentId> retVal = new HashSet<>();
    final Sequence<SegmentAnalysis> sequence = runSegmentMetadataQuery(Iterables.limit(segments, MAX_SEGMENTS_PER_QUERY));
    Yielder<SegmentAnalysis> yielder = Yielders.each(sequence);
    try {
        while (!yielder.isDone()) {
            final SegmentAnalysis analysis = yielder.get();
            final SegmentId segmentId = segmentIdMap.get(analysis.getId());
            if (segmentId == null) {
                log.warn("Got analysis for segment[%s] we didn't ask for, ignoring.", analysis.getId());
            } else {
                final RowSignature rowSignature = analysisToRowSignature(analysis);
                log.debug("Segment[%s] has signature[%s].", segmentId, rowSignature);
                segmentMetadataInfo.compute(dataSource, (datasourceKey, dataSourceSegments) -> {
                    if (dataSourceSegments == null) {
                        // Datasource may have been removed or become unavailable while this refresh was ongoing.
                        log.warn("No segment map found with datasource[%s], skipping refresh of segment[%s]", datasourceKey, segmentId);
                        return null;
                    } else {
                        dataSourceSegments.compute(segmentId, (segmentIdKey, segmentMetadata) -> {
                            if (segmentMetadata == null) {
                                log.warn("No segment[%s] found, skipping refresh", segmentId);
                                return null;
                            } else {
                                final AvailableSegmentMetadata updatedSegmentMetadata = AvailableSegmentMetadata.from(segmentMetadata).withRowSignature(rowSignature).withNumRows(analysis.getNumRows()).build();
                                retVal.add(segmentId);
                                return updatedSegmentMetadata;
                            }
                        });
                        if (dataSourceSegments.isEmpty()) {
                            return null;
                        } else {
                            return dataSourceSegments;
                        }
                    }
                });
            }
            yielder = yielder.next(null);
        }
    } finally {
        yielder.close();
    }
    log.debug("Refreshed metadata for dataSource[%s] in %,d ms (%d segments queried, %d segments left).", dataSource, System.currentTimeMillis() - startTime, retVal.size(), segments.size() - retVal.size());
    return retVal;
}
Also used : SegmentManager(org.apache.druid.server.SegmentManager) Inject(com.google.inject.Inject) LifecycleStart(org.apache.druid.java.util.common.lifecycle.LifecycleStart) AllColumnIncluderator(org.apache.druid.query.metadata.metadata.AllColumnIncluderator) FluentIterable(com.google.common.collect.FluentIterable) Map(java.util.Map) ServerType(org.apache.druid.server.coordination.ServerType) ManageLifecycle(org.apache.druid.guice.ManageLifecycle) TimelineServerView(org.apache.druid.client.TimelineServerView) EnumSet(java.util.EnumSet) DateTimes(org.apache.druid.java.util.common.DateTimes) Sequence(org.apache.druid.java.util.common.guava.Sequence) ImmutableSet(com.google.common.collect.ImmutableSet) Execs(org.apache.druid.java.util.common.concurrent.Execs) ImmutableMap(com.google.common.collect.ImmutableMap) GuardedBy(com.google.errorprone.annotations.concurrent.GuardedBy) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Table(org.apache.calcite.schema.Table) Set(java.util.Set) ISE(org.apache.druid.java.util.common.ISE) Interner(com.google.common.collect.Interner) PlannerConfig(org.apache.druid.sql.calcite.planner.PlannerConfig) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) CountDownLatch(java.util.concurrent.CountDownLatch) SegmentMetadataQuery(org.apache.druid.query.metadata.metadata.SegmentMetadataQuery) BrokerInternalQueryConfig(org.apache.druid.client.BrokerInternalQueryConfig) DataSegment(org.apache.druid.timeline.DataSegment) Optional(java.util.Optional) SegmentId(org.apache.druid.timeline.SegmentId) QueryLifecycleFactory(org.apache.druid.server.QueryLifecycleFactory) Iterables(com.google.common.collect.Iterables) GlobalTableDataSource(org.apache.druid.query.GlobalTableDataSource) JoinableFactory(org.apache.druid.segment.join.JoinableFactory) Function(java.util.function.Function) TreeSet(java.util.TreeSet) ConcurrentMap(java.util.concurrent.ConcurrentMap) ServerView(org.apache.druid.client.ServerView) Yielders(org.apache.druid.java.util.common.guava.Yielders) MultipleSpecificSegmentSpec(org.apache.druid.query.spec.MultipleSpecificSegmentSpec) HashSet(java.util.HashSet) LifecycleStop(org.apache.druid.java.util.common.lifecycle.LifecycleStop) DruidServerMetadata(org.apache.druid.server.coordination.DruidServerMetadata) SegmentAnalysis(org.apache.druid.query.metadata.metadata.SegmentAnalysis) Predicates(com.google.common.base.Predicates) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) StreamSupport(java.util.stream.StreamSupport) Yielder(org.apache.druid.java.util.common.guava.Yielder) ExecutorService(java.util.concurrent.ExecutorService) Access(org.apache.druid.server.security.Access) EmittingLogger(org.apache.druid.java.util.emitter.EmittingLogger) Interners(com.google.common.collect.Interners) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) TableDataSource(org.apache.druid.query.TableDataSource) ColumnAnalysis(org.apache.druid.query.metadata.metadata.ColumnAnalysis) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) TreeMap(java.util.TreeMap) DruidTable(org.apache.druid.sql.calcite.table.DruidTable) RowSignature(org.apache.druid.segment.column.RowSignature) ColumnType(org.apache.druid.segment.column.ColumnType) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Escalator(org.apache.druid.server.security.Escalator) Comparator(java.util.Comparator) SegmentId(org.apache.druid.timeline.SegmentId) ISE(org.apache.druid.java.util.common.ISE) SegmentAnalysis(org.apache.druid.query.metadata.metadata.SegmentAnalysis) RowSignature(org.apache.druid.segment.column.RowSignature) HashSet(java.util.HashSet)

Example 25 with Sequence

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

the class NativeQueryMaker method runQuery.

@Override
public Sequence<Object[]> runQuery(final DruidQuery druidQuery) {
    final Query<?> query = druidQuery.getQuery();
    if (plannerContext.getPlannerConfig().isRequireTimeCondition() && !(druidQuery.getDataSource() instanceof InlineDataSource)) {
        if (Intervals.ONLY_ETERNITY.equals(findBaseDataSourceIntervals(query))) {
            throw new CannotBuildQueryException("requireTimeCondition is enabled, all queries must include a filter condition on the __time column");
        }
    }
    int numFilters = plannerContext.getPlannerConfig().getMaxNumericInFilters();
    // Instead of IN(v1,v2,v3) user should specify IN('v1','v2','v3')
    if (numFilters != PlannerConfig.NUM_FILTER_NOT_USED) {
        if (query.getFilter() instanceof OrDimFilter) {
            OrDimFilter orDimFilter = (OrDimFilter) query.getFilter();
            int numBoundFilters = 0;
            for (DimFilter filter : orDimFilter.getFields()) {
                numBoundFilters += filter instanceof BoundDimFilter ? 1 : 0;
            }
            if (numBoundFilters > numFilters) {
                String dimension = ((BoundDimFilter) (orDimFilter.getFields().get(0))).getDimension();
                throw new UOE(StringUtils.format("The number of values in the IN clause for [%s] in query exceeds configured maxNumericFilter limit of [%s] for INs. Cast [%s] values of IN clause to String", dimension, numFilters, orDimFilter.getFields().size()));
            }
        }
    }
    final List<String> rowOrder;
    if (query instanceof TimeseriesQuery && !druidQuery.getGrouping().getDimensions().isEmpty()) {
        // Hack for timeseries queries: when generating them, DruidQuery.toTimeseriesQuery translates a dimension
        // based on a timestamp_floor expression into a 'granularity'. This is not reflected in the druidQuery's
        // output row signature, so we have to account for it here.
        // TODO: We can remove this once https://github.com/apache/druid/issues/9974 is done.
        final String timeDimension = Iterables.getOnlyElement(druidQuery.getGrouping().getDimensions()).getOutputName();
        rowOrder = druidQuery.getOutputRowSignature().getColumnNames().stream().map(f -> timeDimension.equals(f) ? ColumnHolder.TIME_COLUMN_NAME : f).collect(Collectors.toList());
    } else {
        rowOrder = druidQuery.getOutputRowSignature().getColumnNames();
    }
    final List<SqlTypeName> columnTypes = druidQuery.getOutputRowType().getFieldList().stream().map(f -> f.getType().getSqlTypeName()).collect(Collectors.toList());
    return execute(query, mapColumnList(rowOrder, fieldMapping), mapColumnList(columnTypes, fieldMapping));
}
Also used : Arrays(java.util.Arrays) DimensionHandlerUtils(org.apache.druid.segment.DimensionHandlerUtils) Object2IntOpenHashMap(it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap) IAE(org.apache.druid.java.util.common.IAE) DateTimes(org.apache.druid.java.util.common.DateTimes) Sequence(org.apache.druid.java.util.common.guava.Sequence) Collection(java.util.Collection) StringUtils(org.apache.druid.java.util.common.StringUtils) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) ISE(org.apache.druid.java.util.common.ISE) UUID(java.util.UUID) PlannerConfig(org.apache.druid.sql.calcite.planner.PlannerConfig) Collectors(java.util.stream.Collectors) QuerySegmentSpec(org.apache.druid.query.spec.QuerySegmentSpec) List(java.util.List) DimFilter(org.apache.druid.query.filter.DimFilter) Hook(org.apache.calcite.runtime.Hook) DataSourceAnalysis(org.apache.druid.query.planning.DataSourceAnalysis) QueryLifecycleFactory(org.apache.druid.server.QueryLifecycleFactory) Iterables(com.google.common.collect.Iterables) ComparableList(org.apache.druid.segment.data.ComparableList) Intervals(org.apache.druid.java.util.common.Intervals) CannotBuildQueryException(org.apache.druid.sql.calcite.rel.CannotBuildQueryException) QueryLifecycle(org.apache.druid.server.QueryLifecycle) ArrayList(java.util.ArrayList) AuthenticationResult(org.apache.druid.server.security.AuthenticationResult) Interval(org.joda.time.Interval) ColumnHolder(org.apache.druid.segment.column.ColumnHolder) Query(org.apache.druid.query.Query) Pair(org.apache.calcite.util.Pair) PlannerContext(org.apache.druid.sql.calcite.planner.PlannerContext) ComparableStringArray(org.apache.druid.segment.data.ComparableStringArray) UOE(org.apache.druid.java.util.common.UOE) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) Sequences(org.apache.druid.java.util.common.guava.Sequences) DruidQuery(org.apache.druid.sql.calcite.rel.DruidQuery) RelDataType(org.apache.calcite.rel.type.RelDataType) Access(org.apache.druid.server.security.Access) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) NlsString(org.apache.calcite.util.NlsString) InlineDataSource(org.apache.druid.query.InlineDataSource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) QueryToolChest(org.apache.druid.query.QueryToolChest) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Ints(com.google.common.primitives.Ints) Object2IntMap(it.unimi.dsi.fastutil.objects.Object2IntMap) NullHandling(org.apache.druid.common.config.NullHandling) OrDimFilter(org.apache.druid.query.filter.OrDimFilter) Calcites(org.apache.druid.sql.calcite.planner.Calcites) Evals(org.apache.druid.math.expr.Evals) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) UOE(org.apache.druid.java.util.common.UOE) NlsString(org.apache.calcite.util.NlsString) CannotBuildQueryException(org.apache.druid.sql.calcite.rel.CannotBuildQueryException) InlineDataSource(org.apache.druid.query.InlineDataSource) OrDimFilter(org.apache.druid.query.filter.OrDimFilter) DimFilter(org.apache.druid.query.filter.DimFilter) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) OrDimFilter(org.apache.druid.query.filter.OrDimFilter)

Aggregations

Sequence (org.apache.druid.java.util.common.guava.Sequence)102 Test (org.junit.Test)53 List (java.util.List)44 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)37 ResponseContext (org.apache.druid.query.context.ResponseContext)32 ImmutableList (com.google.common.collect.ImmutableList)29 Intervals (org.apache.druid.java.util.common.Intervals)28 Granularities (org.apache.druid.java.util.common.granularity.Granularities)28 QueryRunner (org.apache.druid.query.QueryRunner)28 ArrayList (java.util.ArrayList)27 VirtualColumns (org.apache.druid.segment.VirtualColumns)26 Cursor (org.apache.druid.segment.Cursor)25 QueryPlus (org.apache.druid.query.QueryPlus)24 Result (org.apache.druid.query.Result)24 NullHandling (org.apache.druid.common.config.NullHandling)22 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)22 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)21 QueryableIndexStorageAdapter (org.apache.druid.segment.QueryableIndexStorageAdapter)20 DataSegment (org.apache.druid.timeline.DataSegment)20 ImmutableMap (com.google.common.collect.ImmutableMap)18