Search in sources :

Example 6 with InMemoryRecordSet

use of io.trino.spi.connector.InMemoryRecordSet in project trino by trinodb.

the class TestThriftIndexPageSource method testGetNextPageTwoConcurrentRequests.

@Test
public void testGetNextPageTwoConcurrentRequests() throws Exception {
    final int splits = 3;
    final int lookupRequestsConcurrency = 2;
    final int rowsPerSplit = 1;
    List<SettableFuture<TrinoThriftPageResult>> futures = IntStream.range(0, splits).mapToObj(i -> SettableFuture.<TrinoThriftPageResult>create()).collect(toImmutableList());
    List<CountDownLatch> signals = IntStream.range(0, splits).mapToObj(i -> new CountDownLatch(1)).collect(toImmutableList());
    TestingThriftService client = new TestingThriftService(rowsPerSplit, false, false) {

        @Override
        public ListenableFuture<TrinoThriftPageResult> getRows(TrinoThriftId splitId, List<String> columns, long maxBytes, TrinoThriftNullableToken nextToken) {
            int key = Ints.fromByteArray(splitId.getId());
            signals.get(key).countDown();
            return futures.get(key);
        }
    };
    ThriftConnectorStats stats = new ThriftConnectorStats();
    long pageSizeReceived = 0;
    ThriftIndexPageSource pageSource = new ThriftIndexPageSource((context, headers) -> client, ImmutableMap.of(), stats, new ThriftIndexHandle(new SchemaTableName("default", "table1"), TupleDomain.all()), ImmutableList.of(column("a", INTEGER)), ImmutableList.of(column("b", INTEGER)), new InMemoryRecordSet(ImmutableList.of(INTEGER), generateKeys(0, splits)), MAX_BYTES_PER_RESPONSE, lookupRequestsConcurrency);
    assertNull(pageSource.getNextPage());
    assertEquals((long) stats.getIndexPageSize().getAllTime().getTotal(), 0);
    signals.get(0).await(1, SECONDS);
    signals.get(1).await(1, SECONDS);
    signals.get(2).await(1, SECONDS);
    assertEquals(signals.get(0).getCount(), 0, "first request wasn't sent");
    assertEquals(signals.get(1).getCount(), 0, "second request wasn't sent");
    assertEquals(signals.get(2).getCount(), 1, "third request shouldn't be sent");
    // at this point first two requests were sent
    assertFalse(pageSource.isFinished());
    assertNull(pageSource.getNextPage());
    assertEquals((long) stats.getIndexPageSize().getAllTime().getTotal(), 0);
    // completing the second request
    futures.get(1).set(pageResult(20, null));
    Page page = pageSource.getNextPage();
    pageSizeReceived += page.getSizeInBytes();
    assertEquals((long) stats.getIndexPageSize().getAllTime().getTotal(), pageSizeReceived);
    assertNotNull(page);
    assertEquals(page.getPositionCount(), 1);
    assertEquals(page.getBlock(0).getInt(0, 0), 20);
    // not complete yet
    assertFalse(pageSource.isFinished());
    // once one of the requests completes the next one should be sent
    signals.get(2).await(1, SECONDS);
    assertEquals(signals.get(2).getCount(), 0, "third request wasn't sent");
    // completing the first request
    futures.get(0).set(pageResult(10, null));
    page = pageSource.getNextPage();
    assertNotNull(page);
    pageSizeReceived += page.getSizeInBytes();
    assertEquals((long) stats.getIndexPageSize().getAllTime().getTotal(), pageSizeReceived);
    assertEquals(page.getPositionCount(), 1);
    assertEquals(page.getBlock(0).getInt(0, 0), 10);
    // still not complete
    assertFalse(pageSource.isFinished());
    // completing the third request
    futures.get(2).set(pageResult(30, null));
    page = pageSource.getNextPage();
    assertNotNull(page);
    pageSizeReceived += page.getSizeInBytes();
    assertEquals((long) stats.getIndexPageSize().getAllTime().getTotal(), pageSizeReceived);
    assertEquals(page.getPositionCount(), 1);
    assertEquals(page.getBlock(0).getInt(0, 0), 30);
    // finished now
    assertTrue(pageSource.isFinished());
    // after completion
    assertNull(pageSource.getNextPage());
    pageSource.close();
}
Also used : SettableFuture(com.google.common.util.concurrent.SettableFuture) IntStream(java.util.stream.IntStream) Collections.shuffle(java.util.Collections.shuffle) TrinoThriftBlock.integerData(io.trino.plugin.thrift.api.TrinoThriftBlock.integerData) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Assert.assertNull(org.testng.Assert.assertNull) TrinoThriftTupleDomain(io.trino.plugin.thrift.api.TrinoThriftTupleDomain) Type(io.trino.spi.type.Type) Page(io.trino.spi.Page) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) CompletableFuture(java.util.concurrent.CompletableFuture) SettableFuture(com.google.common.util.concurrent.SettableFuture) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) TrinoThriftId(io.trino.plugin.thrift.api.TrinoThriftId) Block(io.trino.spi.block.Block) TrinoThriftService(io.trino.plugin.thrift.api.TrinoThriftService) TrinoThriftNullableToken(io.trino.plugin.thrift.api.TrinoThriftNullableToken) TrinoThriftSplit(io.trino.plugin.thrift.api.TrinoThriftSplit) INTEGER(io.trino.spi.type.IntegerType.INTEGER) Assert.assertFalse(org.testng.Assert.assertFalse) Futures.immediateFuture(com.google.common.util.concurrent.Futures.immediateFuture) TrinoThriftPageResult(io.trino.plugin.thrift.api.TrinoThriftPageResult) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TrinoThriftInteger(io.trino.plugin.thrift.api.datatypes.TrinoThriftInteger) TrinoThriftSplitBatch(io.trino.plugin.thrift.api.TrinoThriftSplitBatch) TrinoThriftNullableTableMetadata(io.trino.plugin.thrift.api.TrinoThriftNullableTableMetadata) Assert.assertNotNull(org.testng.Assert.assertNotNull) TupleDomain(io.trino.spi.predicate.TupleDomain) Ints(com.google.common.primitives.Ints) InMemoryRecordSet(io.trino.spi.connector.InMemoryRecordSet) SchemaTableName(io.trino.spi.connector.SchemaTableName) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) TrinoThriftSchemaTableName(io.trino.plugin.thrift.api.TrinoThriftSchemaTableName) TrinoThriftNullableColumnSet(io.trino.plugin.thrift.api.TrinoThriftNullableColumnSet) Assert.assertTrue(org.testng.Assert.assertTrue) TrinoThriftNullableSchemaName(io.trino.plugin.thrift.api.TrinoThriftNullableSchemaName) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Page(io.trino.spi.Page) CountDownLatch(java.util.concurrent.CountDownLatch) TrinoThriftId(io.trino.plugin.thrift.api.TrinoThriftId) SchemaTableName(io.trino.spi.connector.SchemaTableName) TrinoThriftSchemaTableName(io.trino.plugin.thrift.api.TrinoThriftSchemaTableName) InMemoryRecordSet(io.trino.spi.connector.InMemoryRecordSet) TrinoThriftPageResult(io.trino.plugin.thrift.api.TrinoThriftPageResult) TrinoThriftNullableToken(io.trino.plugin.thrift.api.TrinoThriftNullableToken) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) Test(org.testng.annotations.Test)

