Search in sources :

Example 66 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.

the class CountsComputerTest method shouldCreateACountsStoreWhenThereAreUnusedRelationshipRecordsInTheDB.

@Test
void shouldCreateACountsStoreWhenThereAreUnusedRelationshipRecordsInTheDB() throws IOException, KernelException {
    DatabaseManagementService managementService = dbBuilder.build();
    final GraphDatabaseAPI db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    Label[] labels = null;
    int[] labelIds = null;
    RelationshipType[] relTypes = null;
    int[] relTypeIds = null;
    Node[] nodes = null;
    Relationship[] rels = null;
    try (Transaction tx = db.beginTx()) {
        labels = createLabels(4);
        labelIds = getLabelIdsFrom(tx, labels);
        relTypes = createRelationShipTypes(2);
        relTypeIds = getRelTypeIdsFrom(tx, relTypes);
        nodes = new Node[] { tx.createNode(labels[0]), tx.createNode(labels[1]) };
        rels = new Relationship[] { nodes[0].createRelationshipTo(nodes[1], relTypes[0]), nodes[1].createRelationshipTo(nodes[0], relTypes[1]) };
        rels[0].delete();
        tx.commit();
    }
    long lastCommittedTransactionId = getLastTxId(db);
    managementService.shutdown();
    rebuildCounts(lastCommittedTransactionId);
    try (GBPTreeCountsStore store = createCountsStore()) {
        softly.assertThat(store.txId()).as("Store Transaction id").isEqualTo(lastCommittedTransactionId);
        softly.assertThat(store.nodeCount(ANY_LABEL, NULL)).as("count: ()").isEqualTo(nodes.length);
        softly.assertThat(store.nodeCount(labelIds[0], NULL)).as("count: (:%s)", labels[0]).isEqualTo(1);
        softly.assertThat(store.nodeCount(labelIds[1], NULL)).as("count: (:%s)", labels[1]).isEqualTo(1);
        softly.assertThat(store.nodeCount(labelIds[2], NULL)).as("count: (:%s)", labels[2]).isEqualTo(0);
        softly.assertThat(store.nodeCount(labelIds[3], NULL)).as("count: (:%s)", labels[3]).isEqualTo(0);
        softly.assertThat(store.relationshipCount(ANY_LABEL, ANY_RELATIONSHIP_TYPE, ANY_LABEL, NULL)).as("()-[]->()").isEqualTo(rels.length - 1);
        softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[0], ANY_LABEL, NULL)).as("count: ()-[:%s]->()", relTypes[0]).isEqualTo(0);
        softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[1], ANY_LABEL, NULL)).as("count: ()-[:%s]->()", relTypes[1]).isEqualTo(1);
    }
}
Also used : Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) RelationshipType(org.neo4j.graphdb.RelationshipType) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Relationship(org.neo4j.graphdb.Relationship) GBPTreeCountsStore(org.neo4j.internal.counts.GBPTreeCountsStore) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test)

Example 67 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.

the class StoreMigratorTest method extractTransactionalInformationFromLogs.

