Search in sources :

Example 1 with TermState

use of org.neo4j.causalclustering.core.consensus.term.TermState in project neo4j by neo4j.

the class RaftStateBuilder method build.

public RaftState build() throws IOException {
    StateStorage<TermState> termStore = new InMemoryStateStorage<>(new TermState());
    StateStorage<VoteState> voteStore = new InMemoryStateStorage<>(new VoteState());
    StubMembership membership = new StubMembership(votingMembers, replicationMembers);
    RaftState state = new RaftState(myself, termStore, membership, entryLog, voteStore, new InFlightMap<>(), NullLogProvider.getInstance());
    Collection<RaftMessages.Directed> noMessages = Collections.emptyList();
    List<RaftLogCommand> noLogCommands = Collections.emptyList();
    state.update(new Outcome(null, term, leader, leaderCommit, votedFor, votesForMe, lastLogIndexBeforeWeBecameLeader, followerStates, false, noLogCommands, noMessages, emptySet(), commitIndex, emptySet()));
    return state;
}
Also used : RaftLogCommand(org.neo4j.causalclustering.core.consensus.outcome.RaftLogCommand) InMemoryStateStorage(org.neo4j.causalclustering.core.state.storage.InMemoryStateStorage) VoteState(org.neo4j.causalclustering.core.consensus.vote.VoteState) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) TermState(org.neo4j.causalclustering.core.consensus.term.TermState)

Example 2 with TermState

use of org.neo4j.causalclustering.core.consensus.term.TermState 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 3 with TermState

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

Aggregations

Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)3 TermState (org.neo4j.causalclustering.core.consensus.term.TermState)3 VoteState (org.neo4j.causalclustering.core.consensus.vote.VoteState)3 Test (org.junit.Test)2 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)2 RaftLogCommand (org.neo4j.causalclustering.core.consensus.outcome.RaftLogCommand)2 LinkedList (java.util.LinkedList)1 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)1 InFlightMap (org.neo4j.causalclustering.core.consensus.log.segmented.InFlightMap)1 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)1 TruncateLogCommand (org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand)1 FollowerStates (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerStates)1 InMemoryStateStorage (org.neo4j.causalclustering.core.state.storage.InMemoryStateStorage)1