Search in sources :

Example 1 with QueryCreatedEvent

use of io.trino.spi.eventlistener.QueryCreatedEvent in project trino by trinodb.

the class TestEventListenerWithSplits method testSplitsForNormalQuery.

@Test
public void testSplitsForNormalQuery() throws Exception {
    // We expect the following events
    // QueryCreated: 1, QueryCompleted: 1, Splits: SPLITS_PER_NODE (leaf splits) + LocalExchange[SINGLE] split + Aggregation/Output split
    int expectedEvents = 1 + 1 + SPLITS_PER_NODE + 1 + 1;
    runQueryAndWaitForEvents("SELECT sum(linenumber) FROM lineitem", expectedEvents);
    QueryCreatedEvent queryCreatedEvent = getOnlyElement(generatedEvents.getQueryCreatedEvents());
    assertEquals(queryCreatedEvent.getContext().getServerVersion(), "testversion");
    assertEquals(queryCreatedEvent.getContext().getServerAddress(), "127.0.0.1");
    assertEquals(queryCreatedEvent.getContext().getEnvironment(), "testing");
    assertEquals(queryCreatedEvent.getContext().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
    assertEquals(queryCreatedEvent.getMetadata().getQuery(), "SELECT sum(linenumber) FROM lineitem");
    assertFalse(queryCreatedEvent.getMetadata().getPreparedQuery().isPresent());
    QueryCompletedEvent queryCompletedEvent = getOnlyElement(generatedEvents.getQueryCompletedEvents());
    assertTrue(queryCompletedEvent.getContext().getResourceGroupId().isPresent());
    assertEquals(queryCompletedEvent.getContext().getResourceGroupId().get(), createResourceGroupId("global", "user-user"));
    assertEquals(queryCompletedEvent.getIoMetadata().getOutput(), Optional.empty());
    assertEquals(queryCompletedEvent.getIoMetadata().getInputs().size(), 1);
    assertEquals(queryCompletedEvent.getContext().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
    assertEquals(getOnlyElement(queryCompletedEvent.getIoMetadata().getInputs()).getCatalogName(), "tpch");
    assertEquals(queryCreatedEvent.getMetadata().getQueryId(), queryCompletedEvent.getMetadata().getQueryId());
    assertFalse(queryCompletedEvent.getMetadata().getPreparedQuery().isPresent());
    assertEquals(queryCompletedEvent.getStatistics().getCompletedSplits(), SPLITS_PER_NODE + 2);
    List<SplitCompletedEvent> splitCompletedEvents = generatedEvents.getSplitCompletedEvents();
    // leaf splits + aggregation split
    assertEquals(splitCompletedEvents.size(), SPLITS_PER_NODE + 2);
    // All splits must have the same query ID
    Set<String> actual = splitCompletedEvents.stream().map(SplitCompletedEvent::getQueryId).collect(toSet());
    assertEquals(actual, ImmutableSet.of(queryCompletedEvent.getMetadata().getQueryId()));
    // Sum of row count processed by all leaf stages is equal to the number of rows in the table
    long actualCompletedPositions = splitCompletedEvents.stream().filter(// filter out the root stage
    e -> !e.getStageId().endsWith(".0")).mapToLong(e -> e.getStatistics().getCompletedPositions()).sum();
    MaterializedResult result = runQueryAndWaitForEvents("SELECT count(*) FROM lineitem", expectedEvents);
    long expectedCompletedPositions = (long) result.getMaterializedRows().get(0).getField(0);
    assertEquals(actualCompletedPositions, expectedCompletedPositions);
    QueryStatistics statistics = queryCompletedEvent.getStatistics();
    // Aggregation can have memory pool usage
    assertTrue(statistics.getPeakUserMemoryBytes() >= 0);
    assertTrue(statistics.getPeakTaskUserMemory() >= 0);
    assertTrue(statistics.getPeakTaskTotalMemory() >= 0);
    assertTrue(statistics.getCumulativeMemory() >= 0);
    // Not a write query
    assertEquals(statistics.getWrittenBytes(), 0);
    assertEquals(statistics.getWrittenRows(), 0);
    assertEquals(statistics.getStageGcStatistics().size(), 2);
    // Deterministic statistics
    assertEquals(statistics.getPhysicalInputBytes(), 0);
    assertEquals(statistics.getPhysicalInputRows(), expectedCompletedPositions);
    assertEquals(statistics.getInternalNetworkBytes(), 381);
    assertEquals(statistics.getInternalNetworkRows(), 3);
    assertEquals(statistics.getTotalBytes(), 0);
    assertEquals(statistics.getOutputBytes(), 9);
    assertEquals(statistics.getOutputRows(), 1);
    assertTrue(statistics.isComplete());
    // Check only the presence because they are non-deterministic.
    assertTrue(statistics.getScheduledTime().isPresent());
    assertTrue(statistics.getResourceWaitingTime().isPresent());
    assertTrue(statistics.getAnalysisTime().isPresent());
    assertTrue(statistics.getPlanningTime().isPresent());
    assertTrue(statistics.getExecutionTime().isPresent());
    assertTrue(statistics.getPlanNodeStatsAndCosts().isPresent());
    assertTrue(statistics.getCpuTime().getSeconds() >= 0);
    assertTrue(statistics.getWallTime().getSeconds() >= 0);
    assertTrue(statistics.getCpuTimeDistribution().size() > 0);
    assertTrue(statistics.getOperatorSummaries().size() > 0);
}
Also used : QueryCompletedEvent(io.trino.spi.eventlistener.QueryCompletedEvent) MaterializedResult(io.trino.testing.MaterializedResult) QueryCreatedEvent(io.trino.spi.eventlistener.QueryCreatedEvent) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) TPCH_SPLITS_PER_NODE(io.trino.plugin.tpch.TpchConnectorFactory.TPCH_SPLITS_PER_NODE) AbstractTestQueryFramework(io.trino.testing.AbstractTestQueryFramework) DistributedQueryRunner(io.trino.testing.DistributedQueryRunner) ImmutableList(com.google.common.collect.ImmutableList) MockConnectorFactory(io.trino.connector.MockConnectorFactory) Duration(java.time.Duration) TpchPlugin(io.trino.plugin.tpch.TpchPlugin) ConnectorFactory(io.trino.spi.connector.ConnectorFactory) Assert.assertFalse(org.testng.Assert.assertFalse) Collectors.toSet(java.util.stream.Collectors.toSet) QueryStatistics(io.trino.spi.eventlistener.QueryStatistics) SplitCompletedEvent(io.trino.spi.eventlistener.SplitCompletedEvent) ImmutableSet(com.google.common.collect.ImmutableSet) TestingEventListenerPlugin(io.trino.execution.TestEventListenerPlugin.TestingEventListenerPlugin) QueryType(io.trino.spi.resourcegroups.QueryType) ImmutableMap(com.google.common.collect.ImmutableMap) ResourceGroupManagerPlugin(io.trino.plugin.resourcegroups.ResourceGroupManagerPlugin) Language(org.intellij.lang.annotations.Language) Set(java.util.Set) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) SchemaTableName(io.trino.spi.connector.SchemaTableName) Plugin(io.trino.spi.Plugin) List(java.util.List) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) TestQueues.createResourceGroupId(io.trino.execution.TestQueues.createResourceGroupId) QueryRunner(io.trino.testing.QueryRunner) Optional(java.util.Optional) Assert.assertTrue(org.testng.Assert.assertTrue) Session(io.trino.Session) SplitCompletedEvent(io.trino.spi.eventlistener.SplitCompletedEvent) QueryCreatedEvent(io.trino.spi.eventlistener.QueryCreatedEvent) QueryCompletedEvent(io.trino.spi.eventlistener.QueryCompletedEvent) QueryStatistics(io.trino.spi.eventlistener.QueryStatistics) MaterializedResult(io.trino.testing.MaterializedResult) Test(org.testng.annotations.Test)

