Search in sources :

Example 16 with RaftLogEntry

use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.

the class SegmentedRaftLogPartialEntryRecoveryTest method shouldNotAppendAtTheEndOfLogFileWithIncompleteEntries.

@Test
public void shouldNotAppendAtTheEndOfLogFileWithIncompleteEntries() throws Throwable {
    // Given
    // we use a RaftLog to create a raft log file and then we will chop some bits off the end
    SegmentedRaftLog raftLog = createRaftLog(100_000);
    raftLog.start();
    raftLog.append(new RaftLogEntry(4, new NewLeaderBarrier()));
    raftLog.stop();
    // We use a temporary RecoveryProtocol to get the file to chop
    RecoveryProtocol recovery = createRecoveryProtocol();
    State recoveryState = recovery.run();
    String logFilename = recoveryState.segments.last().getFilename();
    File logFile = new File(logDirectory, logFilename);
    StoreChannel lastFile = fsRule.get().open(logFile, "rw");
    long currentSize = lastFile.size();
    lastFile.close();
    // When
    // We induce an incomplete entry at the end of the last file
    lastFile = fsRule.get().open(logFile, "rw");
    lastFile.truncate(currentSize - 1);
    lastFile.close();
    // We start the raft log again, on the previous log file with truncated last entry.
    raftLog = createRaftLog(100_000);
    //  Recovery will run here
    raftLog.start();
    // Append an entry
    raftLog.append(new RaftLogEntry(4, new NewLeaderBarrier()));
    // The log should report as containing only the last entry we've appended
    try (RaftLogCursor entryCursor = raftLog.getEntryCursor(0)) {
        // There should be exactly one entry, of type NewLeaderBarrier
        assertTrue(entryCursor.next());
        RaftLogEntry raftLogEntry = entryCursor.get();
        assertEquals(NewLeaderBarrier.class, raftLogEntry.content().getClass());
        assertFalse(entryCursor.next());
    }
}
Also used : RaftLogCursor(org.neo4j.causalclustering.core.consensus.log.RaftLogCursor) StoreChannel(org.neo4j.io.fs.StoreChannel) NewLeaderBarrier(org.neo4j.causalclustering.core.consensus.NewLeaderBarrier) File(java.io.File) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 17 with RaftLogEntry

use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.

the class RaftMembershipManagerTest method membershipManagerShouldRevertToOldMembershipSetAfterTruncationCausesLossOfAllAppendedMembershipSets.

@Test
public void membershipManagerShouldRevertToOldMembershipSetAfterTruncationCausesLossOfAllAppendedMembershipSets() throws Exception {
    // given
    final InMemoryRaftLog raftLog = new InMemoryRaftLog();
    RaftMembershipManager membershipManager = lifeRule.add(raftMembershipManager(raftLog));
    // when
    List<RaftLogCommand> logCommands = asList(new AppendLogEntry(0, new RaftLogEntry(0, new RaftTestGroup(1, 2, 3, 4))), new AppendLogEntry(1, new RaftLogEntry(0, new RaftTestGroup(1, 2, 3, 5))), new TruncateLogCommand(1));
    for (RaftLogCommand logCommand : logCommands) {
        logCommand.applyTo(raftLog, log);
    }
    membershipManager.processLog(0, logCommands);
    // then
    assertEquals(new RaftTestGroup(1, 2, 3, 4).getMembers(), membershipManager.votingMembers());
    assertFalse(membershipManager.uncommittedMemberChangeInLog());
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) TruncateLogCommand(org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) RaftLogCommand(org.neo4j.causalclustering.core.consensus.outcome.RaftLogCommand) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 18 with RaftLogEntry

use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.

the class RaftMembershipManagerTest method membershipManagerShouldRevertToEarlierAppendedMembershipSetAfterTruncationCausesLossOfLastAppened.

