Search in sources :

Example 1 with BlockBasedRamAccounting

use of io.crate.breaker.BlockBasedRamAccounting in project crate by crate.

the class JobSetup method registerContextPhases.

private void registerContextPhases(Iterable<? extends NodeOperation> nodeOperations, Context context) {
    for (NodeOperation nodeOperation : nodeOperations) {
        // context for nodeOperations without dependencies can be built immediately (e.g. FetchPhase)
        if (nodeOperation.downstreamExecutionPhaseId() == NodeOperation.NO_DOWNSTREAM) {
            LOGGER.trace("Building context for nodeOp without downstream: {}", nodeOperation);
            createContexts(nodeOperation.executionPhase(), context);
            context.opCtx.builtNodeOperations.set(nodeOperation.executionPhase().phaseId());
        }
        if (ExecutionPhases.hasDirectResponseDownstream(nodeOperation.downstreamNodes())) {
            var executionPhase = nodeOperation.executionPhase();
            CircuitBreaker breaker = breaker();
            int ramAccountingBlockSizeInBytes = BlockBasedRamAccounting.blockSizeInBytes(breaker.getLimit());
            var ramAccounting = new BlockBasedRamAccounting(b -> breaker.addEstimateBytesAndMaybeBreak(b, executionPhase.label()), ramAccountingBlockSizeInBytes);
            Streamer<?>[] streamers = StreamerVisitor.streamersFromOutputs(executionPhase);
            SingleBucketBuilder bucketBuilder = new SingleBucketBuilder(streamers, ramAccounting);
            context.directResponseFutures.add(bucketBuilder.completionFuture().whenComplete((res, err) -> ramAccounting.close()));
            context.registerBatchConsumer(nodeOperation.downstreamExecutionPhaseId(), bucketBuilder);
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) DistributionType(io.crate.planner.distribution.DistributionType) SessionSettings(io.crate.metadata.settings.SessionSettings) FetchPhase(io.crate.execution.dsl.phases.FetchPhase) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) Projections.nodeProjections(io.crate.execution.dsl.projection.Projections.nodeProjections) MapSideDataCollectOperation(io.crate.execution.engine.collect.MapSideDataCollectOperation) ProjectorFactory(io.crate.execution.engine.pipeline.ProjectorFactory) BlockBasedRamAccounting(io.crate.breaker.BlockBasedRamAccounting) Projections.shardProjections(io.crate.execution.dsl.projection.Projections.shardProjections) IntArrayList(com.carrotsearch.hppc.IntArrayList) Map(java.util.Map) Collector(java.util.stream.Collector) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) ProjectingRowConsumer(io.crate.execution.engine.pipeline.ProjectingRowConsumer) PKLookupOperation(io.crate.execution.engine.collect.PKLookupOperation) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) AggregationProjection(io.crate.execution.dsl.projection.AggregationProjection) Logger(org.apache.logging.log4j.Logger) Row(io.crate.data.Row) ExecutionPhase(io.crate.execution.dsl.phases.ExecutionPhase) IntObjectCursor(com.carrotsearch.hppc.cursors.IntObjectCursor) Tuple(io.crate.common.collections.Tuple) NestedLoopOperation(io.crate.execution.engine.join.NestedLoopOperation) ClusterService(org.elasticsearch.cluster.service.ClusterService) Operation(io.crate.metadata.table.Operation) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) ArrayList(java.util.ArrayList) Routing(io.crate.metadata.Routing) RowCellsAccountingWithEstimators(io.crate.breaker.RowCellsAccountingWithEstimators) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) IndicesService(org.elasticsearch.indices.IndicesService) LongObjectHashMap(com.carrotsearch.hppc.LongObjectHashMap) Nullable(javax.annotation.Nullable) Executor(java.util.concurrent.Executor) SystemCollectSource(io.crate.execution.engine.collect.sources.SystemCollectSource) RamAccounting(io.crate.breaker.RamAccounting) IncrementalPageBucketReceiver(io.crate.execution.IncrementalPageBucketReceiver) ConcurrentRamAccounting(io.crate.breaker.ConcurrentRamAccounting) HierarchyCircuitBreakerService(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService) IntObjectHashMap(com.carrotsearch.hppc.IntObjectHashMap) Schemas(io.crate.metadata.Schemas) NodeOperation(io.crate.execution.dsl.phases.NodeOperation) AggregationPipe(io.crate.execution.engine.aggregation.AggregationPipe) InputFactory(io.crate.expression.InputFactory) MemoryManagerFactory(io.crate.memory.MemoryManagerFactory) TransactionContext(io.crate.metadata.TransactionContext) PKAndVersion(io.crate.planner.operators.PKAndVersion) HashJoinPhase(io.crate.execution.dsl.phases.HashJoinPhase) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) MergePhase(io.crate.execution.dsl.phases.MergePhase) Settings(org.elasticsearch.common.settings.Settings) DistributingConsumerFactory(io.crate.execution.engine.distribution.DistributingConsumerFactory) Locale(java.util.Locale) ThreadPool(org.elasticsearch.threadpool.ThreadPool) LongObjectMap(com.carrotsearch.hppc.LongObjectMap) NodeContext(io.crate.metadata.NodeContext) HashJoinOperation(io.crate.execution.engine.join.HashJoinOperation) Streamer(io.crate.Streamer) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IntContainer(com.carrotsearch.hppc.IntContainer) NestedLoopPhase(io.crate.execution.dsl.phases.NestedLoopPhase) UUID(java.util.UUID) CollectTask(io.crate.execution.engine.collect.CollectTask) Objects(java.util.Objects) List(java.util.List) ExecutionPhaseVisitor(io.crate.execution.dsl.phases.ExecutionPhaseVisitor) Projection(io.crate.execution.dsl.projection.Projection) DataTypes(io.crate.types.DataTypes) Singleton(org.elasticsearch.common.inject.Singleton) Paging(io.crate.data.Paging) FetchTask(io.crate.execution.engine.fetch.FetchTask) StreamBucket(io.crate.execution.engine.distribution.StreamBucket) SingleBucketBuilder(io.crate.execution.engine.distribution.SingleBucketBuilder) RowAccountingWithEstimators(io.crate.breaker.RowAccountingWithEstimators) CompletableFuture(java.util.concurrent.CompletableFuture) CountPhase(io.crate.execution.dsl.phases.CountPhase) Inject(org.elasticsearch.common.inject.Inject) ObjectProcedure(com.carrotsearch.hppc.procedures.ObjectProcedure) Node(org.elasticsearch.node.Node) CollectPhase(io.crate.execution.dsl.phases.CollectPhase) PKLookupPhase(io.crate.execution.dsl.phases.PKLookupPhase) UpstreamPhase(io.crate.execution.dsl.phases.UpstreamPhase) CountOperation(io.crate.execution.engine.collect.count.CountOperation) GroupProjection(io.crate.execution.dsl.projection.GroupProjection) StreamerVisitor(io.crate.planner.node.StreamerVisitor) IntHashSet(com.carrotsearch.hppc.IntHashSet) ShardCollectSource(io.crate.execution.engine.collect.sources.ShardCollectSource) IntCollection(com.carrotsearch.hppc.IntCollection) GroupingProjector(io.crate.execution.engine.aggregation.GroupingProjector) ProjectionToProjectorVisitor(io.crate.execution.engine.pipeline.ProjectionToProjectorVisitor) MemoryManager(io.crate.memory.MemoryManager) TransportActionProvider(io.crate.execution.TransportActionProvider) PagingIterator(io.crate.execution.engine.distribution.merge.PagingIterator) IntObjectMap(com.carrotsearch.hppc.IntObjectMap) RowConsumer(io.crate.data.RowConsumer) RowFilter(io.crate.expression.RowFilter) BitSet(java.util.BitSet) ExecutionPhases(io.crate.execution.dsl.phases.ExecutionPhases) LogManager(org.apache.logging.log4j.LogManager) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) BlockBasedRamAccounting(io.crate.breaker.BlockBasedRamAccounting) Streamer(io.crate.Streamer) NodeOperation(io.crate.execution.dsl.phases.NodeOperation) SingleBucketBuilder(io.crate.execution.engine.distribution.SingleBucketBuilder)

