Search in sources :

Example 81 with DatabaseManagementService

use of org.neo4j.dbms.api.DatabaseManagementService 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 82 with DatabaseManagementService

use of org.neo4j.dbms.api.DatabaseManagementService in project neo4j by neo4j.

the class DatabaseRecoveryIT method reportProgressOnRecovery.

@Test
void reportProgressOnRecovery() throws IOException {
    GraphDatabaseService database = startDatabase(directory.homePath());
    for (int i = 0; i < 10; i++) {
        try (Transaction transaction = database.beginTx()) {
            transaction.createNode();
            transaction.commit();
        }
    }
    var restoreDbLayout = copyStore();
    DatabaseManagementService recoveredService = getManagementService(restoreDbLayout.getNeo4jLayout().homeDirectory());
    GraphDatabaseService recoveredDatabase = recoveredService.database(DEFAULT_DATABASE_NAME);
    try (Transaction tx = recoveredDatabase.beginTx()) {
        assertEquals(10, count(tx.getAllNodes()));
    }
    assertThat(logProvider).containsMessages("10% completed", "100% completed");
    recoveredService.shutdown();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test)

Example 83 with DatabaseManagementService

use of org.neo4j.dbms.api.DatabaseManagementService in project neo4j by neo4j.

the class DatabaseRecoveryIT method recoveryShouldFixPartiallyAppliedSchemaIndexUpdates.

@Test
void recoveryShouldFixPartiallyAppliedSchemaIndexUpdates() {
    Label label = Label.label("Foo");
    String property = "Bar";
    // cause failure during 'relationship.delete()' command application
    ClassGuardedAdversary adversary = new ClassGuardedAdversary(new CountingAdversary(1, true), Command.RelationshipCommand.class);
    adversary.disable();
    Path storeDir = directory.homePath();
    DatabaseManagementService managementService = AdversarialPageCacheGraphDatabaseFactory.create(storeDir, fileSystem, adversary).build();
    GraphDatabaseService db = managementService.database(DEFAULT_DATABASE_NAME);
    try {
        try (Transaction tx = db.beginTx()) {
            tx.schema().constraintFor(label).assertPropertyIsUnique(property).create();
            tx.commit();
        }
        long relationshipId = createRelationship(db);
        TransactionFailureException txFailure = null;
        try (Transaction tx = db.beginTx()) {
            Node node = tx.createNode(label);
            node.setProperty(property, "B");
            // this should fail because of the adversary
            tx.getRelationshipById(relationshipId).delete();
            adversary.enable();
            tx.commit();
        } catch (TransactionFailureException e) {
            txFailure = e;
        }
        assertNotNull(txFailure);
        adversary.disable();
        // heal the db so it is possible to inspect the data
        healthOf(db).healed();
        // now we can observe partially committed state: node is in the index and relationship still present
        try (Transaction tx = db.beginTx()) {
            assertNotNull(findNode(label, property, "B", tx));
            assertNotNull(tx.getRelationshipById(relationshipId));
            tx.commit();
        }
        // panic the db again to force recovery on the next startup
        healthOf(db).panic(txFailure.getCause());
        // restart the database, now with regular page cache
        managementService.shutdown();
        db = startDatabase(storeDir);
        // now we observe correct state: node is in the index and relationship is removed
        try (Transaction tx = db.beginTx()) {
            assertNotNull(findNode(label, property, "B", tx));
            assertRelationshipNotExist(tx, relationshipId);
            tx.commit();
        }
    } finally {
        managementService.shutdown();
    }
}
Also used : Path(java.nio.file.Path) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) CountingAdversary(org.neo4j.adversaries.CountingAdversary) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) ClassGuardedAdversary(org.neo4j.adversaries.ClassGuardedAdversary) Transaction(org.neo4j.graphdb.Transaction) Command(org.neo4j.internal.recordstorage.Command) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test)

Example 84 with DatabaseManagementService

use of org.neo4j.dbms.api.DatabaseManagementService in project neo4j by neo4j.

the class RecoveryCorruptedTransactionLogIT method startWithoutProblemsIfRotationForcedBeforeFileEnd.

