Search in sources :

Example 11 with KernelTransactionHandle

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

the class KernelTransactionTimeoutMonitorTest method terminateExpiredTransactions.

@Test
void terminateExpiredTransactions() {
    Set<KernelTransactionHandle> transactions = new HashSet<>();
    KernelTransactionImplementation tx1 = prepareTxMock(3, 1, 3);
    KernelTransactionImplementation tx2 = prepareTxMock(4, 1, 8);
    KernelTransactionImplementationHandle handle1 = new KernelTransactionImplementationHandle(tx1, fakeClock);
    KernelTransactionImplementationHandle handle2 = new KernelTransactionImplementationHandle(tx2, fakeClock);
    transactions.add(handle1);
    transactions.add(handle2);
    when(kernelTransactions.activeTransactions()).thenReturn(transactions);
    KernelTransactionMonitor transactionMonitor = buildTransactionMonitor();
    fakeClock.forward(3, TimeUnit.MILLISECONDS);
    transactionMonitor.run();
    verify(tx1, never()).markForTermination(Status.Transaction.TransactionTimedOut);
    verify(tx2, never()).markForTermination(Status.Transaction.TransactionTimedOut);
    assertThat(logProvider).doesNotContainMessage("timeout");
    fakeClock.forward(2, TimeUnit.MILLISECONDS);
    transactionMonitor.run();
    verify(tx1).markForTermination(EXPECTED_REUSE_COUNT, Status.Transaction.TransactionTimedOut);
    verify(tx2, never()).markForTermination(Status.Transaction.TransactionTimedOut);
    assertThat(logProvider).containsMessages("timeout");
    logProvider.clear();
    fakeClock.forward(10, TimeUnit.MILLISECONDS);
    transactionMonitor.run();
    verify(tx2).markForTermination(EXPECTED_REUSE_COUNT, Status.Transaction.TransactionTimedOut);
    assertThat(logProvider).containsMessages("timeout");
}
Also used : KernelTransactionMonitor(org.neo4j.kernel.impl.api.transaction.monitor.KernelTransactionMonitor) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 12 with KernelTransactionHandle

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

the class KernelTransactionTimeoutMonitorTest method skipTransactionWithoutTimeout.

@Test
void skipTransactionWithoutTimeout() {
    Set<KernelTransactionHandle> transactions = new HashSet<>();
    KernelTransactionImplementation tx1 = prepareTxMock(7, 3, 0);
    KernelTransactionImplementation tx2 = prepareTxMock(8, 4, 0);
    KernelTransactionImplementationHandle handle1 = new KernelTransactionImplementationHandle(tx1, fakeClock);
    KernelTransactionImplementationHandle handle2 = new KernelTransactionImplementationHandle(tx2, fakeClock);
    transactions.add(handle1);
    transactions.add(handle2);
    when(kernelTransactions.activeTransactions()).thenReturn(transactions);
    KernelTransactionMonitor transactionMonitor = buildTransactionMonitor();
    fakeClock.forward(300, TimeUnit.MILLISECONDS);
    transactionMonitor.run();
    verify(tx1, never()).markForTermination(Status.Transaction.TransactionTimedOut);
    verify(tx2, never()).markForTermination(Status.Transaction.TransactionTimedOut);
    assertThat(logProvider).doesNotContainMessage("timeout");
}
Also used : KernelTransactionMonitor(org.neo4j.kernel.impl.api.transaction.monitor.KernelTransactionMonitor) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 13 with KernelTransactionHandle

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

the class TransactionDependenciesResolverTest method blockingChainDescriptionForChainedBlockedTransaction.

@Test
void blockingChainDescriptionForChainedBlockedTransaction() {
    HashMap<KernelTransactionHandle, Optional<QuerySnapshot>> map = new HashMap<>();
    TestKernelTransactionHandle handle1 = new TestKernelTransactionHandleWithLocks(new StubKernelTransaction(), 4, singletonList(new ActiveLock(ResourceTypes.NODE, EXCLUSIVE, 4, 1)));
    TestKernelTransactionHandle handle2 = new TestKernelTransactionHandleWithLocks(new StubKernelTransaction(), 5, singletonList(new ActiveLock(ResourceTypes.NODE, SHARED, 5, 2)));
    TestKernelTransactionHandle handle3 = new TestKernelTransactionHandleWithLocks(new StubKernelTransaction(), 6);
    map.put(handle1, Optional.of(createQuerySnapshot(1)));
    map.put(handle2, Optional.of(createQuerySnapshotWaitingForLock(2, EXCLUSIVE, ResourceTypes.NODE, 5, 1)));
    map.put(handle3, Optional.of(createQuerySnapshotWaitingForLock(3, EXCLUSIVE, ResourceTypes.NODE, 6, 2)));
    TransactionDependenciesResolver resolver = new TransactionDependenciesResolver(map);
    assertThat(resolver.describeBlockingTransactions(handle1)).isEmpty();
    assertEquals("[transaction-4]", resolver.describeBlockingTransactions(handle2));
    assertEquals("[transaction-4, transaction-5]", resolver.describeBlockingTransactions(handle3));
}
Also used : ActiveLock(org.neo4j.lock.ActiveLock) Optional(java.util.Optional) HashMap(java.util.HashMap) TestKernelTransactionHandle(org.neo4j.kernel.impl.api.TestKernelTransactionHandle) TestKernelTransactionHandle(org.neo4j.kernel.impl.api.TestKernelTransactionHandle) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle) Test(org.junit.jupiter.api.Test)

Example 14 with KernelTransactionHandle

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

the class TransactionDependenciesResolverTest method detectBlockedTransactionsBySharedLock.

@Test
void detectBlockedTransactionsBySharedLock() {
    HashMap<KernelTransactionHandle, Optional<QuerySnapshot>> map = new HashMap<>();
    TestKernelTransactionHandle handle1 = new TestKernelTransactionHandleWithLocks(new StubKernelTransaction(), 0, singletonList(new ActiveLock(ResourceTypes.NODE, SHARED, 1, 1)));
    TestKernelTransactionHandle handle2 = new TestKernelTransactionHandleWithLocks(new StubKernelTransaction());
    map.put(handle1, Optional.of(createQuerySnapshot(1)));
    map.put(handle2, Optional.of(createQuerySnapshotWaitingForLock(2, EXCLUSIVE, ResourceTypes.NODE, 1, 1)));
    TransactionDependenciesResolver resolver = new TransactionDependenciesResolver(map);
    assertFalse(resolver.isBlocked(handle1));
    assertTrue(resolver.isBlocked(handle2));
}
Also used : ActiveLock(org.neo4j.lock.ActiveLock) Optional(java.util.Optional) HashMap(java.util.HashMap) TestKernelTransactionHandle(org.neo4j.kernel.impl.api.TestKernelTransactionHandle) TestKernelTransactionHandle(org.neo4j.kernel.impl.api.TestKernelTransactionHandle) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle) Test(org.junit.jupiter.api.Test)

Example 15 with KernelTransactionHandle

use of org.neo4j.kernel.api.KernelTransactionHandle 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();
}
Also used : HashMap(java.util.HashMap) DatabaseIdRepository(org.neo4j.kernel.database.DatabaseIdRepository) ArrayList(java.util.ArrayList) FabricTransaction(org.neo4j.fabric.transaction.FabricTransaction) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle) DatabaseContext(org.neo4j.dbms.database.DatabaseContext) ExecutingQuery(org.neo4j.kernel.api.query.ExecutingQuery) 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

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