Example 2 with BlockBasedRamAccounting

use of io.crate.breaker.BlockBasedRamAccounting in project crate by crate.

the class ReaderBucketsTest method test_reader_bucket_accounts_memory_for_added_rows.

@Test
public void test_reader_bucket_accounts_memory_for_added_rows() throws Exception {
    var e = SQLExecutor.builder(clusterService).addTable("create table t1 (x text)").build();
    var t1 = e.resolveTableInfo("t1");
    var x = (Reference) e.asSymbol("x");
    var fetchSource = new FetchSource();
    fetchSource.addFetchIdColumn(new InputColumn(0, DataTypes.LONG));
    fetchSource.addRefToFetch(x);
    var fetchRows = FetchRows.create(CoordinatorTxnCtx.systemTransactionContext(), TestingHelpers.createNodeContext(), Map.of(t1.ident(), fetchSource), List.of(new FetchReference(new InputColumn(0, DataTypes.LONG), x), new InputColumn(1, DataTypes.INTEGER)));
    var bytesAccounted = new AtomicLong();
    var ramAccounting = new BlockBasedRamAccounting(bytes -> bytesAccounted.addAndGet(bytes), 1024);
    int readerId = 1;
    var readerBuckets = new ReaderBuckets(fetchRows, reader -> fetchSource, new EstimateCellsSize(List.of(DataTypes.LONG, DataTypes.INTEGER)), ramAccounting);
    long fetchId = FetchId.encode(readerId, 1);
    readerBuckets.add(new RowN(fetchId, 42));
    assertThat(bytesAccounted.get(), is(1024L));
    assertThat(readerBuckets.ramBytesUsed(), is(40L));
    IntObjectHashMap<Bucket> bucketsByReader = new IntObjectHashMap<>();
    bucketsByReader.put(readerId, new CollectionBucket(List.<Object[]>of(new Object[] { "I eat memory for breakfast" })));
    IntHashSet readerIds = new IntHashSet(2);
    readerIds.add(readerId);
    readerBuckets.generateToFetch(readerIds);
    try (var outputRows = readerBuckets.getOutputRows(List.of(bucketsByReader))) {
        assertThat(bytesAccounted.get(), is(1024L));
        assertThat(readerBuckets.ramBytesUsed(), is(136L));
    }
    assertThat("After outputRows are closed the readerBuckets are released", readerBuckets.ramBytesUsed(), is(0L));
}
Also used : FetchSource(io.crate.planner.node.fetch.FetchSource) EstimateCellsSize(io.crate.breaker.EstimateCellsSize) Reference(io.crate.metadata.Reference) FetchReference(io.crate.expression.symbol.FetchReference) IntObjectHashMap(com.carrotsearch.hppc.IntObjectHashMap) IntHashSet(com.carrotsearch.hppc.IntHashSet) AtomicLong(java.util.concurrent.atomic.AtomicLong) BlockBasedRamAccounting(io.crate.breaker.BlockBasedRamAccounting) RowN(io.crate.data.RowN) Bucket(io.crate.data.Bucket) CollectionBucket(io.crate.data.CollectionBucket) InputColumn(io.crate.expression.symbol.InputColumn) FetchReference(io.crate.expression.symbol.FetchReference) CollectionBucket(io.crate.data.CollectionBucket) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 3 with BlockBasedRamAccounting

