Search in sources :

Example 1 with TransactionId

use of io.trino.transaction.TransactionId in project trino by trinodb.

the class QueryStateMachine method beginWithTicker.

static QueryStateMachine beginWithTicker(Optional<TransactionId> existingTransactionId, String query, Optional<String> preparedQuery, Session session, URI self, ResourceGroupId resourceGroup, boolean transactionControl, TransactionManager transactionManager, AccessControl accessControl, Executor executor, Ticker ticker, Metadata metadata, WarningCollector warningCollector, Optional<QueryType> queryType) {
    // if there is an existing transaction, activate it
    existingTransactionId.ifPresent(transactionId -> {
        if (transactionControl) {
            transactionManager.trySetActive(transactionId);
        } else {
            transactionManager.checkAndSetActive(transactionId);
        }
    });
    // NOTE: for the start transaction command, no transaction is created
    if (existingTransactionId.isPresent() || !transactionControl) {
        // TODO: make autocommit isolation level a session parameter
        TransactionId transactionId = existingTransactionId.orElseGet(() -> transactionManager.beginTransaction(true));
        session = session.beginTransactionId(transactionId, transactionManager, accessControl);
    }
    QueryStateMachine queryStateMachine = new QueryStateMachine(query, preparedQuery, session, self, resourceGroup, transactionManager, executor, ticker, metadata, warningCollector, queryType);
    queryStateMachine.addStateChangeListener(newState -> {
        QUERY_STATE_LOG.debug("Query %s is %s", queryStateMachine.getQueryId(), newState);
        if (newState.isDone()) {
            queryStateMachine.getSession().getTransactionId().ifPresent(transactionManager::trySetInactive);
        }
    });
    return queryStateMachine;
}
Also used : TransactionId(io.trino.transaction.TransactionId)

Example 2 with TransactionId

use of io.trino.transaction.TransactionId in project trino by trinodb.

the class TestAccessControlManager method testDenyCatalogAccessControl.

