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();
}
}
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");
}
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()));
}
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();
}
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();
}
}
Aggregations