use of io.crate.breaker.BlockBasedRamAccounting in project crate by crate.

the class NodeFetchOperation method doFetch.

private CompletableFuture<? extends IntObjectMap<StreamBucket>> doFetch(FetchTask fetchTask, IntObjectMap<IntArrayList> toFetch) throws Exception {
    HashMap<RelationName, TableFetchInfo> tableFetchInfos = getTableFetchInfos(fetchTask);
    // RamAccounting is per doFetch call instead of per FetchTask/fetchPhase
    // To be able to free up the memory count when the operation is complete
    final var ramAccounting = ConcurrentRamAccounting.forCircuitBreaker("fetch-" + fetchTask.id(), circuitBreaker);
    ArrayList<Supplier<StreamBucket>> collectors = new ArrayList<>(toFetch.size());
    for (IntObjectCursor<IntArrayList> toFetchCursor : toFetch) {
        final int readerId = toFetchCursor.key;
        final IntArrayList docIds = toFetchCursor.value;
        RelationName ident = fetchTask.tableIdent(readerId);
        final TableFetchInfo tfi = tableFetchInfos.get(ident);
        assert tfi != null : "tfi must not be null";
        var collector = tfi.createCollector(readerId, new BlockBasedRamAccounting(ramAccounting::addBytes, BlockBasedRamAccounting.MAX_BLOCK_SIZE_IN_BYTES));
        collectors.add(() -> collector.collect(docIds));
    }
    return ThreadPools.runWithAvailableThreads(executor, ThreadPools.numIdleThreads(executor, numProcessors), collectors).thenApply(buckets -> {
        var toFetchIt = toFetch.iterator();
        assert toFetch.size() == buckets.size() : "Must have a bucket per reader and they must be in the same order";
        IntObjectHashMap<StreamBucket> bucketByReader = new IntObjectHashMap<>(toFetch.size());
        for (var bucket : buckets) {
            assert toFetchIt.hasNext() : "toFetchIt must have an element if there is one in buckets";
            int readerId = toFetchIt.next().key;
            bucketByReader.put(readerId, bucket);
        }
        return bucketByReader;
    }).whenComplete((result, err) -> ramAccounting.close());
}
Also used : IntObjectCursor(com.carrotsearch.hppc.cursors.IntObjectCursor) StreamBucket(io.crate.execution.engine.distribution.StreamBucket) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RelationName(io.crate.metadata.RelationName) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) RootTask(io.crate.execution.jobs.RootTask) Supplier(java.util.function.Supplier) JobsLogs(io.crate.execution.engine.collect.stats.JobsLogs) ArrayList(java.util.ArrayList) BlockBasedRamAccounting(io.crate.breaker.BlockBasedRamAccounting) IntArrayList(com.carrotsearch.hppc.IntArrayList) Symbols(io.crate.expression.symbol.Symbols) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) Nullable(javax.annotation.Nullable) LuceneReferenceResolver(io.crate.expression.reference.doc.lucene.LuceneReferenceResolver) Streamer(io.crate.Streamer) Collection(java.util.Collection) IndexService(org.elasticsearch.index.IndexService) Reference(io.crate.metadata.Reference) UUID(java.util.UUID) RamAccounting(io.crate.breaker.RamAccounting) LuceneCollectorExpression(io.crate.expression.reference.doc.lucene.LuceneCollectorExpression) TasksService(io.crate.execution.jobs.TasksService) IntObjectMap(com.carrotsearch.hppc.IntObjectMap) ConcurrentRamAccounting(io.crate.breaker.ConcurrentRamAccounting) IntObjectHashMap(com.carrotsearch.hppc.IntObjectHashMap) SQLExceptions(io.crate.exceptions.SQLExceptions) ThreadPools(io.crate.execution.support.ThreadPools) IntObjectHashMap(com.carrotsearch.hppc.IntObjectHashMap) ArrayList(java.util.ArrayList) IntArrayList(com.carrotsearch.hppc.IntArrayList) BlockBasedRamAccounting(io.crate.breaker.BlockBasedRamAccounting) RelationName(io.crate.metadata.RelationName) Supplier(java.util.function.Supplier) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 4 with BlockBasedRamAccounting

