Search in sources :

Example 1 with OnePhaseCommit

use of org.neo4j.kernel.impl.transaction.log.entry.OnePhaseCommit in project neo4j by neo4j.

the class BatchingTransactionAppenderTest method shouldNotAppendCommittedTransactionsWhenTooFarAhead.

@Test
public void shouldNotAppendCommittedTransactionsWhenTooFarAhead() throws Exception {
    // GIVEN
    InMemoryClosableChannel channel = new InMemoryClosableChannel();
    when(logFile.getWriter()).thenReturn(channel);
    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 = 4545, timeCommitted = timeStarted + 10;
    PhysicalTransactionRepresentation transactionRepresentation = new PhysicalTransactionRepresentation(singleCreateNodeCommand(0));
    transactionRepresentation.setHeader(additionalHeader, masterId, authorId, timeStarted, latestCommittedTxWhenStarted, timeCommitted, -1);
    when(transactionIdStore.getLastCommittedTransactionId()).thenReturn(latestCommittedTxWhenStarted);
    LogEntryStart start = new LogEntryStart(0, 0, 0L, latestCommittedTxWhenStarted, null, LogPosition.UNSPECIFIED);
    LogEntryCommit commit = new OnePhaseCommit(latestCommittedTxWhenStarted + 2, 0L);
    CommittedTransactionRepresentation transaction = new CommittedTransactionRepresentation(start, transactionRepresentation, commit);
    try {
        appender.append(new TransactionToApply(transaction.getTransactionRepresentation(), transaction.getCommitEntry().getTxId()), logAppendEvent);
        fail("should have thrown");
    } catch (Throwable e) {
        assertThat(e.getMessage(), containsString("to be applied, but appending it ended up generating an"));
    }
}
Also used : LogEntryStart(org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart) TransactionToApply(org.neo4j.kernel.impl.api.TransactionToApply) CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) LogEntryCommit(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommit) OnePhaseCommit(org.neo4j.kernel.impl.transaction.log.entry.OnePhaseCommit) Test(org.junit.Test)

Example 2 with OnePhaseCommit

use of org.neo4j.kernel.impl.transaction.log.entry.OnePhaseCommit in project neo4j by neo4j.

the class LegacyLogEntryWriterTest method shouldWriteAllTheEntryInACommitToTheFile.

@Test
public void shouldWriteAllTheEntryInACommitToTheFile() 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 start = new LogEntryStart(0, 1, 2L, 3L, EMPTY_ADDITIONAL_ARRAY, UNSPECIFIED);
    final LogEntryCommand command = new LogEntryCommand(new Command.NodeCommand(nodeRecord, nodeRecord));
    final LogEntryCommit commit = new OnePhaseCommit(42L, 43L);
    // when
    final IOCursor<LogEntry> cursor = mockCursor(start, command, commit);
    writer.writeAllLogEntries(channel, cursor);
    // then
    verify(logEntryWriter, times(1)).writeStartEntry(0, 1, 2L, 3L, EMPTY_ADDITIONAL_ARRAY);
    final TransactionRepresentation expected = new PhysicalTransactionRepresentation(Arrays.asList(command.getXaCommand()));
    verify(logEntryWriter, times(1)).serialize(eq(expected));
    verify(logEntryWriter, times(1)).writeCommitEntry(42L, 43L);
}
Also used : LogEntryStart(org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart) LogVersionedStoreChannel(org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel) LogEntryCommand(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand) TransactionRepresentation(org.neo4j.kernel.impl.transaction.TransactionRepresentation) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation) Command(org.neo4j.kernel.impl.transaction.command.Command) LogEntryCommand(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand) LogEntryCommit(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommit) LogEntryWriter(org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter) OnePhaseCommit(org.neo4j.kernel.impl.transaction.log.entry.OnePhaseCommit) LogEntry(org.neo4j.kernel.impl.transaction.log.entry.LogEntry) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation) Test(org.junit.Test)

Example 3 with OnePhaseCommit

use of org.neo4j.kernel.impl.transaction.log.entry.OnePhaseCommit in project neo4j by neo4j.

the class TxPullResponseEncodeDecodeTest method newCommittedTransactionRepresentation.

