Search in sources :

Example 1 with ControlledRenewableTimeoutService

use of org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService in project neo4j by neo4j.

the class RaftMachineTest method shouldVoteFalseForCandidateInOldTerm.

@Test
public void shouldVoteFalseForCandidateInOldTerm() throws Exception {
    // Given
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    OutboundMessageCollector messages = new OutboundMessageCollector();
    RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).clock(fakeClock).outbound(messages).build();
    raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
    raft.startTimers();
    // When
    raft.handle(voteRequest().from(member1).term(-1).candidate(member1).lastLogIndex(0).lastLogTerm(-1).build());
    // Then
    assertThat(messages.sentTo(member1).size(), equalTo(1));
    assertThat(messages.sentTo(member1), hasItem(voteResponse().from(myself).term(0).deny().build()));
}
Also used : 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)

Example 2 with ControlledRenewableTimeoutService

use of org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService in project neo4j by neo4j.

the class RaftMachineTest method shouldNotBecomeLeaderWhenVotingOnItself.

@Test
public void shouldNotBecomeLeaderWhenVotingOnItself() throws Exception {
    // Given
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).clock(fakeClock).build();
    raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
    raft.startTimers();
    timeouts.invokeTimeout(ELECTION);
    // When
    raft.handle(voteResponse().from(myself).term(1).grant().build());
    // Then
    assertThat(raft.isLeader(), is(false));
}
Also used : 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)

Example 3 with ControlledRenewableTimeoutService

use of org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService in project neo4j by neo4j.

the class RaftMachineTest method shouldAppendNewLeaderBarrierAfterBecomingLeader.

@Test
public void shouldAppendNewLeaderBarrierAfterBecomingLeader() throws Exception {
    // Given
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    OutboundMessageCollector messages = new OutboundMessageCollector();
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).clock(fakeClock).outbound(messages).raftLog(raftLog).build();
    raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
    raft.startTimers();
    // When
    timeouts.invokeTimeout(ELECTION);
    raft.handle(voteResponse().from(member1).term(1).grant().build());
    // Then
    assertEquals(new NewLeaderBarrier(), readLogEntry(raftLog, raftLog.appendIndex()).content());
}
Also used : MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) 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)

Example 4 with ControlledRenewableTimeoutService

use of org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService in project neo4j by neo4j.

the class RaftMachineTest method shouldThrowExceptionIfReceivesClientRequestWithNoLeaderElected.

@Test
public void shouldThrowExceptionIfReceivesClientRequestWithNoLeaderElected() throws Exception {
    // Given
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).clock(fakeClock).build();
    raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
    raft.startTimers();
    try {
        // When
        // There is no leader
        raft.getLeader();
        fail("Should have thrown exception");
    }// Then
     catch (NoLeaderFoundException e) {
    // expected
    }
}
Also used : 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)

Example 5 with ControlledRenewableTimeoutService

use of org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService in project neo4j by neo4j.

the class RaftMachineTest method shouldNotBecomeLeaderOnMultipleVotesFromSameMember.

@Test
public void shouldNotBecomeLeaderOnMultipleVotesFromSameMember() throws Exception {
    // Given
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).clock(fakeClock).build();
    raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2, member3, member4))));
    raft.startTimers();
    timeouts.invokeTimeout(ELECTION);
    // When
    raft.handle(voteResponse().from(member1).term(1).grant().build());
    raft.handle(voteResponse().from(member1).term(1).grant().build());
    // Then
    assertThat(raft.isLeader(), is(false));
}
Also used : 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

Test (org.junit.Test)18 ControlledRenewableTimeoutService (org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService)18 MembershipEntry (org.neo4j.causalclustering.core.consensus.membership.MembershipEntry)17 RaftCoreState (org.neo4j.causalclustering.core.state.snapshot.RaftCoreState)17 FakeClock (org.neo4j.time.FakeClock)17 RaftMachine (org.neo4j.causalclustering.core.consensus.RaftMachine)3 RaftMachineBuilder (org.neo4j.causalclustering.core.consensus.RaftMachineBuilder)3 TestMessageBuilders.voteRequest (org.neo4j.causalclustering.core.consensus.TestMessageBuilders.voteRequest)3 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)2 TestMessageBuilders.appendEntriesRequest (org.neo4j.causalclustering.core.consensus.TestMessageBuilders.appendEntriesRequest)1 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)1 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)1 MemberIdSet (org.neo4j.causalclustering.core.consensus.membership.MemberIdSet)1 MemberId (org.neo4j.causalclustering.identity.MemberId)1 Inbound (org.neo4j.causalclustering.messaging.Inbound)1 Monitors (org.neo4j.kernel.monitoring.Monitors)1