private void extractTransactionalInformationFromLogs(Path customLogsLocation) throws IOException {
    Config config = Config.defaults(transaction_logs_root_path, customLogsLocation);
    LogService logService = new SimpleLogService(NullLogProvider.getInstance(), NullLogProvider.getInstance());
    DatabaseManagementService managementService = new TestDatabaseManagementServiceBuilder(databaseLayout).setConfig(transaction_logs_root_path, customLogsLocation).build();
    GraphDatabaseAPI database = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    StorageEngineFactory storageEngineFactory = database.getDependencyResolver().resolveDependency(StorageEngineFactory.class);
    for (int i = 0; i < 10; i++) {
        try (Transaction transaction = database.beginTx()) {
            transaction.createNode();
            transaction.commit();
        }
    }
    DatabaseLayout databaseLayout = database.databaseLayout();
    Path neoStore = databaseLayout.metadataStore();
    managementService.shutdown();
    MetaDataStore.setRecord(pageCache, neoStore, MetaDataStore.Position.LAST_CLOSED_TRANSACTION_LOG_VERSION, MetaDataRecordFormat.FIELD_NOT_PRESENT, databaseLayout.getDatabaseName(), NULL);
    RecordStorageMigrator migrator = new RecordStorageMigrator(fileSystem, pageCache, config, logService, jobScheduler, PageCacheTracer.NULL, batchImporterFactory, INSTANCE);
    LogPosition logPosition = migrator.extractTransactionLogPosition(neoStore, databaseLayout, 100, NULL);
    LogFiles logFiles = LogFilesBuilder.activeFilesBuilder(databaseLayout, fileSystem, pageCache).withConfig(config).withCommandReaderFactory(storageEngineFactory.commandReaderFactory()).build();
    assertEquals(0, logPosition.getLogVersion());
    assertEquals(Files.size(logFiles.getLogFile().getHighestLogFile()), logPosition.getByteOffset());
}
Also used : Path(java.nio.file.Path) SimpleLogService(org.neo4j.logging.internal.SimpleLogService) Config(org.neo4j.configuration.Config) StorageEngineFactory(org.neo4j.storageengine.api.StorageEngineFactory) LogFiles(org.neo4j.kernel.impl.transaction.log.files.LogFiles) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) NullLogService(org.neo4j.logging.internal.NullLogService) SimpleLogService(org.neo4j.logging.internal.SimpleLogService) LogService(org.neo4j.logging.internal.LogService) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition)

Example 68 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.

the class CheckPointerIntegrationTest method shouldNotCheckPointWhenThereAreNoCommits.

