Search in sources :

Example 1 with SharedShardContext

use of io.crate.action.job.SharedShardContext in project crate by crate.

the class ShardCollectSource method createMultiShardScoreDocCollector.

private CrateCollector createMultiShardScoreDocCollector(RoutedCollectPhase collectPhase, BatchConsumer consumer, JobCollectContext jobCollectContext, String localNodeId) {
    Map<String, Map<String, List<Integer>>> locations = collectPhase.routing().locations();
    SharedShardContexts sharedShardContexts = jobCollectContext.sharedShardContexts();
    Map<String, List<Integer>> indexShards = locations.get(localNodeId);
    List<OrderedDocCollector> orderedDocCollectors = new ArrayList<>();
    for (Map.Entry<String, List<Integer>> entry : indexShards.entrySet()) {
        String indexName = entry.getKey();
        for (Integer shardNum : entry.getValue()) {
            ShardId shardId = new ShardId(indexName, shardNum);
            SharedShardContext context = sharedShardContexts.getOrCreateContext(shardId);
            try {
                ShardCollectorProvider shardCollectorProvider = getCollectorProviderSafe(shardId);
                orderedDocCollectors.add(shardCollectorProvider.getOrderedCollector(collectPhase, context, jobCollectContext, consumer.requiresScroll()));
            } catch (ShardNotFoundException | IllegalIndexShardStateException e) {
                throw e;
            } catch (IndexNotFoundException e) {
                if (PartitionName.isPartition(indexName)) {
                    break;
                }
                throw e;
            } catch (Throwable t) {
                throw new UnhandledServerException(t);
            }
        }
    }
    OrderBy orderBy = collectPhase.orderBy();
    assert orderBy != null : "orderBy must not be null";
    return BatchIteratorCollectorBridge.newInstance(OrderedLuceneBatchIteratorFactory.newInstance(orderedDocCollectors, collectPhase.toCollect().size(), OrderingByPosition.rowOrdering(OrderByPositionVisitor.orderByPositions(orderBy.orderBySymbols(), collectPhase.toCollect()), orderBy.reverseFlags(), orderBy.nullsFirst()), executor, consumer.requiresScroll()), consumer);
}
Also used : OrderBy(io.crate.analyze.OrderBy) ArrayList(java.util.ArrayList) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) OrderedDocCollector(io.crate.operation.collect.collectors.OrderedDocCollector) ShardId(org.elasticsearch.index.shard.ShardId) SharedShardContexts(io.crate.action.job.SharedShardContexts) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) UnhandledServerException(io.crate.exceptions.UnhandledServerException) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SharedShardContext(io.crate.action.job.SharedShardContext)

Example 2 with SharedShardContext

use of io.crate.action.job.SharedShardContext in project crate by crate.

the class LuceneShardCollectorProvider method getBuilder.

@Override
protected CrateCollector.Builder getBuilder(RoutedCollectPhase collectPhase, boolean requiresScroll, JobCollectContext jobCollectContext) {
    SharedShardContext sharedShardContext = jobCollectContext.sharedShardContexts().getOrCreateContext(indexShard.shardId());
    Engine.Searcher searcher = sharedShardContext.acquireSearcher();
    IndexShard indexShard = sharedShardContext.indexShard();
    try {
        LuceneQueryBuilder.Context queryContext = luceneQueryBuilder.convert(collectPhase.whereClause(), indexShard.mapperService(), indexShard.indexFieldDataService(), indexShard.indexService().cache());
        jobCollectContext.addSearcher(sharedShardContext.readerId(), searcher);
        InputFactory.Context<? extends LuceneCollectorExpression<?>> docCtx = docInputFactory.extractImplementations(collectPhase);
        return new CrateDocCollectorBuilder(searcher.searcher(), queryContext.query(), queryContext.minScore(), Symbols.containsColumn(collectPhase.toCollect(), DocSysColumns.SCORE), getCollectorContext(sharedShardContext.readerId(), docCtx), jobCollectContext.queryPhaseRamAccountingContext(), docCtx.topLevelInputs(), docCtx.expressions());
    } catch (Throwable t) {
        searcher.close();
        throw t;
    }
}
Also used : InputFactory(io.crate.operation.InputFactory) IndexShard(org.elasticsearch.index.shard.IndexShard) LuceneQueryBuilder(io.crate.lucene.LuceneQueryBuilder) SharedShardContext(io.crate.action.job.SharedShardContext) Engine(org.elasticsearch.index.engine.Engine) CrateDocCollectorBuilder(io.crate.operation.collect.collectors.CrateDocCollectorBuilder)

