Search in sources :

Example 1 with QueryRegistryOperations

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

the class Neo4jTransactionalContextTest method neverStopsExecutingQueryDuringCommitAndRestartTx.

@SuppressWarnings("ConstantConditions")
@Test
public void neverStopsExecutingQueryDuringCommitAndRestartTx() {
    // Given
    KernelTransaction initialKTX = mock(KernelTransaction.class);
    InternalTransaction initialTransaction = mock(InternalTransaction.class, new ReturnsDeepStubs());
    KernelTransaction.Type transactionType = KernelTransaction.Type.implicit;
    SecurityContext securityContext = SecurityContext.AUTH_DISABLED;
    when(initialTransaction.transactionType()).thenReturn(transactionType);
    when(initialTransaction.securityContext()).thenReturn(securityContext);
    QueryRegistryOperations initialQueryRegistry = mock(QueryRegistryOperations.class);
    ExecutingQuery executingQuery = mock(ExecutingQuery.class);
    PropertyContainerLocker locker = null;
    ThreadToStatementContextBridge txBridge = mock(ThreadToStatementContextBridge.class);
    KernelTransaction secondKTX = mock(KernelTransaction.class);
    InternalTransaction secondTransaction = mock(InternalTransaction.class);
    Statement secondStatement = mock(Statement.class);
    QueryRegistryOperations secondQueryRegistry = mock(QueryRegistryOperations.class);
    when(executingQuery.queryText()).thenReturn("X");
    when(executingQuery.queryParameters()).thenReturn(Collections.emptyMap());
    when(initialStatement.queryRegistration()).thenReturn(initialQueryRegistry);
    when(queryService.beginTransaction(transactionType, securityContext)).thenReturn(secondTransaction);
    when(txBridge.getKernelTransactionBoundToThisThread(true)).thenReturn(initialKTX, secondKTX);
    when(txBridge.get()).thenReturn(secondStatement);
    when(secondStatement.queryRegistration()).thenReturn(secondQueryRegistry);
    Neo4jTransactionalContext context = new Neo4jTransactionalContext(queryService, null, guard, txBridge, locker, initialTransaction, initialStatement, executingQuery);
    // When
    context.commitAndRestartTx();
    // Then
    Object[] mocks = { txBridge, initialTransaction, initialQueryRegistry, initialKTX, secondQueryRegistry, secondKTX };
    InOrder order = Mockito.inOrder(mocks);
    // (0) Constructor
    order.verify(initialTransaction).transactionType();
    order.verify(initialTransaction).securityContext();
    // (1) Unbind old
    order.verify(txBridge).getKernelTransactionBoundToThisThread(true);
    order.verify(txBridge).unbindTransactionFromCurrentThread();
    // (2) Register and unbind new
    order.verify(txBridge).get();
    order.verify(secondQueryRegistry).registerExecutingQuery(executingQuery);
    order.verify(txBridge).getKernelTransactionBoundToThisThread(true);
    order.verify(txBridge).unbindTransactionFromCurrentThread();
    // (3) Rebind, unregister, and close old
    order.verify(txBridge).bindTransactionToCurrentThread(initialKTX);
    order.verify(initialQueryRegistry).unregisterExecutingQuery(executingQuery);
    order.verify(initialTransaction).success();
    order.verify(initialTransaction).close();
    order.verify(txBridge).unbindTransactionFromCurrentThread();
    // (4) Rebind new
    order.verify(txBridge).bindTransactionToCurrentThread(secondKTX);
    verifyNoMoreInteractions(mocks);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InOrder(org.mockito.InOrder) Statement(org.neo4j.kernel.api.Statement) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) QueryRegistryOperations(org.neo4j.kernel.api.QueryRegistryOperations) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) ExecutingQuery(org.neo4j.kernel.api.query.ExecutingQuery) PropertyContainerLocker(org.neo4j.kernel.impl.coreapi.PropertyContainerLocker) SecurityContext(org.neo4j.kernel.api.security.SecurityContext) ReturnsDeepStubs(org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs) Test(org.junit.Test)

Example 2 with QueryRegistryOperations

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

the class Neo4jTransactionalContextTest method rollsBackNewlyCreatedTransactionIfTerminationDetectedOnCloseDuringPeriodicCommit.

