Search in sources :

Example 16 with EphemeralFileSystemAbstraction

use of org.neo4j.io.fs.EphemeralFileSystemAbstraction in project neo4j by neo4j.

the class ConstraintRecoveryIT method shouldHaveAvailableOrphanedConstraintIndexIfUniqueConstraintCreationFails.

@Test
void shouldHaveAvailableOrphanedConstraintIndexIfUniqueConstraintCreationFails() {
    // given
    Path pathToDb = testDirectory.homePath();
    TestDatabaseManagementServiceBuilder dbFactory = new TestDatabaseManagementServiceBuilder(pathToDb);
    dbFactory.setFileSystem(fs);
    final EphemeralFileSystemAbstraction[] storeInNeedOfRecovery = new EphemeralFileSystemAbstraction[1];
    final AtomicBoolean monitorCalled = new AtomicBoolean(false);
    Monitors monitors = new Monitors();
    monitors.addMonitorListener(new IndexingService.MonitorAdapter() {

        @Override
        public void indexPopulationScanComplete() {
            monitorCalled.set(true);
            db.getDependencyResolver().resolveDependency(RecordStorageEngine.class).testAccessNeoStores().getSchemaStore().flush(NULL);
            storeInNeedOfRecovery[0] = fs.snapshot();
        }
    });
    dbFactory.setMonitors(monitors);
    // This test relies on behaviour that is specific to the Lucene populator, where uniqueness is controlled
    // after index has been populated, which is why we're using NATIVE20 and index booleans (they end up in Lucene)
    DatabaseManagementService managementService = configure(dbFactory.impermanent().setConfig(default_schema_provider, NATIVE30.providerName())).build();
    db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    try (Transaction tx = db.beginTx()) {
        for (int i = 0; i < 2; i++) {
            tx.createNode(LABEL).setProperty(KEY, "true");
        }
        tx.commit();
    }
    assertThrows(ConstraintViolationException.class, () -> {
        try (Transaction tx = db.beginTx()) {
            tx.schema().constraintFor(LABEL).assertPropertyIsUnique(KEY).create();
        }
    });
    managementService.shutdown();
    assertTrue(monitorCalled.get());
    // when
    dbFactory = new TestDatabaseManagementServiceBuilder(pathToDb);
    dbFactory.setFileSystem(storeInNeedOfRecovery[0]);
    DatabaseManagementService secondManagementService = configure(dbFactory.impermanent()).build();
    db = (GraphDatabaseAPI) secondManagementService.database(DEFAULT_DATABASE_NAME);
    // then
    try (Transaction tx = db.beginTx()) {
        tx.schema().awaitIndexesOnline(10, TimeUnit.MINUTES);
    }
    try (Transaction transaction = db.beginTx()) {
        assertEquals(2, Iterables.count(transaction.getAllNodes()));
    }
    try (Transaction tx = db.beginTx()) {
        assertEquals(0, Iterables.count(Iterables.asList(tx.schema().getConstraints())));
    }
    try (Transaction tx = db.beginTx()) {
        IndexDefinition orphanedConstraintIndex = single(tx.schema().getIndexes(LABEL));
        assertEquals(LABEL.name(), single(orphanedConstraintIndex.getLabels()).name());
        assertEquals(KEY, single(orphanedConstraintIndex.getPropertyKeys()));
    }
    secondManagementService.shutdown();
}
Also used : Path(java.nio.file.Path) EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) Transaction(org.neo4j.graphdb.Transaction) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) Monitors(org.neo4j.monitoring.Monitors) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test)

Example 17 with EphemeralFileSystemAbstraction

use of org.neo4j.io.fs.EphemeralFileSystemAbstraction in project neo4j by neo4j.

the class DatabaseRecoveryIT method shouldSeeTheSameRecordsAtCheckpointAsAfterReverseRecovery.

