use of org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart in project neo4j by neo4j.
the class CommittedTransactionSerializer method visit.
@Override
public boolean visit(CommittedTransactionRepresentation tx) throws IOException {
LogEntryStart startEntry = tx.getStartEntry();
writer.writeStartEntry(startEntry.getMasterId(), startEntry.getLocalId(), startEntry.getTimeWritten(), startEntry.getLastCommittedTxWhenTransactionStarted(), startEntry.getAdditionalHeader());
writer.serialize(tx.getTransactionRepresentation());
LogEntryCommit commitEntry = tx.getCommitEntry();
writer.writeCommitEntry(commitEntry.getTxId(), commitEntry.getTimeWritten());
return false;
}
use of org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart in project neo4j by neo4j.
the class LegacyLogs method getTransactionInformation.
public Optional<TransactionId> getTransactionInformation(File storeDir, long transactionId) throws IOException {
List<File> logFiles = Arrays.asList(fs.listFiles(storeDir, versionedLegacyLogFilesFilter));
logFiles.sort(NEWEST_FIRST);
for (File file : logFiles) {
Pair<LogHeader, IOCursor<LogEntry>> pair = reader.openReadableChannel(file);
boolean hadAnyTransactions = false;
try (IOCursor<LogEntry> cursor = pair.other()) {
// The log entries will come sorted from this cursor, so no need to keep track of identifiers and such.
LogEntryStart startEntry = null;
while (cursor.next()) {
LogEntry logEntry = cursor.get();
if (logEntry instanceof LogEntryStart) {
startEntry = (LogEntryStart) logEntry;
} else if (logEntry instanceof LogEntryCommit) {
hadAnyTransactions = true;
LogEntryCommit commitEntry = logEntry.as();
if (commitEntry.getTxId() == transactionId) {
return Optional.of(new TransactionId(transactionId, startEntry.checksum(), commitEntry.getTimeWritten()));
}
}
}
}
if (hadAnyTransactions) {
// No need to go further back than this. We're looking for the last transaction
break;
}
}
return Optional.empty();
}
use of org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart in project neo4j by neo4j.
the class LegacyLogEntryWriter method writeAllLogEntries.
public void writeAllLogEntries(LogVersionedStoreChannel channel, IOCursor<LogEntry> cursor) throws IOException {
try (PositionAwarePhysicalFlushableChannel writable = new PositionAwarePhysicalFlushableChannel(channel)) {
final LogEntryWriter writer = factory.apply(writable);
List<StorageCommand> commands = new ArrayList<>();
while (cursor.next()) {
LogEntry entry = cursor.get();
if (entry instanceof LogEntryStart) {
final LogEntryStart startEntry = entry.as();
writer.writeStartEntry(startEntry.getMasterId(), startEntry.getLocalId(), startEntry.getTimeWritten(), startEntry.getLastCommittedTxWhenTransactionStarted(), startEntry.getAdditionalHeader());
} else if (entry instanceof LogEntryCommit) {
if (!commands.isEmpty()) {
writer.serialize(new PhysicalTransactionRepresentation(commands));
commands = new ArrayList<>();
}
final LogEntryCommit commitEntry = (LogEntryCommit) entry;
writer.writeCommitEntry(commitEntry.getTxId(), commitEntry.getTimeWritten());
} else if (entry instanceof LogEntryCommand) {
commands.add(((LogEntryCommand) entry).getXaCommand());
} else {
throw new IllegalStateException("Unknown entry: " + entry);
}
}
}
}
use of org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart in project neo4j by neo4j.
the class RecoveryTest method shouldRecoverExistingData.
@Test
public void shouldRecoverExistingData() throws Exception {
final PhysicalLogFiles logFiles = new PhysicalLogFiles(directory.directory(), "log", fileSystemRule.get());
File file = logFiles.getLogFileForVersion(logVersion);
writeSomeData(file, new Visitor<Pair<LogEntryWriter, Consumer<LogPositionMarker>>, IOException>() {
@Override
public boolean visit(Pair<LogEntryWriter, Consumer<LogPositionMarker>> pair) throws IOException {
LogEntryWriter writer = pair.first();
Consumer<LogPositionMarker> consumer = pair.other();
LogPositionMarker marker = new LogPositionMarker();
// last committed tx
consumer.accept(marker);
LogPosition lastCommittedTxPosition = marker.newPosition();
writer.writeStartEntry(0, 1, 2L, 3L, new byte[0]);
lastCommittedTxStartEntry = new LogEntryStart(0, 1, 2L, 3L, new byte[0], lastCommittedTxPosition);
writer.writeCommitEntry(4L, 5L);
lastCommittedTxCommitEntry = new OnePhaseCommit(4L, 5L);
// check point pointing to the previously committed transaction
writer.writeCheckPointEntry(lastCommittedTxPosition);
expectedCheckPointEntry = new CheckPoint(lastCommittedTxPosition);
// tx committed after checkpoint
consumer.accept(marker);
writer.writeStartEntry(0, 1, 6L, 4L, new byte[0]);
expectedStartEntry = new LogEntryStart(0, 1, 6L, 4L, new byte[0], marker.newPosition());
writer.writeCommitEntry(5L, 7L);
expectedCommitEntry = new OnePhaseCommit(5L, 7L);
return true;
}
});
LifeSupport life = new LifeSupport();
Recovery.Monitor monitor = mock(Recovery.Monitor.class);
final AtomicBoolean recoveryRequired = new AtomicBoolean();
try {
StorageEngine storageEngine = mock(StorageEngine.class);
final LogEntryReader<ReadableClosablePositionAwareChannel> reader = new VersionAwareLogEntryReader<>();
LatestCheckPointFinder finder = new LatestCheckPointFinder(logFiles, fileSystemRule.get(), reader);
LogHeaderCache logHeaderCache = new LogHeaderCache(10);
TransactionMetadataCache metadataCache = new TransactionMetadataCache(100);
LogFile logFile = life.add(new PhysicalLogFile(fileSystemRule.get(), logFiles, 50, () -> transactionIdStore.getLastCommittedTransactionId(), logVersionRepository, mock(PhysicalLogFile.Monitor.class), logHeaderCache));
LogicalTransactionStore txStore = new PhysicalLogicalTransactionStore(logFile, metadataCache, reader);
life.add(new Recovery(new DefaultRecoverySPI(storageEngine, logFiles, fileSystemRule.get(), logVersionRepository, finder, transactionIdStore, txStore, NO_MONITOR) {
private int nr = 0;
@Override
public Visitor<CommittedTransactionRepresentation, Exception> startRecovery() {
recoveryRequired.set(true);
final Visitor<CommittedTransactionRepresentation, Exception> actual = super.startRecovery();
return new Visitor<CommittedTransactionRepresentation, Exception>() {
@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;
}
};
}
}, monitor));
life.start();
InOrder order = inOrder(monitor);
order.verify(monitor, times(1)).recoveryRequired(any(LogPosition.class));
order.verify(monitor, times(1)).recoveryCompleted(2);
assertTrue(recoveryRequired.get());
} finally {
life.shutdown();
}
}
use of org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart in project neo4j by neo4j.
the class LegacyLogEntryReaderTest method shouldReadTheEntries.
@Test
public void shouldReadTheEntries() throws IOException {
// given
final LogEntry start = new LogEntryStart(0, 1, 2, 3, EMPTY_ADDITIONAL_ARRAY, UNSPECIFIED);
NodeRecord record = new NodeRecord(42);
final LogEntry command = new LogEntryCommand(new Command.NodeCommand(record, record));
final LogEntry commit = new OnePhaseCommit(42, 43);
final LogEntryReader<ReadableLogChannel> logEntryReader = mock(LogEntryReader.class);
when(logEntryReader.readLogEntry(any(ReadableLogChannel.class))).thenReturn(new IdentifiableLogEntry(start, 1), new IdentifiableLogEntry(command, 1), new IdentifiableLogEntry(commit, 1), null);
final LegacyLogEntryReader reader = new LegacyLogEntryReader(fs, from -> logEntryReader);
// when
final IOCursor<LogEntry> cursor = reader.openReadableChannel(input).other();
// then
assertTrue(cursor.next());
assertEquals(start, cursor.get());
assertTrue(cursor.next());
assertEquals(command, cursor.get());
assertTrue(cursor.next());
assertEquals(commit, cursor.get());
assertFalse(cursor.next());
cursor.close();
}
Aggregations