@Test
void shouldNotCheckPointWhenThereAreNoCommits() throws Throwable {
    // given
    DatabaseManagementService managementService = builder.setConfig(check_point_interval_time, Duration.ofSeconds(1)).setConfig(check_point_interval_tx, 10000).setConfig(logical_log_rotation_threshold, gibiBytes(1)).build();
    GraphDatabaseService db = managementService.database(DEFAULT_DATABASE_NAME);
    GraphDatabaseAPI databaseAPI = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    getCheckPointer(databaseAPI).forceCheckPoint(new SimpleTriggerInfo("given"));
    var checkPointsBefore = checkPointInTxLog(db).size();
    // when
    // nothing happens
    triggerCheckPointAttempt(db);
    assertThat(checkPointInTxLog(db)).hasSize(checkPointsBefore);
    managementService.shutdown();
    managementService = builder.build();
    try {
        // then - 1 check point has been written in the log
        var checkPoints = checkPointsInTxLog(managementService.database(DEFAULT_DATABASE_NAME));
        assertEquals(checkPointsBefore + 1, checkPoints.size());
    } finally {
        managementService.shutdown();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test)

Example 69 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.

the class VersionAwareLogEntryReaderIT method setUp.

@BeforeEach
void setUp() {
    GraphDatabaseService database = managementService.database(DEFAULT_DATABASE_NAME);
    createNode(database);
    GraphDatabaseAPI dbApi = (GraphDatabaseAPI) database;
    databaseLayout = dbApi.databaseLayout();
    entryReader = new VersionAwareLogEntryReader(dbApi.getDependencyResolver().resolveDependency(StorageEngineFactory.class).commandReaderFactory());
    managementService.shutdown();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) StorageEngineFactory(org.neo4j.storageengine.api.StorageEngineFactory) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 70 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.

the class PartialTransactionFailureIT method concurrentlyCommittingTransactionsMustNotRotateOutLoggedCommandsOfFailingTransaction.

@Test
void concurrentlyCommittingTransactionsMustNotRotateOutLoggedCommandsOfFailingTransaction() throws Exception {
    final ClassGuardedAdversary adversary = new ClassGuardedAdversary(new CountingAdversary(1, false), Command.RelationshipCommand.class);
    adversary.disable();
    Path storeDir = testDirectory.homePath();
    final Map<Setting<?>, Object> params = Map.of(GraphDatabaseSettings.pagecache_memory, "8m");
    managementService = new TestDatabaseManagementServiceBuilder(storeDir).setFileSystem(new AdversarialFileSystemAbstraction(adversary)).setConfig(params).build();
    GraphDatabaseAPI db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    Node a;
    Node b;
    Node c;
    Node d;
    try (Transaction tx = db.beginTx()) {
        a = tx.createNode();
        b = tx.createNode();
        c = tx.createNode();
        d = tx.createNode();
        tx.commit();
    }
    adversary.enable();
    CountDownLatch latch = new CountDownLatch(1);
    Thread t1 = new Thread(createRelationship(db, a, b, latch), "T1");
    Thread t2 = new Thread(createRelationship(db, c, d, latch), "T2");
    t1.start();
    t2.start();
    // Wait for both threads to get going
    t1.join(10);
    t2.join(10);
    latch.countDown();
    // Wait for the transactions to finish
    t1.join(25000);
    t2.join(25000);
    managementService.shutdown();
    // We should observe the store in a consistent state
    managementService = new TestDatabaseManagementServiceBuilder(storeDir).setConfig(params).build();
    GraphDatabaseService database = managementService.database(DEFAULT_DATABASE_NAME);
    try (Transaction tx = database.beginTx()) {
        Node x = tx.getNodeById(a.getId());
        Node y = tx.getNodeById(b.getId());
        Node z = tx.getNodeById(c.getId());
        Node w = tx.getNodeById(d.getId());
        Iterator<Relationship> itrRelX = x.getRelationships().iterator();
        Iterator<Relationship> itrRelY = y.getRelationships().iterator();
        Iterator<Relationship> itrRelZ = z.getRelationships().iterator();
        Iterator<Relationship> itrRelW = w.getRelationships().iterator();
        if (itrRelX.hasNext() != itrRelY.hasNext()) {
            fail("Node x and y have inconsistent relationship counts");
        } else if (itrRelX.hasNext()) {
            Relationship rel = itrRelX.next();
            assertEquals(rel, itrRelY.next());
            assertFalse(itrRelX.hasNext());
            assertFalse(itrRelY.hasNext());
        }
        if (itrRelZ.hasNext() != itrRelW.hasNext()) {
            fail("Node z and w have inconsistent relationship counts");
        } else if (itrRelZ.hasNext()) {
            Relationship rel = itrRelZ.next();
            assertEquals(rel, itrRelW.next());
            assertFalse(itrRelZ.hasNext());
            assertFalse(itrRelW.hasNext());
        }
    }
}
Also used : Path(java.nio.file.Path) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) CountingAdversary(org.neo4j.adversaries.CountingAdversary) ClassGuardedAdversary(org.neo4j.adversaries.ClassGuardedAdversary) Setting(org.neo4j.graphdb.config.Setting) Node(org.neo4j.graphdb.Node) CountDownLatch(java.util.concurrent.CountDownLatch) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) Command(org.neo4j.internal.recordstorage.Command) Relationship(org.neo4j.graphdb.Relationship) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) Test(org.junit.jupiter.api.Test)

Aggregations

GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)262 Test (org.junit.Test)91 Test (org.junit.jupiter.api.Test)88 Transaction (org.neo4j.graphdb.Transaction)87 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)61 Node (org.neo4j.graphdb.Node)38 Path (java.nio.file.Path)30 TestDatabaseManagementServiceBuilder (org.neo4j.test.TestDatabaseManagementServiceBuilder)30 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)28 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)26 File (java.io.File)25 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)24 DatabaseLayout (org.neo4j.io.layout.DatabaseLayout)19 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)18 DependencyResolver (org.neo4j.graphdb.DependencyResolver)17 Label (org.neo4j.graphdb.Label)15 PageCache (org.neo4j.io.pagecache.PageCache)15 Config (org.neo4j.kernel.configuration.Config)15 DatabaseStateService (org.neo4j.dbms.DatabaseStateService)14 IOException (java.io.IOException)11