@SuppressWarnings("ConstantConditions")
@Test
public void rollsBackNewlyCreatedTransactionIfTerminationDetectedOnCloseDuringPeriodicCommit() {
    // Given
    InternalTransaction initialTransaction = mock(InternalTransaction.class, new ReturnsDeepStubs());
    KernelTransaction.Type transactionType = KernelTransaction.Type.implicit;
    SecurityContext securityContext = SecurityContext.AUTH_DISABLED;
    when(initialTransaction.transactionType()).thenReturn(transactionType);
    when(initialTransaction.securityContext()).thenReturn(securityContext);
    GraphDatabaseQueryService queryService = mock(GraphDatabaseQueryService.class);
    KernelTransaction initialKTX = mock(KernelTransaction.class);
    Statement initialStatement = mock(Statement.class);
    QueryRegistryOperations initialQueryRegistry = mock(QueryRegistryOperations.class);
    ExecutingQuery executingQuery = mock(ExecutingQuery.class);
    PropertyContainerLocker locker = new PropertyContainerLocker();
    ThreadToStatementContextBridge txBridge = mock(ThreadToStatementContextBridge.class);
    KernelTransaction secondKTX = mock(KernelTransaction.class);
    InternalTransaction secondTransaction = mock(InternalTransaction.class);
    Statement secondStatement = mock(Statement.class);
    QueryRegistryOperations secondQueryRegistry = mock(QueryRegistryOperations.class);
    when(executingQuery.queryText()).thenReturn("X");
    when(executingQuery.queryParameters()).thenReturn(Collections.emptyMap());
    Mockito.doThrow(RuntimeException.class).when(initialTransaction).close();
    when(initialStatement.queryRegistration()).thenReturn(initialQueryRegistry);
    when(queryService.beginTransaction(transactionType, securityContext)).thenReturn(secondTransaction);
    when(txBridge.getKernelTransactionBoundToThisThread(true)).thenReturn(initialKTX, secondKTX);
    when(txBridge.get()).thenReturn(secondStatement);
    when(secondStatement.queryRegistration()).thenReturn(secondQueryRegistry);
    Neo4jTransactionalContext context = new Neo4jTransactionalContext(queryService, null, guard, txBridge, locker, initialTransaction, initialStatement, executingQuery);
    // When
    try {
        context.commitAndRestartTx();
        throw new AssertionError("Expected RuntimeException to be thrown");
    } catch (RuntimeException e) {
        // Then
        Object[] mocks = { txBridge, initialTransaction, initialQueryRegistry, initialKTX, secondQueryRegistry, secondKTX, secondTransaction };
        InOrder order = Mockito.inOrder(mocks);
        // (0) Constructor
        order.verify(initialTransaction).transactionType();
        order.verify(initialTransaction).securityContext();
        // (1) Unbind old
        order.verify(txBridge).getKernelTransactionBoundToThisThread(true);
        order.verify(txBridge).unbindTransactionFromCurrentThread();
        // (2) Register and unbind new
        order.verify(txBridge).get();
        order.verify(secondQueryRegistry).registerExecutingQuery(executingQuery);
        order.verify(txBridge).getKernelTransactionBoundToThisThread(true);
        order.verify(txBridge).unbindTransactionFromCurrentThread();
        // (3) Rebind, unregister, and close old
        order.verify(txBridge).bindTransactionToCurrentThread(initialKTX);
        order.verify(initialQueryRegistry).unregisterExecutingQuery(executingQuery);
        order.verify(initialTransaction).success();
        order.verify(initialTransaction).close();
        order.verify(txBridge).bindTransactionToCurrentThread(secondKTX);
        order.verify(secondTransaction).failure();
        order.verify(secondTransaction).close();
        order.verify(txBridge).unbindTransactionFromCurrentThread();
        verifyNoMoreInteractions(mocks);
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InOrder(org.mockito.InOrder) GraphDatabaseQueryService(org.neo4j.kernel.GraphDatabaseQueryService) Statement(org.neo4j.kernel.api.Statement) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) QueryRegistryOperations(org.neo4j.kernel.api.QueryRegistryOperations) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) ExecutingQuery(org.neo4j.kernel.api.query.ExecutingQuery) PropertyContainerLocker(org.neo4j.kernel.impl.coreapi.PropertyContainerLocker) SecurityContext(org.neo4j.kernel.api.security.SecurityContext) ReturnsDeepStubs(org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs) Test(org.junit.Test)

