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();
}
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);
}
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);
}
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()));
}
}
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()));
}
Aggregations