private CommittedTransactionRepresentation newCommittedTransactionRepresentation() {
    final long arbitraryRecordId = 27L;
    Command.NodeCommand command = new Command.NodeCommand(new NodeRecord(arbitraryRecordId), new NodeRecord(arbitraryRecordId));
    PhysicalTransactionRepresentation physicalTransactionRepresentation = new PhysicalTransactionRepresentation(asList(new LogEntryCommand(command).getXaCommand()));
    physicalTransactionRepresentation.setHeader(new byte[] {}, 0, 0, 0, 0, 0, 0);
    LogEntryStart startEntry = new LogEntryStart(0, 0, 0L, 0L, new byte[] {}, LogPosition.UNSPECIFIED);
    OnePhaseCommit commitEntry = new OnePhaseCommit(42, 0);
    return new CommittedTransactionRepresentation(startEntry, physicalTransactionRepresentation, commitEntry);
}
Also used : LogEntryStart(org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart) NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) LogEntryCommand(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand) Command(org.neo4j.kernel.impl.transaction.command.Command) LogEntryCommand(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand) OnePhaseCommit(org.neo4j.kernel.impl.transaction.log.entry.OnePhaseCommit) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)

Example 4 with OnePhaseCommit

use of org.neo4j.kernel.impl.transaction.log.entry.OnePhaseCommit 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();
    }
}
Also used : DefaultRecoverySPI(org.neo4j.kernel.recovery.DefaultRecoverySPI) Visitor(org.neo4j.helpers.collection.Visitor) PhysicalLogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.PhysicalLogicalTransactionStore) LogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore) StorageEngine(org.neo4j.storageengine.api.StorageEngine) Recovery(org.neo4j.kernel.recovery.Recovery) PhysicalLogFiles(org.neo4j.kernel.impl.transaction.log.PhysicalLogFiles) LogPositionMarker(org.neo4j.kernel.impl.transaction.log.LogPositionMarker) ReadableClosablePositionAwareChannel(org.neo4j.kernel.impl.transaction.log.ReadableClosablePositionAwareChannel) LogFile(org.neo4j.kernel.impl.transaction.log.LogFile) PhysicalLogFile(org.neo4j.kernel.impl.transaction.log.PhysicalLogFile) Consumer(java.util.function.Consumer) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) VersionAwareLogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader) LogEntryWriter(org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter) OnePhaseCommit(org.neo4j.kernel.impl.transaction.log.entry.OnePhaseCommit) Pair(org.neo4j.helpers.collection.Pair) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition) CheckPoint(org.neo4j.kernel.impl.transaction.log.entry.CheckPoint) LogEntryStart(org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart) PhysicalLogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.PhysicalLogicalTransactionStore) LatestCheckPointFinder(org.neo4j.kernel.recovery.LatestCheckPointFinder) CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) InOrder(org.mockito.InOrder) TransactionMetadataCache(org.neo4j.kernel.impl.transaction.log.TransactionMetadataCache) IOException(java.io.IOException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LogFile(org.neo4j.kernel.impl.transaction.log.LogFile) PhysicalLogFile(org.neo4j.kernel.impl.transaction.log.PhysicalLogFile) File(java.io.File) LogHeaderCache(org.neo4j.kernel.impl.transaction.log.LogHeaderCache) PhysicalLogFile(org.neo4j.kernel.impl.transaction.log.PhysicalLogFile) Test(org.junit.Test)

Example 5 with OnePhaseCommit

use of org.neo4j.kernel.impl.transaction.log.entry.OnePhaseCommit 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();
}
Also used : LogEntryStart(org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart) NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) ReadableLogChannel(org.neo4j.kernel.impl.transaction.log.ReadableLogChannel) LogEntryCommand(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand) Command(org.neo4j.kernel.impl.transaction.command.Command) LogEntryCommand(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand) OnePhaseCommit(org.neo4j.kernel.impl.transaction.log.entry.OnePhaseCommit) IdentifiableLogEntry(org.neo4j.kernel.impl.transaction.log.entry.IdentifiableLogEntry) LogEntry(org.neo4j.kernel.impl.transaction.log.entry.LogEntry) IdentifiableLogEntry(org.neo4j.kernel.impl.transaction.log.entry.IdentifiableLogEntry) Test(org.junit.Test)

Aggregations

LogEntryStart (org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart)7 OnePhaseCommit (org.neo4j.kernel.impl.transaction.log.entry.OnePhaseCommit)7 Test (org.junit.Test)6 CommittedTransactionRepresentation (org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation)4 Command (org.neo4j.kernel.impl.transaction.command.Command)4 LogEntryCommand (org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand)4 LogEntryCommit (org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommit)4 TransactionRepresentation (org.neo4j.kernel.impl.transaction.TransactionRepresentation)3 PhysicalTransactionRepresentation (org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)3 LogEntry (org.neo4j.kernel.impl.transaction.log.entry.LogEntry)3 LogEntryWriter (org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter)3 TransactionToApply (org.neo4j.kernel.impl.api.TransactionToApply)2 NodeRecord (org.neo4j.kernel.impl.store.record.NodeRecord)2 LogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel)2 VersionAwareLogEntryReader (org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader)2 File (java.io.File)1 IOException (java.io.IOException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Consumer (java.util.function.Consumer)1 InOrder (org.mockito.InOrder)1