use of org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter in project neo4j by neo4j.
the class LegacyLogEntryWriterTest method shouldWriteAllTheEntryInSeveralCommitsToTheFile.
@Test
public void shouldWriteAllTheEntryInSeveralCommitsToTheFile() throws IOException {
// given
final LogVersionedStoreChannel channel = mock(LogVersionedStoreChannel.class);
final LogEntryWriter logEntryWriter = mock(LogEntryWriter.class);
final LegacyLogEntryWriter writer = new LegacyLogEntryWriter(fs, liftToFactory(logEntryWriter));
final LogEntryStart start1 = new LogEntryStart(0, 1, 2L, 3L, EMPTY_ADDITIONAL_ARRAY, UNSPECIFIED);
final LogEntryCommand command1 = new LogEntryCommand(new Command.NodeCommand(nodeRecord, nodeRecord));
final LogEntryCommit commit1 = new OnePhaseCommit(42L, 43L);
final LogEntryStart start2 = new LogEntryStart(9, 8, 7L, 6L, EMPTY_ADDITIONAL_ARRAY, UNSPECIFIED);
final LogEntryCommand command2 = new LogEntryCommand(new Command.RelationshipCommand(relRecord, relRecord));
final LogEntryCommit commit2 = new OnePhaseCommit(84L, 85L);
// when
IOCursor<LogEntry> cursor = mockCursor(start1, command1, commit1, start2, command2, commit2);
writer.writeAllLogEntries(channel, cursor);
// then
verify(logEntryWriter, times(1)).writeStartEntry(0, 1, 2L, 3L, EMPTY_ADDITIONAL_ARRAY);
final TransactionRepresentation expected1 = new PhysicalTransactionRepresentation(Arrays.asList(command1.getXaCommand()));
verify(logEntryWriter, times(1)).serialize(eq(expected1));
verify(logEntryWriter, times(1)).writeCommitEntry(42L, 43L);
verify(logEntryWriter, times(1)).writeStartEntry(9, 8, 7L, 6L, EMPTY_ADDITIONAL_ARRAY);
final TransactionRepresentation expected2 = new PhysicalTransactionRepresentation(Arrays.asList(command2.getXaCommand()));
verify(logEntryWriter, times(1)).serialize(eq(expected2));
verify(logEntryWriter, times(1)).writeCommitEntry(84L, 85L);
}
use of org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter in project neo4j by neo4j.
the class TransactionLogsRecoveryTest method shouldRecoverExistingData.
@Test
void shouldRecoverExistingData() throws Exception {
LogFile logFile = logFiles.getLogFile();
Path file = logFile.getLogFileForVersion(logVersion);
writeSomeData(file, pair -> {
LogEntryWriter writer = pair.first();
Consumer<LogPositionMarker> consumer = pair.other();
LogPositionMarker marker = new LogPositionMarker();
// last committed tx
int previousChecksum = BASE_TX_CHECKSUM;
consumer.accept(marker);
LogPosition lastCommittedTxPosition = marker.newPosition();
writer.writeStartEntry(2L, 3L, previousChecksum, new byte[0]);
lastCommittedTxStartEntry = new LogEntryStart(2L, 3L, previousChecksum, new byte[0], lastCommittedTxPosition);
previousChecksum = writer.writeCommitEntry(4L, 5L);
lastCommittedTxCommitEntry = new LogEntryCommit(4L, 5L, previousChecksum);
// check point pointing to the previously committed transaction
var checkpointFile = logFiles.getCheckpointFile();
var checkpointAppender = checkpointFile.getCheckpointAppender();
checkpointAppender.checkPoint(LogCheckPointEvent.NULL, lastCommittedTxPosition, Instant.now(), "test");
// tx committed after checkpoint
consumer.accept(marker);
writer.writeStartEntry(6L, 4L, previousChecksum, new byte[0]);
expectedStartEntry = new LogEntryStart(6L, 4L, previousChecksum, new byte[0], marker.newPosition());
previousChecksum = writer.writeCommitEntry(5L, 7L);
expectedCommitEntry = new LogEntryCommit(5L, 7L, previousChecksum);
return true;
});
LifeSupport life = new LifeSupport();
var recoveryRequired = new AtomicBoolean();
var recoveredTransactions = new MutableInt();
RecoveryMonitor monitor = new RecoveryMonitor() {
@Override
public void recoveryRequired(LogPosition recoveryPosition) {
recoveryRequired.set(true);
}
@Override
public void recoveryCompleted(int numberOfRecoveredTransactions, long recoveryTimeInMilliseconds) {
recoveredTransactions.setValue(numberOfRecoveredTransactions);
}
};
try {
StorageEngine storageEngine = mock(StorageEngine.class);
final LogEntryReader reader = logEntryReader();
TransactionMetadataCache metadataCache = new TransactionMetadataCache();
LogicalTransactionStore txStore = new PhysicalLogicalTransactionStore(logFiles, metadataCache, reader, monitors, false);
CorruptedLogsTruncator logPruner = new CorruptedLogsTruncator(storeDir, logFiles, fileSystem, INSTANCE);
monitors.addMonitorListener(monitor);
life.add(new TransactionLogsRecovery(new DefaultRecoveryService(storageEngine, transactionIdStore, txStore, versionRepository, logFiles, NO_MONITOR, mock(Log.class), false) {
private int nr;
@Override
public RecoveryApplier getRecoveryApplier(TransactionApplicationMode mode, PageCacheTracer cacheTracer, String tracerTag) {
RecoveryApplier actual = super.getRecoveryApplier(mode, cacheTracer, tracerTag);
if (mode == TransactionApplicationMode.REVERSE_RECOVERY) {
return actual;
}
return new RecoveryApplier() {
@Override
public void close() throws Exception {
actual.close();
}
@Override
public boolean visit(CommittedTransactionRepresentation tx) throws Exception {
actual.visit(tx);
switch(nr++) {
case 0:
assertEquals(lastCommittedTxStartEntry, tx.getStartEntry());
assertEquals(lastCommittedTxCommitEntry, tx.getCommitEntry());
break;
case 1:
assertEquals(expectedStartEntry, tx.getStartEntry());
assertEquals(expectedCommitEntry, tx.getCommitEntry());
break;
default:
fail("Too many recovered transactions");
}
return false;
}
};
}
}, logPruner, schemaLife, monitor, ProgressReporter.SILENT, false, EMPTY_CHECKER, NULL));
life.start();
assertTrue(recoveryRequired.get());
assertEquals(2, recoveredTransactions.getValue());
} finally {
life.shutdown();
}
}
use of org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter in project neo4j by neo4j.
the class TransactionLogsRecoveryTest method writeSomeDataWithVersion.
private void writeSomeDataWithVersion(Path file, Visitor<Pair<LogEntryWriter, Consumer<LogPositionMarker>>, IOException> visitor, KernelVersion version) throws IOException {
try (LogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(fileSystem.write(file), logVersion, CURRENT_LOG_FORMAT_VERSION, file, EMPTY_ACCESSOR);
PositionAwarePhysicalFlushableChecksumChannel writableLogChannel = new PositionAwarePhysicalFlushableChecksumChannel(versionedStoreChannel, new HeapScopedBuffer(1, KibiByte, INSTANCE))) {
writeLogHeader(writableLogChannel, new LogHeader(logVersion, 2L, StoreId.UNKNOWN));
writableLogChannel.beginChecksum();
Consumer<LogPositionMarker> consumer = marker -> {
try {
writableLogChannel.getCurrentPosition(marker);
} catch (IOException e) {
throw new RuntimeException(e);
}
};
LogEntryWriter first = new LogEntryWriter(writableLogChannel, version);
visitor.visit(Pair.of(first, consumer));
}
}
use of org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter in project neo4j by neo4j.
the class TransactionLogsRecoveryTest method shouldSeeThatACleanDatabaseShouldNotRequireRecovery.
@ParameterizedTest(name = "{0}")
@CsvSource({ "separateCheckpoints,true", "legacyCheckpoints,false" })
void shouldSeeThatACleanDatabaseShouldNotRequireRecovery(String name, boolean useSeparateLogFiles) throws Exception {
Path file = logFiles.getLogFile().getLogFileForVersion(logVersion);
LogPositionMarker marker = new LogPositionMarker();
writeSomeDataWithVersion(file, pair -> {
LogEntryWriter writer = pair.first();
Consumer<LogPositionMarker> consumer = pair.other();
// last committed tx
consumer.accept(marker);
writer.writeStartEntry(2L, 3L, BASE_TX_CHECKSUM, new byte[0]);
writer.writeCommitEntry(4L, 5L);
// check point
consumer.accept(marker);
if (useSeparateLogFiles) {
var checkpointFile = logFiles.getCheckpointFile();
var checkpointAppender = checkpointFile.getCheckpointAppender();
checkpointAppender.checkPoint(LogCheckPointEvent.NULL, marker.newPosition(), Instant.now(), "test");
} else {
writer.writeLegacyCheckPointEntry(marker.newPosition());
}
return true;
}, useSeparateLogFiles ? KernelVersion.LATEST : KernelVersion.V4_0);
LifeSupport life = new LifeSupport();
RecoveryMonitor monitor = mock(RecoveryMonitor.class);
try {
StorageEngine storageEngine = mock(StorageEngine.class);
final LogEntryReader reader = logEntryReader();
TransactionMetadataCache metadataCache = new TransactionMetadataCache();
LogicalTransactionStore txStore = new PhysicalLogicalTransactionStore(logFiles, metadataCache, reader, monitors, false);
CorruptedLogsTruncator logPruner = new CorruptedLogsTruncator(storeDir, logFiles, fileSystem, INSTANCE);
monitors.addMonitorListener(new RecoveryMonitor() {
@Override
public void recoveryRequired(LogPosition recoveryPosition) {
fail("Recovery should not be required");
}
});
life.add(new TransactionLogsRecovery(new DefaultRecoveryService(storageEngine, transactionIdStore, txStore, versionRepository, logFiles, NO_MONITOR, mock(Log.class), false), logPruner, schemaLife, monitor, ProgressReporter.SILENT, false, EMPTY_CHECKER, NULL));
life.start();
verifyNoInteractions(monitor);
} finally {
life.shutdown();
}
}
use of org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter in project neo4j by neo4j.
the class TransactionLogsRecoveryTest method shouldTruncateLogAfterLastCompleteTransactionAfterSuccessfulRecovery.
@Test
void shouldTruncateLogAfterLastCompleteTransactionAfterSuccessfulRecovery() throws Exception {
// GIVEN
Path file = logFiles.getLogFile().getLogFileForVersion(logVersion);
final LogPositionMarker marker = new LogPositionMarker();
writeSomeData(file, pair -> {
LogEntryWriter writer = pair.first();
Consumer<LogPositionMarker> consumer = pair.other();
// last committed tx
int previousChecksum = BASE_TX_CHECKSUM;
writer.writeStartEntry(2L, 3L, previousChecksum, new byte[0]);
previousChecksum = writer.writeCommitEntry(4L, 5L);
// incomplete tx
// <-- marker has the last good position
consumer.accept(marker);
writer.writeStartEntry(5L, 4L, previousChecksum, new byte[0]);
return true;
});
// WHEN
boolean recoveryRequired = recover(storeDir, logFiles);
// THEN
assertTrue(recoveryRequired);
assertEquals(marker.getByteOffset(), Files.size(file));
}
Aggregations