Search in sources :

Example 6 with QueryExecution

use of org.neo4j.kernel.impl.query.QueryExecution in project neo4j by neo4j.

the class SnapshotExecutionEngine method executeWithRetries.

protected MaterialisedResult executeWithRetries(String query, TransactionalContext context, QueryExecutor executor) throws QueryExecutionKernelException {
    VersionContext versionContext = getCursorContext(context);
    MaterialisedResult materialisedResult;
    int attempt = 0;
    boolean dirtySnapshot;
    do {
        if (attempt == maxQueryExecutionAttempts) {
            throw new QueryExecutionKernelException(new UnstableSnapshotException("Unable to get clean data snapshot for query '%s' after %d attempts.", query, attempt));
        }
        if (attempt > 0) {
            context.executingQuery().onRetryAttempted();
        }
        attempt++;
        versionContext.initRead();
        materialisedResult = new MaterialisedResult();
        QueryExecution queryExecution = executor.execute(materialisedResult);
        materialisedResult.consumeAll(queryExecution);
        // objects is guaranteed by schema and not by execution engine
        if (context.transaction().kernelTransaction().isSchemaTransaction()) {
            return materialisedResult;
        }
        dirtySnapshot = versionContext.isDirty();
        if (isUnstableSnapshot(materialisedResult, dirtySnapshot)) {
            throw new QueryExecutionKernelException(new UnstableSnapshotException("Unable to get clean data snapshot for query '%s' that performs updates.", query, attempt));
        }
    } while (dirtySnapshot);
    return materialisedResult;
}
Also used : VersionContext(org.neo4j.io.pagecache.context.VersionContext) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) QueryExecution(org.neo4j.kernel.impl.query.QueryExecution)

Example 7 with QueryExecution

use of org.neo4j.kernel.impl.query.QueryExecution in project neo4j by neo4j.

the class SnapshotExecutionEngine method executeQuery.

@Override
public Result executeQuery(String query, MapValue parameters, TransactionalContext context, boolean prePopulate) throws QueryExecutionKernelException {
    QueryExecutor queryExecutor = querySubscriber -> super.executeQuery(query, parameters, context, prePopulate, querySubscriber);
    ResultSubscriber resultSubscriber = new ResultSubscriber(context);
    MaterialisedResult materialisedResult = executeWithRetries(query, context, queryExecutor);
    QueryExecution queryExecution = materialisedResult.stream(resultSubscriber);
    resultSubscriber.init(queryExecution);
    return resultSubscriber;
}
Also used : VersionContext(org.neo4j.io.pagecache.context.VersionContext) FullyParsedQuery(org.neo4j.cypher.internal.FullyParsedQuery) Result(org.neo4j.graphdb.Result) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) LogProvider(org.neo4j.logging.LogProvider) Config(org.neo4j.configuration.Config) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) GraphDatabaseQueryService(org.neo4j.kernel.GraphDatabaseQueryService) QueryExecutionMonitor(org.neo4j.kernel.impl.query.QueryExecutionMonitor) CompilerFactory(org.neo4j.cypher.internal.CompilerFactory) InputDataStream(org.neo4j.cypher.internal.runtime.InputDataStream) GraphDatabaseInternalSettings(org.neo4j.configuration.GraphDatabaseInternalSettings) QuerySubscriber(org.neo4j.kernel.impl.query.QuerySubscriber) MapValue(org.neo4j.values.virtual.MapValue) CaffeineCacheFactory(org.neo4j.cypher.internal.cache.CaffeineCacheFactory) QueryExecution(org.neo4j.kernel.impl.query.QueryExecution) QueryExecution(org.neo4j.kernel.impl.query.QueryExecution)

Example 8 with QueryExecution

use of org.neo4j.kernel.impl.query.QueryExecution in project neo4j by neo4j.

the class SnapshotExecutionEngineTest method setUp.

