Search in sources :

Example 6 with KernelTransactionHandle

use of org.neo4j.kernel.api.KernelTransactionHandle in project neo4j by neo4j.

the class TransactionDependenciesResolverTest method detectIndependentTransactionsAsNotBlocked.

@Test
void detectIndependentTransactionsAsNotBlocked() {
    HashMap<KernelTransactionHandle, Optional<QuerySnapshot>> map = new HashMap<>();
    TestKernelTransactionHandle handle1 = new TestKernelTransactionHandleWithLocks(new StubKernelTransaction());
    TestKernelTransactionHandle handle2 = new TestKernelTransactionHandleWithLocks(new StubKernelTransaction());
    map.put(handle1, Optional.of(createQuerySnapshot(1)));
    map.put(handle2, Optional.of(createQuerySnapshot(2)));
    TransactionDependenciesResolver resolver = new TransactionDependenciesResolver(map);
    assertFalse(resolver.isBlocked(handle1));
    assertFalse(resolver.isBlocked(handle2));
}
Also used : Optional(java.util.Optional) HashMap(java.util.HashMap) TestKernelTransactionHandle(org.neo4j.kernel.impl.api.TestKernelTransactionHandle) TestKernelTransactionHandle(org.neo4j.kernel.impl.api.TestKernelTransactionHandle) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle) Test(org.junit.jupiter.api.Test)

Example 7 with KernelTransactionHandle

use of org.neo4j.kernel.api.KernelTransactionHandle in project neo4j by neo4j.

the class BuiltInDbmsProcedures method listTransactions.

@SystemProcedure
@Description("List all transactions currently executing at this instance that are visible to the user.")
@Procedure(name = "dbms.listTransactions", mode = DBMS)
public Stream<TransactionStatusResult> listTransactions() throws InvalidArgumentsException {
    ZoneId zoneId = getConfiguredTimeZone();
    List<TransactionStatusResult> result = new ArrayList<>();
    for (DatabaseContext databaseContext : getDatabaseManager().registeredDatabases().values()) {
        if (databaseContext.database().isStarted()) {
            DatabaseScope dbScope = new DatabaseScope(databaseContext.database().getNamedDatabaseId().name());
            Map<KernelTransactionHandle, Optional<QuerySnapshot>> handleQuerySnapshotsMap = new HashMap<>();
            for (KernelTransactionHandle tx : getExecutingTransactions(databaseContext)) {
                String username = tx.subject().username();
                var action = new AdminActionOnResource(SHOW_TRANSACTION, dbScope, new UserSegment(username));
                if (isSelfOrAllows(username, action)) {
                    handleQuerySnapshotsMap.put(tx, tx.executingQuery().map(ExecutingQuery::snapshot));
                }
            }
            TransactionDependenciesResolver transactionBlockerResolvers = new TransactionDependenciesResolver(handleQuerySnapshotsMap);
            for (KernelTransactionHandle tx : handleQuerySnapshotsMap.keySet()) {
                result.add(new TransactionStatusResult(databaseContext.databaseFacade().databaseName(), tx, transactionBlockerResolvers, handleQuerySnapshotsMap, zoneId));
            }
        }
    }
    return result.stream();
}
Also used : AdminActionOnResource(org.neo4j.internal.kernel.api.security.AdminActionOnResource) ZoneId(java.time.ZoneId) DatabaseScope(org.neo4j.internal.kernel.api.security.AdminActionOnResource.DatabaseScope) Optional(java.util.Optional) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle) DatabaseContext(org.neo4j.dbms.database.DatabaseContext) UserSegment(org.neo4j.internal.kernel.api.security.UserSegment) Description(org.neo4j.procedure.Description) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) Procedure(org.neo4j.procedure.Procedure)

Example 8 with KernelTransactionHandle

use of org.neo4j.kernel.api.KernelTransactionHandle in project neo4j by neo4j.

the class BuiltInDbmsProcedures method listQueries.

