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