Example 7 with InMemoryRecordSet

use of io.trino.spi.connector.InMemoryRecordSet in project trino by trinodb.

the class TestThriftIndexPageSource method runGeneralTest.

private static void runGeneralTest(int splits, int lookupRequestsConcurrency, int rowsPerSplit, boolean twoSplitBatches) throws Exception {
    TestingThriftService client = new TestingThriftService(rowsPerSplit, true, twoSplitBatches);
    ThriftIndexPageSource pageSource = new ThriftIndexPageSource((context, headers) -> client, ImmutableMap.of(), new ThriftConnectorStats(), new ThriftIndexHandle(new SchemaTableName("default", "table1"), TupleDomain.all()), ImmutableList.of(column("a", INTEGER)), ImmutableList.of(column("b", INTEGER)), new InMemoryRecordSet(ImmutableList.of(INTEGER), generateKeys(1, splits + 1)), MAX_BYTES_PER_RESPONSE, lookupRequestsConcurrency);
    List<Integer> actual = new ArrayList<>();
    while (!pageSource.isFinished()) {
        CompletableFuture<?> blocked = pageSource.isBlocked();
        blocked.get(1, SECONDS);
        Page page = pageSource.getNextPage();
        if (page != null) {
            Block block = page.getBlock(0);
            for (int position = 0; position < block.getPositionCount(); position++) {
                actual.add(block.getInt(position, 0));
            }
        }
    }
    Collections.sort(actual);
    List<Integer> expected = new ArrayList<>(splits * rowsPerSplit);
    for (int split = 1; split <= splits; split++) {
        for (int row = 0; row < rowsPerSplit; row++) {
            expected.add(split * 10 + row);
        }
    }
    assertEquals(actual, expected);
    // must be null after finish
    assertNull(pageSource.getNextPage());
    pageSource.close();
}
Also used : ArrayList(java.util.ArrayList) Page(io.trino.spi.Page) SchemaTableName(io.trino.spi.connector.SchemaTableName) TrinoThriftSchemaTableName(io.trino.plugin.thrift.api.TrinoThriftSchemaTableName) InMemoryRecordSet(io.trino.spi.connector.InMemoryRecordSet) TrinoThriftInteger(io.trino.plugin.thrift.api.datatypes.TrinoThriftInteger) Block(io.trino.spi.block.Block)

Aggregations

InMemoryRecordSet (io.trino.spi.connector.InMemoryRecordSet)7 SchemaTableName (io.trino.spi.connector.SchemaTableName)6 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 List (java.util.List)5 ImmutableList (com.google.common.collect.ImmutableList)4 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)4 ConnectorSession (io.trino.spi.connector.ConnectorSession)4 ConnectorTableMetadata (io.trino.spi.connector.ConnectorTableMetadata)4 SystemTable (io.trino.spi.connector.SystemTable)4 Type (io.trino.spi.type.Type)4 Map (java.util.Map)4 Optional (java.util.Optional)4 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)3 Block (io.trino.spi.block.Block)3 ArrayList (java.util.ArrayList)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 TrinoThriftSchemaTableName (io.trino.plugin.thrift.api.TrinoThriftSchemaTableName)2 TrinoThriftInteger (io.trino.plugin.thrift.api.datatypes.TrinoThriftInteger)2 Page (io.trino.spi.Page)2 RecordCursor (io.trino.spi.connector.RecordCursor)2