@Test
public void membershipManagerShouldRevertToEarlierAppendedMembershipSetAfterTruncationCausesLossOfLastAppened() throws Exception {
    // given
    final InMemoryRaftLog raftLog = new InMemoryRaftLog();
    RaftMembershipManager membershipManager = lifeRule.add(raftMembershipManager(raftLog));
    // when
    List<RaftLogCommand> logCommands = asList(new AppendLogEntry(0, new RaftLogEntry(0, new RaftTestGroup(1, 2, 3, 4))), new AppendLogEntry(1, new RaftLogEntry(0, new RaftTestGroup(1, 2, 3, 5))), new AppendLogEntry(2, new RaftLogEntry(0, new RaftTestGroup(1, 2, 3, 6))), new TruncateLogCommand(2));
    for (RaftLogCommand logCommand : logCommands) {
        logCommand.applyTo(raftLog, log);
    }
    membershipManager.processLog(0, logCommands);
    // then
    assertEquals(new RaftTestGroup(1, 2, 3, 5).getMembers(), membershipManager.votingMembers());
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) TruncateLogCommand(org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) RaftLogCommand(org.neo4j.causalclustering.core.consensus.outcome.RaftLogCommand) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 19 with RaftLogEntry

use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.

the class RaftMembershipManagerTest method membershipManagerShouldUseLatestAppendedMembershipSetEntries.

@Test
public void membershipManagerShouldUseLatestAppendedMembershipSetEntries() throws Exception {
    // given
    final InMemoryRaftLog log = new InMemoryRaftLog();
    RaftMembershipManager membershipManager = lifeRule.add(raftMembershipManager(log));
    // when
    membershipManager.processLog(0, asList(new AppendLogEntry(0, new RaftLogEntry(0, new RaftTestGroup(1, 2, 3, 4))), new AppendLogEntry(1, new RaftLogEntry(0, new RaftTestGroup(1, 2, 3, 5)))));
    // then
    assertEquals(new RaftTestGroup(1, 2, 3, 5).getMembers(), membershipManager.votingMembers());
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 20 with RaftLogEntry

use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.

the class TruncateLogCommandTest method applyTo.

@Test
public void applyTo() throws Exception {
    //Test that truncate commands correctly remove entries from the cache.
    //given
    AssertableLogProvider logProvider = new AssertableLogProvider();
    Log log = logProvider.getLog(getClass());
    long fromIndex = 2L;
    TruncateLogCommand truncateLogCommand = new TruncateLogCommand(fromIndex);
    InFlightMap<RaftLogEntry> inFlightMap = new InFlightMap<>();
    inFlightMap.put(0L, new RaftLogEntry(0L, valueOf(0)));
    inFlightMap.put(1L, new RaftLogEntry(1L, valueOf(1)));
    inFlightMap.put(2L, new RaftLogEntry(2L, valueOf(2)));
    inFlightMap.put(3L, new RaftLogEntry(3L, valueOf(3)));
    //when
    truncateLogCommand.applyTo(inFlightMap, log);
    //then
    assertNotNull(inFlightMap.get(0L));
    assertNotNull(inFlightMap.get(1L));
    assertNull(inFlightMap.get(2L));
    assertNull(inFlightMap.get(3L));
    logProvider.assertAtLeastOnce(inLog(getClass()).debug("Start truncating in-flight-map from index %d. Current map:%n%s", fromIndex, inFlightMap));
}
Also used : Log(org.neo4j.logging.Log) AssertableLogProvider.inLog(org.neo4j.logging.AssertableLogProvider.inLog) NullLog(org.neo4j.logging.NullLog) InFlightMap(org.neo4j.causalclustering.core.consensus.log.segmented.InFlightMap) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Aggregations

RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)87 Test (org.junit.Test)69 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)27 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)25 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)20 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)12 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)10 ReadableRaftState (org.neo4j.causalclustering.core.consensus.state.ReadableRaftState)10 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)8 NewLeaderBarrier (org.neo4j.causalclustering.core.consensus.NewLeaderBarrier)7 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)7 MemberId (org.neo4j.causalclustering.identity.MemberId)7 InOrder (org.mockito.InOrder)6 RaftLogCursor (org.neo4j.causalclustering.core.consensus.log.RaftLogCursor)6 ReadableRaftLog (org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog)5 RaftTestGroup (org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup)5 BatchAppendLogEntries (org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries)5 ShipCommand (org.neo4j.causalclustering.core.consensus.outcome.ShipCommand)5 TruncateLogCommand (org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand)5 File (java.io.File)4