Example 3 with QueryRegistryOperations

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

the class Neo4jTransactionalContext method commitAndRestartTx.

@Override
public void commitAndRestartTx() {
    /*
        * This method is use by the Cypher runtime to cater for PERIODIC COMMIT, which allows a single query to
        * periodically, after x number of rows, to commit a transaction and spawn a new one.
        *
        * To still keep track of the running stream after switching transactions, we need to open the new transaction
        * before closing the old one. This way, a query will not disappear and appear when switching transactions.
        *
        * Since our transactions are thread bound, we must first unbind the old transaction from the thread before
        * creating a new one. And then we need to do that thread switching again to close the old transaction.
        */
    // (1) Unbind current transaction
    QueryRegistryOperations oldQueryRegistryOperations = statement.queryRegistration();
    InternalTransaction oldTransaction = transaction;
    KernelTransaction oldKernelTx = txBridge.getKernelTransactionBoundToThisThread(true);
    txBridge.unbindTransactionFromCurrentThread();
    // (2) Create, bind, register, and unbind new transaction
    transaction = graph.beginTransaction(transactionType, securityContext);
    statement = txBridge.get();
    statement.queryRegistration().registerExecutingQuery(executingQuery);
    KernelTransaction kernelTx = txBridge.getKernelTransactionBoundToThisThread(true);
    txBridge.unbindTransactionFromCurrentThread();
    // (3) Rebind old transaction just to commit and close it (and unregister as a side effect of that)
    txBridge.bindTransactionToCurrentThread(oldKernelTx);
    oldQueryRegistryOperations.unregisterExecutingQuery(executingQuery);
    try {
        oldTransaction.success();
        oldTransaction.close();
    } catch (Throwable t) {
        // Corner case: The old transaction might have been terminated by the user. Now we also need to
        // terminate the new transaction.
        txBridge.bindTransactionToCurrentThread(kernelTx);
        transaction.failure();
        transaction.close();
        txBridge.unbindTransactionFromCurrentThread();
        throw t;
    }
    // (4) Unbind the now closed old transaction and rebind the new transaction for continued execution
    txBridge.unbindTransactionFromCurrentThread();
    txBridge.bindTransactionToCurrentThread(kernelTx);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) QueryRegistryOperations(org.neo4j.kernel.api.QueryRegistryOperations) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction)

Example 4 with QueryRegistryOperations

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

the class Neo4jTransactionalContextTest method setUpMocks.

private void setUpMocks() {
    queryService = mock(GraphDatabaseQueryService.class);
    DependencyResolver resolver = mock(DependencyResolver.class);
    ThreadToStatementContextBridge txBridge = mock(ThreadToStatementContextBridge.class);
    guard = mock(Guard.class);
    initialStatement = mock(KernelStatement.class);
    QueryRegistryOperations queryRegistryOperations = mock(QueryRegistryOperations.class);
    when(initialStatement.queryRegistration()).thenReturn(queryRegistryOperations);
    when(queryService.getDependencyResolver()).thenReturn(resolver);
    when(resolver.resolveDependency(ThreadToStatementContextBridge.class)).thenReturn(txBridge);
    when(resolver.resolveDependency(Guard.class)).thenReturn(guard);
}
Also used : KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) GraphDatabaseQueryService(org.neo4j.kernel.GraphDatabaseQueryService) QueryRegistryOperations(org.neo4j.kernel.api.QueryRegistryOperations) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) Guard(org.neo4j.kernel.guard.Guard) DependencyResolver(org.neo4j.graphdb.DependencyResolver)

Example 5 with QueryRegistryOperations

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

the class CypherExecutorTest method setUpMocks.

