Search in sources :

Example 6 with DatabaseContext

use of org.neo4j.dbms.database.DatabaseContext 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 7 with DatabaseContext

use of org.neo4j.dbms.database.DatabaseContext in project neo4j by neo4j.

the class BuiltInDbmsProcedures method killQueries.

@SystemProcedure
@Description("Kill all transactions executing a query with any of the given query ids.")
@Procedure(name = "dbms.killQueries", mode = DBMS)
public Stream<QueryTerminationResult> killQueries(@Name("ids") List<String> idTexts) throws InvalidArgumentsException {
    DatabaseManager<DatabaseContext> databaseManager = getDatabaseManager();
    DatabaseIdRepository databaseIdRepository = databaseManager.databaseIdRepository();
    Map<Long, QueryId> queryIds = new HashMap<>(idTexts.size());
    for (String idText : idTexts) {
        QueryId id = QueryId.parse(idText);
        queryIds.put(id.internalId(), id);
    }
    List<QueryTerminationResult> result = new ArrayList<>(queryIds.size());
    for (FabricTransaction tx : getFabricTransactions()) {
        for (ExecutingQuery query : getActiveFabricQueries(tx)) {
            QueryId givenQueryId = queryIds.remove(query.internalQueryId());
            if (givenQueryId != null) {
                result.add(killFabricQueryTransaction(givenQueryId, tx, query));
            }
        }
    }
    for (Map.Entry<NamedDatabaseId, DatabaseContext> databaseEntry : databaseManager.registeredDatabases().entrySet()) {
        NamedDatabaseId databaseId = databaseEntry.getKey();
        DatabaseContext databaseContext = databaseEntry.getValue();
        if (databaseContext.database().isStarted()) {
            for (KernelTransactionHandle tx : getExecutingTransactions(databaseContext)) {
                if (tx.executingQuery().isPresent()) {
                    QueryId givenQueryId = queryIds.remove(tx.executingQuery().get().internalQueryId());
                    if (givenQueryId != null) {
                        result.add(killQueryTransaction(givenQueryId, tx, databaseId));
                    }
                }
            }
        }
    }
    // Add error about the rest
    for (QueryId queryId : queryIds.values()) {
        result.add(new QueryFailedTerminationResult(queryId, "n/a", "No Query found with this id"));
    }
    return result.stream();
}
Also used : HashMap(java.util.HashMap) DatabaseIdRepository(org.neo4j.kernel.database.DatabaseIdRepository) ArrayList(java.util.ArrayList) FabricTransaction(org.neo4j.fabric.transaction.FabricTransaction) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle) DatabaseContext(org.neo4j.dbms.database.DatabaseContext) ExecutingQuery(org.neo4j.kernel.api.query.ExecutingQuery) 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 8 with DatabaseContext

use of org.neo4j.dbms.database.DatabaseContext in project neo4j by neo4j.

the class DatabaseAvailabilityIT method notConfusingMessageOnDatabaseNonAvailability.

@Test
void notConfusingMessageOnDatabaseNonAvailability() {
    DependencyResolver dependencyResolver = database.getDependencyResolver();
    DatabaseManager<?> databaseManager = getDatabaseManager(dependencyResolver);
    DatabaseContext databaseContext = databaseManager.getDatabaseContext(defaultNamedDatabaseId).get();
    DatabaseAvailability databaseAvailability = databaseContext.database().getDependencyResolver().resolveDependency(DatabaseAvailability.class);
    databaseAvailability.stop();
    TransactionFailureException exception = assertThrows(TransactionFailureException.class, () -> database.beginTx());
    assertEquals("Timeout waiting for database to become available and allow new transactions. Waited 1s. 1 reasons for blocking: Database unavailable.", exception.getMessage());
}
Also used : DatabaseContext(org.neo4j.dbms.database.DatabaseContext) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) DatabaseAvailability(org.neo4j.kernel.availability.DatabaseAvailability) DependencyResolver(org.neo4j.common.DependencyResolver) Test(org.junit.jupiter.api.Test)

Aggregations

DatabaseContext (org.neo4j.dbms.database.DatabaseContext)8 Test (org.junit.jupiter.api.Test)4 DependencyResolver (org.neo4j.common.DependencyResolver)4 KernelTransactionHandle (org.neo4j.kernel.api.KernelTransactionHandle)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 HashMap (java.util.HashMap)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 ZoneId (java.time.ZoneId)2 Map (java.util.Map)2 FabricTransaction (org.neo4j.fabric.transaction.FabricTransaction)2 ExecutingQuery (org.neo4j.kernel.api.query.ExecutingQuery)2 DatabaseIdRepository (org.neo4j.kernel.database.DatabaseIdRepository)2 NamedDatabaseId (org.neo4j.kernel.database.NamedDatabaseId)2 HashSet (java.util.HashSet)1 Optional (java.util.Optional)1