Search in sources :

Example 6 with InputEventStream

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

the class InvocationTest method shouldInterruptTransaction.

@Test
void shouldInterruptTransaction() throws Exception {
    // given
    TransactionalContext transactionalContext = prepareKernelWithQuerySession();
    when(executionEngine.executeQuery("query", NO_PARAMS, transactionalContext, false)).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);
    invocation.execute(outputEventStream);
    // when
    handle.terminate();
    // then
    verify(internalTransaction).terminate();
}
Also used : InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) Statement(org.neo4j.server.http.cypher.format.api.Statement) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) MemoryPool(org.neo4j.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Example 7 with InputEventStream

use of org.neo4j.server.http.cypher.format.api.InputEventStream 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 8 with InputEventStream

use of org.neo4j.server.http.cypher.format.api.InputEventStream 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 9 with InputEventStream

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

the class AbstractCypherResource method commitNewTransaction.

@POST
@Path("/commit")
public Response commitNewTransaction(InputEventStream inputEventStream, @Context HttpServletRequest request, @Context HttpHeaders headers) {
    try (var memoryTracker = createMemoryTracker()) {
        InputEventStream inputStream = ensureNotNull(inputEventStream);
        Optional<GraphDatabaseAPI> graphDatabaseAPI = httpTransactionManager.getGraphDatabaseAPI(databaseName);
        return graphDatabaseAPI.map(databaseAPI -> {
            if (isDatabaseNotAvailable(databaseAPI)) {
                return createNonAvailableDatabaseResponse(inputStream.getParameters());
            }
            memoryTracker.allocateHeap(Invocation.SHALLOW_SIZE);
            final TransactionFacade transactionFacade = httpTransactionManager.createTransactionFacade(databaseAPI, memoryTracker);
            TransactionHandle transactionHandle = createNewTransactionHandle(transactionFacade, request, headers, memoryTracker, true);
            Invocation invocation = new Invocation(log, transactionHandle, null, memoryPool, inputStream, true);
            OutputEventStreamImpl outputStream = new OutputEventStreamImpl(inputStream.getParameters(), transactionHandle, uriScheme, invocation::execute);
            return Response.ok(outputStream).build();
        }).orElse(createNonExistentDatabaseResponse(inputStream.getParameters()));
    }
}
Also used : PathParam(javax.ws.rs.PathParam) Neo4jError(org.neo4j.server.rest.Neo4jError) Log(org.neo4j.logging.Log) TransactionUriScheme(org.neo4j.server.http.cypher.format.api.TransactionUriScheme) Status(org.neo4j.kernel.api.exceptions.Status) Path(javax.ws.rs.Path) LoginContext(org.neo4j.internal.kernel.api.security.LoginContext) LocalMemoryTracker(org.neo4j.memory.LocalMemoryTracker) Objects.requireNonNullElse(java.util.Objects.requireNonNullElse) HttpServletRequest(javax.servlet.http.HttpServletRequest) Map(java.util.Map) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) MemoryTracker(org.neo4j.memory.MemoryTracker) DELETE(javax.ws.rs.DELETE) HttpHeaderUtils.getTransactionTimeout(org.neo4j.server.web.HttpHeaderUtils.getTransactionTimeout) Collections.emptyMap(java.util.Collections.emptyMap) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) MemoryPool(org.neo4j.memory.MemoryPool) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) AuthorizedRequestWrapper(org.neo4j.server.rest.dbms.AuthorizedRequestWrapper) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) UriInfo(javax.ws.rs.core.UriInfo) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 10 with InputEventStream

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

the class AbstractCypherResource method executeInExistingTransaction.

private Response executeInExistingTransaction(long transactionId, InputEventStream inputEventStream, MemoryTracker memoryTracker, boolean finishWithCommit) {
    InputEventStream inputStream = ensureNotNull(inputEventStream);
    Optional<GraphDatabaseAPI> graphDatabaseAPI = httpTransactionManager.getGraphDatabaseAPI(databaseName);
    return graphDatabaseAPI.map(databaseAPI -> {
        if (isDatabaseNotAvailable(databaseAPI)) {
            return createNonAvailableDatabaseResponse(inputStream.getParameters());
        }
        memoryTracker.allocateHeap(Invocation.SHALLOW_SIZE);
        final TransactionFacade transactionFacade = httpTransactionManager.createTransactionFacade(databaseAPI, memoryTracker);
        TransactionHandle transactionHandle;
        try {
            transactionHandle = transactionFacade.findTransactionHandle(transactionId);
        } catch (TransactionLifecycleException e) {
            return invalidTransaction(e, inputStream.getParameters());
        }
        Invocation invocation = new Invocation(log, transactionHandle, uriScheme.txCommitUri(transactionHandle.getId()), memoryPool, inputStream, finishWithCommit);
        OutputEventStreamImpl outputEventStream = new OutputEventStreamImpl(inputStream.getParameters(), transactionHandle, uriScheme, invocation::execute);
        return Response.ok(outputEventStream).build();
    }).orElse(createNonExistentDatabaseResponse(inputStream.getParameters()));
}
Also used : PathParam(javax.ws.rs.PathParam) Neo4jError(org.neo4j.server.rest.Neo4jError) Log(org.neo4j.logging.Log) TransactionUriScheme(org.neo4j.server.http.cypher.format.api.TransactionUriScheme) Status(org.neo4j.kernel.api.exceptions.Status) Path(javax.ws.rs.Path) LoginContext(org.neo4j.internal.kernel.api.security.LoginContext) LocalMemoryTracker(org.neo4j.memory.LocalMemoryTracker) Objects.requireNonNullElse(java.util.Objects.requireNonNullElse) HttpServletRequest(javax.servlet.http.HttpServletRequest) Map(java.util.Map) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) MemoryTracker(org.neo4j.memory.MemoryTracker) DELETE(javax.ws.rs.DELETE) HttpHeaderUtils.getTransactionTimeout(org.neo4j.server.web.HttpHeaderUtils.getTransactionTimeout) Collections.emptyMap(java.util.Collections.emptyMap) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) MemoryPool(org.neo4j.memory.MemoryPool) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) AuthorizedRequestWrapper(org.neo4j.server.rest.dbms.AuthorizedRequestWrapper) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) UriInfo(javax.ws.rs.core.UriInfo) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream)

Aggregations

InputEventStream (org.neo4j.server.http.cypher.format.api.InputEventStream)25 MemoryPool (org.neo4j.memory.MemoryPool)24 Test (org.junit.jupiter.api.Test)21 Statement (org.neo4j.server.http.cypher.format.api.Statement)18 InOrder (org.mockito.InOrder)17 IOException (java.io.IOException)5 LoginContext (org.neo4j.internal.kernel.api.security.LoginContext)5 ConnectionException (org.neo4j.server.http.cypher.format.api.ConnectionException)4 URI (java.net.URI)3 Collections.emptyMap (java.util.Collections.emptyMap)3 Map (java.util.Map)3 Objects.requireNonNullElse (java.util.Objects.requireNonNullElse)3 Optional (java.util.Optional)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 DELETE (javax.ws.rs.DELETE)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 PathParam (javax.ws.rs.PathParam)3 Context (javax.ws.rs.core.Context)3 HttpHeaders (javax.ws.rs.core.HttpHeaders)3