Search in sources :

Example 1 with Projectors

use of io.crate.execution.engine.pipeline.Projectors in project crate by crate.

the class PKLookupOperation method lookup.

public BatchIterator<Row> lookup(UUID jobId, TransactionContext txnCtx, Supplier<RamAccounting> ramAccountingSupplier, Supplier<MemoryManager> memoryManagerSupplier, boolean ignoreMissing, Map<ShardId, List<PKAndVersion>> idsByShard, Collection<? extends Projection> projections, boolean requiresScroll, Function<Doc, Row> resultToRow) {
    ArrayList<BatchIterator<Row>> iterators = new ArrayList<>(idsByShard.size());
    for (Map.Entry<ShardId, List<PKAndVersion>> idsByShardEntry : idsByShard.entrySet()) {
        ShardId shardId = idsByShardEntry.getKey();
        IndexService indexService = indicesService.indexService(shardId.getIndex());
        if (indexService == null) {
            if (ignoreMissing) {
                continue;
            }
            throw new IndexNotFoundException(shardId.getIndex());
        }
        IndexShard shard = indexService.getShardOrNull(shardId.id());
        if (shard == null) {
            if (ignoreMissing) {
                continue;
            }
            throw new ShardNotFoundException(shardId);
        }
        Stream<Row> rowStream = idsByShardEntry.getValue().stream().map(pkAndVersion -> lookupDoc(shard, pkAndVersion.id(), pkAndVersion.version(), VersionType.EXTERNAL, pkAndVersion.seqNo(), pkAndVersion.primaryTerm())).filter(Objects::nonNull).map(resultToRow);
        if (projections.isEmpty()) {
            final Iterable<Row> rowIterable = requiresScroll ? rowStream.map(row -> new RowN(row.materialize())).collect(Collectors.toList()) : rowStream::iterator;
            iterators.add(InMemoryBatchIterator.of(rowIterable, SentinelRow.SENTINEL, true));
        } else {
            ProjectorFactory projectorFactory;
            try {
                projectorFactory = shardCollectSource.getProjectorFactory(shardId);
            } catch (ShardNotFoundException e) {
                if (ignoreMissing) {
                    continue;
                }
                throw e;
            }
            Projectors projectors = new Projectors(projections, jobId, txnCtx, ramAccountingSupplier.get(), memoryManagerSupplier.get(), projectorFactory);
            final Iterable<Row> rowIterable = requiresScroll && !projectors.providesIndependentScroll() ? rowStream.map(row -> new RowN(row.materialize())).collect(Collectors.toList()) : rowStream::iterator;
            iterators.add(projectors.wrap(InMemoryBatchIterator.of(rowIterable, SentinelRow.SENTINEL, true)));
        }
    }
    // noinspection unchecked
    return CompositeBatchIterator.seqComposite(iterators.toArray(new BatchIterator[0]));
}
Also used : IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) ArrayList(java.util.ArrayList) BatchIterator(io.crate.data.BatchIterator) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) CompositeBatchIterator(io.crate.data.CompositeBatchIterator) ShardId(org.elasticsearch.index.shard.ShardId) Projectors(io.crate.execution.engine.pipeline.Projectors) RowN(io.crate.data.RowN) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) ProjectorFactory(io.crate.execution.engine.pipeline.ProjectorFactory) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ArrayList(java.util.ArrayList) List(java.util.List) Row(io.crate.data.Row) SentinelRow(io.crate.data.SentinelRow) Map(java.util.Map)

Example 2 with Projectors

use of io.crate.execution.engine.pipeline.Projectors in project crate by crate.

the class ShardCollectSource method getIterator.