Example 2 with QueryCreatedEvent

use of io.trino.spi.eventlistener.QueryCreatedEvent in project trino by trinodb.

the class TestEventListenerWithSplits method testSplitsForConstantQuery.

@Test
public void testSplitsForConstantQuery() throws Exception {
    // QueryCreated: 1, QueryCompleted: 1, Splits: 1
    runQueryAndWaitForEvents("SELECT 1", 3);
    QueryCreatedEvent queryCreatedEvent = getOnlyElement(generatedEvents.getQueryCreatedEvents());
    assertEquals(queryCreatedEvent.getContext().getServerVersion(), "testversion");
    assertEquals(queryCreatedEvent.getContext().getServerAddress(), "127.0.0.1");
    assertEquals(queryCreatedEvent.getContext().getEnvironment(), "testing");
    assertEquals(queryCreatedEvent.getContext().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
    assertEquals(queryCreatedEvent.getContext().getQueryType().get(), QueryType.SELECT);
    assertEquals(queryCreatedEvent.getMetadata().getQuery(), "SELECT 1");
    assertFalse(queryCreatedEvent.getMetadata().getPreparedQuery().isPresent());
    QueryCompletedEvent queryCompletedEvent = getOnlyElement(generatedEvents.getQueryCompletedEvents());
    assertTrue(queryCompletedEvent.getContext().getResourceGroupId().isPresent());
    assertEquals(queryCompletedEvent.getContext().getResourceGroupId().get(), createResourceGroupId("global", "user-user"));
    assertEquals(queryCompletedEvent.getStatistics().getTotalRows(), 0L);
    assertEquals(queryCompletedEvent.getContext().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
    assertEquals(queryCreatedEvent.getMetadata().getQueryId(), queryCompletedEvent.getMetadata().getQueryId());
    assertFalse(queryCompletedEvent.getMetadata().getPreparedQuery().isPresent());
    assertEquals(queryCompletedEvent.getContext().getQueryType().get(), QueryType.SELECT);
    List<SplitCompletedEvent> splitCompletedEvents = generatedEvents.getSplitCompletedEvents();
    assertEquals(splitCompletedEvents.get(0).getQueryId(), queryCompletedEvent.getMetadata().getQueryId());
    assertEquals(splitCompletedEvents.get(0).getStatistics().getCompletedPositions(), 1);
}
Also used : SplitCompletedEvent(io.trino.spi.eventlistener.SplitCompletedEvent) QueryCreatedEvent(io.trino.spi.eventlistener.QueryCreatedEvent) QueryCompletedEvent(io.trino.spi.eventlistener.QueryCompletedEvent) Test(org.testng.annotations.Test)

Example 3 with QueryCreatedEvent

use of io.trino.spi.eventlistener.QueryCreatedEvent in project trino by trinodb.

the class TestEventListenerBasic method testOutputStats.

@Test
public void testOutputStats() throws Exception {
    MaterializedResult result = runQueryAndWaitForEvents("SELECT 1 FROM lineitem", 2);
    QueryCreatedEvent queryCreatedEvent = getOnlyElement(generatedEvents.getQueryCreatedEvents());
    QueryCompletedEvent queryCompletedEvent = getOnlyElement(generatedEvents.getQueryCompletedEvents());
    QueryStats queryStats = getDistributedQueryRunner().getCoordinator().getQueryManager().getFullQueryInfo(new QueryId(queryCreatedEvent.getMetadata().getQueryId())).getQueryStats();
    assertTrue(queryStats.getOutputDataSize().toBytes() > 0L);
    assertTrue(queryCompletedEvent.getStatistics().getOutputBytes() > 0L);
    assertEquals(result.getRowCount(), queryStats.getOutputPositions());
    assertEquals(result.getRowCount(), queryCompletedEvent.getStatistics().getOutputRows());
    runQueryAndWaitForEvents("SELECT COUNT(1) FROM lineitem", 2);
    queryCreatedEvent = getOnlyElement(generatedEvents.getQueryCreatedEvents());
    queryCompletedEvent = getOnlyElement(generatedEvents.getQueryCompletedEvents());
    queryStats = getDistributedQueryRunner().getCoordinator().getQueryManager().getFullQueryInfo(new QueryId(queryCreatedEvent.getMetadata().getQueryId())).getQueryStats();
    assertTrue(queryStats.getOutputDataSize().toBytes() > 0L);
    assertTrue(queryCompletedEvent.getStatistics().getOutputBytes() > 0L);
    assertEquals(1L, queryStats.getOutputPositions());
    assertEquals(1L, queryCompletedEvent.getStatistics().getOutputRows());
    // Ensure the proper conversion in QueryMonitor#createQueryStatistics
    QueryStatistics statistics = queryCompletedEvent.getStatistics();
    assertEquals(statistics.getCpuTime().toMillis(), queryStats.getTotalCpuTime().toMillis());
    assertEquals(statistics.getWallTime().toMillis(), queryStats.getElapsedTime().toMillis());
    assertEquals(statistics.getQueuedTime().toMillis(), queryStats.getQueuedTime().toMillis());
    assertEquals(statistics.getScheduledTime().get().toMillis(), queryStats.getTotalScheduledTime().toMillis());
    assertEquals(statistics.getResourceWaitingTime().get().toMillis(), queryStats.getResourceWaitingTime().toMillis());
    assertEquals(statistics.getAnalysisTime().get().toMillis(), queryStats.getAnalysisTime().toMillis());
    assertEquals(statistics.getPlanningTime().get().toMillis(), queryStats.getPlanningTime().toMillis());
    assertEquals(statistics.getExecutionTime().get().toMillis(), queryStats.getExecutionTime().toMillis());
    assertEquals(statistics.getPeakUserMemoryBytes(), queryStats.getPeakUserMemoryReservation().toBytes());
    assertEquals(statistics.getPeakTaskUserMemory(), queryStats.getPeakTaskUserMemory().toBytes());
    assertEquals(statistics.getPeakTaskTotalMemory(), queryStats.getPeakTaskTotalMemory().toBytes());
    assertEquals(statistics.getPhysicalInputBytes(), queryStats.getPhysicalInputDataSize().toBytes());
    assertEquals(statistics.getPhysicalInputRows(), queryStats.getPhysicalInputPositions());
    assertEquals(statistics.getInternalNetworkBytes(), queryStats.getInternalNetworkInputDataSize().toBytes());
    assertEquals(statistics.getInternalNetworkRows(), queryStats.getInternalNetworkInputPositions());
    assertEquals(statistics.getTotalBytes(), queryStats.getRawInputDataSize().toBytes());
    assertEquals(statistics.getTotalRows(), queryStats.getRawInputPositions());
    assertEquals(statistics.getOutputBytes(), queryStats.getOutputDataSize().toBytes());
    assertEquals(statistics.getOutputRows(), queryStats.getOutputPositions());
    assertEquals(statistics.getWrittenBytes(), queryStats.getLogicalWrittenDataSize().toBytes());
    assertEquals(statistics.getWrittenRows(), queryStats.getWrittenPositions());
    assertEquals(statistics.getCumulativeMemory(), queryStats.getCumulativeUserMemory());
    assertEquals(statistics.getStageGcStatistics(), queryStats.getStageGcStatistics());
    assertEquals(statistics.getCompletedSplits(), queryStats.getCompletedDrivers());
}
Also used : QueryCreatedEvent(io.trino.spi.eventlistener.QueryCreatedEvent) QueryCompletedEvent(io.trino.spi.eventlistener.QueryCompletedEvent) QueryStatistics(io.trino.spi.eventlistener.QueryStatistics) QueryId(io.trino.spi.QueryId) MaterializedResult(io.trino.testing.MaterializedResult) Test(org.testng.annotations.Test)

Example 4 with QueryCreatedEvent

use of io.trino.spi.eventlistener.QueryCreatedEvent in project trino by trinodb.

the class TestEventListenerBasic method testPrepareAndExecute.

@Test
public void testPrepareAndExecute() throws Exception {
    String selectQuery = "SELECT count(*) FROM lineitem WHERE shipmode = ?";
    String prepareQuery = "PREPARE stmt FROM " + selectQuery;
    // QueryCreated: 1, QueryCompleted: 1, Splits: 0
    runQueryAndWaitForEvents(prepareQuery, 2);
    QueryCreatedEvent queryCreatedEvent = getOnlyElement(generatedEvents.getQueryCreatedEvents());
    assertEquals(queryCreatedEvent.getContext().getServerVersion(), "testversion");
    assertEquals(queryCreatedEvent.getContext().getServerAddress(), "127.0.0.1");
    assertEquals(queryCreatedEvent.getContext().getEnvironment(), "testing");
    assertEquals(queryCreatedEvent.getContext().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
    assertEquals(queryCreatedEvent.getMetadata().getQuery(), prepareQuery);
    assertFalse(queryCreatedEvent.getMetadata().getPreparedQuery().isPresent());
    QueryCompletedEvent queryCompletedEvent = getOnlyElement(generatedEvents.getQueryCompletedEvents());
    assertTrue(queryCompletedEvent.getContext().getResourceGroupId().isPresent());
    assertEquals(queryCompletedEvent.getContext().getResourceGroupId().get(), createResourceGroupId("global", "user-user"));
    assertEquals(queryCompletedEvent.getIoMetadata().getOutput(), Optional.empty());
    // Prepare has no inputs
    assertEquals(queryCompletedEvent.getIoMetadata().getInputs().size(), 0);
    assertEquals(queryCompletedEvent.getContext().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
    assertEquals(queryCreatedEvent.getMetadata().getQueryId(), queryCompletedEvent.getMetadata().getQueryId());
    assertFalse(queryCompletedEvent.getMetadata().getPreparedQuery().isPresent());
    // Prepare has no splits
    assertEquals(queryCompletedEvent.getStatistics().getCompletedSplits(), 0);
    // Add prepared statement to a new session to eliminate any impact on other tests in this suite.
    Session sessionWithPrepare = Session.builder(getSession()).addPreparedStatement("stmt", selectQuery).build();
    queries.runQueryAndWaitForEvents("EXECUTE stmt USING 'SHIP'", 2, sessionWithPrepare);
    queryCreatedEvent = getOnlyElement(generatedEvents.getQueryCreatedEvents());
    assertEquals(queryCreatedEvent.getContext().getServerVersion(), "testversion");
    assertEquals(queryCreatedEvent.getContext().getServerAddress(), "127.0.0.1");
    assertEquals(queryCreatedEvent.getContext().getEnvironment(), "testing");
    assertEquals(queryCreatedEvent.getContext().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
    assertEquals(queryCreatedEvent.getMetadata().getQuery(), "EXECUTE stmt USING 'SHIP'");
    assertTrue(queryCreatedEvent.getMetadata().getPreparedQuery().isPresent());
    assertEquals(queryCreatedEvent.getMetadata().getPreparedQuery().get(), selectQuery);
    queryCompletedEvent = getOnlyElement(generatedEvents.getQueryCompletedEvents());
    assertTrue(queryCompletedEvent.getContext().getResourceGroupId().isPresent());
    assertEquals(queryCompletedEvent.getContext().getResourceGroupId().get(), createResourceGroupId("global", "user-user"));
    assertEquals(queryCompletedEvent.getIoMetadata().getOutput(), Optional.empty());
    assertEquals(queryCompletedEvent.getIoMetadata().getInputs().size(), 1);
    assertEquals(queryCompletedEvent.getContext().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
    assertEquals(getOnlyElement(queryCompletedEvent.getIoMetadata().getInputs()).getCatalogName(), "tpch");
    assertEquals(queryCreatedEvent.getMetadata().getQueryId(), queryCompletedEvent.getMetadata().getQueryId());
    assertTrue(queryCompletedEvent.getMetadata().getPreparedQuery().isPresent());
    assertEquals(queryCompletedEvent.getMetadata().getPreparedQuery().get(), selectQuery);
}
Also used : QueryCreatedEvent(io.trino.spi.eventlistener.QueryCreatedEvent) QueryCompletedEvent(io.trino.spi.eventlistener.QueryCompletedEvent) Session(io.trino.Session) Test(org.testng.annotations.Test)

Aggregations

QueryCompletedEvent (io.trino.spi.eventlistener.QueryCompletedEvent)4 QueryCreatedEvent (io.trino.spi.eventlistener.QueryCreatedEvent)4 Test (org.testng.annotations.Test)4 Session (io.trino.Session)2 QueryStatistics (io.trino.spi.eventlistener.QueryStatistics)2 SplitCompletedEvent (io.trino.spi.eventlistener.SplitCompletedEvent)2 MaterializedResult (io.trino.testing.MaterializedResult)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)1 MockConnectorFactory (io.trino.connector.MockConnectorFactory)1 TestingEventListenerPlugin (io.trino.execution.TestEventListenerPlugin.TestingEventListenerPlugin)1 TestQueues.createResourceGroupId (io.trino.execution.TestQueues.createResourceGroupId)1 ResourceGroupManagerPlugin (io.trino.plugin.resourcegroups.ResourceGroupManagerPlugin)1 TPCH_SPLITS_PER_NODE (io.trino.plugin.tpch.TpchConnectorFactory.TPCH_SPLITS_PER_NODE)1 TpchPlugin (io.trino.plugin.tpch.TpchPlugin)1 Plugin (io.trino.spi.Plugin)1 QueryId (io.trino.spi.QueryId)1 ConnectorFactory (io.trino.spi.connector.ConnectorFactory)1