Search in sources :

Example 16 with KernelTransactionHandle

use of org.neo4j.kernel.api.KernelTransactionHandle in project neo4j by neo4j.

the class BuiltInDbmsProcedures method terminateTransaction.

private TransactionMarkForTerminationResult terminateTransaction(Map<String, KernelTransactionHandle> handles, String transactionId) {
    KernelTransactionHandle handle = handles.get(transactionId);
    String currentUser = securityContext.subject().username();
    if (handle == null) {
        return new TransactionMarkForTerminationFailedResult(transactionId, currentUser);
    }
    if (handle.isClosing()) {
        return new TransactionMarkForTerminationFailedResult(transactionId, currentUser, "Unable to kill closing transactions.");
    }
    log.debug("User %s terminated transaction %s.", currentUser, transactionId);
    handle.markForTermination(Status.Transaction.Terminated);
    return new TransactionMarkForTerminationResult(transactionId, handle.subject().username());
}
Also used : KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle)

Example 17 with KernelTransactionHandle

use of org.neo4j.kernel.api.KernelTransactionHandle in project neo4j by neo4j.

the class TransactionDependenciesResolver method evaluateDirectDependencies.

private static void evaluateDirectDependencies(Map<KernelTransactionHandle, Set<KernelTransactionHandle>> directDependencies, Map<KernelTransactionHandle, List<ActiveLock>> handleLocksMap, KernelTransactionHandle txHandle, QuerySnapshot querySnapshot) {
    List<ActiveLock> waitingOnLocks = querySnapshot.waitingLocks();
    for (ActiveLock activeLock : waitingOnLocks) {
        for (Map.Entry<KernelTransactionHandle, List<ActiveLock>> handleListEntry : handleLocksMap.entrySet()) {
            KernelTransactionHandle kernelTransactionHandle = handleListEntry.getKey();
            if (!kernelTransactionHandle.equals(txHandle)) {
                if (isBlocked(activeLock, handleListEntry.getValue())) {
                    Set<KernelTransactionHandle> kernelTransactionHandles = directDependencies.computeIfAbsent(txHandle, handle -> new HashSet<>());
                    kernelTransactionHandles.add(kernelTransactionHandle);
                }
            }
        }
    }
}
Also used : ActiveLock(org.neo4j.lock.ActiveLock) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) HashMap(java.util.HashMap) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle)

Example 18 with KernelTransactionHandle

use of org.neo4j.kernel.api.KernelTransactionHandle in project neo4j by neo4j.

the class TransactionDependenciesResolver method describeBlockingTransactions.

String describeBlockingTransactions(KernelTransactionHandle handle) {
    Set<KernelTransactionHandle> allBlockers = new TreeSet<>(Comparator.comparingLong(KernelTransactionHandle::getUserTransactionId));
    Set<KernelTransactionHandle> handles = directDependencies.get(handle);
    if (handles != null) {
        Deque<KernelTransactionHandle> blockerQueue = new ArrayDeque<>(handles);
        while (!blockerQueue.isEmpty()) {
            KernelTransactionHandle transactionHandle = blockerQueue.pop();
            if (allBlockers.add(transactionHandle)) {
                Set<KernelTransactionHandle> transactionHandleSet = directDependencies.get(transactionHandle);
                if (transactionHandleSet != null) {
                    blockerQueue.addAll(transactionHandleSet);
                }
            }
        }
    }
    return describe(allBlockers);
}
Also used : TreeSet(java.util.TreeSet) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle) ArrayDeque(java.util.ArrayDeque)

Example 19 with KernelTransactionHandle

use of org.neo4j.kernel.api.KernelTransactionHandle in project neo4j by neo4j.

the class TransactionDependenciesResolver method initDirectDependencies.

private Map<KernelTransactionHandle, Set<KernelTransactionHandle>> initDirectDependencies() {
    Map<KernelTransactionHandle, Set<KernelTransactionHandle>> directDependencies = new HashMap<>();
    Map<KernelTransactionHandle, List<ActiveLock>> transactionLocksMap = handleSnapshotsMap.keySet().stream().collect(toMap(identity(), getTransactionLocks()));
    for (Map.Entry<KernelTransactionHandle, Optional<QuerySnapshot>> entry : handleSnapshotsMap.entrySet()) {
        Optional<QuerySnapshot> snapshot = entry.getValue();
        if (snapshot.isPresent()) {
            KernelTransactionHandle txHandle = entry.getKey();
            evaluateDirectDependencies(directDependencies, transactionLocksMap, txHandle, snapshot.get());
        }
    }
    return directDependencies;
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Optional(java.util.Optional) HashMap(java.util.HashMap) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle) HashMap(java.util.HashMap) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) QuerySnapshot(org.neo4j.kernel.api.query.QuerySnapshot)

Example 20 with KernelTransactionHandle

use of org.neo4j.kernel.api.KernelTransactionHandle in project neo4j by neo4j.

the class BoltV3TransportIT method shouldSetTxMetadata.

@ParameterizedTest(name = "{0}")
@MethodSource("argumentsProvider")
public void shouldSetTxMetadata(Class<? extends TransportConnection> connectionClass) throws Exception {
    init(connectionClass);
    // Given
    negotiateBoltV3();
    Map<String, Object> txMetadata = map("who-is-your-boss", "Molly-mostly-white");
    Map<String, Object> msgMetadata = map("tx_metadata", txMetadata);
    MapValue meta = asMapValue(msgMetadata);
    connection.send(util.chunk(new BeginMessage(meta, List.of(), null, AccessMode.WRITE, txMetadata), new RunMessage("RETURN 1"), PullAllMessage.INSTANCE));
    // When
    assertThat(connection).satisfies(util.eventuallyReceives(msgSuccess(), msgSuccess(), msgRecord(eqRecord(longEquals(1L))), msgSuccess()));
    // Then
    GraphDatabaseAPI gdb = (GraphDatabaseAPI) server.graphDatabaseService();
    Set<KernelTransactionHandle> txHandles = gdb.getDependencyResolver().resolveDependency(KernelTransactions.class).activeTransactions();
    assertThat(txHandles.size()).isEqualTo(1);
    for (KernelTransactionHandle txHandle : txHandles) {
        assertThat(txHandle.getMetaData()).isEqualTo(txMetadata);
    }
    connection.send(util.chunk(ROLLBACK_MESSAGE));
}
Also used : BeginMessage(org.neo4j.bolt.v3.messaging.request.BeginMessage) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) KernelTransactions(org.neo4j.kernel.impl.api.KernelTransactions) ValueUtils.asMapValue(org.neo4j.kernel.impl.util.ValueUtils.asMapValue) MapValue(org.neo4j.values.virtual.MapValue) RunMessage(org.neo4j.bolt.v3.messaging.request.RunMessage) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

KernelTransactionHandle (org.neo4j.kernel.api.KernelTransactionHandle)20 HashMap (java.util.HashMap)11 Test (org.junit.jupiter.api.Test)9 Optional (java.util.Optional)8 TestKernelTransactionHandle (org.neo4j.kernel.impl.api.TestKernelTransactionHandle)6 ActiveLock (org.neo4j.lock.ActiveLock)5 HashSet (java.util.HashSet)4 Map (java.util.Map)4 DatabaseContext (org.neo4j.dbms.database.DatabaseContext)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 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 KernelTransactionMonitor (org.neo4j.kernel.impl.api.transaction.monitor.KernelTransactionMonitor)3 ZoneId (java.time.ZoneId)2 List (java.util.List)2 Set (java.util.Set)2