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