Search in sources :

Example 86 with DatabaseManagementService

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

the class RecoveryCorruptedTransactionLogIT method doNotTruncateNewerTransactionLogFileWhenFailOnError.

@Test
void doNotTruncateNewerTransactionLogFileWhenFailOnError() throws IOException {
    DatabaseManagementService managementService1 = databaseFactory.build();
    GraphDatabaseAPI database = (GraphDatabaseAPI) managementService1.database(DEFAULT_DATABASE_NAME);
    logFiles = buildDefaultLogFiles(getStoreId(database));
    for (int i = 0; i < 10; i++) {
        generateTransaction(database);
    }
    managementService1.shutdown();
    removeLastCheckpointRecordFromLastLogFile();
    addRandomBytesToLastLogFile(this::randomInvalidVersionsBytes);
    DatabaseManagementService managementService = databaseFactory.build();
    GraphDatabaseAPI db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    try {
        DatabaseStateService dbStateService = db.getDependencyResolver().resolveDependency(DatabaseStateService.class);
        assertTrue(dbStateService.causeOfFailure(db.databaseId()).isPresent());
        assertThat(dbStateService.causeOfFailure(db.databaseId()).get()).hasRootCauseInstanceOf(UnsupportedLogVersionException.class);
    } finally {
        managementService.shutdown();
    }
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) DatabaseStateService(org.neo4j.dbms.DatabaseStateService) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 87 with DatabaseManagementService

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

the class RecoveryCorruptedTransactionLogIT method repetitiveRecoveryOfCorruptedLogs.

@Test
void repetitiveRecoveryOfCorruptedLogs() throws IOException {
    DatabaseManagementService service = databaseFactory.build();
    GraphDatabaseAPI database = (GraphDatabaseAPI) service.database(DEFAULT_DATABASE_NAME);
    logFiles = buildDefaultLogFiles(getStoreId(database));
    generateTransactionsAndRotate(database, 4, false);
    service.shutdown();
    removeLastCheckpointRecordFromLastLogFile();
    int expectedRecoveredTransactions = 7;
    while (expectedRecoveredTransactions > 0) {
        truncateBytesFromLastLogFile(1 + random.nextInt(10));
        startStopDbRecoveryOfCorruptedLogs();
        int numberOfRecoveredTransactions = recoveryMonitor.getNumberOfRecoveredTransactions();
        assertEquals(expectedRecoveredTransactions, numberOfRecoveredTransactions);
        expectedRecoveredTransactions--;
        removeLastCheckpointRecordFromLastLogFile();
    }
}
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 88 with DatabaseManagementService

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

the class RecoveryCorruptedTransactionLogIT method truncateNewerTransactionLogFileWhenForced.

@Test
void truncateNewerTransactionLogFileWhenForced() throws IOException {
    DatabaseManagementService managementService = databaseFactory.build();
    GraphDatabaseAPI database = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    TransactionIdStore transactionIdStore = getTransactionIdStore(database);
    long numberOfClosedTransactionsAfterStartup = transactionIdStore.getLastClosedTransactionId();
    logFiles = buildDefaultLogFiles(getStoreId(database));
    for (int i = 0; i < 10; i++) {
        generateTransaction(database);
    }
    long numberOfTransactionsToRecover = transactionIdStore.getLastClosedTransactionId() - numberOfClosedTransactionsAfterStartup;
    managementService.shutdown();
    removeLastCheckpointRecordFromLastLogFile();
    Supplier<Byte> randomBytesSupplier = this::randomInvalidVersionsBytes;
    BytesCaptureSupplier capturingSupplier = new BytesCaptureSupplier(randomBytesSupplier);
    addRandomBytesToLastLogFile(capturingSupplier);
    assertFalse(recoveryMonitor.wasRecoveryRequired());
    startStopDbRecoveryOfCorruptedLogs();
    try {
        assertEquals(numberOfTransactionsToRecover, recoveryMonitor.getNumberOfRecoveredTransactions());
        assertTrue(recoveryMonitor.wasRecoveryRequired());
        assertThat(logProvider).containsMessages("Fail to read transaction log version 0.", "Fail to read transaction log version 0. Last valid transaction start offset is: " + (5548 + txOffsetAfterStart) + ".");
    } catch (Throwable t) {
        throw new RuntimeException("Generated random bytes: " + capturingSupplier.getCapturedBytes(), t);
    }
}
Also used : 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) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 89 with DatabaseManagementService

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

the class RecoveryCorruptedTransactionLogIT method recoverNotAFirstCorruptedTransactionMultipleFilesNoCheckpoints.

@ParameterizedTest(name = "[{index}] ({0})")
@MethodSource("corruptedLogEntryWriters")
void recoverNotAFirstCorruptedTransactionMultipleFilesNoCheckpoints(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();
    generateTransactionsAndRotate(database, 3);
    for (int i = 0; i < 7; 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 3.", "Recovery required from position LogPosition{logVersion=0, byteOffset=" + txOffsetAfterStart + "}", "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(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 90 with DatabaseManagementService

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

the class RecoveryCorruptedTransactionLogIT method evenTruncateNewerTransactionLogFile.

@Test
void evenTruncateNewerTransactionLogFile() 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 numberOfClosedTransactions = getTransactionIdStore(database).getLastClosedTransactionId() - lastClosedTransactionBeforeStart;
    managementService.shutdown();
    removeLastCheckpointRecordFromLastLogFile();
    addRandomBytesToLastLogFile(this::randomNonZeroBytes);
    startStopDbRecoveryOfCorruptedLogs();
    assertEquals(numberOfClosedTransactions, recoveryMonitor.getNumberOfRecoveredTransactions());
}
Also used : 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) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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