Search in sources :

Example 1 with SystemProcedure

use of org.neo4j.kernel.api.procedure.SystemProcedure in project neo4j by neo4j.

the class BuiltInDbmsProcedures method listConfig.

@Admin
@SystemProcedure
@Description("List the currently active config of Neo4j.")
@Procedure(name = "dbms.listConfig", mode = DBMS)
public Stream<ConfigResult> listConfig(@Name(value = "searchString", defaultValue = "") String searchString) {
    String lowerCasedSearchString = searchString.toLowerCase();
    List<ConfigResult> results = new ArrayList<>();
    Config config = graph.getDependencyResolver().resolveDependency(Config.class);
    config.getValues().forEach((setting, value) -> {
        if (!setting.internal() && setting.name().toLowerCase().contains(lowerCasedSearchString)) {
            results.add(new ConfigResult(setting, value));
        }
    });
    return results.stream().sorted(Comparator.comparing(c -> c.name));
}
Also used : SystemGraphComponent(org.neo4j.dbms.database.SystemGraphComponent) Log(org.neo4j.logging.Log) StoreIdProvider(org.neo4j.storageengine.api.StoreIdProvider) Status(org.neo4j.kernel.api.exceptions.Status) FunctionInformation(org.neo4j.kernel.impl.query.FunctionInformation) Config(org.neo4j.configuration.Config) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) ProceduresTimeFormatHelper.formatTime(org.neo4j.procedure.builtin.ProceduresTimeFormatHelper.formatTime) ProcedureCallFailed(org.neo4j.kernel.api.exceptions.Status.Procedure.ProcedureCallFailed) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) FabricTransaction(org.neo4j.fabric.transaction.FabricTransaction) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) Collections.singletonList(java.util.Collections.singletonList) Capability(org.neo4j.capabilities.Capability) UserFunctionSignature(org.neo4j.internal.kernel.api.procs.UserFunctionSignature) Map(java.util.Map) SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) ExecutingQuery(org.neo4j.kernel.api.query.ExecutingQuery) NamedDatabaseId(org.neo4j.kernel.database.NamedDatabaseId) Transaction(org.neo4j.graphdb.Transaction) Procedure(org.neo4j.procedure.Procedure) DatabaseIdRepository(org.neo4j.kernel.database.DatabaseIdRepository) Dependencies(org.neo4j.collection.Dependencies) TrackedNetworkConnection(org.neo4j.kernel.api.net.TrackedNetworkConnection) Set(java.util.Set) SHOW_TRANSACTION(org.neo4j.internal.kernel.api.security.PrivilegeAction.SHOW_TRANSACTION) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) DatabaseContext(org.neo4j.dbms.database.DatabaseContext) Internal(org.neo4j.procedure.Internal) String.format(java.lang.String.format) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) List(java.util.List) Stream(java.util.stream.Stream) UserSegment(org.neo4j.internal.kernel.api.security.UserSegment) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Optional(java.util.Optional) Name(org.neo4j.procedure.Name) StoreIdDecodeUtils.decodeId(org.neo4j.procedure.builtin.StoreIdDecodeUtils.decodeId) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Admin(org.neo4j.procedure.Admin) DatabaseManager(org.neo4j.dbms.database.DatabaseManager) FabricStatementLifecycles(org.neo4j.fabric.executor.FabricStatementLifecycles) KernelTransactions(org.neo4j.kernel.impl.api.KernelTransactions) QueryExecutionEngine(org.neo4j.kernel.impl.query.QueryExecutionEngine) GraphDatabaseSettings(org.neo4j.configuration.GraphDatabaseSettings) Context(org.neo4j.procedure.Context) UNINITIALIZED(org.neo4j.dbms.database.SystemGraphComponent.Status.UNINITIALIZED) HashMap(java.util.HashMap) CapabilitiesService(org.neo4j.capabilities.CapabilitiesService) SYSTEM_DATABASE_NAME(org.neo4j.configuration.GraphDatabaseSettings.SYSTEM_DATABASE_NAME) ArrayList(java.util.ArrayList) GlobalProcedures(org.neo4j.kernel.api.procedure.GlobalProcedures) HashSet(java.util.HashSet) WRITE(org.neo4j.procedure.Mode.WRITE) SystemGraphComponents(org.neo4j.dbms.database.SystemGraphComponents) Objects.requireNonNull(java.util.Objects.requireNonNull) DBMS(org.neo4j.procedure.Mode.DBMS) DependencyResolver(org.neo4j.common.DependencyResolver) TransactionManager(org.neo4j.fabric.transaction.TransactionManager) QuerySnapshot(org.neo4j.kernel.api.query.QuerySnapshot) REQUIRES_UPGRADE(org.neo4j.dbms.database.SystemGraphComponent.Status.REQUIRES_UPGRADE) READ(org.neo4j.procedure.Mode.READ) InvalidArgumentsException(org.neo4j.kernel.api.exceptions.InvalidArgumentsException) Description(org.neo4j.procedure.Description) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle) TERMINATE_TRANSACTION(org.neo4j.internal.kernel.api.security.PrivilegeAction.TERMINATE_TRANSACTION) ProcedureCallContext(org.neo4j.internal.kernel.api.procs.ProcedureCallContext) Collectors.toList(java.util.stream.Collectors.toList) NetworkConnectionTracker(org.neo4j.kernel.api.net.NetworkConnectionTracker) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ProcedureSignature(org.neo4j.internal.kernel.api.procs.ProcedureSignature) ALL(org.neo4j.internal.kernel.api.security.AdminActionOnResource.DatabaseScope.ALL) Comparator(java.util.Comparator) AdminActionOnResource(org.neo4j.internal.kernel.api.security.AdminActionOnResource) Collections(java.util.Collections) DatabaseScope(org.neo4j.internal.kernel.api.security.AdminActionOnResource.DatabaseScope) Config(org.neo4j.configuration.Config) ArrayList(java.util.ArrayList) Description(org.neo4j.procedure.Description) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) Procedure(org.neo4j.procedure.Procedure) Admin(org.neo4j.procedure.Admin)