@BeforeEach
void setUp() throws Exception {
    transactionalContext = mock(TransactionalContext.class, RETURNS_DEEP_STUBS);
    KernelStatement kernelStatement = mock(KernelStatement.class);
    executor = mock(SnapshotExecutionEngine.QueryExecutor.class);
    versionContext = mock(VersionContext.class);
    statistics = mock(QueryStatistics.class);
    executionEngine = new SnapshotExecutionEngine(new GraphDatabaseCypherService(db), config, TestExecutorCaffeineCacheFactory$.MODULE$, NullLogProvider.getInstance(), mock(CompilerFactory.class));
    when(transactionalContext.kernelTransaction().cursorContext()).thenReturn(new CursorContext(NULL, versionContext));
    when(transactionalContext.statement()).thenReturn(kernelStatement);
    var innerExecution = mock(QueryExecution.class);
    when(executor.execute(any())).thenAnswer((Answer<QueryExecution>) invocationOnMock -> {
        MaterialisedResult materialisedResult = invocationOnMock.getArgument(0);
        materialisedResult.onResultCompleted(statistics);
        return innerExecution;
    });
}
Also used : VersionContext(org.neo4j.io.pagecache.context.VersionContext) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) TestExecutorCaffeineCacheFactory$(org.neo4j.cypher.internal.cache.TestExecutorCaffeineCacheFactory$) CursorContext(org.neo4j.io.pagecache.context.CursorContext) Config(org.neo4j.configuration.Config) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) NullLogProvider(org.neo4j.logging.NullLogProvider) Answer(org.mockito.stubbing.Answer) ImpermanentDbmsExtension(org.neo4j.test.extension.ImpermanentDbmsExtension) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Inject(org.neo4j.test.extension.Inject) RETURNS_DEEP_STUBS(org.mockito.Mockito.RETURNS_DEEP_STUBS) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) VersionContext(org.neo4j.io.pagecache.context.VersionContext) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) NULL(org.neo4j.io.pagecache.tracing.cursor.PageCursorTracer.NULL) CompilerFactory(org.neo4j.cypher.internal.CompilerFactory) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) QueryStatistics(org.neo4j.graphdb.QueryStatistics) QueryExecution(org.neo4j.kernel.impl.query.QueryExecution) Mockito.mock(org.mockito.Mockito.mock) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) QueryStatistics(org.neo4j.graphdb.QueryStatistics) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) CursorContext(org.neo4j.io.pagecache.context.CursorContext) QueryExecution(org.neo4j.kernel.impl.query.QueryExecution) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 9 with QueryExecution

use of org.neo4j.kernel.impl.query.QueryExecution in project neo4j by neo4j.

the class AbstractCypherAdapterStreamTest method shouldIncludeProfileIfPresent.

@Test
void shouldIncludeProfileIfPresent() throws Throwable {
    // Given
    QueryStatistics queryStatistics = mock(QueryStatistics.class);
    when(queryStatistics.containsUpdates()).thenReturn(false);
    QueryExecution result = mock(QueryExecution.class);
    BoltAdapterSubscriber subscriber = new BoltAdapterSubscriber();
    when(result.fieldNames()).thenReturn(new String[0]);
    when(result.executionType()).thenReturn(explained(READ_ONLY));
    subscriber.onResultCompleted(queryStatistics);
    when(result.getNotifications()).thenReturn(Collections.emptyList());
    when(result.executionPlanDescription()).thenReturn(plan("Join", map("arg1", 1), 2, 4, 3, 1, 2, singletonList("id1"), plan("Scan", map("arg2", 1), 2, 4, 7, 1, 1, singletonList("id2"))));
    var stream = new TestAbstractCypherAdapterStream(result, subscriber, Clock.systemUTC());
    // When
    MapValue meta = metadataOf(stream);
    // Then
    MapValue expectedChild = mapValues("args", mapValues("arg2", intValue(1)), "identifiers", list(stringValue("id2")), "operatorType", stringValue("Scan"), "children", VirtualValues.EMPTY_LIST, "rows", longValue(1L), "dbHits", longValue(2L), "pageCacheHits", longValue(4L), "pageCacheMisses", longValue(7L), "pageCacheHitRatio", doubleValue(4.0 / 11), "time", longValue(1));
    MapValue expectedProfile = mapValues("args", mapValues("arg1", intValue(1)), "identifiers", list(stringValue("id1")), "operatorType", stringValue("Join"), "children", list(expectedChild), "rows", longValue(1L), "dbHits", longValue(2L), "pageCacheHits", longValue(4L), "pageCacheMisses", longValue(3L), "pageCacheHitRatio", doubleValue(4.0 / 7), "time", longValue(2));
    assertMapEqualsWithDelta((MapValue) meta.get("profile"), expectedProfile, 0.0001);
}
Also used : QueryStatistics(org.neo4j.graphdb.QueryStatistics) BoltAdapterSubscriber(org.neo4j.bolt.runtime.statemachine.impl.BoltAdapterSubscriber) MapValue(org.neo4j.values.virtual.MapValue) QueryExecution(org.neo4j.kernel.impl.query.QueryExecution) Test(org.junit.jupiter.api.Test)