@Test
public void testDenyCatalogAccessControl() {
    try (LocalQueryRunner queryRunner = LocalQueryRunner.create(TEST_SESSION)) {
        TransactionManager transactionManager = queryRunner.getTransactionManager();
        AccessControlManager accessControlManager = createAccessControlManager(transactionManager);
        TestSystemAccessControlFactory accessControlFactory = new TestSystemAccessControlFactory("test");
        accessControlManager.addSystemAccessControlFactory(accessControlFactory);
        accessControlManager.setSystemAccessControl("test", ImmutableMap.of());
        queryRunner.createCatalog("catalog", MockConnectorFactory.create(), ImmutableMap.of());
        accessControlManager.addCatalogAccessControl(new CatalogName("catalog"), new DenyConnectorAccessControl());
        assertThatThrownBy(() -> transaction(transactionManager, accessControlManager).execute(transactionId -> {
            accessControlManager.checkCanSelectFromColumns(context(transactionId), new QualifiedObjectName("catalog", "schema", "table"), ImmutableSet.of("column"));
        })).isInstanceOf(TrinoException.class).hasMessageMatching("Access Denied: Cannot select from columns \\[column\\] in table or view schema.table");
    }
}
Also used : QueryId(io.trino.spi.QueryId) TransactionBuilder.transaction(io.trino.transaction.TransactionBuilder.transaction) TransactionManager(io.trino.transaction.TransactionManager) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.testng.annotations.Test) SystemSecurityContext(io.trino.spi.security.SystemSecurityContext) CatalogName(io.trino.connector.CatalogName) MockConnectorFactory(io.trino.connector.MockConnectorFactory) Map(java.util.Map) CatalogSchemaName(io.trino.spi.connector.CatalogSchemaName) TEST_SESSION(io.trino.SessionTestUtils.TEST_SESSION) Path(java.nio.file.Path) WRITE(java.nio.file.StandardOpenOption.WRITE) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) ViewExpression(io.trino.spi.security.ViewExpression) ConnectorAccessControl(io.trino.spi.connector.ConnectorAccessControl) Set(java.util.Set) TrinoException(io.trino.spi.TrinoException) SchemaTableName(io.trino.spi.connector.SchemaTableName) BasicPrincipal(io.trino.spi.security.BasicPrincipal) TestingEventListenerManager.emptyEventListenerManager(io.trino.testing.TestingEventListenerManager.emptyEventListenerManager) List(java.util.List) Principal(java.security.Principal) BIGINT(io.trino.spi.type.BigintType.BIGINT) ReadOnlySystemAccessControl(io.trino.plugin.base.security.ReadOnlySystemAccessControl) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) Optional(java.util.Optional) SystemAccessControlFactory(io.trino.spi.security.SystemAccessControlFactory) AccessDeniedException(io.trino.spi.security.AccessDeniedException) TestingEventListenerManager(io.trino.testing.TestingEventListenerManager) TRUNCATE_EXISTING(java.nio.file.StandardOpenOption.TRUNCATE_EXISTING) Type(io.trino.spi.type.Type) Assert.assertEquals(org.testng.Assert.assertEquals) AllowAllAccessControl(io.trino.plugin.base.security.AllowAllAccessControl) SystemAccessControl(io.trino.spi.security.SystemAccessControl) InMemoryTransactionManager.createTestTransactionManager(io.trino.transaction.InMemoryTransactionManager.createTestTransactionManager) ImmutableList(com.google.common.collect.ImmutableList) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Identity(io.trino.spi.security.Identity) LocalQueryRunner(io.trino.testing.LocalQueryRunner) Objects.requireNonNull(java.util.Objects.requireNonNull) ConnectorSecurityContext(io.trino.spi.connector.ConnectorSecurityContext) Files(java.nio.file.Files) AccessDeniedException.denySelectTable(io.trino.spi.security.AccessDeniedException.denySelectTable) IOException(java.io.IOException) Files.createTempFile(java.nio.file.Files.createTempFile) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) DefaultSystemAccessControl(io.trino.plugin.base.security.DefaultSystemAccessControl) EventListenerManager(io.trino.eventlistener.EventListenerManager) CatalogManager(io.trino.metadata.CatalogManager) CREATE(java.nio.file.StandardOpenOption.CREATE) TransactionId(io.trino.transaction.TransactionId) EventListener(io.trino.spi.eventlistener.EventListener) AllowAllSystemAccessControl(io.trino.plugin.base.security.AllowAllSystemAccessControl) TransactionManager(io.trino.transaction.TransactionManager) InMemoryTransactionManager.createTestTransactionManager(io.trino.transaction.InMemoryTransactionManager.createTestTransactionManager) TrinoException(io.trino.spi.TrinoException) CatalogName(io.trino.connector.CatalogName) LocalQueryRunner(io.trino.testing.LocalQueryRunner) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) Test(org.testng.annotations.Test)

Example 3 with TransactionId

use of io.trino.transaction.TransactionId in project trino by trinodb.

the class CommitTask method execute.

@Override
public ListenableFuture<Void> execute(Commit statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Session session = stateMachine.getSession();
    if (session.getTransactionId().isEmpty()) {
        throw new TrinoException(NOT_IN_TRANSACTION, "No transaction in progress");
    }
    TransactionId transactionId = session.getTransactionId().get();
    stateMachine.clearTransactionId();
    return transactionManager.asyncCommit(transactionId);
}
Also used : TrinoException(io.trino.spi.TrinoException) Session(io.trino.Session) TransactionId(io.trino.transaction.TransactionId)

Example 4 with TransactionId

use of io.trino.transaction.TransactionId in project trino by trinodb.

the class QueryMonitor method logQueryTimeline.

private static void logQueryTimeline(BasicQueryInfo queryInfo) {
    DateTime queryStartTime = queryInfo.getQueryStats().getCreateTime();
    DateTime queryEndTime = queryInfo.getQueryStats().getEndTime();
    // query didn't finish cleanly
    if (queryStartTime == null || queryEndTime == null) {
        return;
    }
    long elapsed = max(queryEndTime.getMillis() - queryStartTime.getMillis(), 0);
    logQueryTimeline(queryInfo.getQueryId(), queryInfo.getSession().getTransactionId().map(TransactionId::toString).orElse(""), elapsed, elapsed, 0, 0, 0, 0, queryStartTime, queryEndTime);
}
Also used : DateTime(org.joda.time.DateTime) TransactionId(io.trino.transaction.TransactionId)

Example 5 with TransactionId

use of io.trino.transaction.TransactionId in project trino by trinodb.