Example 2 with SystemProcedure

use of org.neo4j.kernel.api.procedure.SystemProcedure 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 3 with SystemProcedure

use of org.neo4j.kernel.api.procedure.SystemProcedure in project neo4j by neo4j.

the class BuiltInDbmsProcedures method clearAllQueryCaches.

@Admin
@SystemProcedure
@Description("Clears all query caches.")
@Procedure(name = "db.clearQueryCaches", mode = DBMS)
public Stream<StringResult> clearAllQueryCaches() {
    QueryExecutionEngine queryExecutionEngine = graph.getDependencyResolver().resolveDependency(QueryExecutionEngine.class);
    // this query itself does not count
    long numberOfClearedQueries = queryExecutionEngine.clearQueryCaches() - 1;
    String result = numberOfClearedQueries == 0 ? "Query cache already empty." : "Query caches successfully cleared of " + numberOfClearedQueries + " queries.";
    log.info("Called db.clearQueryCaches(): " + result);
    return Stream.of(new StringResult(result));
}
Also used : QueryExecutionEngine(org.neo4j.kernel.impl.query.QueryExecutionEngine) Description(org.neo4j.procedure.Description) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) Procedure(org.neo4j.procedure.Procedure) Admin(org.neo4j.procedure.Admin)

Example 4 with SystemProcedure

use of org.neo4j.kernel.api.procedure.SystemProcedure 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)

Example 5 with SystemProcedure

use of org.neo4j.kernel.api.procedure.SystemProcedure 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)

Aggregations

SystemProcedure (org.neo4j.kernel.api.procedure.SystemProcedure)20 Procedure (org.neo4j.procedure.Procedure)20 Description (org.neo4j.procedure.Description)19 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)11 ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)11 ZoneId (java.time.ZoneId)10 Map (java.util.Map)10 Admin (org.neo4j.procedure.Admin)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)9 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)9 QueryExecutionEngine (org.neo4j.kernel.impl.query.QueryExecutionEngine)9 Comparator (java.util.Comparator)8 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 Stream (java.util.stream.Stream)8 DependencyResolver (org.neo4j.common.DependencyResolver)8 Config (org.neo4j.configuration.Config)8 GraphDatabaseSettings (org.neo4j.configuration.GraphDatabaseSettings)8 DatabaseContext (org.neo4j.dbms.database.DatabaseContext)8