Search in sources :

Example 26 with LocalMemoryContext

use of com.facebook.presto.memory.context.LocalMemoryContext in project presto by prestodb.

the class TestMemoryTracking method testTrySetZeroBytesFullPool.

@Test
public void testTrySetZeroBytesFullPool() {
    LocalMemoryContext localMemoryContext = operatorContext.localUserMemoryContext();
    // fill up the pool
    memoryPool.reserve(new QueryId("test_query"), "test", memoryPool.getFreeBytes());
    // try to reserve 0 bytes in the full pool
    assertTrue(localMemoryContext.trySetBytes(localMemoryContext.getBytes()));
}
Also used : LocalMemoryContext(com.facebook.presto.memory.context.LocalMemoryContext) QueryId(com.facebook.presto.spi.QueryId) Test(org.testng.annotations.Test)

Example 27 with LocalMemoryContext

use of com.facebook.presto.memory.context.LocalMemoryContext in project presto by prestodb.

the class TestMemoryTracking method testCumulativeUserMemoryEstimation.

@Test
public void testCumulativeUserMemoryEstimation() {
    LocalMemoryContext userMemory = operatorContext.localUserMemoryContext();
    long userMemoryBytes = 100_000_000;
    userMemory.setBytes(userMemoryBytes);
    long startTime = System.nanoTime();
    double cumulativeUserMemory = taskContext.getTaskStats().getCumulativeUserMemory();
    long endTime = System.nanoTime();
    double elapsedTimeInMillis = (endTime - startTime) / 1_000_000.0;
    long averageMemoryForLastPeriod = userMemoryBytes / 2;
    assertTrue(cumulativeUserMemory < elapsedTimeInMillis * averageMemoryForLastPeriod);
}
Also used : LocalMemoryContext(com.facebook.presto.memory.context.LocalMemoryContext) Test(org.testng.annotations.Test)

Example 28 with LocalMemoryContext

use of com.facebook.presto.memory.context.LocalMemoryContext in project presto by prestodb.

the class TestMemoryTracking method testLocalRevocableMemoryLimitExceeded.

@Test
public void testLocalRevocableMemoryLimitExceeded() {
    LocalMemoryContext revocableMemoryContext = operatorContext.localRevocableMemoryContext();
    revocableMemoryContext.setBytes(100);
    assertOperatorMemoryAllocations(operatorContext.getOperatorMemoryContext(), 0, 0, 100);
    revocableMemoryContext.setBytes(queryMaxRevocableMemory.toBytes());
    assertOperatorMemoryAllocations(operatorContext.getOperatorMemoryContext(), 0, 0, queryMaxRevocableMemory.toBytes());
    try {
        revocableMemoryContext.setBytes(queryMaxRevocableMemory.toBytes() + 1);
        fail("allocation should hit the per-node revocable memory limit");
    } catch (ExceededMemoryLimitException e) {
        assertEquals(e.getMessage(), format("Query exceeded per-node revocable memory limit of %1$s [Allocated: %1$s, Delta: 1B]", queryMaxRevocableMemory));
    }
}
Also used : LocalMemoryContext(com.facebook.presto.memory.context.LocalMemoryContext) ExceededMemoryLimitException(com.facebook.presto.ExceededMemoryLimitException) Test(org.testng.annotations.Test)

Example 29 with LocalMemoryContext

use of com.facebook.presto.memory.context.LocalMemoryContext in project presto by prestodb.

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(com.facebook.presto.operator.WorkProcessor.ProcessState) LocalMemoryContext(com.facebook.presto.memory.context.LocalMemoryContext) WorkProcessor(com.facebook.presto.operator.WorkProcessor)

Example 30 with LocalMemoryContext

use of com.facebook.presto.memory.context.LocalMemoryContext in project presto by prestodb.

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(pageWithPositionOptional -> {
        boolean finished = !pageWithPositionOptional.isPresent();
        if (finished && pageBuilder.isEmpty()) {
            memoryContext.close();
            return TransformationState.finished();
        }
        if (finished || pageBreakPredicate.test(pageBuilder, pageWithPositionOptional.get())) {
            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) {
                pageWithPositionOptional.get().appendTo(pageBuilder, outputChannels, outputTypes);
            }
            if (updateMemoryAfterEveryPosition) {
                memoryContext.setBytes(pageBuilder.getRetainedSizeInBytes());
            }
            return TransformationState.ofResult(page, !finished);
        }
        pageWithPositionOptional.get().appendTo(pageBuilder, outputChannels, outputTypes);
        if (updateMemoryAfterEveryPosition) {
            memoryContext.setBytes(pageBuilder.getRetainedSizeInBytes());
        }
        return TransformationState.needsMoreData();
    });
}
Also used : LocalMemoryContext(com.facebook.presto.memory.context.LocalMemoryContext) Page(com.facebook.presto.common.Page) PageBuilder(com.facebook.presto.common.PageBuilder)

Aggregations

LocalMemoryContext (com.facebook.presto.memory.context.LocalMemoryContext)31 Test (org.testng.annotations.Test)23 Page (com.facebook.presto.common.Page)8 QueryId (com.facebook.presto.spi.QueryId)6 Optional (java.util.Optional)6 DriverYieldSignal (com.facebook.presto.operator.DriverYieldSignal)5 Block (com.facebook.presto.common.block.Block)3 Type (com.facebook.presto.common.type.Type)3 DriverContext (com.facebook.presto.operator.DriverContext)3 OperatorContext (com.facebook.presto.operator.OperatorContext)3 Slice (io.airlift.slice.Slice)3 List (java.util.List)3 Supplier (java.util.function.Supplier)3 ClassLayout (org.openjdk.jol.info.ClassLayout)3 Threads.daemonThreadsNamed (com.facebook.airlift.concurrent.Threads.daemonThreadsNamed)2 TestingTicker (com.facebook.airlift.testing.TestingTicker)2 ExceededMemoryLimitException (com.facebook.presto.ExceededMemoryLimitException)2 BlockAssertions (com.facebook.presto.block.BlockAssertions)2 BlockAssertions.createLongSequenceBlock (com.facebook.presto.block.BlockAssertions.createLongSequenceBlock)2 BlockAssertions.createMapType (com.facebook.presto.block.BlockAssertions.createMapType)2