@Override
public CompletableFuture<BatchIterator<Row>> getIterator(TransactionContext txnCtx, CollectPhase phase, CollectTask collectTask, boolean supportMoveToStart) {
    RoutedCollectPhase collectPhase = (RoutedCollectPhase) phase;
    String localNodeId = clusterService.localNode().getId();
    Projectors projectors = new Projectors(collectPhase.projections(), collectPhase.jobId(), collectTask.txnCtx(), collectTask.getRamAccounting(), collectTask.memoryManager(), sharedProjectorFactory);
    boolean requireMoveToStartSupport = supportMoveToStart && !projectors.providesIndependentScroll();
    if (collectPhase.maxRowGranularity() == RowGranularity.SHARD) {
        return CompletableFuture.completedFuture(projectors.wrap(InMemoryBatchIterator.of(getShardsIterator(collectTask.txnCtx(), collectPhase, localNodeId), SentinelRow.SENTINEL, true)));
    }
    OrderBy orderBy = collectPhase.orderBy();
    if (collectPhase.maxRowGranularity() == RowGranularity.DOC && orderBy != null) {
        return createMultiShardScoreDocCollector(collectPhase, requireMoveToStartSupport, collectTask, localNodeId).thenApply(projectors::wrap);
    }
    boolean hasShardProjections = Projections.hasAnyShardProjections(collectPhase.projections());
    Map<String, IntIndexedContainer> indexShards = collectPhase.routing().locations().get(localNodeId);
    List<CompletableFuture<BatchIterator<Row>>> iterators = indexShards == null ? Collections.emptyList() : getIterators(collectTask, collectPhase, requireMoveToStartSupport, indexShards);
    final CompletableFuture<BatchIterator<Row>> result;
    switch(iterators.size()) {
        case 0:
            result = CompletableFuture.completedFuture(InMemoryBatchIterator.empty(SentinelRow.SENTINEL));
            break;
        case 1:
            result = iterators.get(0);
            break;
        default:
            if (hasShardProjections) {
                // use AsyncCompositeBatchIterator for multi-threaded loadNextBatch
                // in order to process shard-based projections concurrently
                // noinspection unchecked
                result = CompletableFutures.allAsList(iterators).thenApply(its -> CompositeBatchIterator.asyncComposite(executor, availableThreads, its.toArray(new BatchIterator[0])));
            } else {
                // noinspection unchecked
                result = CompletableFutures.allAsList(iterators).thenApply(its -> CompositeBatchIterator.seqComposite(its.toArray(new BatchIterator[0])));
            }
    }
    return result.thenApply(it -> projectors.wrap(it));
}
Also used : OrderBy(io.crate.analyze.OrderBy) ShardId(org.elasticsearch.index.shard.ShardId) IndexParts(io.crate.metadata.IndexParts) TransactionContext(io.crate.metadata.TransactionContext) Projections(io.crate.execution.dsl.projection.Projections) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) OrderedDocCollector(io.crate.execution.engine.collect.collectors.OrderedDocCollector) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) NodeLimits(io.crate.execution.jobs.NodeLimits) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) ProjectorFactory(io.crate.execution.engine.pipeline.ProjectorFactory) Settings(org.elasticsearch.common.settings.Settings) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Map(java.util.Map) SharedShardContexts(io.crate.execution.jobs.SharedShardContexts) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ThreadPools.numIdleThreads(io.crate.execution.support.ThreadPools.numIdleThreads) OrderingByPosition(io.crate.execution.engine.sort.OrderingByPosition) ShardRowContext(io.crate.expression.reference.sys.shard.ShardRowContext) DocSysColumns(io.crate.metadata.doc.DocSysColumns) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) NodeContext(io.crate.metadata.NodeContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) OrderedLuceneBatchIteratorFactory(io.crate.execution.engine.collect.collectors.OrderedLuceneBatchIteratorFactory) StaticTableReferenceResolver(io.crate.expression.reference.StaticTableReferenceResolver) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) CompletableFutures(io.crate.concurrent.CompletableFutures) RowsTransformer(io.crate.execution.engine.collect.RowsTransformer) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) Iterables(io.crate.common.collections.Iterables) CollectTask(io.crate.execution.engine.collect.CollectTask) SysShardsTableInfo(io.crate.metadata.sys.SysShardsTableInfo) OrderByPositionVisitor(io.crate.planner.consumer.OrderByPositionVisitor) List(java.util.List) Exceptions(io.crate.exceptions.Exceptions) Logger(org.apache.logging.log4j.Logger) OrderBy(io.crate.analyze.OrderBy) Row(io.crate.data.Row) Singleton(org.elasticsearch.common.inject.Singleton) Projectors(io.crate.execution.engine.pipeline.Projectors) SharedShardContext(io.crate.execution.jobs.SharedShardContext) SentinelRow(io.crate.data.SentinelRow) MapBackedRefResolver(io.crate.metadata.MapBackedRefResolver) CompositeBatchIterator(io.crate.data.CompositeBatchIterator) RemoteCollectorFactory(io.crate.execution.engine.collect.RemoteCollectorFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ClusterService(org.elasticsearch.cluster.service.ClusterService) RowAccountingWithEstimators(io.crate.breaker.RowAccountingWithEstimators) CompletableFuture(java.util.concurrent.CompletableFuture) BatchIterator(io.crate.data.BatchIterator) Index(org.elasticsearch.index.Index) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Inject(org.elasticsearch.common.inject.Inject) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) Metadata(org.elasticsearch.cluster.metadata.Metadata) UnassignedShard(io.crate.metadata.shard.unassigned.UnassignedShard) Symbols(io.crate.expression.symbol.Symbols) CollectPhase(io.crate.execution.dsl.phases.CollectPhase) IndicesService(org.elasticsearch.indices.IndicesService) IntSupplier(java.util.function.IntSupplier) Nullable(javax.annotation.Nullable) EsExecutors(org.elasticsearch.common.util.concurrent.EsExecutors) IndexEventListener(org.elasticsearch.index.shard.IndexEventListener) Executor(java.util.concurrent.Executor) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) DataType(io.crate.types.DataType) ProjectionToProjectorVisitor(io.crate.execution.engine.pipeline.ProjectionToProjectorVisitor) TransportActionProvider(io.crate.execution.TransportActionProvider) RowGranularity(io.crate.metadata.RowGranularity) ShardCollectorProvider(io.crate.execution.engine.collect.ShardCollectorProvider) Suppliers(io.crate.common.Suppliers) InputFactory(io.crate.expression.InputFactory) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) CompositeBatchIterator(io.crate.data.CompositeBatchIterator) BatchIterator(io.crate.data.BatchIterator) Projectors(io.crate.execution.engine.pipeline.Projectors) CompletableFuture(java.util.concurrent.CompletableFuture) Row(io.crate.data.Row) SentinelRow(io.crate.data.SentinelRow) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase)

