Search in sources :

Example 1 with DatabaseContext

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

the class DatabaseAvailabilityIT method restartedDatabaseIsAvailable.

@Test
void restartedDatabaseIsAvailable() {
    DependencyResolver dependencyResolver = database.getDependencyResolver();
    DatabaseManager<?> databaseManager = getDatabaseManager(dependencyResolver);
    DatabaseContext databaseContext = databaseManager.getDatabaseContext(defaultNamedDatabaseId).get();
    Database database = databaseContext.database();
    executeTransactionOnDefaultDatabase();
    database.stop();
    assertThrows(DatabaseShutdownException.class, () -> this.database.beginTx());
    database.start();
    executeTransactionOnDefaultDatabase();
}
Also used : DatabaseContext(org.neo4j.dbms.database.DatabaseContext) Database(org.neo4j.kernel.database.Database) DependencyResolver(org.neo4j.common.DependencyResolver) Test(org.junit.jupiter.api.Test)

Example 2 with DatabaseContext

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

the class DatabaseAvailabilityIT method stoppedDatabaseIsNotAvailable.

@Test
void stoppedDatabaseIsNotAvailable() {
    DependencyResolver dependencyResolver = database.getDependencyResolver();
    DatabaseManager<?> databaseManager = getDatabaseManager(dependencyResolver);
    DatabaseContext databaseContext = databaseManager.getDatabaseContext(defaultNamedDatabaseId).get();
    databaseContext.database().stop();
    assertThrows(DatabaseShutdownException.class, () -> database.beginTx());
}
Also used : DatabaseContext(org.neo4j.dbms.database.DatabaseContext) DependencyResolver(org.neo4j.common.DependencyResolver) Test(org.junit.jupiter.api.Test)

Example 3 with DatabaseContext

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

the class DatabaseAvailabilityIT method anyOfDatabaseUnavailabilityIsGlobalUnavailability.

@Test
void anyOfDatabaseUnavailabilityIsGlobalUnavailability() {
    AvailabilityRequirement outerSpaceRequirement = () -> "outer space";
    DependencyResolver dependencyResolver = database.getDependencyResolver();
    DatabaseManager<?> databaseManager = getDatabaseManager(dependencyResolver);
    CompositeDatabaseAvailabilityGuard compositeGuard = dependencyResolver.resolveDependency(CompositeDatabaseAvailabilityGuard.class);
    assertTrue(compositeGuard.isAvailable());
    DatabaseContext systemContext = databaseManager.getDatabaseContext(NAMED_SYSTEM_DATABASE_ID).get();
    DatabaseContext defaultContext = databaseManager.getDatabaseContext(defaultNamedDatabaseId).get();
    AvailabilityGuard systemGuard = systemContext.dependencies().resolveDependency(DatabaseAvailabilityGuard.class);
    systemGuard.require(outerSpaceRequirement);
    assertFalse(compositeGuard.isAvailable());
    systemGuard.fulfill(outerSpaceRequirement);
    assertTrue(compositeGuard.isAvailable());
    AvailabilityGuard defaultGuard = defaultContext.dependencies().resolveDependency(DatabaseAvailabilityGuard.class);
    defaultGuard.require(outerSpaceRequirement);
    assertFalse(compositeGuard.isAvailable());
    defaultGuard.fulfill(outerSpaceRequirement);
    assertTrue(compositeGuard.isAvailable());
}
Also used : DatabaseContext(org.neo4j.dbms.database.DatabaseContext) CompositeDatabaseAvailabilityGuard(org.neo4j.kernel.availability.CompositeDatabaseAvailabilityGuard) AvailabilityGuard(org.neo4j.kernel.availability.AvailabilityGuard) CompositeDatabaseAvailabilityGuard(org.neo4j.kernel.availability.CompositeDatabaseAvailabilityGuard) DatabaseAvailabilityGuard(org.neo4j.kernel.availability.DatabaseAvailabilityGuard) AvailabilityRequirement(org.neo4j.kernel.availability.AvailabilityRequirement) DependencyResolver(org.neo4j.common.DependencyResolver) Test(org.junit.jupiter.api.Test)

Example 4 with DatabaseContext

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

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

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