private void setUpMocks() {
    database = mock(Database.class);
    databaseFacade = mock(GraphDatabaseFacade.class);
    resolver = mock(DependencyResolver.class);
    executionEngine = mock(ExecutionEngine.class);
    statementBridge = mock(ThreadToStatementContextBridge.class);
    databaseQueryService = mock(GraphDatabaseQueryService.class);
    kernelTransaction = mock(KernelTransaction.class);
    statement = mock(Statement.class);
    request = mock(HttpServletRequest.class);
    InternalTransaction transaction = new TopLevelTransaction(kernelTransaction, () -> statement);
    SecurityContext securityContext = AUTH_DISABLED;
    KernelTransaction.Type type = KernelTransaction.Type.implicit;
    QueryRegistryOperations registryOperations = mock(QueryRegistryOperations.class);
    when(statement.queryRegistration()).thenReturn(registryOperations);
    when(statementBridge.get()).thenReturn(statement);
    when(kernelTransaction.securityContext()).thenReturn(securityContext);
    when(kernelTransaction.transactionType()).thenReturn(type);
    when(database.getGraph()).thenReturn(databaseFacade);
    when(databaseFacade.getDependencyResolver()).thenReturn(resolver);
    when(resolver.resolveDependency(QueryExecutionEngine.class)).thenReturn(executionEngine);
    when(resolver.resolveDependency(ThreadToStatementContextBridge.class)).thenReturn(statementBridge);
    when(resolver.resolveDependency(GraphDatabaseQueryService.class)).thenReturn(databaseQueryService);
    when(databaseQueryService.beginTransaction(type, securityContext)).thenReturn(transaction);
    when(databaseQueryService.beginTransaction(type, securityContext, CUSTOM_TRANSACTION_TIMEOUT, TimeUnit.MILLISECONDS)).thenReturn(transaction);
    when(databaseQueryService.getDependencyResolver()).thenReturn(resolver);
    when(request.getScheme()).thenReturn("http");
    when(request.getRemoteAddr()).thenReturn("127.0.0.1");
    when(request.getRemotePort()).thenReturn(5678);
    when(request.getServerName()).thenReturn("127.0.0.1");
    when(request.getServerPort()).thenReturn(7474);
    when(request.getRequestURI()).thenReturn("/");
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) GraphDatabaseQueryService(org.neo4j.kernel.GraphDatabaseQueryService) Statement(org.neo4j.kernel.api.Statement) QueryRegistryOperations(org.neo4j.kernel.api.QueryRegistryOperations) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) TopLevelTransaction(org.neo4j.kernel.impl.coreapi.TopLevelTransaction) DependencyResolver(org.neo4j.graphdb.DependencyResolver) HttpServletRequest(javax.servlet.http.HttpServletRequest) QueryExecutionEngine(org.neo4j.kernel.impl.query.QueryExecutionEngine) ExecutionEngine(org.neo4j.cypher.internal.javacompat.ExecutionEngine) SecurityContext(org.neo4j.kernel.api.security.SecurityContext) GraphDatabaseFacade(org.neo4j.kernel.impl.factory.GraphDatabaseFacade)

Aggregations

QueryRegistryOperations (org.neo4j.kernel.api.QueryRegistryOperations)5 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)4 ThreadToStatementContextBridge (org.neo4j.kernel.impl.core.ThreadToStatementContextBridge)4 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)4 GraphDatabaseQueryService (org.neo4j.kernel.GraphDatabaseQueryService)3 Statement (org.neo4j.kernel.api.Statement)3 SecurityContext (org.neo4j.kernel.api.security.SecurityContext)3 KernelStatement (org.neo4j.kernel.impl.api.KernelStatement)3 Test (org.junit.Test)2 InOrder (org.mockito.InOrder)2 ReturnsDeepStubs (org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs)2 DependencyResolver (org.neo4j.graphdb.DependencyResolver)2 ExecutingQuery (org.neo4j.kernel.api.query.ExecutingQuery)2 PropertyContainerLocker (org.neo4j.kernel.impl.coreapi.PropertyContainerLocker)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ExecutionEngine (org.neo4j.cypher.internal.javacompat.ExecutionEngine)1 Guard (org.neo4j.kernel.guard.Guard)1 TopLevelTransaction (org.neo4j.kernel.impl.coreapi.TopLevelTransaction)1 GraphDatabaseFacade (org.neo4j.kernel.impl.factory.GraphDatabaseFacade)1 QueryExecutionEngine (org.neo4j.kernel.impl.query.QueryExecutionEngine)1