@SystemProcedure
@Description("List all queries currently executing at this instance that are visible to the user.")
@Procedure(name = "dbms.listQueries", mode = DBMS)
public Stream<QueryStatusResult> listQueries() throws InvalidArgumentsException {
    ZoneId zoneId = getConfiguredTimeZone();
    List<QueryStatusResult> result = new ArrayList<>();
    for (FabricTransaction tx : getFabricTransactions()) {
        for (ExecutingQuery query : getActiveFabricQueries(tx)) {
            String username = query.username();
            var action = new AdminActionOnResource(SHOW_TRANSACTION, ALL, new UserSegment(username));
            if (isSelfOrAllows(username, action)) {
                result.add(new QueryStatusResult(query, (InternalTransaction) transaction, zoneId, "none"));
            }
        }
    }
    for (DatabaseContext databaseContext : getDatabaseManager().registeredDatabases().values()) {
        if (databaseContext.database().isStarted()) {
            DatabaseScope dbScope = new DatabaseScope(databaseContext.database().getNamedDatabaseId().name());
            for (KernelTransactionHandle tx : getExecutingTransactions(databaseContext)) {
                if (tx.executingQuery().isPresent()) {
                    ExecutingQuery query = tx.executingQuery().get();
                    // Include both the executing query and any previous queries (parent queries of nested query) in the result.
                    while (query != null) {
                        String username = query.username();
                        var action = new AdminActionOnResource(SHOW_TRANSACTION, dbScope, new UserSegment(username));
                        if (isSelfOrAllows(username, action)) {
                            result.add(new QueryStatusResult(query, (InternalTransaction) transaction, zoneId, databaseContext.databaseFacade().databaseName()));
                        }
                        query = query.getPreviousQuery();
                    }
                }
            }
        }
    }
    return result.stream();
}
Also used : AdminActionOnResource(org.neo4j.internal.kernel.api.security.AdminActionOnResource) ZoneId(java.time.ZoneId) DatabaseScope(org.neo4j.internal.kernel.api.security.AdminActionOnResource.DatabaseScope) ArrayList(java.util.ArrayList) FabricTransaction(org.neo4j.fabric.transaction.FabricTransaction) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle) DatabaseContext(org.neo4j.dbms.database.DatabaseContext) ExecutingQuery(org.neo4j.kernel.api.query.ExecutingQuery) UserSegment(org.neo4j.internal.kernel.api.security.UserSegment) Description(org.neo4j.procedure.Description) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) Procedure(org.neo4j.procedure.Procedure)

Example 9 with KernelTransactionHandle

use of org.neo4j.kernel.api.KernelTransactionHandle in project neo4j by neo4j.

the class BuiltInDbmsProcedures method killTransactions.

