Search in sources :

Example 6 with Statement

use of org.neo4j.server.http.cypher.format.api.Statement in project neo4j by neo4j.

the class InvocationTest method shouldHandleConnectionErrorWhenWritingOutputInImplicitTransaction.

@Test
void shouldHandleConnectionErrorWhenWritingOutputInImplicitTransaction() {
    // given
    when(internalTransaction.execute("query", emptyMap())).thenReturn(executionResult);
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    TransactionHandle handle = getTransactionHandle(executionEngine, registry);
    InputEventStream inputEventStream = mock(InputEventStream.class);
    Statement statement = new Statement("query", map());
    when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
    mockDefaultResult();
    Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, true);
    doThrow(new ConnectionException("Connection error", new IOException("Broken pipe"))).when(outputEventStream).writeStatementEnd(any(), any(), any(), any());
    // when
    var e = assertThrows(ConnectionException.class, () -> invocation.execute(outputEventStream));
    assertEquals("Connection error", e.getMessage());
    // then
    verify(internalTransaction).rollback();
    verify(registry).forget(1337L);
    InOrder outputOrder = inOrder(outputEventStream);
    outputOrder.verify(outputEventStream).writeStatementStart(statement, List.of("c1", "c2", "c3"));
    verifyDefaultResultRows(outputOrder);
    outputOrder.verify(outputEventStream).writeStatementEnd(queryExecutionType, queryStatistics, executionPlanDescription, notifications);
    verifyNoMoreInteractions(outputEventStream);
}
Also used : InOrder(org.mockito.InOrder) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) Statement(org.neo4j.server.http.cypher.format.api.Statement) IOException(java.io.IOException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException) MemoryPool(org.neo4j.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Example 7 with Statement

use of org.neo4j.server.http.cypher.format.api.Statement in project neo4j by neo4j.

the class InvocationTest method shouldHandleRollbackError.

@Test
void shouldHandleRollbackError() {
    // given
    when(internalTransaction.execute("query", emptyMap())).thenThrow(new RuntimeException("BOO"));
    doThrow(new IllegalStateException("Something went wrong")).when(internalTransaction).rollback();
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    TransactionHandle handle = getTransactionHandle(executionEngine, registry);
    InputEventStream inputEventStream = mock(InputEventStream.class);
    Statement statement = new Statement("query", map());
    when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
    Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, true);
    // when
    invocation.execute(outputEventStream);
    // then
    verify(internalTransaction).execute("query", emptyMap());
    verify(log).error(eq("Failed to roll back transaction."), any(IllegalStateException.class));
    verify(registry).forget(1337L);
    InOrder outputOrder = inOrder(outputEventStream);
    outputOrder.verify(outputEventStream).writeFailure(Status.Statement.ExecutionFailed, "BOO");
    outputOrder.verify(outputEventStream).writeFailure(Status.Transaction.TransactionRollbackFailed, "Something went wrong");
    outputOrder.verify(outputEventStream).writeTransactionInfo(TransactionNotificationState.UNKNOWN, uriScheme.txCommitUri(1337L), -1);
    verifyNoMoreInteractions(outputEventStream);
}
Also used : InOrder(org.mockito.InOrder) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) Statement(org.neo4j.server.http.cypher.format.api.Statement) MemoryPool(org.neo4j.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Example 8 with Statement

use of org.neo4j.server.http.cypher.format.api.Statement in project neo4j by neo4j.

the class Invocation method executeStatements.

private void executeStatements() {
    try {
        while (outputError == null) {
            memoryPool.reserveHeap(Statement.SHALLOW_SIZE);
            try {
                Statement statement = readStatement();
                if (statement == null) {
                    return;
                }
                executeStatement(statement);
            } finally {
                memoryPool.releaseHeap(Statement.SHALLOW_SIZE);
            }
        }
    } catch (InputFormatException e) {
        handleNeo4jError(Status.Request.InvalidFormat, e);
    } catch (KernelException | Neo4jException | AuthorizationViolationException | WriteOperationsNotAllowedException e) {
        handleNeo4jError(e.status(), e);
    } catch (DeadlockDetectedException e) {
        handleNeo4jError(Status.Transaction.DeadlockDetected, e);
    } catch (Exception e) {
        Throwable cause = e.getCause();
        if (cause instanceof Status.HasStatus) {
            handleNeo4jError(((Status.HasStatus) cause).status(), cause);
        } else {
            handleNeo4jError(Status.Statement.ExecutionFailed, e);
        }
    }
}
Also used : Status(org.neo4j.kernel.api.exceptions.Status) Statement(org.neo4j.server.http.cypher.format.api.Statement) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) Neo4jException(org.neo4j.exceptions.Neo4jException) InputFormatException(org.neo4j.server.http.cypher.format.api.InputFormatException) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) Neo4jException(org.neo4j.exceptions.Neo4jException) WriteOperationsNotAllowedException(org.neo4j.graphdb.WriteOperationsNotAllowedException) KernelException(org.neo4j.exceptions.KernelException) InputFormatException(org.neo4j.server.http.cypher.format.api.InputFormatException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException) AuthorizationViolationException(org.neo4j.graphdb.security.AuthorizationViolationException) InvalidSemanticsException(org.neo4j.exceptions.InvalidSemanticsException) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) OutputFormatException(org.neo4j.server.http.cypher.format.api.OutputFormatException) WriteOperationsNotAllowedException(org.neo4j.graphdb.WriteOperationsNotAllowedException) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) KernelException(org.neo4j.exceptions.KernelException) AuthorizationViolationException(org.neo4j.graphdb.security.AuthorizationViolationException)

