Search in sources :

Example 1 with NamedDatabaseId

use of org.neo4j.kernel.database.NamedDatabaseId in project neo4j by neo4j.

the class DefaultDatabaseManager method initialiseDefaultDatabase.

@Override
public void initialiseDefaultDatabase() {
    String databaseName = config.get(default_database);
    NamedDatabaseId namedDatabaseId = databaseIdRepository().getByName(databaseName).orElseThrow(() -> new DatabaseNotFoundException("Default database not found: " + databaseName));
    StandaloneDatabaseContext context = createDatabase(namedDatabaseId);
    if (manageDatabasesOnStartAndStop) {
        this.startDatabase(namedDatabaseId, context);
    }
}
Also used : DatabaseNotFoundException(org.neo4j.dbms.api.DatabaseNotFoundException) NamedDatabaseId(org.neo4j.kernel.database.NamedDatabaseId)

Example 2 with NamedDatabaseId

use of org.neo4j.kernel.database.NamedDatabaseId in project neo4j by neo4j.

the class AbstractDatabaseManager method forEachDatabase.

private void forEachDatabase(BiConsumer<NamedDatabaseId, DB> consumer, boolean systemDatabaseLast, String operationName) {
    var snapshot = systemDatabaseLast ? databasesSnapshot().descendingMap().entrySet() : databasesSnapshot().entrySet();
    DatabaseManagementException dbmsExceptions = null;
    for (var entry : snapshot) {
        NamedDatabaseId namedDatabaseId = entry.getKey();
        DB context = entry.getValue();
        try {
            consumer.accept(namedDatabaseId, context);
        } catch (Throwable t) {
            var dbmsException = new DatabaseManagementException(format("An error occurred! Unable to %s the database `%s`.", operationName, namedDatabaseId), t);
            dbmsExceptions = Exceptions.chain(dbmsExceptions, dbmsException);
        }
    }
    if (dbmsExceptions != null) {
        throw dbmsExceptions;
    }
}
Also used : DatabaseManagementException(org.neo4j.dbms.api.DatabaseManagementException) NamedDatabaseId(org.neo4j.kernel.database.NamedDatabaseId)

Example 3 with NamedDatabaseId

use of org.neo4j.kernel.database.NamedDatabaseId 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 4 with NamedDatabaseId

use of org.neo4j.kernel.database.NamedDatabaseId in project neo4j by neo4j.

the class DefaultDatabaseManagerTest method shouldThrowIfUpgradingNonExistingDatabase.

@Test
void shouldThrowIfUpgradingNonExistingDatabase() {
    // Given
    NamedDatabaseId namedDatabaseId = DatabaseIdFactory.from("foo", UUID.randomUUID());
    GlobalModule mock = mock(GlobalModule.class, RETURNS_MOCKS);
    DefaultDatabaseManager databaseManager = new DefaultDatabaseManager(mock, mock(AbstractEditionModule.class));
    // Then
    DatabaseManagementException e = assertThrows(DatabaseManagementException.class, () -> databaseManager.upgradeDatabase(namedDatabaseId));
    assertThat(e).hasMessage("Database not found: " + namedDatabaseId);
}
Also used : GlobalModule(org.neo4j.graphdb.factory.module.GlobalModule) DatabaseManagementException(org.neo4j.dbms.api.DatabaseManagementException) AbstractEditionModule(org.neo4j.graphdb.factory.module.edition.AbstractEditionModule) NamedDatabaseId(org.neo4j.kernel.database.NamedDatabaseId) Test(org.junit.jupiter.api.Test)

Example 5 with NamedDatabaseId

use of org.neo4j.kernel.database.NamedDatabaseId in project neo4j by neo4j.

the class DbmsDiagnosticsManagerTest method dumpDiagnosticsInConciseForm.

@Test
void dumpDiagnosticsInConciseForm() {
    Map<NamedDatabaseId, StandaloneDatabaseContext> databaseMap = new HashMap<>();
    int numberOfDatabases = 1000;
    for (int i = 0; i < numberOfDatabases; i++) {
        Database database = mock(Database.class);
        NamedDatabaseId namedDatabaseId = TestDatabaseIdRepository.randomNamedDatabaseId();
        when(database.getNamedDatabaseId()).thenReturn(namedDatabaseId);
        databaseMap.put(namedDatabaseId, new StandaloneDatabaseContext(database));
    }
    when(databaseManager.registeredDatabases()).thenReturn(new TreeMap<>(databaseMap));
    diagnosticsManager.dumpAll();
    var logAssertions = assertThat(logProvider);
    var databaseNames = databaseMap.keySet().stream().map(NamedDatabaseId::name).toArray(String[]::new);
    logAssertions.containsMessagesOnce(databaseNames);
}
Also used : HashMap(java.util.HashMap) Database(org.neo4j.kernel.database.Database) NamedDatabaseId(org.neo4j.kernel.database.NamedDatabaseId) StandaloneDatabaseContext(org.neo4j.dbms.database.StandaloneDatabaseContext) Test(org.junit.jupiter.api.Test)

Aggregations

NamedDatabaseId (org.neo4j.kernel.database.NamedDatabaseId)13 Test (org.junit.jupiter.api.Test)7 HashMap (java.util.HashMap)3 DatabaseManagementException (org.neo4j.dbms.api.DatabaseManagementException)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 CommunityDatabaseState (org.neo4j.dbms.CommunityDatabaseState)2 DatabaseState (org.neo4j.dbms.DatabaseState)2 StubDatabaseStateService (org.neo4j.dbms.StubDatabaseStateService)2 DatabaseContext (org.neo4j.dbms.database.DatabaseContext)2 KernelTransactionHandle (org.neo4j.kernel.api.KernelTransactionHandle)2 ResourceTracker (org.neo4j.kernel.api.ResourceTracker)2 Context (org.neo4j.kernel.api.procedure.Context)2 SystemProcedure (org.neo4j.kernel.api.procedure.SystemProcedure)2 DatabaseIdRepository (org.neo4j.kernel.database.DatabaseIdRepository)2 Description (org.neo4j.procedure.Description)2 Procedure (org.neo4j.procedure.Procedure)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 BoltIOException (org.neo4j.bolt.messaging.BoltIOException)1