use of io.crate.breaker.BlockBasedRamAccounting in project crate by crate.

the class ReservoirSampler method getSamples.

public Samples getSamples(RelationName relationName, List<Reference> columns, int maxSamples) {
    TableInfo table;
    try {
        table = schemas.getTableInfo(relationName);
    } catch (RelationUnknown e) {
        return Samples.EMPTY;
    }
    if (!(table instanceof DocTableInfo)) {
        return Samples.EMPTY;
    }
    DocTableInfo docTable = (DocTableInfo) table;
    Random random = Randomness.get();
    Metadata metadata = clusterService.state().metadata();
    CoordinatorTxnCtx coordinatorTxnCtx = CoordinatorTxnCtx.systemTransactionContext();
    List<Streamer> streamers = Arrays.asList(Symbols.streamerArray(columns));
    List<Engine.Searcher> searchersToRelease = new ArrayList<>();
    CircuitBreaker breaker = circuitBreakerService.getBreaker(HierarchyCircuitBreakerService.QUERY);
    RamAccounting ramAccounting = new BlockBasedRamAccounting(b -> breaker.addEstimateBytesAndMaybeBreak(b, "Reservoir-sampling"), MAX_BLOCK_SIZE_IN_BYTES);
    try {
        return getSamples(columns, maxSamples, docTable, random, metadata, coordinatorTxnCtx, streamers, searchersToRelease, ramAccounting);
    } finally {
        ramAccounting.close();
        for (Engine.Searcher searcher : searchersToRelease) {
            searcher.close();
        }
    }
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) BlockBasedRamAccounting(io.crate.breaker.BlockBasedRamAccounting) RamAccounting(io.crate.breaker.RamAccounting) RelationUnknown(io.crate.exceptions.RelationUnknown) Metadata(org.elasticsearch.cluster.metadata.Metadata) ArrayList(java.util.ArrayList) BlockBasedRamAccounting(io.crate.breaker.BlockBasedRamAccounting) Random(java.util.Random) Streamer(io.crate.Streamer) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) Engine(org.elasticsearch.index.engine.Engine)

