use of org.neo4j.kernel.impl.coreapi.InternalTransaction in project neo4j by neo4j.
the class Neo4jTransactionalContextTest method rollsBackNewlyCreatedTransactionIfTerminationDetectedOnCloseDuringPeriodicCommit.
@SuppressWarnings("ConstantConditions")
@Test
void rollsBackNewlyCreatedTransactionIfTerminationDetectedOnCloseDuringPeriodicCommit() throws TransactionFailureException {
// Given
InternalTransaction userTransaction = mock(InternalTransaction.class, new ReturnsDeepStubs());
KernelTransaction.Type transactionType = KernelTransaction.Type.IMPLICIT;
SecurityContext securityContext = SecurityContext.AUTH_DISABLED;
ClientConnectionInfo connectionInfo = ClientConnectionInfo.EMBEDDED_CONNECTION;
when(userTransaction.transactionType()).thenReturn(transactionType);
when(userTransaction.clientInfo()).thenReturn(connectionInfo);
when(userTransaction.securityContext()).thenReturn(securityContext);
when(userTransaction.terminationReason()).thenReturn(Optional.empty());
GraphDatabaseQueryService queryService = mock(GraphDatabaseQueryService.class);
KernelStatement initialStatement = mock(KernelStatement.class);
KernelTransaction initialKTX = mockTransaction(initialStatement);
QueryRegistry initialQueryRegistry = mock(QueryRegistry.class);
ExecutingQuery executingQuery = mock(ExecutingQuery.class);
KernelStatement secondStatement = mock(KernelStatement.class);
KernelTransaction secondKTX = mockTransaction(secondStatement);
QueryRegistry secondQueryRegistry = mock(QueryRegistry.class);
when(transactionFactory.beginKernelTransaction(transactionType, securityContext, connectionInfo)).thenReturn(secondKTX);
when(executingQuery.databaseId()).thenReturn(Optional.of(namedDatabaseId));
Mockito.doThrow(RuntimeException.class).when(initialKTX).commit();
when(initialStatement.queryRegistration()).thenReturn(initialQueryRegistry);
when(userTransaction.kernelTransaction()).thenReturn(initialKTX, initialKTX, secondKTX);
when(secondStatement.queryRegistration()).thenReturn(secondQueryRegistry);
Neo4jTransactionalContext context = new Neo4jTransactionalContext(queryService, userTransaction, initialStatement, executingQuery, transactionFactory);
// When
assertThrows(RuntimeException.class, context::commitAndRestartTx);
Object[] mocks = { userTransaction, initialQueryRegistry, initialKTX, secondQueryRegistry, secondKTX };
InOrder order = Mockito.inOrder(mocks);
// (0) Constructor
order.verify(userTransaction).transactionType();
order.verify(userTransaction).securityContext();
order.verify(userTransaction).clientInfo();
// not terminated check
order.verify(userTransaction).terminationReason();
// (1) Collect statistics
order.verify(initialKTX).executionStatistics();
// (3) Register new
order.verify(secondKTX).acquireStatement();
order.verify(secondQueryRegistry).registerExecutingQuery(executingQuery);
// (4) Unregister, and close old
order.verify(initialQueryRegistry).unregisterExecutingQuery(executingQuery);
order.verify(userTransaction).rollback();
}
use of org.neo4j.kernel.impl.coreapi.InternalTransaction in project neo4j by neo4j.
the class Neo4jTransactionalContextTest method neverStopsExecutingQueryDuringCommitAndRestartTx.
@Test
void neverStopsExecutingQueryDuringCommitAndRestartTx() throws TransactionFailureException {
// Given
KernelTransaction initialKTX = mockTransaction(statement);
InternalTransaction userTransaction = mock(InternalTransaction.class, new ReturnsDeepStubs());
KernelTransaction.Type transactionType = KernelTransaction.Type.IMPLICIT;
SecurityContext securityContext = SecurityContext.AUTH_DISABLED;
ClientConnectionInfo connectionInfo = ClientConnectionInfo.EMBEDDED_CONNECTION;
when(userTransaction.transactionType()).thenReturn(transactionType);
when(userTransaction.securityContext()).thenReturn(securityContext);
when(userTransaction.terminationReason()).thenReturn(Optional.empty());
when(userTransaction.clientInfo()).thenReturn(connectionInfo);
QueryRegistry initialQueryRegistry = mock(QueryRegistry.class);
ExecutingQuery executingQuery = mock(ExecutingQuery.class);
KernelStatement secondStatement = mock(KernelStatement.class);
KernelTransaction secondKTX = mockTransaction(secondStatement);
QueryRegistry secondQueryRegistry = mock(QueryRegistry.class);
when(transactionFactory.beginKernelTransaction(transactionType, securityContext, connectionInfo)).thenReturn(secondKTX);
when(executingQuery.databaseId()).thenReturn(Optional.of(namedDatabaseId));
when(statement.queryRegistration()).thenReturn(initialQueryRegistry);
when(userTransaction.kernelTransaction()).thenReturn(initialKTX, initialKTX, secondKTX);
when(secondStatement.queryRegistration()).thenReturn(secondQueryRegistry);
Neo4jTransactionalContext context = new Neo4jTransactionalContext(queryService, userTransaction, statement, executingQuery, transactionFactory);
// When
context.commitAndRestartTx();
// Then
Object[] mocks = { userTransaction, initialKTX, initialQueryRegistry, secondQueryRegistry, secondKTX };
InOrder order = Mockito.inOrder(mocks);
// (0) Constructor
order.verify(userTransaction).transactionType();
order.verify(userTransaction).securityContext();
order.verify(userTransaction).clientInfo();
// not terminated check
order.verify(userTransaction).terminationReason();
// (1) Collect stats
order.verify(initialKTX).executionStatistics();
// (3) Register new
order.verify(secondKTX).acquireStatement();
order.verify(secondQueryRegistry).registerExecutingQuery(executingQuery);
// (4) Unregister, and close old
order.verify(initialQueryRegistry).unregisterExecutingQuery(executingQuery);
order.verify(initialKTX).commit();
}
use of org.neo4j.kernel.impl.coreapi.InternalTransaction in project neo4j by neo4j.
the class Neo4jTransactionalContextTest method shouldNotBePossibleToCloseMultipleTimes.
@Test
void shouldNotBePossibleToCloseMultipleTimes() {
InternalTransaction tx = mock(InternalTransaction.class);
Neo4jTransactionalContext context = newContext(tx);
context.close();
context.close();
context.close();
}
use of org.neo4j.kernel.impl.coreapi.InternalTransaction in project neo4j by neo4j.
the class BuiltInDbmsProcedures method listQueries.
@SystemProcedure
@Description("List all queries currently executing at this instance that are visible to the user.")
@Procedure(name = "dbms.listQueries", mode = DBMS)
public Stream<QueryStatusResult> listQueries() throws InvalidArgumentsException {
ZoneId zoneId = getConfiguredTimeZone();
List<QueryStatusResult> result = new ArrayList<>();
for (FabricTransaction tx : getFabricTransactions()) {
for (ExecutingQuery query : getActiveFabricQueries(tx)) {
String username = query.username();
var action = new AdminActionOnResource(SHOW_TRANSACTION, ALL, new UserSegment(username));
if (isSelfOrAllows(username, action)) {
result.add(new QueryStatusResult(query, (InternalTransaction) transaction, zoneId, "none"));
}
}
}
for (DatabaseContext databaseContext : getDatabaseManager().registeredDatabases().values()) {
if (databaseContext.database().isStarted()) {
DatabaseScope dbScope = new DatabaseScope(databaseContext.database().getNamedDatabaseId().name());
for (KernelTransactionHandle tx : getExecutingTransactions(databaseContext)) {
if (tx.executingQuery().isPresent()) {
ExecutingQuery query = tx.executingQuery().get();
// Include both the executing query and any previous queries (parent queries of nested query) in the result.
while (query != null) {
String username = query.username();
var action = new AdminActionOnResource(SHOW_TRANSACTION, dbScope, new UserSegment(username));
if (isSelfOrAllows(username, action)) {
result.add(new QueryStatusResult(query, (InternalTransaction) transaction, zoneId, databaseContext.databaseFacade().databaseName()));
}
query = query.getPreviousQuery();
}
}
}
}
}
return result.stream();
}
use of org.neo4j.kernel.impl.coreapi.InternalTransaction in project neo4j by neo4j.
the class IndexStatisticsTest method assertIndexedNodesMatchesStoreNodes.
private void assertIndexedNodesMatchesStoreNodes(IndexDescriptor index) throws Exception {
int nodesInStore = 0;
Label label = Label.label(PERSON_LABEL);
try (Transaction transaction = db.beginTx()) {
KernelTransaction ktx = ((InternalTransaction) transaction).kernelTransaction();
List<String> mismatches = new ArrayList<>();
int propertyKeyId = ktx.tokenRead().propertyKey(NAME_PROPERTY);
IndexReadSession indexSession = ktx.dataRead().indexReadSession(index);
try (NodeValueIndexCursor cursor = ktx.cursors().allocateNodeValueIndexCursor(ktx.cursorContext(), ktx.memoryTracker())) {
// Node --> Index
for (Node node : filter(n -> n.hasLabel(label) && n.hasProperty(NAME_PROPERTY), transaction.getAllNodes())) {
nodesInStore++;
String name = (String) node.getProperty(NAME_PROPERTY);
ktx.dataRead().nodeIndexSeek(indexSession, cursor, unconstrained(), PropertyIndexQuery.exact(propertyKeyId, name));
boolean found = false;
while (cursor.next()) {
long indexedNode = cursor.nodeReference();
if (indexedNode == node.getId()) {
if (found) {
mismatches.add("Index has multiple entries for " + name + " and " + indexedNode);
}
found = true;
}
}
if (!found) {
mismatches.add("Index is missing entry for " + name + " " + node);
}
}
if (!mismatches.isEmpty()) {
fail(String.join(format("%n"), mismatches));
}
// Node count == indexed node count
ktx.dataRead().nodeIndexSeek(indexSession, cursor, unconstrained(), PropertyIndexQuery.exists(propertyKeyId));
int nodesInIndex = 0;
while (cursor.next()) {
nodesInIndex++;
}
assertEquals(nodesInStore, nodesInIndex);
}
}
}
Aggregations