Search in sources :

Example 21 with InMemoryRaftLog

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

the class AppendEntriesRequestTest method shouldCommitEntry.

@Test
public void shouldCommitEntry() throws Exception {
    // given
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    RaftState state = raftState().entryLog(raftLog).myself(myself).build();
    long leaderTerm = state.term() + leaderTermDifference;
    raftLog.append(new RaftLogEntry(leaderTerm, content()));
    // when
    Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(raftLog.appendIndex()).prevLogTerm(leaderTerm).leaderCommit(0).build(), state, log());
    // then
    assertTrue(((Response) messageFor(outcome, leader)).success());
    assertThat(outcome.getCommitIndex(), Matchers.equalTo(0L));
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 22 with InMemoryRaftLog

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

the class RaftLogShipperTest method setup.

@Before
public void setup() {
    // defaults
    outbound = new OutboundMessageCollector();
    raftLog = new InMemoryRaftLog();
    clock = Clocks.systemClock();
    leader = member(0);
    follower = member(1);
    leaderTerm = 0;
    leaderCommit = 0;
    retryTimeMillis = 100000;
    logProvider = mock(LogProvider.class);
    log = mock(Log.class);
    when(logProvider.getLog(RaftLogShipper.class)).thenReturn(log);
}
Also used : LogProvider(org.neo4j.logging.LogProvider) OutboundMessageCollector(org.neo4j.causalclustering.core.consensus.OutboundMessageCollector) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Log(org.neo4j.logging.Log) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) Before(org.junit.Before)

Example 23 with InMemoryRaftLog

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

the class RaftStateTest method shouldUpdateCacheState.

@Test
public void shouldUpdateCacheState() throws Exception {
    //Test that updates applied to the raft state will be refelcted in the entry cache.
    //given
    InFlightMap<RaftLogEntry> cache = new InFlightMap<>();
    RaftState raftState = new RaftState(member(0), new InMemoryStateStorage<>(new TermState()), new FakeMembership(), new InMemoryRaftLog(), new InMemoryStateStorage<>(new VoteState()), cache, NullLogProvider.getInstance());
    List<RaftLogCommand> logCommands = new LinkedList<RaftLogCommand>() {

        {
            add(new AppendLogEntry(1, new RaftLogEntry(0L, valueOf(0))));
            add(new AppendLogEntry(2, new RaftLogEntry(0L, valueOf(1))));
            add(new AppendLogEntry(3, new RaftLogEntry(0L, valueOf(2))));
            add(new AppendLogEntry(4, new RaftLogEntry(0L, valueOf(4))));
            add(new TruncateLogCommand(3));
            add(new AppendLogEntry(3, new RaftLogEntry(0L, valueOf(5))));
        }
    };
    Outcome raftTestMemberOutcome = new Outcome(CANDIDATE, 0, null, -1, null, emptySet(), -1, initialFollowerStates(), true, logCommands, emptyOutgoingMessages(), emptySet(), -1, emptySet());
    //when
    raftState.update(raftTestMemberOutcome);
    //then
    assertNotNull(cache.get(1L));
    assertNotNull(cache.get(2L));
    assertNotNull(cache.get(3L));
    assertEquals(valueOf(5), cache.get(3L).content());
    assertNull(cache.get(4L));
}
Also used : TruncateLogCommand(org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand) LinkedList(java.util.LinkedList) RaftLogCommand(org.neo4j.causalclustering.core.consensus.outcome.RaftLogCommand) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) VoteState(org.neo4j.causalclustering.core.consensus.vote.VoteState) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) InFlightMap(org.neo4j.causalclustering.core.consensus.log.segmented.InFlightMap) TermState(org.neo4j.causalclustering.core.consensus.term.TermState) Test(org.junit.Test)

Example 24 with InMemoryRaftLog

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

the class RaftStateTest method shouldRemoveFollowerStateAfterBecomingLeader.

@Test
public void shouldRemoveFollowerStateAfterBecomingLeader() throws Exception {
    // given
    RaftState raftState = new RaftState(member(0), new InMemoryStateStorage<>(new TermState()), new FakeMembership(), new InMemoryRaftLog(), new InMemoryStateStorage<>(new VoteState()), new InFlightMap<>(), NullLogProvider.getInstance());
    raftState.update(new Outcome(CANDIDATE, 1, null, -1, null, emptySet(), -1, initialFollowerStates(), true, emptyLogCommands(), emptyOutgoingMessages(), emptySet(), -1, emptySet()));
    // when
    raftState.update(new Outcome(CANDIDATE, 1, null, -1, null, emptySet(), -1, new FollowerStates<>(), true, emptyLogCommands(), emptyOutgoingMessages(), emptySet(), -1, emptySet()));
    // then
    assertEquals(0, raftState.followerStates().size());
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) VoteState(org.neo4j.causalclustering.core.consensus.vote.VoteState) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) FollowerStates(org.neo4j.causalclustering.core.consensus.roles.follower.FollowerStates) TermState(org.neo4j.causalclustering.core.consensus.term.TermState) Test(org.junit.Test)

Example 25 with InMemoryRaftLog

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

the class HeartbeatTest method shouldNotResultInCommitIfReferringToFutureEntries.

@Test
public void shouldNotResultInCommitIfReferringToFutureEntries() throws Exception {
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    RaftState state = raftState().myself(myself).entryLog(raftLog).build();
    long leaderTerm = state.term() + leaderTermDifference;
    raftLog.append(new RaftLogEntry(leaderTerm, content()));
    RaftMessages.Heartbeat heartbeat = heartbeat().from(leader).commitIndex(// The leader is talking about committing stuff we don't know about
    raftLog.appendIndex() + 1).commitIndexTerm(// And is in the same term
    leaderTerm).leaderTerm(leaderTerm).build();
    Outcome outcome = role.handler.handle(heartbeat, state, log());
    assertThat(outcome.getLogCommands(), empty());
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Aggregations

InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)30 Test (org.junit.Test)27 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)23 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)18 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)15 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)7 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)6 RaftTestGroup (org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup)5 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)4 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)4 TruncateLogCommand (org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand)4 ReplicatedString (org.neo4j.causalclustering.core.consensus.ReplicatedString)3 RaftLogCommand (org.neo4j.causalclustering.core.consensus.outcome.RaftLogCommand)3 ReadableRaftState (org.neo4j.causalclustering.core.consensus.state.ReadableRaftState)3 Before (org.junit.Before)2 RaftMachine (org.neo4j.causalclustering.core.consensus.RaftMachine)2 FollowerStates (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerStates)2 ExposedRaftState (org.neo4j.causalclustering.core.consensus.state.ExposedRaftState)2 TermState (org.neo4j.causalclustering.core.consensus.term.TermState)2 VoteState (org.neo4j.causalclustering.core.consensus.vote.VoteState)2