Search in sources :

Example 1 with LocalMemoryContext

use of io.trino.memory.context.LocalMemoryContext in project trino by trinodb.

the class SpoolingExchangeOutputBuffer method updateMemoryUsage.

private void updateMemoryUsage(long bytes) {
    LocalMemoryContext context = getSystemMemoryContextOrNull();
    if (context != null) {
        context.setBytes(bytes);
    }
    updatePeakMemoryUsage(bytes);
}
Also used : LocalMemoryContext(io.trino.memory.context.LocalMemoryContext)

Example 2 with LocalMemoryContext

use of io.trino.memory.context.LocalMemoryContext in project trino by trinodb.

the class OutputBufferMemoryManager method close.

public synchronized void close() {
    updateMemoryUsage(-bufferedBytes.get());
    LocalMemoryContext memoryContext = getMemoryContextOrNull();
    if (memoryContext != null) {
        memoryContext.close();
    }
    closed = true;
}
Also used : LocalMemoryContext(io.trino.memory.context.LocalMemoryContext)

Example 3 with LocalMemoryContext

use of io.trino.memory.context.LocalMemoryContext in project trino by trinodb.

the class OutputBufferMemoryManager method updateMemoryUsage.

public void updateMemoryUsage(long bytesAdded) {
    // If the memoryContext doesn't exist, the task is probably already
    // aborted, so we can just return (see the comment in getMemoryContextOrNull()).
    LocalMemoryContext memoryContext = getMemoryContextOrNull();
    if (memoryContext == null) {
        return;
    }
    ListenableFuture<Void> waitForMemory = null;
    SettableFuture<Void> notifyUnblocked = null;
    long currentBufferedBytes;
    synchronized (this) {
        // we can also safely ignore any calls after OutputBufferMemoryManager is closed.
        if (closed) {
            return;
        }
        currentBufferedBytes = bufferedBytes.updateAndGet(bytes -> {
            long result = bytes + bytesAdded;
            checkArgument(result >= 0, "bufferedBytes (%s) plus delta (%s) would be negative", bytes, bytesAdded);
            return result;
        });
        ListenableFuture<Void> blockedOnMemory = memoryContext.setBytes(currentBufferedBytes);
        if (!blockedOnMemory.isDone()) {
            if (this.blockedOnMemory != blockedOnMemory) {
                this.blockedOnMemory = blockedOnMemory;
                // only register a callback when blocked and the future is different
                waitForMemory = blockedOnMemory;
            }
        } else {
            this.blockedOnMemory = NOT_BLOCKED;
            if (currentBufferedBytes <= maxBufferedBytes || !blockOnFull.get()) {
                // Complete future in a new thread to avoid making a callback on the caller thread.
                // This make is easier for callers to use this class since they can update the memory
                // usage while holding locks.
                notifyUnblocked = this.bufferBlockedFuture;
                this.bufferBlockedFuture = null;
            }
        }
    }
    peakMemoryUsage.accumulateAndGet(currentBufferedBytes, Math::max);
    // Notify listeners outside of the critical section
    notifyListener(notifyUnblocked);
    if (waitForMemory != null) {
        waitForMemory.addListener(this::onMemoryAvailable, notificationExecutor);
    }
}
Also used : Futures.immediateVoidFuture(com.google.common.util.concurrent.Futures.immediateVoidFuture) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Executor(java.util.concurrent.Executor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadSafe(javax.annotation.concurrent.ThreadSafe) SettableFuture(com.google.common.util.concurrent.SettableFuture) GuardedBy(javax.annotation.concurrent.GuardedBy) Supplier(java.util.function.Supplier) AtomicLong(java.util.concurrent.atomic.AtomicLong) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Objects.requireNonNull(java.util.Objects.requireNonNull) Suppliers(com.google.common.base.Suppliers) VisibleForTesting(com.google.common.annotations.VisibleForTesting) LocalMemoryContext(io.trino.memory.context.LocalMemoryContext) Nullable(javax.annotation.Nullable) LocalMemoryContext(io.trino.memory.context.LocalMemoryContext)

Example 4 with LocalMemoryContext

use of io.trino.memory.context.LocalMemoryContext in project trino by trinodb.

the class MergeSortedPages method pageWithPositions.

private static WorkProcessor<PageWithPosition> pageWithPositions(WorkProcessor<Page> pages, AggregatedMemoryContext aggregatedMemoryContext) {
    return pages.flatMap(page -> {
        LocalMemoryContext memoryContext = aggregatedMemoryContext.newLocalMemoryContext(MergeSortedPages.class.getSimpleName());
        memoryContext.setBytes(page.getRetainedSizeInBytes());
        return WorkProcessor.create(new WorkProcessor.Process<PageWithPosition>() {

            int position;

            @Override
            public ProcessState<PageWithPosition> process() {
                if (position >= page.getPositionCount()) {
                    memoryContext.close();
                    return ProcessState.finished();
                }
                return ProcessState.ofResult(new PageWithPosition(page, position++));
            }
        });
    });
}
Also used : ProcessState(io.trino.operator.WorkProcessor.ProcessState) LocalMemoryContext(io.trino.memory.context.LocalMemoryContext) WorkProcessor(io.trino.operator.WorkProcessor)

Example 5 with LocalMemoryContext

use of io.trino.memory.context.LocalMemoryContext in project trino by trinodb.

the class MergeSortedPages method buildPage.

private static WorkProcessor<Page> buildPage(WorkProcessor<PageWithPosition> pageWithPositions, List<Integer> outputChannels, List<Type> outputTypes, BiPredicate<PageBuilder, PageWithPosition> pageBreakPredicate, boolean updateMemoryAfterEveryPosition, AggregatedMemoryContext aggregatedMemoryContext, DriverYieldSignal yieldSignal) {
    LocalMemoryContext memoryContext = aggregatedMemoryContext.newLocalMemoryContext(MergeSortedPages.class.getSimpleName());
    PageBuilder pageBuilder = new PageBuilder(outputTypes);
    return pageWithPositions.yielding(yieldSignal::isSet).transform(pageWithPosition -> {
        boolean finished = pageWithPosition == null;
        if (finished && pageBuilder.isEmpty()) {
            memoryContext.close();
            return TransformationState.finished();
        }
        if (finished || pageBreakPredicate.test(pageBuilder, pageWithPosition)) {
            if (!updateMemoryAfterEveryPosition) {
                // update memory usage just before producing page to cap from top
                memoryContext.setBytes(pageBuilder.getRetainedSizeInBytes());
            }
            Page page = pageBuilder.build();
            pageBuilder.reset();
            if (!finished) {
                pageWithPosition.appendTo(pageBuilder, outputChannels, outputTypes);
            }
            if (updateMemoryAfterEveryPosition) {
                memoryContext.setBytes(pageBuilder.getRetainedSizeInBytes());
            }
            return TransformationState.ofResult(page, !finished);
        }
        pageWithPosition.appendTo(pageBuilder, outputChannels, outputTypes);
        if (updateMemoryAfterEveryPosition) {
            memoryContext.setBytes(pageBuilder.getRetainedSizeInBytes());
        }
        return TransformationState.needsMoreData();
    });
}
Also used : LocalMemoryContext(io.trino.memory.context.LocalMemoryContext) Page(io.trino.spi.Page) PageBuilder(io.trino.spi.PageBuilder)

Aggregations

LocalMemoryContext (io.trino.memory.context.LocalMemoryContext)24 Test (org.testng.annotations.Test)17 Page (io.trino.spi.Page)7 DriverYieldSignal (io.trino.operator.DriverYieldSignal)5 Optional (java.util.Optional)5 QueryId (io.trino.spi.QueryId)3 LazyBlock (io.trino.spi.block.LazyBlock)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Suppliers (com.google.common.base.Suppliers)1 Futures.immediateVoidFuture (com.google.common.util.concurrent.Futures.immediateVoidFuture)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 Slice (io.airlift.slice.Slice)1 TestingGcMonitor (io.airlift.stats.TestingGcMonitor)1 ExceededMemoryLimitException (io.trino.ExceededMemoryLimitException)1 SqlTask.createSqlTask (io.trino.execution.SqlTask.createSqlTask)1 QueryContext (io.trino.memory.QueryContext)1 MemoryTrackingContext (io.trino.memory.context.MemoryTrackingContext)1