Example 3 with SharedShardContext

use of io.crate.action.job.SharedShardContext in project crate by crate.

the class FetchContext method innerPrepare.

@Override
public void innerPrepare() {
    HashMap<String, TableIdent> index2TableIdent = new HashMap<>();
    for (Map.Entry<TableIdent, Collection<String>> entry : phase.tableIndices().asMap().entrySet()) {
        for (String indexName : entry.getValue()) {
            index2TableIdent.put(indexName, entry.getKey());
        }
    }
    Set<TableIdent> tablesWithFetchRefs = new HashSet<>();
    for (Reference reference : phase.fetchRefs()) {
        tablesWithFetchRefs.add(reference.ident().tableIdent());
    }
    for (Routing routing : routingIterable) {
        Map<String, Map<String, List<Integer>>> locations = routing.locations();
        Map<String, List<Integer>> indexShards = locations.get(localNodeId);
        for (Map.Entry<String, List<Integer>> indexShardsEntry : indexShards.entrySet()) {
            String index = indexShardsEntry.getKey();
            Integer base = phase.bases().get(index);
            if (base == null) {
                continue;
            }
            TableIdent ident = index2TableIdent.get(index);
            assert ident != null : "no tableIdent found for index " + index;
            tableIdents.put(base, ident);
            toFetch.put(ident, new ArrayList<Reference>());
            for (Integer shard : indexShardsEntry.getValue()) {
                ShardId shardId = new ShardId(index, shard);
                int readerId = base + shardId.id();
                SharedShardContext shardContext = shardContexts.get(readerId);
                if (shardContext == null) {
                    shardContext = sharedShardContexts.createContext(shardId, readerId);
                    shardContexts.put(readerId, shardContext);
                    if (tablesWithFetchRefs.contains(ident)) {
                        try {
                            searchers.put(readerId, shardContext.acquireSearcher());
                        } catch (IndexNotFoundException e) {
                            if (!PartitionName.isPartition(index)) {
                                throw e;
                            }
                        }
                    }
                }
            }
        }
    }
    for (Reference reference : phase.fetchRefs()) {
        Collection<Reference> references = toFetch.get(reference.ident().tableIdent());
        if (references != null) {
            references.add(reference);
        }
    }
}
Also used : IntObjectHashMap(com.carrotsearch.hppc.IntObjectHashMap) Reference(io.crate.metadata.Reference) Routing(io.crate.metadata.Routing) TableIdent(io.crate.metadata.TableIdent) ShardId(org.elasticsearch.index.shard.ShardId) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IntObjectHashMap(com.carrotsearch.hppc.IntObjectHashMap) SharedShardContext(io.crate.action.job.SharedShardContext)

Aggregations

SharedShardContext (io.crate.action.job.SharedShardContext)3 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)2 ShardId (org.elasticsearch.index.shard.ShardId)2 IntObjectHashMap (com.carrotsearch.hppc.IntObjectHashMap)1 SharedShardContexts (io.crate.action.job.SharedShardContexts)1 OrderBy (io.crate.analyze.OrderBy)1 UnhandledServerException (io.crate.exceptions.UnhandledServerException)1 LuceneQueryBuilder (io.crate.lucene.LuceneQueryBuilder)1 Reference (io.crate.metadata.Reference)1 Routing (io.crate.metadata.Routing)1 TableIdent (io.crate.metadata.TableIdent)1 InputFactory (io.crate.operation.InputFactory)1 CrateDocCollectorBuilder (io.crate.operation.collect.collectors.CrateDocCollectorBuilder)1 OrderedDocCollector (io.crate.operation.collect.collectors.OrderedDocCollector)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Engine (org.elasticsearch.index.engine.Engine)1 IllegalIndexShardStateException (org.elasticsearch.index.shard.IllegalIndexShardStateException)1