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;
}
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");
}
}
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);
}
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);
}
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");
}
}
Aggregations