Search in sources :

Example 31 with LocalMemoryContext

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

the class TestFileSingleStreamSpiller method assertSpill.

private void assertSpill(boolean compression, boolean encryption) throws Exception {
    File spillPath = new File(tempDirectory, UUID.randomUUID().toString());
    FileSingleStreamSpillerFactory spillerFactory = new FileSingleStreamSpillerFactory(// executor won't be closed, because we don't call destroy() on the spiller factory
    executor, new BlockEncodingManager(), new SpillerStats(), ImmutableList.of(spillPath.toPath()), 1.0, compression, encryption);
    LocalMemoryContext memoryContext = newSimpleAggregatedMemoryContext().newLocalMemoryContext("test");
    SingleStreamSpiller singleStreamSpiller = spillerFactory.create(TYPES, new TestingSpillContext(), memoryContext);
    assertTrue(singleStreamSpiller instanceof FileSingleStreamSpiller);
    FileSingleStreamSpiller spiller = (FileSingleStreamSpiller) singleStreamSpiller;
    Page page = buildPage();
    // The spillers will reserve memory in their constructors
    assertEquals(memoryContext.getBytes(), 4096);
    spiller.spill(page).get();
    spiller.spill(Iterators.forArray(page, page, page)).get();
    assertEquals(listFiles(spillPath.toPath()).size(), 1);
    // Assert the spill codec flags match the expected configuration
    try (InputStream is = newInputStream(listFiles(spillPath.toPath()).get(0))) {
        Iterator<SerializedPage> serializedPages = PagesSerdeUtil.readSerializedPages(new InputStreamSliceInput(is));
        assertTrue(serializedPages.hasNext(), "at least one page should be successfully read back");
        byte markers = serializedPages.next().getPageCodecMarkers();
        assertEquals(PageCodecMarker.COMPRESSED.isSet(markers), compression);
        assertEquals(PageCodecMarker.ENCRYPTED.isSet(markers), encryption);
    }
    // The spillers release their memory reservations when they are closed, therefore at this point
    // they will have non-zero memory reservation.
    // assertEquals(memoryContext.getBytes(), 0);
    Iterator<Page> spilledPagesIterator = spiller.getSpilledPages();
    assertEquals(memoryContext.getBytes(), FileSingleStreamSpiller.BUFFER_SIZE);
    ImmutableList<Page> spilledPages = ImmutableList.copyOf(spilledPagesIterator);
    // The spillers release their memory reservations when they are closed, therefore at this point
    // they will have non-zero memory reservation.
    // assertEquals(memoryContext.getBytes(), 0);
    assertEquals(4, spilledPages.size());
    for (int i = 0; i < 4; ++i) {
        PageAssertions.assertPageEquals(TYPES, page, spilledPages.get(i));
    }
    spiller.close();
    assertEquals(listFiles(spillPath.toPath()).size(), 0);
    assertEquals(memoryContext.getBytes(), 0);
}
Also used : LocalMemoryContext(com.facebook.presto.memory.context.LocalMemoryContext) Files.newInputStream(java.nio.file.Files.newInputStream) InputStream(java.io.InputStream) Page(com.facebook.presto.common.Page) SerializedPage(com.facebook.presto.spi.page.SerializedPage) BlockEncodingManager(com.facebook.presto.common.block.BlockEncodingManager) SerializedPage(com.facebook.presto.spi.page.SerializedPage) InputStreamSliceInput(io.airlift.slice.InputStreamSliceInput) File(java.io.File)

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