Example 10 with QueryExecution

use of org.neo4j.kernel.impl.query.QueryExecution in project neo4j by neo4j.

the class AbstractCypherAdapterStreamTest method shouldDiscardAllReadQuery.

@Test
void shouldDiscardAllReadQuery() throws Throwable {
    // Given
    QueryExecution queryExecution = mock(QueryExecution.class);
    when(queryExecution.fieldNames()).thenReturn(new String[] { "foo" });
    when(queryExecution.executionType()).thenReturn(query(READ_ONLY));
    when(queryExecution.getNotifications()).thenReturn(Collections.emptyList());
    when(queryExecution.await()).thenReturn(false);
    BoltAdapterSubscriber subscriber = new BoltAdapterSubscriber();
    QueryStatistics queryStatistics = mock(QueryStatistics.class);
    when(queryStatistics.containsUpdates()).thenReturn(false);
    when(queryStatistics.getNodesCreated()).thenReturn(0);
    when(queryStatistics.getNodesDeleted()).thenReturn(0);
    when(queryStatistics.getRelationshipsCreated()).thenReturn(0);
    when(queryStatistics.getRelationshipsDeleted()).thenReturn(0);
    when(queryStatistics.getPropertiesSet()).thenReturn(0);
    when(queryStatistics.getIndexesAdded()).thenReturn(0);
    when(queryStatistics.getIndexesRemoved()).thenReturn(0);
    when(queryStatistics.getConstraintsAdded()).thenReturn(0);
    when(queryStatistics.getConstraintsRemoved()).thenReturn(0);
    when(queryStatistics.getLabelsAdded()).thenReturn(0);
    when(queryStatistics.getLabelsRemoved()).thenReturn(0);
    subscriber.onResultCompleted(queryStatistics);
    Clock clock = mock(Clock.class);
    var stream = new TestAbstractCypherAdapterStream(queryExecution, subscriber, clock);
    // When
    stream.discardRecords(mock(BoltResult.DiscardingRecordConsumer.class), STREAM_LIMIT_UNLIMITED);
    // Then
    verify(queryExecution, times(1)).cancel();
    verify(queryExecution, times(1)).await();
}
Also used : QueryStatistics(org.neo4j.graphdb.QueryStatistics) BoltAdapterSubscriber(org.neo4j.bolt.runtime.statemachine.impl.BoltAdapterSubscriber) Clock(java.time.Clock) QueryExecution(org.neo4j.kernel.impl.query.QueryExecution) Test(org.junit.jupiter.api.Test)

Aggregations

QueryExecution (org.neo4j.kernel.impl.query.QueryExecution)15 Test (org.junit.jupiter.api.Test)10 QueryStatistics (org.neo4j.graphdb.QueryStatistics)9 BoltAdapterSubscriber (org.neo4j.bolt.runtime.statemachine.impl.BoltAdapterSubscriber)8 MapValue (org.neo4j.values.virtual.MapValue)6 Clock (java.time.Clock)5 TransactionalContext (org.neo4j.kernel.impl.query.TransactionalContext)4 VersionContext (org.neo4j.io.pagecache.context.VersionContext)3 QueryExecutionKernelException (org.neo4j.kernel.impl.query.QueryExecutionKernelException)3 Config (org.neo4j.configuration.Config)2 CompilerFactory (org.neo4j.cypher.internal.CompilerFactory)2 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 RETURNS_DEEP_STUBS (org.mockito.Mockito.RETURNS_DEEP_STUBS)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.times (org.mockito.Mockito.times)1 Mockito.verify (org.mockito.Mockito.verify)1 Mockito.when (org.mockito.Mockito.when)1