Example 9 with Statement

use of org.neo4j.server.http.cypher.format.api.Statement in project neo4j by neo4j.

the class JsonMessageBodyReader method readFrom.

@Override
public InputEventStream readFrom(Class<InputEventStream> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws WebApplicationException {
    Map<Statement, InputStatement> inputStatements = new HashMap<>();
    Map<String, Object> parameters = new HashMap<>();
    parameters.put(STATEMENTS_KEY, inputStatements);
    StatementDeserializer statementDeserializer = new StatementDeserializer(this.jsonFactory, entityStream);
    return new InputEventStream(parameters, () -> {
        InputStatement inputStatement = statementDeserializer.read();
        if (inputStatement == null) {
            return null;
        }
        Statement statement = new Statement(inputStatement.statement(), inputStatement.parameters());
        inputStatements.put(statement, inputStatement);
        return statement;
    });
}
Also used : HashMap(java.util.HashMap) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) Statement(org.neo4j.server.http.cypher.format.api.Statement)

Example 10 with Statement

use of org.neo4j.server.http.cypher.format.api.Statement in project neo4j by neo4j.

the class InvocationTest method shouldResumeTransactionWhenExecutingStatementsOnSecondRequest.

@Test
void shouldResumeTransactionWhenExecutingStatementsOnSecondRequest() {
    // given
    when(internalTransaction.execute("query", emptyMap())).thenReturn(executionResult);
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    TransactionHandle handle = getTransactionHandle(executionEngine, registry);
    InputEventStream inputEventStream = mock(InputEventStream.class);
    Statement statement = new Statement("query", map());
    when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
    mockDefaultResult();
    Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, false);
    invocation.execute(outputEventStream);
    reset(transactionContext, registry, internalTransaction, outputEventStream);
    when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
    when(internalTransaction.execute("query", emptyMap())).thenReturn(executionResult);
    // when
    invocation.execute(outputEventStream);
    // then
    InOrder order = inOrder(transactionContext, registry, internalTransaction);
    order.verify(internalTransaction).execute("query", emptyMap());
    order.verify(registry).release(1337L, handle);
    InOrder outputOrder = inOrder(outputEventStream);
    outputOrder.verify(outputEventStream).writeStatementStart(statement, List.of("c1", "c2", "c3"));
    verifyDefaultResultRows(outputOrder);
    outputOrder.verify(outputEventStream).writeStatementEnd(queryExecutionType, queryStatistics, executionPlanDescription, notifications);
    outputOrder.verify(outputEventStream).writeTransactionInfo(TransactionNotificationState.OPEN, uriScheme.txCommitUri(1337L), 0);
    verifyNoMoreInteractions(outputEventStream);
}
Also used : InOrder(org.mockito.InOrder) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) Statement(org.neo4j.server.http.cypher.format.api.Statement) MemoryPool(org.neo4j.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Aggregations

Statement (org.neo4j.server.http.cypher.format.api.Statement)21 Test (org.junit.jupiter.api.Test)19 InputEventStream (org.neo4j.server.http.cypher.format.api.InputEventStream)18 MemoryPool (org.neo4j.memory.MemoryPool)17 InOrder (org.mockito.InOrder)16 ConnectionException (org.neo4j.server.http.cypher.format.api.ConnectionException)4 IOException (java.io.IOException)3 AuthorizationViolationException (org.neo4j.graphdb.security.AuthorizationViolationException)2 LoginContext (org.neo4j.internal.kernel.api.security.LoginContext)2 DeadlockDetectedException (org.neo4j.kernel.DeadlockDetectedException)2 Log (org.neo4j.logging.Log)2 HashMap (java.util.HashMap)1 TimeUnit (java.util.concurrent.TimeUnit)1 InvalidSemanticsException (org.neo4j.exceptions.InvalidSemanticsException)1 KernelException (org.neo4j.exceptions.KernelException)1 Neo4jException (org.neo4j.exceptions.Neo4jException)1 SyntaxException (org.neo4j.exceptions.SyntaxException)1 QueryExecutionType (org.neo4j.graphdb.QueryExecutionType)1 WriteOperationsNotAllowedException (org.neo4j.graphdb.WriteOperationsNotAllowedException)1 ClientConnectionInfo (org.neo4j.internal.kernel.api.connectioninfo.ClientConnectionInfo)1