Search in sources :

Example 56 with DatabaseManagementService

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

the class RecoveryCorruptedTransactionLogIT method startWithTransactionLogsWithDataAfterLastEntryAndCorruptedLogsRecoveryEnabled.

@Test
void startWithTransactionLogsWithDataAfterLastEntryAndCorruptedLogsRecoveryEnabled() throws IOException {
    long initialTransactionOffset = txOffsetAfterStart + 996;
    DatabaseManagementService managementService = databaseFactory.build();
    GraphDatabaseAPI database = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    logFiles = buildDefaultLogFiles(getStoreId(database));
    generateTransaction(database);
    assertEquals(initialTransactionOffset, getLastClosedTransactionOffset(database));
    managementService.shutdown();
    writeRandomBytesAfterLastCommandInLastLogFile(() -> ByteBuffer.wrap(new byte[] { 1, 2, 3, 4, 5 }));
    managementService = databaseFactory.setConfig(fail_on_corrupted_log_files, false).build();
    try {
        assertThat(logProvider).containsMessages("Recovery required from position " + "LogPosition{logVersion=0, byteOffset=" + initialTransactionOffset + "}").assertExceptionForLogMessage("Fail to read transaction log version 0.").hasMessageContaining("Transaction log files with version 0 has some data available after last readable log entry. " + "Last readable position " + initialTransactionOffset);
        GraphDatabaseAPI restartedDb = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
        assertEquals(initialTransactionOffset, getLastClosedTransactionOffset(restartedDb));
    } finally {
        managementService.shutdown();
    }
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 57 with DatabaseManagementService

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

the class RecoveryCorruptedTransactionLogIT method failToStartWithNotLastTransactionLogHavingZerosInTheEnd.

@Test
void failToStartWithNotLastTransactionLogHavingZerosInTheEnd() 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();
        logFiles.getLogFile().rotate();
        // append zeros in the end of previous file causing illegal suffix
        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 }));
            }
        }
    }
    startStopDatabase();
    assertThat(logProvider).assertExceptionForLogMessage("Fail to read transaction log version 0.").hasMessageContaining("Transaction log files with version 0 has 50 unreadable bytes");
}
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 58 with DatabaseManagementService

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

the class RecoveryCorruptedTransactionLogIT method recoverNotAFirstCorruptedTransactionSingleFileNoCheckpoint.

@ParameterizedTest(name = "[{index}] ({0})")
@MethodSource("corruptedLogEntryWriters")
void recoverNotAFirstCorruptedTransactionSingleFileNoCheckpoint(String testName, LogEntryWriterWrapper logEntryWriterWrapper) throws IOException {
    DatabaseManagementService managementService = databaseFactory.build();
    GraphDatabaseAPI database = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    logFiles = buildDefaultLogFiles(getStoreId(database));
    TransactionIdStore transactionIdStore = getTransactionIdStore(database);
    long lastClosedTransactionBeforeStart = transactionIdStore.getLastClosedTransactionId();
    for (int i = 0; i < 10; i++) {
        generateTransaction(database);
    }
    long numberOfTransactions = transactionIdStore.getLastClosedTransactionId() - lastClosedTransactionBeforeStart;
    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 0.", "Recovery required from position LogPosition{logVersion=0, byteOffset=" + txOffsetAfterStart + "}", "Fail to recover all transactions.", "Any later transaction after LogPosition{logVersion=0, byteOffset=" + (6117 + txOffsetAfterStart) + "} are unreadable and will be truncated.");
    assertEquals(0, logFiles.getLogFile().getHighestLogVersion());
    assertEquals(numberOfTransactions, recoveryMonitor.getNumberOfRecoveredTransactions());
    assertEquals(originalFileLength, fileSystem.getFileSize(highestLogFile));
    // 2 shutdowns will create a checkpoint and recovery that will be triggered by removing tx logs for default db
    // during the setup and starting db as part of the test
    assertEquals(CURRENT_FORMAT_LOG_HEADER_SIZE + 3 * 192, Files.size(logFiles.getCheckpointFile().getCurrentFile()));
}
Also used : Path(java.nio.file.Path) TransactionIdStore(org.neo4j.storageengine.api.TransactionIdStore) SimpleTransactionIdStore(org.neo4j.kernel.impl.transaction.SimpleTransactionIdStore) 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)

Example 59 with DatabaseManagementService

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

the class RecoveryCorruptedTransactionLogIT method startStopDbRecoveryOfCorruptedLogs.

private void startStopDbRecoveryOfCorruptedLogs() {
    DatabaseManagementService managementService = databaseFactory.setConfig(fail_on_corrupted_log_files, false).build();
    managementService.shutdown();
}
Also used : DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService)

Example 60 with DatabaseManagementService

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

the class KernelDiagnosticsIT method createIndexInIsolatedDbInstance.

private static void createIndexInIsolatedDbInstance(Path homeDir, GraphDatabaseSettings.SchemaIndex index) {
    DatabaseManagementService managementService = new TestDatabaseManagementServiceBuilder(homeDir).setConfig(GraphDatabaseSettings.default_schema_provider, index.providerName()).build();
    GraphDatabaseService db = managementService.database(DEFAULT_DATABASE_NAME);
    try {
        Label label = Label.label("Label-" + index.providerName());
        String key = "key";
        try (Transaction tx = db.beginTx()) {
            for (int i = 0; i < 100; i++) {
                tx.createNode(label).setProperty(key, i);
            }
            tx.commit();
        }
        try (Transaction tx = db.beginTx()) {
            tx.schema().indexFor(label).on(key).create();
            tx.commit();
        }
        try (Transaction tx = db.beginTx()) {
            tx.schema().awaitIndexesOnline(2, MINUTES);
            tx.commit();
        }
    } finally {
        managementService.shutdown();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) Transaction(org.neo4j.graphdb.Transaction) Label(org.neo4j.graphdb.Label) ByteUnit.bytesToString(org.neo4j.io.ByteUnit.bytesToString) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService)

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