@Test
void startWithoutProblemsIfRotationForcedBeforeFileEnd() throws IOException {
    DatabaseManagementService managementService = databaseFactory.build();
    GraphDatabaseAPI database = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    logFiles = buildDefaultLogFiles(getStoreId(database));
    generateTransaction(database);
    managementService.shutdown();
    try (Lifespan lifespan = new Lifespan(logFiles)) {
        Path originalFile = logFiles.getLogFile().getHighestLogFile();
        // in it its current position.
        try (StoreFileChannel writeChannel = fileSystem.write(originalFile)) {
            writeChannel.position(writeChannel.size());
            for (int i = 0; i < 10; i++) {
                writeChannel.writeAll(ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0 }));
            }
        }
        logFiles.getLogFile().rotate();
    }
    startStopDatabase();
    assertThat(logProvider).doesNotContainMessage("Fail to read transaction log version 0.");
}
Also used : Path(java.nio.file.Path) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) StoreFileChannel(org.neo4j.io.fs.StoreFileChannel) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 85 with DatabaseManagementService

use of org.neo4j.dbms.api.DatabaseManagementService in project neo4j by neo4j.

the class RecoveryCorruptedTransactionLogIT method recoverNotAFirstCorruptedTransactionMultipleFilesMultipleCheckpoints.

@ParameterizedTest(name = "[{index}] ({0})")
@MethodSource("corruptedLogEntryWriters")
void recoverNotAFirstCorruptedTransactionMultipleFilesMultipleCheckpoints(String testName, LogEntryWriterWrapper logEntryWriterWrapper) throws IOException {
    DatabaseManagementService managementService = databaseFactory.build();
    GraphDatabaseAPI database = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    logFiles = buildDefaultLogFiles(getStoreId(database));
    long transactionsToRecover = 7;
    generateTransactionsAndRotateWithCheckpoint(database, 3);
    for (int i = 0; i < transactionsToRecover; i++) {
        generateTransaction(database);
    }
    managementService.shutdown();
    Path highestLogFile = logFiles.getLogFile().getHighestLogFile();
    long originalFileLength = getLastReadablePosition(highestLogFile).getByteOffset();
    removeLastCheckpointRecordFromLastLogFile();
    addCorruptedCommandsToLastLogFile(logEntryWriterWrapper);
    long modifiedFileLength = fileSystem.getFileSize(highestLogFile);
    assertThat(modifiedFileLength).isGreaterThan(originalFileLength);
    startStopDbRecoveryOfCorruptedLogs();
    assertThat(logProvider).containsMessages("Fail to read transaction log version 3.", "Recovery required from position LogPosition{logVersion=3, byteOffset=" + (569 + HEADER_OFFSET) + "}", "Fail to recover all transactions.", "Any later transaction after LogPosition{logVersion=3, byteOffset=" + (4552 + HEADER_OFFSET) + "} are unreadable and will be truncated.");
    assertEquals(3, logFiles.getLogFile().getHighestLogVersion());
    assertEquals(transactionsToRecover, recoveryMonitor.getNumberOfRecoveredTransactions());
    assertEquals(originalFileLength, fileSystem.getFileSize(highestLogFile));
    assertEquals(CURRENT_FORMAT_LOG_HEADER_SIZE + 6 * 192, Files.size(logFiles.getCheckpointFile().getCurrentFile()));
}
Also used : Path(java.nio.file.Path) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)155 Test (org.junit.jupiter.api.Test)100 TestDatabaseManagementServiceBuilder (org.neo4j.test.TestDatabaseManagementServiceBuilder)79 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)61 Transaction (org.neo4j.graphdb.Transaction)60 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)56 Path (java.nio.file.Path)37 Node (org.neo4j.graphdb.Node)29 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 Label (org.neo4j.graphdb.Label)16 MethodSource (org.junit.jupiter.params.provider.MethodSource)10 EphemeralFileSystemAbstraction (org.neo4j.io.fs.EphemeralFileSystemAbstraction)9 DatabaseLayout (org.neo4j.io.layout.DatabaseLayout)9 DatabaseStateService (org.neo4j.dbms.DatabaseStateService)8 Config (org.neo4j.configuration.Config)7 DatabaseManagementServiceBuilder (org.neo4j.dbms.api.DatabaseManagementServiceBuilder)7 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)7 GBPTreeCountsStore (org.neo4j.internal.counts.GBPTreeCountsStore)6 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)6 Lifespan (org.neo4j.kernel.lifecycle.Lifespan)6