@Test
void shouldSeeTheSameRecordsAtCheckpointAsAfterReverseRecovery() throws Exception {
    // given
    EphemeralFileSystemAbstraction fs = new EphemeralFileSystemAbstraction();
    managementService = new TestDatabaseManagementServiceBuilder(directory.homePath()).setFileSystem(fs).impermanent().build();
    GraphDatabaseService db = managementService.database(DEFAULT_DATABASE_NAME);
    produceRandomGraphUpdates(db, 100);
    checkPoint(db);
    EphemeralFileSystemAbstraction checkPointFs = fs.snapshot();
    // when
    produceRandomGraphUpdates(db, 100);
    flush(db);
    EphemeralFileSystemAbstraction crashedFs = fs.snapshot();
    managementService.shutdown();
    fs.close();
    Dependencies dependencies = new Dependencies();
    Monitors monitors;
    AtomicReference<EphemeralFileSystemAbstraction> reversedFs;
    try (PageCache pageCache = pageCacheExtension.getPageCache(crashedFs)) {
        dependencies.satisfyDependencies(pageCache);
        monitors = new Monitors();
        reversedFs = new AtomicReference<>();
        monitors.addMonitorListener(new RecoveryMonitor() {

            @Override
            public void reverseStoreRecoveryCompleted(long checkpointTxId) {
                try {
                    // Flush the page cache which will fished out of the GlobalModule at the point of constructing the database
                    pageCache.flushAndForce();
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
                // The stores should now be equal in content to the db as it was right after the checkpoint.
                // Grab a snapshot so that we can compare later.
                reversedFs.set(crashedFs.snapshot());
            }
        });
        DatabaseManagementService managementService = new TestDatabaseManagementServiceBuilder(directory.homePath()).setFileSystem(crashedFs).setExternalDependencies(dependencies).setMonitors(monitors).impermanent().build();
        managementService.shutdown();
    }
    // then
    fs.close();
    try {
        // Here we verify that the neostore contents, record by record are exactly the same when comparing
        // the store as it was right after the checkpoint with the store as it was right after reverse recovery completed.
        assertSameStoreContents(checkPointFs, reversedFs.get(), databaseLayout);
    } finally {
        IOUtils.closeAll(checkPointFs, reversedFs.get());
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) Monitors(org.neo4j.monitoring.Monitors) Dependencies(org.neo4j.collection.Dependencies) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.jupiter.api.Test)

Example 18 with EphemeralFileSystemAbstraction

use of org.neo4j.io.fs.EphemeralFileSystemAbstraction in project neo4j by neo4j.

the class TestTxEntries method testStartEntryWrittenOnceOnRollback.

@Test
void testStartEntryWrittenOnceOnRollback() {
    Path storeDir = testDirectory.homePath();
    DatabaseManagementService managementService = new TestDatabaseManagementServiceBuilder(storeDir).setFileSystem(fs).impermanent().build();
    final GraphDatabaseService db = managementService.database(DEFAULT_DATABASE_NAME);
    createSomeTransactions(db);
    EphemeralFileSystemAbstraction snapshot = fs.snapshot();
    managementService.shutdown();
    managementService = new TestDatabaseManagementServiceBuilder(storeDir).setFileSystem(snapshot).impermanent().build();
    managementService.shutdown();
}
Also used : Path(java.nio.file.Path) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test)

Example 19 with EphemeralFileSystemAbstraction

use of org.neo4j.io.fs.EphemeralFileSystemAbstraction in project neo4j by neo4j.

the class KernelRecoveryTest method shouldHandleWritesProperlyAfterRecovery.

@Test
void shouldHandleWritesProperlyAfterRecovery() throws Exception {
    // Given
    GraphDatabaseService db = newDB(fileSystem, "main");
    // We don't want to include any transactions that may have run on start-up since they
    // will have run on start-up of rebuilt db already.
    long txIdToExtractFrom = getLastClosedTransactionId((GraphDatabaseAPI) db) + 1;
    long node1 = createNode(db, "k", "v1");
    // And given the power goes out
    List<TransactionRepresentation> transactions = new ArrayList<>();
    long node2;
    try (EphemeralFileSystemAbstraction crashedFs = fileSystem.snapshot()) {
        managementService.shutdown();
        db = newDB(crashedFs, "main");
        node2 = createNode(db, "k", "v2");
        extractTransactions((GraphDatabaseAPI) db, transactions, txIdToExtractFrom);
        managementService.shutdown();
    }
    // Then both those nodes should be there, i.e. they are properly there in the log
    GraphDatabaseService rebuilt = newDB(fileSystem, "rebuilt");
    applyTransactions(transactions, (GraphDatabaseAPI) rebuilt);
    try (Transaction tx = rebuilt.beginTx()) {
        assertEquals("v1", tx.getNodeById(node1).getProperty("k"));
        assertEquals("v2", tx.getNodeById(node2).getProperty("k"));
        tx.commit();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction) TransactionRepresentation(org.neo4j.kernel.impl.transaction.TransactionRepresentation) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 20 with EphemeralFileSystemAbstraction

use of org.neo4j.io.fs.EphemeralFileSystemAbstraction in project neo4j by neo4j.

the class EphemeralFileSystemRule method snapshot.

public EphemeralFileSystemAbstraction snapshot(Runnable action) throws Exception {
    EphemeralFileSystemAbstraction snapshot = fs.snapshot();
    try {
        action.run();
    } finally {
        fs.close();
        fs = snapshot;
    }
    return fs;
}
Also used : EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction)

Aggregations

EphemeralFileSystemAbstraction (org.neo4j.io.fs.EphemeralFileSystemAbstraction)30 Test (org.junit.jupiter.api.Test)17 TestDatabaseManagementServiceBuilder (org.neo4j.test.TestDatabaseManagementServiceBuilder)10 PageCache (org.neo4j.io.pagecache.PageCache)9 Path (java.nio.file.Path)7 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)7 Transaction (org.neo4j.graphdb.Transaction)6 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 RepeatedTest (org.junit.jupiter.api.RepeatedTest)2 AdversarialFileSystemAbstraction (org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction)2 Dependencies (org.neo4j.collection.Dependencies)2 DatabaseManagementServiceBuilder (org.neo4j.dbms.api.DatabaseManagementServiceBuilder)2 Label (org.neo4j.graphdb.Label)2 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)2 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)2