use of org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation 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.CommittedTransactionRepresentation in project neo4j by neo4j.
the class PhysicalLogicalTransactionStoreTest method shouldOpenAndRecoverExistingData.
@Test
public void shouldOpenAndRecoverExistingData() throws Exception {
// GIVEN
TransactionIdStore transactionIdStore = new DeadSimpleTransactionIdStore();
TransactionMetadataCache positionCache = new TransactionMetadataCache(100);
LogHeaderCache logHeaderCache = new LogHeaderCache(10);
final byte[] additionalHeader = new byte[] { 1, 2, 5 };
final int masterId = 2, authorId = 1;
final long timeStarted = 12345, latestCommittedTxWhenStarted = 4545, timeCommitted = timeStarted + 10;
LifeSupport life = new LifeSupport();
final PhysicalLogFiles logFiles = new PhysicalLogFiles(testDir, DEFAULT_NAME, fileSystemRule.get());
Monitor monitor = new Monitors().newMonitor(PhysicalLogFile.Monitor.class);
LogFile logFile = life.add(new PhysicalLogFile(fileSystemRule.get(), logFiles, 1000, transactionIdStore::getLastCommittedTransactionId, mock(LogVersionRepository.class), monitor, logHeaderCache));
life.start();
try {
addATransactionAndRewind(life, logFile, positionCache, transactionIdStore, additionalHeader, masterId, authorId, timeStarted, latestCommittedTxWhenStarted, timeCommitted);
} finally {
life.shutdown();
}
life = new LifeSupport();
final AtomicBoolean recoveryRequired = new AtomicBoolean();
FakeRecoveryVisitor visitor = new FakeRecoveryVisitor(additionalHeader, masterId, authorId, timeStarted, timeCommitted, latestCommittedTxWhenStarted);
logFile = life.add(new PhysicalLogFile(fileSystemRule.get(), logFiles, 1000, transactionIdStore::getLastCommittedTransactionId, mock(LogVersionRepository.class), monitor, logHeaderCache));
LogicalTransactionStore txStore = new PhysicalLogicalTransactionStore(logFile, positionCache, new VersionAwareLogEntryReader<>());
life.add(new BatchingTransactionAppender(logFile, NO_ROTATION, positionCache, transactionIdStore, BYPASS, DATABASE_HEALTH));
life.add(new Recovery(new Recovery.SPI() {
@Override
public void forceEverything() {
}
@Override
public Visitor<CommittedTransactionRepresentation, Exception> startRecovery() {
recoveryRequired.set(true);
return visitor;
}
@Override
public LogPosition getPositionToRecoverFrom() throws IOException {
return LogPosition.start(0);
}
@Override
public TransactionCursor getTransactions(LogPosition position) throws IOException {
return txStore.getTransactions(position);
}
@Override
public void allTransactionsRecovered(CommittedTransactionRepresentation lastRecoveredTransaction, LogPosition positionAfterLastRecoveredTransaction) throws Exception {
}
}, mock(Recovery.Monitor.class)));
// WHEN
try {
life.start();
} finally {
life.shutdown();
}
// THEN
assertEquals(1, visitor.getVisitedTransactions());
assertTrue(recoveryRequired.get());
}
use of org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation in project neo4j by neo4j.
the class PhysicalLogicalTransactionStoreTest method verifyTransaction.
private void verifyTransaction(TransactionIdStore transactionIdStore, TransactionMetadataCache positionCache, byte[] additionalHeader, int masterId, int authorId, long timeStarted, long latestCommittedTxWhenStarted, long timeCommitted, LogicalTransactionStore store) throws IOException {
TransactionMetadata expectedMetadata;
try (TransactionCursor cursor = store.getTransactions(TransactionIdStore.BASE_TX_ID + 1)) {
boolean hasNext = cursor.next();
assertTrue(hasNext);
CommittedTransactionRepresentation tx = cursor.get();
TransactionRepresentation transaction = tx.getTransactionRepresentation();
assertArrayEquals(additionalHeader, transaction.additionalHeader());
assertEquals(masterId, transaction.getMasterId());
assertEquals(authorId, transaction.getAuthorId());
assertEquals(timeStarted, transaction.getTimeStarted());
assertEquals(timeCommitted, transaction.getTimeCommitted());
assertEquals(latestCommittedTxWhenStarted, transaction.getLatestCommittedTxWhenStarted());
expectedMetadata = new TransactionMetadata(masterId, authorId, tx.getStartEntry().getStartPosition(), tx.getStartEntry().checksum(), timeCommitted);
}
positionCache.clear();
TransactionMetadata actualMetadata = store.getMetadataFor(transactionIdStore.getLastCommittedTransactionId());
assertEquals(expectedMetadata, actualMetadata);
}
use of org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation in project neo4j by neo4j.
the class BatchingTransactionAppenderTest method shouldAppendCommittedTransactions.
@Test
public void shouldAppendCommittedTransactions() throws Exception {
// GIVEN
when(logFile.getWriter()).thenReturn(channel);
long nextTxId = 15;
when(transactionIdStore.nextCommittingTransactionId()).thenReturn(nextTxId);
TransactionAppender appender = life.add(new BatchingTransactionAppender(logFile, NO_ROTATION, positionCache, transactionIdStore, BYPASS, databaseHealth));
// WHEN
final byte[] additionalHeader = new byte[] { 1, 2, 5 };
final int masterId = 2, authorId = 1;
final long timeStarted = 12345, latestCommittedTxWhenStarted = nextTxId - 5, timeCommitted = timeStarted + 10;
PhysicalTransactionRepresentation transactionRepresentation = new PhysicalTransactionRepresentation(singleCreateNodeCommand(0));
transactionRepresentation.setHeader(additionalHeader, masterId, authorId, timeStarted, latestCommittedTxWhenStarted, timeCommitted, -1);
LogEntryStart start = new LogEntryStart(0, 0, 0L, latestCommittedTxWhenStarted, null, LogPosition.UNSPECIFIED);
LogEntryCommit commit = new OnePhaseCommit(nextTxId, 0L);
CommittedTransactionRepresentation transaction = new CommittedTransactionRepresentation(start, transactionRepresentation, commit);
appender.append(new TransactionToApply(transactionRepresentation, transaction.getCommitEntry().getTxId()), logAppendEvent);
// THEN
LogEntryReader<ReadableLogChannel> logEntryReader = new VersionAwareLogEntryReader<>();
try (PhysicalTransactionCursor<ReadableLogChannel> reader = new PhysicalTransactionCursor<>(channel, logEntryReader)) {
reader.next();
TransactionRepresentation result = reader.get().getTransactionRepresentation();
assertArrayEquals(additionalHeader, result.additionalHeader());
assertEquals(masterId, result.getMasterId());
assertEquals(authorId, result.getAuthorId());
assertEquals(timeStarted, result.getTimeStarted());
assertEquals(timeCommitted, result.getTimeCommitted());
assertEquals(latestCommittedTxWhenStarted, result.getLatestCommittedTxWhenStarted());
}
}
use of org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation in project neo4j by neo4j.
the class TransactionRecordStateTest method shouldDeleteDynamicLabelsForDeletedNodeForRecoveredTransaction.
@Test
public void shouldDeleteDynamicLabelsForDeletedNodeForRecoveredTransaction() throws Throwable {
// GIVEN a store that has got a node with a dynamic label record
NeoStores store = neoStoresRule.open();
BatchTransactionApplier applier = new NeoStoreBatchTransactionApplier(store, mock(CacheAccessBackDoor.class), LockService.NO_LOCK_SERVICE);
AtomicLong nodeId = new AtomicLong();
AtomicLong dynamicLabelRecordId = new AtomicLong();
apply(applier, transaction(nodeWithDynamicLabelRecord(store, nodeId, dynamicLabelRecordId)));
assertDynamicLabelRecordInUse(store, dynamicLabelRecordId.get(), true);
// WHEN applying a transaction, which has first round-tripped through a log (written then read)
TransactionRepresentation transaction = transaction(deleteNode(store, nodeId.get()));
InMemoryVersionableReadableClosablePositionAwareChannel channel = new InMemoryVersionableReadableClosablePositionAwareChannel();
writeToChannel(transaction, channel);
CommittedTransactionRepresentation recoveredTransaction = readFromChannel(channel);
// and applying that recovered transaction
apply(applier, recoveredTransaction.getTransactionRepresentation());
// THEN should have the dynamic label record should be deleted as well
assertDynamicLabelRecordInUse(store, dynamicLabelRecordId.get(), false);
}
Aggregations