Search in sources :

Example 6 with AppendLogEntry

use of org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry in project neo4j by neo4j.

the class CandidateTest method shouldBeElectedLeaderOnReceivingGrantedVoteResponseWithCurrentTerm.

@Test
public void shouldBeElectedLeaderOnReceivingGrantedVoteResponseWithCurrentTerm() throws Exception {
    // given
    RaftState state = RaftStateBuilder.raftState().term(1).myself(myself).votingMembers(member1, member2).replicationMembers(member1, member2).build();
    // when
    Outcome outcome = CANDIDATE.handler.handle(voteResponse().term(state.term()).from(member1).grant().build(), state, log());
    // then
    assertEquals(LEADER, outcome.getRole());
    assertEquals(true, outcome.electionTimeoutRenewed());
    assertThat(outcome.getLogCommands(), hasItem(new AppendLogEntry(0, new RaftLogEntry(state.term(), new NewLeaderBarrier()))));
    assertThat(outcome.getOutgoingMessages(), hasItems(new RaftMessages.Directed(member1, new RaftMessages.Heartbeat(myself, state.term(), -1, -1)), new RaftMessages.Directed(member2, new RaftMessages.Heartbeat(myself, state.term(), -1, -1))));
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) NewLeaderBarrier(org.neo4j.causalclustering.core.consensus.NewLeaderBarrier) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 7 with AppendLogEntry

use of org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry 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)

Aggregations

RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)7 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)7 Test (org.junit.Test)6 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)4 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)3 RaftLogCommand (org.neo4j.causalclustering.core.consensus.outcome.RaftLogCommand)3 TruncateLogCommand (org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand)3 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)2 ShipCommand (org.neo4j.causalclustering.core.consensus.outcome.ShipCommand)2 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)2 LinkedList (java.util.LinkedList)1 NewLeaderBarrier (org.neo4j.causalclustering.core.consensus.NewLeaderBarrier)1 InFlightMap (org.neo4j.causalclustering.core.consensus.log.segmented.InFlightMap)1 ReadableRaftState (org.neo4j.causalclustering.core.consensus.state.ReadableRaftState)1 TermState (org.neo4j.causalclustering.core.consensus.term.TermState)1 VoteState (org.neo4j.causalclustering.core.consensus.vote.VoteState)1