Search in sources :

Example 1 with RaftMachineBuilder

use of org.neo4j.causalclustering.core.consensus.RaftMachineBuilder in project neo4j by neo4j.

the class AppendEntriesMessageFlowTest method setup.

@Before
public void setup() throws IOException {
    // given
    RaftLog raftLog = new InMemoryRaftLog();
    raftLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
    raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).raftLog(raftLog).outbound(outbound).build();
}
Also used : RaftMachineBuilder(org.neo4j.causalclustering.core.consensus.RaftMachineBuilder) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftTestGroup(org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Before(org.junit.Before)

Example 2 with RaftMachineBuilder

use of org.neo4j.causalclustering.core.consensus.RaftMachineBuilder in project neo4j by neo4j.

the class ElectionTest method candidateShouldLoseElectionAndRemainCandidate.

@Test
public void candidateShouldLoseElectionAndRemainCandidate() throws Exception {
    // Note the etcd implementation seems to diverge from the paper here, since the paper suggests that it should
    // remain as a candidate
    // given
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).outbound(outbound).timeoutService(timeouts).clock(fakeClock).build();
    raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
    raft.startTimers();
    timeouts.invokeTimeout(RaftMachine.Timeouts.ELECTION);
    // when
    raft.handle(voteResponse().from(member1).term(1).deny().build());
    raft.handle(voteResponse().from(member2).term(1).deny().build());
    // then
    assertEquals(1, raft.term());
    assertEquals(CANDIDATE, raft.currentRole());
    verify(outbound, never()).send(eq(member1), isA(RaftMessages.AppendEntries.Request.class));
    verify(outbound, never()).send(eq(member2), isA(RaftMessages.AppendEntries.Request.class));
}
Also used : RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) RaftMachineBuilder(org.neo4j.causalclustering.core.consensus.RaftMachineBuilder) MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) FakeClock(org.neo4j.time.FakeClock) TestMessageBuilders.voteRequest(org.neo4j.causalclustering.core.consensus.TestMessageBuilders.voteRequest) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) ControlledRenewableTimeoutService(org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService) Test(org.junit.Test)

Example 3 with RaftMachineBuilder

use of org.neo4j.causalclustering.core.consensus.RaftMachineBuilder in project neo4j by neo4j.

the class ElectionTest method candidateShouldWinElectionAndBecomeLeader.

@Test
public void candidateShouldWinElectionAndBecomeLeader() throws Exception {
    // given
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).outbound(outbound).timeoutService(timeouts).clock(fakeClock).build();
    raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
    raft.startTimers();
    timeouts.invokeTimeout(RaftMachine.Timeouts.ELECTION);
    // when
    raft.handle(voteResponse().from(member1).term(1).grant().build());
    raft.handle(voteResponse().from(member2).term(1).grant().build());
    // then
    assertEquals(1, raft.term());
    assertEquals(LEADER, raft.currentRole());
    verify(outbound).send(eq(member1), isA(RaftMessages.AppendEntries.Request.class));
    verify(outbound).send(eq(member2), isA(RaftMessages.AppendEntries.Request.class));
}
Also used : RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) RaftMachineBuilder(org.neo4j.causalclustering.core.consensus.RaftMachineBuilder) MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) FakeClock(org.neo4j.time.FakeClock) TestMessageBuilders.voteRequest(org.neo4j.causalclustering.core.consensus.TestMessageBuilders.voteRequest) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) ControlledRenewableTimeoutService(org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService) Test(org.junit.Test)

Example 4 with RaftMachineBuilder

use of org.neo4j.causalclustering.core.consensus.RaftMachineBuilder in project neo4j by neo4j.

the class ElectionTest method candidateShouldVoteForTheSameCandidateInTheSameTerm.

@Test
public void candidateShouldVoteForTheSameCandidateInTheSameTerm() throws Exception {
    // given
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).outbound(outbound).timeoutService(timeouts).clock(fakeClock).build();
    raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
    // when
    raft.handle(voteRequest().from(member1).candidate(member1).term(1).build());
    raft.handle(voteRequest().from(member1).candidate(member1).term(1).build());
    // then
    verify(outbound, times(2)).send(member1, voteResponse().term(1).grant().build());
}
Also used : RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) RaftMachineBuilder(org.neo4j.causalclustering.core.consensus.RaftMachineBuilder) MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) FakeClock(org.neo4j.time.FakeClock) ControlledRenewableTimeoutService(org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService) Test(org.junit.Test)

Aggregations

RaftMachineBuilder (org.neo4j.causalclustering.core.consensus.RaftMachineBuilder)4 Test (org.junit.Test)3 RaftMachine (org.neo4j.causalclustering.core.consensus.RaftMachine)3 MembershipEntry (org.neo4j.causalclustering.core.consensus.membership.MembershipEntry)3 ControlledRenewableTimeoutService (org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService)3 RaftCoreState (org.neo4j.causalclustering.core.state.snapshot.RaftCoreState)3 FakeClock (org.neo4j.time.FakeClock)3 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)2 TestMessageBuilders.voteRequest (org.neo4j.causalclustering.core.consensus.TestMessageBuilders.voteRequest)2 Before (org.junit.Before)1 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)1 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)1 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)1 RaftTestGroup (org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup)1