Search in sources :

Example 6 with RaftMachine

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

the class MembershipWaiterTest method shouldReturnImmediatelyIfMemberAndCaughtUp.

@Test
public void shouldReturnImmediatelyIfMemberAndCaughtUp() throws Exception {
    OnDemandJobScheduler jobScheduler = new OnDemandJobScheduler();
    MembershipWaiter waiter = new MembershipWaiter(member(0), jobScheduler, () -> dbHealth, 500, NullLogProvider.getInstance());
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    raftLog.append(new RaftLogEntry(0, valueOf(0)));
    ExposedRaftState raftState = RaftStateBuilder.raftState().votingMembers(member(0)).leaderCommit(0).entryLog(raftLog).commitIndex(0L).build().copy();
    RaftMachine raft = mock(RaftMachine.class);
    when(raft.state()).thenReturn(raftState);
    CompletableFuture<Boolean> future = waiter.waitUntilCaughtUpMember(raft);
    jobScheduler.runJob();
    jobScheduler.runJob();
    future.get(0, NANOSECONDS);
}
Also used : RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) ExposedRaftState(org.neo4j.causalclustering.core.consensus.state.ExposedRaftState) OnDemandJobScheduler(org.neo4j.test.OnDemandJobScheduler) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 7 with RaftMachine

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

the class MembershipWaiterTest method shouldTimeoutIfCaughtUpButNotMember.

@Test
public void shouldTimeoutIfCaughtUpButNotMember() throws Exception {
    OnDemandJobScheduler jobScheduler = new OnDemandJobScheduler();
    MembershipWaiter waiter = new MembershipWaiter(member(0), jobScheduler, () -> dbHealth, 1, NullLogProvider.getInstance());
    ExposedRaftState raftState = RaftStateBuilder.raftState().votingMembers(member(1)).leaderCommit(0).build().copy();
    RaftMachine raft = mock(RaftMachine.class);
    when(raft.state()).thenReturn(raftState);
    CompletableFuture<Boolean> future = waiter.waitUntilCaughtUpMember(raft);
    jobScheduler.runJob();
    jobScheduler.runJob();
    try {
        future.get(10, MILLISECONDS);
        fail("Should have timed out.");
    } catch (TimeoutException e) {
    // expected
    }
}
Also used : RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) ExposedRaftState(org.neo4j.causalclustering.core.consensus.state.ExposedRaftState) OnDemandJobScheduler(org.neo4j.test.OnDemandJobScheduler) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 8 with RaftMachine

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

the class MembershipWaiterTest method shouldTimeoutIfLeaderCommitIsNeverKnown.

@Test
public void shouldTimeoutIfLeaderCommitIsNeverKnown() throws Exception {
    OnDemandJobScheduler jobScheduler = new OnDemandJobScheduler();
    MembershipWaiter waiter = new MembershipWaiter(member(0), jobScheduler, () -> dbHealth, 1, NullLogProvider.getInstance());
    ExposedRaftState raftState = RaftStateBuilder.raftState().leaderCommit(-1).build().copy();
    RaftMachine raft = mock(RaftMachine.class);
    when(raft.state()).thenReturn(raftState);
    CompletableFuture<Boolean> future = waiter.waitUntilCaughtUpMember(raft);
    jobScheduler.runJob();
    try {
        future.get(10, MILLISECONDS);
        fail("Should have timed out.");
    } catch (TimeoutException e) {
    // expected
    }
}
Also used : RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) ExposedRaftState(org.neo4j.causalclustering.core.consensus.state.ExposedRaftState) OnDemandJobScheduler(org.neo4j.test.OnDemandJobScheduler) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 9 with RaftMachine

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

the class MembershipWaiterTest method shouldTimeoutIfMemberButNotCaughtUp.

@Test
public void shouldTimeoutIfMemberButNotCaughtUp() throws Exception {
    OnDemandJobScheduler jobScheduler = new OnDemandJobScheduler();
    MembershipWaiter waiter = new MembershipWaiter(member(0), jobScheduler, () -> dbHealth, 1, NullLogProvider.getInstance());
    ExposedRaftState raftState = RaftStateBuilder.raftState().votingMembers(member(0), member(1)).leaderCommit(0).build().copy();
    RaftMachine raft = mock(RaftMachine.class);
    when(raft.state()).thenReturn(raftState);
    CompletableFuture<Boolean> future = waiter.waitUntilCaughtUpMember(raft);
    jobScheduler.runJob();
    jobScheduler.runJob();
    try {
        future.get(10, MILLISECONDS);
        fail("Should have timed out.");
    } catch (TimeoutException e) {
    // expected
    }
}
Also used : RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) ExposedRaftState(org.neo4j.causalclustering.core.consensus.state.ExposedRaftState) OnDemandJobScheduler(org.neo4j.test.OnDemandJobScheduler) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 10 with RaftMachine

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

Aggregations

RaftMachine (org.neo4j.causalclustering.core.consensus.RaftMachine)12 Test (org.junit.Test)10 ExposedRaftState (org.neo4j.causalclustering.core.consensus.state.ExposedRaftState)5 OnDemandJobScheduler (org.neo4j.test.OnDemandJobScheduler)5 TimeoutException (java.util.concurrent.TimeoutException)4 RaftMachineBuilder (org.neo4j.causalclustering.core.consensus.RaftMachineBuilder)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 ArrayList (java.util.ArrayList)2 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)2 TestMessageBuilders.voteRequest (org.neo4j.causalclustering.core.consensus.TestMessageBuilders.voteRequest)2 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)2 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)2 MemberId (org.neo4j.causalclustering.identity.MemberId)2 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)2 HashMap (java.util.HashMap)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1