Example 5 with BlockBasedRamAccounting

use of io.crate.breaker.BlockBasedRamAccounting in project crate by crate.

the class SqlHttpHandler method executeSimpleRequest.

private CompletableFuture<XContentBuilder> executeSimpleRequest(Session session, String stmt, List<Object> args, boolean includeTypes) throws IOException {
    long startTimeInNs = System.nanoTime();
    session.parse(UNNAMED, stmt, emptyList());
    session.bind(UNNAMED, UNNAMED, args == null ? emptyList() : args, null);
    DescribeResult description = session.describe('P', UNNAMED);
    List<Symbol> resultFields = description.getFields();
    ResultReceiver<XContentBuilder> resultReceiver;
    if (resultFields == null) {
        resultReceiver = new RestRowCountReceiver(JsonXContent.contentBuilder(), startTimeInNs, includeTypes);
    } else {
        CircuitBreaker breaker = circuitBreakerProvider.apply(HierarchyCircuitBreakerService.QUERY);
        RamAccounting ramAccounting = new BlockBasedRamAccounting(b -> breaker.addEstimateBytesAndMaybeBreak(b, "http-result"), MAX_BLOCK_SIZE_IN_BYTES);
        resultReceiver = new RestResultSetReceiver(JsonXContent.contentBuilder(), resultFields, startTimeInNs, new RowAccountingWithEstimators(Symbols.typeView(resultFields), ramAccounting), includeTypes);
        resultReceiver.completionFuture().whenComplete((result, error) -> ramAccounting.close());
    }
    session.execute(UNNAMED, 0, resultReceiver);
    return session.sync().thenCompose(ignored -> resultReceiver.completionFuture());
}
Also used : DescribeResult(io.crate.action.sql.DescribeResult) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) BlockBasedRamAccounting(io.crate.breaker.BlockBasedRamAccounting) BlockBasedRamAccounting(io.crate.breaker.BlockBasedRamAccounting) RamAccounting(io.crate.breaker.RamAccounting) Symbol(io.crate.expression.symbol.Symbol) RowAccountingWithEstimators(io.crate.breaker.RowAccountingWithEstimators) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

BlockBasedRamAccounting (io.crate.breaker.BlockBasedRamAccounting)5 RamAccounting (io.crate.breaker.RamAccounting)4 IntObjectHashMap (com.carrotsearch.hppc.IntObjectHashMap)3 Streamer (io.crate.Streamer)3 CircuitBreaker (org.elasticsearch.common.breaker.CircuitBreaker)3 IntArrayList (com.carrotsearch.hppc.IntArrayList)2 IntHashSet (com.carrotsearch.hppc.IntHashSet)2 IntObjectMap (com.carrotsearch.hppc.IntObjectMap)2 IntObjectCursor (com.carrotsearch.hppc.cursors.IntObjectCursor)2 ConcurrentRamAccounting (io.crate.breaker.ConcurrentRamAccounting)2 RowAccountingWithEstimators (io.crate.breaker.RowAccountingWithEstimators)2 ArrayList (java.util.ArrayList)2 IntCollection (com.carrotsearch.hppc.IntCollection)1 IntContainer (com.carrotsearch.hppc.IntContainer)1 IntIndexedContainer (com.carrotsearch.hppc.IntIndexedContainer)1 LongObjectHashMap (com.carrotsearch.hppc.LongObjectHashMap)1 LongObjectMap (com.carrotsearch.hppc.LongObjectMap)1 IntCursor (com.carrotsearch.hppc.cursors.IntCursor)1 ObjectProcedure (com.carrotsearch.hppc.procedures.ObjectProcedure)1 DescribeResult (io.crate.action.sql.DescribeResult)1