the class QueryMonitor method logQueryTimeline.

private static void logQueryTimeline(QueryInfo queryInfo) {
    try {
        QueryStats queryStats = queryInfo.getQueryStats();
        DateTime queryStartTime = queryStats.getCreateTime();
        DateTime queryEndTime = queryStats.getEndTime();
        // query didn't finish cleanly
        if (queryStartTime == null || queryEndTime == null) {
            return;
        }
        // planning duration -- start to end of planning
        long planning = queryStats.getPlanningTime().toMillis();
        // Time spent waiting for required no. of worker nodes to be present
        long waiting = queryStats.getResourceWaitingTime().toMillis();
        List<StageInfo> stages = getAllStages(queryInfo.getOutputStage());
        // long lastSchedulingCompletion = 0;
        long firstTaskStartTime = queryEndTime.getMillis();
        long lastTaskStartTime = queryStartTime.getMillis() + planning;
        long lastTaskEndTime = queryStartTime.getMillis() + planning;
        for (StageInfo stage : stages) {
            // only consider leaf stages
            if (!stage.getSubStages().isEmpty()) {
                continue;
            }
            for (TaskInfo taskInfo : stage.getTasks()) {
                TaskStats taskStats = taskInfo.getStats();
                DateTime firstStartTime = taskStats.getFirstStartTime();
                if (firstStartTime != null) {
                    firstTaskStartTime = Math.min(firstStartTime.getMillis(), firstTaskStartTime);
                }
                DateTime lastStartTime = taskStats.getLastStartTime();
                if (lastStartTime != null) {
                    lastTaskStartTime = max(lastStartTime.getMillis(), lastTaskStartTime);
                }
                DateTime endTime = taskStats.getEndTime();
                if (endTime != null) {
                    lastTaskEndTime = max(endTime.getMillis(), lastTaskEndTime);
                }
            }
        }
        long elapsed = max(queryEndTime.getMillis() - queryStartTime.getMillis(), 0);
        long scheduling = max(firstTaskStartTime - queryStartTime.getMillis() - planning, 0);
        long running = max(lastTaskEndTime - firstTaskStartTime, 0);
        long finishing = max(queryEndTime.getMillis() - lastTaskEndTime, 0);
        logQueryTimeline(queryInfo.getQueryId(), queryInfo.getSession().getTransactionId().map(TransactionId::toString).orElse(""), elapsed, planning, waiting, scheduling, running, finishing, queryStartTime, queryEndTime);
    } catch (Exception e) {
        log.error(e, "Error logging query timeline");
    }
}
Also used : TaskInfo(io.trino.execution.TaskInfo) QueryStats(io.trino.execution.QueryStats) StageInfo(io.trino.execution.StageInfo) TaskStats(io.trino.operator.TaskStats) DateTime(org.joda.time.DateTime) TransactionId(io.trino.transaction.TransactionId)

Aggregations

TransactionId (io.trino.transaction.TransactionId)22 Test (org.testng.annotations.Test)12 SchemaTableName (io.trino.spi.connector.SchemaTableName)11 ImmutableMap (com.google.common.collect.ImmutableMap)8 ConnectorSession (io.trino.spi.connector.ConnectorSession)8 Session (io.trino.Session)7 InformationSchemaMetadata (io.trino.connector.informationschema.InformationSchemaMetadata)7 InformationSchemaTableHandle (io.trino.connector.informationschema.InformationSchemaTableHandle)7 ConnectorMetadata (io.trino.spi.connector.ConnectorMetadata)7 Constraint (io.trino.spi.connector.Constraint)7 QualifiedObjectName (io.trino.metadata.QualifiedObjectName)6 TrinoException (io.trino.spi.TrinoException)6 CatalogName (io.trino.connector.CatalogName)5 ImmutableList (com.google.common.collect.ImmutableList)4 MockConnectorFactory (io.trino.connector.MockConnectorFactory)4 TransactionManager (io.trino.transaction.TransactionManager)4 Map (java.util.Map)4 ImmutableSet (com.google.common.collect.ImmutableSet)3 TEST_SESSION (io.trino.SessionTestUtils.TEST_SESSION)3 InformationSchemaColumnHandle (io.trino.connector.informationschema.InformationSchemaColumnHandle)3