@SystemProcedure
@Description("Kill transactions with provided ids.")
@Procedure(name = "dbms.killTransactions", mode = DBMS)
public Stream<TransactionMarkForTerminationResult> killTransactions(@Name("ids") List<String> transactionIds) throws InvalidArgumentsException {
    requireNonNull(transactionIds);
    log.warn("User %s trying to kill transactions: %s.", securityContext.subject().username(), transactionIds.toString());
    DatabaseManager<DatabaseContext> databaseManager = getDatabaseManager();
    DatabaseIdRepository databaseIdRepository = databaseManager.databaseIdRepository();
    Map<NamedDatabaseId, Set<TransactionId>> byDatabase = new HashMap<>();
    for (String idText : transactionIds) {
        TransactionId id = TransactionId.parse(idText);
        Optional<NamedDatabaseId> namedDatabaseId = databaseIdRepository.getByName(id.database());
        namedDatabaseId.ifPresent(databaseId -> byDatabase.computeIfAbsent(databaseId, ignore -> new HashSet<>()).add(id));
    }
    Map<String, KernelTransactionHandle> handles = new HashMap<>(transactionIds.size());
    for (Map.Entry<NamedDatabaseId, Set<TransactionId>> entry : byDatabase.entrySet()) {
        NamedDatabaseId databaseId = entry.getKey();
        var dbScope = new DatabaseScope(databaseId.name());
        Optional<DatabaseContext> maybeDatabaseContext = databaseManager.getDatabaseContext(databaseId);
        if (maybeDatabaseContext.isPresent()) {
            Set<TransactionId> txIds = entry.getValue();
            DatabaseContext databaseContext = maybeDatabaseContext.get();
            for (KernelTransactionHandle tx : getExecutingTransactions(databaseContext)) {
                String username = tx.subject().username();
                var action = new AdminActionOnResource(TERMINATE_TRANSACTION, dbScope, new UserSegment(username));
                if (!isSelfOrAllows(username, action)) {
                    continue;
                }
                TransactionId txIdRepresentation = new TransactionId(databaseId.name(), tx.getUserTransactionId());
                if (txIds.contains(txIdRepresentation)) {
                    handles.put(txIdRepresentation.toString(), tx);
                }
            }
        }
    }
    return transactionIds.stream().map(id -> terminateTransaction(handles, id));
}
Also used : AdminActionOnResource(org.neo4j.internal.kernel.api.security.AdminActionOnResource) Set(java.util.Set) HashSet(java.util.HashSet) DatabaseScope(org.neo4j.internal.kernel.api.security.AdminActionOnResource.DatabaseScope) HashMap(java.util.HashMap) DatabaseIdRepository(org.neo4j.kernel.database.DatabaseIdRepository) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle) DatabaseContext(org.neo4j.dbms.database.DatabaseContext) UserSegment(org.neo4j.internal.kernel.api.security.UserSegment) NamedDatabaseId(org.neo4j.kernel.database.NamedDatabaseId) Map(java.util.Map) HashMap(java.util.HashMap) Description(org.neo4j.procedure.Description) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) Procedure(org.neo4j.procedure.Procedure)

Example 10 with KernelTransactionHandle

use of org.neo4j.kernel.api.KernelTransactionHandle in project neo4j by neo4j.

the class KernelTransactionTimeoutMonitorIT method terminateOngoingTransaction.

private void terminateOngoingTransaction() {
    Set<KernelTransactionHandle> kernelTransactionHandles = kernelTransactions.activeTransactions();
    assertThat(kernelTransactionHandles).hasSize(1);
    for (KernelTransactionHandle kernelTransactionHandle : kernelTransactionHandles) {
        kernelTransactionHandle.markForTermination(Status.Transaction.Terminated);
    }
}
Also used : KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle)

Aggregations

KernelTransactionHandle (org.neo4j.kernel.api.KernelTransactionHandle)20 HashMap (java.util.HashMap)11 Test (org.junit.jupiter.api.Test)9 Optional (java.util.Optional)8 TestKernelTransactionHandle (org.neo4j.kernel.impl.api.TestKernelTransactionHandle)6 ActiveLock (org.neo4j.lock.ActiveLock)5 HashSet (java.util.HashSet)4 Map (java.util.Map)4 DatabaseContext (org.neo4j.dbms.database.DatabaseContext)4 SystemProcedure (org.neo4j.kernel.api.procedure.SystemProcedure)4 Description (org.neo4j.procedure.Description)4 Procedure (org.neo4j.procedure.Procedure)4 ArrayList (java.util.ArrayList)3 AdminActionOnResource (org.neo4j.internal.kernel.api.security.AdminActionOnResource)3 DatabaseScope (org.neo4j.internal.kernel.api.security.AdminActionOnResource.DatabaseScope)3 UserSegment (org.neo4j.internal.kernel.api.security.UserSegment)3 KernelTransactionMonitor (org.neo4j.kernel.impl.api.transaction.monitor.KernelTransactionMonitor)3 ZoneId (java.time.ZoneId)2 List (java.util.List)2 Set (java.util.Set)2