Aggregations

BatchIterator (io.crate.data.BatchIterator)2 CompositeBatchIterator (io.crate.data.CompositeBatchIterator)2 InMemoryBatchIterator (io.crate.data.InMemoryBatchIterator)2 Row (io.crate.data.Row)2 SentinelRow (io.crate.data.SentinelRow)2 ProjectorFactory (io.crate.execution.engine.pipeline.ProjectorFactory)2 Projectors (io.crate.execution.engine.pipeline.Projectors)2 IntIndexedContainer (com.carrotsearch.hppc.IntIndexedContainer)1 IntCursor (com.carrotsearch.hppc.cursors.IntCursor)1 OrderBy (io.crate.analyze.OrderBy)1 RowAccountingWithEstimators (io.crate.breaker.RowAccountingWithEstimators)1 Suppliers (io.crate.common.Suppliers)1 Iterables (io.crate.common.collections.Iterables)1 CompletableFutures (io.crate.concurrent.CompletableFutures)1 RowN (io.crate.data.RowN)1 Exceptions (io.crate.exceptions.Exceptions)1 TransportActionProvider (io.crate.execution.TransportActionProvider)1 CollectPhase (io.crate.execution.dsl.phases.CollectPhase)1 RoutedCollectPhase (io.crate.execution.dsl.phases.RoutedCollectPhase)1 Projections (io.crate.execution.dsl.projection.Projections)1