Search in sources :

Example 1 with LuceneReferenceResolver

use of io.crate.expression.reference.doc.lucene.LuceneReferenceResolver in project crate by crate.

the class ReservoirSampler method getSamples.

private Samples getSamples(List<Reference> columns, int maxSamples, DocTableInfo docTable, Random random, Metadata metadata, CoordinatorTxnCtx coordinatorTxnCtx, List<Streamer> streamers, List<Engine.Searcher> searchersToRelease, RamAccounting ramAccounting) {
    ramAccounting.addBytes(DataTypes.LONG.fixedSize() * maxSamples);
    Reservoir<Long> fetchIdSamples = new Reservoir<>(maxSamples, random);
    ArrayList<DocIdToRow> docIdToRowsFunctionPerReader = new ArrayList<>();
    long totalNumDocs = 0;
    long totalSizeInBytes = 0;
    for (String index : docTable.concreteOpenIndices()) {
        var indexMetadata = metadata.index(index);
        if (indexMetadata == null) {
            continue;
        }
        var indexService = indicesService.indexService(indexMetadata.getIndex());
        if (indexService == null) {
            continue;
        }
        var mapperService = indexService.mapperService();
        FieldTypeLookup fieldTypeLookup = mapperService::fullName;
        var ctx = new DocInputFactory(nodeCtx, new LuceneReferenceResolver(indexService.index().getName(), fieldTypeLookup, docTable.partitionedByColumns())).getCtx(coordinatorTxnCtx);
        ctx.add(columns);
        List<Input<?>> inputs = ctx.topLevelInputs();
        List<? extends LuceneCollectorExpression<?>> expressions = ctx.expressions();
        CollectorContext collectorContext = new CollectorContext();
        for (LuceneCollectorExpression<?> expression : expressions) {
            expression.startCollect(collectorContext);
        }
        for (IndexShard indexShard : indexService) {
            if (!indexShard.routingEntry().primary()) {
                continue;
            }
            try {
                Engine.Searcher searcher = indexShard.acquireSearcher("update-table-statistics");
                searchersToRelease.add(searcher);
                totalNumDocs += searcher.getIndexReader().numDocs();
                totalSizeInBytes += indexShard.storeStats().getSizeInBytes();
                DocIdToRow docIdToRow = new DocIdToRow(searcher, inputs, expressions);
                docIdToRowsFunctionPerReader.add(docIdToRow);
                try {
                    // We do the sampling in 2 phases. First we get the docIds;
                    // then we retrieve the column values for the sampled docIds.
                    // we do this in 2 phases because the reservoir sampling might override previously seen
                    // items and we want to avoid unnecessary disk-lookup
                    var collector = new ReservoirCollector(fetchIdSamples, searchersToRelease.size() - 1);
                    searcher.search(new MatchAllDocsQuery(), collector);
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            } catch (IllegalIndexShardStateException | AlreadyClosedException ignored) {
            }
        }
    }
    var rowAccounting = new RowCellsAccountingWithEstimators(Symbols.typeView(columns), ramAccounting, 0);
    ArrayList<Row> records = new ArrayList<>();
    for (long fetchId : fetchIdSamples.samples()) {
        int readerId = FetchId.decodeReaderId(fetchId);
        DocIdToRow docIdToRow = docIdToRowsFunctionPerReader.get(readerId);
        Object[] row = docIdToRow.apply(FetchId.decodeDocId(fetchId));
        try {
            rowAccounting.accountForAndMaybeBreak(row);
        } catch (CircuitBreakingException e) {
            LOGGER.info("Stopped gathering samples for `ANALYZE` operation because circuit breaker triggered. " + "Generating statistics with {} instead of {} records", records.size(), maxSamples);
            break;
        }
        records.add(new RowN(row));
    }
    return new Samples(records, streamers, totalNumDocs, totalSizeInBytes);
}
Also used : DocInputFactory(io.crate.execution.engine.collect.DocInputFactory) ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) RowCellsAccountingWithEstimators(io.crate.breaker.RowCellsAccountingWithEstimators) Input(io.crate.data.Input) LuceneReferenceResolver(io.crate.expression.reference.doc.lucene.LuceneReferenceResolver) CollectorContext(io.crate.expression.reference.doc.lucene.CollectorContext) Engine(org.elasticsearch.index.engine.Engine) IndexShard(org.elasticsearch.index.shard.IndexShard) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) RowN(io.crate.data.RowN) FieldTypeLookup(io.crate.lucene.FieldTypeLookup) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) Row(io.crate.data.Row)

Aggregations

RowCellsAccountingWithEstimators (io.crate.breaker.RowCellsAccountingWithEstimators)1 Input (io.crate.data.Input)1 Row (io.crate.data.Row)1 RowN (io.crate.data.RowN)1 DocInputFactory (io.crate.execution.engine.collect.DocInputFactory)1 CollectorContext (io.crate.expression.reference.doc.lucene.CollectorContext)1 LuceneReferenceResolver (io.crate.expression.reference.doc.lucene.LuceneReferenceResolver)1 FieldTypeLookup (io.crate.lucene.FieldTypeLookup)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)1 CircuitBreakingException (org.elasticsearch.common.breaker.CircuitBreakingException)1 Engine (org.elasticsearch.index.engine.Engine)1 IllegalIndexShardStateException (org.elasticsearch.index.shard.IllegalIndexShardStateException)1 IndexShard (org.elasticsearch.index.shard.IndexShard)1