use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class RecoveryCorruptedTransactionLogIT method addCorruptedCommandsToLastLogFile.
private void addCorruptedCommandsToLastLogFile(LogEntryWriterWrapper logEntryWriterWrapper) throws IOException {
PositiveLogFilesBasedLogVersionRepository versionRepository = new PositiveLogFilesBasedLogVersionRepository(logFiles);
LogFiles internalLogFiles = LogFilesBuilder.builder(databaseLayout, fileSystem).withLogVersionRepository(versionRepository).withTransactionIdStore(new SimpleTransactionIdStore()).withStoreId(StoreId.UNKNOWN).withCommandReaderFactory(StorageEngineFactory.defaultStorageEngine().commandReaderFactory()).build();
try (Lifespan lifespan = new Lifespan(internalLogFiles)) {
LogFile transactionLogFile = internalLogFiles.getLogFile();
LogEntryWriter<FlushablePositionAwareChecksumChannel> realLogEntryWriter = transactionLogFile.getTransactionLogWriter().getWriter();
LogEntryWriter<FlushablePositionAwareChecksumChannel> wrappedLogEntryWriter = logEntryWriterWrapper.wrap(realLogEntryWriter);
StaticLogEntryWriterFactory<FlushablePositionAwareChecksumChannel> factory = new StaticLogEntryWriterFactory<>(wrappedLogEntryWriter);
TransactionLogWriter writer = new TransactionLogWriter(realLogEntryWriter.getChannel(), factory);
List<StorageCommand> commands = new ArrayList<>();
commands.add(new Command.PropertyCommand(new PropertyRecord(1), new PropertyRecord(2)));
commands.add(new Command.NodeCommand(new NodeRecord(2), new NodeRecord(3)));
PhysicalTransactionRepresentation transaction = new PhysicalTransactionRepresentation(commands);
transaction.setHeader(new byte[0], 0, 0, 0, 0, ANONYMOUS);
writer.append(transaction, 1000, BASE_TX_CHECKSUM);
}
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class RecoveryCorruptedTransactionLogIT method startWithNotLastTransactionLogHavingZerosInTheEndAndCorruptedLogRecoveryEnabled.
@Test
void startWithNotLastTransactionLogHavingZerosInTheEndAndCorruptedLogRecoveryEnabled() throws IOException {
DatabaseManagementService managementService = databaseFactory.build();
GraphDatabaseAPI database = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
logFiles = buildDefaultLogFiles(getStoreId(database));
generateTransaction(database);
managementService.shutdown();
long originalLogDataLength;
Path firstLogFile;
try (Lifespan lifespan = new Lifespan(logFiles)) {
LogFile logFile = logFiles.getLogFile();
LogPosition readablePosition = getLastReadablePosition(logFile);
firstLogFile = logFiles.getLogFile().getHighestLogFile();
originalLogDataLength = readablePosition.getByteOffset();
logFile.rotate();
// append zeros in the end of previous file causing illegal suffix
try (StoreFileChannel writeChannel = fileSystem.write(firstLogFile)) {
writeChannel.position(writeChannel.size());
for (int i = 0; i < 10; i++) {
writeChannel.writeAll(ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0 }));
}
}
}
startStopDbRecoveryOfCorruptedLogs();
assertEquals(originalLogDataLength, fileSystem.getFileSize(firstLogFile));
assertThat(logProvider).containsMessages("Recovery required from position LogPosition{logVersion=0, byteOffset=" + (996 + txOffsetAfterStart) + "}").assertExceptionForLogMessage("Fail to read transaction log version 0.").hasMessage("Transaction log files with version 0 has 50 unreadable bytes. Was able to read upto " + (996 + txOffsetAfterStart) + " but " + (1046 + txOffsetAfterStart) + " is available.");
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class RecoveryCorruptedTransactionLogIT method startWithoutProblemsIfRotationForcedBeforeFileEndAndCorruptedLogFilesRecoveryEnabled.
@Test
void startWithoutProblemsIfRotationForcedBeforeFileEndAndCorruptedLogFilesRecoveryEnabled() 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();
}
startStopDbRecoveryOfCorruptedLogs();
assertThat(logProvider).doesNotContainMessage("Fail to read transaction log version 0.");
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class LogVersionUpgradeCheckerIT method appendCheckpoint.
private void appendCheckpoint(byte logEntryVersion, boolean removeCheckpointFile) throws IOException {
VersionAwareLogEntryReader logEntryReader = new VersionAwareLogEntryReader(StorageEngineFactory.defaultStorageEngine().commandReaderFactory());
LogFiles logFiles = LogFilesBuilder.activeFilesBuilder(databaseLayout, fileSystem, pageCache).withLogEntryReader(logEntryReader).withStoreId(StoreId.UNKNOWN).build();
if (removeCheckpointFile) {
for (Path file : fileSystem.listFiles(logFiles.logFilesDirectory(), path -> path.getFileName().toString().startsWith(CHECKPOINT_FILE_PREFIX))) {
fileSystem.deleteFile(file);
}
}
try (Lifespan lifespan = new Lifespan(logFiles)) {
LogFile logFile = logFiles.getLogFile();
TransactionLogWriter transactionLogWriter = logFile.getTransactionLogWriter();
var channel = transactionLogWriter.getChannel();
LogPosition logPosition = transactionLogWriter.getCurrentPosition();
// Fake record
channel.put(logEntryVersion).put(LEGACY_CHECK_POINT).putLong(logPosition.getLogVersion()).putLong(logPosition.getByteOffset());
logFile.flush();
}
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class VersionAwareLogEntryReaderIT method correctlyResetPositionWhenEndOfCommandsReached.
@Test
void correctlyResetPositionWhenEndOfCommandsReached() throws IOException {
LogFiles logFiles = LogFilesBuilder.builder(databaseLayout, fs).withLogEntryReader(entryReader).withLogVersionRepository(new SimpleLogVersionRepository()).withTransactionIdStore(new SimpleTransactionIdStore()).withStoreId(StoreId.UNKNOWN).build();
try (Lifespan lifespan = new Lifespan(logFiles)) {
LogPosition logPosition = entryReader.lastPosition();
assertEquals(0L, logPosition.getLogVersion());
// this position in a log file before 0's are actually starting
assertEquals(END_OF_DATA_OFFSET, logPosition.getByteOffset());
for (int i = 0; i < 10; i++) {
assertEquals(END_OF_DATA_OFFSET, getLastReadablePosition(logFiles));
}
}
}
Aggregations